upgrade rust-toolchain to nightly-2021-09-13 and bump version to 1.1.4
diff --git a/buildenv.mk b/buildenv.mk
index 0fa6a39..2df01b4 100644
--- a/buildenv.mk
+++ b/buildenv.mk
@@ -17,18 +17,54 @@
 #
 #
 
+# -----------------------------------------------------------------------------
+# Function : parent-dir
+# Arguments: 1: path
+# Returns  : Parent dir or path of $1, with final separator removed.
+# -----------------------------------------------------------------------------
+parent-dir = $(patsubst %/,%,$(dir $(1:%/=%)))
+
+# -----------------------------------------------------------------------------
+# Macro    : my-dir
+# Returns  : the directory of the current Makefile
+# Usage    : $(my-dir)
+# -----------------------------------------------------------------------------
+my-dir = $(realpath $(call parent-dir,$(lastword $(MAKEFILE_LIST))))
+
+
+ROOT_DIR              := $(call my-dir)
+ifneq ($(words $(subst :, ,$(ROOT_DIR))), 1)
+  $(error main directory cannot contain spaces nor colons)
+endif
+
+COMMON_DIR            := $(ROOT_DIR)/common
+
 CP    := /bin/cp -f
 MKDIR := mkdir -p
 STRIP := strip
 OBJCOPY := objcopy
+CC ?= gcc
 
 # clean the content of 'INCLUDE' - this variable will be set by vcvars32.bat
 # thus it will cause build error when this variable is used by our Makefile,
 # when compiling the code under Cygwin tainted by MSVC environment settings.
 INCLUDE :=
 
+# this will return the path to the file that included the buildenv.mk file
+CUR_DIR := $(realpath $(call parent-dir,$(lastword $(wordlist 2,$(words $(MAKEFILE_LIST)),x $(MAKEFILE_LIST)))))
+
+CC_VERSION := $(shell $(CC) -dumpversion)
+CC_VERSION_MAJOR := $(shell echo $(CC_VERSION) | cut -f1 -d.)
+CC_VERSION_MINOR := $(shell echo $(CC_VERSION) | cut -f2 -d.)
+CC_BELOW_4_9 := $(shell [ $(CC_VERSION_MAJOR) -lt 4 -o \( $(CC_VERSION_MAJOR) -eq 4 -a $(CC_VERSION_MINOR) -le 9 \) ] && echo 1)
+CC_BELOW_5_2 := $(shell [ $(CC_VERSION_MAJOR) -lt 5 -o \( $(CC_VERSION_MAJOR) -eq 5 -a $(CC_VERSION_MINOR) -le 2 \) ] && echo 1)
+
 # turn on stack protector for SDK
-COMMON_FLAGS += -fstack-protector
+ifeq ($(CC_BELOW_4_9), 1)
+    COMMON_FLAGS += -fstack-protector
+else
+    COMMON_FLAGS += -fstack-protector-strong
+endif
 
 ifdef DEBUG
     COMMON_FLAGS += -O0 -g -DDEBUG -UNDEBUG
@@ -123,13 +159,15 @@
     MITIGATION_LIB_PATH := cve_2020_0551_cf
 endif
 
-MITIGATION_CFLAGS :=
-MITIGATION_ASFLAGS :=
 ifeq ($(MITIGATION_C), 1)
 ifeq ($(MITIGATION_INDIRECT), 1)
     MITIGATION_CFLAGS += -mindirect-branch-register
 endif
 ifeq ($(MITIGATION_RET), 1)
+CC_NO_LESS_THAN_8 := $(shell expr $(CC_VERSION) \>\= "8")
+ifeq ($(CC_NO_LESS_THAN_8), 1)
+    MITIGATION_CFLAGS += -fcf-protection=none
+endif
     MITIGATION_CFLAGS += -mfunction-return=thunk-extern
 endif
 endif
@@ -137,12 +175,12 @@
 ifeq ($(MITIGATION_ASM), 1)
     MITIGATION_ASFLAGS += -fno-plt
 ifeq ($(MITIGATION_AFTERLOAD), 1)
-    MITIGATION_ASFLAGS += -Wa,-mlfence-after-load=yes
+    MITIGATION_ASFLAGS += -Wa,-mlfence-after-load=yes -Wa,-mlfence-before-indirect-branch=memory
 else
-    MITIGATION_ASFLAGS += -Wa,-mlfence-before-indirect-branch=register
+    MITIGATION_ASFLAGS += -Wa,-mlfence-before-indirect-branch=all
 endif
 ifeq ($(MITIGATION_RET), 1)
-    MITIGATION_ASFLAGS += -Wa,-mlfence-before-ret=not
+    MITIGATION_ASFLAGS += -Wa,-mlfence-before-ret=shl
 endif
 endif
 
diff --git a/dockerfile/Dockerfile.1604.nightly b/dockerfile/Dockerfile.1604.nightly
index 037b5e7..167a47f 100644
--- a/dockerfile/Dockerfile.1604.nightly
+++ b/dockerfile/Dockerfile.1604.nightly
@@ -39,7 +39,7 @@
 
 # Seventh, Rust
 
-ENV rust_toolchain  nightly-2020-10-25
+ENV rust_toolchain  nightly-2021-09-13
 ADD 05_rust.sh /root
 RUN bash /root/05_rust.sh
 
diff --git a/dockerfile/Dockerfile.1804.nightly b/dockerfile/Dockerfile.1804.nightly
index 006b4c0..a04bca1 100644
--- a/dockerfile/Dockerfile.1804.nightly
+++ b/dockerfile/Dockerfile.1804.nightly
@@ -31,7 +31,7 @@
 
 # Seventh, Rust
 
-ENV rust_toolchain  nightly-2020-10-25
+ENV rust_toolchain  nightly-2021-09-13
 ADD 05_rust.sh /root
 RUN bash /root/05_rust.sh
 
diff --git a/dockerfile/Dockerfile.2004.nightly b/dockerfile/Dockerfile.2004.nightly
index 1a8c16d..8776a50 100644
--- a/dockerfile/Dockerfile.2004.nightly
+++ b/dockerfile/Dockerfile.2004.nightly
@@ -34,7 +34,7 @@
 
 # Seventh, Rust
 
-ENV rust_toolchain  nightly-2020-10-25
+ENV rust_toolchain  nightly-2021-09-13
 ADD 05_rust.sh /root
 RUN bash /root/05_rust.sh
 
diff --git a/dockerfile/Dockerfile.centos8.nightly b/dockerfile/Dockerfile.centos8.nightly
index 4e1e8b4..6ad9b7e 100644
--- a/dockerfile/Dockerfile.centos8.nightly
+++ b/dockerfile/Dockerfile.centos8.nightly
@@ -22,7 +22,7 @@
 ADD 04_psw_rpm.sh /root
 RUN bash /root/04_psw_rpm.sh
 
-ENV rust_toolchain  nightly-2020-10-25
+ENV rust_toolchain  nightly-2021-09-13
 ADD 05_rust.sh /root
 RUN bash /root/05_rust.sh
 
diff --git a/dockerfile/Dockerfile.fedora31.nightly b/dockerfile/Dockerfile.fedora31.nightly
index 5181966..1d58f1d 100644
--- a/dockerfile/Dockerfile.fedora31.nightly
+++ b/dockerfile/Dockerfile.fedora31.nightly
@@ -28,7 +28,7 @@
 
 # Seventh, Rust
 
-ENV rust_toolchain  nightly-2020-10-25
+ENV rust_toolchain  nightly-2021-09-13
 ADD 05_rust.sh /root
 RUN bash /root/05_rust.sh
 
diff --git a/edl/sgx_file.edl b/edl/sgx_file.edl
index 1e28036..07fb1eb 100644
--- a/edl/sgx_file.edl
+++ b/edl/sgx_file.edl
@@ -46,6 +46,7 @@
         int u_fchmod_ocall([out] int *error, int fd, uint32_t mode);
         int u_unlink_ocall([out] int *error, [in, string] const char *pathname);
         int u_link_ocall([out] int *error, [in, string] const char *oldpath, [in, string] const char *newpath);
+        int u_linkat_ocall([out] int *error, int olddirfd, [in, string] const char *oldpath, int newdirfd, [in, string] const char *newpath, int flags);
         int u_rename_ocall([out] int *error, [in, string] const char *oldpath, [in, string] const char *newpath);
         int u_chmod_ocall([out] int *error, [in, string] const char *path, uint32_t mode);
         size_t u_readlink_ocall([out] int *error, [in, string] const char *path, [out, size=bufsz] char *buf, size_t bufsz);
diff --git a/edl/sgx_socket.edl b/edl/sgx_socket.edl
index 68ce3b6..6fc8ff7 100644
--- a/edl/sgx_socket.edl
+++ b/edl/sgx_socket.edl
@@ -54,7 +54,18 @@
                                 [out, size=addrlen_in] struct sockaddr *src_addr,
                                 socklen_t addrlen_in,
                                 [out] socklen_t *addrlen_out);
-        size_t u_recvmsg_ocall([out] int *error, int sockfd, [in, out] struct msghdr *msg, int flags);
+        size_t u_recvmsg_ocall([out] int *error,
+                               int sockfd,
+                               [out, size=msg_namelen] void *msg_name,
+                               socklen_t msg_namelen,
+                               [out] socklen_t* msg_namelen_out,
+                               [in, count=msg_iovlen] struct iovec* msg_iov,
+                               size_t msg_iovlen,
+                               [out, size=msg_controllen] void *msg_control,
+                               size_t msg_controllen,
+                               [out] size_t* msg_controllen_out,
+                               [out] int* msg_flags,
+                               int flags);
         size_t u_send_ocall([out] int *error, int sockfd, [user_check] const void *buf, size_t len, int flags);
         size_t u_sendto_ocall([out] int *error,
                               int sockfd,
@@ -63,7 +74,15 @@
                               int flags,
                               [in, size=addrlen] const struct sockaddr *dest_addr,
                               socklen_t addrlen);
-        size_t u_sendmsg_ocall([out] int *error, int sockfd, [in] const struct msghdr *msg, int flags);
+        size_t u_sendmsg_ocall([out] int *error,
+                               int sockfd,
+                               [in, size=msg_namelen] const void* msg_name,
+                               socklen_t msg_namelen,
+                               [in, count=msg_iovlen] const struct iovec* msg_iov,
+                               size_t msg_iovlen,
+                               [in, size=msg_controllen] const void* msg_control,
+                               size_t msg_controllen,
+                               int flags);
         int u_getsockopt_ocall([out] int *error,
                                int sockfd,
                                int level,
diff --git a/edl/sgx_sys.edl b/edl/sgx_sys.edl
index f5f3d2c..bc74b96 100644
--- a/edl/sgx_sys.edl
+++ b/edl/sgx_sys.edl
@@ -26,7 +26,7 @@
     untrusted {
         long u_sysconf_ocall([out] int *error, int name);
         int u_prctl_ocall([out] int *error, int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
-        int u_sched_setaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [in, size=cpusetsize]cpu_set_t *mask);
-        int u_sched_getaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [out, size=cpusetsize]cpu_set_t *mask);
+        int u_sched_setaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [in, size=cpusetsize] cpu_set_t *mask);
+        int u_sched_getaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [out, size=cpusetsize] cpu_set_t *mask);
     };
 };
diff --git a/rust-toolchain b/rust-toolchain
index 148ed93..7b9ce47 100644
--- a/rust-toolchain
+++ b/rust-toolchain
@@ -1 +1 @@
-nightly-2020-10-25
+nightly-2021-09-13
diff --git a/samplecode/thread/enclave/src/lib.rs b/samplecode/thread/enclave/src/lib.rs
index 8ae423c..7b7f1bd 100644
--- a/samplecode/thread/enclave/src/lib.rs
+++ b/samplecode/thread/enclave/src/lib.rs
@@ -53,40 +53,41 @@
 
 #[no_mangle]
 pub extern "C" fn ecall_initialize() {
-
-    let lock = Box::new((SgxMutex::<CondBuffer>::new(CondBuffer::default()), SgxCondvar::new(), SgxCondvar::new()));
+    let lock = Box::new((
+        SgxMutex::<CondBuffer>::new(CondBuffer::default()),
+        SgxCondvar::new(),
+        SgxCondvar::new(),
+    ));
     let ptr = Box::into_raw(lock);
     GLOBAL_COND_BUFFER.store(ptr as *mut (), Ordering::SeqCst);
 }
 
 #[no_mangle]
 pub extern "C" fn ecall_uninitialize() {
-
-    let ptr = GLOBAL_COND_BUFFER.swap(0 as * mut (), Ordering::SeqCst) as * mut (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar);
+    let ptr = GLOBAL_COND_BUFFER.swap(0 as *mut (), Ordering::SeqCst)
+        as *mut (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar);
     if ptr.is_null() {
-       return;
+        return;
     }
     let _ = unsafe { Box::from_raw(ptr) };
 }
 
-fn get_ref_cond_buffer() -> Option<&'static (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar)>
-{
-    let ptr = GLOBAL_COND_BUFFER.load(Ordering::SeqCst) as * mut (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar);
+fn get_ref_cond_buffer() -> Option<&'static (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar)> {
+    let ptr = GLOBAL_COND_BUFFER.load(Ordering::SeqCst)
+        as *mut (SgxMutex<CondBuffer>, SgxCondvar, SgxCondvar);
     if ptr.is_null() {
         None
     } else {
-        Some(unsafe { &* ptr })
+        Some(unsafe { &*ptr })
     }
 }
 
 #[no_mangle]
 pub extern "C" fn ecall_producer() {
-
     let max_index = 4 * LOOPS_PER_THREAD;
     let &(ref mutex, ref more, ref less) = get_ref_cond_buffer().unwrap();
 
     for _ in 0..max_index {
-
         let mut guard = mutex.lock().unwrap();
 
         while guard.occupied >= BUFFER_SIZE as i32 {
@@ -105,12 +106,10 @@
 
 #[no_mangle]
 pub extern "C" fn ecall_consumer() {
-
     let max_index = 4 * LOOPS_PER_THREAD;
     let &(ref mutex, ref more, ref less) = get_ref_cond_buffer().unwrap();
 
     for _ in 0..max_index {
-
         let mut guard = mutex.lock().unwrap();
 
         while guard.occupied <= 0 {
diff --git a/samplecode/unit-test/app/Cargo.toml b/samplecode/unit-test/app/Cargo.toml
index 7428c18..ffe9cb8 100644
--- a/samplecode/unit-test/app/Cargo.toml
+++ b/samplecode/unit-test/app/Cargo.toml
@@ -6,7 +6,7 @@
 
 [dependencies]
 sgx_types = { git = "https://github.com/apache/teaclave-sgx-sdk.git" }
-sgx_urts = { git = "https://github.com/apache/teaclave-sgx-sdk.git",  features = ["global_init", "signal"] }
+sgx_urts = { git = "https://github.com/apache/teaclave-sgx-sdk.git",  features = ["global_init"] }
 
 [patch.'https://github.com/apache/teaclave-sgx-sdk.git']
 sgx_types = { path = "../../../sgx_types" }
diff --git a/samplecode/unit-test/enclave/build.rs b/samplecode/unit-test/enclave/build.rs
index 1839008..1b816d8 100644
--- a/samplecode/unit-test/enclave/build.rs
+++ b/samplecode/unit-test/enclave/build.rs
@@ -1,14 +1,13 @@
 use std::env;
 
-fn main () {
-    let is_sim = env::var("SGX_MODE")
-                    .unwrap_or_else(|_| "HW".to_string());
+fn main() {
+    let is_sim = env::var("SGX_MODE").unwrap_or_else(|_| "HW".to_string());
 
     match is_sim.as_ref() {
-        "SW" => {
-        },
-        _    => { // HW by default
+        "SW" => {}
+        _ => {
+            // HW by default
             println!("cargo:rustc-cfg=feature=\"hw_test\"");
-        },
+        }
     }
 }
diff --git a/samplecode/unit-test/enclave/src/lib.rs b/samplecode/unit-test/enclave/src/lib.rs
index e010cd5..0ff6d16 100644
--- a/samplecode/unit-test/enclave/src/lib.rs
+++ b/samplecode/unit-test/enclave/src/lib.rs
@@ -17,7 +17,6 @@
 
 #![crate_name = "unittestsampleenclave"]
 #![crate_type = "staticlib"]
-
 #![feature(box_syntax)]
 #![feature(core_intrinsics)]
 #![cfg_attr(not(target_env = "sgx"), no_std)]
@@ -33,27 +32,27 @@
 extern crate sgx_tcrypto;
 #[macro_use]
 extern crate sgx_tunittest;
-extern crate sgx_trts;
-extern crate sgx_rand;
-extern crate sgx_tseal;
-extern crate sgx_alloc;
-extern crate sgx_align_struct_attribute;
 extern crate rand;
+extern crate sgx_align_struct_attribute;
+extern crate sgx_alloc;
+extern crate sgx_rand;
+extern crate sgx_trts;
+extern crate sgx_tseal;
 #[macro_use]
 extern crate memoffset;
 extern crate sgx_serialize;
 pub use sgx_serialize::*;
 #[macro_use]
 extern crate sgx_serialize_derive;
-extern crate sgx_signal;
 extern crate sgx_libc;
+extern crate sgx_signal;
 
 pub use sgx_serialize::*;
-use sgx_types::*;
 use sgx_tunittest::*;
+use sgx_types::*;
 
-use std::vec::Vec;
 use std::string::String;
+use std::vec::Vec;
 
 mod utils;
 
@@ -104,247 +103,251 @@
 
 mod test_alignstruct;
 
-
 mod test_signal;
 use test_signal::*;
 
 mod test_exception;
 use test_exception::*;
+
+mod test_fp;
+use test_fp::*;
+
 #[no_mangle]
-pub extern "C"
-fn test_main_entrance() -> size_t {
+pub extern "C" fn test_main_entrance() -> size_t {
     rsgx_unit_tests!(
-                    // tcrypto
-                    test_rsgx_sha256_slice,
-                    test_rsgx_sha256_handle,
-                    // assert
-                    foo_panic,
-                    foo_should,
-                    foo_assert,
-                    // rts::veh
-                    test_register_first_exception_handler,
-                    test_register_last_exception_handler,
-                    test_register_multiple_exception_handler,
-                    // rts::trts
-                    test_rsgx_get_thread_policy,
-                    test_trts_sizes,
-                    test_read_rand,
-                    test_data_is_within_enclave,
-                    test_slice_is_within_enlave,
-                    test_raw_is_within_enclave,
-                    test_data_is_outside_enclave,
-                    test_slice_is_outside_enclave,
-                    test_raw_is_outside_enclave,
-                    // rts::macros
-                    test_global_ctors_object,
-                    // rts::error
-                    test_error,
-                    // rts::libc
-                    test_rts_libc_memchr,
-                    test_rts_libc_memrchr,
-                    // rts::memchr
-                    test_rts_memchr_memchr,
-                    test_rts_memchr_memrchr,
-                    test_ascii,
-                    // rts::c_str
-                    test_cstr,
-                    // tseal
-                    test_seal_unseal,
-                    test_number_sealing,        // Thanks to @silvanegli
-                    test_array_sealing,         // Thanks to @silvanegli
-                    test_mac_aadata_slice,
-                    test_mac_aadata_number,
-                    // rand
-                    test_rand_os_sgxrng,
-                    test_rand_distributions,
-                    test_rand_isaac_isaacrng,
-                    test_rand_chacharng,
-                    test_rand_reseeding,
-                    // serialize
-                    test_serialize_base,
-                    test_serialize_struct,
-                    test_serialize_enum,
-                    // std::sgxfs
-                    test_sgxfs,
-                    // std::fs
-                    test_fs,
-                    // std::fs untrusted mode
-                    test_fs_untrusted_fs_feature_enabled,
-                    // std::time
-                    test_std_time,
-                    // rand
-                    test_rand_cratesio,
-                    // types
-                    check_metadata_size,
-                    check_version,
-                    // env
-                    test_env_vars_os,
-                    test_env_self_exe_path,
-                    test_env_current_dir,
-                    test_env_home_dir,
-                    //path
-                    test_path_stat_is_correct_on_is_dir,
-                    test_path_fileinfo_false_when_checking_is_file_on_a_directory,
-                    test_path_directoryinfo_check_exists_before_and_after_mkdir,
-                    test_path_directoryinfo_readdir,
-                    test_path_mkdir_path_already_exists_error,
-                    test_path_recursive_mkdir,
-                    test_path_recursive_mkdir_failure,
-                    test_path_recursive_mkdir_slash,
-                    test_path_recursive_mkdir_dot,
-                    test_path_recursive_mkdir_empty,
-                    test_path_recursive_rmdir,
-                    test_path_recursive_rmdir_of_symlink,
-                    test_path_unicode_path_is_dir,
-                    test_path_unicode_path_exists,
-                    test_path_copy_file_dst_dir,
-                    test_path_copy_file_src_dir,
-                    test_path_canonicalize_works_simple,
-                    test_path_dir_entry_methods,
-                    test_path_read_dir_not_found,
-                    test_path_mkdir_trailing_slash,
-                    test_path_create_dir_all_with_junctions,
-                    test_path_copy_file_follows_dst_symlink,
-                    // thread
-                    test_thread_unnamed_thread,
-                    test_thread_named_thread,
-                    test_thread_run_basic,
-                    test_thread_spawn_sched,
-                    test_thread_spawn_sched_childs_on_default_sched,
-                    test_thread_test_avoid_copying_the_body_spawn,
-                    test_thread_test_avoid_copying_the_body_thread_spawn,
-                    test_thread_test_avoid_copying_the_body_join,
-                    test_thread_invalid_named_thread,
-                    test_thread_join_panic,
-                    test_thread_child_doesnt_ref_parent,
-                    test_thread_simple_newsched_spawn,
-                    test_thread_try_panic_message_static_str,
-                    test_thread_try_panic_message_owned_str,
-                    test_thread_try_panic_message_any,
-                    test_thread_try_panic_message_unit_struct,
-                    test_thread_park_timeout_unpark_before,
-                    test_thread_park_timeout_unpark_not_called,
-                    test_thread_park_timeout_unpark_called_other_thread,
-                    test_thread_sleep_ms_smoke,
-                    test_thread_size_of_option_thread_id,
-                    test_thread_id_equal,
-                    test_thread_id_not_equal,
-                    //test mpsc
-                    test_mpsc_smoke,
-                    test_mpsc_drop_full,
-                    test_mpsc_drop_full_shared,
-                    test_mpsc_smoke_shared,
-                    test_mpsc_smoke_threads,
-                    test_mpsc_smoke_port_gone,
-                    test_mpsc_smoke_shared_port_gone,
-                    test_mpsc_smoke_shared_port_gone2,
-                    test_mpsc_port_gone_concurrent,
-                    test_mpsc_port_gone_concurrent_shared,
-                    test_mpsc_smoke_chan_gone,
-                    test_mpsc_smoke_chan_gone_shared,
-                    test_mpsc_chan_gone_concurrent,
-                    test_mpsc_stress,
-                    test_mpsc_stress_shared,
-                    test_mpsc_send_from_outside_runtime,
-                    test_mpsc_recv_from_outside_runtime,
-                    test_mpsc_no_runtime,
-                    test_mpsc_oneshot_single_thread_close_port_first,
-                    test_mpsc_oneshot_single_thread_close_chan_first,
-                    test_mpsc_oneshot_single_thread_send_port_close,
-                    //test_mpsc_oneshot_single_thread_recv_chan_close, //should panic
-                    test_mpsc_oneshot_single_thread_send_then_recv,
-                    test_mpsc_oneshot_single_thread_try_send_open,
-                    test_mpsc_oneshot_single_thread_try_send_closed,
-                    test_mpsc_oneshot_single_thread_try_recv_open,
-                    test_mpsc_oneshot_single_thread_try_recv_closed,
-                    test_mpsc_oneshot_single_thread_peek_data,
-                    test_mpsc_oneshot_single_thread_peek_close,
-                    test_mpsc_oneshot_single_thread_peek_open,
-                    test_mpsc_oneshot_multi_task_recv_then_send,
-                    //test_mpsc_oneshot_multi_task_recv_then_close, //should panic
-                    test_mpsc_oneshot_multi_thread_close_stress,
-                    //test_mpsc_oneshot_multi_thread_send_close_stress, //should panic
-                    //test_mpsc_oneshot_multi_thread_recv_close_stress, //should panic
-                    test_mpsc_oneshot_multi_thread_send_recv_stress,
-                    test_mpsc_stream_send_recv_stress,
-                    test_mpsc_oneshot_single_thread_recv_timeout,
-                    test_mpsc_stress_recv_timeout_two_threads,
-                    test_mpsc_recv_timeout_upgrade,
-                    test_mpsc_stress_recv_timeout_shared,
-                    test_mpsc_very_long_recv_timeout_wont_panic,
-                    test_mpsc_recv_a_lot,
-                    test_mpsc_shared_recv_timeout,
-                    test_mpsc_shared_chan_stress,
-                    test_mpsc_test_nested_recv_iter,
-                    test_mpsc_test_recv_iter_break,
-                    test_mpsc_test_recv_try_iter,
-                    test_mpsc_test_recv_into_iter_owned,
-                    test_mpsc_try_recv_states,
-                    test_mpsc_destroy_upgraded_shared_port_when_sender_still_active,
-                    test_mpsc_issue_32114,
-                    test_mpsc_sync_smoke,
-                    test_mpsc_sync_drop_full,
-                    test_mpsc_sync_smoke_shared,
-                    test_mpsc_sync_recv_timeout,
-                    test_mpsc_sync_smoke_threads,
-                    test_mpsc_sync_smoke_port_gone,
-                    test_mpsc_sync_smoke_shared_port_gone2,
-                    test_mpsc_sync_port_gone_concurrent,
-                    test_mpsc_sync_port_gone_concurrent_shared,
-                    test_mpsc_sync_smoke_chan_gone,
-                    test_mpsc_sync_smoke_chan_gone_shared,
-                    test_mpsc_sync_chan_gone_concurrent,
-                    test_mpsc_sync_stress,
-                    test_mpsc_sync_stress_recv_timeout_two_threads,
-                    test_mpsc_sync_stress_recv_timeout_shared,
-                    test_mpsc_sync_stress_shared,
-                    test_mpsc_sync_oneshot_single_thread_close_port_first,
-                    test_mpsc_sync_oneshot_single_thread_send_port_close,
-                    //test_mpsc_sync_oneshot_single_thread_recv_chan_close, //should panic
-                    test_mpsc_sync_oneshot_single_thread_send_then_recv,
-                    test_mpsc_sync_oneshot_single_thread_try_send_open,
-                    test_mpsc_sync_oneshot_single_thread_try_send_closed,
-                    test_mpsc_sync_oneshot_single_thread_try_send_closed2,
-                    test_mpsc_sync_oneshot_single_thread_try_recv_open,
-                    test_mpsc_sync_oneshot_single_thread_try_recv_closed,
-                    test_mpsc_sync_oneshot_single_thread_try_recv_closed_with_data,
-                    test_mpsc_sync_oneshot_single_thread_peek_data,
-                    test_mpsc_sync_oneshot_single_thread_peek_close,
-                    test_mpsc_sync_oneshot_single_thread_peek_open,
-                    test_mpsc_sync_oneshot_multi_task_recv_then_send,
-                    //test_mpsc_sync_oneshot_multi_task_recv_then_close, //should panic
-                    test_mpsc_sync_oneshot_multi_thread_close_stress,
-                    //test_mpsc_sync_oneshot_multi_thread_send_close_stress, //should panic
-                    //test_mpsc_sync_oneshot_multi_thread_recv_close_stress, //should panic
-                    test_mpsc_sync_oneshot_multi_thread_send_recv_stress,
-                    test_mpsc_sync_stream_send_recv_stress,
-                    test_mpsc_sync_recv_a_lot,
-                    test_mpsc_sync_shared_sync_chan_stress,
-                    test_mpsc_sync_test_nested_recv_iter,
-                    test_mpsc_sync_test_recv_iter_break,
-                    test_mpsc_sync_try_recv_states,
-                    test_mpsc_sync_destroy_upgraded_shared_port_when_sender_still_active,
-                    test_mpsc_sync_send1,
-                    test_mpsc_sync_send2,
-                    test_mpsc_sync_send3,
-                    test_mpsc_sync_send4,
-                    test_mpsc_sync_try_send1,
-                    test_mpsc_sync_try_send2,
-                    test_mpsc_sync_try_send3,
-                    test_mpsc_sync_issue_15761,
-                    //test alignbox
-                    test_alignbox,
-                    test_alignbox_heap_init,
-                    test_alignbox_clone,
-                    test_alignbox_clonefrom,
-                    test_alignbox_clonefrom_no_eq_size,
-                    //test signal
-                    test_signal_forbidden,
-                    test_signal_without_pid,
-                    test_signal_with_pid,
-                    test_signal_register_unregister,
-                    test_signal_register_unregister1,
-                    //test exception
-                    test_exception_handler,
-                    )
+        // tcrypto
+        test_rsgx_sha256_slice,
+        test_rsgx_sha256_handle,
+        // assert
+        foo_panic,
+        foo_should,
+        foo_assert,
+        // rts::veh
+        test_register_first_exception_handler,
+        test_register_last_exception_handler,
+        test_register_multiple_exception_handler,
+        // rts::trts
+        test_rsgx_get_thread_policy,
+        test_trts_sizes,
+        test_read_rand,
+        test_data_is_within_enclave,
+        test_slice_is_within_enlave,
+        test_raw_is_within_enclave,
+        test_data_is_outside_enclave,
+        test_slice_is_outside_enclave,
+        test_raw_is_outside_enclave,
+        // rts::macros
+        test_global_ctors_object,
+        // rts::error
+        test_error,
+        // rts::libc
+        test_rts_libc_memchr,
+        test_rts_libc_memrchr,
+        // rts::memchr
+        test_rts_memchr_memchr,
+        test_rts_memchr_memrchr,
+        test_ascii,
+        // rts::c_str
+        test_cstr,
+        // tseal
+        test_seal_unseal,
+        test_number_sealing, // Thanks to @silvanegli
+        test_array_sealing,  // Thanks to @silvanegli
+        test_mac_aadata_slice,
+        test_mac_aadata_number,
+        // rand
+        test_rand_os_sgxrng,
+        test_rand_distributions,
+        test_rand_isaac_isaacrng,
+        test_rand_chacharng,
+        test_rand_reseeding,
+        // serialize
+        test_serialize_base,
+        test_serialize_struct,
+        test_serialize_enum,
+        // std::sgxfs
+        test_sgxfs,
+        // std::fs
+        test_fs,
+        // std::fs untrusted mode
+        test_fs_untrusted_fs_feature_enabled,
+        // std::time
+        test_std_time,
+        // rand
+        test_rand_cratesio,
+        // types
+        check_metadata_size,
+        check_version,
+        // env
+        test_env_vars_os,
+        test_env_self_exe_path,
+        test_env_current_dir,
+        test_env_home_dir,
+        //path
+        test_path_stat_is_correct_on_is_dir,
+        test_path_fileinfo_false_when_checking_is_file_on_a_directory,
+        test_path_directoryinfo_check_exists_before_and_after_mkdir,
+        test_path_directoryinfo_readdir,
+        test_path_mkdir_path_already_exists_error,
+        test_path_recursive_mkdir,
+        test_path_recursive_mkdir_failure,
+        test_path_recursive_mkdir_slash,
+        test_path_recursive_mkdir_dot,
+        test_path_recursive_mkdir_empty,
+        test_path_recursive_rmdir,
+        test_path_recursive_rmdir_of_symlink,
+        test_path_unicode_path_is_dir,
+        test_path_unicode_path_exists,
+        test_path_copy_file_dst_dir,
+        test_path_copy_file_src_dir,
+        test_path_canonicalize_works_simple,
+        test_path_dir_entry_methods,
+        test_path_read_dir_not_found,
+        test_path_mkdir_trailing_slash,
+        test_path_create_dir_all_with_junctions,
+        test_path_copy_file_follows_dst_symlink,
+        // thread
+        test_thread_unnamed_thread,
+        test_thread_named_thread,
+        test_thread_run_basic,
+        test_thread_spawn_sched,
+        test_thread_spawn_sched_childs_on_default_sched,
+        test_thread_test_avoid_copying_the_body_spawn,
+        test_thread_test_avoid_copying_the_body_thread_spawn,
+        test_thread_test_avoid_copying_the_body_join,
+        test_thread_invalid_named_thread,
+        test_thread_join_panic,
+        test_thread_child_doesnt_ref_parent,
+        test_thread_simple_newsched_spawn,
+        test_thread_try_panic_message_static_str,
+        test_thread_try_panic_message_owned_str,
+        test_thread_try_panic_message_any,
+        test_thread_try_panic_message_unit_struct,
+        test_thread_park_timeout_unpark_before,
+        test_thread_park_timeout_unpark_not_called,
+        test_thread_park_timeout_unpark_called_other_thread,
+        test_thread_sleep_ms_smoke,
+        test_thread_size_of_option_thread_id,
+        test_thread_id_equal,
+        test_thread_id_not_equal,
+        //test mpsc
+        test_mpsc_smoke,
+        test_mpsc_drop_full,
+        test_mpsc_drop_full_shared,
+        test_mpsc_smoke_shared,
+        test_mpsc_smoke_threads,
+        test_mpsc_smoke_port_gone,
+        test_mpsc_smoke_shared_port_gone,
+        test_mpsc_smoke_shared_port_gone2,
+        test_mpsc_port_gone_concurrent,
+        test_mpsc_port_gone_concurrent_shared,
+        test_mpsc_smoke_chan_gone,
+        test_mpsc_smoke_chan_gone_shared,
+        test_mpsc_chan_gone_concurrent,
+        test_mpsc_stress,
+        test_mpsc_stress_shared,
+        test_mpsc_send_from_outside_runtime,
+        test_mpsc_recv_from_outside_runtime,
+        test_mpsc_no_runtime,
+        test_mpsc_oneshot_single_thread_close_port_first,
+        test_mpsc_oneshot_single_thread_close_chan_first,
+        test_mpsc_oneshot_single_thread_send_port_close,
+        //test_mpsc_oneshot_single_thread_recv_chan_close, //should panic
+        test_mpsc_oneshot_single_thread_send_then_recv,
+        test_mpsc_oneshot_single_thread_try_send_open,
+        test_mpsc_oneshot_single_thread_try_send_closed,
+        test_mpsc_oneshot_single_thread_try_recv_open,
+        test_mpsc_oneshot_single_thread_try_recv_closed,
+        test_mpsc_oneshot_single_thread_peek_data,
+        test_mpsc_oneshot_single_thread_peek_close,
+        test_mpsc_oneshot_single_thread_peek_open,
+        test_mpsc_oneshot_multi_task_recv_then_send,
+        //test_mpsc_oneshot_multi_task_recv_then_close, //should panic
+        test_mpsc_oneshot_multi_thread_close_stress,
+        //test_mpsc_oneshot_multi_thread_send_close_stress, //should panic
+        //test_mpsc_oneshot_multi_thread_recv_close_stress, //should panic
+        test_mpsc_oneshot_multi_thread_send_recv_stress,
+        test_mpsc_stream_send_recv_stress,
+        test_mpsc_oneshot_single_thread_recv_timeout,
+        test_mpsc_stress_recv_timeout_two_threads,
+        test_mpsc_recv_timeout_upgrade,
+        test_mpsc_stress_recv_timeout_shared,
+        test_mpsc_very_long_recv_timeout_wont_panic,
+        test_mpsc_recv_a_lot,
+        test_mpsc_shared_recv_timeout,
+        test_mpsc_shared_chan_stress,
+        test_mpsc_test_nested_recv_iter,
+        test_mpsc_test_recv_iter_break,
+        test_mpsc_test_recv_try_iter,
+        test_mpsc_test_recv_into_iter_owned,
+        test_mpsc_try_recv_states,
+        test_mpsc_destroy_upgraded_shared_port_when_sender_still_active,
+        test_mpsc_issue_32114,
+        test_mpsc_sync_smoke,
+        test_mpsc_sync_drop_full,
+        test_mpsc_sync_smoke_shared,
+        test_mpsc_sync_recv_timeout,
+        test_mpsc_sync_smoke_threads,
+        test_mpsc_sync_smoke_port_gone,
+        test_mpsc_sync_smoke_shared_port_gone2,
+        test_mpsc_sync_port_gone_concurrent,
+        test_mpsc_sync_port_gone_concurrent_shared,
+        test_mpsc_sync_smoke_chan_gone,
+        test_mpsc_sync_smoke_chan_gone_shared,
+        test_mpsc_sync_chan_gone_concurrent,
+        test_mpsc_sync_stress,
+        test_mpsc_sync_stress_recv_timeout_two_threads,
+        test_mpsc_sync_stress_recv_timeout_shared,
+        test_mpsc_sync_stress_shared,
+        test_mpsc_sync_oneshot_single_thread_close_port_first,
+        test_mpsc_sync_oneshot_single_thread_send_port_close,
+        //test_mpsc_sync_oneshot_single_thread_recv_chan_close, //should panic
+        test_mpsc_sync_oneshot_single_thread_send_then_recv,
+        test_mpsc_sync_oneshot_single_thread_try_send_open,
+        test_mpsc_sync_oneshot_single_thread_try_send_closed,
+        test_mpsc_sync_oneshot_single_thread_try_send_closed2,
+        test_mpsc_sync_oneshot_single_thread_try_recv_open,
+        test_mpsc_sync_oneshot_single_thread_try_recv_closed,
+        test_mpsc_sync_oneshot_single_thread_try_recv_closed_with_data,
+        test_mpsc_sync_oneshot_single_thread_peek_data,
+        test_mpsc_sync_oneshot_single_thread_peek_close,
+        test_mpsc_sync_oneshot_single_thread_peek_open,
+        test_mpsc_sync_oneshot_multi_task_recv_then_send,
+        //test_mpsc_sync_oneshot_multi_task_recv_then_close, //should panic
+        test_mpsc_sync_oneshot_multi_thread_close_stress,
+        //test_mpsc_sync_oneshot_multi_thread_send_close_stress, //should panic
+        //test_mpsc_sync_oneshot_multi_thread_recv_close_stress, //should panic
+        test_mpsc_sync_oneshot_multi_thread_send_recv_stress,
+        test_mpsc_sync_stream_send_recv_stress,
+        test_mpsc_sync_recv_a_lot,
+        test_mpsc_sync_shared_sync_chan_stress,
+        test_mpsc_sync_test_nested_recv_iter,
+        test_mpsc_sync_test_recv_iter_break,
+        test_mpsc_sync_try_recv_states,
+        test_mpsc_sync_destroy_upgraded_shared_port_when_sender_still_active,
+        test_mpsc_sync_send1,
+        test_mpsc_sync_send2,
+        test_mpsc_sync_send3,
+        test_mpsc_sync_send4,
+        test_mpsc_sync_try_send1,
+        test_mpsc_sync_try_send2,
+        test_mpsc_sync_try_send3,
+        test_mpsc_sync_issue_15761,
+        //test alignbox
+        test_alignbox,
+        test_alignbox_heap_init,
+        test_alignbox_clone,
+        test_alignbox_clonefrom,
+        test_alignbox_clonefrom_no_eq_size,
+        //test signal
+        test_signal_forbidden,
+        test_signal_without_pid,
+        test_signal_with_pid,
+        test_signal_register_unregister,
+        test_signal_register_unregister1,
+        //test float point
+        test_fp64,
+        //test exception
+        test_exception_handler,
+    )
 }
diff --git a/samplecode/unit-test/enclave/src/test_alignbox.rs b/samplecode/unit-test/enclave/src/test_alignbox.rs
index f9431c4..e879566 100644
--- a/samplecode/unit-test/enclave/src/test_alignbox.rs
+++ b/samplecode/unit-test/enclave/src/test_alignbox.rs
@@ -15,14 +15,14 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use std::mem;
 use sgx_alloc::alignbox::*;
 use sgx_types::*;
+use std::mem;
 
 impl_struct! {
     pub struct user_struct {
         pub a: u8,
-        pub b: [u16; 2], 
+        pub b: [u16; 2],
         pub c: u32,
         pub d: [u64; 6],
     }
@@ -56,100 +56,106 @@
     }
 }
 
- pub fn test_alignbox() {
+pub fn test_alignbox() {
     //align
     {
-        let aligned_box: Option<AlignBox<struct_align_128_t>> = AlignBox::<struct_align_128_t>::new();
+        let aligned_box: Option<AlignBox<struct_align_128_t>> =
+            AlignBox::<struct_align_128_t>::new();
         assert!(aligned_box.is_some());
     }
     //no align
     {
         let str_slice: &[AlignReq] = &[];
-        let aligned_box: Option<AlignBox<user_struct>> = AlignBox::<user_struct>::new_with_req(1, &str_slice);
+        let aligned_box: Option<AlignBox<user_struct>> =
+            AlignBox::<user_struct>::new_with_req(1, &str_slice);
         assert!(aligned_box.is_none());
     }
     //align
     {
         let str_slice: &[AlignReq] = &[
             AlignReq {
-                offset:offset_of!(user_struct, a),
-                len:mem::size_of::<u8>(),
+                offset: offset_of!(user_struct, a),
+                len: mem::size_of::<u8>(),
             },
             AlignReq {
-                offset:offset_of!(user_struct, b),
-                len:mem::size_of::<[u16; 2]>(),
+                offset: offset_of!(user_struct, b),
+                len: mem::size_of::<[u16; 2]>(),
             },
             AlignReq {
                 offset: offset_of!(user_struct, d),
-                len:mem::size_of::<[u64; 6]>(),
-            }
+                len: mem::size_of::<[u64; 6]>(),
+            },
         ];
-        let aligned_box: Option<AlignBox<user_struct>> = AlignBox::<user_struct>::new_with_req(1, &str_slice);
+        let aligned_box: Option<AlignBox<user_struct>> =
+            AlignBox::<user_struct>::new_with_req(1, &str_slice);
         assert!(aligned_box.is_some());
     }
     //no align
     {
         let str_slice: &[AlignReq] = &[
             AlignReq {
-                offset:offset_of!(user_struct, a),
-                len:mem::size_of::<u8>(),
+                offset: offset_of!(user_struct, a),
+                len: mem::size_of::<u8>(),
             },
             AlignReq {
-                offset:offset_of!(user_struct, b),
-                len:mem::size_of::<[u16; 2]>(),
+                offset: offset_of!(user_struct, b),
+                len: mem::size_of::<[u16; 2]>(),
             },
             AlignReq {
                 offset: offset_of!(user_struct, d),
-                len:mem::size_of::<[u64; 6]>(),
-            }
+                len: mem::size_of::<[u64; 6]>(),
+            },
         ];
-        let aligned_box: Option<AlignBox<user_struct>>  = AlignBox::<user_struct>::new_with_req(16, &str_slice);
+        let aligned_box: Option<AlignBox<user_struct>> =
+            AlignBox::<user_struct>::new_with_req(16, &str_slice);
         assert!(aligned_box.is_none());
     }
     //no align
     {
         let str_slice: &[AlignReq] = &[
             AlignReq {
-                offset:offset_of!(struct_align_t, key1),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                offset: offset_of!(struct_align_t, key1),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
                 offset: offset_of!(struct_align_t, key2),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
-                offset:offset_of!(struct_align_t, key3),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                offset: offset_of!(struct_align_t, key3),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
                 offset: offset_of!(struct_align_t, key4),
-                len:mem::size_of::<sgx_key_128bit_t>(),
-            }
+                len: mem::size_of::<sgx_key_128bit_t>(),
+            },
         ];
-        let aligned_box: Option<AlignBox<struct_align_t>>  = AlignBox::<struct_align_t>::new_with_req(32, &str_slice);
+        let aligned_box: Option<AlignBox<struct_align_t>> =
+            AlignBox::<struct_align_t>::new_with_req(32, &str_slice);
         assert!(aligned_box.is_none());
     }
     //align
     {
         let str_slice: &[AlignReq] = &[
             AlignReq {
-                offset:offset_of!(struct_align_t, key1),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                offset: offset_of!(struct_align_t, key1),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
                 offset: offset_of!(struct_align_t, key2),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
-                offset:offset_of!(struct_align_t, key3),
-                len:mem::size_of::<sgx_key_128bit_t>(),
+                offset: offset_of!(struct_align_t, key3),
+                len: mem::size_of::<sgx_key_128bit_t>(),
             },
             AlignReq {
                 offset: offset_of!(struct_align_t, key4),
-                len:mem::size_of::<sgx_key_128bit_t>(),
-            }
+                len: mem::size_of::<sgx_key_128bit_t>(),
+            },
         ];
-        let aligned_box: Option<AlignBox<struct_align_t>>  = AlignBox::<struct_align_t>::new_with_req(16, &str_slice);
+        let aligned_box: Option<AlignBox<struct_align_t>> =
+            AlignBox::<struct_align_t>::new_with_req(16, &str_slice);
         assert!(aligned_box.is_some());
     }
 }
@@ -157,41 +163,69 @@
 pub fn test_alignbox_heap_init() {
     let str_slice: &[AlignReq] = &[
         AlignReq {
-            offset:offset_of!(struct_align_t, key1),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key1),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key2),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
-            offset:offset_of!(struct_align_t, key3),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key3),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key4),
-            len:mem::size_of::<sgx_key_128bit_t>(),
-        }
+            len: mem::size_of::<sgx_key_128bit_t>(),
+        },
     ];
 
     let stack_align_obj = struct_align_t {
-        key1: [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff],
+        key1: [
+            0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+            0xfe, 0xff,
+        ],
         pad1: [0x00; 16],
-        key2: [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff],
+        key2: [
+            0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+            0xfe, 0xff,
+        ],
         pad2: [0x00; 16],
-        key3: [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff],
+        key3: [
+            0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+            0xfe, 0xff,
+        ],
         pad3: [0x00; 16],
-        key4: [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff],
+        key4: [
+            0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+            0xfe, 0xff,
+        ],
     };
-    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(|mut t| {
-        t.key1 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad1 = [0x00; 16];
-        t.key2 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad2 = [0x00; 16];
-        t.key3 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad3 = [0x00; 16];
-        t.key4 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-    }, 16, &str_slice);
+    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(
+        |mut t| {
+            t.key1 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad1 = [0x00; 16];
+            t.key2 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad2 = [0x00; 16];
+            t.key3 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad3 = [0x00; 16];
+            t.key4 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+        },
+        16,
+        &str_slice,
+    );
     assert!(heap_align_obj.is_some());
     assert_eq!(stack_align_obj, *(heap_align_obj.unwrap()));
 }
@@ -199,64 +233,96 @@
 pub fn test_alignbox_clone() {
     let str_slice: &[AlignReq] = &[
         AlignReq {
-            offset:offset_of!(struct_align_t, key1),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key1),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key2),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
-            offset:offset_of!(struct_align_t, key3),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key3),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key4),
-            len:mem::size_of::<sgx_key_128bit_t>(),
-        }
+            len: mem::size_of::<sgx_key_128bit_t>(),
+        },
     ];
-    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(|mut t| {
-        t.key1 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad1 = [0x00; 16];
-        t.key2 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad2 = [0x00; 16];
-        t.key3 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad3 = [0x00; 16];
-        t.key4 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-    }, 16, &str_slice);
+    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(
+        |mut t| {
+            t.key1 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad1 = [0x00; 16];
+            t.key2 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad2 = [0x00; 16];
+            t.key3 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad3 = [0x00; 16];
+            t.key4 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+        },
+        16,
+        &str_slice,
+    );
     assert!(heap_align_obj.is_some());
     let align_box_clone = heap_align_obj.clone();
-   assert_eq!(*align_box_clone.unwrap(), *heap_align_obj.unwrap());
+    assert_eq!(*align_box_clone.unwrap(), *heap_align_obj.unwrap());
 }
 
 pub fn test_alignbox_clonefrom() {
     let str_slice: &[AlignReq] = &[
         AlignReq {
-            offset:offset_of!(struct_align_t, key1),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key1),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key2),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
-            offset:offset_of!(struct_align_t, key3),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key3),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key4),
-            len:mem::size_of::<sgx_key_128bit_t>(),
-        }
+            len: mem::size_of::<sgx_key_128bit_t>(),
+        },
     ];
-    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(|mut t| {
-        t.key1 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad1 = [0x00; 16];
-        t.key2 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad2 = [0x00; 16];
-        t.key3 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad3 = [0x00; 16];
-        t.key4 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-    }, 16, &str_slice);
+    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(
+        |mut t| {
+            t.key1 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad1 = [0x00; 16];
+            t.key2 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad2 = [0x00; 16];
+            t.key3 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad3 = [0x00; 16];
+            t.key4 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+        },
+        16,
+        &str_slice,
+    );
     assert!(heap_align_obj.is_some());
 
     let mut heap_align_zero_obj = AlignBox::<struct_align_t>::new_with_req(16, &str_slice);
@@ -268,31 +334,47 @@
 pub fn test_alignbox_clonefrom_no_eq_size() {
     let str_slice: &[AlignReq] = &[
         AlignReq {
-            offset:offset_of!(struct_align_t, key1),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key1),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key2),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
-            offset:offset_of!(struct_align_t, key3),
-            len:mem::size_of::<sgx_key_128bit_t>(),
+            offset: offset_of!(struct_align_t, key3),
+            len: mem::size_of::<sgx_key_128bit_t>(),
         },
         AlignReq {
             offset: offset_of!(struct_align_t, key4),
-            len:mem::size_of::<sgx_key_128bit_t>(),
-        }
+            len: mem::size_of::<sgx_key_128bit_t>(),
+        },
     ];
-    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(|mut t| {
-        t.key1 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad1 = [0x00; 16];
-        t.key2 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad2 = [0x00; 16];
-        t.key3 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-        t.pad3 = [0x00; 16];
-        t.key4 = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff];
-    }, 16, &str_slice);
+    let heap_align_obj = AlignBox::<struct_align_t>::heap_init_with_req(
+        |mut t| {
+            t.key1 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad1 = [0x00; 16];
+            t.key2 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad2 = [0x00; 16];
+            t.key3 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+            t.pad3 = [0x00; 16];
+            t.key4 = [
+                0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
+                0xfe, 0xff,
+            ];
+        },
+        16,
+        &str_slice,
+    );
     assert!(heap_align_obj.is_some());
 
     let mut heap_align_zero_obj = AlignBox::<struct_align_t>::new_with_req(1, &str_slice);
diff --git a/samplecode/unit-test/enclave/src/test_alignstruct.rs b/samplecode/unit-test/enclave/src/test_alignstruct.rs
index 6fe53f5..7ad8170 100644
--- a/samplecode/unit-test/enclave/src/test_alignstruct.rs
+++ b/samplecode/unit-test/enclave/src/test_alignstruct.rs
@@ -22,7 +22,7 @@
 // #[sgx_align(align=1, size=64)]
 // pub struct user_struct {
 //     pub a: u8,
-//     pub b: [u16; 2], 
+//     pub b: [u16; 2],
 //     pub c: u32,
 //     pub d: [u64; 6],
 // }
@@ -32,7 +32,7 @@
 // #[repr(align(4))]
 // pub struct user_struct {
 //     pub a: u8,
-//     pub b: [u16; 2], 
+//     pub b: [u16; 2],
 //     pub c: u32,
 //     pub d: [u64; 6],
 // }
@@ -42,7 +42,7 @@
 //     #[sgx_align(align=1, size=64)]
 //     pub struct user_struct {
 //         pub a: u8,
-//         pub b: [u16; 2], 
+//         pub b: [u16; 2],
 //         pub c: u32,
 //         pub d: [u64; 6],
 //     }
@@ -53,7 +53,7 @@
 //     #[sgx_align(align=16, size=64)]
 //     pub struct user_struct {
 //         pub a: u8,
-//         pub b: [u16; 2], 
+//         pub b: [u16; 2],
 //         pub c: u32,
 //         pub d: [u64; 6],
 //     }
@@ -77,6 +77,3 @@
 //         pub key4: sgx_key_128bit_t,
 //     }
 // }
-
-
-
diff --git a/samplecode/unit-test/enclave/src/test_assert.rs b/samplecode/unit-test/enclave/src/test_assert.rs
index 339be48..f8f527d 100644
--- a/samplecode/unit-test/enclave/src/test_assert.rs
+++ b/samplecode/unit-test/enclave/src/test_assert.rs
@@ -15,16 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use std::panic;
 use std::vec::Vec;
 
-pub fn foo_panic () {
-    let v:Vec<u32> = vec![1];
+pub fn foo_panic() {
+    let v: Vec<u32> = vec![1];
     v[0];
 }
 
 pub fn foo_should() {
-    let v:Vec<u32> = vec![];
+    let v: Vec<u32> = vec![];
     should_panic!(v[0]);
 }
 
diff --git a/samplecode/unit-test/enclave/src/test_crypto.rs b/samplecode/unit-test/enclave/src/test_crypto.rs
index 99573cd..e3ed127 100644
--- a/samplecode/unit-test/enclave/src/test_crypto.rs
+++ b/samplecode/unit-test/enclave/src/test_crypto.rs
@@ -15,9 +15,9 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use utils::*;
-use std::string::String;
 use sgx_tcrypto::*;
+use std::string::String;
+use utils::*;
 
 static HASH_TEST_VEC: &'static [&'static str] = &[
     &"abc",
@@ -40,7 +40,7 @@
     }
 }
 
-pub fn test_rsgx_sha256_handle(){
+pub fn test_rsgx_sha256_handle() {
     let test_size = HASH_TEST_VEC.len();
     for i in 0..test_size {
         let input_str = String::from(HASH_TEST_VEC[i]);
@@ -52,4 +52,3 @@
         assert_eq!(hex_to_bytes(HASH_SHA256_TRUTH[i]), hash);
     }
 }
-
diff --git a/samplecode/unit-test/enclave/src/test_env.rs b/samplecode/unit-test/enclave/src/test_env.rs
index 3f177e7..dd3264f 100644
--- a/samplecode/unit-test/enclave/src/test_env.rs
+++ b/samplecode/unit-test/enclave/src/test_env.rs
@@ -6,7 +6,7 @@
     assert_ne!(0, p.size_hint().0);
 }
 
-pub  fn test_env_self_exe_path() {
+pub fn test_env_self_exe_path() {
     let path = current_exe();
     assert!(path.is_ok());
     let path = path.unwrap();
diff --git a/samplecode/unit-test/enclave/src/test_exception.rs b/samplecode/unit-test/enclave/src/test_exception.rs
index 963cd46..ae2d057 100644
--- a/samplecode/unit-test/enclave/src/test_exception.rs
+++ b/samplecode/unit-test/enclave/src/test_exception.rs
@@ -15,22 +15,26 @@
 // specific language governing permissions and limitations
 // under the License..
 
+use sgx_signal::exception::{register_exception, unregister};
+use sgx_signal::ContinueType;
+use sgx_trts::enclave;
+use sgx_types::sgx_exception_info_t;
+use std::backtrace::{self, PrintFormat};
+use std::panic;
 use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Arc;
 use std::thread;
 use std::time::Duration;
-use std::backtrace::{self, PrintFormat};
-use std::panic;
-use sgx_signal::ContinueType;
-use sgx_signal::exception::{register_exception, unregister};
-use sgx_types::sgx_exception_info_t;
-use sgx_trts::enclave;
 
 #[no_mangle]
 #[inline(never)]
 fn test_abort() -> ! {
     let td = enclave::SgxThreadData::current();
-    println!("test_abort stack: {:x}-{:x}", td.stack_base(), td.stack_limit());
+    println!(
+        "test_abort stack: {:x}-{:x}",
+        td.stack_base(),
+        td.stack_limit()
+    );
 
     std::intrinsics::abort()
 }
@@ -62,9 +66,7 @@
     let r1 = register_exception(false, handler1);
     let r2 = register_exception(true, handler2);
 
-    panic::catch_unwind(||{
-        test_abort()
-    }).ok();
+    panic::catch_unwind(|| test_abort()).ok();
 
     for _ in 0..10 {
         thread::sleep(Duration::from_millis(100));
@@ -82,4 +84,3 @@
     unregister(r2.unwrap());
     panic!("Timed out waiting for the exception");
 }
-
diff --git a/samplecode/unit-test/enclave/src/test_file.rs b/samplecode/unit-test/enclave/src/test_file.rs
index 715794f..2e447ac 100644
--- a/samplecode/unit-test/enclave/src/test_file.rs
+++ b/samplecode/unit-test/enclave/src/test_file.rs
@@ -16,14 +16,13 @@
 // under the License..
 
 use sgx_rand::{Rng, StdRng};
-use std::sgxfs::{self, SgxFile};
-use std::untrusted::fs::File;
-use std::untrusted::fs::remove_file;
 use std::io::{Read, Write};
+use std::sgxfs::{self, SgxFile};
 use std::string::*;
+use std::untrusted::fs::remove_file;
+use std::untrusted::fs::File;
 
 pub fn test_sgxfs() {
-
     let mut write_data: [u8; 16] = [0; 16];
     let mut read_data: [u8; 16] = [0; 16];
     let write_size;
@@ -87,7 +86,7 @@
     }
 }
 
-pub fn test_fs () {
+pub fn test_fs() {
     {
         let f = File::create("foo.txt");
         assert!(f.is_ok());
diff --git a/samplecode/unit-test/enclave/src/test_fp.rs b/samplecode/unit-test/enclave/src/test_fp.rs
new file mode 100644
index 0000000..85f7aac
--- /dev/null
+++ b/samplecode/unit-test/enclave/src/test_fp.rs
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+pub fn test_fp64() {
+    let f = 3.7_f64;
+    let g = 3.0_f64;
+    let h = -3.7_f64;
+    
+    assert_eq!(f.floor(), 3.0);
+    assert_eq!(g.floor(), 3.0);
+    assert_eq!(h.floor(), -4.0);
+
+    let f = 3.01_f64;
+    let g = 4.0_f64;
+    assert_eq!(f.ceil(), 4.0);
+    assert_eq!(g.ceil(), 4.0);
+    
+    let f = 3.3_f64;
+    let g = -3.3_f64;
+    assert_eq!(f.round(), 3.0);
+    assert_eq!(g.round(), -3.0);
+    
+    let f = 3.7_f64;
+    let g = 3.0_f64;
+    let h = -3.7_f64;
+    assert_eq!(f.trunc(), 3.0);
+    assert_eq!(g.trunc(), 3.0);
+    assert_eq!(h.trunc(), -3.0);
+    
+    let x = 3.6_f64;
+    let y = -3.6_f64;
+    let abs_difference_x = (x.fract() - 0.6).abs();
+    let abs_difference_y = (y.fract() - (-0.6)).abs();
+    assert!(abs_difference_x < 1e-10);
+    assert!(abs_difference_y < 1e-10);
+    
+    let x = 3.5_f64;
+    let y = -3.5_f64;
+    let abs_difference_x = (x.abs() - x).abs();
+    let abs_difference_y = (y.abs() - (-y)).abs();
+    assert!(abs_difference_x < 1e-10);
+    assert!(abs_difference_y < 1e-10);
+    assert!(f64::NAN.abs().is_nan());
+
+    let f = 3.5_f64;
+    assert_eq!(f.signum(), 1.0);
+    assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
+    assert!(f64::NAN.signum().is_nan());
+    
+    let f = 3.5_f64;
+    assert_eq!(f.copysign(0.42), 3.5_f64);
+    assert_eq!(f.copysign(-0.42), -3.5_f64);
+    assert_eq!((-f).copysign(0.42), 3.5_f64);
+    assert_eq!((-f).copysign(-0.42), -3.5_f64);
+    assert!(f64::NAN.copysign(1.0).is_nan());
+    
+    let m = 10.0_f64;
+    let x = 4.0_f64;
+    let b = 60.0_f64;
+    // 100.0
+    let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let a: f64 = 7.0;
+    let b = 4.0;
+    assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
+    assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
+    assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
+    assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
+    
+    let a: f64 = 7.0;
+    let b = 4.0;
+    assert_eq!(a.rem_euclid(b), 3.0);
+    assert_eq!((-a).rem_euclid(b), 1.0);
+    assert_eq!(a.rem_euclid(-b), 3.0);
+    assert_eq!((-a).rem_euclid(-b), 1.0);
+    // limitation due to round-off error
+    assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
+    
+    let x = 2.0_f64;
+    let abs_difference = (x.powi(2) - (x * x)).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let x = 2.0_f64;
+    let abs_difference = (x.powf(2.0) - (x * x)).abs();
+    assert!(abs_difference < 1e-10);
+
+    let positive = 4.0_f64;
+    let negative = -4.0_f64;
+    let abs_difference = (positive.sqrt() - 2.0).abs();
+    assert!(abs_difference < 1e-10);
+    assert!(negative.sqrt().is_nan());
+    
+    let one = 1.0_f64;
+    // e^1
+    let e = one.exp();
+    // ln(e) - 1 == 0
+    let abs_difference = (e.ln() - 1.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let f = 2.0_f64;
+    // 2^2 - 4 == 0
+    let abs_difference = (f.exp2() - 4.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let one = 1.0_f64;
+    // e^1
+    let e = one.exp();
+    // ln(e) - 1 == 0
+    let abs_difference = (e.ln() - 1.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let twenty_five = 25.0_f64;
+    // log5(25) - 2 == 0
+    let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let four = 4.0_f64;
+    // log2(4) - 2 == 0
+    let abs_difference = (four.log2() - 2.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let hundred = 100.0_f64;
+    // log10(100) - 2 == 0
+    let abs_difference = (hundred.log10() - 2.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let x = 3.0_f64;
+    let y = -3.0_f64;
+    let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
+    let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
+    assert!(abs_difference_x < 1e-10);
+    assert!(abs_difference_y < 1e-10);
+    
+    let x = 8.0_f64;
+    // x^(1/3) - 2 == 0
+    let abs_difference = (x.cbrt() - 2.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let x = 2.0_f64;
+    let y = 3.0_f64;
+    // sqrt(x^2 + y^2)
+    let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let x = std::f64::consts::FRAC_PI_2;
+    let abs_difference = (x.sin() - 1.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let x = 2.0 * std::f64::consts::PI;
+    let abs_difference = (x.cos() - 1.0).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let f = std::f64::consts::FRAC_PI_2;
+    // asin(sin(pi/2))
+    let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let f = std::f64::consts::FRAC_PI_4;
+    // acos(cos(pi/4))
+    let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let f = 1.0_f64;
+    // atan(tan(1))
+    let abs_difference = (f.tan().atan() - 1.0).abs();
+    assert!(abs_difference < 1e-10);
+
+    // Positive angles measured counter-clockwise
+    // from positive x axis
+    // -pi/4 radians (45 deg clockwise)
+    let x1 = 3.0_f64;
+    let y1 = -3.0_f64;
+    // 3pi/4 radians (135 deg counter-clockwise)
+    let x2 = -3.0_f64;
+    let y2 = 3.0_f64;
+    let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
+    let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
+    assert!(abs_difference_1 < 1e-10);
+    assert!(abs_difference_2 < 1e-10);
+    
+    let x = std::f64::consts::FRAC_PI_4;
+    let f = x.sin_cos();
+    let abs_difference_0 = (f.0 - x.sin()).abs();
+    let abs_difference_1 = (f.1 - x.cos()).abs();
+    assert!(abs_difference_0 < 1e-10);
+    assert!(abs_difference_1 < 1e-10);
+    
+    let x = 1e-16_f64;
+    // for very small x, e^x is approximately 1 + x + x^2 / 2
+    let approx = x + x * x / 2.0;
+    let abs_difference = (x.exp_m1() - approx).abs();
+    assert!(abs_difference < 1e-20);
+    
+    let x = 1e-16_f64;
+    // for very small x, ln(1 + x) is approximately x - x^2 / 2
+    let approx = x - x * x / 2.0;
+    let abs_difference = (x.ln_1p() - approx).abs();
+    assert!(abs_difference < 1e-20);
+    
+    let e = std::f64::consts::E;
+    let x = 1.0_f64;
+    let f = x.sinh();
+    // Solving sinh() at 1 gives `(e^2-1)/(2e)`
+    let g = ((e * e) - 1.0) / (2.0 * e);
+    let abs_difference = (f - g).abs();
+    assert!(abs_difference < 1e-10);
+    
+    let e = std::f64::consts::E;
+    let x = 1.0_f64;
+    let f = x.cosh();
+    // Solving cosh() at 1 gives this result
+    let g = ((e * e) + 1.0) / (2.0 * e);
+    let abs_difference = (f - g).abs();
+    // Same result
+    assert!(abs_difference < 1.0e-10);
+    
+    let e = std::f64::consts::E;
+    let x = 1.0_f64;
+    let f = x.tanh();
+    // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
+    let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
+    let abs_difference = (f - g).abs();
+    assert!(abs_difference < 1.0e-10);
+    
+    let x = 1.0_f64;
+    let f = x.sinh().asinh();
+    let abs_difference = (f - x).abs();
+    assert!(abs_difference < 1.0e-10);
+    
+    let x = 1.0_f64;
+    let f = x.cosh().acosh();
+    let abs_difference = (f - x).abs();
+    assert!(abs_difference < 1.0e-10);
+    
+    let e = std::f64::consts::E;
+    let f = e.tanh().atanh();
+    let abs_difference = (f - e).abs();
+    assert!(abs_difference < 1.0e-10);
+}
diff --git a/samplecode/unit-test/enclave/src/test_mpsc.rs b/samplecode/unit-test/enclave/src/test_mpsc.rs
index afb836f..c36164d 100644
--- a/samplecode/unit-test/enclave/src/test_mpsc.rs
+++ b/samplecode/unit-test/enclave/src/test_mpsc.rs
@@ -1,9 +1,12 @@
+use std::boxed::Box;
+use std::env;
+use std::sync::mpsc::{
+    channel, sync_channel, Receiver, RecvTimeoutError, SendError, Sender, SyncSender, TryRecvError,
+    TrySendError,
+};
 use std::thread;
 use std::time::{Duration, Instant};
-use std::sync::mpsc::{channel, sync_channel, Sender, Receiver, SyncSender, SendError, TryRecvError, RecvTimeoutError, TrySendError};
-use std::boxed::Box;
 use std::untrusted::time::InstantEx;
-use std::env;
 
 pub fn stress_factor() -> usize {
     match env::var("SGXRUST_TEST_STRESS") {
@@ -23,7 +26,6 @@
     tx.send(box 1).unwrap();
 }
 
-
 pub fn test_mpsc_drop_full_shared() {
     let (tx, _rx) = channel::<Box<isize>>();
     drop(tx.clone());
@@ -42,7 +44,7 @@
 
 pub fn test_mpsc_smoke_threads() {
     let (tx, rx) = channel::<i32>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         tx.send(1).unwrap();
     });
     assert_eq!(rx.recv().unwrap(), 1);
@@ -70,7 +72,7 @@
 
 pub fn test_mpsc_port_gone_concurrent() {
     let (tx, rx) = channel::<i32>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap();
     });
     while tx.send(1).is_ok() {}
@@ -79,7 +81,7 @@
 pub fn test_mpsc_port_gone_concurrent_shared() {
     let (tx, rx) = channel::<i32>();
     let tx2 = tx.clone();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap();
     });
     while tx.send(1).is_ok() && tx2.send(1).is_ok() {}
@@ -101,7 +103,7 @@
 
 pub fn test_mpsc_chan_gone_concurrent() {
     let (tx, rx) = channel::<i32>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         tx.send(1).unwrap();
         tx.send(1).unwrap();
     });
@@ -110,8 +112,10 @@
 
 pub fn test_mpsc_stress() {
     let (tx, rx) = channel::<i32>();
-    let t = thread::spawn(move|| {
-        for _ in 0..100 { tx.send(1).unwrap(); }
+    let t = thread::spawn(move || {
+        for _ in 0..100 {
+            tx.send(1).unwrap();
+        }
     });
     for _ in 0..100 {
         assert_eq!(rx.recv().unwrap(), 1);
@@ -124,7 +128,7 @@
     const NTHREADS: u32 = 2;
     let (tx, rx) = channel::<i32>();
 
-    let t = thread::spawn(move|| {
+    let t = thread::spawn(move || {
         for _ in 0..AMT * NTHREADS {
             assert_eq!(rx.recv().unwrap(), 1);
         }
@@ -136,8 +140,10 @@
 
     for _ in 0..NTHREADS {
         let tx = tx.clone();
-        thread::spawn(move|| {
-            for _ in 0..AMT { tx.send(1).unwrap(); }
+        thread::spawn(move || {
+            for _ in 0..AMT {
+                tx.send(1).unwrap();
+            }
         });
     }
     drop(tx);
@@ -147,14 +153,14 @@
 pub fn test_mpsc_send_from_outside_runtime() {
     let (tx1, rx1) = channel::<()>();
     let (tx2, rx2) = channel::<i32>();
-    let t1 = thread::spawn(move|| {
+    let t1 = thread::spawn(move || {
         tx1.send(()).unwrap();
         for _ in 0..40 {
             assert_eq!(rx2.recv().unwrap(), 1);
         }
     });
     rx1.recv().unwrap();
-    let t2 = thread::spawn(move|| {
+    let t2 = thread::spawn(move || {
         for _ in 0..40 {
             tx2.send(1).unwrap();
         }
@@ -165,7 +171,7 @@
 
 pub fn test_mpsc_recv_from_outside_runtime() {
     let (tx, rx) = channel::<i32>();
-    let t = thread::spawn(move|| {
+    let t = thread::spawn(move || {
         for _ in 0..40 {
             assert_eq!(rx.recv().unwrap(), 1);
         }
@@ -179,11 +185,11 @@
 pub fn test_mpsc_no_runtime() {
     let (tx1, rx1) = channel::<i32>();
     let (tx2, rx2) = channel::<i32>();
-    let t1 = thread::spawn(move|| {
+    let t1 = thread::spawn(move || {
         assert_eq!(rx1.recv().unwrap(), 1);
         tx2.send(2).unwrap();
     });
-    let t2 = thread::spawn(move|| {
+    let t2 = thread::spawn(move || {
         tx1.send(1).unwrap();
         assert_eq!(rx2.recv().unwrap(), 2);
     });
@@ -212,11 +218,12 @@
 #[allow(dead_code)]
 pub fn test_mpsc_oneshot_single_thread_recv_chan_close() {
     // Receiving on a closed chan will panic
-    let res = thread::spawn(move|| {
+    let res = thread::spawn(move || {
         let (tx, rx) = channel::<i32>();
         drop(tx);
         rx.recv().unwrap();
-    }).join();
+    })
+    .join();
     // What is our res?
     assert!(res.is_err());
 }
@@ -272,7 +279,7 @@
 
 pub fn test_mpsc_oneshot_multi_task_recv_then_send() {
     let (tx, rx) = channel::<Box<i32>>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         assert!(*rx.recv().unwrap() == 10);
     });
 
@@ -282,19 +289,20 @@
 #[allow(dead_code)]
 pub fn test_mpsc_oneshot_multi_task_recv_then_close() {
     let (tx, rx) = channel::<Box<i32>>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         drop(tx);
     });
-    let res = thread::spawn(move|| {
+    let res = thread::spawn(move || {
         assert!(*rx.recv().unwrap() == 10);
-    }).join();
+    })
+    .join();
     assert!(res.is_err());
 }
 
 pub fn test_mpsc_oneshot_multi_thread_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = channel::<i32>();
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             drop(rx);
         });
         drop(tx);
@@ -305,12 +313,13 @@
 pub fn test_mpsc_oneshot_multi_thread_send_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = channel::<i32>();
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             drop(rx);
         });
-        let _ = thread::spawn(move|| {
+        let _ = thread::spawn(move || {
             tx.send(1).unwrap();
-        }).join();
+        })
+        .join();
     }
 }
 
@@ -318,14 +327,15 @@
 pub fn test_mpsc_oneshot_multi_thread_recv_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = channel::<i32>();
-        thread::spawn(move|| {
-            let res = thread::spawn(move|| {
+        thread::spawn(move || {
+            let res = thread::spawn(move || {
                 rx.recv().unwrap();
-            }).join();
+            })
+            .join();
             assert!(res.is_err());
         });
-        let _t = thread::spawn(move|| {
-            thread::spawn(move|| {
+        let _t = thread::spawn(move || {
+            thread::spawn(move || {
                 drop(tx);
             });
         });
@@ -335,7 +345,7 @@
 pub fn test_mpsc_oneshot_multi_thread_send_recv_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = channel::<Box<isize>>();
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             tx.send(box 10).unwrap();
             thread::sleep_ms(60000);
         });
@@ -351,18 +361,22 @@
         recv(rx, 0);
 
         fn send(tx: Sender<Box<i32>>, i: i32) {
-            if i == 10 { return }
+            if i == 10 {
+                return;
+            }
 
-            thread::spawn(move|| {
+            thread::spawn(move || {
                 tx.send(box i).unwrap();
                 send(tx, i + 1);
             });
         }
 
         fn recv(rx: Receiver<Box<i32>>, i: i32) {
-            if i == 10 { return }
+            if i == 10 {
+                return;
+            }
 
-            thread::spawn(move|| {
+            thread::spawn(move || {
                 assert!(*rx.recv().unwrap() == i);
                 recv(rx, i + 1);
             });
@@ -374,7 +388,10 @@
     let (tx, rx) = channel();
     tx.send(()).unwrap();
     assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(()));
-    assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Err(RecvTimeoutError::Timeout));
+    assert_eq!(
+        rx.recv_timeout(Duration::from_millis(1)),
+        Err(RecvTimeoutError::Timeout)
+    );
     tx.send(()).unwrap();
     assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(()));
 }
@@ -449,9 +466,7 @@
 
 pub fn test_mpsc_very_long_recv_timeout_wont_panic() {
     let (tx, rx) = channel::<()>();
-    let join_handle = thread::spawn(move || {
-        rx.recv_timeout(Duration::from_secs(u64::MAX))
-    });
+    let join_handle = thread::spawn(move || rx.recv_timeout(Duration::from_secs(u64::MAX)));
     thread::sleep(Duration::from_secs(1));
     assert!(tx.send(()).is_ok());
     assert_eq!(join_handle.join().unwrap(), Ok(()));
@@ -460,8 +475,12 @@
 pub fn test_mpsc_recv_a_lot() {
     // Regression test that we don't run out of stack in scheduler context
     let (tx, rx) = channel();
-    for _ in 0..10000 { tx.send(()).unwrap(); }
-    for _ in 0..10000 { rx.recv().unwrap(); }
+    for _ in 0..10000 {
+        tx.send(()).unwrap();
+    }
+    for _ in 0..10000 {
+        rx.recv().unwrap();
+    }
 }
 
 pub fn test_mpsc_shared_recv_timeout() {
@@ -469,14 +488,19 @@
     let total = 5;
     for _ in 0..total {
         let tx = tx.clone();
-        thread::spawn(move|| {
+        thread::spawn(move || {
             tx.send(()).unwrap();
         });
     }
 
-    for _ in 0..total { rx.recv().unwrap(); }
+    for _ in 0..total {
+        rx.recv().unwrap();
+    }
 
-    assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Err(RecvTimeoutError::Timeout));
+    assert_eq!(
+        rx.recv_timeout(Duration::from_millis(1)),
+        Err(RecvTimeoutError::Timeout)
+    );
     tx.send(()).unwrap();
     assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(()));
 }
@@ -486,7 +510,7 @@
     let total = stress_factor() + 3;
     for _ in 0..total {
         let tx = tx.clone();
-        thread::spawn(move|| {
+        thread::spawn(move || {
             tx.send(()).unwrap();
         });
     }
@@ -499,7 +523,7 @@
     let (tx, rx) = channel::<i32>();
     let (total_tx, total_rx) = channel::<i32>();
 
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         let mut acc = 0;
         for x in rx.iter() {
             acc += x;
@@ -518,7 +542,7 @@
     let (tx, rx) = channel::<i32>();
     let (count_tx, count_rx) = channel();
 
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         let mut count = 0;
         for x in rx.iter() {
             if count >= 3 {
@@ -543,7 +567,7 @@
     let (response_tx, response_rx) = channel();
 
     // Request `x`s until we have `6`.
-    let t = thread::spawn(move|| {
+    let t = thread::spawn(move || {
         let mut count = 0;
         loop {
             for x in response_rx.try_iter() {
@@ -582,7 +606,7 @@
     let (tx1, rx1) = channel::<i32>();
     let (tx2, rx2) = channel::<()>();
     let (tx3, rx3) = channel::<()>();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx2.recv().unwrap();
         tx1.send(1).unwrap();
         tx3.send(()).unwrap();
@@ -601,18 +625,20 @@
     assert_eq!(rx1.try_recv(), Err(TryRecvError::Disconnected));
 }
 
-    // This bug used to end up in a livelock inside of the Receiver destructor
-    // because the internal state of the Shared packet was corrupted
+// This bug used to end up in a livelock inside of the Receiver destructor
+// because the internal state of the Shared packet was corrupted
 pub fn test_mpsc_destroy_upgraded_shared_port_when_sender_still_active() {
     let (tx, rx) = channel();
     let (tx2, rx2) = channel();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap(); // wait on a oneshot
-        drop(rx);  // destroy a shared
+        drop(rx); // destroy a shared
         tx2.send(()).unwrap();
     });
     // make sure the other thread has gone to sleep
-    for _ in 0..5000 { thread::yield_now(); }
+    for _ in 0..5000 {
+        thread::yield_now();
+    }
 
     // upgrade to a shared chan and send a message
     let t = tx.clone();
@@ -635,7 +661,7 @@
     assert_eq!(rx.recv().unwrap(), 1);
 }
 
-pub  fn test_mpsc_sync_drop_full() {
+pub fn test_mpsc_sync_drop_full() {
     let (tx, _rx) = sync_channel::<Box<isize>>(1);
     tx.send(box 1).unwrap();
 }
@@ -651,14 +677,17 @@
 
 pub fn test_mpsc_sync_recv_timeout() {
     let (tx, rx) = sync_channel::<i32>(1);
-    assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Err(RecvTimeoutError::Timeout));
+    assert_eq!(
+        rx.recv_timeout(Duration::from_millis(1)),
+        Err(RecvTimeoutError::Timeout)
+    );
     tx.send(1).unwrap();
     assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(1));
 }
 
 pub fn test_mpsc_sync_smoke_threads() {
     let (tx, rx) = sync_channel::<i32>(0);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         tx.send(1).unwrap();
     });
     assert_eq!(rx.recv().unwrap(), 1);
@@ -680,7 +709,7 @@
 
 pub fn test_mpsc_sync_port_gone_concurrent() {
     let (tx, rx) = sync_channel::<i32>(0);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap();
     });
     while tx.send(1).is_ok() {}
@@ -689,7 +718,7 @@
 pub fn test_mpsc_sync_port_gone_concurrent_shared() {
     let (tx, rx) = sync_channel::<i32>(0);
     let tx2 = tx.clone();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap();
     });
     while tx.send(1).is_ok() && tx2.send(1).is_ok() {}
@@ -711,7 +740,7 @@
 
 pub fn test_mpsc_sync_chan_gone_concurrent() {
     let (tx, rx) = sync_channel::<i32>(0);
-    thread::spawn(move|| {
+    thread::spawn(move || {
         tx.send(1).unwrap();
         tx.send(1).unwrap();
     });
@@ -720,19 +749,23 @@
 
 pub fn test_mpsc_sync_stress() {
     let (tx, rx) = sync_channel::<i32>(0);
-    thread::spawn(move|| {
-        for _ in 0..10 { tx.send(1).unwrap(); }
+    thread::spawn(move || {
+        for _ in 0..10 {
+            tx.send(1).unwrap();
+        }
     });
     for _ in 0..10 {
         assert_eq!(rx.recv().unwrap(), 1);
     }
 }
 
-pub  fn test_mpsc_sync_stress_recv_timeout_two_threads() {
+pub fn test_mpsc_sync_stress_recv_timeout_two_threads() {
     let (tx, rx) = sync_channel::<i32>(0);
 
-    thread::spawn(move|| {
-        for _ in 0..10 { tx.send(1).unwrap(); }
+    thread::spawn(move || {
+        for _ in 0..10 {
+            tx.send(1).unwrap();
+        }
     });
 
     let mut recv_count = 0;
@@ -741,7 +774,7 @@
             Ok(v) => {
                 assert_eq!(v, 1);
                 recv_count += 1;
-            },
+            }
             Err(RecvTimeoutError::Timeout) => continue,
             Err(RecvTimeoutError::Disconnected) => break,
         }
@@ -756,14 +789,14 @@
     let (tx, rx) = sync_channel::<i32>(0);
     let (dtx, drx) = sync_channel::<()>(0);
 
-    thread::spawn(move|| {
+    thread::spawn(move || {
         let mut recv_count = 0;
         loop {
             match rx.recv_timeout(Duration::from_millis(10)) {
                 Ok(v) => {
                     assert_eq!(v, 1);
                     recv_count += 1;
-                },
+                }
                 Err(RecvTimeoutError::Timeout) => continue,
                 Err(RecvTimeoutError::Disconnected) => break,
             }
@@ -777,8 +810,10 @@
 
     for _ in 0..NTHREADS {
         let tx = tx.clone();
-        thread::spawn(move|| {
-            for _ in 0..AMT { tx.send(1).unwrap(); }
+        thread::spawn(move || {
+            for _ in 0..AMT {
+                tx.send(1).unwrap();
+            }
         });
     }
 
@@ -793,7 +828,7 @@
     let (tx, rx) = sync_channel::<i32>(0);
     let (dtx, drx) = sync_channel::<()>(0);
 
-    thread::spawn(move|| {
+    thread::spawn(move || {
         for _ in 0..AMT * NTHREADS {
             assert_eq!(rx.recv().unwrap(), 1);
         }
@@ -806,8 +841,10 @@
 
     for _ in 0..NTHREADS {
         let tx = tx.clone();
-        thread::spawn(move|| {
-            for _ in 0..AMT { tx.send(1).unwrap(); }
+        thread::spawn(move || {
+            for _ in 0..AMT {
+                tx.send(1).unwrap();
+            }
         });
     }
     drop(tx);
@@ -830,11 +867,12 @@
 #[allow(dead_code)]
 pub fn test_mpsc_sync_oneshot_single_thread_recv_chan_close() {
     // Receiving on a closed chan will panic
-    let res = thread::spawn(move|| {
+    let res = thread::spawn(move || {
         let (tx, rx) = sync_channel::<i32>(0);
         drop(tx);
         rx.recv().unwrap();
-    }).join();
+    })
+    .join();
     // What is our res?
     assert!(res.is_err());
 }
@@ -845,7 +883,7 @@
     assert!(*rx.recv().unwrap() == 10);
 }
 
-pub  fn test_mpsc_sync_oneshot_single_thread_try_send_open() {
+pub fn test_mpsc_sync_oneshot_single_thread_try_send_open() {
     let (tx, rx) = sync_channel::<i32>(1);
     assert_eq!(tx.try_send(10), Ok(()));
     assert!(rx.recv().unwrap() == 10);
@@ -857,7 +895,7 @@
     assert_eq!(tx.try_send(10), Err(TrySendError::Disconnected(10)));
 }
 
-pub  fn test_mpsc_sync_oneshot_single_thread_try_send_closed2() {
+pub fn test_mpsc_sync_oneshot_single_thread_try_send_closed2() {
     let (tx, _rx) = sync_channel::<i32>(0);
     assert_eq!(tx.try_send(10), Err(TrySendError::Full(10)));
 }
@@ -903,7 +941,7 @@
 
 pub fn test_mpsc_sync_oneshot_multi_task_recv_then_send() {
     let (tx, rx) = sync_channel::<Box<i32>>(0);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         assert!(*rx.recv().unwrap() == 10);
     });
 
@@ -913,19 +951,20 @@
 #[allow(dead_code)]
 pub fn test_mpsc_sync_oneshot_multi_task_recv_then_close() {
     let (tx, rx) = sync_channel::<Box<i32>>(0);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         drop(tx);
     });
-    let res = thread::spawn(move|| {
+    let res = thread::spawn(move || {
         assert!(*rx.recv().unwrap() == 10);
-    }).join();
+    })
+    .join();
     assert!(res.is_err());
 }
 
 pub fn test_mpsc_sync_oneshot_multi_thread_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = sync_channel::<i32>(0);
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             drop(rx);
         });
         drop(tx);
@@ -936,12 +975,13 @@
 pub fn test_mpsc_sync_oneshot_multi_thread_send_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = sync_channel::<i32>(0);
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             drop(rx);
         });
         let _ = thread::spawn(move || {
             tx.send(1).unwrap();
-        }).join();
+        })
+        .join();
     }
 }
 
@@ -949,14 +989,15 @@
 pub fn test_mpsc_sync_oneshot_multi_thread_recv_close_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = sync_channel::<i32>(0);
-        let _t = thread::spawn(move|| {
-            let res = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
+            let res = thread::spawn(move || {
                 rx.recv().unwrap();
-            }).join();
+            })
+            .join();
             assert!(res.is_err());
         });
-        let _t = thread::spawn(move|| {
-            thread::spawn(move|| {
+        let _t = thread::spawn(move || {
+            thread::spawn(move || {
                 drop(tx);
             });
         });
@@ -966,7 +1007,7 @@
 pub fn test_mpsc_sync_oneshot_multi_thread_send_recv_stress() {
     for _ in 0..stress_factor() {
         let (tx, rx) = sync_channel::<Box<i32>>(0);
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             tx.send(box 10).unwrap();
         });
         assert!(*rx.recv().unwrap() == 10);
@@ -981,18 +1022,22 @@
         recv(rx, 0);
 
         fn send(tx: SyncSender<Box<i32>>, i: i32) {
-            if i == 10 { return }
+            if i == 10 {
+                return;
+            }
 
-            thread::spawn(move|| {
+            thread::spawn(move || {
                 tx.send(box i).unwrap();
                 send(tx, i + 1);
             });
         }
 
         fn recv(rx: Receiver<Box<i32>>, i: i32) {
-            if i == 10 { return }
+            if i == 10 {
+                return;
+            }
 
-            thread::spawn(move|| {
+            thread::spawn(move || {
                 assert!(*rx.recv().unwrap() == i);
                 recv(rx, i + 1);
             });
@@ -1003,8 +1048,12 @@
 pub fn test_mpsc_sync_recv_a_lot() {
     // Regression test that we don't run out of stack in scheduler context
     let (tx, rx) = sync_channel(10000);
-    for _ in 0..10000 { tx.send(()).unwrap(); }
-    for _ in 0..10000 { rx.recv().unwrap(); }
+    for _ in 0..10000 {
+        tx.send(()).unwrap();
+    }
+    for _ in 0..10000 {
+        rx.recv().unwrap();
+    }
 }
 
 pub fn test_mpsc_sync_shared_sync_chan_stress() {
@@ -1012,7 +1061,7 @@
     let total = stress_factor() + 2;
     for _ in 0..total {
         let tx = tx.clone();
-        thread::spawn(move|| {
+        thread::spawn(move || {
             tx.send(()).unwrap();
         });
     }
@@ -1026,7 +1075,7 @@
     let (tx, rx) = sync_channel::<i32>(0);
     let (total_tx, total_rx) = sync_channel::<i32>(0);
 
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         let mut acc = 0;
         for x in rx.iter() {
             acc += x;
@@ -1044,7 +1093,7 @@
     let (tx, rx) = sync_channel::<i32>(0);
     let (count_tx, count_rx) = sync_channel(0);
 
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         let mut count = 0;
         for x in rx.iter() {
             if count >= 3 {
@@ -1068,7 +1117,7 @@
     let (tx1, rx1) = sync_channel::<i32>(1);
     let (tx2, rx2) = sync_channel::<()>(1);
     let (tx3, rx3) = sync_channel::<()>(1);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx2.recv().unwrap();
         tx1.send(1).unwrap();
         tx3.send(()).unwrap();
@@ -1087,18 +1136,20 @@
     assert_eq!(rx1.try_recv(), Err(TryRecvError::Disconnected));
 }
 
-    // This bug used to end up in a livelock inside of the Receiver destructor
-    // because the internal state of the Shared packet was corrupted
-pub fn  test_mpsc_sync_destroy_upgraded_shared_port_when_sender_still_active() {
+// This bug used to end up in a livelock inside of the Receiver destructor
+// because the internal state of the Shared packet was corrupted
+pub fn test_mpsc_sync_destroy_upgraded_shared_port_when_sender_still_active() {
     let (tx, rx) = sync_channel::<()>(0);
     let (tx2, rx2) = sync_channel::<()>(0);
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         rx.recv().unwrap(); // wait on a oneshot
-        drop(rx);  // destroy a shared
+        drop(rx); // destroy a shared
         tx2.send(()).unwrap();
     });
     // make sure the other thread has gone to sleep
-    for _ in 0..5000 { thread::yield_now(); }
+    for _ in 0..5000 {
+        thread::yield_now();
+    }
 
     // upgrade to a shared chan and send a message
     let t = tx.clone();
@@ -1111,20 +1162,26 @@
 
 pub fn test_mpsc_sync_send1() {
     let (tx, rx) = sync_channel::<i32>(0);
-    let _t = thread::spawn(move|| { rx.recv().unwrap(); });
+    let _t = thread::spawn(move || {
+        rx.recv().unwrap();
+    });
     assert_eq!(tx.send(1), Ok(()));
 }
 
 pub fn test_mpsc_sync_send2() {
     let (tx, rx) = sync_channel::<i32>(0);
-    let _t = thread::spawn(move|| { drop(rx); });
+    let _t = thread::spawn(move || {
+        drop(rx);
+    });
     assert!(tx.send(1).is_err());
 }
 
 pub fn test_mpsc_sync_send3() {
     let (tx, rx) = sync_channel::<i32>(1);
     assert_eq!(tx.send(1), Ok(()));
-    let _t =thread::spawn(move|| { drop(rx); });
+    let _t = thread::spawn(move || {
+        drop(rx);
+    });
     assert!(tx.send(1).is_err());
 }
 
@@ -1133,11 +1190,11 @@
     let tx2 = tx.clone();
     let (done, donerx) = channel();
     let done2 = done.clone();
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         assert!(tx.send(1).is_err());
         done.send(()).unwrap();
     });
-    let _t = thread::spawn(move|| {
+    let _t = thread::spawn(move || {
         assert!(tx2.send(2).is_err());
         done2.send(()).unwrap();
     });
@@ -1157,7 +1214,7 @@
     assert_eq!(tx.try_send(1), Err(TrySendError::Full(1)));
 }
 
-pub fn  test_mpsc_sync_try_send3() {
+pub fn test_mpsc_sync_try_send3() {
     let (tx, rx) = sync_channel::<i32>(1);
     assert_eq!(tx.try_send(1), Ok(()));
     drop(rx);
@@ -1169,7 +1226,7 @@
         let (tx1, rx1) = sync_channel::<()>(3);
         let (tx2, rx2) = sync_channel::<()>(3);
 
-        let _t = thread::spawn(move|| {
+        let _t = thread::spawn(move || {
             rx1.recv().unwrap();
             tx2.try_send(()).unwrap();
         });
@@ -1181,4 +1238,4 @@
     for _ in 0..5 {
         repro()
     }
-}
\ No newline at end of file
+}
diff --git a/samplecode/unit-test/enclave/src/test_path.rs b/samplecode/unit-test/enclave/src/test_path.rs
index b550d39..42583c3 100644
--- a/samplecode/unit-test/enclave/src/test_path.rs
+++ b/samplecode/unit-test/enclave/src/test_path.rs
@@ -1,18 +1,19 @@
+use core::str;
 use sgx_rand::*;
 use std::env;
-use std::path::{Path, PathBuf};
-use std::fs::File;
 use std::fs;
-use std::panic;
-use core::str;
-use std::io::{Read, Write, ErrorKind};
+use std::fs::File;
+use std::io::{ErrorKind, Read, Write};
+use std::path::{Path, PathBuf};
 
-macro_rules! check { ($e:expr) => (
-    match $e {
-        Ok(t) => t,
-        Err(e) => panic!("{} failed with: {}", stringify!($e), e),
-    }
-) }
+macro_rules! check {
+    ($e:expr) => {
+        match $e {
+            Ok(t) => t,
+            Err(e) => panic!("{} failed with: {}", stringify!($e), e),
+        }
+    };
+}
 
 pub struct TempDir(PathBuf);
 
@@ -48,7 +49,7 @@
 pub fn test_path_stat_is_correct_on_is_dir() {
     let tmpdir = tmpdir();
     let filename = &tmpdir.join("file_stat_correct_on_is_dir");
-    
+
     check!(fs::create_dir(filename));
     let stat_res_fn = check!(fs::metadata(filename));
     assert!(stat_res_fn.is_dir());
@@ -88,12 +89,12 @@
         let msg = msg_str.as_bytes();
         check!(w.write(msg));
     }
-  
+
     let files = check!(fs::read_dir(dir));
     let mut mem = [0; 4];
     for f in files {
         let f = f.unwrap().path();
-    
+
         {
             let n = f.file_stem().unwrap();
             check!(check!(File::open(&f)).read(&mut mem));
@@ -172,7 +173,7 @@
     let dir = tmpdir.join("d2");
     let canary = dir.join("do_not_delete");
     check!(fs::create_dir_all(&dir));
-  
+
     check!(check!(File::create(&canary)).write(b"foo"));
     check!(fs::soft_link(&dir, &link));
 
@@ -218,17 +219,18 @@
 
     check!(File::create(&out));
     match fs::copy(&*out, tmpdir.path()) {
-        Ok(..) => panic!(), Err(..) => {}
+        Ok(..) => panic!(),
+        Err(..) => {}
     }
 }
 
-
 pub fn test_path_copy_file_src_dir() {
     let tmpdir = tmpdir();
     let out = tmpdir.join("out");
 
     match fs::copy(tmpdir.path(), &out) {
-        Ok(..) => panic!(), Err(..) => {}
+        Ok(..) => panic!(),
+        Err(..) => {}
     }
     assert!(!out.exists());
 }
@@ -250,8 +252,8 @@
 pub fn test_path_dir_entry_methods() {
     let tmpdir = tmpdir();
 
-   fs::create_dir_all(&tmpdir.join("a")).unwrap();
-   File::create(&tmpdir.join("b")).unwrap();
+    fs::create_dir_all(&tmpdir.join("a")).unwrap();
+    File::create(&tmpdir.join("b")).unwrap();
 
     for file in tmpdir.path().read_dir().unwrap().map(|f| f.unwrap()) {
         let fname = file.file_name();
@@ -311,7 +313,9 @@
 
     check!(fs::copy(&in_path, &out_path_symlink));
 
-    assert!(check!(out_path_symlink.symlink_metadata()).file_type().is_symlink());
+    assert!(check!(out_path_symlink.symlink_metadata())
+        .file_type()
+        .is_symlink());
     assert_eq!(check!(fs::read(&out_path_symlink)), b"foo".to_vec());
     assert_eq!(check!(fs::read(&out_path)), b"foo".to_vec());
 }
diff --git a/samplecode/unit-test/enclave/src/test_rand.rs b/samplecode/unit-test/enclave/src/test_rand.rs
index 6d2d8ca..c809e85 100644
--- a/samplecode/unit-test/enclave/src/test_rand.rs
+++ b/samplecode/unit-test/enclave/src/test_rand.rs
@@ -18,7 +18,7 @@
 use sgx_rand::*;
 
 // pub use os::SgxRng
-pub fn test_rand_os_sgxrng(){
+pub fn test_rand_os_sgxrng() {
     let mut os_rng = os::SgxRng::new().unwrap();
     let mut checksum_u32: u32 = 0;
     let mut checksum_u64: u64 = 0;
@@ -38,9 +38,9 @@
 
 // pub mod distribution
 // Too hard to test
-pub fn test_rand_distributions () {
-    use sgx_rand::distributions::*;
+pub fn test_rand_distributions() {
     use sgx_rand::distributions::Range;
+    use sgx_rand::distributions::*;
     use sgx_rand::*;
 
     // From rust-src/librand/distributions/rand.rs
@@ -152,7 +152,9 @@
 
     should_panic!(LogNormal::new(10.0, -1.0));
 
-    use sgx_rand::distributions::{IndependentSample, RandSample, Sample, Weighted, WeightedChoice};
+    use sgx_rand::distributions::{
+        IndependentSample, RandSample, Sample, Weighted, WeightedChoice,
+    };
 
     #[derive(PartialEq, Debug)]
     struct ConstRand(usize);
@@ -192,203 +194,428 @@
             for &val in &expected {
                 assert_eq!(wc.ind_sample(&mut rng), val)
             }
-        }}
+        }};
     }
 
-    t!(vec![Weighted { weight: 1, item: 10 }],
-       [10]);
+    t!(
+        vec![Weighted {
+            weight: 1,
+            item: 10
+        }],
+        [10]
+    );
 
     // skip some
-    t!(vec![Weighted { weight: 0, item: 20 },
-            Weighted { weight: 2, item: 21 },
-            Weighted { weight: 0, item: 22 },
-            Weighted { weight: 1, item: 23 }],
-       [21, 21, 23]);
+    t!(
+        vec![
+            Weighted {
+                weight: 0,
+                item: 20
+            },
+            Weighted {
+                weight: 2,
+                item: 21
+            },
+            Weighted {
+                weight: 0,
+                item: 22
+            },
+            Weighted {
+                weight: 1,
+                item: 23
+            }
+        ],
+        [21, 21, 23]
+    );
 
     // different weights
-    t!(vec![Weighted { weight: 4, item: 30 },
-            Weighted { weight: 3, item: 31 }],
-       [30, 30, 30, 30, 31, 31, 31]);
+    t!(
+        vec![
+            Weighted {
+                weight: 4,
+                item: 30
+            },
+            Weighted {
+                weight: 3,
+                item: 31
+            }
+        ],
+        [30, 30, 30, 30, 31, 31, 31]
+    );
 
     // check that we're binary searching
     // correctly with some vectors of odd
     // length.
-    t!(vec![Weighted { weight: 1, item: 40 },
-            Weighted { weight: 1, item: 41 },
-            Weighted { weight: 1, item: 42 },
-            Weighted { weight: 1, item: 43 },
-            Weighted { weight: 1, item: 44 }],
-       [40, 41, 42, 43, 44]);
-    t!(vec![Weighted { weight: 1, item: 50 },
-            Weighted { weight: 1, item: 51 },
-            Weighted { weight: 1, item: 52 },
-            Weighted { weight: 1, item: 53 },
-            Weighted { weight: 1, item: 54 },
-            Weighted { weight: 1, item: 55 },
-            Weighted { weight: 1, item: 56 }],
-       [50, 51, 52, 53, 54, 55, 56]);
+    t!(
+        vec![
+            Weighted {
+                weight: 1,
+                item: 40
+            },
+            Weighted {
+                weight: 1,
+                item: 41
+            },
+            Weighted {
+                weight: 1,
+                item: 42
+            },
+            Weighted {
+                weight: 1,
+                item: 43
+            },
+            Weighted {
+                weight: 1,
+                item: 44
+            }
+        ],
+        [40, 41, 42, 43, 44]
+    );
+    t!(
+        vec![
+            Weighted {
+                weight: 1,
+                item: 50
+            },
+            Weighted {
+                weight: 1,
+                item: 51
+            },
+            Weighted {
+                weight: 1,
+                item: 52
+            },
+            Weighted {
+                weight: 1,
+                item: 53
+            },
+            Weighted {
+                weight: 1,
+                item: 54
+            },
+            Weighted {
+                weight: 1,
+                item: 55
+            },
+            Weighted {
+                weight: 1,
+                item: 56
+            }
+        ],
+        [50, 51, 52, 53, 54, 55, 56]
+    );
 
     should_panic!(WeightedChoice::<isize>::new(&mut []));
 
     should_panic!({
-            let mut t = [Weighted { weight: 0, item: 0 },Weighted { weight: 0, item: 1 }];
-            WeightedChoice::new(&mut t);
+        let mut t = [
+            Weighted { weight: 0, item: 0 },
+            Weighted { weight: 0, item: 1 },
+        ];
+        WeightedChoice::new(&mut t);
     });
 
     let x = (!0) as u32 / 2; // x + x + 2 is the overflow
     should_panic!({
-            WeightedChoice::new(&mut [Weighted { weight: x, item: 0 },
-                              Weighted { weight: 1, item: 1 },
-                              Weighted { weight: x, item: 2 },
-                              Weighted { weight: 1, item: 3 }]);});
+        WeightedChoice::new(&mut [
+            Weighted { weight: x, item: 0 },
+            Weighted { weight: 1, item: 1 },
+            Weighted { weight: x, item: 2 },
+            Weighted { weight: 1, item: 3 },
+        ]);
+    });
 }
 
-
 // pub use isaac::{IsaacRng, Isaac64Rng};
 // We cannot test because methods are private
-pub fn test_rand_isaac_isaacrng () {
+pub fn test_rand_isaac_isaacrng() {
     let mut unseeded_32 = IsaacRng::new_unseeded();
-    let mut u32_arr = [0;32];
-    for i in 0..32{
+    let mut u32_arr = [0; 32];
+    for i in 0..32 {
         u32_arr[i] = unseeded_32.next_u32();
     }
-    assert_eq!(u32_arr, [1909923794, 3041581799, 3564668249, 3277924858,
-            568073897, 1018722762, 3627754367, 4198294973, 2365883657,
-            3626931337, 3678742600, 3391154246, 1343199022, 250607936,
-            14072211, 3155345791, 3971560663, 4015545006, 1861263481,
-            249057481, 1397017464, 4117866301, 1748577758, 2418980344,
-            2932544972, 3384981086, 3677243665, 3631899496, 1984325203,
-            1082540525, 1970086400, 1634359601]);
+    assert_eq!(
+        u32_arr,
+        [
+            1909923794, 3041581799, 3564668249, 3277924858, 568073897, 1018722762, 3627754367,
+            4198294973, 2365883657, 3626931337, 3678742600, 3391154246, 1343199022, 250607936,
+            14072211, 3155345791, 3971560663, 4015545006, 1861263481, 249057481, 1397017464,
+            4117866301, 1748577758, 2418980344, 2932544972, 3384981086, 3677243665, 3631899496,
+            1984325203, 1082540525, 1970086400, 1634359601
+        ]
+    );
 
     let mut unseeded_64 = Isaac64Rng::new_unseeded();
     let mut u64_arr = [0; 32];
     for i in 0..32 {
         u64_arr[i] = unseeded_64.next_u64();
     }
-    assert_eq!(u64_arr, [17761629189777429372, 9558052838949843840,
-            18366607087662878139, 5554737638101975254, 9657764456686032042,
-            1042790411024899657, 18326046843346171767, 6155338025429880858,
-            604674934004978847, 15021509919615502100, 3524189203627442317,
-            3491674822202243807, 2812011890713530556, 6197411209201204330,
-            3423587972865533405, 13333204927595598570, 8278113110980066893,
-            16728828598150967100, 4739548913818718572, 12247507049266305179,
-            13400017821939308292, 14607632686415099658, 9600795998696455796,
-            5780363980783644600, 4671476687197924946, 1273203271378292130,
-            11668772324252115302, 5778980740914384907, 3192360781399669869,
-            8925632526356214095, 13227689911971237637, 16127447699671226298]);
+    assert_eq!(
+        u64_arr,
+        [
+            17761629189777429372,
+            9558052838949843840,
+            18366607087662878139,
+            5554737638101975254,
+            9657764456686032042,
+            1042790411024899657,
+            18326046843346171767,
+            6155338025429880858,
+            604674934004978847,
+            15021509919615502100,
+            3524189203627442317,
+            3491674822202243807,
+            2812011890713530556,
+            6197411209201204330,
+            3423587972865533405,
+            13333204927595598570,
+            8278113110980066893,
+            16728828598150967100,
+            4739548913818718572,
+            12247507049266305179,
+            13400017821939308292,
+            14607632686415099658,
+            9600795998696455796,
+            5780363980783644600,
+            4671476687197924946,
+            1273203271378292130,
+            11668772324252115302,
+            5778980740914384907,
+            3192360781399669869,
+            8925632526356214095,
+            13227689911971237637,
+            16127447699671226298
+        ]
+    );
 
-    unseeded_32.reseed(&[1,2,3,4]);
-    for i in 0..32{
+    unseeded_32.reseed(&[1, 2, 3, 4]);
+    for i in 0..32 {
         u32_arr[i] = unseeded_32.next_u32();
     }
-    assert_eq!(u32_arr, [3673720382, 1957022519, 2949967219, 2273082436,
-            2412264859, 1616913581, 4286187434, 1337573575, 3564981768,
-            3931377724, 180676801, 4234301014, 3123903540, 804531392,
-            1687941800, 3180870208, 3007460037, 1643580469, 1601966685,
-            385631690, 3412228283, 2633128738, 2786818767, 1385157572,
-            3925804522, 4251266711, 4219473709, 3054074232, 4185095632,
-            2891236088, 1792921516, 1421268925]);
+    assert_eq!(
+        u32_arr,
+        [
+            3673720382, 1957022519, 2949967219, 2273082436, 2412264859, 1616913581, 4286187434,
+            1337573575, 3564981768, 3931377724, 180676801, 4234301014, 3123903540, 804531392,
+            1687941800, 3180870208, 3007460037, 1643580469, 1601966685, 385631690, 3412228283,
+            2633128738, 2786818767, 1385157572, 3925804522, 4251266711, 4219473709, 3054074232,
+            4185095632, 2891236088, 1792921516, 1421268925
+        ]
+    );
 
-    unseeded_64.reseed(&[5,6,7,8]);
-    for i in 0..32{
+    unseeded_64.reseed(&[5, 6, 7, 8]);
+    for i in 0..32 {
         u64_arr[i] = unseeded_64.next_u64();
     }
-    assert_eq!(u64_arr, [1519553293257924390, 8496251504967724708,
-            4814852879883820893, 701662672566014897, 4253212733272379994,
-            14710390856535061408, 13525913188857210438, 4176812646507392158,
-            5274347845703478634, 7494101755291139543, 12433257569627749800,
-            13555030297923086622, 12912035290107031759, 10277213319518338357,
-            13212221592643678736, 14214333261821823450, 13451082182892489478,
-            17471061581144953786, 663937856451252907, 7794799705525010541,
-            8878465633657608553, 11684451817656999723, 3085412699425654134,
-            7996244368322668557, 9264264892592899720, 18202308129527220178,
-            14762563311397316905, 7967643698755143779, 13033364966409644229,
-            2619244012980533217, 2251084057656619285, 16661636750341142669]);
+    assert_eq!(
+        u64_arr,
+        [
+            1519553293257924390,
+            8496251504967724708,
+            4814852879883820893,
+            701662672566014897,
+            4253212733272379994,
+            14710390856535061408,
+            13525913188857210438,
+            4176812646507392158,
+            5274347845703478634,
+            7494101755291139543,
+            12433257569627749800,
+            13555030297923086622,
+            12912035290107031759,
+            10277213319518338357,
+            13212221592643678736,
+            14214333261821823450,
+            13451082182892489478,
+            17471061581144953786,
+            663937856451252907,
+            7794799705525010541,
+            8878465633657608553,
+            11684451817656999723,
+            3085412699425654134,
+            7996244368322668557,
+            9264264892592899720,
+            18202308129527220178,
+            14762563311397316905,
+            7967643698755143779,
+            13033364966409644229,
+            2619244012980533217,
+            2251084057656619285,
+            16661636750341142669
+        ]
+    );
 
-    let mut from_seed_obj = IsaacRng::from_seed(&[1,2,3,4]);
-    for i in 0..32{
+    let mut from_seed_obj = IsaacRng::from_seed(&[1, 2, 3, 4]);
+    for i in 0..32 {
         u32_arr[i] = from_seed_obj.next_u32();
     }
-    assert_eq!(u32_arr, [3673720382, 1957022519, 2949967219, 2273082436,
-            2412264859, 1616913581, 4286187434, 1337573575, 3564981768,
-            3931377724, 180676801, 4234301014, 3123903540, 804531392,
-            1687941800, 3180870208, 3007460037, 1643580469, 1601966685,
-            385631690, 3412228283, 2633128738, 2786818767, 1385157572,
-            3925804522, 4251266711, 4219473709, 3054074232, 4185095632,
-            2891236088, 1792921516, 1421268925]);
+    assert_eq!(
+        u32_arr,
+        [
+            3673720382, 1957022519, 2949967219, 2273082436, 2412264859, 1616913581, 4286187434,
+            1337573575, 3564981768, 3931377724, 180676801, 4234301014, 3123903540, 804531392,
+            1687941800, 3180870208, 3007460037, 1643580469, 1601966685, 385631690, 3412228283,
+            2633128738, 2786818767, 1385157572, 3925804522, 4251266711, 4219473709, 3054074232,
+            4185095632, 2891236088, 1792921516, 1421268925
+        ]
+    );
 
-    let mut from_seed_obj_64 = Isaac64Rng::from_seed(&[5,6,7,8]);
-    for i in 0..32{
+    let mut from_seed_obj_64 = Isaac64Rng::from_seed(&[5, 6, 7, 8]);
+    for i in 0..32 {
         u64_arr[i] = from_seed_obj_64.next_u64();
     }
-    assert_eq!(u64_arr, [1519553293257924390, 8496251504967724708,
-            4814852879883820893, 701662672566014897, 4253212733272379994,
-            14710390856535061408, 13525913188857210438, 4176812646507392158,
-            5274347845703478634, 7494101755291139543, 12433257569627749800,
-            13555030297923086622, 12912035290107031759, 10277213319518338357,
-            13212221592643678736, 14214333261821823450, 13451082182892489478,
-            17471061581144953786, 663937856451252907, 7794799705525010541,
-            8878465633657608553, 11684451817656999723, 3085412699425654134,
-            7996244368322668557, 9264264892592899720, 18202308129527220178,
-            14762563311397316905, 7967643698755143779, 13033364966409644229,
-            2619244012980533217, 2251084057656619285, 16661636750341142669]);
+    assert_eq!(
+        u64_arr,
+        [
+            1519553293257924390,
+            8496251504967724708,
+            4814852879883820893,
+            701662672566014897,
+            4253212733272379994,
+            14710390856535061408,
+            13525913188857210438,
+            4176812646507392158,
+            5274347845703478634,
+            7494101755291139543,
+            12433257569627749800,
+            13555030297923086622,
+            12912035290107031759,
+            10277213319518338357,
+            13212221592643678736,
+            14214333261821823450,
+            13451082182892489478,
+            17471061581144953786,
+            663937856451252907,
+            7794799705525010541,
+            8878465633657608553,
+            11684451817656999723,
+            3085412699425654134,
+            7996244368322668557,
+            9264264892592899720,
+            18202308129527220178,
+            14762563311397316905,
+            7967643698755143779,
+            13033364966409644229,
+            2619244012980533217,
+            2251084057656619285,
+            16661636750341142669
+        ]
+    );
 
     let mut isaac_rand = IsaacRng::rand(&mut unseeded_32);
-    for i in 0..32{
+    for i in 0..32 {
         u32_arr[i] = isaac_rand.next_u32();
     }
-    assert_eq!(u32_arr, [3731735955, 1060971802, 2060608277, 2017700212,
-            2906693947, 983683489, 2442131015, 1804959108, 133350985,
-            1854001588, 1536802647, 1681331746, 3926133099, 2418741330,
-            2933560360, 3445159402, 372862460, 584055518, 3889338902,
-            2010948997, 156911533, 1516660418, 2985909906, 2363648761,
-            3441715164, 1549975272, 1594543237, 4051120660, 4197709030,
-            3816779293, 3783690383, 2208018815]);
+    assert_eq!(
+        u32_arr,
+        [
+            3731735955, 1060971802, 2060608277, 2017700212, 2906693947, 983683489, 2442131015,
+            1804959108, 133350985, 1854001588, 1536802647, 1681331746, 3926133099, 2418741330,
+            2933560360, 3445159402, 372862460, 584055518, 3889338902, 2010948997, 156911533,
+            1516660418, 2985909906, 2363648761, 3441715164, 1549975272, 1594543237, 4051120660,
+            4197709030, 3816779293, 3783690383, 2208018815
+        ]
+    );
 
     let mut isaac_rand64 = Isaac64Rng::rand(&mut unseeded_64);
-    for i in 0..32{
+    for i in 0..32 {
         u64_arr[i] = isaac_rand64.next_u64();
     }
-    assert_eq!(u64_arr, [9641310933002597978, 1377462969700517843,
-            505161414013526504, 15945732720326764686, 15418802195980990528,
-            16470443181580299993, 2257820220688421011, 11605056212052353438,
-            16446934715429265221, 2703881146695451334, 13779048927678904385,
-            3096257807787648519, 8078370161234145075, 11428361090362647425,
-            11163353455881119221, 2479568798082945555, 12997537085833811642,
-            15719812629115354877, 11384722140468426949, 1034436110230828879,
-            13933827272989341125, 16841927942905074390, 18283030736011842727,
-            18161628412401034420, 5992070539860306822, 14657980260306047343,
-            4240530741486241664, 16128750684074755458, 13382816331913570910,
-            1450784801074240917, 12172859719381448420, 11561024719408488519]);
+    assert_eq!(
+        u64_arr,
+        [
+            9641310933002597978,
+            1377462969700517843,
+            505161414013526504,
+            15945732720326764686,
+            15418802195980990528,
+            16470443181580299993,
+            2257820220688421011,
+            11605056212052353438,
+            16446934715429265221,
+            2703881146695451334,
+            13779048927678904385,
+            3096257807787648519,
+            8078370161234145075,
+            11428361090362647425,
+            11163353455881119221,
+            2479568798082945555,
+            12997537085833811642,
+            15719812629115354877,
+            11384722140468426949,
+            1034436110230828879,
+            13933827272989341125,
+            16841927942905074390,
+            18283030736011842727,
+            18161628412401034420,
+            5992070539860306822,
+            14657980260306047343,
+            4240530741486241664,
+            16128750684074755458,
+            13382816331913570910,
+            1450784801074240917,
+            12172859719381448420,
+            11561024719408488519
+        ]
+    );
 }
 
 // pub use chacha::ChaChaRng;
-pub fn test_rand_chacharng () {
+pub fn test_rand_chacharng() {
     let mut chacha_obj = ChaChaRng::new_unseeded();
-    let mut arr_32 = [0;32];
-    let mut arr_64 = [0;32];
+    let mut arr_32 = [0; 32];
+    let mut arr_64 = [0; 32];
     for i in 0..32 {
         arr_32[i] = chacha_obj.next_u32();
         arr_64[i] = chacha_obj.next_u64();
     }
-    assert_eq!(arr_32, [2917185654, 683509331, 3438229160, 2370328401,
-            4105716586, 2254827186, 2090318488, 1768285000, 1981921065,
-            3577337972, 520806828, 3781961315, 2576249230, 1572053161,
-            4248444456, 3230977993, 1486888979, 180343358, 882336707,
-            772637944, 4294454303, 2115397425, 3755235835, 720939292,
-            261668430, 2229903491, 3478228973, 1178947740, 3612365086,
-            3675722242, 2702218887, 562719562]);
-    assert_eq!(arr_64, [10393729188386987328, 13265865887038934432,
-            14343251829215281626, 4602718913620007096, 2062956584559347651,
-            13755971968358175061, 939050496938282955, 4491151038014484018,
-            4850067409370051029, 2891290281678408273, 8020199878315477293,
-            635428310198806553, 14295220693338866608, 7509513789169826591,
-            2214742853367878259, 7093718481497530944, 7734567148921168085,
-            2355867238271518203, 11858361224218268717, 1394059717204795920,
-            6203934356551920360, 8397145010625398571, 12762635603290365669,
-            15798090700413956747, 14488165283315532822, 14534818617999799127,
-            5587461883115671776, 438217751243844519, 12085977715001316377,
-            14462731156256673612, 5957300949303581564, 9621633472041422470]);
+    assert_eq!(
+        arr_32,
+        [
+            2917185654, 683509331, 3438229160, 2370328401, 4105716586, 2254827186, 2090318488,
+            1768285000, 1981921065, 3577337972, 520806828, 3781961315, 2576249230, 1572053161,
+            4248444456, 3230977993, 1486888979, 180343358, 882336707, 772637944, 4294454303,
+            2115397425, 3755235835, 720939292, 261668430, 2229903491, 3478228973, 1178947740,
+            3612365086, 3675722242, 2702218887, 562719562
+        ]
+    );
+    assert_eq!(
+        arr_64,
+        [
+            10393729188386987328,
+            13265865887038934432,
+            14343251829215281626,
+            4602718913620007096,
+            2062956584559347651,
+            13755971968358175061,
+            939050496938282955,
+            4491151038014484018,
+            4850067409370051029,
+            2891290281678408273,
+            8020199878315477293,
+            635428310198806553,
+            14295220693338866608,
+            7509513789169826591,
+            2214742853367878259,
+            7093718481497530944,
+            7734567148921168085,
+            2355867238271518203,
+            11858361224218268717,
+            1394059717204795920,
+            6203934356551920360,
+            8397145010625398571,
+            12762635603290365669,
+            15798090700413956747,
+            14488165283315532822,
+            14534818617999799127,
+            5587461883115671776,
+            438217751243844519,
+            12085977715001316377,
+            14462731156256673612,
+            5957300949303581564,
+            9621633472041422470
+        ]
+    );
 
     let mut chacha_obj = ChaChaRng::new_unseeded();
     chacha_obj.set_counter(0, 1234567890u64);
@@ -396,60 +623,98 @@
         arr_32[i] = chacha_obj.next_u32();
         arr_64[i] = chacha_obj.next_u64();
     }
-    assert_eq!(arr_32, [3689073239, 2242961698, 417536494, 1503493075,
-            2585895478, 1865454281, 2938464816, 3998462791, 1427673087,
-            2023308892, 3934458784, 2154405649, 4261926467, 944974180,
-            1017947150, 1858486886, 2081637745, 6698551, 2593531725, 989113434,
-            952425310, 2821646029, 2012236296, 2466857171, 1705021287,
-            2712374590, 2467996873, 2529275143, 3691921567, 4164253542,
-            88339832, 3139246687]);
-    assert_eq!(arr_64, [5208214742801571470, 12560379081283373091,
-            16366572874613089836, 5810973028396244379, 5062530146601697561,
-            217785109116347749, 10945155018052356834, 3830068552016855006,
-            8631305173908678621, 2826768761480962968, 10354302052483333752,
-            14791155778847304997, 9236871951464247547, 10565693803420516117,
-            2303174007018927082, 11829861525625440339, 17157070760956407958,
-            18121029959197227032, 15895614391418239344, 16692872359552745709,
-            364895533855401959, 15289680275263377042, 14444259489219757497,
-            6497689978530420464, 7473989280813969277, 3566235157971589227,
-            5502428546971923888, 15258817180873710847, 1169964352668698116,
-            5863926115043645599, 15610749783507155661, 5502704700771473503]);
+    assert_eq!(
+        arr_32,
+        [
+            3689073239, 2242961698, 417536494, 1503493075, 2585895478, 1865454281, 2938464816,
+            3998462791, 1427673087, 2023308892, 3934458784, 2154405649, 4261926467, 944974180,
+            1017947150, 1858486886, 2081637745, 6698551, 2593531725, 989113434, 952425310,
+            2821646029, 2012236296, 2466857171, 1705021287, 2712374590, 2467996873, 2529275143,
+            3691921567, 4164253542, 88339832, 3139246687
+        ]
+    );
+    assert_eq!(
+        arr_64,
+        [
+            5208214742801571470,
+            12560379081283373091,
+            16366572874613089836,
+            5810973028396244379,
+            5062530146601697561,
+            217785109116347749,
+            10945155018052356834,
+            3830068552016855006,
+            8631305173908678621,
+            2826768761480962968,
+            10354302052483333752,
+            14791155778847304997,
+            9236871951464247547,
+            10565693803420516117,
+            2303174007018927082,
+            11829861525625440339,
+            17157070760956407958,
+            18121029959197227032,
+            15895614391418239344,
+            16692872359552745709,
+            364895533855401959,
+            15289680275263377042,
+            14444259489219757497,
+            6497689978530420464,
+            7473989280813969277,
+            3566235157971589227,
+            5502428546971923888,
+            15258817180873710847,
+            1169964352668698116,
+            5863926115043645599,
+            15610749783507155661,
+            5502704700771473503
+        ]
+    );
 
-    chacha_obj.reseed(&[1,2,3,4]);
+    chacha_obj.reseed(&[1, 2, 3, 4]);
     for i in 0..32 {
         arr_32[i] = chacha_obj.next_u32();
     }
-    assert_eq!(arr_32, [3931434082, 34647919, 2091071039, 2892552082,
-            3564830854, 314912736, 3076587290, 3191577189, 2464328121,
-            2779997206, 4263592728, 2125004658, 1054360779, 3051661270,
-            3060579487, 3656634963, 1388834776, 581640397, 1637469323,
-            1640397641, 2995955648, 4048658223, 1600465837, 1968152875,
-            1014202063, 3686500314, 3021394822, 2541451384, 2305439270,
-            1469542314, 3487304956, 2286598508]);
+    assert_eq!(
+        arr_32,
+        [
+            3931434082, 34647919, 2091071039, 2892552082, 3564830854, 314912736, 3076587290,
+            3191577189, 2464328121, 2779997206, 4263592728, 2125004658, 1054360779, 3051661270,
+            3060579487, 3656634963, 1388834776, 581640397, 1637469323, 1640397641, 2995955648,
+            4048658223, 1600465837, 1968152875, 1014202063, 3686500314, 3021394822, 2541451384,
+            2305439270, 1469542314, 3487304956, 2286598508
+        ]
+    );
 
-    let mut chacha_obj = ChaChaRng::from_seed(&[5,6,7,8]);
+    let mut chacha_obj = ChaChaRng::from_seed(&[5, 6, 7, 8]);
     for i in 0..32 {
         arr_32[i] = chacha_obj.next_u32();
     }
-    assert_eq!(arr_32, [337897705, 1044901876, 2470587838, 3671677287,
-            1941090964, 2010394378, 3290836538, 1347698920, 2248500576,
-            2681774419, 1002813011, 65452860, 1850555370, 3036027713,
-            2306938851, 3450488430, 2195520755, 38533316, 692482625,
-            3410200303, 1052930979, 4087429478, 2501438173, 2495319620,
-            277097193, 1034107694, 2396644634, 3334027496, 3808708279,
-            3705622889, 874306690, 1237186790]);
+    assert_eq!(
+        arr_32,
+        [
+            337897705, 1044901876, 2470587838, 3671677287, 1941090964, 2010394378, 3290836538,
+            1347698920, 2248500576, 2681774419, 1002813011, 65452860, 1850555370, 3036027713,
+            2306938851, 3450488430, 2195520755, 38533316, 692482625, 3410200303, 1052930979,
+            4087429478, 2501438173, 2495319620, 277097193, 1034107694, 2396644634, 3334027496,
+            3808708279, 3705622889, 874306690, 1237186790
+        ]
+    );
 
     let mut chacha_obj = ChaChaRng::rand(&mut chacha_obj);
     for i in 0..32 {
         arr_32[i] = chacha_obj.next_u32();
     }
-    assert_eq!(arr_32, [1225539984, 2122523891, 4046438709, 2772996924,
-            2441149875, 4253841608, 2837944766, 1256279550, 134014938,
-            192621923, 4187133245, 45039689, 3455715024, 3744176230,
-            3194372814, 3469543760, 1107844861, 3990938641, 4095630261,
-            3461287666, 321560749, 934078781, 2671586377, 1048877492,
-            1523056052, 3373403085, 913407943, 2835509350, 3232974874,
-            3910156243, 3652032104, 442368320]);
+    assert_eq!(
+        arr_32,
+        [
+            1225539984, 2122523891, 4046438709, 2772996924, 2441149875, 4253841608, 2837944766,
+            1256279550, 134014938, 192621923, 4187133245, 45039689, 3455715024, 3744176230,
+            3194372814, 3469543760, 1107844861, 3990938641, 4095630261, 3461287666, 321560749,
+            934078781, 2671586377, 1048877492, 1523056052, 3373403085, 913407943, 2835509350,
+            3232974874, 3910156243, 3652032104, 442368320
+        ]
+    );
 }
 
 // reseeding.rs
@@ -495,7 +760,8 @@
 
     let mut ra: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
     let mut rb: MyRng = SeedableRng::from_seed((ReseedWithDefault, 2));
-    assert!(ra.gen_ascii_chars()
+    assert!(ra
+        .gen_ascii_chars()
         .take(100)
         .eq(rb.gen_ascii_chars().take(100)));
 
diff --git a/samplecode/unit-test/enclave/src/test_rand_cratesio.rs b/samplecode/unit-test/enclave/src/test_rand_cratesio.rs
index d458292..e5f9bb8 100644
--- a/samplecode/unit-test/enclave/src/test_rand_cratesio.rs
+++ b/samplecode/unit-test/enclave/src/test_rand_cratesio.rs
@@ -1,13 +1,13 @@
-use rand::{RngCore, SeedableRng, ChaChaRng};
+use rand::{ChaChaRng, RngCore, SeedableRng};
 
 pub fn test_rand_cratesio() {
-        let seed = [0u8; 32];
-        let mut rng = ChaChaRng::from_seed(seed);
-        let mut results = [0u8; 32];
-        rng.fill_bytes(&mut results);
-        let expected = [118, 184, 224, 173, 160, 241, 61, 144,
-                        64, 93, 106, 229, 83, 134, 189, 40,
-                        189, 210, 25, 184, 160, 141, 237, 26,
-                        168, 54, 239, 204, 139, 119, 13, 199];
-        assert_eq!(results, expected);
+    let seed = [0u8; 32];
+    let mut rng = ChaChaRng::from_seed(seed);
+    let mut results = [0u8; 32];
+    rng.fill_bytes(&mut results);
+    let expected = [
+        118, 184, 224, 173, 160, 241, 61, 144, 64, 93, 106, 229, 83, 134, 189, 40, 189, 210, 25,
+        184, 160, 141, 237, 26, 168, 54, 239, 204, 139, 119, 13, 199,
+    ];
+    assert_eq!(results, expected);
 }
diff --git a/samplecode/unit-test/enclave/src/test_rts.rs b/samplecode/unit-test/enclave/src/test_rts.rs
index 668e50f..3941918 100644
--- a/samplecode/unit-test/enclave/src/test_rts.rs
+++ b/samplecode/unit-test/enclave/src/test_rts.rs
@@ -16,17 +16,17 @@
 // under the License..
 
 use sgx_types::*;
-use std::vec::Vec;
 use std::string::String;
+use std::vec::Vec;
 
-use sgx_trts::trts::*;
-use sgx_trts::veh::*;
 use sgx_trts::ascii::AsciiExt;
 use sgx_trts::c_str::*;
-use sgx_trts::error;
-use sgx_trts::memchr;
-use sgx_trts::libc;
 use sgx_trts::enclave::*;
+use sgx_trts::error;
+use sgx_trts::libc;
+use sgx_trts::memchr;
+use sgx_trts::trts::*;
+use sgx_trts::veh::*;
 
 //Only during dev
 //use core::mem;
@@ -36,7 +36,7 @@
 }
 
 // veh
-extern "C" fn sample_exception_handler(_ : *mut sgx_exception_info_t) -> int32_t {
+extern "C" fn sample_exception_handler(_: *mut sgx_exception_info_t) -> int32_t {
     0
 }
 
@@ -63,12 +63,11 @@
 }
 
 pub fn test_register_multiple_exception_handler() {
-    let mut handler_vec : Vec<exception_handle> = Vec::new();
-    let ntest:usize = 100;
+    let mut handler_vec: Vec<exception_handle> = Vec::new();
+    let ntest: usize = 100;
 
     for i in 0..ntest {
-        let handle = rsgx_register_exception_handler(i as uint32_t % 2,
-                                                     sample_exception_handler);
+        let handle = rsgx_register_exception_handler(i as uint32_t % 2, sample_exception_handler);
         assert!(handle.is_some());
         handler_vec.push(handle.unwrap());
     }
@@ -85,7 +84,7 @@
 }
 
 // trts
-pub fn test_read_rand(){
+pub fn test_read_rand() {
     let mut rand_arr = [0; 100];
     assert_eq!(rsgx_read_rand(&mut rand_arr[..]), Ok(()));
     // Cannot all be zero
@@ -96,13 +95,17 @@
 pub fn test_data_is_within_enclave() {
     #[allow(dead_code)]
     #[derive(Clone, Copy)]
-    struct SampleDs{
+    struct SampleDs {
         x: i32,
         y: i32,
         z: [i32; 100],
     }
     unsafe impl marker::ContiguousMemory for SampleDs {}
-    let mut sample_object : SampleDs = SampleDs{ x: 0, y: 0, z: [0; 100]};
+    let mut sample_object: SampleDs = SampleDs {
+        x: 0,
+        y: 0,
+        z: [0; 100],
+    };
     sample_object.x = 100;
     sample_object.y = 100;
     sample_object.z[0] = 100;
@@ -110,7 +113,7 @@
 
     let ooo;
     unsafe {
-        let ppp = 0xdeadbeafdeadbeaf as * const u8;
+        let ppp = 0xdeadbeafdeadbeaf as *const u8;
         ooo = &*ppp;
     }
     assert_eq!(rsgx_data_is_within_enclave(ooo), false);
@@ -129,25 +132,31 @@
     //assert_eq!(rsgx_slice_is_within_enclave(ooo), false);
 }
 
-pub fn test_raw_is_within_enclave(){
-    assert_eq!(rsgx_raw_is_within_enclave(test_raw_is_within_enclave as * const u8,
-                                          10),
-               true);
-    assert_eq!(rsgx_raw_is_within_enclave(0xdeadbeafdeadbeaf as * const u8,
-                                          10),
-               false);
+pub fn test_raw_is_within_enclave() {
+    assert_eq!(
+        rsgx_raw_is_within_enclave(test_raw_is_within_enclave as *const u8, 10),
+        true
+    );
+    assert_eq!(
+        rsgx_raw_is_within_enclave(0xdeadbeafdeadbeaf as *const u8, 10),
+        false
+    );
 }
 
 pub fn test_data_is_outside_enclave() {
     #[allow(dead_code)]
     #[derive(Clone, Copy)]
-    struct SampleDs{
+    struct SampleDs {
         x: i32,
         y: i32,
         z: [i32; 100],
     }
     unsafe impl marker::ContiguousMemory for SampleDs {}
-    let mut sample_object : SampleDs = SampleDs{ x: 0, y: 0, z: [0; 100]};
+    let mut sample_object: SampleDs = SampleDs {
+        x: 0,
+        y: 0,
+        z: [0; 100],
+    };
     sample_object.x = 100;
     sample_object.y = 100;
     sample_object.z[0] = 100;
@@ -155,7 +164,7 @@
 
     let ooo;
     unsafe {
-        let ppp = 0xdeadbeafdeadbeaf as * const u8;
+        let ppp = 0xdeadbeafdeadbeaf as *const u8;
         ooo = &*ppp;
     }
     assert_eq!(rsgx_data_is_outside_enclave(ooo), true);
@@ -174,18 +183,20 @@
     //assert_eq!(rsgx_slice_is_within_enclave(ooo), true);
 }
 
-pub fn test_raw_is_outside_enclave(){
-    assert_eq!(rsgx_raw_is_outside_enclave(test_raw_is_outside_enclave as * const u8,
-                                          10),
-               false);
-    assert_eq!(rsgx_raw_is_outside_enclave(0xdeadbeafdeadbeaf as * const u8,
-                                          10),
-               true);
+pub fn test_raw_is_outside_enclave() {
+    assert_eq!(
+        rsgx_raw_is_outside_enclave(test_raw_is_outside_enclave as *const u8, 10),
+        false
+    );
+    assert_eq!(
+        rsgx_raw_is_outside_enclave(0xdeadbeafdeadbeaf as *const u8, 10),
+        true
+    );
 }
 
 // macros
 
-pub fn test_global_ctors_object (){
+pub fn test_global_ctors_object() {
     assert_eq!(VARNAME(), ());
 }
 
@@ -195,72 +206,60 @@
 // error
 pub fn test_error() {
     // XXX: Top 11 should be the same in all unix?
-    let errorinfo_vec:Vec<(i32,&'static str)> = vec![
-        (1 , "Operation not permitted"),
-        (2 , "No such file or directory"),
-        (3 , "No such process"),
-        (4 , "Interrupted system call"),
-        (5 , "Input/output error"),
-        (6 , "Device not configured"),
-        (7 , "Argument list too long"),
-        (8 , "Exec format error"),
-        (9 , "Bad file descriptor"),
+    let errorinfo_vec: Vec<(i32, &'static str)> = vec![
+        (1, "Operation not permitted"),
+        (2, "No such file or directory"),
+        (3, "No such process"),
+        (4, "Interrupted system call"),
+        (5, "Input/output error"),
+        (6, "Device not configured"),
+        (7, "Argument list too long"),
+        (8, "Exec format error"),
+        (9, "Bad file descriptor"),
         (10, "No child processes"),
         (11, "Resource deadlock avoided"),
-        ];
+    ];
 
     for case in errorinfo_vec {
-        let mut buf:[i8;64] = [0;64];
+        let mut buf: [i8; 64] = [0; 64];
         error::set_errno(case.0);
-        unsafe { error::error_string(error::errno(),&mut buf[..]);}
-        let answer:Vec<u8> = buf.iter().map(|&x| x as u8).collect();
+        unsafe {
+            error::error_string(error::errno(), &mut buf[..]);
+        }
+        let answer: Vec<u8> = buf.iter().map(|&x| x as u8).collect();
         let ans_str = String::from_utf8(answer).unwrap();
-        assert_eq!(ans_str.trim_matches('\0'),
-                   case.1);
+        assert_eq!(ans_str.trim_matches('\0'), case.1);
     }
-
 }
 
 // libc
-pub fn test_rts_libc_memchr(){
+pub fn test_rts_libc_memchr() {
     let test_str = "abcdedfg";
     assert_eq!(
-            unsafe {
-                libc::memchr(
-                        test_str.as_ptr() as * const u8,
-                        'd' as u8,
-                        test_str.len())},
-            test_str[3..].as_ptr());
+        unsafe { libc::memchr(test_str.as_ptr() as *const u8, 'd' as u8, test_str.len()) },
+        test_str[3..].as_ptr()
+    );
     assert_eq!(
-            unsafe {
-                libc::memchr(
-                        "abcdefg".as_ptr() as * const u8,
-                        'z' as u8,
-                        test_str.len())},
-            0 as * const u8);
+        unsafe { libc::memchr("abcdefg".as_ptr() as *const u8, 'z' as u8, test_str.len()) },
+        0 as *const u8
+    );
 }
 
-pub fn test_rts_libc_memrchr(){
+pub fn test_rts_libc_memrchr() {
     let test_str = "abcdedfg";
     assert_eq!(
-            unsafe {
-                libc::memrchr(
-                        test_str.as_ptr() as * const u8,
-                        'd' as u8,
-                        test_str.len())},
-            test_str[5..].as_ptr());
+        unsafe { libc::memrchr(test_str.as_ptr() as *const u8, 'd' as u8, test_str.len()) },
+        test_str[5..].as_ptr()
+    );
     assert_eq!(
-            unsafe {
-                libc::memrchr(
-                        "abcdefg".as_ptr() as * const u8,
-                        'z' as u8,
-                        test_str.len())},
-            0 as * const u8);
+        unsafe { libc::memrchr("abcdefg".as_ptr() as *const u8, 'z' as u8, test_str.len()) },
+        0 as *const u8
+    );
 }
 
 // memchr
 
-pub fn test_rts_memchr_memchr(){
+pub fn test_rts_memchr_memchr() {
     let test_str = "abcdedfg".as_bytes();
     let needle = 'd' as u8;
     assert_eq!(memchr::memchr(needle, test_str), Some(3));
@@ -268,7 +267,7 @@
     assert_eq!(memchr::memchr(needle, test_str), None);
 }
 
-pub fn test_rts_memchr_memrchr(){
+pub fn test_rts_memchr_memrchr() {
     let test_str = "abcdedfg".as_bytes();
     let needle = 'd' as u8;
     assert_eq!(memchr::memrchr(needle, test_str), Some(5));
@@ -277,7 +276,7 @@
 }
 
 // ascii
-pub fn test_ascii(){
+pub fn test_ascii() {
     assert_eq!("café".to_ascii_uppercase(), "CAFÉ");
     assert_eq!("café".to_ascii_uppercase(), "CAFé");
 
@@ -292,7 +291,6 @@
     assert_eq!('❤', non_ascii.to_ascii_uppercase());
     assert_eq!(65, int_ascii.to_ascii_uppercase());
 
-
     let ascii = 'A';
     let non_ascii = '❤';
     let int_ascii = 65;
@@ -320,7 +318,7 @@
 }
 
 // c_str
-pub fn test_cstr(){
+pub fn test_cstr() {
     let c_string = CString::new("foo").unwrap();
     let ptr = c_string.into_raw();
 
@@ -373,7 +371,7 @@
 
     let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
     assert_eq!(
-                c_str.to_string_lossy(),
-                    Cow::Owned(String::from("Hello �World")) as Cow<str>
-            );
+        c_str.to_string_lossy(),
+        Cow::Owned(String::from("Hello �World")) as Cow<str>
+    );
 }
diff --git a/samplecode/unit-test/enclave/src/test_seal.rs b/samplecode/unit-test/enclave/src/test_seal.rs
index 8c02afe..5b87750 100644
--- a/samplecode/unit-test/enclave/src/test_seal.rs
+++ b/samplecode/unit-test/enclave/src/test_seal.rs
@@ -17,26 +17,33 @@
 
 use sgx_rand::*;
 use sgx_tseal::*;
-use sgx_types::*;
 use sgx_types::marker::*;
+use sgx_types::*;
 use std::prelude::v1::*;
 
-fn to_sealed_log<T: Copy + ContiguousMemory>(sealed_data: &SgxSealedData<T>,
-                                             sealed_log: * mut u8,
-                                             sealed_log_size: u32)
-                                             -> Option<* mut sgx_sealed_data_t> {
+fn to_sealed_log<T: Copy + ContiguousMemory>(
+    sealed_data: &SgxSealedData<T>,
+    sealed_log: *mut u8,
+    sealed_log_size: u32,
+) -> Option<*mut sgx_sealed_data_t> {
     unsafe {
-        sealed_data.to_raw_sealed_data_t(sealed_log as * mut sgx_sealed_data_t, sealed_log_size)
+        sealed_data.to_raw_sealed_data_t(sealed_log as *mut sgx_sealed_data_t, sealed_log_size)
     }
 }
 
-fn from_sealed_log<'a, T: Copy + ContiguousMemory>(sealed_log: * mut u8, sealed_log_size: u32) -> Option<SgxSealedData<'a, T>> {
+fn from_sealed_log<'a, T: Copy + ContiguousMemory>(
+    sealed_log: *mut u8,
+    sealed_log_size: u32,
+) -> Option<SgxSealedData<'a, T>> {
     unsafe {
-        SgxSealedData::<T>::from_raw_sealed_data_t(sealed_log as * mut sgx_sealed_data_t, sealed_log_size)
+        SgxSealedData::<T>::from_raw_sealed_data_t(
+            sealed_log as *mut sgx_sealed_data_t,
+            sealed_log_size,
+        )
     }
 }
 
-pub fn test_seal_unseal () {
+pub fn test_seal_unseal() {
     #[derive(Copy, Clone, Default, Debug)]
     struct RandData {
         key: u32,
@@ -53,9 +60,9 @@
     let aad: [u8; 0] = [0_u8; 0];
     let sealed_data = SgxSealedData::<RandData>::seal_data(&aad, &data).unwrap();
 
-    let mut sealed_log_arr:[u8;2048] = [0;2048];
+    let mut sealed_log_arr: [u8; 2048] = [0; 2048];
     let sealed_log = sealed_log_arr.as_mut_ptr();
-    let sealed_log_size : u32 = 2048;
+    let sealed_log_size: u32 = 2048;
     let opt = to_sealed_log(&sealed_data, sealed_log, sealed_log_size);
     assert_eq!(opt.is_some(), true);
 
@@ -69,34 +76,40 @@
 pub fn test_number_sealing() {
     let data: u64 = 123456789;
     let aad: [u8; 0] = [0_u8; 0];
-    let sealed_data = SgxSealedData::<u64>::seal_data(&aad, &data).expect("error while sealing u64");
-    let unsealed_data = sealed_data.unseal_data().expect("error while unsealing u64");
+    let sealed_data =
+        SgxSealedData::<u64>::seal_data(&aad, &data).expect("error while sealing u64");
+    let unsealed_data = sealed_data
+        .unseal_data()
+        .expect("error while unsealing u64");
     assert_eq!(*unsealed_data.get_decrypt_txt(), data);
 }
 
 pub fn test_array_sealing() {
     let data: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
     let aad: [u8; 0] = [0_u8; 0];
-    let sealed_data = SgxSealedData::<[u8]>::seal_data(&aad, &data).expect("error while sealing array");
-    let unsealed_data = sealed_data.unseal_data().expect("error while unsealing array");
+    let sealed_data =
+        SgxSealedData::<[u8]>::seal_data(&aad, &data).expect("error while sealing array");
+    let unsealed_data = sealed_data
+        .unseal_data()
+        .expect("error while unsealing array");
     assert_eq!(unsealed_data.get_decrypt_txt(), data);
 }
 
 pub fn test_mac_aadata_number() {
-    let aad_data  : u64 = 123456789;
+    let aad_data: u64 = 123456789;
     let mmac = SgxMacAadata::<u64>::mac_aadata(&aad_data).expect("error while mac data");
     let unsealed_mac = mmac.unmac_aadata().expect("error when unmac data");
     let inner = Box::into_raw(unsealed_mac);
-    let inner_val = unsafe { * (inner as *mut u64) };
+    let inner_val = unsafe { *(inner as *mut u64) };
     assert_eq!(inner_val, aad_data);
 }
 
 pub fn test_mac_aadata_slice() {
     use std::slice;
-    let aad_data  : [u8;10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+    let aad_data: [u8; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
     let mmac = SgxMacAadata::<[u8]>::mac_aadata(&aad_data).expect("error while mac data");
     let unsealed_mac = mmac.unmac_aadata().expect("error when unmac data");
     let inner = Box::into_raw(unsealed_mac);
-    let inner_slice = unsafe {slice::from_raw_parts(inner as *mut u8, 10)};
+    let inner_slice = unsafe { slice::from_raw_parts(inner as *mut u8, 10) };
     assert_eq!(inner_slice, aad_data);
 }
diff --git a/samplecode/unit-test/enclave/src/test_serialize.rs b/samplecode/unit-test/enclave/src/test_serialize.rs
index b22dd16..c3e83db 100644
--- a/samplecode/unit-test/enclave/src/test_serialize.rs
+++ b/samplecode/unit-test/enclave/src/test_serialize.rs
@@ -1,9 +1,9 @@
-use std::vec::Vec;
+use sgx_serialize::{DeSerializable, DeSerializeHelper, Serializable, SerializeHelper};
 use std::fmt::Debug;
-use std::string::{ToString, String};
-use sgx_serialize::{Serializable, DeSerializable, SerializeHelper, DeSerializeHelper};
+use std::string::{String, ToString};
+use std::vec::Vec;
 
-fn test_serialize_internal<T: Serializable + DeSerializable>(target: &T) -> Option<T>{
+fn test_serialize_internal<T: Serializable + DeSerializable>(target: &T) -> Option<T> {
     let helper = SerializeHelper::new();
     let data = helper.encode(target).unwrap();
     let helper = DeSerializeHelper::<T>::new(data);
@@ -16,7 +16,10 @@
         a1: u32,
         a2: u32,
     }
-    let a = TestSturct {a1: 2017u32, a2: 829u32};
+    let a = TestSturct {
+        a1: 2017u32,
+        a2: 829u32,
+    };
     let c = test_serialize_internal::<TestSturct>(&a).unwrap();
     assert_eq!(a, c);
 
@@ -50,11 +53,14 @@
         EnumUnit,
         EnumNewType(u32),
         EnumTuple(u32, u32),
-        EnumStruct{a1:i32, a2:i32},
+        EnumStruct { a1: i32, a2: i32 },
         EnumSubStruct(TestSturct),
     }
 
-    let a = TestSturct { a1: 2017u32, a2: 928u32 };
+    let a = TestSturct {
+        a1: 2017u32,
+        a2: 928u32,
+    };
     let a = TestEnum::EnumSubStruct(a);
     let c = test_serialize_internal::<TestEnum>(&a).unwrap();
     assert_eq!(a, c);
@@ -67,7 +73,7 @@
     let c = test_serialize_internal::<TestEnum>(&a).unwrap();
     assert_eq!(a, c);
 
-    let a = TestEnum::EnumStruct {a1: 2017, a2:829};
+    let a = TestEnum::EnumStruct { a1: 2017, a2: 829 };
     let c = test_serialize_internal::<TestEnum>(&a).unwrap();
     assert_eq!(a, c);
 }
@@ -130,7 +136,17 @@
     }
 
     fn test_usize() {
-        check_round_trip(vec![1, 2, 3, ::std::usize::MIN, 0, 1, ::std::usize::MAX, 2, 1]);
+        check_round_trip(vec![
+            1,
+            2,
+            3,
+            ::std::usize::MIN,
+            0,
+            1,
+            ::std::usize::MAX,
+            2,
+            1,
+        ]);
     }
 
     fn test_i8() {
@@ -148,15 +164,45 @@
     }
 
     fn test_i32() {
-        check_round_trip(vec![-1, 2, -3, ::std::i32::MIN, 0, 1, ::std::i32::MAX, 2, 1]);
+        check_round_trip(vec![
+            -1,
+            2,
+            -3,
+            ::std::i32::MIN,
+            0,
+            1,
+            ::std::i32::MAX,
+            2,
+            1,
+        ]);
     }
 
     fn test_i64() {
-        check_round_trip(vec![-1, 2, -3, ::std::i64::MIN, 0, 1, ::std::i64::MAX, 2, 1]);
+        check_round_trip(vec![
+            -1,
+            2,
+            -3,
+            ::std::i64::MIN,
+            0,
+            1,
+            ::std::i64::MAX,
+            2,
+            1,
+        ]);
     }
 
     fn test_isize() {
-        check_round_trip(vec![-1, 2, -3, ::std::isize::MIN, 0, 1, ::std::isize::MAX, 2, 1]);
+        check_round_trip(vec![
+            -1,
+            2,
+            -3,
+            ::std::isize::MIN,
+            0,
+            1,
+            ::std::isize::MAX,
+            2,
+            1,
+        ]);
     }
 
     fn test_bool() {
@@ -185,13 +231,15 @@
     }
 
     fn test_string() {
-        let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
-                       "abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
-                       "abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
-                       "abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
-                       "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
-                       "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
-                       "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()];
+        let vec = vec![
+            "abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
+            "abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
+            "abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
+            "abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
+            "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
+            "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(),
+            "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string(),
+        ];
 
         check_round_trip(vec);
     }
@@ -221,74 +269,72 @@
 
     fn test_struct() {
         check_round_trip(vec![Struct {
-                                  a: (),
-                                  b: 10,
-                                  c: 11,
-                                  d: 12,
-                                  e: 13,
-                                  f: 14,
+            a: (),
+            b: 10,
+            c: 11,
+            d: 12,
+            e: 13,
+            f: 14,
 
-                                  g: 15,
-                                  h: 16,
-                                  i: 17,
-                                  j: 18,
-                                  k: 19,
+            g: 15,
+            h: 16,
+            i: 17,
+            j: 18,
+            k: 19,
 
-                                  l: 'x',
-                                  m: "abc".to_string(),
-                                  n: 20.5,
-                                  o: 21.5,
-                                  p: false,
-                                  q: None,
-                              }]);
+            l: 'x',
+            m: "abc".to_string(),
+            n: 20.5,
+            o: 21.5,
+            p: false,
+            q: None,
+        }]);
 
         check_round_trip(vec![Struct {
-                                  a: (),
-                                  b: 101,
-                                  c: 111,
-                                  d: 121,
-                                  e: 131,
-                                  f: 141,
+            a: (),
+            b: 101,
+            c: 111,
+            d: 121,
+            e: 131,
+            f: 141,
 
-                                  g: -15,
-                                  h: -16,
-                                  i: -17,
-                                  j: -18,
-                                  k: -19,
+            g: -15,
+            h: -16,
+            i: -17,
+            j: -18,
+            k: -19,
 
-                                  l: 'y',
-                                  m: "def".to_string(),
-                                  n: -20.5,
-                                  o: -21.5,
-                                  p: true,
-                                  q: Some(1234567),
-                              }]);
+            l: 'y',
+            m: "def".to_string(),
+            n: -20.5,
+            o: -21.5,
+            p: true,
+            q: Some(1234567),
+        }]);
     }
 
     #[derive(PartialEq, Clone, Debug, Serializable, DeSerializable)]
     enum Enum {
         Variant1,
         Variant2(usize, f32),
-        Variant3 {
-            a: i32,
-            b: char,
-            c: bool,
-        },
+        Variant3 { a: i32, b: char, c: bool },
     }
 
     fn test_enum() {
-        check_round_trip(vec![Enum::Variant1,
-                              Enum::Variant2(1, 2.5),
-                              Enum::Variant3 {
-                                  a: 3,
-                                  b: 'b',
-                                  c: false,
-                              },
-                              Enum::Variant3 {
-                                  a: -4,
-                                  b: 'f',
-                                  c: true,
-                              }]);
+        check_round_trip(vec![
+            Enum::Variant1,
+            Enum::Variant2(1, 2.5),
+            Enum::Variant3 {
+                a: 3,
+                b: 'b',
+                c: false,
+            },
+            Enum::Variant3 {
+                a: -4,
+                b: 'f',
+                c: true,
+            },
+        ]);
     }
 
     fn test_sequence() {
@@ -340,4 +386,4 @@
     test_sequence();
     test_hash_map();
     test_tuples();
-}
\ No newline at end of file
+}
diff --git a/samplecode/unit-test/enclave/src/test_signal.rs b/samplecode/unit-test/enclave/src/test_signal.rs
index c944e14..0a703f9 100644
--- a/samplecode/unit-test/enclave/src/test_signal.rs
+++ b/samplecode/unit-test/enclave/src/test_signal.rs
@@ -1,128 +1,127 @@
+use sgx_libc::ocall::getpid;
+use sgx_libc::{c_int, pid_t, siginfo_t, SIGILL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2};
+use sgx_signal::signal::{
+    raise_signal, register, register_sigaction, unregister, unregister_signal,
+};
 use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Arc;
 use std::thread;
 use std::time::Duration;
-use sgx_libc::{siginfo_t, c_int, pid_t, SIGUSR1, SIGUSR2, SIGILL, SIGTERM,SIGINT};
-use sgx_libc::ocall::getpid;
-use sgx_signal::signal::{register, register_sigaction, unregister, unregister_signal, raise_signal};
 
 pub fn test_signal_forbidden() {
-   let ret = register(SIGILL, || ());
-   assert_eq!(ret.is_ok(), false);
+    let ret = register(SIGILL, || ());
+    assert_eq!(ret.is_ok(), false);
 }
 
 pub fn test_signal_without_pid() {
-   let status = Arc::new(AtomicUsize::new(0));
-   let action = {
-      let status = Arc::clone(&status);
-      move || {
-         status.store(1, Ordering::Relaxed);
-      }
-   };
+    let status = Arc::new(AtomicUsize::new(0));
+    let action = {
+        let status = Arc::clone(&status);
+        move || {
+            status.store(1, Ordering::Relaxed);
+        }
+    };
 
-   register(SIGUSR2, action).unwrap();
-   raise_signal(SIGUSR2);
+    register(SIGUSR2, action).unwrap();
+    raise_signal(SIGUSR2);
 
-   for _ in 0..10 {
-      thread::sleep(Duration::from_millis(100));
-      let current = status.load(Ordering::Relaxed);
-      match current {
-         // Not yet
-         0 => continue,
-         // Good, we are done with the correct result
-         _ if current == 1 => return,
-         _ => panic!("Wrong result value {}", current),
-      }
-   }
-   panic!("Timed out waiting for the signal");
+    for _ in 0..10 {
+        thread::sleep(Duration::from_millis(100));
+        let current = status.load(Ordering::Relaxed);
+        match current {
+            // Not yet
+            0 => continue,
+            // Good, we are done with the correct result
+            _ if current == 1 => return,
+            _ => panic!("Wrong result value {}", current),
+        }
+    }
+    panic!("Timed out waiting for the signal");
 }
 
 pub fn test_signal_with_pid() {
-   let status = Arc::new(AtomicUsize::new(0));
-   let action = {
-      let status = Arc::clone(&status);
-      move |siginfo: &siginfo_t| {
-         // Hack: currently, libc exposes only the first 3 fields of siginfo_t. The pid
-         // comes somewhat later on. Therefore, we do a Really Ugly Hack and define our
-         // own structure (and hope it is correct on all platforms). But hey, this is
-         // only the tests, so we are going to get away with this.
-         #[repr(C)]
-         struct SigInfo {
-            _fields: [c_int; 3],
-            #[cfg(all(target_pointer_width = "64", target_os = "linux"))]
-            _pad: c_int,
-            pid: pid_t,
-         }
-         let info: &SigInfo = unsafe {
-            (siginfo as *const _ as *const SigInfo)
-               .as_ref()
-               .unwrap()
-         };
+    let status = Arc::new(AtomicUsize::new(0));
+    let action = {
+        let status = Arc::clone(&status);
+        move |siginfo: &siginfo_t| {
+            // Hack: currently, libc exposes only the first 3 fields of siginfo_t. The pid
+            // comes somewhat later on. Therefore, we do a Really Ugly Hack and define our
+            // own structure (and hope it is correct on all platforms). But hey, this is
+            // only the tests, so we are going to get away with this.
+            #[repr(C)]
+            struct SigInfo {
+                _fields: [c_int; 3],
+                #[cfg(all(target_pointer_width = "64", target_os = "linux"))]
+                _pad: c_int,
+                pid: pid_t,
+            }
+            let info: &SigInfo =
+                unsafe { (siginfo as *const _ as *const SigInfo).as_ref().unwrap() };
 
-         status.store(info.pid as usize, Ordering::Relaxed);
-      }
-   };
+            status.store(info.pid as usize, Ordering::Relaxed);
+        }
+    };
 
-   let pid = unsafe { getpid() };
-   register_sigaction(SIGUSR2, action).unwrap();
-   raise_signal(SIGUSR2);
+    let pid = unsafe { getpid() };
+    register_sigaction(SIGUSR2, action).unwrap();
+    raise_signal(SIGUSR2);
 
-   for _ in 0..10 {
-      thread::sleep(Duration::from_millis(100));
-      let current = status.load(Ordering::Relaxed);
-      match current {
-         // Not yet (PID == 0 doesn't happen)
-         0 => continue,
-         // Good, we are done with the correct result
-         _ if current == pid as usize => return,
-         _ => panic!("Wrong status value {}", current),
-      }
-   }
-   panic!("Timed out waiting for the signal");
+    for _ in 0..10 {
+        thread::sleep(Duration::from_millis(100));
+        let current = status.load(Ordering::Relaxed);
+        match current {
+            // Not yet (PID == 0 doesn't happen)
+            0 => continue,
+            // Good, we are done with the correct result
+            _ if current == pid as usize => return,
+            _ => panic!("Wrong status value {}", current),
+        }
+    }
+    panic!("Timed out waiting for the signal");
 }
 
 // Check that registration works as expected and that unregister tells if it did or not.
 pub fn test_signal_register_unregister() {
-   let signal = register(SIGUSR1, || ()).unwrap();
-   // It was there now, so we can unregister
-   assert!(unregister(signal));
-   // The next time unregistering does nothing and tells us so.
-   assert!(!unregister(signal));
+    let signal = register(SIGUSR1, || ()).unwrap();
+    // It was there now, so we can unregister
+    assert!(unregister(signal));
+    // The next time unregistering does nothing and tells us so.
+    assert!(!unregister(signal));
 }
 
 pub fn test_signal_register_unregister1() {
-   let called = Arc::new(AtomicUsize::new(0));
-   let action = {
-      let called = Arc::clone(&called);
-      move || {
-         called.fetch_add(1, Ordering::Relaxed);
-      }
-   };
+    let called = Arc::new(AtomicUsize::new(0));
+    let action = {
+        let called = Arc::clone(&called);
+        move || {
+            called.fetch_add(1, Ordering::Relaxed);
+        }
+    };
 
-   register(SIGTERM, action.clone()).unwrap();
-   register(SIGTERM, action.clone()).unwrap();
-   register(SIGINT, action.clone()).unwrap();
+    register(SIGTERM, action.clone()).unwrap();
+    register(SIGTERM, action.clone()).unwrap();
+    register(SIGINT, action.clone()).unwrap();
 
-   raise_signal(SIGTERM);
-   // The closure is run twice.
-   assert_eq!(2, called.load(Ordering::Relaxed));
+    raise_signal(SIGTERM);
+    // The closure is run twice.
+    assert_eq!(2, called.load(Ordering::Relaxed));
 
-   assert!(unregister_signal(SIGTERM));
+    assert!(unregister_signal(SIGTERM));
 
-   raise_signal(SIGTERM);
-   // Second one unregisters nothing.
-   assert!(!unregister_signal(SIGTERM));
+    raise_signal(SIGTERM);
+    // Second one unregisters nothing.
+    assert!(!unregister_signal(SIGTERM));
 
-   // After unregistering (both), it is no longer run at all.
-   assert_eq!(2, called.load(Ordering::Relaxed));
+    // After unregistering (both), it is no longer run at all.
+    assert_eq!(2, called.load(Ordering::Relaxed));
 
-   // The SIGINT one is not disturbed.
-   raise_signal(SIGINT);
-   assert_eq!(3, called.load(Ordering::Relaxed));
+    // The SIGINT one is not disturbed.
+    raise_signal(SIGINT);
+    assert_eq!(3, called.load(Ordering::Relaxed));
 
-   // But it's possible to register it again just fine.
-   register(SIGTERM, action).unwrap();
+    // But it's possible to register it again just fine.
+    register(SIGTERM, action).unwrap();
 
-   raise_signal(SIGTERM);
-   assert_eq!(4, called.load(Ordering::Relaxed));
+    raise_signal(SIGTERM);
+    assert_eq!(4, called.load(Ordering::Relaxed));
 }
diff --git a/samplecode/unit-test/enclave/src/test_thread.rs b/samplecode/unit-test/enclave/src/test_thread.rs
index 597daaa..21ef4e7 100644
--- a/samplecode/unit-test/enclave/src/test_thread.rs
+++ b/samplecode/unit-test/enclave/src/test_thread.rs
@@ -1,34 +1,42 @@
-use std::mem;
 use std::any::Any;
-use std::time::Duration;
-use std::result;
-use std::thread;
-use std::panic;
-use std::thread::Builder;
-use std::thread::sleep;
-use std::thread::ThreadId;
 use std::boxed::Box;
+use std::mem;
+use std::panic;
+use std::result;
 use std::string::String;
 use std::string::ToString;
-use std::u32;
 use std::sync::mpsc::{channel, Sender};
+use std::thread;
+use std::thread::sleep;
+use std::thread::Builder;
+use std::thread::ThreadId;
+use std::time::Duration;
+use std::u32;
 
 pub fn test_thread_unnamed_thread() {
-    thread::spawn(move|| {
+    thread::spawn(move || {
         assert!(thread::current().name().is_none());
-    }).join().ok().expect("thread panicked");
+    })
+    .join()
+    .ok()
+    .expect("thread panicked");
 }
 
 pub fn test_thread_named_thread() {
-    Builder::new().name("ada lovelace".to_string()).spawn(move|| {
-        assert!(thread::current().name().unwrap() == "ada lovelace".to_string());
-    }).unwrap().join().unwrap();
+    Builder::new()
+        .name("ada lovelace".to_string())
+        .spawn(move || {
+            assert!(thread::current().name().unwrap() == "ada lovelace".to_string());
+        })
+        .unwrap()
+        .join()
+        .unwrap();
 }
 
-pub fn test_thread_run_basic(){
+pub fn test_thread_run_basic() {
     let (tx, rx) = channel();
-        thread::spawn(move|| {
-            tx.send(()).unwrap();
+    thread::spawn(move || {
+        tx.send(()).unwrap();
     });
     rx.recv().unwrap();
 }
@@ -37,7 +45,7 @@
     let (tx, rx) = channel();
     fn f(i: i32, tx: Sender<()>) {
         let tx = tx.clone();
-        thread::spawn(move|| {
+        thread::spawn(move || {
             if i == 0 {
                 tx.send(()).unwrap();
             } else {
@@ -52,21 +60,24 @@
 pub fn test_thread_spawn_sched_childs_on_default_sched() {
     let (tx, rx) = channel();
 
-    thread::spawn(move|| {
-        thread::spawn(move|| {
+    thread::spawn(move || {
+        thread::spawn(move || {
             tx.send(()).unwrap();
         });
     });
     rx.recv().unwrap();
 }
 
-fn avoid_copying_the_body<F>(spawnfn: F) where F: FnOnce(Box<dyn Fn() + Send>) {
+fn avoid_copying_the_body<F>(spawnfn: F)
+where
+    F: FnOnce(Box<dyn Fn() + Send>),
+{
     let (tx, rx) = channel();
 
     let x: Box<_> = Box::new(1);
     let x_in_parent = (&*x) as *const i32 as usize;
 
-    spawnfn(Box::new(move|| {
+    spawnfn(Box::new(move || {
         let x_in_child = (&*x) as *const i32 as usize;
         tx.send(x_in_child).unwrap();
     }));
@@ -83,7 +94,7 @@
 
 pub fn test_thread_test_avoid_copying_the_body_thread_spawn() {
     avoid_copying_the_body(|f| {
-        thread::spawn(move|| {
+        thread::spawn(move || {
             f();
         });
     })
@@ -91,22 +102,20 @@
 
 pub fn test_thread_test_avoid_copying_the_body_join() {
     avoid_copying_the_body(|f| {
-        let _ = thread::spawn(move|| {
-            f()
-        }).join();
+        let _ = thread::spawn(move || f()).join();
     })
 }
 
 pub fn test_thread_invalid_named_thread() {
-    should_panic!(Builder::new().name("ada l\0velace".to_string()).spawn(|| {}));
+    should_panic!(Builder::new()
+        .name("ada l\0velace".to_string())
+        .spawn(|| {}));
 }
 
 pub fn test_thread_join_panic() {
-    match thread::spawn(move|| {
-        panic!()
-    }).join() {
+    match thread::spawn(move || panic!()).join() {
         result::Result::Err(_) => (),
-        result::Result::Ok(()) => panic!()
+        result::Result::Ok(()) => panic!(),
     }
 }
 
@@ -117,50 +126,56 @@
     // valgrind-friendly. try this at home, instead..!)
     const GENERATIONS: u32 = 16;
     fn child_no(x: u32) -> Box<dyn Fn() + Send> {
-        return Box::new(move|| {
+        return Box::new(move || {
             if x < GENERATIONS {
-                thread::spawn(move|| child_no(x+1)());
+                thread::spawn(move || child_no(x + 1)());
             }
         });
     }
     thread::spawn(|| child_no(0)());
-} 
+}
 
 pub fn test_thread_simple_newsched_spawn() {
-    let j = thread::spawn(move || {println!("test")});
+    let j = thread::spawn(move || println!("test"));
     let _res = j.join();
 }
 
 pub fn test_thread_try_panic_message_static_str() {
-    match thread::spawn(move|| {
+    match thread::spawn(move || {
         panic!("static string");
-    }).join() {
+    })
+    .join()
+    {
         Err(e) => {
             type T = &'static str;
             assert!(e.is::<T>());
             assert_eq!(*e.downcast::<T>().unwrap(), "static string");
         }
-        Ok(()) => panic!()
+        Ok(()) => panic!(),
     }
 }
 
 pub fn test_thread_try_panic_message_owned_str() {
-    match thread::spawn(move|| {
-        panic!("owned string".to_string());
-    }).join() {
+    match thread::spawn(move || {
+        panic::panic_any("owned string".to_string());
+    })
+    .join()
+    {
         Err(e) => {
             type T = String;
             assert!(e.is::<T>());
             assert_eq!(*e.downcast::<T>().unwrap(), "owned string".to_string());
         }
-        Ok(()) => panic!()
+        Ok(()) => panic!(),
     }
 }
 
 pub fn test_thread_try_panic_message_any() {
-    match thread::spawn(move|| {
-        panic!(Box::new(413u16) as Box<dyn Any + Send>);
-    }).join() {
+    match thread::spawn(move || {
+        panic::panic_any(Box::new(413u16) as Box<dyn Any + Send>);
+    })
+    .join()
+    {
         Err(e) => {
             type T = Box<dyn Any + Send>;
             assert!(e.is::<T>());
@@ -168,16 +183,14 @@
             assert!(any.is::<u16>());
             assert_eq!(*any.downcast::<u16>().unwrap(), 413);
         }
-        Ok(()) => panic!()
+        Ok(()) => panic!(),
     }
 }
 pub fn test_thread_try_panic_message_unit_struct() {
     struct Juju;
-    match thread::spawn(move|| {
-        panic!(Juju)
-    }).join() {
+    match thread::spawn(move || panic::panic_any(Juju)).join() {
         Err(ref e) if e.is::<Juju>() => {}
-        Err(_) | Ok(()) => panic!()
+        Err(_) | Ok(()) => panic!(),
     }
 }
 
@@ -189,7 +202,7 @@
 }
 
 pub fn test_thread_park_timeout_unpark_not_called() {
-    for i in 0..8{
+    for i in 0..8 {
         println!("{:?} timeout in ", i);
         thread::park_timeout(Duration::from_millis(1000));
         println!("{:?} timeout", i);
@@ -214,7 +227,10 @@
 }
 
 pub fn test_thread_size_of_option_thread_id() {
-    assert_eq!(mem::size_of::<Option<ThreadId>>(), mem::size_of::<ThreadId>());
+    assert_eq!(
+        mem::size_of::<Option<ThreadId>>(),
+        mem::size_of::<ThreadId>()
+    );
 }
 
 pub fn test_thread_id_equal() {
@@ -225,5 +241,3 @@
     let spawned_id = thread::spawn(|| thread::current().id()).join().unwrap();
     assert!(thread::current().id() != spawned_id);
 }
-
-
diff --git a/samplecode/unit-test/enclave/src/test_time.rs b/samplecode/unit-test/enclave/src/test_time.rs
index a9ec118..a0a3f1d 100644
--- a/samplecode/unit-test/enclave/src/test_time.rs
+++ b/samplecode/unit-test/enclave/src/test_time.rs
@@ -3,13 +3,13 @@
 
 pub fn test_std_time() {
     macro_rules! assert_almost_eq {
-        ($a:expr, $b:expr) => ({
+        ($a:expr, $b:expr) => {{
             let (a, b) = ($a, $b);
             if a != b {
-                let (a, b) = if a > b {(a, b)} else {(b, a)};
+                let (a, b) = if a > b { (a, b) } else { (b, a) };
                 assert!(a - Duration::new(0, 100) <= b);
             }
-        })
+        }};
     }
 
     {
@@ -61,8 +61,7 @@
 
         let second = Duration::new(1, 0);
         assert_almost_eq!(a.duration_since(a - second).unwrap(), second);
-        assert_almost_eq!(a.duration_since(a + second).unwrap_err()
-                           .duration(), second);
+        assert_almost_eq!(a.duration_since(a + second).unwrap_err().duration(), second);
 
         assert_almost_eq!(a - second + second, a);
 
@@ -74,8 +73,8 @@
         //}
 
         let one_second_from_epoch = UNIX_EPOCH + Duration::new(1, 0);
-        let one_second_from_epoch2 = UNIX_EPOCH + Duration::new(0, 500_000_000)
-            + Duration::new(0, 500_000_000);
+        let one_second_from_epoch2 =
+            UNIX_EPOCH + Duration::new(0, 500_000_000) + Duration::new(0, 500_000_000);
         assert_eq!(one_second_from_epoch, one_second_from_epoch2);
     }
 
@@ -109,4 +108,3 @@
         assert!(a < hundred_twenty_years);
     }
 }
-
diff --git a/samplecode/unit-test/enclave/src/test_types.rs b/samplecode/unit-test/enclave/src/test_types.rs
index a8cd504..8698b43 100644
--- a/samplecode/unit-test/enclave/src/test_types.rs
+++ b/samplecode/unit-test/enclave/src/test_types.rs
@@ -18,7 +18,16 @@
 pub fn check_version() {
     //https://github.com/intel/linux-sgx/blob/master/common/inc/internal/metadata.h#L41
     let curr_version = 0x0000000200000004;
-    assert_eq!(meta_data_make_version!(MAJOR_VERSION,MINOR_VERSION), curr_version);
-    assert_eq!(major_version_of_metadata!(curr_version), MAJOR_VERSION as u64);
-    assert_eq!(minor_version_of_metadata!(curr_version), MINOR_VERSION as u64);
+    assert_eq!(
+        meta_data_make_version!(MAJOR_VERSION, MINOR_VERSION),
+        curr_version
+    );
+    assert_eq!(
+        major_version_of_metadata!(curr_version),
+        MAJOR_VERSION as u64
+    );
+    assert_eq!(
+        minor_version_of_metadata!(curr_version),
+        MINOR_VERSION as u64
+    );
 }
diff --git a/samplecode/unit-test/enclave/src/utils.rs b/samplecode/unit-test/enclave/src/utils.rs
index d61c103..68810b5 100644
--- a/samplecode/unit-test/enclave/src/utils.rs
+++ b/samplecode/unit-test/enclave/src/utils.rs
@@ -20,10 +20,12 @@
 pub fn hex_to_bytes(hex_string: &str) -> Vec<u8> {
     let input_chars: Vec<_> = hex_string.chars().collect();
 
-    input_chars.chunks(2).map(|chunk| {
-        let first_byte = chunk[0].to_digit(16).unwrap();
-        let second_byte = chunk[1].to_digit(16).unwrap();
-        ((first_byte << 4) | second_byte) as u8
-    }).collect()
+    input_chars
+        .chunks(2)
+        .map(|chunk| {
+            let first_byte = chunk[0].to_digit(16).unwrap();
+            let second_byte = chunk[1].to_digit(16).unwrap();
+            ((first_byte << 4) | second_byte) as u8
+        })
+        .collect()
 }
-
diff --git a/sgx_align_struct_attribute/Cargo.toml b/sgx_align_struct_attribute/Cargo.toml
index d007343..fb81633 100644
--- a/sgx_align_struct_attribute/Cargo.toml
+++ b/sgx_align_struct_attribute/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_align_struct_attribute"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_align_struct_attribute/src/align.rs b/sgx_align_struct_attribute/src/align.rs
index e023831..9e3a523 100644
--- a/sgx_align_struct_attribute/src/align.rs
+++ b/sgx_align_struct_attribute/src/align.rs
@@ -25,6 +25,7 @@
     Token, Type,
 };
 
+#[allow(dead_code)]
 struct KeyValue {
     pub ident: Ident,
     pub punct: Punct,
@@ -62,8 +63,7 @@
         let align_iter = self
             .vars
             .iter()
-            .filter(|v| v.ident.to_string() == "align")
-            .next();
+            .find(|v| v.ident.to_string() == "align");
         let align: usize = if let Some(align_value) = align_iter {
             self.parse_align(&align_value.value)?
         } else {
@@ -76,8 +76,7 @@
         let size_iter = self
             .vars
             .iter()
-            .filter(|v| v.ident.to_string() == "size")
-            .next();
+            .find(|v| v.ident.to_string() == "size");
         let size: usize = if let Some(size_value) = size_iter {
             self.parse_size(&size_value.value)?
         } else {
@@ -194,7 +193,7 @@
             let ident = attr.path.get_ident();
             let meta = attr.parse_meta();
             if ident.map_or(false, |v| v.to_string() == "repr") && meta.is_ok() {
-                if let Some(Meta::List(ref m)) = meta.ok() {
+                if let Ok(Meta::List(ref m)) = meta {
                     if m.nested.len() > 0 {
                         erxcept_attr = m
                             .nested
diff --git a/sgx_alloc/Cargo.toml b/sgx_alloc/Cargo.toml
index af14267..02b464b 100644
--- a/sgx_alloc/Cargo.toml
+++ b/sgx_alloc/Cargo.toml
@@ -1,8 +1,7 @@
 [package]
 name = "sgx_alloc"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
-build = "build.rs"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
 documentation = "https://dingelish.github.io/"
diff --git a/sgx_alloc/build.rs b/sgx_alloc/build.rs
deleted file mode 100644
index 47ea68c..0000000
--- a/sgx_alloc/build.rs
+++ /dev/null
@@ -1,79 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use std::env;
-use std::process::Command;
-
-fn main() {
-    // nightly-2020-12-05 (rustc 2020-12-04)
-    // Rename `AllocRef` to `Allocator` and `(de)alloc` to `(de)allocate`
-    // https://github.com/rust-lang/rust/commit/9274b37d99f608e5fde569788ee79bd72fc3cf13
-    if let Some(true) = is_min_date("2020-12-04") {
-        println!("cargo:rustc-cfg=enable_allocator_traits");
-    }
-}
-
-// code below copied from crate version_check
-// we want to remove the build dependencies to make the dependency tree
-// as clean as possible. the following codes credit to SergioBenitez
-#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
-struct Date(u32);
-
-impl Date {
-    fn read() -> Option<Date> {
-        get_version_and_date()
-            .and_then(|(_, date)| date)
-            .and_then(|date| Date::parse(&date))
-    }
-
-    fn parse(date: &str) -> Option<Date> {
-        let ymd: Vec<u32> = date.split("-")
-            .filter_map(|s| s.parse::<u32>().ok())
-            .collect();
-    
-        if ymd.len() != 3 {
-            return None
-        }
-    
-        let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
-        Some(Date((y << 9) | ((m & 0xF) << 5) | (d & 0x1F)))
-    }
-}
-
-fn get_version_and_date() -> Option<(Option<String>, Option<String>)> {
-    env::var("RUSTC").ok()
-        .and_then(|rustc| Command::new(rustc).arg("--version").output().ok())
-        .or_else(|| Command::new("rustc").arg("--version").output().ok())
-        .and_then(|output| String::from_utf8(output.stdout).ok())
-        .map(|s| version_and_date_from_rustc_version(&s))
-}
-
-fn version_and_date_from_rustc_version(s: &str) -> (Option<String>, Option<String>) {
-    let last_line = s.lines().last().unwrap_or(s);
-    let mut components = last_line.trim().split(" ");
-    let version = components.nth(1);
-    let date = components.filter(|c| c.ends_with(')')).next()
-        .map(|s| s.trim_end().trim_end_matches(")").trim_start().trim_start_matches('('));
-    (version.map(|s| s.to_string()), date.map(|s| s.to_string()))
-}
-
-fn is_min_date(min_date: &str) -> Option<bool> {
-    match (Date::read(), Date::parse(min_date)) {
-        (Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date),
-        _ => None
-    }
-}
diff --git a/sgx_alloc/src/system.rs b/sgx_alloc/src/system.rs
index defdb1a..c6d2167 100644
--- a/sgx_alloc/src/system.rs
+++ b/sgx_alloc/src/system.rs
@@ -22,11 +22,7 @@
 //! It is essential, because we depends on Intel SGX's SDK.
 //! 2018-06-22 Add liballoc components here
 
-use core::alloc::{AllocError, GlobalAlloc, Layout};
-#[cfg(enable_allocator_traits)]
-use core::alloc::Allocator;
-#[cfg(not(enable_allocator_traits))]
-use core::alloc::AllocRef as Allocator;
+use core::alloc::{AllocError, Allocator, GlobalAlloc, Layout};
 use core::intrinsics;
 use core::ptr::{self, NonNull};
 
@@ -61,7 +57,7 @@
         }
     }
 
-    // Safety: Same as `Allocator::grow`
+    // SAFETY: Same as `Allocator::grow`
     #[inline]
     unsafe fn grow_impl(
         &self,
@@ -78,8 +74,9 @@
         match old_layout.size() {
             0 => self.alloc_impl(new_layout, zeroed),
 
-            // SAFETY: `new_size` is non-zero as `old_size` is greater than or equal to `new_size`
-            // as required by safety conditions. Other conditions must be upheld by the caller
+            // SAFETY: `new_size` is non-zero as `new_size` is greater than or equal to `old_size`
+            // as required by safety conditions and the `old_size == 0` case was handled in the
+            // previous match arm. Other conditions must be upheld by the caller
             old_size if old_layout.align() == new_layout.align() => {
                 let new_size = new_layout.size();
 
@@ -92,7 +89,7 @@
                     raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
                 }
                 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
-            },
+            }
 
             // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
             // both the old and new memory allocation are valid for reads and writes for `old_size`
@@ -102,41 +99,26 @@
             old_size => {
                 let new_ptr = self.alloc_impl(new_layout, zeroed)?;
                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
-                if old_layout.size() != 0 {
-                    GlobalAlloc::dealloc(self, ptr.as_ptr(), old_layout)
-                }
+                Allocator::deallocate(&self, ptr, old_layout);
                 Ok(new_ptr)
-            },
+            }
         }
     }
 }
 
+// The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl,
+// which is in `std::sys::*::alloc`.
 unsafe impl Allocator for System {
-    #[cfg(enable_allocator_traits)]
     #[inline]
     fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
         self.alloc_impl(layout, false)
     }
 
-    #[cfg(not(enable_allocator_traits))]
-    #[inline]
-    fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
-        self.alloc_impl(layout, false)
-    }
-
-    #[cfg(enable_allocator_traits)]
     #[inline]
     fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
         self.alloc_impl(layout, true)
     }
 
-    #[cfg(not(enable_allocator_traits))]
-    #[inline]
-    fn alloc_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
-         self.alloc_impl(layout, true)
-    }
-
-    #[cfg(enable_allocator_traits)]
     #[inline]
     unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
         if layout.size() != 0 {
@@ -146,14 +128,6 @@
         }
     }
 
-    #[cfg(not(enable_allocator_traits))]
-    #[inline]
-    unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
-        if layout.size() != 0 {
-            GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
-        }
-    }
-
     #[inline]
     unsafe fn grow(
         &self,
@@ -161,8 +135,8 @@
         old_layout: Layout,
         new_layout: Layout,
     ) -> Result<NonNull<[u8]>, AllocError> {
-         // SAFETY: all conditions must be upheld by the caller
-         self.grow_impl(ptr, old_layout, new_layout, false)
+        // SAFETY: all conditions must be upheld by the caller
+        self.grow_impl(ptr, old_layout, new_layout, false)
     }
 
     #[inline]
@@ -183,38 +157,40 @@
         old_layout: Layout,
         new_layout: Layout,
     ) -> Result<NonNull<[u8]>, AllocError> {
-         match new_layout.size() {
-             // SAFETY: conditions must be upheld by the caller
-             0 => {
-                if old_layout.size() != 0 {
-                    GlobalAlloc::dealloc(self, ptr.as_ptr(), old_layout)
-                }
+        debug_assert!(
+            new_layout.size() <= old_layout.size(),
+            "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
+        );
+
+        match new_layout.size() {
+            // SAFETY: conditions must be upheld by the caller
+            0 => {
+                Allocator::deallocate(&self, ptr, old_layout);
                 Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
-             },
-             // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
-             new_size if old_layout.align() == new_layout.align() => {
+            }
+
+            // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
+            new_size if old_layout.align() == new_layout.align() => {
                 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
                 intrinsics::assume(new_size <= old_layout.size());
 
                 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
                 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
                 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
-             },
+            }
 
-             // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
-             // both the old and new memory allocation are valid for reads and writes for `new_size`
-             // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
-             // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
-             // for `dealloc` must be upheld by the caller.
-             new_size => {
-                let new_ptr =  self.alloc_impl(new_layout, false)?;
+            // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
+            // both the old and new memory allocation are valid for reads and writes for `new_size`
+            // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
+            // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
+            // for `dealloc` must be upheld by the caller.
+            new_size => {
+                let new_ptr = Allocator::allocate(&self, new_layout)?;
                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
-                if old_layout.size() != 0 {
-                    GlobalAlloc::dealloc(self, ptr.as_ptr(), old_layout)
-                }
+                Allocator::deallocate(&self, ptr, old_layout);
                 Ok(new_ptr)
-             },
-         }
+            }
+        }
     }
 }
 
@@ -266,7 +242,7 @@
             if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
                 libc::calloc(layout.size(), 1) as *mut u8
             } else {
-                let ptr = GlobalAlloc::alloc(self, layout);
+                let ptr = self.alloc(layout);
                 if !ptr.is_null() {
                     ptr::write_bytes(ptr, 0, layout.size());
                 }
diff --git a/sgx_backtrace/Cargo.toml b/sgx_backtrace/Cargo.toml
index 436e690..0c4799a 100644
--- a/sgx_backtrace/Cargo.toml
+++ b/sgx_backtrace/Cargo.toml
@@ -1,11 +1,11 @@
 
 [package]
 name = "sgx_backtrace"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_backtrace/src/backtrace/libunwind.rs b/sgx_backtrace/src/backtrace/libunwind.rs
index 045ba65..8c4fc8d 100644
--- a/sgx_backtrace/src/backtrace/libunwind.rs
+++ b/sgx_backtrace/src/backtrace/libunwind.rs
@@ -32,12 +32,15 @@
 //!
 //! This is the default unwinding API for all non-Windows platforms currently.
 
+#![allow(clippy::upper_case_acronyms)]
+use super::super::Bomb;
 use core::ffi::c_void;
 
 pub enum Frame {
     Raw(*mut uw::_Unwind_Context),
     Cloned {
         ip: *mut c_void,
+        sp: *mut c_void,
         symbol_address: *mut c_void,
     },
 }
@@ -58,6 +61,13 @@
         unsafe { uw::_Unwind_GetIP(ctx) as *mut c_void }
     }
 
+    pub fn sp(&self) -> *mut c_void {
+        match *self {
+            Frame::Raw(ctx) => unsafe { uw::get_sp(ctx) as *mut c_void },
+            Frame::Cloned { sp, .. } => sp,
+        }
+    }
+
     pub fn symbol_address(&self) -> *mut c_void {
         if let Frame::Cloned { symbol_address, .. } = *self {
             return symbol_address;
@@ -77,12 +87,17 @@
             unsafe { uw::_Unwind_FindEnclosingFunction(self.ip()) }
         }
     }
+
+    pub fn module_base_address(&self) -> Option<*mut c_void> {
+        None
+    }
 }
 
 impl Clone for Frame {
     fn clone(&self) -> Frame {
         Frame::Cloned {
             ip: self.ip(),
+            sp: self.sp(),
             symbol_address: self.symbol_address(),
         }
     }
@@ -101,7 +116,7 @@
             inner: Frame::Raw(ctx),
         };
 
-        let mut bomb = crate::Bomb { enabled: true };
+        let mut bomb = Bomb { enabled: true };
         let keep_going = cb(&cx);
         bomb.enabled = false;
 
@@ -159,25 +174,61 @@
             not(all(target_os = "freebsd", target_arch = "arm")),
             not(all(target_os = "linux", target_arch = "arm"))
         ))]
-        pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> sgx_libc::uintptr_t;
+        pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t;
 
         #[cfg(all(
-            not(target_os = "android"),
+            not(all(target_os = "android", target_arch = "arm")),
             not(all(target_os = "freebsd", target_arch = "arm")),
             not(all(target_os = "linux", target_arch = "arm"))
         ))]
         pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
+
+        #[cfg(all(
+            not(all(target_os = "android", target_arch = "arm")),
+            not(all(target_os = "freebsd", target_arch = "arm")),
+            not(all(target_os = "linux", target_arch = "arm")),
+            not(all(target_os = "linux", target_arch = "s390x"))
+        ))]
+        // This function is a misnomer: rather than getting this frame's
+        // Canonical Frame Address (aka the caller frame's SP) it
+        // returns this frame's SP.
+        //
+        // https://github.com/libunwind/libunwind/blob/d32956507cf29d9b1a98a8bce53c78623908f4fe/src/unwind/GetCFA.c#L28-L35
+        #[link_name = "_Unwind_GetCFA"]
+        pub fn get_sp(ctx: *mut _Unwind_Context) -> libc::uintptr_t;
     }
 
-    // On android, the function _Unwind_GetIP is a macro, and this is the
-    // expansion of the macro. This is all copy/pasted directly from the
-    // header file with the definition of _Unwind_GetIP.
+    // s390x uses a biased CFA value, therefore we need to use
+    // _Unwind_GetGR to get the stack pointer register (%r15)
+    // instead of relying on _Unwind_GetCFA.
+    #[cfg(all(target_os = "linux", target_arch = "s390x"))]
+    pub unsafe fn get_sp(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
+        extern "C" {
+            pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, index: libc::c_int) -> libc::uintptr_t;
+        }
+        _Unwind_GetGR(ctx, 15)
+    }
+
+    // On android and arm, the function `_Unwind_GetIP` and a bunch of others
+    // are macros, so we define functions containing the expansion of the
+    // macros.
+    //
+    // TODO: link to the header file that defines these macros, if you can find
+    // it. (I, fitzgen, cannot find the header file that some of these macro
+    // expansions were originally borrowed from.)
     #[cfg(any(
         all(target_os = "android", target_arch = "arm"),
         all(target_os = "freebsd", target_arch = "arm"),
         all(target_os = "linux", target_arch = "arm")
     ))]
-    pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> sgx_libc::uintptr_t {
+    pub use self::arm::*;
+    #[cfg(any(
+        all(target_os = "android", target_arch = "arm"),
+        all(target_os = "freebsd", target_arch = "arm"),
+        all(target_os = "linux", target_arch = "arm")
+    ))]
+    mod arm {
+        pub use super::*;
         #[repr(C)]
         enum _Unwind_VRS_Result {
             _UVRSR_OK = 0,
@@ -213,28 +264,39 @@
             ) -> _Unwind_VRS_Result;
         }
 
-        let mut val: _Unwind_Word = 0;
-        let ptr = &mut val as *mut _Unwind_Word;
-        let _ = _Unwind_VRS_Get(
-            ctx,
-            _Unwind_VRS_RegClass::_UVRSC_CORE,
-            15,
-            _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
-            ptr as *mut c_void,
-        );
-        (val & !1) as libc::uintptr_t
-    }
+        pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
+            let mut val: _Unwind_Word = 0;
+            let ptr = &mut val as *mut _Unwind_Word;
+            let _ = _Unwind_VRS_Get(
+                ctx,
+                _Unwind_VRS_RegClass::_UVRSC_CORE,
+                15,
+                _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
+                ptr as *mut c_void,
+            );
+            (val & !1) as libc::uintptr_t
+        }
 
-    // This function also doesn't exist on Android or ARM/Linux, so make it
-    // a no-op
-    #[cfg(any(
-        target_os = "android",
-        all(target_os = "freebsd", target_arch = "arm"),
-        all(target_os = "linux", target_arch = "arm")
-    ))]
-    pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
-        pc
+        // R13 is the stack pointer on arm.
+        const SP: _Unwind_Word = 13;
+
+        pub unsafe fn get_sp(ctx: *mut _Unwind_Context) -> libc::uintptr_t {
+            let mut val: _Unwind_Word = 0;
+            let ptr = &mut val as *mut _Unwind_Word;
+            let _ = _Unwind_VRS_Get(
+                ctx,
+                _Unwind_VRS_RegClass::_UVRSC_CORE,
+                SP,
+                _Unwind_VRS_DataRepresentation::_UVRSD_UINT32,
+                ptr as *mut c_void,
+            );
+            val as libc::uintptr_t
+        }
+
+        // This function also doesn't exist on Android or ARM/Linux, so make it
+        // a no-op.
+        pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
+            pc
+        }
     }
 }
-
-
diff --git a/sgx_backtrace/src/backtrace/mod.rs b/sgx_backtrace/src/backtrace/mod.rs
index 4be0e0a..6cd1d6a 100644
--- a/sgx_backtrace/src/backtrace/mod.rs
+++ b/sgx_backtrace/src/backtrace/mod.rs
@@ -107,6 +107,14 @@
         self.inner.ip()
     }
 
+    /// Returns the current stack pointer of this frame.
+    ///
+    /// In the case that a backend cannot recover the stack pointer for this
+    /// frame, a null pointer is returned.
+    pub fn sp(&self) -> *mut c_void {
+        self.inner.sp()
+    }
+
     /// Returns the starting symbol address of the frame of this function.
     ///
     /// This will attempt to rewind the instruction pointer returned by `ip` to
@@ -118,10 +126,15 @@
     pub fn symbol_address(&self) -> *mut c_void {
         self.inner.symbol_address()
     }
+
+    /// Returns the base address of the module to which the frame belongs.
+    pub fn module_base_address(&self) -> Option<*mut c_void> {
+        self.inner.module_base_address()
+    }
 }
 
 impl fmt::Debug for Frame {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("Frame")
             .field("ip", &self.ip())
             .field("symbol_address", &self.symbol_address())
diff --git a/sgx_backtrace/src/capture.rs b/sgx_backtrace/src/capture.rs
index f55af29..a12236f 100644
--- a/sgx_backtrace/src/capture.rs
+++ b/sgx_backtrace/src/capture.rs
@@ -17,10 +17,10 @@
 
 use crate::PrintFmt;
 use crate::{resolve, resolve_frame, trace, BacktraceFmt, Symbol, SymbolName};
-use std::path::{Path, PathBuf};
-use std::prelude::v1::*;
 use core::ffi::c_void;
 use core::fmt;
+use std::path::{Path, PathBuf};
+use std::prelude::v1::*;
 
 /// Representation of an owned and self-contained backtrace.
 ///
@@ -71,6 +71,7 @@
     Deserialized {
         ip: usize,
         symbol_address: usize,
+        module_base_address: Option<usize>,
     },
 }
 
@@ -88,6 +89,16 @@
             Frame::Deserialized { symbol_address, .. } => symbol_address as *mut c_void,
         }
     }
+
+    fn module_base_address(&self) -> Option<*mut c_void> {
+        match *self {
+            Frame::Raw(ref f) => f.module_base_address(),
+            Frame::Deserialized {
+                module_base_address,
+                ..
+            } => module_base_address.map(|addr| addr as *mut c_void),
+        }
+    }
 }
 
 /// Captured version of a symbol in a backtrace.
@@ -106,6 +117,7 @@
     addr: Option<usize>,
     filename: Option<PathBuf>,
     lineno: Option<u32>,
+    colno: Option<u32>,
 }
 
 impl Backtrace {
@@ -225,6 +237,7 @@
                         addr: symbol.addr().map(|a| a as usize),
                         filename: symbol.filename().map(|m| m.to_owned()),
                         lineno: symbol.lineno(),
+                        colno: symbol.colno(),
                     });
                 };
                 match frame.frame {
@@ -248,9 +261,9 @@
     }
 }
 
-impl Into<Vec<BacktraceFrame>> for Backtrace {
-    fn into(self) -> Vec<BacktraceFrame> {
-        self.frames
+impl From<Backtrace> for Vec<BacktraceFrame> {
+    fn from(bt: Backtrace) -> Vec<BacktraceFrame> {
+        bt.frames
     }
 }
 
@@ -275,6 +288,18 @@
         self.frame.symbol_address() as *mut c_void
     }
 
+    /// Same as `Frame::module_base_address`
+    ///
+    /// # Required features
+    ///
+    /// This function requires the `std` feature of the `backtrace` crate to be
+    /// enabled, and the `std` feature is enabled by default.
+    pub fn module_base_address(&self) -> Option<*mut c_void> {
+        self.frame
+            .module_base_address()
+            .map(|addr| addr as *mut c_void)
+    }
+
     /// Returns the list of symbols that this frame corresponds to.
     ///
     /// Normally there is only one symbol per frame, but sometimes if a number
@@ -301,7 +326,7 @@
     ///
     /// This function requires the `std` feature of the `backtrace` crate to be
     /// enabled, and the `std` feature is enabled by default.
-    pub fn name(&self) -> Option<SymbolName> {
+    pub fn name(&self) -> Option<SymbolName<'_>> {
         self.name.as_ref().map(|s| SymbolName::new(s))
     }
 
@@ -322,7 +347,7 @@
     /// This function requires the `std` feature of the `backtrace` crate to be
     /// enabled, and the `std` feature is enabled by default.
     pub fn filename(&self) -> Option<&Path> {
-        self.filename.as_ref().map(|p| &**p)
+        self.filename.as_deref()
     }
 
     /// Same as `Symbol::lineno`
@@ -334,10 +359,20 @@
     pub fn lineno(&self) -> Option<u32> {
         self.lineno
     }
+
+    /// Same as `Symbol::colno`
+    ///
+    /// # Required features
+    ///
+    /// This function requires the `std` feature of the `backtrace` crate to be
+    /// enabled, and the `std` feature is enabled by default.
+    pub fn colno(&self) -> Option<u32> {
+        self.colno
+    }
 }
 
 impl fmt::Debug for Backtrace {
-    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
         let full = fmt.alternate();
         let (frames, style) = if full {
             (&self.frames[..], PrintFmt::Full)
@@ -350,17 +385,18 @@
         // short format, because if it's full we presumably want to print
         // everything.
         // let cwd = std::env::current_dir();
-        let mut print_path = move |fmt: &mut fmt::Formatter, path: crate::BytesOrWideString| {
-            let path = path.into_path_buf();
-            // if !full {
-            //     if let Ok(cwd) = &cwd {
-            //         if let Ok(suffix) = path.strip_prefix(cwd) {
-            //             return fmt::Display::fmt(&suffix.display(), fmt);
-            //         }
-            //     }
-            // }
-            fmt::Display::fmt(&path.display(), fmt)
-        };
+        let mut print_path =
+            move |fmt: &mut fmt::Formatter<'_>, path: crate::BytesOrWideString<'_>| {
+                let path = path.into_path_buf();
+                // if !full {
+                //     if let Ok(cwd) = &cwd {
+                //         if let Ok(suffix) = path.strip_prefix(cwd) {
+                //             return fmt::Display::fmt(&suffix.display(), fmt);
+                //         }
+                //     }
+                // }
+                fmt::Display::fmt(&path.display(), fmt)
+            };
 
         let mut f = BacktraceFmt::new(fmt, style, &mut print_path);
         f.add_context()?;
@@ -379,7 +415,7 @@
 }
 
 impl fmt::Debug for BacktraceFrame {
-    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt.debug_struct("BacktraceFrame")
             .field("ip", &self.ip())
             .field("symbol_address", &self.symbol_address())
@@ -388,12 +424,13 @@
 }
 
 impl fmt::Debug for BacktraceSymbol {
-    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt.debug_struct("BacktraceSymbol")
             .field("name", &self.name())
             .field("addr", &self.addr())
             .field("filename", &self.filename())
             .field("lineno", &self.lineno())
+            .field("colno", &self.colno())
             .finish()
     }
 }
@@ -401,12 +438,13 @@
 #[cfg(feature = "serialize")]
 mod sgx_serialize_impls {
     use super::*;
-    use sgx_serialize::{Serializable, DeSerializable, Encoder, Decoder};
+    use sgx_serialize::{DeSerializable, Decoder, Encoder, Serializable};
 
     #[derive(Serializable, DeSerializable)]
     struct SerializedFrame {
         ip: usize,
         symbol_address: usize,
+        module_base_address: Option<usize>,
         symbols: Option<Vec<BacktraceSymbol>>,
     }
 
@@ -420,6 +458,7 @@
                 frame: Frame::Deserialized {
                     ip: frame.ip,
                     symbol_address: frame.symbol_address,
+                    module_base_address: frame.module_base_address,
                 },
                 symbols: frame.symbols,
             })
@@ -435,6 +474,7 @@
             SerializedFrame {
                 ip: frame.ip() as usize,
                 symbol_address: frame.symbol_address() as usize,
+                module_base_address: frame.module_base_address().map(|addr| addr as usize),
                 symbols: symbols.clone(),
             }
             .encode(e)
diff --git a/sgx_backtrace/src/lib.rs b/sgx_backtrace/src/lib.rs
index 32cdf2c..775863f 100644
--- a/sgx_backtrace/src/lib.rs
+++ b/sgx_backtrace/src/lib.rs
@@ -23,25 +23,6 @@
 //! parsed, for example, and expose the functionality of multiple backend
 //! implementations.
 //!
-//! # Implementation
-//!
-//! This library makes use of a number of strategies for actually acquiring a
-//! backtrace. For example unix uses libgcc's libunwind bindings by default to
-//! acquire a backtrace, but coresymbolication or dladdr is used on OSX to
-//! acquire symbol names while linux uses gcc's libbacktrace.
-//!
-//! When using the default feature set of this library the "most reasonable" set
-//! of defaults is chosen for the current platform, but the features activated
-//! can also be controlled at a finer granularity.
-//!
-//! # API Principles
-//!
-//! This library attempts to be as flexible as possible to accommodate different
-//! backend implementations of acquiring a backtrace. Consequently the currently
-//! exported functions are closure-based as opposed to the likely expected
-//! iterator-based versions. This is done due to limitations of the underlying
-//! APIs used from the system.
-//!
 //! # Usage
 //!
 //! First, add this to your Cargo.toml
@@ -80,9 +61,12 @@
 //! # }
 //! ```
 #![no_std]
-
-#![cfg_attr(all(target_env = "sgx", target_vendor = "mesalock", feature = "std"), feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock", feature = "std"),
+    feature(rustc_private)
+)]
 #![cfg_attr(feature = "nostd", feature(panic_unwind))]
+#![allow(clippy::missing_safety_doc)]
 
 #[cfg(all(not(target_env = "sgx"), feature = "std"))]
 #[macro_use]
@@ -100,9 +84,9 @@
 
 #[macro_use]
 extern crate sgx_types;
-extern crate sgx_trts;
-extern crate sgx_libc;
 extern crate sgx_demangle;
+extern crate sgx_libc as libc;
+extern crate sgx_trts;
 
 #[cfg(feature = "serialize")]
 extern crate sgx_serialize;
@@ -110,27 +94,27 @@
 #[macro_use]
 extern crate sgx_serialize_derive;
 
-pub use crate::backtrace::{trace_unsynchronized, Frame};
+pub use self::backtrace::{trace_unsynchronized, Frame};
 mod backtrace;
 
-pub use crate::symbolize::resolve_frame_unsynchronized;
-pub use crate::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
-pub use crate::symbolize::set_enclave_path;
+pub use self::symbolize::resolve_frame_unsynchronized;
+pub use self::symbolize::set_enclave_path;
+pub use self::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
 mod symbolize;
 
-pub use crate::types::BytesOrWideString;
+pub use self::types::BytesOrWideString;
 mod types;
 
 #[cfg(feature = "std")]
-pub use crate::symbolize::clear_symbol_cache;
+pub use self::symbolize::clear_symbol_cache;
 
 mod print;
 pub use print::{BacktraceFmt, BacktraceFrameFmt, PrintFmt};
 cfg_if! {
     if #[cfg(feature = "std")] {
-        pub use crate::backtrace::trace;
-        pub use crate::symbolize::{resolve, resolve_frame};
-        pub use crate::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
+        pub use self::backtrace::trace;
+        pub use self::symbolize::{resolve, resolve_frame};
+        pub use self::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
         mod capture;
     }
 }
@@ -154,7 +138,7 @@
 mod lock {
     use std::boxed::Box;
     use std::cell::Cell;
-    use std::sync::{SgxMutex, SgxMutexGuard, Once};
+    use std::sync::{Once, SgxMutex, SgxMutexGuard};
 
     pub struct LockGuard(Option<SgxMutexGuard<'static, ()>>);
 
diff --git a/sgx_backtrace/src/print.rs b/sgx_backtrace/src/print.rs
index ad47eb9..f70a9ca 100644
--- a/sgx_backtrace/src/print.rs
+++ b/sgx_backtrace/src/print.rs
@@ -15,7 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::BytesOrWideString;
+use super::{BacktraceFrame, BacktraceSymbol};
+use super::{BytesOrWideString, Frame, SymbolName};
 use core::ffi::c_void;
 use core::fmt;
 
@@ -30,10 +31,13 @@
     fmt: &'a mut fmt::Formatter<'b>,
     frame_index: usize,
     format: PrintFmt,
-    print_path: &'a mut (dyn FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b),
+    print_path:
+        &'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result + 'b),
 }
 
 /// The styles of printing that we can print
+#[allow(clippy::manual_non_exhaustive)]
+#[non_exhaustive]
 #[derive(Copy, Clone, Eq, PartialEq)]
 pub enum PrintFmt {
     /// Prints a terser backtrace which ideally only contains relevant information
@@ -55,7 +59,8 @@
     pub fn new(
         fmt: &'a mut fmt::Formatter<'b>,
         format: PrintFmt,
-        print_path: &'a mut (dyn FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b),
+        print_path: &'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result
+                     + 'b),
     ) -> Self {
         BacktraceFmt {
             fmt,
@@ -68,7 +73,7 @@
     /// Prints a preamble for the backtrace about to be printed.
     ///
     /// This is required on some platforms for backtraces to be fully
-    /// sumbolicated later, and otherwise this should just be the first method
+    /// symbolicated later, and otherwise this should just be the first method
     /// you call after creating a `BacktraceFmt`.
     pub fn add_context(&mut self) -> fmt::Result {
         Ok(())
@@ -107,7 +112,7 @@
 impl BacktraceFrameFmt<'_, '_, '_> {
     /// Prints a `BacktraceFrame` with this frame formatter.
     ///
-    /// This will recusrively print all `BacktraceSymbol` instances within the
+    /// This will recursively print all `BacktraceSymbol` instances within the
     /// `BacktraceFrame`.
     ///
     /// # Required features
@@ -115,7 +120,7 @@
     /// This function requires the `std` feature of the `backtrace` crate to be
     /// enabled, and the `std` feature is enabled by default.
     #[cfg(feature = "std")]
-    pub fn backtrace_frame(&mut self, frame: &crate::BacktraceFrame) -> fmt::Result {
+    pub fn backtrace_frame(&mut self, frame: &BacktraceFrame) -> fmt::Result {
         let symbols = frame.symbols();
         for symbol in symbols {
             self.backtrace_symbol(frame, symbol)?;
@@ -135,10 +140,10 @@
     #[cfg(feature = "std")]
     pub fn backtrace_symbol(
         &mut self,
-        frame: &crate::BacktraceFrame,
-        symbol: &crate::BacktraceSymbol,
+        frame: &BacktraceFrame,
+        symbol: &BacktraceSymbol,
     ) -> fmt::Result {
-        self.print_raw(
+        self.print_raw_with_column(
             frame.ip(),
             symbol.name(),
             // TODO: this isn't great that we don't end up printing anything
@@ -148,18 +153,20 @@
                 .filename()
                 .and_then(|p| Some(BytesOrWideString::Bytes(p.to_str()?.as_bytes()))),
             symbol.lineno(),
+            symbol.colno(),
         )?;
         Ok(())
     }
 
     /// Prints a raw traced `Frame` and `Symbol`, typically from within the raw
     /// callbacks of this crate.
-    pub fn symbol(&mut self, frame: &crate::Frame, symbol: &crate::Symbol) -> fmt::Result {
-        self.print_raw(
+    pub fn symbol(&mut self, frame: &Frame, symbol: &super::Symbol) -> fmt::Result {
+        self.print_raw_with_column(
             frame.ip(),
             symbol.name(),
             symbol.filename_raw(),
             symbol.lineno(),
+            symbol.colno(),
         )?;
         Ok(())
     }
@@ -172,11 +179,27 @@
     pub fn print_raw(
         &mut self,
         frame_ip: *mut c_void,
-        symbol_name: Option<crate::SymbolName>,
-        filename: Option<BytesOrWideString>,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
         lineno: Option<u32>,
     ) -> fmt::Result {
-        self.print_raw_generic(frame_ip, symbol_name, filename, lineno)?;
+        self.print_raw_with_column(frame_ip, symbol_name, filename, lineno, None)
+    }
+
+    /// Adds a raw frame to the backtrace output, including column information.
+    ///
+    /// This method, like the previous, takes the raw arguments in case
+    /// they're being source from different locations. Note that this may be
+    /// called multiple times for one frame.
+    pub fn print_raw_with_column(
+        &mut self,
+        frame_ip: *mut c_void,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
+        lineno: Option<u32>,
+        colno: Option<u32>,
+    ) -> fmt::Result {
+        self.print_raw_generic(frame_ip, symbol_name, filename, lineno, colno)?;
         self.symbol_index += 1;
         Ok(())
     }
@@ -185,9 +208,10 @@
     fn print_raw_generic(
         &mut self,
         mut frame_ip: *mut c_void,
-        symbol_name: Option<crate::SymbolName>,
-        filename: Option<BytesOrWideString>,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
         lineno: Option<u32>,
+        colno: Option<u32>,
     ) -> fmt::Result {
         // No need to print "null" frames, it basically just means that the
         // system backtrace was a bit eager to trace back super far.
@@ -220,18 +244,22 @@
             (Some(name), PrintFmt::Full) => write!(self.fmt.fmt, "{}", name)?,
             (None, _) | (_, PrintFmt::__Nonexhaustive) => write!(self.fmt.fmt, "<unknown>")?,
         }
-
         self.fmt.fmt.write_str("\n")?;
 
         // And last up, print out the filename/line number if they're available.
         if let (Some(file), Some(line)) = (filename, lineno) {
-            self.print_fileline(file, line)?;
+            self.print_fileline(file, line, colno)?;
         }
 
         Ok(())
     }
 
-    fn print_fileline(&mut self, file: BytesOrWideString, line: u32) -> fmt::Result {
+    fn print_fileline(
+        &mut self,
+        file: BytesOrWideString<'_>,
+        line: u32,
+        colno: Option<u32>,
+    ) -> fmt::Result {
         // Filename/line are printed on lines under the symbol name, so print
         // some appropriate whitespace to sort of right-align ourselves.
         if let PrintFmt::Full = self.fmt.format {
@@ -242,7 +270,14 @@
         // Delegate to our internal callback to print the filename and then
         // print out the line number.
         (self.fmt.print_path)(self.fmt.fmt, file)?;
-        write!(self.fmt.fmt, ":{}\n", line)?;
+        write!(self.fmt.fmt, ":{}", line)?;
+
+        // Add column number, if available.
+        if let Some(colno) = colno {
+            write!(self.fmt.fmt, ":{}", colno)?;
+        }
+
+        writeln!(self.fmt.fmt,)?;
         Ok(())
     }
 }
diff --git a/sgx_backtrace/src/symbolize/libbacktrace.rs b/sgx_backtrace/src/symbolize/libbacktrace.rs
index af8e743..6a14d9f 100644
--- a/sgx_backtrace/src/symbolize/libbacktrace.rs
+++ b/sgx_backtrace/src/symbolize/libbacktrace.rs
@@ -41,17 +41,19 @@
 #![allow(bad_style)]
 #![allow(dead_code)]
 
-use core::{ptr, slice};
 use crate::bt;
+use core::{marker, ptr, slice};
+use libc::{self, c_char, c_int, c_void, uintptr_t};
+use sgx_trts::c_str::CString;
+
 use crate::symbolize::{ResolveWhat, SymbolName};
 use crate::types::BytesOrWideString;
-use sgx_trts::c_str::CString;
-use sgx_libc::{self, c_char, c_int, c_void, uintptr_t};
 
-pub enum Symbol {
+pub enum Symbol<'a> {
     Syminfo {
         pc: uintptr_t,
         symname: *const c_char,
+        _marker: marker::PhantomData<&'a ()>,
     },
     Pcinfo {
         pc: uintptr_t,
@@ -62,13 +64,13 @@
     },
 }
 
-impl Symbol {
-    pub fn name(&self) -> Option<SymbolName> {
+impl Symbol<'_> {
+    pub fn name(&self) -> Option<SymbolName<'_>> {
         let symbol = |ptr: *const c_char| unsafe {
             if ptr.is_null() {
                 None
             } else {
-                let len = sgx_libc::strlen(ptr);
+                let len = libc::strlen(ptr);
                 Some(SymbolName::new(slice::from_raw_parts(
                     ptr as *const u8,
                     len,
@@ -102,11 +104,8 @@
             if ptr.is_null() {
                 None
             } else {
-                let len = sgx_libc::strlen(ptr);
-                Some(slice::from_raw_parts(
-                    ptr as *const u8,
-                    len,
-                ))
+                let len = libc::strlen(ptr);
+                Some(slice::from_raw_parts(ptr as *const u8, len))
             }
         };
         match *self {
@@ -143,7 +142,7 @@
         }
     }
 
-    pub fn filename_bytes(&self) -> Option<&[u8]> {
+    fn filename_bytes(&self) -> Option<&[u8]> {
         match *self {
             Symbol::Syminfo { .. } => None,
             Symbol::Pcinfo { filename, .. } => {
@@ -152,14 +151,14 @@
                     return None;
                 }
                 unsafe {
-                    let len = sgx_libc::strlen(filename);
+                    let len = libc::strlen(filename);
                     Some(slice::from_raw_parts(ptr, len))
                 }
             }
         }
     }
 
-    pub fn filename_raw(&self) -> Option<BytesOrWideString> {
+    pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
         self.filename_bytes().map(BytesOrWideString::Bytes)
     }
 
@@ -167,6 +166,7 @@
     pub fn filename(&self) -> Option<&::std::path::Path> {
         use std::path::Path;
 
+        #[allow(clippy::unnecessary_wraps)]
         fn bytes2path(bytes: &[u8]) -> Option<&Path> {
             use std::ffi::OsStr;
             use std::os::unix::prelude::*;
@@ -181,6 +181,10 @@
             Symbol::Pcinfo { lineno, .. } => Some(lineno as u32),
         }
     }
+
+    pub fn colno(&self) -> Option<u32> {
+        None
+    }
 }
 
 extern "C" fn error_cb(_data: *mut c_void, _msg: *const c_char, _errnum: c_int) {
@@ -215,7 +219,7 @@
     // not debug info, so if that happens we're sure to call the callback with
     // at least one symbol from the `syminfo_cb`.
     unsafe {
-        let syminfo_state = &mut *(data as *mut SyminfoState);
+        let syminfo_state = &mut *(data as *mut SyminfoState<'_>);
         let mut pcinfo_state = PcinfoState {
             symname,
             called: false,
@@ -230,14 +234,16 @@
         );
         if !pcinfo_state.called {
             let inner = Symbol::Syminfo {
-                pc: pc,
-                symname: symname,
+                pc,
+                symname,
+                _marker: marker::PhantomData,
             };
             (pcinfo_state.cb)(&super::Symbol {
                 name: inner.name_bytes().map(|m| m.to_vec()),
                 addr: inner.addr(),
                 filename: inner.filename_bytes().map(|m| m.to_vec()),
                 lineno: inner.lineno(),
+                colno: inner.colno(),
             });
         }
     }
@@ -268,9 +274,9 @@
         let state = &mut *(data as *mut PcinfoState);
         state.called = true;
         let inner = Symbol::Pcinfo {
-            pc: pc,
-            filename: filename,
-            lineno: lineno,
+            pc,
+            filename,
+            lineno,
             symname: state.symname,
             function,
         };
@@ -279,11 +285,12 @@
             addr: inner.addr(),
             filename: inner.filename_bytes().map(|m| m.to_vec()),
             lineno: inner.lineno(),
+            colno: inner.colno(),
         });
     }
 
     bomb.enabled = false;
-    return 0;
+    0
 }
 
 // The libbacktrace API supports creating a state, but it does not
@@ -356,7 +363,7 @@
     }
 }
 
-pub unsafe fn resolve(what: ResolveWhat, cb: &mut dyn FnMut(&super::Symbol)) {
+pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
     let symaddr = what.address_or_ip() as usize;
 
     // backtrace errors are currently swept under the rug
@@ -365,31 +372,24 @@
         return;
     }
 
-    // Call the `backtrace_syminfo` API first. This is (from reading the code)
-    // guaranteed to call `syminfo_cb` exactly once (or fail with an error
+    // Call the `backtrace_syminfo` API which (from reading the code)
+    // should call `syminfo_cb` exactly once (or fail with an error
     // presumably). We then handle more within the `syminfo_cb`.
     //
     // Note that we do this since `syminfo` will consult the symbol table,
     // finding symbol names even if there's no debug information in the binary.
-    let mut called = false;
-    {
-        let mut syminfo_state = SyminfoState {
-            pc: symaddr,
-            cb: &mut |sym| {
-                called = true;
-                cb(sym);
-            },
-        };
-        bt::backtrace_syminfo(
-            state,
-            symaddr as uintptr_t,
-            syminfo_cb,
-            error_cb,
-            &mut syminfo_state as *mut _ as *mut _,
-        );
-    }
+    let mut syminfo_state = SyminfoState { pc: symaddr, cb };
+    bt::backtrace_syminfo(
+        state,
+        symaddr as uintptr_t,
+        syminfo_cb,
+        error_cb,
+        &mut syminfo_state as *mut _ as *mut _,
+    );
 }
 
+pub unsafe fn clear_symbol_cache() {}
+
 static mut ENCLAVE_PATH: Option<CString> = None;
 
 pub fn set_enclave_path(path: &str) -> bool {
diff --git a/sgx_backtrace/src/symbolize/mod.rs b/sgx_backtrace/src/symbolize/mod.rs
index 1c6ef35..8554ba1 100644
--- a/sgx_backtrace/src/symbolize/mod.rs
+++ b/sgx_backtrace/src/symbolize/mod.rs
@@ -15,8 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::{fmt, str};
 use alloc::vec::Vec;
+use core::{fmt, str};
 
 cfg_if! {
     if #[cfg(feature = "std")] {
@@ -25,8 +25,8 @@
     }
 }
 
-use crate::backtrace::Frame;
-use crate::types::BytesOrWideString;
+use super::backtrace::Frame;
+use super::types::BytesOrWideString;
 use core::ffi::c_void;
 use sgx_demangle::{try_demangle, Demangle};
 
@@ -78,7 +78,7 @@
 #[cfg(feature = "std")]
 pub fn resolve<F: FnMut(&Symbol)>(addr: *mut c_void, cb: F) {
     let _guard = crate::lock::lock();
-    unsafe { resolve_unsynchronized(addr, cb) }
+    unsafe { resolve_unsynchronized(addr as *mut c_void, cb) }
 }
 
 /// Resolve a previously capture frame to a symbol, passing the symbol to the
@@ -177,7 +177,7 @@
 where
     F: FnMut(&Symbol),
 {
-    resolve_imp(ResolveWhat::Address(addr), &mut cb)
+    imp::resolve(ResolveWhat::Address(addr), &mut cb)
 }
 
 /// Same as `resolve_frame`, only unsafe as it's unsynchronized.
@@ -193,7 +193,7 @@
 where
     F: FnMut(&Symbol),
 {
-    resolve_imp(ResolveWhat::Frame(frame), &mut cb)
+    imp::resolve(ResolveWhat::Frame(frame), &mut cb)
 }
 
 /// A trait representing the resolution of a symbol in a file.
@@ -213,6 +213,7 @@
     addr: Option<*mut c_void>,
     filename: Option<Vec<u8>>,
     lineno: Option<u32>,
+    colno: Option<u32>,
 }
 
 impl Symbol {
@@ -225,19 +226,29 @@
     /// * The raw `str` value of the symbol can be accessed (if it's valid
     ///   utf-8).
     /// * The raw bytes for the symbol name can be accessed.
-    pub fn name(&self) -> Option<SymbolName> {
+    pub fn name(&self) -> Option<SymbolName<'_>> {
         self.name.as_ref().map(|m| SymbolName::new(m.as_slice()))
     }
 
     /// Returns the starting address of this function.
     pub fn addr(&self) -> Option<*mut c_void> {
-       self.addr
+        self.addr
     }
 
     /// Returns the raw filename as a slice. This is mainly useful for `no_std`
     /// environments.
-    pub fn filename_raw(&self) -> Option<BytesOrWideString> {
-        self.filename.as_ref().map(|f| BytesOrWideString::Bytes(f.as_slice()))
+    pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
+        self.filename
+            .as_ref()
+            .map(|f| BytesOrWideString::Bytes(f.as_slice()))
+    }
+
+    /// Returns the column number for where this symbol is currently executing.
+    ///
+    /// Only gimli currently provides a value here and even then only if `filename`
+    /// returns `Some`, and so it is then consequently subject to similar caveats.
+    pub fn colno(&self) -> Option<u32> {
+        self.colno
     }
 
     /// Returns the line number for where this symbol is currently executing.
@@ -265,12 +276,14 @@
         use std::ffi::OsStr;
         use std::os::unix::prelude::*;
 
-        self.filename.as_ref().map(|f| Path::new(OsStr::from_bytes(f.as_slice())))
+        self.filename
+            .as_ref()
+            .map(|f| Path::new(OsStr::from_bytes(f.as_slice())))
     }
 }
 
 impl fmt::Debug for Symbol {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         let mut d = f.debug_struct("Symbol");
         if let Some(name) = self.name() {
             d.field("name", &name);
@@ -289,6 +302,9 @@
         if let Some(lineno) = self.lineno() {
             d.field("lineno", &lineno);
         }
+        if let Some(colno) = self.colno() {
+            d.field("colno", &colno);
+        }
         d.finish()
     }
 }
@@ -308,10 +324,7 @@
         let str_bytes = str::from_utf8(bytes).ok();
         let demangled = str_bytes.and_then(|s| try_demangle(s).ok());
 
-        SymbolName {
-            bytes: bytes,
-            demangled: demangled,
-        }
+        SymbolName { bytes, demangled }
     }
 
     /// Returns the raw (mangled) symbol name as a `str` if the symbol is valid utf-8.
@@ -331,11 +344,11 @@
 }
 
 fn format_symbol_name(
-    fmt: fn(&str, &mut fmt::Formatter) -> fmt::Result,
+    fmt: fn(&str, &mut fmt::Formatter<'_>) -> fmt::Result,
     mut bytes: &[u8],
-    f: &mut fmt::Formatter,
+    f: &mut fmt::Formatter<'_>,
 ) -> fmt::Result {
-    while bytes.len() > 0 {
+    while !bytes.is_empty() {
         match str::from_utf8(bytes) {
             Ok(name) => {
                 fmt(name, f)?;
@@ -355,7 +368,7 @@
 }
 
 impl<'a> fmt::Display for SymbolName<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if let Some(ref s) = self.demangled {
             s.fmt(f)
         } else {
@@ -365,7 +378,7 @@
 }
 
 impl<'a> fmt::Debug for SymbolName<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if let Some(ref s) = self.demangled {
             s.fmt(f)
         } else {
@@ -391,13 +404,11 @@
 pub fn clear_symbol_cache() {
     let _guard = crate::lock::lock();
     unsafe {
-        clear_symbol_cache_imp();
+        imp::clear_symbol_cache();
     }
 }
 
 mod libbacktrace;
-use self::libbacktrace::resolve as resolve_imp;
-pub use libbacktrace::set_enclave_path;
+use libbacktrace as imp;
 
-#[allow(dead_code)]
-unsafe fn clear_symbol_cache_imp() {}
\ No newline at end of file
+pub use imp::set_enclave_path;
diff --git a/sgx_backtrace/src/types.rs b/sgx_backtrace/src/types.rs
index 7098d70..a4d9675 100644
--- a/sgx_backtrace/src/types.rs
+++ b/sgx_backtrace/src/types.rs
@@ -50,8 +50,8 @@
         use self::BytesOrWideString::*;
 
         match self {
-            &Bytes(slice) => String::from_utf8_lossy(slice),
-            &Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)),
+            Bytes(slice) => String::from_utf8_lossy(slice),
+            Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)),
         }
     }
 
@@ -62,20 +62,19 @@
     /// This function requires the `std` feature of the `backtrace` crate to be
     /// enabled, and the `std` feature is enabled by default.
     pub fn into_path_buf(self) -> PathBuf {
-        use self::BytesOrWideString::*;
         use std::ffi::OsStr;
         use std::os::unix::ffi::OsStrExt;
 
-        match self {
-            Bytes(slice) => PathBuf::from(OsStr::from_bytes(slice)),
-            _ => unreachable!(),
+        if let BytesOrWideString::Bytes(slice) = self {
+            return PathBuf::from(OsStr::from_bytes(slice));
         }
+        unreachable!()
     }
 }
 
 #[cfg(feature = "std")]
 impl<'a> fmt::Display for BytesOrWideString<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         self.to_str_lossy().fmt(f)
     }
 }
diff --git a/sgx_backtrace_sys/Cargo.toml b/sgx_backtrace_sys/Cargo.toml
index d6a0ae8..81645da 100644
--- a/sgx_backtrace_sys/Cargo.toml
+++ b/sgx_backtrace_sys/Cargo.toml
@@ -1,11 +1,10 @@
 [package]
 name = "sgx_backtrace_sys"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
-build = "build.rs"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_backtrace_sys/build.rs b/sgx_backtrace_sys/build.rs
index 37a4d05..f6aac67 100644
--- a/sgx_backtrace_sys/build.rs
+++ b/sgx_backtrace_sys/build.rs
@@ -15,8 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_build_helper as build_helper;
 use self::build_helper::native_lib_boilerplate;
+use sgx_build_helper as build_helper;
 use std::env;
 use std::fs::File;
 use std::path::Path;
@@ -27,8 +27,7 @@
 }
 
 fn build_libbacktrace(_target: &str) -> Result<(), ()> {
-    let mut sdk_dir = env::var("SGX_SDK")
-                    .unwrap_or_else(|_| "/opt/sgxsdk".to_string());
+    let mut sdk_dir = env::var("SGX_SDK").unwrap_or_else(|_| "/opt/sgxsdk".to_string());
 
     if !Path::new(&sdk_dir).exists() {
         sdk_dir = "/opt/intel/sgxsdk".to_string();
@@ -37,11 +36,12 @@
     let sdk_include = format!("{}/{}", sdk_dir, "include");
 
     let native = native_lib_boilerplate(
-                    "sgx_backtrace_sys/libbacktrace",
-                    "libbacktrace",
-                    "backtrace",
-                    "",
-                    &vec![])?;
+        "sgx_backtrace_sys/libbacktrace",
+        "libbacktrace",
+        "backtrace",
+        "",
+        &[],
+    )?;
 
     let mut build = cc::Build::new();
     build
@@ -82,7 +82,7 @@
                 .flag(mitigation_asflags)
                 .flag(mitigation_loadflags1)
                 .flag(mitigation_loadflags2);
-        },
+        }
         "CF" => {
             build
                 .flag(mitigation_cflags1)
@@ -90,12 +90,12 @@
                 .flag(mitigation_asflags)
                 .flag(mitigation_cfflags1)
                 .flag(mitigation_cfflags2);
-        },
-        _  => {},
+        }
+        _ => {}
     }
 
-    let any_debug = env::var("RUSTC_DEBUGINFO").unwrap_or_default() == "true" ||
-        env::var("RUSTC_DEBUGINFO_LINES").unwrap_or_default() == "true";
+    let any_debug = env::var("RUSTC_DEBUGINFO").unwrap_or_default() == "true"
+        || env::var("RUSTC_DEBUGINFO_LINES").unwrap_or_default() == "true";
     build.debug(any_debug);
 
     build.file("./libbacktrace/elf.c");
diff --git a/sgx_backtrace_sys/libbacktrace/backtrace_t.h b/sgx_backtrace_sys/libbacktrace/backtrace_t.h
index 4b9b976..cad4906 100644
--- a/sgx_backtrace_sys/libbacktrace/backtrace_t.h
+++ b/sgx_backtrace_sys/libbacktrace/backtrace_t.h
@@ -4,6 +4,7 @@
 #include <stdint.h>
 #include <wchar.h>
 #include <stddef.h>
+#include <sys/stat.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -12,8 +13,7 @@
 uint32_t u_open_ocall(int* retval, int* error, const char* pathname, int flags);
 uint32_t u_close_ocall(int* retval, int* error, int fd);
 uint32_t u_fcntl_arg1_ocall(int* retval, int* error, int fd, int cmd, int arg);
-uint32_t u_mmap_ocall(void** retval, int* error, void* start, size_t length, int prot,
-                                int flags, int fd, int64_t offset);
+uint32_t u_mmap_ocall(void** retval, int* error, void* start, size_t length, int prot, int flags, int fd, int64_t offset);
 uint32_t u_munmap_ocall(int* retval, int* error, void* start, size_t length);
 
 uint32_t u_fstat_ocall(int* retval, int* error, int fd, struct stat* buf);
diff --git a/sgx_backtrace_sys/libbacktrace/read.c b/sgx_backtrace_sys/libbacktrace/read.c
index 37afdbc..062b109 100644
--- a/sgx_backtrace_sys/libbacktrace/read.c
+++ b/sgx_backtrace_sys/libbacktrace/read.c
@@ -34,6 +34,7 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -41,6 +42,8 @@
 #include "backtrace_t.h"
 #include "internal.h"
 
+#include "sgx_edger8r.h"
+
 /* This file implements file views when mmap is not available.  */
 
 /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET.  */
@@ -54,7 +57,7 @@
     int error = 0;
     off_t retval = 0;
     uint32_t status = 0;
-
+    void *host = NULL;
 
     status = u_lseek_ocall((uint64_t *)&retval, &error, descriptor, offset, SEEK_SET);
     if (status != 0) {
@@ -66,19 +69,26 @@
         return 0;
     }
 
-    view->base = backtrace_alloc(state, size, error_callback, data);
+    host = sgx_ocalloc(size);
+    if (host == NULL) {
+        error_callback(data, "sgx ocalloc failed", ENOMEM);
+        return 0;
+    }
 
+    view->base = backtrace_alloc(state, size, error_callback, data);
     if (view->base == NULL) {
+        sgx_ocfree();
         return 0;
     }
 
     view->data = view->base;
     view->len = size;
 
-    status = u_read_ocall((size_t *)&got, &error, descriptor, view->base, size);
+    status = u_read_ocall((size_t *)&got, &error, descriptor, host, size);
     if (status != 0) {
         error_callback(data, "sgx ocall failed", status);
         free(view->base);
+        sgx_ocfree();
         return 0;
     }
     if (got < 0) {
@@ -90,9 +100,13 @@
     if ((size_t) got < size) {
         error_callback(data, "file too short", 0);
         free(view->base);
+        sgx_ocfree();
         return 0;
     }
 
+    memcpy(view->base, host, size);
+    sgx_ocfree();
+
     return 1;
 }
 
diff --git a/sgx_backtrace_sys/src/lib.rs b/sgx_backtrace_sys/src/lib.rs
index 68265ff..84cf527 100644
--- a/sgx_backtrace_sys/src/lib.rs
+++ b/sgx_backtrace_sys/src/lib.rs
@@ -16,8 +16,10 @@
 // under the License..
 
 #![no_std]
-
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
 #![allow(non_camel_case_types)]
 
 extern crate sgx_libc as libc;
@@ -25,45 +27,46 @@
 pub use self::bindings::*;
 
 mod bindings {
+    use libc::{c_char, c_int, c_void, uintptr_t};
 
     pub type backtrace_syminfo_callback = extern "C" fn(
-        data: *mut libc::c_void,
-        pc: libc::uintptr_t,
-        symname: *const libc::c_char,
-        symval: libc::uintptr_t,
-        symsize: libc::uintptr_t);
+        data: *mut c_void,
+        pc: uintptr_t,
+        symname: *const c_char,
+        symval: uintptr_t,
+        symsize: uintptr_t,
+    );
     pub type backtrace_full_callback = extern "C" fn(
-        data: *mut libc::c_void,
-        pc: libc::uintptr_t,
-        filename: *const libc::c_char,
-        lineno: libc::c_int,
-        function: *const libc::c_char) -> libc::c_int;
-    pub type backtrace_error_callback = extern "C" fn(
-        data: *mut libc::c_void,
-        msg: *const libc::c_char,
-        errnum: libc::c_int);
+        data: *mut c_void,
+        pc: uintptr_t,
+        filename: *const c_char,
+        lineno: c_int,
+        function: *const c_char,
+    ) -> c_int;
+    pub type backtrace_error_callback =
+        extern "C" fn(data: *mut c_void, msg: *const c_char, errnum: c_int);
     pub enum backtrace_state {}
 
     extern "C" {
         pub fn backtrace_create_state(
-            filename: *const libc::c_char,
-            threaded: libc::c_int,
+            filename: *const c_char,
+            threaded: c_int,
             error: backtrace_error_callback,
-            data: *mut libc::c_void,
+            data: *mut c_void,
         ) -> *mut backtrace_state;
         pub fn backtrace_syminfo(
             state: *mut backtrace_state,
-            addr: libc::uintptr_t,
+            addr: uintptr_t,
             cb: backtrace_syminfo_callback,
             error: backtrace_error_callback,
-            data: *mut libc::c_void,
-        ) -> libc::c_int;
+            data: *mut c_void,
+        ) -> c_int;
         pub fn backtrace_pcinfo(
             state: *mut backtrace_state,
-            addr: libc::uintptr_t,
+            addr: uintptr_t,
             cb: backtrace_full_callback,
             error: backtrace_error_callback,
-            data: *mut libc::c_void,
-        ) -> libc::c_int;
+            data: *mut c_void,
+        ) -> c_int;
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_build_helper/Cargo.toml b/sgx_build_helper/Cargo.toml
index cbe1c18..6705cd8 100644
--- a/sgx_build_helper/Cargo.toml
+++ b/sgx_build_helper/Cargo.toml
@@ -1,13 +1,16 @@
 [package]
 name = "sgx_build_helper"
-version = "0.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
 [lib]
 name = "sgx_build_helper"
-path = "lib.rs"
+crate-type = ["rlib"]
+
+[features]
+default = []
diff --git a/sgx_build_helper/lib.rs b/sgx_build_helper/src/lib.rs
similarity index 93%
rename from sgx_build_helper/lib.rs
rename to sgx_build_helper/src/lib.rs
index 123e265..cfb3822 100644
--- a/sgx_build_helper/lib.rs
+++ b/sgx_build_helper/src/lib.rs
@@ -8,13 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::ffi::OsStr;
 use std::fs::File;
 use std::path::{Path, PathBuf};
 use std::process::{Command, Stdio};
+use std::thread;
 use std::time::{SystemTime, UNIX_EPOCH};
 use std::{env, fs};
-use std::thread;
-use std::ffi::OsStr;
 
 /// A helper macro to `unwrap` a result except also print out details like:
 ///
@@ -164,7 +164,7 @@
     String::from_utf8(output.stdout).unwrap()
 }
 
-pub fn rerun_if_changed_anything_in_dir(dir: &Path, filter: &Vec<&str>) {
+pub fn rerun_if_changed_anything_in_dir(dir: &Path, filter: &[&str]) {
     let mut stack = dir
         .read_dir()
         .unwrap()
@@ -174,17 +174,19 @@
     while let Some(entry) = stack.pop() {
         let path = entry.path();
         if entry.file_type().unwrap().is_dir() {
-            stack.extend(path.read_dir()
-                .unwrap()
-                .map(|e| e.unwrap())
-                .filter(|e| !is_in_filter_list(&*e.file_name(), filter)));
+            stack.extend(
+                path.read_dir()
+                    .unwrap()
+                    .map(|e| e.unwrap())
+                    .filter(|e| !is_in_filter_list(&*e.file_name(), filter)),
+            );
         } else {
             println!("cargo:rerun-if-changed={}", path.display());
         }
     }
 }
 
-fn is_in_filter_list(elem: &OsStr, filter: &Vec<&str>) -> bool {
+fn is_in_filter_list(elem: &OsStr, filter: &[&str]) -> bool {
     filter.iter().any(|e| *e == elem) || elem == ".git"
 }
 
@@ -231,7 +233,7 @@
     /// ensure it's linked against correctly.
     pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
         if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
-            return
+            return;
         }
 
         let dir = self.out_dir.join("build/lib/darwin");
@@ -266,19 +268,20 @@
 // If Err is returned, then everything is up-to-date and further build actions can be skipped.
 // Timestamps are created automatically when the result of `native_lib_boilerplate` goes out
 // of scope, so all the build actions should be completed until then.
+#[allow(clippy::result_unit_err)]
 pub fn native_lib_boilerplate(
     src_name: &str,
     out_name: &str,
     link_name: &str,
     search_subdir: &str,
-    filter: &Vec<&str>,
+    filter: &[&str],
 ) -> Result<NativeLibBoilerplate, ()> {
     let current_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
     let src_dir = current_dir.join("..").join(src_name);
     rerun_if_changed_anything_in_dir(&src_dir, filter);
 
-    let out_dir = env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(||
-        env::var_os("OUT_DIR").unwrap());
+    let out_dir =
+        env::var_os("RUSTBUILD_NATIVE_DIR").unwrap_or_else(|| env::var_os("OUT_DIR").unwrap());
     let out_dir = PathBuf::from(out_dir).join(out_name);
     t!(fs::create_dir_all(&out_dir));
     if link_name.contains('=') {
@@ -293,18 +296,16 @@
 
     let timestamp = out_dir.join("rustbuild.timestamp");
     if !up_to_date(Path::new("build.rs"), &timestamp) || !up_to_date(&src_dir, &timestamp) {
-        Ok(NativeLibBoilerplate {
-            src_dir: src_dir,
-            out_dir: out_dir,
-        })
+        Ok(NativeLibBoilerplate { src_dir, out_dir })
     } else {
         Err(())
     }
 }
 
-pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
-    -> Result<(NativeLibBoilerplate, String), ()>
-{
+#[allow(clippy::result_unit_err)]
+pub fn sanitizer_lib_boilerplate(
+    sanitizer_name: &str,
+) -> Result<(NativeLibBoilerplate, String), ()> {
     let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
         "x86_64-unknown-linux-gnu" => (
             format!("clang_rt.{}-x86_64", sanitizer_name),
@@ -328,7 +329,7 @@
         sanitizer_name,
         &to_link,
         search_path,
-        &vec![],
+        &[],
     )?;
     Ok((lib, link_name))
 }
diff --git a/sgx_cov/Cargo.toml b/sgx_cov/Cargo.toml
index e1c15d5..d2b41a9 100644
--- a/sgx_cov/Cargo.toml
+++ b/sgx_cov/Cargo.toml
@@ -1,16 +1,16 @@
 [package]
 name = "sgx_cov"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Enabling gcov for SGX crates."
 edition = "2018"
 
 [lib]
 name = "sgx_cov"
-path = "lib.rs"
+crate-type = ["rlib"]
 
 [dependencies]
 lazy_static = { version = "1", features = ["spin_no_std"] }
diff --git a/sgx_cov/lib.rs b/sgx_cov/src/lib.rs
similarity index 100%
rename from sgx_cov/lib.rs
rename to sgx_cov/src/lib.rs
diff --git a/sgx_crypto_helper/Cargo.toml b/sgx_crypto_helper/Cargo.toml
index d36b710..9d4a145 100644
--- a/sgx_crypto_helper/Cargo.toml
+++ b/sgx_crypto_helper/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_crypto_helper"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_crypto_helper/x86_64-unknown-linux-sgx.json b/sgx_crypto_helper/x86_64-unknown-linux-sgx.json
deleted file mode 100644
index 69b38be..0000000
--- a/sgx_crypto_helper/x86_64-unknown-linux-sgx.json
+++ /dev/null
@@ -1,38 +0,0 @@
-{
-  "arch": "x86_64",
-  "cpu": "x86-64",
-  "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
-  "dynamic-linking": true,
-  "env": "sgx",
-  "exe-allocation-crate": "alloc_system",
-  "executables": true,
-  "has-elf-tls": true,
-  "has-rpath": true,
-  "linker-flavor": "gcc",
-  "linker-is-gnu": true,
-  "llvm-target": "x86_64-unknown-linux-gnu",
-  "max-atomic-width": 64,
-  "os": "linux",
-  "position-independent-executables": true,
-  "pre-link-args": {
-    "gcc": [
-      "-Wl,--as-needed",
-      "-Wl,-z,noexecstack",
-      "-m64"
-    ]
-  },
-  "relro-level": "full",
-  "stack-probes": {
-    "kind": "inline-or-call",
-    "min-llvm-version-for-inline": [
-      11,
-      0,
-      1
-    ]
-  },
-  "target-c-int-width": "32",
-  "target-endian": "little",
-  "target-family": "unix",
-  "target-pointer-width": "64",
-  "vendor": "mesalock"
-}
diff --git a/sgx_demangle/Cargo.toml b/sgx_demangle/Cargo.toml
index be9ee9d..c05799d 100644
--- a/sgx_demangle/Cargo.toml
+++ b/sgx_demangle/Cargo.toml
@@ -1,10 +1,17 @@
 
 [package]
 name = "sgx_demangle"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
+
+[lib]
+name = "sgx_demangle"
+crate-type = ["rlib"]
+
+[features]
+default = []
diff --git a/sgx_demangle/src/legacy.rs b/sgx_demangle/src/legacy.rs
index e9e329c..7a1667d 100644
--- a/sgx_demangle/src/legacy.rs
+++ b/sgx_demangle/src/legacy.rs
@@ -1,3 +1,23 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+#![allow(clippy::manual_strip)]
+
+use core::char;
 use core::fmt;
 
 /// Representation of a demangled symbol name.
@@ -21,7 +41,7 @@
 /// # Examples
 ///
 /// ```
-/// use rustc_demangle::demangle;
+/// use sgx_demangle::demangle;
 ///
 /// assert_eq!(demangle("_ZN4testE").to_string(), "test");
 /// assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
@@ -45,23 +65,22 @@
 // Note that this demangler isn't quite as fancy as it could be. We have lots
 // of other information in our symbols like hashes, version, type information,
 // etc. Additionally, this doesn't handle glue symbols at all.
-pub fn demangle(s: &str) -> Result<Demangle, ()> {
+pub fn demangle(s: &str) -> Result<(Demangle, &str), ()> {
     // First validate the symbol. If it doesn't look like anything we're
     // expecting, we just print it literally. Note that we must handle non-Rust
     // symbols because we could have any function in the backtrace.
-    let inner;
-    if s.len() > 4 && s.starts_with("_ZN") && s.ends_with('E') {
-        inner = &s[3..s.len() - 1];
-    } else if s.len() > 3 && s.starts_with("ZN") && s.ends_with('E') {
+    let inner = if s.starts_with("_ZN") {
+        &s[3..]
+    } else if s.starts_with("ZN") {
         // On Windows, dbghelp strips leading underscores, so we accept "ZN...E"
         // form too.
-        inner = &s[2..s.len() - 1];
-    } else if s.len() > 5 && s.starts_with("__ZN") && s.ends_with('E') {
+        &s[2..]
+    } else if s.starts_with("__ZN") {
         // On OSX, symbols are prefixed with an extra _
-        inner = &s[4..s.len() - 1];
+        &s[4..]
     } else {
         return Err(());
-    }
+    };
 
     // only work with ascii text
     if inner.bytes().any(|c| c & 0x80 != 0) {
@@ -69,40 +88,32 @@
     }
 
     let mut elements = 0;
-    let mut chars = inner.chars().peekable();
-    loop {
-        let mut i = 0usize;
-        while let Some(&c) = chars.peek() {
-            if !c.is_digit(10) {
-                break
-            }
-            chars.next();
-            let next = i.checked_mul(10)
-                .and_then(|i| i.checked_add(c as usize - '0' as usize));
-            i = match next {
-                Some(i) => i,
-                None => {
-                    return Err(());
-                }
-            };
+    let mut chars = inner.chars();
+    let mut c = chars.next().ok_or(())?;
+    while c != 'E' {
+        // Decode an identifier element's length.
+        if !c.is_digit(10) {
+            return Err(());
+        }
+        let mut len = 0usize;
+        while let Some(d) = c.to_digit(10) {
+            len = len
+                .checked_mul(10)
+                .and_then(|len| len.checked_add(d as usize))
+                .ok_or(())?;
+            c = chars.next().ok_or(())?;
         }
 
-        if i == 0 {
-            if !chars.next().is_none() {
-                return Err(());
-            }
-            break;
-        } else if chars.by_ref().take(i).count() != i {
-            return Err(());
-        } else {
-            elements += 1;
+        // `c` already contains the first character of this identifier, skip it and
+        // all the other characters of this identifier, to reach the next element.
+        for _ in 0..len {
+            c = chars.next().ok_or(())?;
         }
+
+        elements += 1;
     }
 
-    Ok(Demangle {
-        inner: inner,
-        elements: elements,
-    })
+    Ok((Demangle { inner, elements }, chars.as_str()))
 }
 
 // Rust hashes are hex digits with an `h` prepended.
@@ -124,7 +135,7 @@
             rest = &rest[..i];
             // Skip printing the hash if alternate formatting
             // was requested.
-            if f.alternate() && element+1 == self.elements && is_rust_hash(&rest) {
+            if f.alternate() && element + 1 == self.elements && is_rust_hash(rest) {
                 break;
             }
             if element != 0 {
@@ -133,7 +144,7 @@
             if rest.starts_with("_$") {
                 rest = &rest[1..];
             }
-            while !rest.is_empty() {
+            loop {
                 if rest.starts_with('.') {
                     if let Some('.') = rest[1..].chars().next() {
                         f.write_str("::")?;
@@ -143,55 +154,53 @@
                         rest = &rest[1..];
                     }
                 } else if rest.starts_with('$') {
-                    macro_rules! demangle {
-                        ($($pat:expr => $demangled:expr,)*) => ({
-                            $(if rest.starts_with($pat) {
-                                f.write_str($demangled)?;
-                                rest = &rest[$pat.len()..];
-                              } else)*
-                            {
-                                f.write_str(rest)?;
-                                break;
-                            }
-
-                        })
-                    }
-
-                    // see src/librustc/back/link.rs for these mappings
-                    demangle! {
-                        "$SP$" => "@",
-                        "$BP$" => "*",
-                        "$RF$" => "&",
-                        "$LT$" => "<",
-                        "$GT$" => ">",
-                        "$LP$" => "(",
-                        "$RP$" => ")",
-                        "$C$" => ",",
-
-                        // in theory we can demangle any Unicode code point, but
-                        // for simplicity we just catch the common ones.
-                        "$u7e$" => "~",
-                        "$u20$" => " ",
-                        "$u27$" => "'",
-                        "$u3d$" => "=",
-                        "$u5b$" => "[",
-                        "$u5d$" => "]",
-                        "$u7b$" => "{",
-                        "$u7d$" => "}",
-                        "$u3b$" => ";",
-                        "$u2b$" => "+",
-                        "$u21$" => "!",
-                        "$u22$" => "\"",
-                    }
-                } else {
-                    let idx = match rest.char_indices().find(|&(_, c)| c == '$' || c == '.') {
-                        None => rest.len(),
-                        Some((i, _)) => i,
+                    let (escape, after_escape) = if let Some(end) = rest[1..].find('$') {
+                        (&rest[1..=end], &rest[end + 2..])
+                    } else {
+                        break;
                     };
-                    f.write_str(&rest[..idx])?;
-                    rest = &rest[idx..];
+
+                    // see src/librustc_codegen_utils/symbol_names/legacy.rs for these mappings
+                    let unescaped = match escape {
+                        "SP" => "@",
+                        "BP" => "*",
+                        "RF" => "&",
+                        "LT" => "<",
+                        "GT" => ">",
+                        "LP" => "(",
+                        "RP" => ")",
+                        "C" => ",",
+
+                        _ => {
+                            if escape.starts_with('u') {
+                                let digits = &escape[1..];
+                                let all_lower_hex =
+                                    digits.chars().all(|c| matches!(c, '0'..='9' | 'a'..='f'));
+                                let c = u32::from_str_radix(digits, 16)
+                                    .ok()
+                                    .and_then(char::from_u32);
+                                if let (true, Some(c)) = (all_lower_hex, c) {
+                                    // FIXME(eddyb) do we need to filter out control codepoints?
+                                    if !c.is_control() {
+                                        c.fmt(f)?;
+                                        rest = after_escape;
+                                        continue;
+                                    }
+                                }
+                            }
+                            break;
+                        }
+                    };
+                    f.write_str(unescaped)?;
+                    rest = after_escape;
+                } else if let Some(i) = rest.find(|c| c == '$' || c == '.') {
+                    f.write_str(&rest[..i])?;
+                    rest = &rest[i..];
+                } else {
+                    break;
                 }
             }
+            f.write_str(rest)?;
         }
 
         Ok(())
diff --git a/sgx_demangle/src/lib.rs b/sgx_demangle/src/lib.rs
index d5ae1a6..5616a59 100644
--- a/sgx_demangle/src/lib.rs
+++ b/sgx_demangle/src/lib.rs
@@ -1,3 +1,20 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
 //! Demangle Rust compiler symbol names.
 //!
 //! This crate provides a `demangle` function which will return a `Demangle`
@@ -12,7 +29,7 @@
 //! # Examples
 //!
 //! ```
-//! use rustc_demangle::demangle;
+//! use sgx_demangle::demangle;
 //!
 //! assert_eq!(demangle("_ZN4testE").to_string(), "test");
 //! assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
@@ -24,13 +41,16 @@
 //! ```
 
 #![no_std]
-
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
+#![allow(clippy::many_single_char_names)]
 
 mod legacy;
 mod v0;
 
-use core::fmt;
+use core::fmt::{self, Write as _};
 
 /// Representation of a demangled symbol name.
 pub struct Demangle<'a> {
@@ -53,7 +73,7 @@
 /// # Examples
 ///
 /// ```
-/// use rustc_demangle::demangle;
+/// use sgx_demangle::demangle;
 ///
 /// assert_eq!(demangle("_ZN4testE").to_string(), "test");
 /// assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
@@ -66,41 +86,51 @@
     let llvm = ".llvm.";
     if let Some(i) = s.find(llvm) {
         let candidate = &s[i + llvm.len()..];
-        let all_hex = candidate.chars().all(|c| {
-            match c {
-                'A' ..= 'F' | '0' ..= '9' | '@' => true,
-                _ => false,
-            }
-        });
+        let all_hex = candidate
+            .chars()
+            .all(|c| matches!(c, 'A'..='F' | '0'..='9' | '@'));
 
         if all_hex {
             s = &s[..i];
         }
     }
 
+    let mut suffix = "";
+    let mut style = match legacy::demangle(s) {
+        Ok((d, s)) => {
+            suffix = s;
+            Some(DemangleStyle::Legacy(d))
+        }
+        Err(()) => match v0::demangle(s) {
+            Ok((d, s)) => {
+                suffix = s;
+                Some(DemangleStyle::V0(d))
+            }
+            // FIXME(eddyb) would it make sense to treat an unknown-validity
+            // symbol (e.g. one that errored with `RecursedTooDeep`) as
+            // v0-mangled, and have the error show up in the demangling?
+            // (that error already gets past this initial check, and therefore
+            // will show up in the demangling, if hidden behind a backref)
+            Err(v0::ParseError::Invalid) | Err(v0::ParseError::RecursedTooDeep) => None,
+        },
+    };
+
     // Output like LLVM IR adds extra period-delimited words. See if
     // we are in that case and save the trailing words if so.
-    let mut suffix = "";
-    if let Some(i) = s.rfind("E.") {
-        let (head, tail) = s.split_at(i + 1); // After the E, before the period
-
-        if is_symbol_like(tail) {
-            s = head;
-            suffix = tail;
+    if !suffix.is_empty() {
+        if suffix.starts_with('.') && is_symbol_like(suffix) {
+            // Keep the suffix.
+        } else {
+            // Reset the suffix and invalidate the demangling.
+            suffix = "";
+            style = None;
         }
     }
 
-    let style = match legacy::demangle(s) {
-        Ok(d) => Some(DemangleStyle::Legacy(d)),
-        Err(()) => match v0::demangle(s) {
-            Ok(d) => Some(DemangleStyle::V0(d)),
-            Err(v0::Invalid) => None,
-        },
-    };
     Demangle {
-        style: style,
+        style,
         original: s,
-        suffix: suffix,
+        suffix,
     }
 }
 
@@ -114,15 +144,15 @@
 /// to be a Rust symbol, rather than "demangling" the given string as a no-op.
 ///
 /// ```
-/// extern crate rustc_demangle;
+/// extern crate sgx_demangle;
 ///
 /// let not_a_rust_symbol = "la la la";
 ///
 /// // The `try_demangle` function will reject strings which are not Rust symbols.
-/// assert!(rustc_demangle::try_demangle(not_a_rust_symbol).is_err());
+/// assert!(sgx_demangle::try_demangle(not_a_rust_symbol).is_err());
 ///
 /// // While `demangle` will just pass the non-symbol through as a no-op.
-/// assert_eq!(rustc_demangle::demangle(not_a_rust_symbol).as_str(), not_a_rust_symbol);
+/// assert_eq!(sgx_demangle::demangle(not_a_rust_symbol).as_str(), not_a_rust_symbol);
 /// ```
 pub fn try_demangle(s: &str) -> Result<Demangle, TryDemangleError> {
     let sym = demangle(s);
@@ -150,22 +180,44 @@
 
 // Copied from the documentation of `char::is_ascii_alphanumeric`
 fn is_ascii_alphanumeric(c: char) -> bool {
-    match c {
-        '\u{0041}' ..= '\u{005A}' |
-        '\u{0061}' ..= '\u{007A}' |
-        '\u{0030}' ..= '\u{0039}' => true,
-        _ => false,
-    }
+    matches!(c, '\u{0041}'..='\u{005A}' | '\u{0061}'..='\u{007A}' | '\u{0030}'..='\u{0039}')
 }
 
 // Copied from the documentation of `char::is_ascii_punctuation`
 fn is_ascii_punctuation(c: char) -> bool {
-    match c {
-        '\u{0021}' ..= '\u{002F}' |
-        '\u{003A}' ..= '\u{0040}' |
-        '\u{005B}' ..= '\u{0060}' |
-        '\u{007B}' ..= '\u{007E}' => true,
-        _ => false,
+    matches!(c, '\u{0021}'..='\u{002F}' | '\u{003A}'..='\u{0040}' | '\u{005B}'..='\u{0060}' | '\u{007B}'..='\u{007E}')
+}
+
+impl<'a> fmt::Display for DemangleStyle<'a> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match *self {
+            DemangleStyle::Legacy(ref d) => fmt::Display::fmt(d, f),
+            DemangleStyle::V0(ref d) => fmt::Display::fmt(d, f),
+        }
+    }
+}
+
+// Maximum size of the symbol that we'll print.
+const MAX_SIZE: usize = 1_000_000;
+
+#[derive(Copy, Clone, Debug)]
+struct SizeLimitExhausted;
+
+struct SizeLimitedFmtAdapter<F> {
+    remaining: Result<usize, SizeLimitExhausted>,
+    inner: F,
+}
+
+impl<F: fmt::Write> fmt::Write for SizeLimitedFmtAdapter<F> {
+    fn write_str(&mut self, s: &str) -> fmt::Result {
+        self.remaining = self
+            .remaining
+            .and_then(|r| r.checked_sub(s.len()).ok_or(SizeLimitExhausted));
+
+        match self.remaining {
+            Ok(_) => self.inner.write_str(s),
+            Err(SizeLimitExhausted) => Err(fmt::Error),
+        }
     }
 }
 
@@ -173,11 +225,31 @@
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self.style {
             None => f.write_str(self.original)?,
-            Some(DemangleStyle::Legacy(ref d)) => {
-                fmt::Display::fmt(d, f)?
-            }
-            Some(DemangleStyle::V0(ref d)) => {
-                fmt::Display::fmt(d, f)?
+            Some(ref d) => {
+                let alternate = f.alternate();
+                let mut size_limited_fmt = SizeLimitedFmtAdapter {
+                    remaining: Ok(MAX_SIZE),
+                    inner: &mut *f,
+                };
+                let fmt_result = if alternate {
+                    write!(size_limited_fmt, "{:#}", d)
+                } else {
+                    write!(size_limited_fmt, "{}", d)
+                };
+                let size_limit_result = size_limited_fmt.remaining.map(|_| ());
+
+                // Translate a `fmt::Error` generated by `SizeLimitedFmtAdapter`
+                // into an error message, instead of propagating it upwards
+                // (which could cause panicking from inside e.g. `std::io::print`).
+                match (fmt_result, size_limit_result) {
+                    (Err(_), Err(SizeLimitExhausted)) => f.write_str("{size limit reached}")?,
+
+                    _ => {
+                        fmt_result?;
+                        size_limit_result
+                            .expect("`fmt::Error` from `SizeLimitedFmtAdapter` was discarded");
+                    }
+                }
             }
         }
         f.write_str(self.suffix)
diff --git a/sgx_demangle/src/v0.rs b/sgx_demangle/src/v0.rs
index f8b869d..b034925 100644
--- a/sgx_demangle/src/v0.rs
+++ b/sgx_demangle/src/v0.rs
@@ -1,25 +1,54 @@
-use core::char;
-use core::fmt;
-use core::fmt::Display;
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use core::convert::TryFrom;
+use core::{char, fmt, iter, mem, str};
+
+// Maximum recursion depth when parsing symbols before we just bail out saying
+// "this symbol is invalid"
+const MAX_DEPTH: u32 = 500;
 
 /// Representation of a demangled symbol name.
 pub struct Demangle<'a> {
     inner: &'a str,
 }
 
+#[derive(PartialEq, Eq, Debug)]
+pub enum ParseError {
+    /// Symbol doesn't match the expected `v0` grammar.
+    Invalid,
+
+    /// Parsing the symbol crossed the recursion limit (see `MAX_DEPTH`).
+    RecursedTooDeep,
+}
+
 /// De-mangles a Rust symbol into a more readable version
 ///
 /// This function will take a **mangled** symbol and return a value. When printed,
 /// the de-mangled version will be written. If the symbol does not look like
 /// a mangled symbol, the original value will be written instead.
-pub fn demangle(s: &str) -> Result<Demangle, Invalid> {
+pub fn demangle(s: &str) -> Result<(Demangle, &str), ParseError> {
     // First validate the symbol. If it doesn't look like anything we're
     // expecting, we just print it literally. Note that we must handle non-Rust
     // symbols because we could have any function in the backtrace.
     let inner;
     if s.len() > 2 && s.starts_with("_R") {
         inner = &s[2..];
-    } else if s.len() > 1 && s.starts_with("R") {
+    } else if s.len() > 1 && s.starts_with('R') {
         // On Windows, dbghelp strips leading underscores, so we accept "R..."
         // form too.
         inner = &s[1..];
@@ -27,56 +56,62 @@
         // On OSX, symbols are prefixed with an extra _
         inner = &s[3..];
     } else {
-        return Err(Invalid);
+        return Err(ParseError::Invalid);
     }
 
     // Paths always start with uppercase characters.
     match inner.as_bytes()[0] {
         b'A'..=b'Z' => {}
-        _ => return Err(Invalid),
+        _ => return Err(ParseError::Invalid),
     }
 
     // only work with ascii text
     if inner.bytes().any(|c| c & 0x80 != 0) {
-        return Err(Invalid);
+        return Err(ParseError::Invalid);
     }
 
     // Verify that the symbol is indeed a valid path.
+    let try_parse_path = |parser| {
+        let mut dummy_printer = Printer {
+            parser: Ok(parser),
+            out: None,
+            bound_lifetime_depth: 0,
+        };
+        dummy_printer
+            .print_path(false)
+            .expect("`fmt::Error`s should be impossible without a `fmt::Formatter`");
+        dummy_printer.parser
+    };
     let mut parser = Parser {
         sym: inner,
         next: 0,
+        depth: 0,
     };
-    parser.skip_path()?;
-    if parser.next < parser.sym.len() {
-        // Instantiating crate.
-        parser.skip_path()?;
-    }
-    if parser.next != parser.sym.len() {
-        return Err(Invalid);
+    parser = try_parse_path(parser)?;
+
+    // Instantiating crate (paths always start with uppercase characters).
+    if let Some(&(b'A'..=b'Z')) = parser.sym.as_bytes().get(parser.next) {
+        parser = try_parse_path(parser)?;
     }
 
-    Ok(Demangle {
-        inner: inner,
-    })
+    Ok((Demangle { inner }, &parser.sym[parser.next..]))
 }
 
-impl<'s> Display for Demangle<'s> {
+impl<'s> fmt::Display for Demangle<'s> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut printer = Printer {
             parser: Ok(Parser {
                 sym: self.inner,
                 next: 0,
+                depth: 0,
             }),
-            out: f,
+            out: Some(f),
             bound_lifetime_depth: 0,
         };
         printer.print_path(true)
     }
 }
 
-#[derive(PartialEq, Eq)]
-pub struct Invalid;
-
 struct Ident<'s> {
     /// ASCII part of the identifier.
     ascii: &'s str,
@@ -90,10 +125,7 @@
     /// Attempt to decode punycode on the stack (allocation-free),
     /// and pass the char slice to the closure, if successful.
     /// This supports up to `SMALL_PUNYCODE_LEN` characters.
-    fn try_small_punycode_decode<F: FnOnce(&[char]) -> R, R>(
-        &self,
-        f: F,
-    ) -> Option<R> {
+    fn try_small_punycode_decode<F: FnOnce(&[char]) -> R, R>(&self, f: F) -> Option<R> {
         let mut out = ['\0'; SMALL_PUNYCODE_LEN];
         let mut out_len = 0;
         let r = self.punycode_decode(|i, c| {
@@ -157,7 +189,7 @@
             let mut w = 1;
             let mut k: usize = 0;
             loop {
-                use core::cmp::{min, max};
+                use core::cmp::{max, min};
 
                 k += base;
                 let t = min(max(k.saturating_sub(bias), t_min), t_max);
@@ -168,9 +200,7 @@
                     _ => return Err(()),
                 };
                 let d = d as usize;
-                delta = delta.checked_add(
-                    d.checked_mul(w).ok_or(())?
-                ).ok_or(())?;
+                delta = delta.checked_add(d.checked_mul(w).ok_or(())?).ok_or(())?;
                 if d < t {
                     break;
                 }
@@ -214,14 +244,15 @@
     }
 }
 
-impl<'s> Display for Ident<'s> {
+impl<'s> fmt::Display for Ident<'s> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         self.try_small_punycode_decode(|chars| {
             for &c in chars {
                 c.fmt(f)?;
             }
             Ok(())
-        }).unwrap_or_else(|| {
+        })
+        .unwrap_or_else(|| {
             if !self.punycode.is_empty() {
                 f.write_str("punycode{")?;
 
@@ -241,6 +272,109 @@
     }
 }
 
+/// Sequence of lowercase hexadecimal nibbles (`0-9a-f`), used by leaf consts.
+struct HexNibbles<'s> {
+    nibbles: &'s str,
+}
+
+impl<'s> HexNibbles<'s> {
+    /// Decode an integer value (with the "most significant nibble" first),
+    /// returning `None` if it can't fit in an `u64`.
+    // FIXME(eddyb) should this "just" use `u128` instead?
+    fn try_parse_uint(&self) -> Option<u64> {
+        let nibbles = self.nibbles.trim_start_matches('0');
+
+        if nibbles.len() > 16 {
+            return None;
+        }
+
+        let mut v = 0;
+        for nibble in nibbles.chars() {
+            v = (v << 4) | (nibble.to_digit(16).unwrap() as u64);
+        }
+        Some(v)
+    }
+
+    /// Decode a UTF-8 byte sequence (with each byte using a pair of nibbles)
+    /// into individual `char`s, returning `None` for invalid UTF-8.
+    #[allow(clippy::needless_range_loop)]
+    fn try_parse_str_chars(&self) -> Option<impl Iterator<Item = char> + 's> {
+        if self.nibbles.len() % 2 != 0 {
+            return None;
+        }
+
+        // FIXME(eddyb) use `array_chunks` instead, when that becomes stable.
+        let mut bytes = self
+            .nibbles
+            .as_bytes()
+            .chunks_exact(2)
+            .map(|slice| match slice {
+                [a, b] => [a, b],
+                _ => unreachable!(),
+            })
+            .map(|[&hi, &lo]| {
+                let half = |nibble: u8| (nibble as char).to_digit(16).unwrap() as u8;
+                (half(hi) << 4) | half(lo)
+            });
+
+        let chars = iter::from_fn(move || {
+            // As long as there are any bytes left, there's at least one more
+            // UTF-8-encoded `char` to decode (or the possibility of error).
+            bytes.next().map(|first_byte| -> Result<char, ()> {
+                // FIXME(eddyb) this `enum` and `fn` should be somewhere in `core`.
+                enum Utf8FirstByteError {
+                    ContinuationByte,
+                    TooLong,
+                }
+                fn utf8_len_from_first_byte(byte: u8) -> Result<usize, Utf8FirstByteError> {
+                    match byte {
+                        0x00..=0x7f => Ok(1),
+                        0x80..=0xbf => Err(Utf8FirstByteError::ContinuationByte),
+                        0xc0..=0xdf => Ok(2),
+                        0xe0..=0xef => Ok(3),
+                        0xf0..=0xf7 => Ok(4),
+                        0xf8..=0xff => Err(Utf8FirstByteError::TooLong),
+                    }
+                }
+
+                // Collect the appropriate amount of bytes (up to 4), according
+                // to the UTF-8 length implied by the first byte.
+                let utf8_len = utf8_len_from_first_byte(first_byte).map_err(|_| ())?;
+                let utf8 = &mut [first_byte, 0, 0, 0][..utf8_len];
+                for i in 1..utf8_len {
+                    utf8[i] = bytes.next().ok_or(())?;
+                }
+
+                // Fully validate the UTF-8 sequence.
+                let s = str::from_utf8(utf8).map_err(|_| ())?;
+
+                // Since we included exactly one UTF-8 sequence, and validation
+                // succeeded, `str::chars` should return exactly one `char`.
+                let mut chars = s.chars();
+                match (chars.next(), chars.next()) {
+                    (Some(c), None) => Ok(c),
+                    _ => unreachable!(
+                        "str::from_utf8({:?}) = {:?} was expected to have 1 char, \
+                         but {} chars were found",
+                        utf8,
+                        s,
+                        s.chars().count()
+                    ),
+                }
+            })
+        });
+
+        // HACK(eddyb) doing a separate validation iteration like this might be
+        // wasteful, but it's easier to avoid starting to print a string literal
+        // in the first place, than to abort it mid-string.
+        if chars.clone().any(|r| r.is_err()) {
+            None
+        } else {
+            Some(chars.map(Result::unwrap))
+        }
+    }
+}
+
 fn basic_type(tag: u8) -> Option<&'static str> {
     Some(match tag {
         b'b' => "bool",
@@ -272,9 +406,23 @@
 struct Parser<'s> {
     sym: &'s str,
     next: usize,
+    depth: u32,
 }
 
 impl<'s> Parser<'s> {
+    fn push_depth(&mut self) -> Result<(), ParseError> {
+        self.depth += 1;
+        if self.depth > MAX_DEPTH {
+            Err(ParseError::RecursedTooDeep)
+        } else {
+            Ok(())
+        }
+    }
+
+    fn pop_depth(&mut self) {
+        self.depth -= 1;
+    }
+
     fn peek(&self) -> Option<u8> {
         self.sym.as_bytes().get(self.next).cloned()
     }
@@ -288,45 +436,47 @@
         }
     }
 
-    fn next(&mut self) -> Result<u8, Invalid> {
-        let b = self.peek().ok_or(Invalid)?;
+    fn next(&mut self) -> Result<u8, ParseError> {
+        let b = self.peek().ok_or(ParseError::Invalid)?;
         self.next += 1;
         Ok(b)
     }
 
-    fn hex_nibbles(&mut self) -> Result<&'s str, Invalid> {
+    fn hex_nibbles(&mut self) -> Result<HexNibbles<'s>, ParseError> {
         let start = self.next;
         loop {
             match self.next()? {
                 b'0'..=b'9' | b'a'..=b'f' => {}
                 b'_' => break,
-                _ => return Err(Invalid),
+                _ => return Err(ParseError::Invalid),
             }
         }
-        Ok(&self.sym[start..self.next - 1])
+        Ok(HexNibbles {
+            nibbles: &self.sym[start..self.next - 1],
+        })
     }
 
-    fn digit_10(&mut self) -> Result<u8, Invalid> {
+    fn digit_10(&mut self) -> Result<u8, ParseError> {
         let d = match self.peek() {
             Some(d @ b'0'..=b'9') => d - b'0',
-            _ => return Err(Invalid),
+            _ => return Err(ParseError::Invalid),
         };
         self.next += 1;
         Ok(d)
     }
 
-    fn digit_62(&mut self) -> Result<u8, Invalid> {
+    fn digit_62(&mut self) -> Result<u8, ParseError> {
         let d = match self.peek() {
             Some(d @ b'0'..=b'9') => d - b'0',
             Some(d @ b'a'..=b'z') => 10 + (d - b'a'),
             Some(d @ b'A'..=b'Z') => 10 + 26 + (d - b'A'),
-            _ => return Err(Invalid),
+            _ => return Err(ParseError::Invalid),
         };
         self.next += 1;
         Ok(d)
     }
 
-    fn integer_62(&mut self) -> Result<u64, Invalid> {
+    fn integer_62(&mut self) -> Result<u64, ParseError> {
         if self.eat(b'_') {
             return Ok(0);
         }
@@ -334,24 +484,24 @@
         let mut x: u64 = 0;
         while !self.eat(b'_') {
             let d = self.digit_62()? as u64;
-            x = x.checked_mul(62).ok_or(Invalid)?;
-            x = x.checked_add(d).ok_or(Invalid)?;
+            x = x.checked_mul(62).ok_or(ParseError::Invalid)?;
+            x = x.checked_add(d).ok_or(ParseError::Invalid)?;
         }
-        x.checked_add(1).ok_or(Invalid)
+        x.checked_add(1).ok_or(ParseError::Invalid)
     }
 
-    fn opt_integer_62(&mut self, tag: u8) -> Result<u64, Invalid> {
+    fn opt_integer_62(&mut self, tag: u8) -> Result<u64, ParseError> {
         if !self.eat(tag) {
             return Ok(0);
         }
-        self.integer_62()?.checked_add(1).ok_or(Invalid)
+        self.integer_62()?.checked_add(1).ok_or(ParseError::Invalid)
     }
 
-    fn disambiguator(&mut self) -> Result<u64, Invalid> {
+    fn disambiguator(&mut self) -> Result<u64, ParseError> {
         self.opt_integer_62(b's')
     }
 
-    fn namespace(&mut self) -> Result<Option<char>, Invalid> {
+    fn namespace(&mut self) -> Result<Option<char>, ParseError> {
         match self.next()? {
             // Special namespaces, like closures and shims.
             ns @ b'A'..=b'Z' => Ok(Some(ns as char)),
@@ -359,34 +509,32 @@
             // Implementation-specific/unspecified namespaces.
             b'a'..=b'z' => Ok(None),
 
-            _ => Err(Invalid),
+            _ => Err(ParseError::Invalid),
         }
     }
 
-    fn backref(&mut self) -> Result<Parser<'s>, Invalid> {
+    fn backref(&mut self) -> Result<Parser<'s>, ParseError> {
         let s_start = self.next - 1;
         let i = self.integer_62()?;
         if i >= s_start as u64 {
-            return Err(Invalid);
+            return Err(ParseError::Invalid);
         }
-        Ok(Parser {
+        let mut new_parser = Parser {
             sym: self.sym,
             next: i as usize,
-        })
+            depth: self.depth,
+        };
+        new_parser.push_depth()?;
+        Ok(new_parser)
     }
 
-    fn ident(&mut self) -> Result<Ident<'s>, Invalid> {
+    fn ident(&mut self) -> Result<Ident<'s>, ParseError> {
         let is_punycode = self.eat(b'u');
         let mut len = self.digit_10()? as usize;
         if len != 0 {
-            loop {
-                match self.digit_10() {
-                    Ok(d) => {
-                        len = len.checked_mul(10).ok_or(Invalid)?;
-                        len = len.checked_add(d as usize).ok_or(Invalid)?;
-                    }
-                    Err(Invalid) => break,
-                }
+            while let Ok(d) = self.digit_10() {
+                len = len.checked_mul(10).ok_or(ParseError::Invalid)?;
+                len = len.checked_add(d as usize).ok_or(ParseError::Invalid)?;
             }
         }
 
@@ -394,9 +542,9 @@
         self.eat(b'_');
 
         let start = self.next;
-        self.next = self.next.checked_add(len).ok_or(Invalid)?;
+        self.next = self.next.checked_add(len).ok_or(ParseError::Invalid)?;
         if self.next > self.sym.len() {
-            return Err(Invalid);
+            return Err(ParseError::Invalid);
         }
 
         let ident = &self.sym[start..self.next];
@@ -413,7 +561,7 @@
                 },
             };
             if ident.punycode.is_empty() {
-                return Err(Invalid);
+                return Err(ParseError::Invalid);
             }
             Ok(ident)
         } else {
@@ -423,210 +571,177 @@
             })
         }
     }
-
-    fn skip_path(&mut self) -> Result<(), Invalid> {
-        match self.next()? {
-            b'C' => {
-                self.disambiguator()?;
-                self.ident()?;
-            }
-            b'N' => {
-                self.namespace()?;
-                self.skip_path()?;
-                self.disambiguator()?;
-                self.ident()?;
-            }
-            b'M' => {
-                self.disambiguator()?;
-                self.skip_path()?;
-                self.skip_type()?;
-            }
-            b'X' => {
-                self.disambiguator()?;
-                self.skip_path()?;
-                self.skip_type()?;
-                self.skip_path()?;
-            }
-            b'Y' => {
-                self.skip_type()?;
-                self.skip_path()?;
-            }
-            b'I' => {
-                self.skip_path()?;
-                while !self.eat(b'E') {
-                    self.skip_generic_arg()?;
-                }
-            }
-            b'B' => {
-                self.backref()?;
-            }
-            _ => return Err(Invalid),
-        }
-        Ok(())
-    }
-
-    fn skip_generic_arg(&mut self) -> Result<(), Invalid> {
-        if self.eat(b'L') {
-            self.integer_62()?;
-            Ok(())
-        } else if self.eat(b'K') {
-            self.skip_const()
-        } else {
-            self.skip_type()
-        }
-    }
-
-    fn skip_type(&mut self) -> Result<(), Invalid> {
-        match self.next()? {
-            tag if basic_type(tag).is_some() => {}
-
-            b'R' | b'Q' => {
-                if self.eat(b'L') {
-                    self.integer_62()?;
-                }
-                self.skip_type()?;
-            }
-            b'P' | b'O' | b'S' => self.skip_type()?,
-            b'A' => {
-                self.skip_type()?;
-                self.skip_const()?;
-            }
-            b'T' => while !self.eat(b'E') {
-                self.skip_type()?;
-            },
-            b'F' => {
-                let _binder = self.opt_integer_62(b'G')?;
-                let _is_unsafe = self.eat(b'U');
-                if self.eat(b'K') {
-                    let c_abi = self.eat(b'C');
-                    if !c_abi {
-                        let abi = self.ident()?;
-                        if abi.ascii.is_empty() || !abi.punycode.is_empty() {
-                            return Err(Invalid);
-                        }
-                    }
-                }
-                while !self.eat(b'E') {
-                    self.skip_type()?;
-                }
-                self.skip_type()?;
-            }
-            b'D' => {
-                let _binder = self.opt_integer_62(b'G')?;
-                while !self.eat(b'E') {
-                    self.skip_path()?;
-                    while self.eat(b'p') {
-                        self.ident()?;
-                        self.skip_type()?;
-                    }
-                }
-                if !self.eat(b'L') {
-                    return Err(Invalid);
-                }
-                self.integer_62()?;
-            }
-            b'B' => {
-                self.backref()?;
-            }
-            _ => {
-                // Go back to the tag, so `skip_path` also sees it.
-                self.next -= 1;
-                self.skip_path()?;
-            }
-        }
-        Ok(())
-    }
-
-    fn skip_const(&mut self) -> Result<(), Invalid> {
-        if self.eat(b'B') {
-            self.backref()?;
-            return Ok(());
-        }
-
-        match self.next()? {
-            // Unsigned integer types.
-            b'h' | b't' | b'm' | b'y' | b'o' | b'j' => {}
-
-            _ => return Err(Invalid),
-        }
-
-        if self.eat(b'p') {
-            return Ok(());
-        }
-        self.hex_nibbles()?;
-        Ok(())
-    }
 }
 
 struct Printer<'a, 'b: 'a, 's> {
-    parser: Result<Parser<'s>, Invalid>,
-    out: &'a mut fmt::Formatter<'b>,
+    /// The input parser to demangle from, or `Err` if any (parse) error was
+    /// encountered (in order to disallow further likely-incorrect demangling).
+    ///
+    /// See also the documentation on the `invalid!` and `parse!` macros below.
+    parser: Result<Parser<'s>, ParseError>,
+
+    /// The output formatter to demangle to, or `None` while skipping printing.
+    out: Option<&'a mut fmt::Formatter<'b>>,
+
+    /// Cumulative number of lifetimes bound by `for<...>` binders ('G'),
+    /// anywhere "around" the current entity (e.g. type) being demangled.
+    /// This value is not tracked while skipping printing, as it'd be unused.
+    ///
+    /// See also the documentation on the `Printer::in_binder` method.
     bound_lifetime_depth: u32,
 }
 
-/// Mark the parser as errored, print `?` and return early.
-/// This allows callers to keep printing the approximate
-/// syntax of the path/type/const, despite having errors.
-/// E.g. `Vec<[(A, ?); ?]>` instead of `Vec<[(A, ?`.
+impl ParseError {
+    /// Snippet to print when the error is initially encountered.
+    fn message(&self) -> &str {
+        match self {
+            ParseError::Invalid => "{invalid syntax}",
+            ParseError::RecursedTooDeep => "{recursion limit reached}",
+        }
+    }
+}
+
+/// Mark the parser as errored (with `ParseError::Invalid`), print the
+/// appropriate message (see `ParseError::message`) and return early.
 macro_rules! invalid {
     ($printer:ident) => {{
-        $printer.parser = Err(Invalid);
-        return $printer.out.write_str("?");
-    }}
+        let err = ParseError::Invalid;
+        $printer.print(err.message())?;
+        $printer.parser = Err(err);
+        return Ok(());
+    }};
 }
 
 /// Call a parser method (if the parser hasn't errored yet),
-/// and mark the parser as errored if it returns `Err(Invalid)`.
+/// and mark the parser as errored if it returns `Err`.
 ///
-/// If the parser errored, before or now, prints `?`, and
-/// returns early the current function (see `invalid!` above).
+/// If the parser errored, before or now, this returns early,
+/// from the current function, after printing either:
+/// * for a new error, the appropriate message (see `ParseError::message`)
+/// * for an earlier error, only `?` -  this allows callers to keep printing
+///   the approximate syntax of the path/type/const, despite having errors,
+///   e.g. `Vec<[(A, ?); ?]>` instead of `Vec<[(A, ?`
 macro_rules! parse {
     ($printer:ident, $method:ident $(($($arg:expr),*))*) => {
-        match $printer.parser_mut().and_then(|p| p.$method($($($arg),*)*)) {
-            Ok(x) => x,
-            Err(Invalid) => invalid!($printer),
+        match $printer.parser {
+            Ok(ref mut parser) => match parser.$method($($($arg),*)*) {
+                Ok(x) => x,
+                Err(err) => {
+                    $printer.print(err.message())?;
+                    $printer.parser = Err(err);
+                    return Ok(());
+                }
+            }
+            Err(_) => return $printer.print("?"),
         }
     };
 }
 
 impl<'a, 'b, 's> Printer<'a, 'b, 's> {
-    fn parser_mut<'c>(&'c mut self) -> Result<&'c mut Parser<'s>, Invalid> {
-        self.parser.as_mut().map_err(|_| Invalid)
-    }
-
     /// Eat the given character from the parser,
     /// returning `false` if the parser errored.
     fn eat(&mut self, b: u8) -> bool {
-        self.parser_mut().map(|p| p.eat(b)) == Ok(true)
+        self.parser.as_mut().map(|p| p.eat(b)) == Ok(true)
     }
 
-    /// Return a nested parser for a backref.
-    fn backref_printer<'c>(&'c mut self) -> Printer<'c, 'b, 's> {
-        Printer {
-            parser: self.parser_mut().and_then(|p| p.backref()),
-            out: self.out,
-            bound_lifetime_depth: self.bound_lifetime_depth,
+    /// Skip printing (i.e. `self.out` will be `None`) for the duration of the
+    /// given closure. This should not change parsing behavior, only disable the
+    /// output, but there may be optimizations (such as not traversing backrefs).
+    fn skipping_printing<F>(&mut self, f: F)
+    where
+        F: FnOnce(&mut Self) -> fmt::Result,
+    {
+        let orig_out = self.out.take();
+        f(self).expect("`fmt::Error`s should be impossible without a `fmt::Formatter`");
+        self.out = orig_out;
+    }
+
+    /// Print the target of a backref, using the given closure.
+    /// When printing is being skipped, the backref will only be parsed,
+    /// ignoring the backref's target completely.
+    fn print_backref<F>(&mut self, f: F) -> fmt::Result
+    where
+        F: FnOnce(&mut Self) -> fmt::Result,
+    {
+        let backref_parser = parse!(self, backref);
+
+        if self.out.is_none() {
+            return Ok(());
         }
+
+        let orig_parser = mem::replace(&mut self.parser, Ok(backref_parser));
+        let r = f(self);
+        self.parser = orig_parser;
+        r
+    }
+
+    fn pop_depth(&mut self) {
+        if let Ok(ref mut parser) = self.parser {
+            parser.pop_depth();
+        }
+    }
+
+    /// Output the given value to `self.out` (using `fmt::Display` formatting),
+    /// if printing isn't being skipped.
+    fn print(&mut self, x: impl fmt::Display) -> fmt::Result {
+        if let Some(out) = &mut self.out {
+            fmt::Display::fmt(&x, out)?;
+        }
+        Ok(())
+    }
+
+    /// Output the given `char`s (escaped using `char::escape_debug`), with the
+    /// whole sequence wrapped in quotes, for either a `char` or `&str` literal,
+    /// if printing isn't being skipped.
+    fn print_quoted_escaped_chars(
+        &mut self,
+        quote: char,
+        chars: impl Iterator<Item = char>,
+    ) -> fmt::Result {
+        if let Some(out) = &mut self.out {
+            use core::fmt::Write;
+
+            out.write_char(quote)?;
+            for c in chars {
+                // Special-case not escaping a single/double quote, when
+                // inside the opposite kind of quote.
+                if matches!((quote, c), ('\'', '"') | ('"', '\'')) {
+                    out.write_char(c)?;
+                    continue;
+                }
+
+                for escaped in c.escape_debug() {
+                    out.write_char(escaped)?;
+                }
+            }
+            out.write_char(quote)?;
+        }
+        Ok(())
     }
 
     /// Print the lifetime according to the previously decoded index.
     /// An index of `0` always refers to `'_`, but starting with `1`,
     /// indices refer to late-bound lifetimes introduced by a binder.
     fn print_lifetime_from_index(&mut self, lt: u64) -> fmt::Result {
-        self.out.write_str("'")?;
+        // Bound lifetimes aren't tracked when skipping printing.
+        if self.out.is_none() {
+            return Ok(());
+        }
+
+        self.print("'")?;
         if lt == 0 {
-            return self.out.write_str("_");
+            return self.print("_");
         }
         match (self.bound_lifetime_depth as u64).checked_sub(lt) {
             Some(depth) => {
                 // Try to print lifetimes alphabetically first.
                 if depth < 26 {
                     let c = (b'a' + depth as u8) as char;
-                    c.fmt(self.out)
+                    self.print(c)
                 } else {
                     // Use `'_123` after running out of letters.
-                    self.out.write_str("_")?;
-                    depth.fmt(self.out)
+                    self.print("_")?;
+                    self.print(depth)
                 }
             }
             None => invalid!(self),
@@ -637,20 +752,26 @@
     /// printing e.g. `for<'a, 'b> ` before calling the closure,
     /// and make those lifetimes visible to it (via depth level).
     fn in_binder<F>(&mut self, f: F) -> fmt::Result
-        where F: FnOnce(&mut Self) -> fmt::Result,
+    where
+        F: FnOnce(&mut Self) -> fmt::Result,
     {
         let bound_lifetimes = parse!(self, opt_integer_62(b'G'));
 
+        // Don't track bound lifetimes when skipping printing.
+        if self.out.is_none() {
+            return f(self);
+        }
+
         if bound_lifetimes > 0 {
-            self.out.write_str("for<")?;
+            self.print("for<")?;
             for i in 0..bound_lifetimes {
                 if i > 0 {
-                    self.out.write_str(", ")?;
+                    self.print(", ")?;
                 }
                 self.bound_lifetime_depth += 1;
                 self.print_lifetime_from_index(1)?;
             }
-            self.out.write_str("> ")?;
+            self.print("> ")?;
         }
 
         let r = f(self);
@@ -665,12 +786,13 @@
     /// until the end of the list ('E') is found, or the parser errors.
     /// Returns the number of elements printed.
     fn print_sep_list<F>(&mut self, f: F, sep: &str) -> Result<usize, fmt::Error>
-        where F: Fn(&mut Self) -> fmt::Result,
+    where
+        F: Fn(&mut Self) -> fmt::Result,
     {
         let mut i = 0;
         while self.parser.is_ok() && !self.eat(b'E') {
             if i > 0 {
-                self.out.write_str(sep)?;
+                self.print(sep)?;
             }
             f(self)?;
             i += 1;
@@ -679,17 +801,21 @@
     }
 
     fn print_path(&mut self, in_value: bool) -> fmt::Result {
+        parse!(self, push_depth);
+
         let tag = parse!(self, next);
         match tag {
             b'C' => {
                 let dis = parse!(self, disambiguator);
                 let name = parse!(self, ident);
 
-                name.fmt(self.out)?;
-                if !self.out.alternate() {
-                    self.out.write_str("[")?;
-                    fmt::LowerHex::fmt(&dis, self.out)?;
-                    self.out.write_str("]")?;
+                self.print(name)?;
+                if let Some(out) = &mut self.out {
+                    if !out.alternate() {
+                        out.write_str("[")?;
+                        fmt::LowerHex::fmt(&dis, out)?;
+                        out.write_str("]")?;
+                    }
                 }
             }
             b'N' => {
@@ -697,32 +823,41 @@
 
                 self.print_path(in_value)?;
 
+                // HACK(eddyb) if the parser is already marked as having errored,
+                // `parse!` below will print a `?` without its preceding `::`
+                // (because printing the `::` is skipped in certain conditions,
+                // i.e. a lowercase namespace with an empty identifier),
+                // so in order to get `::?`, the `::` has to be printed here.
+                if self.parser.is_err() {
+                    self.print("::")?;
+                }
+
                 let dis = parse!(self, disambiguator);
                 let name = parse!(self, ident);
 
                 match ns {
                     // Special namespaces, like closures and shims.
                     Some(ns) => {
-                        self.out.write_str("::{")?;
+                        self.print("::{")?;
                         match ns {
-                            'C' => self.out.write_str("closure")?,
-                            'S' => self.out.write_str("shim")?,
-                            _ => ns.fmt(self.out)?,
+                            'C' => self.print("closure")?,
+                            'S' => self.print("shim")?,
+                            _ => self.print(ns)?,
                         }
                         if !name.ascii.is_empty() || !name.punycode.is_empty() {
-                            self.out.write_str(":")?;
-                            name.fmt(self.out)?;
+                            self.print(":")?;
+                            self.print(name)?;
                         }
-                        self.out.write_str("#")?;
-                        dis.fmt(self.out)?;
-                        self.out.write_str("}")?;
+                        self.print("#")?;
+                        self.print(dis)?;
+                        self.print("}")?;
                     }
 
                     // Implementation-specific/unspecified namespaces.
                     None => {
                         if !name.ascii.is_empty() || !name.punycode.is_empty() {
-                            self.out.write_str("::")?;
-                            name.fmt(self.out)?;
+                            self.print("::")?;
+                            self.print(name)?;
                         }
                     }
                 }
@@ -731,31 +866,33 @@
                 if tag != b'Y' {
                     // Ignore the `impl`'s own path.
                     parse!(self, disambiguator);
-                    parse!(self, skip_path);
+                    self.skipping_printing(|this| this.print_path(false));
                 }
 
-                self.out.write_str("<")?;
+                self.print("<")?;
                 self.print_type()?;
                 if tag != b'M' {
-                    self.out.write_str(" as ")?;
+                    self.print(" as ")?;
                     self.print_path(false)?;
                 }
-                self.out.write_str(">")?;
+                self.print(">")?;
             }
             b'I' => {
                 self.print_path(in_value)?;
                 if in_value {
-                    self.out.write_str("::")?;
+                    self.print("::")?;
                 }
-                self.out.write_str("<")?;
+                self.print("<")?;
                 self.print_sep_list(Self::print_generic_arg, ", ")?;
-                self.out.write_str(">")?;
+                self.print(">")?;
             }
             b'B' => {
-                self.backref_printer().print_path(in_value)?;
+                self.print_backref(|this| this.print_path(in_value))?;
             }
             _ => invalid!(self),
         }
+
+        self.pop_depth();
         Ok(())
     }
 
@@ -764,7 +901,7 @@
             let lt = parse!(self, integer_62);
             self.print_lifetime_from_index(lt)
         } else if self.eat(b'K') {
-            self.print_const()
+            self.print_const(false)
         } else {
             self.print_type()
         }
@@ -773,53 +910,54 @@
     fn print_type(&mut self) -> fmt::Result {
         let tag = parse!(self, next);
 
-        match basic_type(tag) {
-            Some(ty) => return self.out.write_str(ty),
-            None => {}
+        if let Some(ty) = basic_type(tag) {
+            return self.print(ty);
         }
 
+        parse!(self, push_depth);
+
         match tag {
             b'R' | b'Q' => {
-                self.out.write_str("&")?;
+                self.print("&")?;
                 if self.eat(b'L') {
                     let lt = parse!(self, integer_62);
                     if lt != 0 {
                         self.print_lifetime_from_index(lt)?;
-                        self.out.write_str(" ")?;
+                        self.print(" ")?;
                     }
                 }
                 if tag != b'R' {
-                    self.out.write_str("mut ")?;
+                    self.print("mut ")?;
                 }
                 self.print_type()?;
             }
 
             b'P' | b'O' => {
-                self.out.write_str("*")?;
+                self.print("*")?;
                 if tag != b'P' {
-                    self.out.write_str("mut ")?;
+                    self.print("mut ")?;
                 } else {
-                    self.out.write_str("const ")?;
+                    self.print("const ")?;
                 }
                 self.print_type()?;
             }
 
             b'A' | b'S' => {
-                self.out.write_str("[")?;
+                self.print("[")?;
                 self.print_type()?;
                 if tag == b'A' {
-                    self.out.write_str("; ")?;
-                    self.print_const()?;
+                    self.print("; ")?;
+                    self.print_const(true)?;
                 }
-                self.out.write_str("]")?;
+                self.print("]")?;
             }
             b'T' => {
-                self.out.write_str("(")?;
+                self.print("(")?;
                 let count = self.print_sep_list(Self::print_type, ", ")?;
                 if count == 1 {
-                    self.out.write_str(",")?;
+                    self.print(",")?;
                 }
-                self.out.write_str(")")?;
+                self.print(")")?;
             }
             b'F' => self.in_binder(|this| {
                 let is_unsafe = this.eat(b'U');
@@ -838,42 +976,39 @@
                 };
 
                 if is_unsafe {
-                    this.out.write_str("unsafe ")?;
+                    this.print("unsafe ")?;
                 }
 
-                match abi {
-                    Some(abi) => {
-                        this.out.write_str("extern \"")?;
+                if let Some(abi) = abi {
+                    this.print("extern \"")?;
 
-                        // If the ABI had any `-`, they were replaced with `_`,
-                        // so the parts between `_` have to be re-joined with `-`.
-                        let mut parts = abi.split('_');
-                        this.out.write_str(parts.next().unwrap())?;
-                        for part in parts {
-                            this.out.write_str("-")?;
-                            this.out.write_str(part)?;
-                        }
-
-                        this.out.write_str("\" ")?;
+                    // If the ABI had any `-`, they were replaced with `_`,
+                    // so the parts between `_` have to be re-joined with `-`.
+                    let mut parts = abi.split('_');
+                    this.print(parts.next().unwrap())?;
+                    for part in parts {
+                        this.print("-")?;
+                        this.print(part)?;
                     }
-                    None => {}
+
+                    this.print("\" ")?;
                 }
 
-                this.out.write_str("fn(")?;
+                this.print("fn(")?;
                 this.print_sep_list(Self::print_type, ", ")?;
-                this.out.write_str(")")?;
+                this.print(")")?;
 
                 if this.eat(b'u') {
                     // Skip printing the return type if it's 'u', i.e. `()`.
                 } else {
-                    this.out.write_str(" -> ")?;
+                    this.print(" -> ")?;
                     this.print_type()?;
                 }
 
                 Ok(())
             })?,
             b'D' => {
-                self.out.write_str("dyn ")?;
+                self.print("dyn ")?;
                 self.in_binder(|this| {
                     this.print_sep_list(Self::print_dyn_trait, " + ")?;
                     Ok(())
@@ -884,19 +1019,21 @@
                 }
                 let lt = parse!(self, integer_62);
                 if lt != 0 {
-                    self.out.write_str(" + ")?;
+                    self.print(" + ")?;
                     self.print_lifetime_from_index(lt)?;
                 }
             }
             b'B' => {
-                self.backref_printer().print_type()?;
+                self.print_backref(Self::print_type)?;
             }
             _ => {
                 // Go back to the tag, so `print_path` also sees it.
-                let _ = self.parser_mut().map(|p| p.next -= 1);
+                let _ = self.parser.as_mut().map(|p| p.next -= 1);
                 self.print_path(false)?;
             }
         }
+
+        self.pop_depth();
         Ok(())
     }
 
@@ -907,10 +1044,17 @@
     /// open, by omitting the `>`, and return `Ok(true)` in that case.
     fn print_path_maybe_open_generics(&mut self) -> Result<bool, fmt::Error> {
         if self.eat(b'B') {
-            self.backref_printer().print_path_maybe_open_generics()
+            // NOTE(eddyb) the closure may not run if printing is being skipped,
+            // but in that case the returned boolean doesn't matter.
+            let mut open = false;
+            self.print_backref(|this| {
+                open = this.print_path_maybe_open_generics()?;
+                Ok(())
+            })?;
+            Ok(open)
         } else if self.eat(b'I') {
             self.print_path(false)?;
-            self.out.write_str("<")?;
+            self.print("<")?;
             self.print_sep_list(Self::print_generic_arg, ", ")?;
             Ok(true)
         } else {
@@ -924,68 +1068,181 @@
 
         while self.eat(b'p') {
             if !open {
-                self.out.write_str("<")?;
+                self.print("<")?;
                 open = true;
             } else {
-                self.out.write_str(", ")?;
+                self.print(", ")?;
             }
 
             let name = parse!(self, ident);
-            name.fmt(self.out)?;
-            self.out.write_str(" = ")?;
+            self.print(name)?;
+            self.print(" = ")?;
             self.print_type()?;
         }
 
         if open {
-            self.out.write_str(">")?;
+            self.print(">")?;
         }
 
         Ok(())
     }
 
-    fn print_const(&mut self) -> fmt::Result {
-        if self.eat(b'B') {
-            return self.backref_printer().print_const();
-        }
+    fn print_const(&mut self, in_value: bool) -> fmt::Result {
+        let tag = parse!(self, next);
 
-        let ty_tag = parse!(self, next);
-        let ty = match ty_tag {
-            // Unsigned integer types.
-            b'h' | b't' | b'm' | b'y' | b'o' | b'j' => {
-                basic_type(ty_tag).unwrap()
+        parse!(self, push_depth);
+
+        // Only literals (and the names of `const` generic parameters, but they
+        // don't get mangled at all), can appear in generic argument position
+        // without any disambiguation, all other expressions require braces.
+        // To avoid duplicating the mapping between `tag` and what syntax gets
+        // used (especially any special-casing), every case that needs braces
+        // has to call `open_brace(self)?` (and the closing brace is automatic).
+        let mut opened_brace = false;
+        let mut open_brace_if_outside_expr = |this: &mut Self| {
+            // If this expression is nested in another, braces aren't required.
+            if in_value {
+                return Ok(());
             }
 
-            _ => invalid!(self),
+            opened_brace = true;
+            this.print("{")
         };
 
+        match tag {
+            b'p' => self.print("_")?,
 
-        if self.eat(b'p') {
-            self.out.write_str("_")?;
-        } else {
-            self.print_const_uint()?;
+            // Primitive leaves with hex-encoded values (see `basic_type`).
+            b'h' | b't' | b'm' | b'y' | b'o' | b'j' => self.print_const_uint(tag)?,
+            b'a' | b's' | b'l' | b'x' | b'n' | b'i' => {
+                if self.eat(b'n') {
+                    self.print("-")?;
+                }
+
+                self.print_const_uint(tag)?;
+            }
+            b'b' => match parse!(self, hex_nibbles).try_parse_uint() {
+                Some(0) => self.print("false")?,
+                Some(1) => self.print("true")?,
+                _ => invalid!(self),
+            },
+            b'c' => {
+                let valid_char = parse!(self, hex_nibbles)
+                    .try_parse_uint()
+                    .and_then(|v| u32::try_from(v).ok())
+                    .and_then(char::from_u32);
+                match valid_char {
+                    Some(c) => self.print_quoted_escaped_chars('\'', iter::once(c))?,
+                    None => invalid!(self),
+                }
+            }
+            b'e' => {
+                // NOTE(eddyb) a string literal `"..."` has type `&str`, so
+                // to get back the type `str`, `*"..."` syntax is needed
+                // (even if that may not be valid in Rust itself).
+                open_brace_if_outside_expr(self)?;
+                self.print("*")?;
+
+                self.print_const_str_literal()?;
+            }
+
+            b'R' | b'Q' => {
+                // NOTE(eddyb) this prints `"..."` instead of `&*"..."`, which
+                // is what `Re..._` would imply (see comment for `str` above).
+                if tag == b'R' && self.eat(b'e') {
+                    self.print_const_str_literal()?;
+                } else {
+                    open_brace_if_outside_expr(self)?;
+                    self.print("&")?;
+                    if tag != b'R' {
+                        self.print("mut ")?;
+                    }
+                    self.print_const(true)?;
+                }
+            }
+            b'A' => {
+                open_brace_if_outside_expr(self)?;
+                self.print("[")?;
+                self.print_sep_list(|this| this.print_const(true), ", ")?;
+                self.print("]")?;
+            }
+            b'T' => {
+                open_brace_if_outside_expr(self)?;
+                self.print("(")?;
+                let count = self.print_sep_list(|this| this.print_const(true), ", ")?;
+                if count == 1 {
+                    self.print(",")?;
+                }
+                self.print(")")?;
+            }
+            b'V' => {
+                open_brace_if_outside_expr(self)?;
+                self.print_path(true)?;
+                match parse!(self, next) {
+                    b'U' => {}
+                    b'T' => {
+                        self.print("(")?;
+                        self.print_sep_list(|this| this.print_const(true), ", ")?;
+                        self.print(")")?;
+                    }
+                    b'S' => {
+                        self.print(" { ")?;
+                        self.print_sep_list(
+                            |this| {
+                                parse!(this, disambiguator);
+                                let name = parse!(this, ident);
+                                this.print(name)?;
+                                this.print(": ")?;
+                                this.print_const(true)
+                            },
+                            ", ",
+                        )?;
+                        self.print(" }")?;
+                    }
+                    _ => invalid!(self),
+                }
+            }
+            b'B' => {
+                self.print_backref(|this| this.print_const(in_value))?;
+            }
+            _ => invalid!(self),
         }
 
-        if !self.out.alternate() {
-            self.out.write_str(": ")?;
-            self.out.write_str(ty)?;
+        if opened_brace {
+            self.print("}")?;
+        }
+
+        self.pop_depth();
+        Ok(())
+    }
+
+    fn print_const_uint(&mut self, ty_tag: u8) -> fmt::Result {
+        let hex = parse!(self, hex_nibbles);
+
+        match hex.try_parse_uint() {
+            Some(v) => self.print(v)?,
+
+            // Print anything that doesn't fit in `u64` verbatim.
+            None => {
+                self.print("0x")?;
+                self.print(hex.nibbles)?;
+            }
+        }
+
+        if let Some(out) = &mut self.out {
+            if !out.alternate() {
+                let ty = basic_type(ty_tag).unwrap();
+                self.print(ty)?;
+            }
         }
 
         Ok(())
     }
 
-    fn print_const_uint(&mut self) -> fmt::Result {
-        let hex = parse!(self, hex_nibbles);
-
-        // Print anything that doesn't fit in `u64` verbatim.
-        if hex.len() > 16 {
-            self.out.write_str("0x")?;
-            return self.out.write_str(hex);
+    fn print_const_str_literal(&mut self) -> fmt::Result {
+        match parse!(self, hex_nibbles).try_parse_str_chars() {
+            Some(chars) => self.print_quoted_escaped_chars('"', chars),
+            None => invalid!(self),
         }
-
-        let mut v = 0;
-        for c in hex.chars() {
-            v = (v << 4) | (c.to_digit(16).unwrap() as u64);
-        }
-        v.fmt(self.out)
     }
 }
diff --git a/sgx_edl/Cargo.toml b/sgx_edl/Cargo.toml
index 11ca3d3..b46a6db 100644
--- a/sgx_edl/Cargo.toml
+++ b/sgx_edl/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_edl"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_edl/edl/sgx_file.edl b/sgx_edl/edl/sgx_file.edl
index 1e28036..07fb1eb 100644
--- a/sgx_edl/edl/sgx_file.edl
+++ b/sgx_edl/edl/sgx_file.edl
@@ -46,6 +46,7 @@
         int u_fchmod_ocall([out] int *error, int fd, uint32_t mode);
         int u_unlink_ocall([out] int *error, [in, string] const char *pathname);
         int u_link_ocall([out] int *error, [in, string] const char *oldpath, [in, string] const char *newpath);
+        int u_linkat_ocall([out] int *error, int olddirfd, [in, string] const char *oldpath, int newdirfd, [in, string] const char *newpath, int flags);
         int u_rename_ocall([out] int *error, [in, string] const char *oldpath, [in, string] const char *newpath);
         int u_chmod_ocall([out] int *error, [in, string] const char *path, uint32_t mode);
         size_t u_readlink_ocall([out] int *error, [in, string] const char *path, [out, size=bufsz] char *buf, size_t bufsz);
diff --git a/sgx_edl/edl/sgx_socket.edl b/sgx_edl/edl/sgx_socket.edl
index 68ce3b6..6fc8ff7 100644
--- a/sgx_edl/edl/sgx_socket.edl
+++ b/sgx_edl/edl/sgx_socket.edl
@@ -54,7 +54,18 @@
                                 [out, size=addrlen_in] struct sockaddr *src_addr,
                                 socklen_t addrlen_in,
                                 [out] socklen_t *addrlen_out);
-        size_t u_recvmsg_ocall([out] int *error, int sockfd, [in, out] struct msghdr *msg, int flags);
+        size_t u_recvmsg_ocall([out] int *error,
+                               int sockfd,
+                               [out, size=msg_namelen] void *msg_name,
+                               socklen_t msg_namelen,
+                               [out] socklen_t* msg_namelen_out,
+                               [in, count=msg_iovlen] struct iovec* msg_iov,
+                               size_t msg_iovlen,
+                               [out, size=msg_controllen] void *msg_control,
+                               size_t msg_controllen,
+                               [out] size_t* msg_controllen_out,
+                               [out] int* msg_flags,
+                               int flags);
         size_t u_send_ocall([out] int *error, int sockfd, [user_check] const void *buf, size_t len, int flags);
         size_t u_sendto_ocall([out] int *error,
                               int sockfd,
@@ -63,7 +74,15 @@
                               int flags,
                               [in, size=addrlen] const struct sockaddr *dest_addr,
                               socklen_t addrlen);
-        size_t u_sendmsg_ocall([out] int *error, int sockfd, [in] const struct msghdr *msg, int flags);
+        size_t u_sendmsg_ocall([out] int *error,
+                               int sockfd,
+                               [in, size=msg_namelen] const void* msg_name,
+                               socklen_t msg_namelen,
+                               [in, count=msg_iovlen] const struct iovec* msg_iov,
+                               size_t msg_iovlen,
+                               [in, size=msg_controllen] const void* msg_control,
+                               size_t msg_controllen,
+                               int flags);
         int u_getsockopt_ocall([out] int *error,
                                int sockfd,
                                int level,
diff --git a/sgx_edl/edl/sgx_sys.edl b/sgx_edl/edl/sgx_sys.edl
index f5f3d2c..bc74b96 100644
--- a/sgx_edl/edl/sgx_sys.edl
+++ b/sgx_edl/edl/sgx_sys.edl
@@ -26,7 +26,7 @@
     untrusted {
         long u_sysconf_ocall([out] int *error, int name);
         int u_prctl_ocall([out] int *error, int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
-        int u_sched_setaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [in, size=cpusetsize]cpu_set_t *mask);
-        int u_sched_getaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [out, size=cpusetsize]cpu_set_t *mask);
+        int u_sched_setaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [in, size=cpusetsize] cpu_set_t *mask);
+        int u_sched_getaffinity_ocall([out] int *error, pid_t pid, size_t cpusetsize, [out, size=cpusetsize] cpu_set_t *mask);
     };
 };
diff --git a/sgx_libc/Cargo.toml b/sgx_libc/Cargo.toml
index 36d6a82..7441fb5 100644
--- a/sgx_libc/Cargo.toml
+++ b/sgx_libc/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_libc"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_libc/src/lib.rs b/sgx_libc/src/lib.rs
index d669e18..d60cd39 100644
--- a/sgx_libc/src/lib.rs
+++ b/sgx_libc/src/lib.rs
@@ -16,9 +16,7 @@
 // under the License..
 
 #![no_std]
-
 #![cfg_attr(target_env = "sgx", feature(rustc_private))]
-
 #![allow(non_camel_case_types)]
 #![allow(non_upper_case_globals)]
 #![allow(overflowing_literals)]
diff --git a/sgx_libc/src/linux/x86_64/mod.rs b/sgx_libc/src/linux/x86_64/mod.rs
index bdd0dcb..314e805 100644
--- a/sgx_libc/src/linux/x86_64/mod.rs
+++ b/sgx_libc/src/linux/x86_64/mod.rs
@@ -21,15 +21,19 @@
 //! It is a c-style interface and self-explained. Currently we don't have much
 //! time for documenting it.
 
-pub use sgx_types::{int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t};
-pub use sgx_types::{c_void, c_schar, c_char, c_uchar, c_short, c_ushort, c_int, c_uint, c_float,
-                    c_double, c_longlong, c_ulonglong, intmax_t, uintmax_t, c_ulong, c_long};
-pub use sgx_types::{size_t, ptrdiff_t, intptr_t, uintptr_t, ssize_t};
 pub use sgx_types::time_t;
+pub use sgx_types::{
+    c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
+    c_ulong, c_ulonglong, c_ushort, c_void, intmax_t, uintmax_t,
+};
+pub use sgx_types::{int16_t, int32_t, int64_t, int8_t, uint16_t, uint32_t, uint64_t, uint8_t};
+pub use sgx_types::{intptr_t, ptrdiff_t, size_t, ssize_t, uintptr_t};
 
-use sgx_types::{sgx_thread_mutex_t, sgx_thread_mutex_attr_t, sgx_thread_cond_t, sgx_thread_cond_attr_t};
-use core::ptr;
 use core::mem;
+use core::ptr;
+use sgx_types::{
+    sgx_thread_cond_attr_t, sgx_thread_cond_t, sgx_thread_mutex_attr_t, sgx_thread_mutex_t,
+};
 
 extern "C" {
     pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
@@ -37,39 +41,45 @@
     pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
     pub fn free(p: *mut c_void);
     pub fn memalign(align: size_t, size: size_t) -> *mut c_void;
+    pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
+    pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
+    pub fn consttime_memequal(b1: *const c_void, b2: *const c_void, len: size_t) -> c_int;
 }
 
 #[link(name = "sgx_pthread")]
 extern "C" {
-    pub fn pthread_create(native: *mut pthread_t,
-                          attr: *const pthread_attr_t,
-                          f: extern "C" fn(*mut c_void) -> *mut c_void,
-                          value: *mut c_void) -> c_int;
-    pub fn pthread_join(native: pthread_t,
-                        value: *mut *mut c_void) -> c_int;
+    pub fn pthread_create(
+        native: *mut pthread_t,
+        attr: *const pthread_attr_t,
+        f: extern "C" fn(*mut c_void) -> *mut c_void,
+        value: *mut c_void,
+    ) -> c_int;
+    pub fn pthread_join(native: pthread_t, value: *mut *mut c_void) -> c_int;
     pub fn pthread_exit(value: *mut c_void);
     pub fn pthread_self() -> pthread_t;
     pub fn pthread_equal(t1: pthread_t, t2: pthread_t) -> c_int;
 
     pub fn pthread_once(once_control: *mut pthread_once_t, init_routine: extern "C" fn()) -> c_int;
 
-    pub fn pthread_key_create(key: *mut pthread_key_t,
-                              dtor: Option<unsafe extern "C" fn(*mut c_void)>) -> c_int;
+    pub fn pthread_key_create(
+        key: *mut pthread_key_t,
+        dtor: Option<unsafe extern "C" fn(*mut c_void)>,
+    ) -> c_int;
     pub fn pthread_key_delete(key: pthread_key_t) -> c_int;
     pub fn pthread_getspecific(key: pthread_key_t) -> *mut c_void;
     pub fn pthread_setspecific(key: pthread_key_t, value: *const c_void) -> c_int;
-    
-    pub fn pthread_mutex_init(lock: *mut pthread_mutex_t,
-                              attr: *const pthread_mutexattr_t) -> c_int;
+
+    pub fn pthread_mutex_init(
+        lock: *mut pthread_mutex_t,
+        attr: *const pthread_mutexattr_t,
+    ) -> c_int;
     pub fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> c_int;
     pub fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> c_int;
     pub fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> c_int;
     pub fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> c_int;
 
-    pub fn pthread_cond_init(cond: *mut pthread_cond_t,
-                             attr: *const pthread_condattr_t) -> c_int;
-    pub fn pthread_cond_wait(cond: *mut pthread_cond_t,
-                             lock: *mut pthread_mutex_t) -> c_int;
+    pub fn pthread_cond_init(cond: *mut pthread_cond_t, attr: *const pthread_condattr_t) -> c_int;
+    pub fn pthread_cond_wait(cond: *mut pthread_cond_t, lock: *mut pthread_mutex_t) -> c_int;
     pub fn pthread_cond_signal(cond: *mut pthread_cond_t) -> c_int;
     pub fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> c_int;
     pub fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> c_int;
@@ -100,16 +110,12 @@
 
 /// Get the last error number.
 pub fn errno() -> i32 {
-    unsafe {
-        (*errno_location()) as i32
-    }
+    unsafe { (*errno_location()) as i32 }
 }
 
 /// Set the last error number.
 pub fn set_errno(e: i32) {
-    unsafe {
-        *errno_location() = e as c_int
-    }
+    unsafe { *errno_location() = e as c_int }
 }
 
 /// Gets a detailed string description for the given error number.
@@ -132,7 +138,9 @@
 }
 
 pub unsafe fn memrchr(s: *const u8, c: u8, n: usize) -> *const u8 {
-    if n == 0 { return ptr::null(); }
+    if n == 0 {
+        return ptr::null();
+    }
     let mut ret = ptr::null();
     let mut p: *const u8 = (s as usize + (n - 1)) as *const u8;
     for _ in 0..n {
@@ -140,7 +148,7 @@
             ret = p;
             break;
         }
-         p = p.offset(-1);
+        p = p.offset(-1);
     }
     ret
 }
@@ -171,6 +179,7 @@
 pub type ino_t = u64;
 pub type nlink_t = u64;
 pub type off_t = i64;
+pub type loff_t = i64;
 pub type uid_t = u32;
 pub type gid_t = u32;
 pub type ino64_t = u64;
@@ -193,8 +202,8 @@
 pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = ptr::null_mut();
 pub const PTHREAD_ONCE_INIT: pthread_once_t = pthread_once_t {
     state: PTHREAD_NEEDS_INIT,
-    mutex: PTHREAD_MUTEX_INITIALIZER
-    };
+    mutex: PTHREAD_MUTEX_INITIALIZER,
+};
 
 #[derive(Copy, Clone, Debug)]
 pub enum DIR {}
@@ -423,11 +432,11 @@
     }
 
     pub struct cmsghdr {
-        pub cmsg_len: socklen_t,
-        pub __pad1: c_int,
+        pub cmsg_len: size_t,
         pub cmsg_level: c_int,
         pub cmsg_type: c_int,
     }
+
     pub struct dirent {
         pub d_ino: ino_t,
         pub d_off: off_t,
@@ -493,6 +502,11 @@
     }
 }
 
+pub const SPLICE_F_MOVE: c_uint = 0x01;
+pub const SPLICE_F_NONBLOCK: c_uint = 0x02;
+pub const SPLICE_F_MORE: c_uint = 0x04;
+pub const SPLICE_F_GIFT: c_uint = 0x08;
+
 pub const AT_FDCWD: c_int = -100;
 pub const AT_SYMLINK_NOFOLLOW: c_int = 0x100;
 pub const AT_REMOVEDIR: c_int = 0x200;
@@ -606,6 +620,9 @@
 pub const PROT_WRITE: c_int = 2;
 pub const PROT_EXEC: c_int = 4;
 
+pub const SCM_RIGHTS: c_int = 0x01;
+pub const SCM_CREDENTIALS: c_int = 0x02;
+
 pub const SOL_SOCKET: c_int = 1;
 
 pub const SO_REUSEADDR: c_int = 2;
@@ -759,7 +776,6 @@
 pub const FILENAME_MAX: c_uint = 4096;
 pub const FOPEN_MAX: c_uint = 16;
 
-
 pub const IFF_UP: c_int = 0x1;
 pub const IFF_BROADCAST: c_int = 0x2;
 pub const IFF_DEBUG: c_int = 0x4;
@@ -949,6 +965,8 @@
 
 pub const PATH_MAX: c_int = 4096;
 
+pub const UIO_MAXIOV: c_int = 1024;
+
 pub const FD_SETSIZE: usize = 1024;
 pub const FD_CLOEXEC: c_int = 0x1;
 
@@ -1255,141 +1273,141 @@
 
 pub const CPU_SETSIZE: c_int = 0x400;
 
-pub const EPERM: int32_t              = 1;
-pub const ENOENT: int32_t             = 2;
-pub const ESRCH: int32_t              = 3;
-pub const EINTR: int32_t              = 4;
-pub const EIO: int32_t                = 5;
-pub const ENXIO: int32_t              = 6;
-pub const E2BIG: int32_t              = 7;
-pub const ENOEXEC: int32_t            = 8;
-pub const EBADF: int32_t              = 9;
-pub const ECHILD: int32_t             = 10;
-pub const EAGAIN: int32_t             = 11;
-pub const ENOMEM: int32_t             = 12;
-pub const EACCES: int32_t             = 13;
-pub const EFAULT: int32_t             = 14;
-pub const ENOTBLK: int32_t            = 15;
-pub const EBUSY: int32_t              = 16;
-pub const EEXIST: int32_t             = 17;
-pub const EXDEV: int32_t              = 18;
-pub const ENODEV: int32_t             = 19;
-pub const ENOTDIR: int32_t            = 20;
-pub const EISDIR: int32_t             = 21;
-pub const EINVAL: int32_t             = 22;
-pub const ENFILE: int32_t             = 23;
-pub const EMFILE: int32_t             = 24;
-pub const ENOTTY: int32_t             = 25;
-pub const ETXTBSY: int32_t            = 26;
-pub const EFBIG: int32_t              = 27;
-pub const ENOSPC: int32_t             = 28;
-pub const ESPIPE: int32_t             = 29;
-pub const EROFS: int32_t              = 30;
-pub const EMLINK: int32_t             = 31;
-pub const EPIPE: int32_t              = 32;
-pub const EDOM: int32_t               = 33;
-pub const ERANGE: int32_t             = 34;
-pub const EDEADLK: int32_t            = 35;
-pub const ENAMETOOLONG: int32_t       = 36;
-pub const ENOLCK: int32_t             = 37;
-pub const ENOSYS: int32_t             = 38;
-pub const ENOTEMPTY: int32_t          = 39;
-pub const ELOOP: int32_t              = 40;
-pub const EWOULDBLOCK: int32_t        = EAGAIN;
-pub const ENOMSG: int32_t             = 42;
-pub const EIDRM: int32_t              = 43;
-pub const ECHRNG: int32_t             = 44;
-pub const EL2NSYNC: int32_t           = 45;
-pub const EL3HLT: int32_t             = 46;
-pub const EL3RST: int32_t             = 47;
-pub const ELNRNG: int32_t             = 48;
-pub const EUNATCH: int32_t            = 49;
-pub const ENOCSI: int32_t             = 50;
-pub const EL2HLT: int32_t             = 51;
-pub const EBADE: int32_t              = 52;
-pub const EBADR: int32_t              = 53;
-pub const EXFULL: int32_t             = 54;
-pub const ENOANO: int32_t             = 55;
-pub const EBADRQC: int32_t            = 56;
-pub const EBADSLT: int32_t            = 57;
-pub const EDEADLOCK: int32_t          = EDEADLK;
-pub const EBFONT: int32_t             = 59;
-pub const ENOSTR: int32_t             = 60;
-pub const ENODATA: int32_t            = 61;
-pub const ETIME: int32_t              = 62;
-pub const ENOSR: int32_t              = 63;
-pub const ENONET: int32_t             = 64;
-pub const ENOPKG: int32_t             = 65;
-pub const EREMOTE: int32_t            = 66;
-pub const ENOLINK: int32_t            = 67;
-pub const EADV: int32_t               = 68;
-pub const ESRMNT: int32_t             = 69;
-pub const ECOMM: int32_t              = 70;
-pub const EPROTO: int32_t             = 71;
-pub const EMULTIHOP: int32_t          = 72;
-pub const EDOTDOT: int32_t            = 73;
-pub const EBADMSG: int32_t            = 74;
-pub const EOVERFLOW: int32_t          = 75;
-pub const ENOTUNIQ: int32_t           = 76;
-pub const EBADFD: int32_t             = 77;
-pub const EREMCHG: int32_t            = 78;
-pub const ELIBACC: int32_t            = 79;
-pub const ELIBBAD: int32_t            = 80;
-pub const ELIBSCN: int32_t            = 81;
-pub const ELIBMAX: int32_t            = 82;
-pub const ELIBEXEC: int32_t           = 83;
-pub const EILSEQ: int32_t             = 84;
-pub const ERESTART: int32_t           = 85;
-pub const ESTRPIPE: int32_t           = 86;
-pub const EUSERS: int32_t             = 87;
-pub const ENOTSOCK: int32_t           = 88;
-pub const EDESTADDRREQ: int32_t       = 89;
-pub const EMSGSIZE: int32_t           = 90;
-pub const EPROTOTYPE: int32_t         = 91;
-pub const ENOPROTOOPT: int32_t        = 92;
-pub const EPROTONOSUPPORT: int32_t    = 93;
-pub const ESOCKTNOSUPPORT: int32_t    = 94;
-pub const EOPNOTSUPP: int32_t         = 95;
-pub const EPFNOSUPPORT: int32_t       = 96;
-pub const EAFNOSUPPORT: int32_t       = 97;
-pub const EADDRINUSE: int32_t         = 98;
-pub const EADDRNOTAVAIL: int32_t      = 99;
-pub const ENETDOWN: int32_t           = 100;
-pub const ENETUNREACH: int32_t        = 101;
-pub const ENETRESET: int32_t          = 102;
-pub const ECONNABORTED: int32_t       = 103;
-pub const ECONNRESET: int32_t         = 104;
-pub const ENOBUFS: int32_t            = 105;
-pub const EISCONN: int32_t            = 106;
-pub const ENOTCONN: int32_t           = 107;
-pub const ESHUTDOWN: int32_t          = 108;
-pub const ETOOMANYREFS: int32_t       = 109;
-pub const ETIMEDOUT: int32_t          = 110;
-pub const ECONNREFUSED: int32_t       = 111;
-pub const EHOSTDOWN: int32_t          = 112;
-pub const EHOSTUNREACH: int32_t       = 113;
-pub const EALREADY: int32_t           = 114;
-pub const EINPROGRESS: int32_t        = 115;
-pub const ESTALE: int32_t             = 116;
-pub const EUCLEAN: int32_t            = 117;
-pub const ENOTNAM: int32_t            = 118;
-pub const ENAVAIL: int32_t            = 119;
-pub const EISNAM: int32_t             = 120;
-pub const EREMOTEIO: int32_t          = 121;
-pub const EDQUOT: int32_t             = 122;
-pub const ENOMEDIUM: int32_t          = 123;
-pub const EMEDIUMTYPE: int32_t        = 124;
-pub const ECANCELED: int32_t          = 125;
-pub const ENOKEY: int32_t             = 126;
-pub const EKEYEXPIRED: int32_t        = 127;
-pub const EKEYREVOKED: int32_t        = 128;
-pub const EKEYREJECTED: int32_t       = 129;
-pub const EOWNERDEAD: int32_t         = 130;
-pub const ENOTRECOVERABLE: int32_t    = 131;
-pub const ERFKILL: int32_t            = 132;
-pub const EHWPOISON: int32_t          = 133;
-pub const ENOTSUP: int32_t            = EOPNOTSUPP;
-pub const ESGX: int32_t               = 0x0000_FFFF;
+pub const EPERM: int32_t = 1;
+pub const ENOENT: int32_t = 2;
+pub const ESRCH: int32_t = 3;
+pub const EINTR: int32_t = 4;
+pub const EIO: int32_t = 5;
+pub const ENXIO: int32_t = 6;
+pub const E2BIG: int32_t = 7;
+pub const ENOEXEC: int32_t = 8;
+pub const EBADF: int32_t = 9;
+pub const ECHILD: int32_t = 10;
+pub const EAGAIN: int32_t = 11;
+pub const ENOMEM: int32_t = 12;
+pub const EACCES: int32_t = 13;
+pub const EFAULT: int32_t = 14;
+pub const ENOTBLK: int32_t = 15;
+pub const EBUSY: int32_t = 16;
+pub const EEXIST: int32_t = 17;
+pub const EXDEV: int32_t = 18;
+pub const ENODEV: int32_t = 19;
+pub const ENOTDIR: int32_t = 20;
+pub const EISDIR: int32_t = 21;
+pub const EINVAL: int32_t = 22;
+pub const ENFILE: int32_t = 23;
+pub const EMFILE: int32_t = 24;
+pub const ENOTTY: int32_t = 25;
+pub const ETXTBSY: int32_t = 26;
+pub const EFBIG: int32_t = 27;
+pub const ENOSPC: int32_t = 28;
+pub const ESPIPE: int32_t = 29;
+pub const EROFS: int32_t = 30;
+pub const EMLINK: int32_t = 31;
+pub const EPIPE: int32_t = 32;
+pub const EDOM: int32_t = 33;
+pub const ERANGE: int32_t = 34;
+pub const EDEADLK: int32_t = 35;
+pub const ENAMETOOLONG: int32_t = 36;
+pub const ENOLCK: int32_t = 37;
+pub const ENOSYS: int32_t = 38;
+pub const ENOTEMPTY: int32_t = 39;
+pub const ELOOP: int32_t = 40;
+pub const EWOULDBLOCK: int32_t = EAGAIN;
+pub const ENOMSG: int32_t = 42;
+pub const EIDRM: int32_t = 43;
+pub const ECHRNG: int32_t = 44;
+pub const EL2NSYNC: int32_t = 45;
+pub const EL3HLT: int32_t = 46;
+pub const EL3RST: int32_t = 47;
+pub const ELNRNG: int32_t = 48;
+pub const EUNATCH: int32_t = 49;
+pub const ENOCSI: int32_t = 50;
+pub const EL2HLT: int32_t = 51;
+pub const EBADE: int32_t = 52;
+pub const EBADR: int32_t = 53;
+pub const EXFULL: int32_t = 54;
+pub const ENOANO: int32_t = 55;
+pub const EBADRQC: int32_t = 56;
+pub const EBADSLT: int32_t = 57;
+pub const EDEADLOCK: int32_t = EDEADLK;
+pub const EBFONT: int32_t = 59;
+pub const ENOSTR: int32_t = 60;
+pub const ENODATA: int32_t = 61;
+pub const ETIME: int32_t = 62;
+pub const ENOSR: int32_t = 63;
+pub const ENONET: int32_t = 64;
+pub const ENOPKG: int32_t = 65;
+pub const EREMOTE: int32_t = 66;
+pub const ENOLINK: int32_t = 67;
+pub const EADV: int32_t = 68;
+pub const ESRMNT: int32_t = 69;
+pub const ECOMM: int32_t = 70;
+pub const EPROTO: int32_t = 71;
+pub const EMULTIHOP: int32_t = 72;
+pub const EDOTDOT: int32_t = 73;
+pub const EBADMSG: int32_t = 74;
+pub const EOVERFLOW: int32_t = 75;
+pub const ENOTUNIQ: int32_t = 76;
+pub const EBADFD: int32_t = 77;
+pub const EREMCHG: int32_t = 78;
+pub const ELIBACC: int32_t = 79;
+pub const ELIBBAD: int32_t = 80;
+pub const ELIBSCN: int32_t = 81;
+pub const ELIBMAX: int32_t = 82;
+pub const ELIBEXEC: int32_t = 83;
+pub const EILSEQ: int32_t = 84;
+pub const ERESTART: int32_t = 85;
+pub const ESTRPIPE: int32_t = 86;
+pub const EUSERS: int32_t = 87;
+pub const ENOTSOCK: int32_t = 88;
+pub const EDESTADDRREQ: int32_t = 89;
+pub const EMSGSIZE: int32_t = 90;
+pub const EPROTOTYPE: int32_t = 91;
+pub const ENOPROTOOPT: int32_t = 92;
+pub const EPROTONOSUPPORT: int32_t = 93;
+pub const ESOCKTNOSUPPORT: int32_t = 94;
+pub const EOPNOTSUPP: int32_t = 95;
+pub const EPFNOSUPPORT: int32_t = 96;
+pub const EAFNOSUPPORT: int32_t = 97;
+pub const EADDRINUSE: int32_t = 98;
+pub const EADDRNOTAVAIL: int32_t = 99;
+pub const ENETDOWN: int32_t = 100;
+pub const ENETUNREACH: int32_t = 101;
+pub const ENETRESET: int32_t = 102;
+pub const ECONNABORTED: int32_t = 103;
+pub const ECONNRESET: int32_t = 104;
+pub const ENOBUFS: int32_t = 105;
+pub const EISCONN: int32_t = 106;
+pub const ENOTCONN: int32_t = 107;
+pub const ESHUTDOWN: int32_t = 108;
+pub const ETOOMANYREFS: int32_t = 109;
+pub const ETIMEDOUT: int32_t = 110;
+pub const ECONNREFUSED: int32_t = 111;
+pub const EHOSTDOWN: int32_t = 112;
+pub const EHOSTUNREACH: int32_t = 113;
+pub const EALREADY: int32_t = 114;
+pub const EINPROGRESS: int32_t = 115;
+pub const ESTALE: int32_t = 116;
+pub const EUCLEAN: int32_t = 117;
+pub const ENOTNAM: int32_t = 118;
+pub const ENAVAIL: int32_t = 119;
+pub const EISNAM: int32_t = 120;
+pub const EREMOTEIO: int32_t = 121;
+pub const EDQUOT: int32_t = 122;
+pub const ENOMEDIUM: int32_t = 123;
+pub const EMEDIUMTYPE: int32_t = 124;
+pub const ECANCELED: int32_t = 125;
+pub const ENOKEY: int32_t = 126;
+pub const EKEYEXPIRED: int32_t = 127;
+pub const EKEYREVOKED: int32_t = 128;
+pub const EKEYREJECTED: int32_t = 129;
+pub const EOWNERDEAD: int32_t = 130;
+pub const ENOTRECOVERABLE: int32_t = 131;
+pub const ERFKILL: int32_t = 132;
+pub const EHWPOISON: int32_t = 133;
+pub const ENOTSUP: int32_t = EOPNOTSUPP;
+pub const ESGX: int32_t = 0x0000_FFFF;
 
 pub const SA_NODEFER: c_int = 0x40000000;
 pub const SA_RESETHAND: c_int = 0x80000000;
@@ -1434,58 +1452,59 @@
 pub const NSIG: c_int = SIGRTMAX + 1;
 
 #[inline]
-pub unsafe fn FD_CLR(fd: c_int, set: *mut fd_set) -> () {
+pub unsafe fn FD_CLR(fd: c_int, set: *mut fd_set) {
     let fd = fd as usize;
     let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
     (*set).fds_bits[fd / size] &= !(1 << (fd % size));
-    return
 }
 
 #[inline]
 pub unsafe fn FD_ISSET(fd: c_int, set: *mut fd_set) -> bool {
     let fd = fd as usize;
     let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
-    return ((*set).fds_bits[fd / size] & (1 << (fd % size))) != 0
+    ((*set).fds_bits[fd / size] & (1 << (fd % size))) != 0
 }
 
 #[inline]
-pub unsafe fn FD_SET(fd: c_int, set: *mut fd_set) -> () {
+pub unsafe fn FD_SET(fd: c_int, set: *mut fd_set) {
     let fd = fd as usize;
     let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;
     (*set).fds_bits[fd / size] |= 1 << (fd % size);
-    return
 }
 
 #[inline]
-pub unsafe fn FD_ZERO(set: *mut fd_set) -> () {
+pub unsafe fn FD_ZERO(set: *mut fd_set) {
     for slot in (*set).fds_bits.iter_mut() {
         *slot = 0;
     }
 }
 
 #[inline]
-pub unsafe fn CPU_ZERO(cpuset: &mut cpu_set_t) -> () {
+pub unsafe fn CPU_ALLOC_SIZE(count: c_int) -> size_t {
+    let _dummy: cpu_set_t = mem::zeroed();
+    let size_in_bits = 8 * mem::size_of_val(&_dummy.bits[0]);
+    ((count as size_t + size_in_bits - 1) / 8) as size_t
+}
+
+#[inline]
+pub unsafe fn CPU_ZERO(cpuset: &mut cpu_set_t) {
     for slot in cpuset.bits.iter_mut() {
         *slot = 0;
     }
 }
 
 #[inline]
-pub unsafe fn CPU_SET(cpu: usize, cpuset: &mut cpu_set_t) -> () {
-    let size_in_bits
-        = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
+pub unsafe fn CPU_SET(cpu: usize, cpuset: &mut cpu_set_t) {
+    let size_in_bits = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
     let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
     cpuset.bits[idx] |= 1 << offset;
-    ()
 }
 
 #[inline]
-pub unsafe fn CPU_CLR(cpu: usize, cpuset: &mut cpu_set_t) -> () {
-    let size_in_bits
-        = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
+pub unsafe fn CPU_CLR(cpu: usize, cpuset: &mut cpu_set_t) {
+    let size_in_bits = 8 * mem::size_of_val(&cpuset.bits[0]); // 32, 64 etc
     let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
     cpuset.bits[idx] &= !(1 << offset);
-    ()
 }
 
 #[inline]
@@ -1496,6 +1515,21 @@
 }
 
 #[inline]
+pub unsafe fn CPU_COUNT_S(size: usize, cpuset: &cpu_set_t) -> c_int {
+    let mut s: u32 = 0;
+    let size_of_mask = mem::size_of_val(&cpuset.bits[0]);
+    for i in cpuset.bits[..(size / size_of_mask)].iter() {
+        s += i.count_ones();
+    }
+    s as c_int
+}
+
+#[inline]
+pub unsafe fn CPU_COUNT(cpuset: &cpu_set_t) -> c_int {
+    CPU_COUNT_S(mem::size_of::<cpu_set_t>(), cpuset)
+}
+
+#[inline]
 pub unsafe fn CPU_EQUAL(set1: &cpu_set_t, set2: &cpu_set_t) -> bool {
     set1.bits == set2.bits
 }
@@ -1507,32 +1541,28 @@
 
 #[inline]
 unsafe fn __sigword(sig: c_int) -> u64 {
-   ((sig - 1) / ((8 *  mem::size_of::<u64>()) as i32)) as u64
+    ((sig - 1) / ((8 * mem::size_of::<u64>()) as i32)) as u64
 }
 
 #[inline]
 unsafe fn __sigaddset(set: *mut sigset_t, sig: c_int) {
-    let mask: u64 = __sigmask (sig);
-    let word: u64 = __sigword (sig);
+    let mask: u64 = __sigmask(sig);
+    let word: u64 = __sigword(sig);
     (*set).__val[word as usize] |= mask;
 }
 
 #[inline]
 unsafe fn __sigdelset(set: *mut sigset_t, sig: c_int) {
-    let mask: u64 = __sigmask (sig);
-    let word: u64 = __sigword (sig);
+    let mask: u64 = __sigmask(sig);
+    let word: u64 = __sigword(sig);
     (*set).__val[word as usize] &= !mask;
 }
 
 #[inline]
 unsafe fn __sigismember(set: *const sigset_t, sig: c_int) -> c_int {
-    let mask: u64 = __sigmask (sig);
-    let word: u64 = __sigword (sig);
-    let val = if mask != 0 {
-        1
-    } else {
-        0
-    };
+    let mask: u64 = __sigmask(sig);
+    let word: u64 = __sigword(sig);
+    let val = if mask != 0 { 1 } else { 0 };
     ((*set).__val[word as usize] & val) as c_int
 }
 
@@ -1541,7 +1571,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    ptr::write_bytes(set as * mut sigset_t, 0,  1);
+    ptr::write_bytes(set as *mut sigset_t, 0, 1);
     0
 }
 
@@ -1580,4 +1610,77 @@
     __sigismember(set, signum)
 }
 
+#[inline]
+pub const fn CMSG_ALIGN(len: usize) -> usize {
+    len + mem::size_of::<usize>() - 1 & !(mem::size_of::<usize>() - 1)
+}
+
+#[inline]
+pub unsafe fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
+    if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
+        (*mhdr).msg_control as *mut cmsghdr
+    } else {
+        0 as *mut cmsghdr
+    }
+}
+
+#[inline]
+pub unsafe fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
+    cmsg.offset(1) as *mut c_uchar
+}
+
+#[inline]
+pub const unsafe fn CMSG_SPACE(length: c_uint) -> c_uint {
+    (CMSG_ALIGN(length as usize) + CMSG_ALIGN(mem::size_of::<cmsghdr>())) as c_uint
+}
+
+#[inline]
+pub unsafe fn CMSG_LEN(length: c_uint) -> c_uint {
+    CMSG_ALIGN(mem::size_of::<cmsghdr>()) as c_uint + length
+}
+
+#[inline]
+pub unsafe fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
+    if ((*cmsg).cmsg_len as usize) < mem::size_of::<cmsghdr>() {
+        return 0 as *mut cmsghdr;
+    };
+    let next = (cmsg as usize + CMSG_ALIGN((*cmsg).cmsg_len as usize)) as *mut cmsghdr;
+    let max = (*mhdr).msg_control as usize + (*mhdr).msg_controllen as usize;
+    if (next.offset(1)) as usize > max
+        || next as usize + CMSG_ALIGN((*next).cmsg_len as usize) > max
+    {
+        0 as *mut cmsghdr
+    } else {
+        next as *mut cmsghdr
+    }
+}
+
+#[inline]
+pub unsafe fn major(dev: dev_t) -> c_uint {
+    let mut major = 0;
+    major |= (dev & 0x00000000000fff00) >> 8;
+    major |= (dev & 0xfffff00000000000) >> 32;
+    major as c_uint
+}
+
+#[inline]
+pub unsafe fn minor(dev: dev_t) -> c_uint {
+    let mut minor = 0;
+    minor |= (dev & 0x00000000000000ff) >> 0;
+    minor |= (dev & 0x00000ffffff00000) >> 12;
+    minor as c_uint
+}
+
+#[inline]
+pub unsafe fn makedev(major: c_uint, minor: c_uint) -> dev_t {
+    let major = major as dev_t;
+    let minor = minor as dev_t;
+    let mut dev = 0;
+    dev |= (major & 0x00000fff) << 8;
+    dev |= (major & 0xfffff000) << 32;
+    dev |= (minor & 0x000000ff) << 0;
+    dev |= (minor & 0xffffff00) << 12;
+    dev
+}
+
 pub mod ocall;
diff --git a/sgx_libc/src/linux/x86_64/ocall.rs b/sgx_libc/src/linux/x86_64/ocall.rs
index 205e6c4..8a17915 100644
--- a/sgx_libc/src/linux/x86_64/ocall.rs
+++ b/sgx_libc/src/linux/x86_64/ocall.rs
@@ -15,449 +15,616 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::*;
 use super::*;
+use alloc::boxed::Box;
 use alloc::slice;
 use alloc::vec::Vec;
-use alloc::boxed::Box;
-use core::ptr;
-use core::mem;
-use core::convert::TryInto;
 use core::cmp;
+use core::convert::TryInto;
+use core::mem;
+use core::ptr;
+use sgx_types::*;
 
 const MAX_OCALL_ALLOC_SIZE: size_t = 0x4000; //16K
 extern "C" {
     // memory
-    pub fn u_malloc_ocall(result: *mut *mut c_void, error: *mut c_int, size: size_t) -> sgx_status_t;
+    pub fn u_malloc_ocall(
+        result: *mut *mut c_void,
+        error: *mut c_int,
+        size: size_t,
+    ) -> sgx_status_t;
     pub fn u_free_ocall(p: *mut c_void) -> sgx_status_t;
-    pub fn u_mmap_ocall(result: *mut *mut c_void,
-                        error: *mut c_int,
-                        start: *mut c_void,
-                        length: size_t,
-                        prot: c_int,
-                        flags: c_int,
-                        fd: c_int,
-                        offset: off_t) -> sgx_status_t;
-    pub fn u_munmap_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          start: *mut c_void,
-                          length: size_t) -> sgx_status_t;
-    pub fn u_msync_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         addr: *mut c_void,
-                         length: size_t,
-                         flags: c_int) -> sgx_status_t;
+    pub fn u_mmap_ocall(
+        result: *mut *mut c_void,
+        error: *mut c_int,
+        start: *mut c_void,
+        length: size_t,
+        prot: c_int,
+        flags: c_int,
+        fd: c_int,
+        offset: off_t,
+    ) -> sgx_status_t;
+    pub fn u_munmap_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        start: *mut c_void,
+        length: size_t,
+    ) -> sgx_status_t;
+    pub fn u_msync_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        addr: *mut c_void,
+        length: size_t,
+        flags: c_int,
+    ) -> sgx_status_t;
 
-    pub fn u_mprotect_ocall(result: *mut c_int,
-                            error: *mut c_int,
-                            addr: *mut c_void,
-                            length: size_t,
-                            prot: c_int) -> sgx_status_t;
+    pub fn u_mprotect_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        addr: *mut c_void,
+        length: size_t,
+        prot: c_int,
+    ) -> sgx_status_t;
     // env
     pub fn u_getuid_ocall(result: *mut uid_t) -> sgx_status_t;
     pub fn u_environ_ocall(result: *mut *const *const c_char) -> sgx_status_t;
-    pub fn u_getenv_ocall(result: *mut *const c_char,
-                          name: *const c_char) -> sgx_status_t;
-    pub fn u_setenv_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          name: *const c_char,
-                          value: *const c_char,
-                          overwrite: c_int) -> sgx_status_t;
-    pub fn u_unsetenv_ocall(result: *mut c_int,
-                            error: *mut c_int,
-                            name: *const c_char) -> sgx_status_t;
-    pub fn u_getcwd_ocall(result: *mut *mut c_char,
-                          error: *mut c_int,
-                          buf: *mut c_char,
-                          size: size_t) -> sgx_status_t;
-    pub fn u_chdir_ocall(result: *mut c_int,  error: *mut c_int, dir: *const c_char) -> sgx_status_t;
-    pub fn u_getpwuid_r_ocall(result: *mut c_int,
-                              uid: uid_t,
-                              pwd: *mut passwd,
-                              buf: *mut c_char,
-                              buflen: size_t,
-                              passwd_result: *mut *mut passwd) -> sgx_status_t;
+    pub fn u_getenv_ocall(result: *mut *const c_char, name: *const c_char) -> sgx_status_t;
+    pub fn u_setenv_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        name: *const c_char,
+        value: *const c_char,
+        overwrite: c_int,
+    ) -> sgx_status_t;
+    pub fn u_unsetenv_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        name: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_getcwd_ocall(
+        result: *mut *mut c_char,
+        error: *mut c_int,
+        buf: *mut c_char,
+        size: size_t,
+    ) -> sgx_status_t;
+    pub fn u_chdir_ocall(result: *mut c_int, error: *mut c_int, dir: *const c_char)
+        -> sgx_status_t;
+    pub fn u_getpwuid_r_ocall(
+        result: *mut c_int,
+        uid: uid_t,
+        pwd: *mut passwd,
+        buf: *mut c_char,
+        buflen: size_t,
+        passwd_result: *mut *mut passwd,
+    ) -> sgx_status_t;
     // file
-    pub fn u_open_ocall(result: *mut c_int,
-                        error: *mut c_int,
-                        path: *const c_char,
-                        flags: c_int) -> sgx_status_t;
-    pub fn u_open64_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          path: *const c_char,
-                          oflag: c_int,
-                          mode: c_int) -> sgx_status_t;
-    pub fn u_fstat_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         fd: c_int,
-                         buf: *mut stat) -> sgx_status_t;
-    pub fn u_fstat64_ocall(result: *mut c_int,
-                           error: *mut c_int,
-                           fd: c_int,
-                           buf: *mut stat64) -> sgx_status_t;
-    pub fn u_stat_ocall(result: *mut c_int,
-                        error: *mut c_int,
-                        path: *const c_char,
-                        buf: *mut stat) -> sgx_status_t;
-    pub fn u_stat64_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          path: *const c_char,
-                          buf: *mut stat64) -> sgx_status_t;
-    pub fn u_lstat_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         path: *const c_char,
-                         buf: *mut stat) -> sgx_status_t;
-    pub fn u_lstat64_ocall(result: *mut c_int,
-                           error: *mut c_int,
-                           path: *const c_char,
-                           buf: *mut stat64) -> sgx_status_t;
-    pub fn u_lseek_ocall(result: *mut off_t,
-                         error: *mut c_int,
-                         fd: c_int,
-                         offset: off_t,
-                         whence: c_int) -> sgx_status_t;
-    pub fn u_lseek64_ocall(result: *mut off64_t,
-                           error: *mut c_int,
-                           fd: c_int,
-                           offset: off64_t,
-                           whence: c_int) -> sgx_status_t;
-    pub fn u_ftruncate_ocall(result: *mut c_int,
-                             error: *mut c_int,
-                             fd: c_int,
-                             length: off_t) -> sgx_status_t;
-    pub fn u_ftruncate64_ocall(result: *mut c_int,
-                               error: *mut c_int,
-                               fd: c_int,
-                               length: off64_t) -> sgx_status_t;
-    pub fn u_truncate_ocall(result: *mut c_int,
-                            error: *mut c_int,
-                            path: *const c_char,
-                            length: off_t) -> sgx_status_t;
-    pub fn u_truncate64_ocall(result: *mut c_int,
-                              error: *mut c_int,
-                              path: *const c_char,
-                              length: off64_t) -> sgx_status_t;
-    pub fn u_fsync_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         fd: c_int) -> sgx_status_t;
-    pub fn u_fdatasync_ocall(result: *mut c_int,
-                             error: *mut c_int,
-                             fd: c_int) -> sgx_status_t;
-    pub fn u_fchmod_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          fd: c_int,
-                          mode: mode_t) -> sgx_status_t;
-    pub fn u_unlink_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          pathname: *const c_char) -> sgx_status_t;
-    pub fn u_link_ocall(result: *mut c_int,
-                        error: *mut c_int,
-                        oldpath: *const c_char,
-                        newpath: *const c_char) -> sgx_status_t;
-    pub fn u_rename_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          oldpath: *const c_char,
-                          newpath: *const c_char) -> sgx_status_t;
-    pub fn u_chmod_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         path: *const c_char,
-                         mode: mode_t) -> sgx_status_t;
-    pub fn u_readlink_ocall(result: *mut ssize_t,
-                            error: *mut c_int,
-                            path: *const c_char,
-                            buf: *mut c_char,
-                            bufsz: size_t) -> sgx_status_t;
-    pub fn u_symlink_ocall(result: *mut c_int,
-                           error: *mut c_int,
-                           path1: *const c_char,
-                           path2: *const c_char) -> sgx_status_t;
-    pub fn u_realpath_ocall(result: *mut *mut c_char,
-                            error: *mut c_int,
-                            pathname: *const c_char) -> sgx_status_t;
-    pub fn u_mkdir_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         pathname: *const c_char,
-                         mode: mode_t) -> sgx_status_t;
-    pub fn u_rmdir_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         pathname: *const c_char) -> sgx_status_t;
-    pub fn u_opendir_ocall(result: *mut *mut DIR,
-                           error: *mut c_int,
-                           pathname: *const c_char) -> sgx_status_t;
-    pub fn u_readdir64_r_ocall(result: *mut c_int,
-                               dirp: *mut DIR,
-                               entry: *mut dirent64,
-                               dirresult: *mut *mut  dirent64) -> sgx_status_t;
-    pub fn u_closedir_ocall(result: *mut c_int,
-                            error: *mut c_int,
-                            dirp: *mut DIR) -> sgx_status_t;
-    pub fn u_dirfd_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         dirp: *mut DIR) -> sgx_status_t;
-    pub fn u_fstatat64_ocall(result: *mut c_int,
-                             error: *mut c_int,
-                             dirfd: c_int,
-                             pathname: *const c_char,
-                             buf: *mut stat64,
-                             flags: c_int) -> sgx_status_t;
+    pub fn u_open_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_open64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        oflag: c_int,
+        mode: c_int,
+    ) -> sgx_status_t;
+    pub fn u_fstat_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fd: c_int,
+        buf: *mut stat,
+    ) -> sgx_status_t;
+    pub fn u_fstat64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fd: c_int,
+        buf: *mut stat64,
+    ) -> sgx_status_t;
+    pub fn u_stat_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        buf: *mut stat,
+    ) -> sgx_status_t;
+    pub fn u_stat64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        buf: *mut stat64,
+    ) -> sgx_status_t;
+    pub fn u_lstat_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        buf: *mut stat,
+    ) -> sgx_status_t;
+    pub fn u_lstat64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        buf: *mut stat64,
+    ) -> sgx_status_t;
+    pub fn u_lseek_ocall(
+        result: *mut off_t,
+        error: *mut c_int,
+        fd: c_int,
+        offset: off_t,
+        whence: c_int,
+    ) -> sgx_status_t;
+    pub fn u_lseek64_ocall(
+        result: *mut off64_t,
+        error: *mut c_int,
+        fd: c_int,
+        offset: off64_t,
+        whence: c_int,
+    ) -> sgx_status_t;
+    pub fn u_ftruncate_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fd: c_int,
+        length: off_t,
+    ) -> sgx_status_t;
+    pub fn u_ftruncate64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fd: c_int,
+        length: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_truncate_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        length: off_t,
+    ) -> sgx_status_t;
+    pub fn u_truncate64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        length: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_fsync_ocall(result: *mut c_int, error: *mut c_int, fd: c_int) -> sgx_status_t;
+    pub fn u_fdatasync_ocall(result: *mut c_int, error: *mut c_int, fd: c_int) -> sgx_status_t;
+    pub fn u_fchmod_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fd: c_int,
+        mode: mode_t,
+    ) -> sgx_status_t;
+    pub fn u_unlink_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        pathname: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_link_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        oldpath: *const c_char,
+        newpath: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_linkat_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        olddirfd: c_int,
+        oldpath: *const c_char,
+        newdirfd: c_int,
+        newpath: *const c_char,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_rename_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        oldpath: *const c_char,
+        newpath: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_chmod_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path: *const c_char,
+        mode: mode_t,
+    ) -> sgx_status_t;
+    pub fn u_readlink_ocall(
+        result: *mut ssize_t,
+        error: *mut c_int,
+        path: *const c_char,
+        buf: *mut c_char,
+        bufsz: size_t,
+    ) -> sgx_status_t;
+    pub fn u_symlink_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        path1: *const c_char,
+        path2: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_realpath_ocall(
+        result: *mut *mut c_char,
+        error: *mut c_int,
+        pathname: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_mkdir_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        pathname: *const c_char,
+        mode: mode_t,
+    ) -> sgx_status_t;
+    pub fn u_rmdir_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        pathname: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_opendir_ocall(
+        result: *mut *mut DIR,
+        error: *mut c_int,
+        pathname: *const c_char,
+    ) -> sgx_status_t;
+    pub fn u_readdir64_r_ocall(
+        result: *mut c_int,
+        dirp: *mut DIR,
+        entry: *mut dirent64,
+        dirresult: *mut *mut dirent64,
+    ) -> sgx_status_t;
+    pub fn u_closedir_ocall(result: *mut c_int, error: *mut c_int, dirp: *mut DIR) -> sgx_status_t;
+    pub fn u_dirfd_ocall(result: *mut c_int, error: *mut c_int, dirp: *mut DIR) -> sgx_status_t;
+    pub fn u_fstatat64_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        dirfd: c_int,
+        pathname: *const c_char,
+        buf: *mut stat64,
+        flags: c_int,
+    ) -> sgx_status_t;
     // fd
-    pub fn u_read_ocall(result: *mut ssize_t,
-                        errno: *mut c_int,
-                        fd: c_int,
-                        buf: *mut c_void,
-                        count: size_t) -> sgx_status_t;
-    pub fn u_pread64_ocall(result: *mut ssize_t,
-                           errno: *mut c_int,
-                           fd: c_int,
-                           buf: *mut c_void,
-                           count: size_t,
-                           offset: off64_t) -> sgx_status_t;
-    pub fn u_readv_ocall(result: *mut ssize_t,
-                         errno: *mut c_int,
-                         fd: c_int,
-                         iov: *const iovec,
-                         iovcnt: c_int) -> sgx_status_t;
-    pub fn u_preadv64_ocall(result: *mut ssize_t,
-                            errno: *mut c_int,
-                            fd: c_int,
-                            iov: *const iovec,
-                            iovcnt: c_int,
-                            offset: off64_t) -> sgx_status_t;
-    pub fn u_write_ocall(result: *mut ssize_t,
-                         errno: *mut c_int,
-                         fd: c_int,
-                         buf: *const c_void,
-                         count: size_t) -> sgx_status_t;
-    pub fn u_pwrite64_ocall(result: *mut ssize_t,
-                            errno: *mut c_int,
-                            fd: c_int,
-                            buf: *const c_void,
-                            count: size_t,
-                            offset: off64_t) -> sgx_status_t;
-    pub fn u_writev_ocall(result: *mut ssize_t,
-                          errno: *mut c_int,
-                          fd: c_int,
-                          iov: *const iovec,
-                          iovcnt: c_int) -> sgx_status_t;
-    pub fn u_pwritev64_ocall(result: *mut ssize_t,
-                             errno: *mut c_int,
-                             fd: c_int,
-                             iov: *const iovec,
-                             iovcnt: c_int,
-                             offset: off64_t) -> sgx_status_t;
-    pub fn u_fcntl_arg0_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              fd: c_int,
-                              cmd: c_int) -> sgx_status_t;
-    pub fn u_fcntl_arg1_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              fd: c_int,
-                              cmd: c_int,
-                              arg: c_int) -> sgx_status_t;
-    pub fn u_ioctl_arg0_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              fd: c_int,
-                              request: c_int) -> sgx_status_t;
-    pub fn u_ioctl_arg1_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              fd: c_int,
-                              request: c_int,
-                              arg: *mut c_int) -> sgx_status_t;
-    pub fn u_close_ocall(result: *mut c_int,
-                         errno: *mut c_int,
-                         fd: c_int) -> sgx_status_t;
+    pub fn u_read_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        buf: *mut c_void,
+        count: size_t,
+    ) -> sgx_status_t;
+    pub fn u_pread64_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        buf: *mut c_void,
+        count: size_t,
+        offset: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_readv_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        iov: *const iovec,
+        iovcnt: c_int,
+    ) -> sgx_status_t;
+    pub fn u_preadv64_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        iov: *const iovec,
+        iovcnt: c_int,
+        offset: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_write_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        buf: *const c_void,
+        count: size_t,
+    ) -> sgx_status_t;
+    pub fn u_pwrite64_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        buf: *const c_void,
+        count: size_t,
+        offset: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_writev_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        iov: *const iovec,
+        iovcnt: c_int,
+    ) -> sgx_status_t;
+    pub fn u_pwritev64_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        fd: c_int,
+        iov: *const iovec,
+        iovcnt: c_int,
+        offset: off64_t,
+    ) -> sgx_status_t;
+    pub fn u_fcntl_arg0_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        fd: c_int,
+        cmd: c_int,
+    ) -> sgx_status_t;
+    pub fn u_fcntl_arg1_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        fd: c_int,
+        cmd: c_int,
+        arg: c_int,
+    ) -> sgx_status_t;
+    pub fn u_ioctl_arg0_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        fd: c_int,
+        request: c_int,
+    ) -> sgx_status_t;
+    pub fn u_ioctl_arg1_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        fd: c_int,
+        request: c_int,
+        arg: *mut c_int,
+    ) -> sgx_status_t;
+    pub fn u_close_ocall(result: *mut c_int, errno: *mut c_int, fd: c_int) -> sgx_status_t;
     // time
-    pub fn u_clock_gettime_ocall(result: *mut c_int,
-                                 errno: *mut c_int,
-                                 clk_id: clockid_t,
-                                 tp: *mut timespec) -> sgx_status_t;
+    pub fn u_clock_gettime_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        clk_id: clockid_t,
+        tp: *mut timespec,
+    ) -> sgx_status_t;
     // socket
-    pub fn u_socket_ocall(result: *mut c_int,
-                          errno: *mut c_int,
-                          domain: c_int,
-                          ty: c_int,
-                          protocol: c_int) -> sgx_status_t;
-    pub fn u_socketpair_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              domain: c_int,
-                              ty: c_int,
-                              protocol: c_int,
-                              sv: *mut c_int) -> sgx_status_t;
-    pub fn u_bind_ocall(result: *mut c_int,
-                        errno: *mut c_int,
-                        sockfd: c_int,
-                        address: *const sockaddr,
-                        addrlen: socklen_t) -> sgx_status_t;
-    pub fn u_listen_ocall(result: *mut c_int,
-                          error: *mut c_int,
-                          sockfd: c_int,
-                          backlog: c_int) -> sgx_status_t;
-    pub fn u_accept_ocall(result: *mut c_int,
-                          errno: *mut c_int,
-                          sockfd: c_int,
-                          addr: *mut sockaddr,
-                          addrlen_in: socklen_t,
-                          addrlen_out: *mut socklen_t) -> sgx_status_t;
-    pub fn u_accept4_ocall(result: *mut c_int,
-                           errno: *mut c_int,
-                           sockfd: c_int,
-                           addr: *mut sockaddr,
-                           addrlen_in: socklen_t,
-                           addrlen_out: *mut socklen_t,
-                           flags: c_int) -> sgx_status_t;
-    pub fn u_connect_ocall(result: *mut c_int,
-                           errno: *mut c_int,
-                           sockfd: c_int,
-                           address: *const sockaddr,
-                           addrlen: socklen_t) -> sgx_status_t;
-    pub fn u_send_ocall(result: *mut ssize_t,
-                        errno: *mut c_int,
-                        sockfd: c_int,
-                        buf: *const c_void,
-                        len: size_t,
-                        flags: c_int) -> sgx_status_t;
-    pub fn u_sendto_ocall(result: *mut ssize_t,
-                          errno: *mut c_int,
-                          sockfd: c_int,
-                          buf: *const c_void,
-                          len: size_t,
-                          flags: c_int,
-                          addr: *const sockaddr,
-                          addrlen: socklen_t) -> sgx_status_t;
-    pub fn u_sendmsg_ocall(result: *mut ssize_t,
-                           error: *mut c_int,
-                           sockfd: c_int,
-                           msg: *const msghdr,
-                           flags: c_int) -> sgx_status_t;
-    pub fn u_recv_ocall(result: *mut ssize_t,
-                        errno: *mut c_int,
-                        sockfd: c_int,
-                        buf: *mut c_void,
-                        len: size_t,
-                        flags: c_int) -> sgx_status_t;
-    pub fn u_recvfrom_ocall(result: *mut ssize_t,
-                            errno: *mut c_int,
-                            sockfd: c_int,
-                            buf: *mut c_void,
-                            len: size_t,
-                            flags: c_int,
-                            addr: *mut sockaddr,
-                            addrlen_in: socklen_t,
-                            addrlen_out: *mut socklen_t) -> sgx_status_t;
-    pub fn u_recvmsg_ocall(result: *mut ssize_t,
-                           error: *mut c_int,
-                           sockfd: c_int,
-                           msg: *mut msghdr,
-                           flags: c_int) -> sgx_status_t;
-    pub fn u_setsockopt_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              sockfd: c_int,
-                              level: c_int,
-                              optname: c_int,
-                              optval: *const c_void,
-                              optlen: socklen_t) -> sgx_status_t;
-    pub fn u_getsockopt_ocall(result: *mut c_int,
-                              errno: *mut c_int,
-                              sockfd: c_int,
-                              level: c_int,
-                              optname: c_int,
-                              optval: *mut c_void,
-                              optlen_in: socklen_t,
-                              optlen_out: *mut socklen_t) -> sgx_status_t;
-    pub fn u_getpeername_ocall(result: *mut c_int,
-                               errno: *mut c_int,
-                               sockfd: c_int,
-                               address: *mut sockaddr,
-                               addrlen_in: socklen_t,
-                               addrlen_out: *mut socklen_t) -> sgx_status_t;
-    pub fn u_getsockname_ocall(result: *mut c_int,
-                               errno: *mut c_int,
-                               sockfd: c_int,
-                               address: *mut sockaddr,
-                               addrlen_in: socklen_t,
-                               addrlen_out: *mut socklen_t) -> sgx_status_t;
-    pub fn u_shutdown_ocall(result: *mut c_int,
-                            errno: *mut c_int,
-                            sockfd: c_int,
-                            how: c_int) -> sgx_status_t;
+    pub fn u_socket_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        domain: c_int,
+        ty: c_int,
+        protocol: c_int,
+    ) -> sgx_status_t;
+    pub fn u_socketpair_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        domain: c_int,
+        ty: c_int,
+        protocol: c_int,
+        sv: *mut c_int,
+    ) -> sgx_status_t;
+    pub fn u_bind_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        address: *const sockaddr,
+        addrlen: socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_listen_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        sockfd: c_int,
+        backlog: c_int,
+    ) -> sgx_status_t;
+    pub fn u_accept_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        addr: *mut sockaddr,
+        addrlen_in: socklen_t,
+        addrlen_out: *mut socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_accept4_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        addr: *mut sockaddr,
+        addrlen_in: socklen_t,
+        addrlen_out: *mut socklen_t,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_connect_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        address: *const sockaddr,
+        addrlen: socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_send_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        sockfd: c_int,
+        buf: *const c_void,
+        len: size_t,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_sendto_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        sockfd: c_int,
+        buf: *const c_void,
+        len: size_t,
+        flags: c_int,
+        addr: *const sockaddr,
+        addrlen: socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_sendmsg_ocall(
+        result: *mut ssize_t,
+        error: *mut c_int,
+        sockfd: c_int,
+        msg_name: *const c_void,
+        msg_namelen: socklen_t,
+        msg_iov: *const iovec,
+        msg_iovlen: usize,
+        msg_control: *const c_void,
+        msg_controllen: usize,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_recv_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        sockfd: c_int,
+        buf: *mut c_void,
+        len: size_t,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_recvfrom_ocall(
+        result: *mut ssize_t,
+        errno: *mut c_int,
+        sockfd: c_int,
+        buf: *mut c_void,
+        len: size_t,
+        flags: c_int,
+        addr: *mut sockaddr,
+        addrlen_in: socklen_t,
+        addrlen_out: *mut socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_recvmsg_ocall(
+        result: *mut ssize_t,
+        error: *mut c_int,
+        sockfd: c_int,
+        msg_name: *mut c_void,
+        msg_namelen: socklen_t,
+        msg_namelen_out: *mut socklen_t,
+        msg_iov: *mut iovec,
+        msg_iovlen: usize,
+        msg_control: *mut c_void,
+        msg_controllen: usize,
+        msg_controllen_out: *mut usize,
+        msg_flags: *mut c_int,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_setsockopt_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        level: c_int,
+        optname: c_int,
+        optval: *const c_void,
+        optlen: socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_getsockopt_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        level: c_int,
+        optname: c_int,
+        optval: *mut c_void,
+        optlen_in: socklen_t,
+        optlen_out: *mut socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_getpeername_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        address: *mut sockaddr,
+        addrlen_in: socklen_t,
+        addrlen_out: *mut socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_getsockname_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        address: *mut sockaddr,
+        addrlen_in: socklen_t,
+        addrlen_out: *mut socklen_t,
+    ) -> sgx_status_t;
+    pub fn u_shutdown_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        sockfd: c_int,
+        how: c_int,
+    ) -> sgx_status_t;
     // net
-    pub fn u_getaddrinfo_ocall(result: *mut c_int,
-                               errno: *mut c_int,
-                               node: *const c_char,
-                               service: *const c_char,
-                               hints: *const addrinfo,
-                               res: *mut *mut addrinfo) -> sgx_status_t;
+    pub fn u_getaddrinfo_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        node: *const c_char,
+        service: *const c_char,
+        hints: *const addrinfo,
+        res: *mut *mut addrinfo,
+    ) -> sgx_status_t;
     pub fn u_freeaddrinfo_ocall(res: *mut addrinfo) -> sgx_status_t;
     pub fn u_gai_strerror_ocall(result: *mut *const c_char, errcode: c_int) -> sgx_status_t;
     // async io
-    pub fn u_poll_ocall(result: *mut c_int,
-                        errno: *mut c_int,
-                        fds: *mut pollfd,
-                        nfds: nfds_t,
-                        timeout: c_int) -> sgx_status_t;
-    pub fn u_epoll_create1_ocall(result: *mut c_int,
-                                 error: *mut c_int,
-                                 flags: c_int) -> sgx_status_t;
-    pub fn u_epoll_ctl_ocall(result: *mut c_int,
-                             error: *mut c_int,
-                             epfd: c_int,
-                             op: c_int,
-                             fd: c_int,
-                             event: *mut epoll_event) -> sgx_status_t;
-    pub fn u_epoll_wait_ocall(result: *mut c_int,
-                              error: *mut c_int,
-                              epfd: c_int,
-                              events: *mut epoll_event,
-                              maxevents: c_int,
-                              timeout: c_int) -> sgx_status_t;
+    pub fn u_poll_ocall(
+        result: *mut c_int,
+        errno: *mut c_int,
+        fds: *mut pollfd,
+        nfds: nfds_t,
+        timeout: c_int,
+    ) -> sgx_status_t;
+    pub fn u_epoll_create1_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        flags: c_int,
+    ) -> sgx_status_t;
+    pub fn u_epoll_ctl_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        epfd: c_int,
+        op: c_int,
+        fd: c_int,
+        event: *mut epoll_event,
+    ) -> sgx_status_t;
+    pub fn u_epoll_wait_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        epfd: c_int,
+        events: *mut epoll_event,
+        maxevents: c_int,
+        timeout: c_int,
+    ) -> sgx_status_t;
     // sys
-    pub fn u_sysconf_ocall(result: *mut c_long,
-                           error: *mut c_int,
-                           name: c_int) -> sgx_status_t;
-    pub fn u_prctl_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         option: c_int,
-                         arg2: c_ulong,
-                         arg3: c_ulong,
-                         arg4: c_ulong,
-                         arg5: c_ulong) -> sgx_status_t;
-    pub fn u_sched_setaffinity_ocall(result: *mut c_int,
-                                     error: *mut c_int,
-                                     pid: pid_t,
-                                     cpusetsize: size_t,
-                                     mask: *const cpu_set_t) -> sgx_status_t;
-    pub fn u_sched_getaffinity_ocall(result: *mut c_int,
-                                     error: *mut c_int,
-                                     pid: pid_t,
-                                     cpusetsize: size_t,
-                                     mask: *mut cpu_set_t) -> sgx_status_t;
+    pub fn u_sysconf_ocall(result: *mut c_long, error: *mut c_int, name: c_int) -> sgx_status_t;
+    pub fn u_prctl_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        option: c_int,
+        arg2: c_ulong,
+        arg3: c_ulong,
+        arg4: c_ulong,
+        arg5: c_ulong,
+    ) -> sgx_status_t;
+    pub fn u_sched_setaffinity_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        pid: pid_t,
+        cpusetsize: size_t,
+        mask: *const cpu_set_t,
+    ) -> sgx_status_t;
+    pub fn u_sched_getaffinity_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        pid: pid_t,
+        cpusetsize: size_t,
+        mask: *mut cpu_set_t,
+    ) -> sgx_status_t;
     // pipe
-    pub fn u_pipe_ocall(result: *mut c_int,
-                        error: *mut c_int,
-                        fds: *mut c_int) -> sgx_status_t;
-    pub fn u_pipe2_ocall(result: *mut c_int,
-                         error: *mut c_int,
-                         fds: *mut c_int,
-                         flags: c_int) -> sgx_status_t;
+    pub fn u_pipe_ocall(result: *mut c_int, error: *mut c_int, fds: *mut c_int) -> sgx_status_t;
+    pub fn u_pipe2_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        fds: *mut c_int,
+        flags: c_int,
+    ) -> sgx_status_t;
     //thread
-    pub  fn u_sched_yield_ocall(result: *mut c_int,
-                                error: *mut c_int) -> sgx_status_t;
-    pub  fn u_nanosleep_ocall(result: *mut c_int,
-                              error: *mut c_int,
-                              rqtp: *const timespec,
-                              rmtp: *mut timespec) -> sgx_status_t;
+    pub fn u_sched_yield_ocall(result: *mut c_int, error: *mut c_int) -> sgx_status_t;
+    pub fn u_nanosleep_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        rqtp: *const timespec,
+        rmtp: *mut timespec,
+    ) -> sgx_status_t;
     //signal
-    pub fn u_sigaction_ocall(result: *mut c_int,
-                             error: *mut c_int,
-                             signum: c_int,
-                             act: *const sigaction,
-                             oldact: *mut sigaction,
-                             enclave_id: uint64_t) -> sgx_status_t;
-    pub fn u_sigprocmask_ocall(result: *mut c_int,
-                                error: *mut c_int,
-                                signum: c_int,
-                                set: *const sigset_t,
-                                oldset: *mut sigset_t) -> sgx_status_t;
+    pub fn u_sigaction_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        signum: c_int,
+        act: *const sigaction,
+        oldact: *mut sigaction,
+        enclave_id: uint64_t,
+    ) -> sgx_status_t;
+    pub fn u_sigprocmask_ocall(
+        result: *mut c_int,
+        error: *mut c_int,
+        signum: c_int,
+        set: *const sigset_t,
+        oldset: *mut sigset_t,
+    ) -> sgx_status_t;
     pub fn u_raise_ocall(result: *mut c_int, signum: c_int) -> sgx_status_t;
     //process
     pub fn u_getpid_ocall(result: *mut pid_t) -> sgx_status_t;
@@ -466,24 +633,25 @@
 pub unsafe fn malloc(size: size_t) -> *mut c_void {
     let mut result: *mut c_void = ptr::null_mut();
     let mut error: c_int = 0;
-    let status = u_malloc_ocall(&mut result as *mut *mut c_void,
-                                &mut error as *mut c_int,
-                                size);
+    let status = u_malloc_ocall(
+        &mut result as *mut *mut c_void,
+        &mut error as *mut c_int,
+        size,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
-        if result.is_null() {
+        if !result.is_null() {
+            if sgx_is_outside_enclave(result, size) == 0 {
+                set_errno(ESGX);
+                result = ptr::null_mut();
+            }
+        } else {
             set_errno(error);
         }
     } else {
         set_errno(ESGX);
         result = ptr::null_mut();
     }
-
-    if sgx_is_outside_enclave(result, size) == 0 {
-        set_errno(ESGX);
-        result = ptr::null_mut();
-    }
-
     result
 }
 
@@ -491,47 +659,52 @@
     let _ = u_free_ocall(p);
 }
 
-pub unsafe fn mmap(start: *mut c_void,
-                   length: size_t,
-                   prot: c_int,
-                   flags: c_int,
-                   fd: c_int,
-                   offset: off_t) -> *mut c_void {
+pub unsafe fn mmap(
+    start: *mut c_void,
+    length: size_t,
+    prot: c_int,
+    flags: c_int,
+    fd: c_int,
+    offset: off_t,
+) -> *mut c_void {
     let mut result: *mut c_void = ptr::null_mut();
     let mut error: c_int = 0;
-    let status = u_mmap_ocall(&mut result as *mut *mut c_void,
-                              &mut error as *mut c_int,
-                              start,
-                              length,
-                              prot,
-                              flags,
-                              fd,
-                              offset);
+    let status = u_mmap_ocall(
+        &mut result as *mut *mut c_void,
+        &mut error as *mut c_int,
+        start,
+        length,
+        prot,
+        flags,
+        fd,
+        offset,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
-        if result as isize == -1 {
+        if result as isize != -1 {
+            if sgx_is_outside_enclave(result, length) == 0 {
+                set_errno(ESGX);
+                result = -1 as isize as *mut c_void;
+            }
+        } else {
             set_errno(error);
         }
     } else {
         set_errno(ESGX);
         result = -1 as isize as *mut c_void;
     }
-
-    if sgx_is_outside_enclave(result, length) == 0 {
-        set_errno(ESGX);
-        result = -1 as isize as *mut c_void;
-    }
-
     result
 }
 
 pub unsafe fn munmap(start: *mut c_void, length: size_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_munmap_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                start,
-                                length);
+    let status = u_munmap_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        start,
+        length,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -547,11 +720,13 @@
 pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_msync_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               addr,
-                               length,
-                               flags);
+    let status = u_msync_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        addr,
+        length,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -567,11 +742,13 @@
 pub unsafe fn mprotect(addr: *mut c_void, length: size_t, prot: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_mprotect_ocall(&mut result as *mut c_int,
-                                  &mut error as *mut c_int,
-                                  addr,
-                                  length,
-                                  prot);
+    let status = u_mprotect_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        addr,
+        length,
+        prot,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -588,8 +765,8 @@
     let mut result: uid_t = 0;
     let status = u_getuid_ocall(&mut result as *mut uid_t);
     if status != sgx_status_t::SGX_SUCCESS {
-         set_errno(ESGX);
-         result = 0;
+        set_errno(ESGX);
+        result = 0;
     }
     result
 }
@@ -598,7 +775,27 @@
     let mut result: *const *const c_char = ptr::null();
     let status = u_environ_ocall(&mut result as *mut *const *const c_char);
 
-    if status != sgx_status_t::SGX_SUCCESS {
+    if status == sgx_status_t::SGX_SUCCESS {
+        let mut environ = result;
+        let mut count: usize = 0;
+        if !environ.is_null() {
+            while !(*environ).is_null() {
+                let len = strlen(*environ) + 1;
+                if sgx_is_outside_enclave(*environ as *const c_void, len) == 0 {
+                    return ptr::null();
+                }
+                count += 1;
+                environ = environ.add(1);
+            }
+            if sgx_is_outside_enclave(
+                result as *const c_void,
+                mem::size_of::<*const c_char>() * count,
+            ) == 0
+            {
+                return ptr::null();
+            }
+        }
+    } else {
         result = ptr::null();
     }
     result
@@ -608,7 +805,14 @@
     let mut result: *const c_char = ptr::null();
     let status = u_getenv_ocall(&mut result as *mut *const c_char, name);
 
-    if status != sgx_status_t::SGX_SUCCESS {
+    if status == sgx_status_t::SGX_SUCCESS {
+        if !result.is_null() {
+            let len: usize = strlen(result) + 1;
+            if sgx_is_outside_enclave(result as *const c_void, len) == 0 {
+                result = ptr::null();
+            }
+        }
+    } else {
         result = ptr::null();
     }
     result
@@ -617,11 +821,13 @@
 pub unsafe fn setenv(name: *const c_char, value: *const c_char, overwrite: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_setenv_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                name,
-                                value,
-                                overwrite);
+    let status = u_setenv_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        name,
+        value,
+        overwrite,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -637,9 +843,7 @@
 pub unsafe fn unsetenv(name: *const c_char) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_unsetenv_ocall(&mut result as *mut c_int,
-                                  &mut error as *mut c_int,
-                                  name);
+    let status = u_unsetenv_ocall(&mut result as *mut c_int, &mut error as *mut c_int, name);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -655,12 +859,26 @@
 pub unsafe fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char {
     let mut result: *mut c_char = ptr::null_mut();
     let mut error: c_int = 0;
-    let status = u_getcwd_ocall(&mut result as *mut *mut c_char, &mut error as *mut c_int, buf, size);
+    let status = u_getcwd_ocall(
+        &mut result as *mut *mut c_char,
+        &mut error as *mut c_int,
+        buf,
+        size,
+    );
+
     if status == sgx_status_t::SGX_SUCCESS {
-        if result.is_null() {
-            set_errno(error);
+        if !result.is_null() {
+            if buf.is_null() {
+                let len = if size > 0 { size } else { strlen(result) + 1 };
+                if sgx_is_outside_enclave(result as *const c_void, len) == 0 {
+                    set_errno(ESGX);
+                    result = ptr::null_mut();
+                }
+            } else {
+                result = buf;
+            }
         } else {
-            result = buf;
+            set_errno(error);
         }
     } else {
         set_errno(ESGX);
@@ -673,6 +891,7 @@
     let mut result: c_int = 0;
     let mut error: c_int = 0;
     let status = u_chdir_ocall(&mut result as *mut c_int, &mut error as *mut c_int, dir);
+
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -684,18 +903,23 @@
     result
 }
 
-pub unsafe fn getpwuid_r(uid: uid_t,
-                         pwd: *mut passwd,
-                         buf: *mut c_char,
-                         buflen: size_t,
-                         passwd_result:  *mut *mut passwd) -> c_int {
+pub unsafe fn getpwuid_r(
+    uid: uid_t,
+    pwd: *mut passwd,
+    buf: *mut c_char,
+    buflen: size_t,
+    passwd_result: *mut *mut passwd,
+) -> c_int {
     let mut result: c_int = 0;
-    let status = u_getpwuid_r_ocall(&mut result as *mut c_int,
-                                    uid,
-                                    pwd,
-                                    buf,
-                                    buflen,
-                                    passwd_result);
+    let status = u_getpwuid_r_ocall(
+        &mut result as *mut c_int,
+        uid,
+        pwd,
+        buf,
+        buflen,
+        passwd_result,
+    );
+
     if status == sgx_status_t::SGX_SUCCESS && result == 0 {
         let pwd_ret = *passwd_result;
         if !pwd_ret.is_null() {
@@ -744,10 +968,12 @@
 pub unsafe fn open(path: *const c_char, flags: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_open_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              path,
-                              flags);
+    let status = u_open_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -763,11 +989,13 @@
 pub unsafe fn open64(path: *const c_char, oflag: c_int, mode: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_open64_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                path,
-                                oflag,
-                                mode);
+    let status = u_open64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        oflag,
+        mode,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -783,10 +1011,7 @@
 pub unsafe fn fstat(fd: c_int, buf: *mut stat) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fstat_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               fd,
-                               buf);
+    let status = u_fstat_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd, buf);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -802,10 +1027,7 @@
 pub unsafe fn fstat64(fd: c_int, buf: *mut stat64) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fstat64_ocall(&mut result as *mut c_int,
-                                 &mut error as *mut c_int,
-                                 fd,
-                                 buf);
+    let status = u_fstat64_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd, buf);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -821,10 +1043,12 @@
 pub unsafe fn stat(path: *const c_char, buf: *mut stat) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_stat_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              path,
-                              buf);
+    let status = u_stat_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        buf,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -840,10 +1064,12 @@
 pub unsafe fn stat64(path: *const c_char, buf: *mut stat64) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_stat64_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                path,
-                                buf);
+    let status = u_stat64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        buf,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -859,10 +1085,12 @@
 pub unsafe fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_lstat_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               path,
-                               buf);
+    let status = u_lstat_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        buf,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -878,10 +1106,12 @@
 pub unsafe fn lstat64(path: *const c_char, buf: *mut stat64) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_lstat64_ocall(&mut result as *mut c_int,
-                                 &mut error as *mut c_int,
-                                 path,
-                                 buf);
+    let status = u_lstat64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        buf,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -897,11 +1127,13 @@
 pub unsafe fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t {
     let mut result: off_t = 0;
     let mut error: c_int = 0;
-    let status = u_lseek_ocall(&mut result as *mut off_t,
-                               &mut error as *mut c_int,
-                               fd,
-                               offset,
-                               whence);
+    let status = u_lseek_ocall(
+        &mut result as *mut off_t,
+        &mut error as *mut c_int,
+        fd,
+        offset,
+        whence,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -917,11 +1149,13 @@
 pub unsafe fn lseek64(fd: c_int, offset: off64_t, whence: c_int) -> off64_t {
     let mut result: off64_t = 0;
     let mut error: c_int = 0;
-    let status = u_lseek64_ocall(&mut result as *mut off64_t,
-                                 &mut error as *mut c_int,
-                                 fd,
-                                 offset,
-                                 whence);
+    let status = u_lseek64_ocall(
+        &mut result as *mut off64_t,
+        &mut error as *mut c_int,
+        fd,
+        offset,
+        whence,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -937,10 +1171,12 @@
 pub unsafe fn ftruncate(fd: c_int, length: off_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_ftruncate_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   fd,
-                                   length);
+    let status = u_ftruncate_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        length,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -956,10 +1192,12 @@
 pub unsafe fn ftruncate64(fd: c_int, length: off64_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_ftruncate64_ocall(&mut result as *mut c_int,
-                                     &mut error as *mut c_int,
-                                     fd,
-                                     length);
+    let status = u_ftruncate64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        length,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -975,10 +1213,12 @@
 pub unsafe fn truncate(path: *const c_char, length: off_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_truncate_ocall(&mut result as *mut c_int,
-                                  &mut error as *mut c_int,
-                                  path,
-                                  length);
+    let status = u_truncate_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        length,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -994,10 +1234,12 @@
 pub unsafe fn truncate64(path: *const c_char, length: off64_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_truncate64_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    path,
-                                    length);
+    let status = u_truncate64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        length,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1013,9 +1255,7 @@
 pub unsafe fn fsync(fd: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fsync_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               fd);
+    let status = u_fsync_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1031,9 +1271,7 @@
 pub unsafe fn fdatasync(fd: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fdatasync_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   fd);
+    let status = u_fdatasync_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1049,10 +1287,12 @@
 pub unsafe fn fchmod(fd: c_int, mode: mode_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fchmod_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                fd,
-                                mode);
+    let status = u_fchmod_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        mode,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1068,9 +1308,11 @@
 pub unsafe fn unlink(pathname: *const c_char) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_unlink_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                pathname);
+    let status = u_unlink_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        pathname,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1086,10 +1328,42 @@
 pub unsafe fn link(oldpath: *const c_char, newpath: *const c_char) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_link_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              oldpath,
-                              newpath);
+    let status = u_link_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        oldpath,
+        newpath,
+    );
+
+    if status == sgx_status_t::SGX_SUCCESS {
+        if result == -1 {
+            set_errno(error);
+        }
+    } else {
+        set_errno(ESGX);
+        result = -1;
+    }
+    result
+}
+
+pub unsafe fn linkat(
+    olddirfd: c_int,
+    oldpath: *const c_char,
+    newdirfd: c_int,
+    newpath: *const c_char,
+    flags: c_int,
+) -> c_int {
+    let mut result: c_int = 0;
+    let mut error: c_int = 0;
+    let status = u_linkat_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        olddirfd,
+        oldpath,
+        newdirfd,
+        newpath,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1105,10 +1379,12 @@
 pub unsafe fn rename(oldpath: *const c_char, newpath: *const c_char) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_rename_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                oldpath,
-                                newpath);
+    let status = u_rename_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        oldpath,
+        newpath,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1124,10 +1400,12 @@
 pub unsafe fn chmod(path: *const c_char, mode: mode_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_chmod_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               path,
-                               mode);
+    let status = u_chmod_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path,
+        mode,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1143,11 +1421,13 @@
 pub unsafe fn readlink(path: *const c_char, buf: *mut c_char, bufsz: size_t) -> ssize_t {
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
-    let status = u_readlink_ocall(&mut result as *mut ssize_t,
-                                  &mut error as *mut c_int,
-                                  path,
-                                  buf,
-                                  bufsz);
+    let status = u_readlink_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        path,
+        buf,
+        bufsz,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1163,10 +1443,12 @@
 pub unsafe fn symlink(path1: *const c_char, path2: *const c_char) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_symlink_ocall(&mut result as *mut c_int,
-                                 &mut error as *mut c_int,
-                                 path1,
-                                 path2);
+    let status = u_symlink_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        path1,
+        path2,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1182,12 +1464,20 @@
 pub unsafe fn realpath(pathname: *const c_char) -> *mut c_char {
     let mut result: *mut c_char = ptr::null_mut();
     let mut error: c_int = 0;
-    let status = u_realpath_ocall(&mut result as *mut *mut c_char,
-                                  &mut error as *mut c_int,
-                                  pathname);
+    let status = u_realpath_ocall(
+        &mut result as *mut *mut c_char,
+        &mut error as *mut c_int,
+        pathname,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
-        if result.is_null() {
+        if !result.is_null() {
+            let len: usize = strlen(result) + 1;
+            if sgx_is_outside_enclave(result as *const c_void, len) == 0 {
+                set_errno(ESGX);
+                result = ptr::null_mut();
+            }
+        } else {
             set_errno(error);
         }
     } else {
@@ -1200,10 +1490,12 @@
 pub unsafe fn mkdir(pathname: *const c_char, mode: mode_t) -> c_int {
     let mut error: c_int = 0;
     let mut result: c_int = 0;
-    let status = u_mkdir_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               pathname,
-                               mode);
+    let status = u_mkdir_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        pathname,
+        mode,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1219,9 +1511,11 @@
 pub unsafe fn rmdir(pathname: *const c_char) -> c_int {
     let mut error: c_int = 0;
     let mut result: c_int = 0;
-    let status = u_rmdir_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               pathname);
+    let status = u_rmdir_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        pathname,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1237,9 +1531,11 @@
 pub unsafe fn opendir(pathname: *const c_char) -> *mut DIR {
     let mut result: *mut DIR = ptr::null_mut();
     let mut error: c_int = 0;
-    let status = u_opendir_ocall(&mut result as *mut *mut DIR,
-                                 &mut error as *mut c_int,
-                                 pathname);
+    let status = u_opendir_ocall(
+        &mut result as *mut *mut DIR,
+        &mut error as *mut c_int,
+        pathname,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result.is_null() {
@@ -1252,14 +1548,14 @@
     result
 }
 
-pub unsafe fn readdir64_r(dirp: *mut DIR,
-                          entry: *mut dirent64,
-                          dirresult: *mut *mut dirent64) -> c_int {
+pub unsafe fn readdir64_r(
+    dirp: *mut DIR,
+    entry: *mut dirent64,
+    dirresult: *mut *mut dirent64,
+) -> c_int {
     let mut result: c_int = 0;
-    let status = u_readdir64_r_ocall(&mut result as *mut c_int,
-                                     dirp,
-                                     entry,
-                                     dirresult);
+    let status = u_readdir64_r_ocall(&mut result as *mut c_int, dirp, entry, dirresult);
+
     if status == sgx_status_t::SGX_SUCCESS && result == 0 {
         let dir_ret = *dirresult;
         if !dir_ret.is_null() {
@@ -1273,31 +1569,11 @@
     }
     result
 }
-                           
+
 pub unsafe fn closedir(dirp: *mut DIR) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_closedir_ocall(&mut result as *mut c_int,
-                                  &mut error as *mut c_int,
-                                  dirp);
-
-    if status == sgx_status_t::SGX_SUCCESS {
-        if result == -1 {
-            set_errno(error);
-        }
-    } else {
-        set_errno(ESGX);
-        result = -1;
-    }
-    result
-}   
-
-pub unsafe fn dirfd(dirp: *mut DIR) -> c_int {
-    let mut result: c_int = 0;
-    let mut error: c_int = 0;
-    let status = u_dirfd_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               dirp);
+    let status = u_closedir_ocall(&mut result as *mut c_int, &mut error as *mut c_int, dirp);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1310,18 +1586,38 @@
     result
 }
 
-pub unsafe fn fstatat64(dirfd: c_int,
-                        pathname: *const c_char,
-                        buf: *mut stat64,
-                        flags: c_int) -> c_int {
+pub unsafe fn dirfd(dirp: *mut DIR) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fstatat64_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   dirfd,
-                                   pathname,
-                                   buf,
-                                   flags);
+    let status = u_dirfd_ocall(&mut result as *mut c_int, &mut error as *mut c_int, dirp);
+
+    if status == sgx_status_t::SGX_SUCCESS {
+        if result == -1 {
+            set_errno(error);
+        }
+    } else {
+        set_errno(ESGX);
+        result = -1;
+    }
+    result
+}
+
+pub unsafe fn fstatat64(
+    dirfd: c_int,
+    pathname: *const c_char,
+    buf: *mut stat64,
+    flags: c_int,
+) -> c_int {
+    let mut result: c_int = 0;
+    let mut error: c_int = 0;
+    let status = u_fstatat64_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        dirfd,
+        pathname,
+        buf,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1342,7 +1638,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if count >= usize::max_value() {
+    if count >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -1353,16 +1649,18 @@
         malloc(count)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     tmp_buf.write_bytes(0_u8, count);
 
-    let status = u_read_ocall(&mut result as *mut ssize_t,
-                              &mut error as *mut c_int,
-                              fd,
-                              tmp_buf,
-                              count);
+    let status = u_read_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmp_buf,
+        count,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1374,7 +1672,11 @@
     }
 
     if result != -1 {
-        ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap_or(0)));
+        ptr::copy_nonoverlapping(
+            tmp_buf as *const u8,
+            buf as *mut u8,
+            cmp::min(count, result.try_into().unwrap_or(0)),
+        );
     }
     if count <= MAX_OCALL_ALLOC_SIZE {
         sgx_ocfree();
@@ -1392,7 +1694,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if count >= usize::max_value() {
+    if count >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -1403,17 +1705,19 @@
         malloc(count)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     tmp_buf.write_bytes(0_u8, count);
 
-    let status = u_pread64_ocall(&mut result as *mut ssize_t,
-                                 &mut error as *mut c_int,
-                                 fd,
-                                 tmp_buf,
-                                 count,
-                                 offset);
+    let status = u_pread64_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmp_buf,
+        count,
+        offset,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1425,7 +1729,11 @@
     }
 
     if result != -1 {
-        ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap_or(0)));
+        ptr::copy_nonoverlapping(
+            tmp_buf as *const u8,
+            buf as *mut u8,
+            cmp::min(count, result.try_into().unwrap_or(0)),
+        );
     }
     if count <= MAX_OCALL_ALLOC_SIZE {
         sgx_ocfree();
@@ -1439,46 +1747,66 @@
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
-    let mut iosize: usize = 0;
+    let mut total_size: usize = 0;
 
-    if iov.is_null() || iovcnt <= 0 ||
-       sgx_is_within_enclave(iov as *const c_void, iovcnt as usize * mem::size_of::<iovec>()) == 0 {
+    if iov.is_null()
+        || iovcnt <= 0
+        || sgx_is_within_enclave(
+            iov as *const c_void,
+            iovcnt as usize * mem::size_of::<iovec>(),
+        ) == 0
+    {
         set_errno(EINVAL);
         return -1;
     }
 
     let v = slice::from_raw_parts(iov, iovcnt as usize);
     for io in v {
-        if !io.iov_base.is_null() && io.iov_len > 0 &&
-            sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-            iosize += io.iov_len as usize;
+        if !io.iov_base.is_null()
+            && io.iov_len > 0
+            && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+        {
+            if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                total_size = io_size;
+            } else {
+                set_errno(EINVAL);
+                return -1;
+            }
         } else {
             set_errno(EINVAL);
             return -1;
         }
     }
 
-    let iobase = malloc(iosize) as *mut u8;
+    let iobase = if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocalloc(total_size)
+    } else {
+        malloc(total_size)
+    } as *mut u8;
     if iobase.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
-    iobase.write_bytes(0_u8, iosize);
+    iobase.write_bytes(0_u8, total_size);
 
     let mut tmpiovec: Vec<iovec> = Vec::with_capacity(iovcnt as usize);
     ptr = iobase;
     for io in v {
-        let tmpiov = iovec{iov_base: ptr as *mut c_void,
-                           iov_len: io.iov_len};
+        let tmpiov = iovec {
+            iov_base: ptr as *mut c_void,
+            iov_len: io.iov_len,
+        };
         tmpiovec.push(tmpiov);
-        ptr = ptr.add(io.iov_len as usize);
+        ptr = ptr.add(io.iov_len);
     }
 
-    let status = u_readv_ocall(&mut result as *mut ssize_t,
-                               &mut error as *mut c_int,
-                               fd,
-                               tmpiovec.as_slice().as_ptr(),
-                               iovcnt);
+    let status = u_readv_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmpiovec.as_slice().as_ptr(),
+        iovcnt,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1490,7 +1818,7 @@
     }
 
     if result != -1 {
-        let mut remaining_bytes : usize = result.try_into().unwrap_or(0);
+        let mut remaining_bytes: usize = result.try_into().unwrap_or(0);
         for i in 0..v.len() {
             if remaining_bytes == 0 {
                 break;
@@ -1500,12 +1828,17 @@
             ptr::copy_nonoverlapping(
                 tmpiovec[i].iov_base as *const u8,
                 v[i].iov_base as *mut u8,
-                cmp::min(v[i].iov_len, remaining_bytes));
+                cmp::min(v[i].iov_len, remaining_bytes),
+            );
             remaining_bytes = remaining_bytes.saturating_sub(v[i].iov_len);
         }
     }
 
-    free(iobase as *mut c_void);
+    if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocfree();
+    } else {
+        free(iobase as *mut c_void);
+    }
     result
 }
 
@@ -1513,47 +1846,67 @@
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
-    let mut iosize: usize = 0;
+    let mut total_size: usize = 0;
 
-    if iov.is_null() || iovcnt <= 0 ||
-       sgx_is_within_enclave(iov as *const c_void, iovcnt as usize * mem::size_of::<iovec>()) == 0 {
+    if iov.is_null()
+        || iovcnt <= 0
+        || sgx_is_within_enclave(
+            iov as *const c_void,
+            iovcnt as usize * mem::size_of::<iovec>(),
+        ) == 0
+    {
         set_errno(EINVAL);
         return -1;
     }
 
     let v = slice::from_raw_parts(iov, iovcnt as usize);
     for io in v {
-        if !io.iov_base.is_null() && io.iov_len > 0 &&
-            sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-            iosize += io.iov_len as usize;
+        if !io.iov_base.is_null()
+            && io.iov_len > 0
+            && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+        {
+            if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                total_size = io_size;
+            } else {
+                set_errno(EINVAL);
+                return -1;
+            }
         } else {
             set_errno(EINVAL);
             return -1;
         }
     }
 
-    let iobase = malloc(iosize) as *mut u8;
+    let iobase = if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocalloc(total_size)
+    } else {
+        malloc(total_size)
+    } as *mut u8;
     if iobase.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
-    iobase.write_bytes(0_u8, iosize);
+    iobase.write_bytes(0_u8, total_size);
 
     let mut tmpiovec: Vec<iovec> = Vec::with_capacity(iovcnt as usize);
     ptr = iobase;
     for io in v {
-        let tmpiov = iovec{iov_base: ptr as *mut c_void,
-                           iov_len: io.iov_len};
+        let tmpiov = iovec {
+            iov_base: ptr as *mut c_void,
+            iov_len: io.iov_len,
+        };
         tmpiovec.push(tmpiov);
-        ptr = ptr.add(io.iov_len as usize);
+        ptr = ptr.add(io.iov_len);
     }
 
-    let status = u_preadv64_ocall(&mut result as *mut ssize_t,
-                               &mut error as *mut c_int,
-                               fd,
-                               tmpiovec.as_slice().as_ptr(),
-                               iovcnt,
-                               offset);
+    let status = u_preadv64_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmpiovec.as_slice().as_ptr(),
+        iovcnt,
+        offset,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1565,17 +1918,25 @@
     }
 
     if result != -1 {
-        let mut remaining_bytes : usize = result.try_into().unwrap_or(0);
+        let mut remaining_bytes: usize = result.try_into().unwrap_or(0);
         for i in 0..v.len() {
             if remaining_bytes == 0 {
                 break;
             }
-            ptr::copy_nonoverlapping(tmpiovec[i].iov_base as *const u8, v[i].iov_base as *mut u8, cmp::min(v[i].iov_len, remaining_bytes));
+            ptr::copy_nonoverlapping(
+                tmpiovec[i].iov_base as *const u8,
+                v[i].iov_base as *mut u8,
+                cmp::min(v[i].iov_len, remaining_bytes),
+            );
             remaining_bytes = remaining_bytes.saturating_sub(v[i].iov_len);
         }
     }
 
-    free(iobase as *mut c_void);
+    if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocfree();
+    } else {
+        free(iobase as *mut c_void);
+    }
     result
 }
 
@@ -1587,7 +1948,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if count >= usize::max_value() {
+    if count >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -1598,16 +1959,18 @@
         malloc(count)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     ptr::copy_nonoverlapping(buf as *const u8, tmp_buf as *mut u8, count);
 
-    let status = u_write_ocall(&mut result as *mut ssize_t,
-                               &mut error as *mut c_int,
-                               fd,
-                               tmp_buf,
-                               count);
+    let status = u_write_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmp_buf,
+        count,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1634,7 +1997,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if count >= usize::max_value() {
+    if count >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -1645,17 +2008,19 @@
         malloc(count)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     ptr::copy_nonoverlapping(buf as *const u8, tmp_buf as *mut u8, count);
 
-    let status = u_pwrite64_ocall(&mut result as *mut ssize_t,
-                                  &mut error as *mut c_int,
-                                  fd,
-                                  tmp_buf,
-                                  count,
-                                  offset);
+    let status = u_pwrite64_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmp_buf,
+        count,
+        offset,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1678,47 +2043,71 @@
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
-    let mut iosize: usize = 0;
+    let mut total_size: usize = 0;
 
-    if iov.is_null() || iovcnt <= 0 ||
-       sgx_is_within_enclave(iov as *const c_void, iovcnt as usize * mem::size_of::<iovec>()) == 0 {
+    if iov.is_null()
+        || iovcnt <= 0
+        || sgx_is_within_enclave(
+            iov as *const c_void,
+            iovcnt as usize * mem::size_of::<iovec>(),
+        ) == 0
+    {
         set_errno(EINVAL);
         return -1;
     }
 
     let v = slice::from_raw_parts(iov, iovcnt as usize);
     for io in v {
-        if !io.iov_base.is_null() && io.iov_len > 0 &&
-            sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-            iosize += io.iov_len as usize;
+        if !io.iov_base.is_null()
+            && io.iov_len > 0
+            && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+        {
+            if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                total_size = io_size;
+            } else {
+                set_errno(EINVAL);
+                return -1;
+            }
         } else {
             set_errno(EINVAL);
             return -1;
         }
     }
 
-    let iobase = malloc(iosize) as *mut u8;
+    let iobase = if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocalloc(total_size)
+    } else {
+        malloc(total_size)
+    } as *mut u8;
     if iobase.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
-    iobase.write_bytes(0_u8, iosize);
+    iobase.write_bytes(0_u8, total_size);
 
     let mut tmpiovec: Vec<iovec> = Vec::with_capacity(iovcnt as usize);
     ptr = iobase;
     for io in v {
-        let tmpiov = iovec{iov_base: ptr as *mut c_void,
-                           iov_len: io.iov_len};
-        ptr::copy_nonoverlapping(io.iov_base as *const u8, tmpiov.iov_base as *mut u8, io.iov_len as usize);
+        let tmpiov = iovec {
+            iov_base: ptr as *mut c_void,
+            iov_len: io.iov_len,
+        };
+        ptr::copy_nonoverlapping(
+            io.iov_base as *const u8,
+            tmpiov.iov_base as *mut u8,
+            io.iov_len as usize,
+        );
         tmpiovec.push(tmpiov);
-        ptr = ptr.add(io.iov_len as usize);
+        ptr = ptr.add(io.iov_len);
     }
 
-    let status = u_writev_ocall(&mut result as *mut ssize_t,
-                                &mut error as *mut c_int,
-                                fd,
-                                tmpiovec.as_slice().as_ptr(),
-                                iovcnt);
+    let status = u_writev_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmpiovec.as_slice().as_ptr(),
+        iovcnt,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1729,7 +2118,11 @@
         result = -1;
     }
 
-    free(iobase as *mut c_void);
+    if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocfree();
+    } else {
+        free(iobase as *mut c_void);
+    }
     result
 }
 
@@ -1737,48 +2130,72 @@
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
-    let mut iosize: usize = 0;
+    let mut total_size: usize = 0;
 
-    if iov.is_null() || iovcnt <= 0 ||
-       sgx_is_within_enclave(iov as *const c_void, iovcnt as usize * mem::size_of::<iovec>()) ==0 {
+    if iov.is_null()
+        || iovcnt <= 0
+        || sgx_is_within_enclave(
+            iov as *const c_void,
+            iovcnt as usize * mem::size_of::<iovec>(),
+        ) == 0
+    {
         set_errno(EINVAL);
         return -1;
     }
 
     let v = slice::from_raw_parts(iov, iovcnt as usize);
     for io in v {
-        if !io.iov_base.is_null() && io.iov_len > 0 &&
-            sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-            iosize += io.iov_len as usize;
+        if !io.iov_base.is_null()
+            && io.iov_len > 0
+            && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+        {
+            if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                total_size = io_size;
+            } else {
+                set_errno(EINVAL);
+                return -1;
+            }
         } else {
             set_errno(EINVAL);
             return -1;
         }
     }
 
-    let iobase = malloc(iosize) as *mut u8;
+    let iobase = if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocalloc(total_size)
+    } else {
+        malloc(total_size)
+    } as *mut u8;
     if iobase.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
-    iobase.write_bytes(0_u8, iosize);
+    iobase.write_bytes(0_u8, total_size);
 
     let mut tmpiovec: Vec<iovec> = Vec::with_capacity(iovcnt as usize);
     ptr = iobase;
     for io in v {
-        let tmpiov = iovec{iov_base: ptr as *mut c_void,
-                           iov_len: io.iov_len};
-        ptr::copy_nonoverlapping(io.iov_base as *const u8, tmpiov.iov_base as *mut u8, io.iov_len as usize);
+        let tmpiov = iovec {
+            iov_base: ptr as *mut c_void,
+            iov_len: io.iov_len,
+        };
+        ptr::copy_nonoverlapping(
+            io.iov_base as *const u8,
+            tmpiov.iov_base as *mut u8,
+            io.iov_len as usize,
+        );
         tmpiovec.push(tmpiov);
-        ptr = ptr.add(io.iov_len as usize);
+        ptr = ptr.add(io.iov_len);
     }
 
-    let status = u_pwritev64_ocall(&mut result as *mut ssize_t,
-                                   &mut error as *mut c_int,
-                                   fd,
-                                   tmpiovec.as_slice().as_ptr(),
-                                   iovcnt,
-                                   offset);
+    let status = u_pwritev64_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        fd,
+        tmpiovec.as_slice().as_ptr(),
+        iovcnt,
+        offset,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1789,17 +2206,18 @@
         result = -1;
     }
 
-    free(iobase as *mut c_void);
+    if total_size <= MAX_OCALL_ALLOC_SIZE {
+        sgx_ocfree();
+    } else {
+        free(iobase as *mut c_void);
+    }
     result
 }
 
 pub unsafe fn fcntl_arg0(fd: c_int, cmd: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fcntl_arg0_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    fd,
-                                    cmd);
+    let status = u_fcntl_arg0_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd, cmd);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1815,11 +2233,13 @@
 pub unsafe fn fcntl_arg1(fd: c_int, cmd: c_int, arg: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_fcntl_arg1_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    fd,
-                                    cmd,
-                                    arg);
+    let status = u_fcntl_arg1_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        cmd,
+        arg,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1835,10 +2255,12 @@
 pub unsafe fn ioctl_arg0(fd: c_int, request: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_ioctl_arg0_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    fd,
-                                    request);
+    let status = u_ioctl_arg0_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        request,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1854,11 +2276,13 @@
 pub unsafe fn ioctl_arg1(fd: c_int, request: c_int, arg: *mut c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_ioctl_arg1_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    fd,
-                                    request,
-                                    arg);
+    let status = u_ioctl_arg1_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fd,
+        request,
+        arg,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1874,9 +2298,7 @@
 pub unsafe fn close(fd: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_close_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               fd);
+    let status = u_close_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fd);
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1889,13 +2311,16 @@
     result
 }
 
+// time
 pub unsafe fn clock_gettime(clk_id: clockid_t, tp: *mut timespec) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_clock_gettime_ocall(&mut result as *mut c_int,
-                                       &mut error as *mut c_int,
-                                       clk_id,
-                                       tp);
+    let status = u_clock_gettime_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        clk_id,
+        tp,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1911,11 +2336,13 @@
 pub unsafe fn socket(domain: c_int, ty: c_int, protocol: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_socket_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                domain,
-                                ty,
-                                protocol);
+    let status = u_socket_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        domain,
+        ty,
+        protocol,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1931,12 +2358,14 @@
 pub unsafe fn socketpair(domain: c_int, ty: c_int, protocol: c_int, sv: *mut c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_socketpair_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    domain,
-                                    ty,
-                                    protocol,
-                                    sv);
+    let status = u_socketpair_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        domain,
+        ty,
+        protocol,
+        sv,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1952,11 +2381,13 @@
 pub unsafe fn bind(sockfd: c_int, address: *const sockaddr, addrlen: socklen_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_bind_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              sockfd,
-                              address,
-                              addrlen);
+    let status = u_bind_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        address,
+        addrlen,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1972,10 +2403,12 @@
 pub unsafe fn listen(sockfd: c_int, backlog: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_listen_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                sockfd,
-                                backlog);
+    let status = u_listen_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        backlog,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -1993,12 +2426,14 @@
     let mut error: c_int = 0;
     let len_in: socklen_t = if !addrlen.is_null() { *addrlen } else { 0 };
     let mut len_out: socklen_t = 0 as socklen_t;
-    let status = u_accept_ocall(&mut result as *mut c_int,
-                                &mut error as *mut c_int,
-                                sockfd,
-                                addr,
-                                len_in, // This additional arg is just for EDL
-                                &mut len_out as *mut socklen_t);
+    let status = u_accept_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        addr,
+        len_in, // This additional arg is just for EDL
+        &mut len_out as *mut socklen_t,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2015,18 +2450,25 @@
     result
 }
 
-pub unsafe fn accept4(sockfd: c_int, addr: *mut sockaddr, addrlen: *mut socklen_t, flags: c_int) -> c_int {
+pub unsafe fn accept4(
+    sockfd: c_int,
+    addr: *mut sockaddr,
+    addrlen: *mut socklen_t,
+    flags: c_int,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
     let len_in: socklen_t = if !addrlen.is_null() { *addrlen } else { 0 };
     let mut len_out: socklen_t = 0 as socklen_t;
-    let status = u_accept4_ocall(&mut result as *mut c_int,
-                                 &mut error as *mut c_int,
-                                 sockfd,
-                                 addr,
-                                 len_in, // This additional arg is just for EDL
-                                 &mut len_out as *mut socklen_t,
-                                 flags);
+    let status = u_accept4_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        addr,
+        len_in, // This additional arg is just for EDL
+        &mut len_out as *mut socklen_t,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2046,11 +2488,13 @@
 pub unsafe fn connect(sockfd: c_int, address: *const sockaddr, addrlen: socklen_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_connect_ocall(&mut result as *mut c_int,
-                                 &mut error as *mut c_int,
-                                 sockfd,
-                                 address,
-                                 addrlen);
+    let status = u_connect_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        address,
+        addrlen,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2071,7 +2515,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if len >= usize::max_value() {
+    if len >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -2082,17 +2526,19 @@
         malloc(len)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     ptr::copy_nonoverlapping(buf as *const u8, tmp_buf as *mut u8, len);
 
-    let status = u_send_ocall(&mut result as *mut ssize_t,
-                              &mut error as *mut c_int,
-                              sockfd,
-                              tmp_buf,
-                              len,
-                              flags);
+    let status = u_send_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        tmp_buf,
+        len,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2111,12 +2557,14 @@
     result
 }
 
-pub unsafe fn sendto(sockfd: c_int,
-                     buf: *const c_void,
-                     len: size_t,
-                     flags: c_int,
-                     addr: *const sockaddr,
-                     addrlen: socklen_t) -> ssize_t {
+pub unsafe fn sendto(
+    sockfd: c_int,
+    buf: *const c_void,
+    len: size_t,
+    flags: c_int,
+    addr: *const sockaddr,
+    addrlen: socklen_t,
+) -> ssize_t {
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
 
@@ -2124,7 +2572,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if len >= usize::max_value() {
+    if len >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -2135,19 +2583,21 @@
         malloc(len)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     ptr::copy_nonoverlapping(buf as *const u8, tmp_buf as *mut u8, len);
 
-    let status = u_sendto_ocall(&mut result as *mut ssize_t,
-                                &mut error as *mut c_int,
-                                sockfd,
-                                tmp_buf,
-                                len,
-                                flags,
-                                addr,
-                                addrlen);
+    let status = u_sendto_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        tmp_buf,
+        len,
+        flags,
+        addr,
+        addrlen,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2169,7 +2619,7 @@
 pub unsafe fn sendmsg(sockfd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t {
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
-    let mut hdrsize: usize = 0;
+    let mut total_size: usize = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
 
     if msg.is_null() || sgx_is_within_enclave(msg as *const c_void, mem::size_of::<msghdr>()) == 0 {
@@ -2177,73 +2627,82 @@
         return -1;
     }
 
-    let mhdr: &msghdr = &*msg;
-    if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 &&
-        sgx_is_within_enclave(mhdr.msg_name, mhdr.msg_namelen as usize) != 0 {
-        hdrsize += mhdr.msg_namelen as usize;
-    }
+    let mhdr = &*msg;
+    let (msg_name, msg_namelen) = if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 {
+        (mhdr.msg_name, mhdr.msg_namelen)
+    } else {
+        (ptr::null_mut(), 0)
+    };
 
-    if !mhdr.msg_iov.is_null() && mhdr.msg_iovlen > 0 &&
-       sgx_is_within_enclave(mhdr.msg_iov as *const c_void, mhdr.msg_iovlen as usize * mem::size_of::<iovec>()) != 0 {
-
-        hdrsize += mhdr.msg_iovlen as usize * mem::size_of::<iovec>();
-        let v = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen as usize);
-        for io in v {
-            if !io.iov_base.is_null() && io.iov_len > 0 &&
-                sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-                hdrsize += io.iov_len as usize;
+    let iovecs = if !mhdr.msg_iov.is_null()
+        && mhdr.msg_iovlen > 0
+        && sgx_is_within_enclave(
+            mhdr.msg_iov as *const c_void,
+            mhdr.msg_iovlen * mem::size_of::<iovec>(),
+        ) != 0
+    {
+        let iovs = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen);
+        for io in iovs.iter() {
+            if !io.iov_base.is_null()
+                && io.iov_len > 0
+                && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+            {
+                if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                    total_size = io_size;
+                } else {
+                    set_errno(EINVAL);
+                    return -1;
+                }
             } else {
                 set_errno(EINVAL);
                 return -1;
             }
         }
-    }
+        iovs
+    } else {
+        set_errno(EINVAL);
+        return -1;
+    };
 
-    if !mhdr.msg_control.is_null() &&  mhdr.msg_controllen > 0 &&
-        sgx_is_within_enclave(mhdr.msg_control, mhdr.msg_controllen as usize) != 0 {
-        hdrsize += mhdr.msg_controllen as usize;
-    }
+    let (msg_control, msg_controllen) = if !mhdr.msg_control.is_null() && mhdr.msg_controllen > 0 {
+        (mhdr.msg_control, mhdr.msg_controllen)
+    } else {
+        (ptr::null_mut(), 0)
+    };
 
-    let hdrbase = malloc(hdrsize) as *mut u8;
-    if hdrbase.is_null() {
-        set_errno(ENOMEM );
+    let io_base = malloc(total_size) as *mut u8;
+    if io_base.is_null() {
+        set_errno(ENOMEM);
         return -1;
     }
-    hdrbase.write_bytes(0_u8, hdrsize);
+    io_base.write_bytes(0_u8, total_size);
 
-    let mut tmpmsg: msghdr = mem::zeroed();
-    ptr = hdrbase;
-    if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 {
-        tmpmsg.msg_name = ptr as *mut c_void;
-        tmpmsg.msg_namelen = mhdr.msg_namelen;
-        ptr::copy_nonoverlapping(mhdr.msg_name as *const u8, tmpmsg.msg_name as *mut u8, mhdr.msg_namelen as usize);
-        ptr = ptr.add(mhdr.msg_namelen as usize);
-    }
+    ptr = io_base;
+    let io_data: Vec<iovec> = iovecs
+        .iter()
+        .map(|v| {
+            let iov = iovec {
+                iov_base: ptr as *mut c_void,
+                iov_len: v.iov_len,
+            };
+            ptr::copy_nonoverlapping(v.iov_base as *const u8, iov.iov_base as *mut u8, v.iov_len);
+            ptr = ptr.add(v.iov_len);
+            iov
+        })
+        .collect();
 
-    if !mhdr.msg_iov.is_null() && mhdr.msg_iovlen > 0 {
-        let tmpiov = slice::from_raw_parts_mut(ptr as *mut iovec, mhdr.msg_iovlen as usize);
-        ptr = ptr.add(mhdr.msg_iovlen as usize * mem::size_of::<iovec>());
-
-        let v = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen as usize);
-        for i in 0..v.len() {
-            tmpiov[i].iov_base = ptr as *mut c_void;
-            tmpiov[i].iov_len = v[i].iov_len;
-            ptr::copy_nonoverlapping(v[i].iov_base as *const u8, tmpiov[i].iov_base as *mut u8, v[i].iov_len as usize);
-            ptr = ptr.add(v[i].iov_len as usize);
-        }
-    }
-
-    if !mhdr.msg_control.is_null() &&  mhdr.msg_controllen > 0 {
-        tmpmsg.msg_control = ptr as *mut c_void;
-        tmpmsg.msg_controllen = mhdr.msg_controllen;
-        ptr::copy_nonoverlapping(mhdr.msg_control as *const u8, tmpmsg.msg_control as *mut u8, mhdr.msg_controllen as usize);
-    }
-
-    let status = u_sendmsg_ocall(&mut result as *mut ssize_t,
-                                 &mut error as *mut c_int,
-                                 sockfd,
-                                 &tmpmsg as *const msghdr,
-                                 flags);
+    let status = u_sendmsg_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        msg_name as *const c_void,
+        msg_namelen as socklen_t,
+        io_data.as_ptr(),
+        io_data.len(),
+        msg_control as *const c_void,
+        msg_controllen,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2254,7 +2713,7 @@
         result = -1;
     }
 
-    free(hdrbase as *mut c_void);
+    free(io_base as *mut c_void);
     result
 }
 
@@ -2266,7 +2725,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if len >= usize::max_value() {
+    if len >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -2277,17 +2736,19 @@
         malloc(len)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     tmp_buf.write_bytes(0_u8, len);
 
-    let status = u_recv_ocall(&mut result as *mut ssize_t,
-                              &mut error as *mut c_int,
-                              sockfd,
-                              tmp_buf,
-                              len,
-                              flags);
+    let status = u_recv_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        tmp_buf,
+        len,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2309,12 +2770,14 @@
     result
 }
 
-pub unsafe fn recvfrom(sockfd: c_int,
-                       buf: *mut c_void,
-                       len: size_t,
-                       flags: c_int,
-                       addr: *mut sockaddr,
-                       addrlen: *mut socklen_t) -> ssize_t {
+pub unsafe fn recvfrom(
+    sockfd: c_int,
+    buf: *mut c_void,
+    len: size_t,
+    flags: c_int,
+    addr: *mut sockaddr,
+    addrlen: *mut socklen_t,
+) -> ssize_t {
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
     let len_in: socklen_t = if !addrlen.is_null() { *addrlen } else { 0 };
@@ -2324,7 +2787,7 @@
         set_errno(EINVAL);
         return -1;
     }
-    if len >= usize::max_value() {
+    if len >= usize::MAX {
         set_errno(EINVAL);
         return -1;
     }
@@ -2335,20 +2798,22 @@
         malloc(len)
     };
     if tmp_buf.is_null() {
-        set_errno(ENOMEM );
+        set_errno(ENOMEM);
         return -1;
     }
     tmp_buf.write_bytes(0_u8, len);
 
-    let status = u_recvfrom_ocall(&mut result as *mut ssize_t,
-                                  &mut error as *mut c_int,
-                                  sockfd,
-                                  tmp_buf,
-                                  len,
-                                  flags,
-                                  addr,
-                                  len_in, // This additional arg is just for EDL
-                                  &mut len_out as *mut socklen_t);
+    let status = u_recvfrom_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        tmp_buf,
+        len,
+        flags,
+        addr,
+        len_in, // This additional arg is just for EDL
+        &mut len_out as *mut socklen_t,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2377,7 +2842,7 @@
 pub unsafe fn recvmsg(sockfd: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t {
     let mut result: ssize_t = 0;
     let mut error: c_int = 0;
-    let mut hdrsize: usize = 0;
+    let mut total_size: usize = 0;
     let mut ptr: *mut u8 = ptr::null_mut();
 
     if msg.is_null() || sgx_is_within_enclave(msg as *const c_void, mem::size_of::<msghdr>()) == 0 {
@@ -2385,72 +2850,88 @@
         return -1;
     }
 
-    let mhdr: &mut msghdr = &mut *msg;
-    if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 &&
-        sgx_is_within_enclave(mhdr.msg_name, mhdr.msg_namelen as usize) != 0 {
-        hdrsize += mhdr.msg_namelen as usize;
-    }
+    let mhdr = &mut *msg;
+    let (msg_name, msg_namelen) = if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 {
+        (mhdr.msg_name, mhdr.msg_namelen)
+    } else {
+        (ptr::null_mut(), 0)
+    };
 
-    if !mhdr.msg_iov.is_null() && mhdr.msg_iovlen > 0 &&
-       sgx_is_within_enclave(mhdr.msg_iov as *const c_void, mhdr.msg_iovlen as usize * mem::size_of::<iovec>()) != 0 {
-
-        hdrsize += mhdr.msg_iovlen as usize * mem::size_of::<iovec>();
-        let v = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen as usize);
-        for io in v {
-            if !io.iov_base.is_null() && io.iov_len > 0 &&
-                sgx_is_within_enclave(io.iov_base, io.iov_len as usize) != 0 {
-                hdrsize += io.iov_len as usize;
+    let iovecs = if !mhdr.msg_iov.is_null()
+        && mhdr.msg_iovlen > 0
+        && sgx_is_within_enclave(
+            mhdr.msg_iov as *const c_void,
+            mhdr.msg_iovlen * mem::size_of::<iovec>(),
+        ) != 0
+    {
+        let iovs = slice::from_raw_parts_mut(mhdr.msg_iov, mhdr.msg_iovlen);
+        for io in iovs.iter() {
+            if !io.iov_base.is_null()
+                && io.iov_len > 0
+                && sgx_is_within_enclave(io.iov_base, io.iov_len) != 0
+            {
+                if let Some(io_size) = total_size.checked_add(io.iov_len) {
+                    total_size = io_size;
+                } else {
+                    set_errno(EINVAL);
+                    return -1;
+                }
             } else {
                 set_errno(EINVAL);
                 return -1;
             }
         }
-    }
+        iovs
+    } else {
+        set_errno(EINVAL);
+        return -1;
+    };
 
-    if !mhdr.msg_control.is_null() &&  mhdr.msg_controllen > 0 &&
-        sgx_is_within_enclave(mhdr.msg_control, mhdr.msg_controllen as usize) != 0 {
-        hdrsize += mhdr.msg_controllen as usize;
-    }
+    let (msg_control, msg_controllen) = if !mhdr.msg_control.is_null() && mhdr.msg_controllen > 0 {
+        (mhdr.msg_control, mhdr.msg_controllen)
+    } else {
+        (ptr::null_mut(), 0)
+    };
 
-    let hdrbase = malloc(hdrsize) as *mut u8;
-    if hdrbase.is_null() {
-        set_errno(ENOMEM );
+    let io_base = malloc(total_size) as *mut u8;
+    if io_base.is_null() {
+        set_errno(ENOMEM);
         return -1;
     }
-    hdrbase.write_bytes(0_u8, hdrsize);
+    io_base.write_bytes(0_u8, total_size);
 
-    let mut tmpmsg: msghdr = mem::zeroed();
-    ptr = hdrbase;
-    if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 {
-        tmpmsg.msg_name = ptr as *mut c_void;
-        tmpmsg.msg_namelen = mhdr.msg_namelen;
-        ptr::copy_nonoverlapping(mhdr.msg_name as *const u8, tmpmsg.msg_name as *mut u8, mhdr.msg_namelen as usize);
-        ptr = ptr.add(mhdr.msg_namelen as usize);
-    }
+    let mut msg_namelen_out = 0_u32;
+    let mut msg_controllen_out = 0_usize;
+    let mut msg_flags = 0;
 
-    if !mhdr.msg_iov.is_null() && mhdr.msg_iovlen > 0 {
-        let tmpiov = slice::from_raw_parts_mut(ptr as *mut iovec, mhdr.msg_iovlen as usize);
-        ptr = ptr.add(mhdr.msg_iovlen as usize * mem::size_of::<iovec>());
+    ptr = io_base;
+    let mut io_data: Vec<iovec> = iovecs
+        .iter()
+        .map(|v| {
+            let iov = iovec {
+                iov_base: ptr as *mut c_void,
+                iov_len: v.iov_len,
+            };
+            ptr = ptr.add(v.iov_len);
+            iov
+        })
+        .collect();
 
-        let v = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen as usize);
-        for i in 0..v.len() {
-            tmpiov[i].iov_base = ptr as *mut c_void;
-            tmpiov[i].iov_len = v[i].iov_len;
-            ptr = ptr.add(v[i].iov_len as usize);
-        }
-    }
-
-    if !mhdr.msg_control.is_null() &&  mhdr.msg_controllen > 0 {
-        tmpmsg.msg_control = ptr as *mut c_void;
-        tmpmsg.msg_controllen = mhdr.msg_controllen;
-        ptr::copy_nonoverlapping(mhdr.msg_control as *const u8, tmpmsg.msg_control as *mut u8, mhdr.msg_controllen as usize);
-    }
-
-    let status = u_recvmsg_ocall(&mut result as *mut ssize_t,
-                                 &mut error as *mut c_int,
-                                 sockfd,
-                                 &mut tmpmsg as *mut msghdr,
-                                 flags);
+    let status = u_recvmsg_ocall(
+        &mut result as *mut ssize_t,
+        &mut error as *mut c_int,
+        sockfd,
+        msg_name as *mut c_void,
+        msg_namelen as socklen_t,
+        &mut msg_namelen_out as *mut socklen_t,
+        io_data.as_mut_ptr(),
+        io_data.len(),
+        msg_control as *mut c_void,
+        msg_controllen,
+        &mut msg_controllen_out as *mut usize,
+        &mut msg_flags as *mut i32,
+        flags,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2461,44 +2942,54 @@
         result = -1;
     }
 
-    ptr = hdrbase;
-    if !mhdr.msg_name.is_null() && mhdr.msg_namelen > 0 {
-        ptr = ptr.add(mhdr.msg_namelen as usize);
+    if msg_namelen_out > msg_namelen || msg_controllen_out > msg_controllen {
+        set_errno(ESGX);
+        result = -1;
     }
 
-    if !mhdr.msg_iov.is_null() && mhdr.msg_iovlen > 0 {
-        let tmpiov = slice::from_raw_parts_mut(ptr as *mut iovec, mhdr.msg_iovlen as usize);
-        ptr = ptr.add(mhdr.msg_iovlen as usize * mem::size_of::<iovec>());
-
-        let v = slice::from_raw_parts(mhdr.msg_iov, mhdr.msg_iovlen as usize);
-        for i in 0..v.len() {
-            ptr::copy_nonoverlapping(tmpiov[i].iov_base as *const u8, v[i].iov_base as *mut u8, v[i].iov_len as usize);
-            ptr = ptr.add(v[i].iov_len as usize);
+    if result >= 0 {
+        let nrecv = result.try_into().unwrap_or(0);
+        let mut remaining_bytes = cmp::min(nrecv, total_size);
+        for i in 0..iovecs.len() {
+            let copy_len = cmp::min(iovecs[i].iov_len, remaining_bytes);
+            ptr::copy_nonoverlapping(
+                io_data[i].iov_base as *const u8,
+                iovecs[i].iov_base as *mut u8,
+                copy_len,
+            );
+            remaining_bytes -= copy_len;
+            if remaining_bytes == 0 {
+                break;
+            }
         }
+
+        mhdr.msg_namelen = msg_namelen_out as u32;
+        mhdr.msg_controllen = msg_controllen_out;
+        mhdr.msg_flags = msg_flags;
     }
 
-    if !mhdr.msg_control.is_null() &&  mhdr.msg_controllen > 0 {
-        ptr::copy_nonoverlapping(tmpmsg.msg_control as *const u8, mhdr.msg_control as *mut u8, mhdr.msg_controllen as usize);
-    }
-
-    free(hdrbase as *mut c_void);
+    free(io_base as *mut c_void);
     result
 }
 
-pub unsafe fn setsockopt(sockfd: c_int,
-                         level: c_int,
-                         optname: c_int,
-                         optval: *const c_void,
-                         optlen: socklen_t) -> c_int {
+pub unsafe fn setsockopt(
+    sockfd: c_int,
+    level: c_int,
+    optname: c_int,
+    optval: *const c_void,
+    optlen: socklen_t,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_setsockopt_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    sockfd,
-                                    level,
-                                    optname,
-                                    optval,
-                                    optlen);
+    let status = u_setsockopt_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        level,
+        optname,
+        optval,
+        optlen,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2511,24 +3002,28 @@
     result
 }
 
-pub unsafe fn getsockopt(sockfd: c_int,
-                         level: c_int,
-                         optname: c_int,
-                         optval: *mut c_void,
-                         optlen: *mut socklen_t) -> c_int {
+pub unsafe fn getsockopt(
+    sockfd: c_int,
+    level: c_int,
+    optname: c_int,
+    optval: *mut c_void,
+    optlen: *mut socklen_t,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
     let len_in: socklen_t = if !optlen.is_null() { *optlen } else { 0 };
     let mut len_out: socklen_t = 0 as socklen_t;
 
-    let status = u_getsockopt_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    sockfd,
-                                    level,
-                                    optname,
-                                    optval,
-                                    len_in,
-                                    &mut len_out as *mut socklen_t);
+    let status = u_getsockopt_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        level,
+        optname,
+        optval,
+        len_in,
+        &mut len_out as *mut socklen_t,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2550,12 +3045,14 @@
     let mut error: c_int = 0;
     let len_in: socklen_t = if !addrlen.is_null() { *addrlen } else { 0 };
     let mut len_out: socklen_t = 0 as socklen_t;
-    let status = u_getpeername_ocall(&mut result as *mut c_int,
-                                     &mut error as *mut c_int,
-                                     sockfd,
-                                     address,
-                                     len_in,
-                                     &mut len_out as *mut socklen_t);
+    let status = u_getpeername_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        address,
+        len_in,
+        &mut len_out as *mut socklen_t,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2577,12 +3074,14 @@
     let mut error: c_int = 0;
     let len_in: socklen_t = if !addrlen.is_null() { *addrlen } else { 0 };
     let mut len_out: socklen_t = 0 as socklen_t;
-    let status = u_getsockname_ocall(&mut result as *mut c_int,
-                                     &mut error as *mut c_int,
-                                     sockfd,
-                                     address,
-                                     len_in,
-                                     &mut len_out as *mut socklen_t);
+    let status = u_getsockname_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        address,
+        len_in,
+        &mut len_out as *mut socklen_t,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2602,10 +3101,12 @@
 pub unsafe fn shutdown(sockfd: c_int, how: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_shutdown_ocall(&mut result as *mut c_int,
-                                  &mut error as *mut c_int,
-                                  sockfd,
-                                  how);
+    let status = u_shutdown_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        sockfd,
+        how,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2618,10 +3119,12 @@
     result
 }
 
-pub unsafe fn getaddrinfo(node: *const c_char,
-                          service: *const c_char,
-                          hints: *const addrinfo,
-                          res: *mut *mut addrinfo) -> c_int {
+pub unsafe fn getaddrinfo(
+    node: *const c_char,
+    service: *const c_char,
+    hints: *const addrinfo,
+    res: *mut *mut addrinfo,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
     let mut ret_res: *mut addrinfo = ptr::null_mut();
@@ -2649,18 +3152,25 @@
         }
     };
 
-    let status = u_getaddrinfo_ocall(&mut result as *mut c_int,
-                                     &mut error as *mut c_int,
-                                     node,
-                                     service,
-                                     &hint as *const addrinfo,
-                                     &mut ret_res as *mut *mut addrinfo);
+    let status = u_getaddrinfo_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        node,
+        service,
+        &hint as *const addrinfo,
+        &mut ret_res as *mut *mut addrinfo,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == 0 {
             *res = ptr::null_mut();
             let mut cur_ptr: *mut addrinfo = ret_res;
             let mut addrinfo_vec: Vec<Box<addrinfo>> = Vec::new();
             while cur_ptr != ptr::null_mut() {
+                if sgx_is_outside_enclave(cur_ptr as *const c_void, mem::size_of::<addrinfo>()) == 0
+                {
+                    result = EAI_SYSTEM;
+                    break;
+                }
                 let cur: &addrinfo = &*cur_ptr;
                 let mut info = addrinfo {
                     ai_flags: cur.ai_flags,
@@ -2674,8 +3184,15 @@
                 };
 
                 if !cur.ai_addr.is_null() && cur.ai_addrlen > 0 {
+                    if sgx_is_outside_enclave(cur.ai_addr as *const c_void, cur.ai_addrlen as usize)
+                        == 0
+                    {
+                        result = EAI_SYSTEM;
+                        break;
+                    }
                     let mut addr_vec = vec![0u8; cur.ai_addrlen as usize];
-                    let addr_slice: &[u8] = slice::from_raw_parts(cur.ai_addr as *const u8, cur.ai_addrlen as usize);
+                    let addr_slice: &[u8] =
+                        slice::from_raw_parts(cur.ai_addr as *const u8, cur.ai_addrlen as usize);
                     addr_vec.copy_from_slice(addr_slice);
                     addr_vec.shrink_to_fit();
                     info.ai_addrlen = cur.ai_addrlen;
@@ -2685,8 +3202,13 @@
 
                 if !cur.ai_canonname.is_null() {
                     let len: usize = strlen(cur.ai_canonname) + 1;
+                    if sgx_is_outside_enclave(cur.ai_canonname as *const c_void, len) == 0 {
+                        result = EAI_SYSTEM;
+                        break;
+                    }
                     let mut name_vec = vec![0u8; len];
-                    let name_slice: &[u8] = slice::from_raw_parts(cur.ai_canonname as *const u8, len);
+                    let name_slice: &[u8] =
+                        slice::from_raw_parts(cur.ai_canonname as *const u8, len);
                     name_vec.copy_from_slice(name_slice);
                     name_vec.shrink_to_fit();
                     info.ai_canonname = name_vec.as_mut_ptr() as *mut c_char;
@@ -2698,35 +3220,60 @@
             }
 
             if addrinfo_vec.len() > 0 {
-                for i in 0..addrinfo_vec.len() - 1 {
-                    addrinfo_vec[i].ai_next = addrinfo_vec[i + 1].as_mut() as *mut addrinfo;
-                }
-                let res_ptr = addrinfo_vec[0].as_mut() as *mut addrinfo;
+                if result == 0 {
+                    for i in 0..addrinfo_vec.len() - 1 {
+                        addrinfo_vec[i].ai_next = addrinfo_vec[i + 1].as_mut() as *mut addrinfo;
+                    }
+                    let res_ptr = addrinfo_vec[0].as_mut() as *mut addrinfo;
 
-                for info in addrinfo_vec {
-                    let _ = Box::into_raw(info);
+                    for info in addrinfo_vec {
+                        let _ = Box::into_raw(info);
+                    }
+                    *res = res_ptr;
+                } else {
+                    set_errno(ESGX);
+                    for addrinfo in addrinfo_vec {
+                        if !addrinfo.ai_addr.is_null() {
+                            let len: usize = addrinfo.ai_addrlen as usize;
+                            let addr_vec =
+                                Vec::from_raw_parts(addrinfo.ai_addr as *mut u8, len, len);
+                            drop(addr_vec);
+                        }
+                        if !addrinfo.ai_canonname.is_null() {
+                            let len: usize = strlen(addrinfo.ai_canonname) + 1;
+                            let name_vec =
+                                Vec::from_raw_parts(addrinfo.ai_addr as *mut u8, len, len);
+                            drop(name_vec);
+                        }
+                    }
                 }
-                *res = res_ptr;
             }
-            let _ = u_freeaddrinfo_ocall(ret_res);
-
+            if !ret_res.is_null() {
+                let _ = u_freeaddrinfo_ocall(ret_res);
+            }
         } else if result == EAI_SYSTEM {
+            *res = ptr::null_mut();
             set_errno(error);
         }
     } else {
+        *res = ptr::null_mut();
         set_errno(ESGX);
         result = EAI_SYSTEM;
     }
     result
 }
 
-pub unsafe fn freeaddrinfo(res: *mut addrinfo ) {
+pub unsafe fn freeaddrinfo(res: *mut addrinfo) {
     let mut cur_ptr: *mut addrinfo = res;
     let mut addrinfo_vec: Vec<Box<addrinfo>> = Vec::new();
     while cur_ptr != ptr::null_mut() {
         let cur: &addrinfo = &*cur_ptr;
         if !cur.ai_addr.is_null() && cur.ai_addrlen > 0 {
-            let addr_vec = Vec::from_raw_parts(cur.ai_addr as *mut u8, cur.ai_addrlen as usize, cur.ai_addrlen as usize);
+            let addr_vec = Vec::from_raw_parts(
+                cur.ai_addr as *mut u8,
+                cur.ai_addrlen as usize,
+                cur.ai_addrlen as usize,
+            );
             drop(addr_vec);
         }
         if !cur.ai_canonname.is_null() {
@@ -2752,11 +3299,13 @@
 pub unsafe fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_poll_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              fds,
-                              nfds,
-                              timeout);
+    let status = u_poll_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fds,
+        nfds,
+        timeout,
+    );
 
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
@@ -2772,9 +3321,7 @@
 pub unsafe fn epoll_create1(flags: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_epoll_create1_ocall(&mut result as *mut c_int,
-                                       &mut error as *mut c_int,
-                                       flags);
+    let status = u_epoll_create1_ocall(&mut result as *mut c_int, &mut error as *mut c_int, flags);
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2786,18 +3333,17 @@
     result
 }
 
-pub unsafe fn epoll_ctl(epfd: c_int,
-                        op: c_int,
-                        fd: c_int,
-                        event: *mut epoll_event) -> c_int {
+pub unsafe fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_epoll_ctl_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   epfd,
-                                   op,
-                                   fd,
-                                   event);
+    let status = u_epoll_ctl_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        epfd,
+        op,
+        fd,
+        event,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2809,18 +3355,22 @@
     result
 }
 
-pub unsafe fn epoll_wait(epfd: c_int,
-                         events: *mut epoll_event,
-                         maxevents: c_int,
-                         timeout: c_int) -> c_int {
+pub unsafe fn epoll_wait(
+    epfd: c_int,
+    events: *mut epoll_event,
+    maxevents: c_int,
+    timeout: c_int,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_epoll_wait_ocall(&mut result as *mut c_int,
-                                    &mut error as *mut c_int,
-                                    epfd,
-                                    events,
-                                    maxevents,
-                                    timeout);
+    let status = u_epoll_wait_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        epfd,
+        events,
+        maxevents,
+        timeout,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2835,9 +3385,7 @@
 pub unsafe fn sysconf(name: c_int) -> c_long {
     let mut result: c_long = 0;
     let mut error: c_int = 0;
-    let status = u_sysconf_ocall(&mut result as *mut c_long,
-                                 &mut error as *mut c_int,
-                                 name);
+    let status = u_sysconf_ocall(&mut result as *mut c_long, &mut error as *mut c_int, name);
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2849,20 +3397,24 @@
     result
 }
 
-pub unsafe fn prctl(option: c_int,
-                    arg2: c_ulong,
-                    arg3: c_ulong,
-                    arg4: c_ulong,
-                    arg5: c_ulong) -> c_int {
+pub unsafe fn prctl(
+    option: c_int,
+    arg2: c_ulong,
+    arg3: c_ulong,
+    arg4: c_ulong,
+    arg5: c_ulong,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_prctl_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               option,
-                               arg2,
-                               arg3,
-                               arg4,
-                               arg5);
+    let status = u_prctl_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        option,
+        arg2,
+        arg3,
+        arg4,
+        arg5,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2877,11 +3429,13 @@
 pub unsafe fn sched_setaffinity(pid: pid_t, cpusetsize: size_t, mask: *const cpu_set_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_sched_setaffinity_ocall(&mut result as *mut c_int,
-                                           &mut error as *mut c_int,
-                                           pid,
-                                           cpusetsize,
-                                           mask);
+    let status = u_sched_setaffinity_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        pid,
+        cpusetsize,
+        mask,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2896,11 +3450,13 @@
 pub unsafe fn sched_getaffinity(pid: pid_t, cpusetsize: size_t, mask: *mut cpu_set_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_sched_getaffinity_ocall(&mut result as *mut c_int,
-                                           &mut error as *mut c_int,
-                                           pid,
-                                           cpusetsize,
-                                           mask);
+    let status = u_sched_getaffinity_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        pid,
+        cpusetsize,
+        mask,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2915,9 +3471,7 @@
 pub unsafe fn pipe(fds: *mut c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_pipe_ocall(&mut result as *mut c_int,
-                              &mut error as *mut c_int,
-                              fds);
+    let status = u_pipe_ocall(&mut result as *mut c_int, &mut error as *mut c_int, fds);
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2932,10 +3486,12 @@
 pub unsafe fn pipe2(fds: *mut c_int, flags: c_int) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_pipe2_ocall(&mut result as *mut c_int,
-                               &mut error as *mut c_int,
-                               fds,
-                               flags);
+    let status = u_pipe2_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        fds,
+        flags,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2950,8 +3506,7 @@
 pub unsafe fn sched_yield() -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_sched_yield_ocall(&mut result as *mut c_int,
-                                     &mut error as *mut c_int);
+    let status = u_sched_yield_ocall(&mut result as *mut c_int, &mut error as *mut c_int);
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2966,10 +3521,12 @@
 pub unsafe fn nanosleep(rqtp: *const timespec, rmtp: *mut timespec) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_nanosleep_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   rqtp,
-                                   rmtp);
+    let status = u_nanosleep_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        rqtp,
+        rmtp,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -2981,18 +3538,22 @@
     result
 }
 
-pub unsafe fn sigaction(signum: c_int,
-                        act: *const sigaction,
-                        oldact: *mut sigaction,
-                        enclave_id: uint64_t) -> c_int {
+pub unsafe fn sigaction(
+    signum: c_int,
+    act: *const sigaction,
+    oldact: *mut sigaction,
+    enclave_id: uint64_t,
+) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status = u_sigaction_ocall(&mut result as *mut c_int,
-                                   &mut error as *mut c_int,
-                                   signum,
-                                   act,
-                                   oldact,
-                                   enclave_id);
+    let status = u_sigaction_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        signum,
+        act,
+        oldact,
+        enclave_id,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -3004,16 +3565,16 @@
     result
 }
 
-pub unsafe fn sigprocmask(signum: c_int,
-                          set: *const sigset_t,
-                          oldset: *mut sigset_t) -> c_int {
+pub unsafe fn sigprocmask(signum: c_int, set: *const sigset_t, oldset: *mut sigset_t) -> c_int {
     let mut result: c_int = 0;
     let mut error: c_int = 0;
-    let status =  u_sigprocmask_ocall(&mut result as *mut c_int,
-                                      &mut error as *mut c_int,
-                                      signum,
-                                      set,
-                                      oldset);
+    let status = u_sigprocmask_ocall(
+        &mut result as *mut c_int,
+        &mut error as *mut c_int,
+        signum,
+        set,
+        oldset,
+    );
     if status == sgx_status_t::SGX_SUCCESS {
         if result == -1 {
             set_errno(error);
@@ -3029,7 +3590,7 @@
     let mut result: c_int = -1;
     let status = u_raise_ocall(&mut result as *mut c_int, signum);
     if status != sgx_status_t::SGX_SUCCESS {
-       result = -1;
+        result = -1;
     }
     result
 }
diff --git a/sgx_no_tstd/Cargo.toml b/sgx_no_tstd/Cargo.toml
index 8bddc6f..213e6a6 100644
--- a/sgx_no_tstd/Cargo.toml
+++ b/sgx_no_tstd/Cargo.toml
@@ -1,11 +1,11 @@
 [package]
 name = "sgx_no_tstd"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 build = "build.rs"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_no_tstd/build.rs b/sgx_no_tstd/build.rs
index c6e8bd8..97deabc 100644
--- a/sgx_no_tstd/build.rs
+++ b/sgx_no_tstd/build.rs
@@ -31,6 +31,7 @@
 
 fn build_libunwind(host: &str, target: &str) -> Result<(), ()> {
     let filter = vec![
+        "m4",
         "config",
         "autom4te.cache",
         "Makefile.in",
@@ -71,7 +72,7 @@
 
     run(Command::new("sh")
                 .current_dir(&native.out_dir)
-                .arg(native.src_dir.join("autogen-linux.sh").to_str().unwrap())
+                .arg(native.src_dir.join("autogen.sh").to_str().unwrap())
                 .arg(format!("--host={}", build_helper::gnu_target(target)))
                 .arg(format!("--build={}", build_helper::gnu_target(host)))
                 .env("CFLAGS", cflags));
@@ -81,4 +82,4 @@
                 .arg(format!("INCDIR={}", native.src_dir.display()))
                 .arg("-j5"));
     Ok(())
-}
+}
\ No newline at end of file
diff --git a/sgx_panic_abort/Cargo.toml b/sgx_panic_abort/Cargo.toml
index 3d42675..d8084b0 100644
--- a/sgx_panic_abort/Cargo.toml
+++ b/sgx_panic_abort/Cargo.toml
@@ -9,6 +9,8 @@
 edition = "2018"
 
 [lib]
-path = "lib.rs"
+test = false
+bench = false
+doc = false
 
 [dependencies]
\ No newline at end of file
diff --git a/sgx_panic_abort/lib.rs b/sgx_panic_abort/src/lib.rs
similarity index 81%
rename from sgx_panic_abort/lib.rs
rename to sgx_panic_abort/src/lib.rs
index 95bf3e0..86925c3 100644
--- a/sgx_panic_abort/lib.rs
+++ b/sgx_panic_abort/src/lib.rs
@@ -7,14 +7,16 @@
 #![unstable(feature = "panic_abort", issue = "32837")]
 #![panic_runtime]
 #![allow(unused_features)]
-
 #![feature(core_intrinsics)]
 #![feature(nll)]
 #![feature(panic_runtime)]
+#![feature(std_internals)]
 #![feature(staged_api)]
 #![feature(rustc_attrs)]
+#![feature(c_unwind)]
 
 use core::any::Any;
+use core::panic::BoxMeUp;
 
 #[rustc_std_internal_symbol]
 #[allow(improper_ctypes_definitions)]
@@ -22,18 +24,9 @@
     unreachable!()
 }
 
-// "Leak" the payload and shim to the relevant abort on the platform in
-// question.
-//
-// For Unix we just use `abort` from libc as it'll trigger debuggers, core
-// dumps, etc, as one might expect. On Windows, however, the best option we have
-// is the `__fastfail` intrinsics, but that's unfortunately not defined in LLVM,
-// and the `RaiseFailFastException` function isn't available until Windows 7
-// which would break compat with XP. For now just use `intrinsics::abort` which
-// will kill us with an illegal instruction, which will do a good enough job for
-// now hopefully.
+// "Leak" the payload and shim to the relevant abort on the platform in question.
 #[rustc_std_internal_symbol]
-pub unsafe extern "C" fn __rust_start_panic(_payload: usize) -> u32 {
+pub unsafe extern "C-unwind" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
     sgx_abort();
 
     #[link(name = "sgx_trts")]
diff --git a/sgx_panic_unwind/Cargo.toml b/sgx_panic_unwind/Cargo.toml
index a37915c..df1a35c 100644
--- a/sgx_panic_unwind/Cargo.toml
+++ b/sgx_panic_unwind/Cargo.toml
@@ -9,7 +9,9 @@
 edition = "2018"
 
 [lib]
-path = "lib.rs"
+test = false
+bench = false
+doc = false
 
 [target.'cfg(not(target_env = "sgx"))'.dependencies]
 sgx_libc = { path = "../sgx_libc" }
diff --git a/sgx_panic_unwind/dwarf/eh.rs b/sgx_panic_unwind/src/dwarf/eh.rs
similarity index 89%
rename from sgx_panic_unwind/dwarf/eh.rs
rename to sgx_panic_unwind/src/dwarf/eh.rs
index 302478c..7394fea 100644
--- a/sgx_panic_unwind/dwarf/eh.rs
+++ b/sgx_panic_unwind/src/dwarf/eh.rs
@@ -1,9 +1,9 @@
 //! Parsing of GCC-style Language-Specific Data Area (LSDA)
 //! For details see:
-//!   http://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
-//!   http://mentorembedded.github.io/cxx-abi/exceptions.pdf
-//!   http://www.airs.com/blog/archives/460
-//!   http://www.airs.com/blog/archives/464
+//!  * <https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html>
+//!  * <https://itanium-cxx-abi.github.io/cxx-abi/exceptions.pdf>
+//!  * <https://www.airs.com/blog/archives/460>
+//!  * <https://www.airs.com/blog/archives/464>
 //!
 //! A reference implementation may be found in the GCC source tree
 //! (`<root>/libgcc/unwind-c.c` as of this writing).
@@ -51,11 +51,7 @@
 
 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
 
-pub unsafe fn find_eh_action(
-    lsda: *const u8,
-    context: &EHContext<'_>,
-    foreign_exception: bool,
-) -> Result<EHAction, ()> {
+pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
     if lsda.is_null() {
         return Ok(EHAction::None);
     }
@@ -98,7 +94,7 @@
                     return Ok(EHAction::None);
                 } else {
                     let lpad = lpad_base + cs_lpad;
-                    return Ok(interpret_cs_action(cs_action, lpad, foreign_exception));
+                    return Ok(interpret_cs_action(cs_action, lpad));
                 }
             }
         }
@@ -123,21 +119,17 @@
                 // Can never have null landing pad for sjlj -- that would have
                 // been indicated by a -1 call site index.
                 let lpad = (cs_lpad + 1) as usize;
-                return Ok(interpret_cs_action(cs_action, lpad, foreign_exception));
+                return Ok(interpret_cs_action(cs_action, lpad));
             }
         }
     }
 }
 
-fn interpret_cs_action(cs_action: u64, lpad: usize, foreign_exception: bool) -> EHAction {
+fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
     if cs_action == 0 {
         // If cs_action is 0 then this is a cleanup (Drop::drop). We run these
         // for both Rust panics and foreign exceptions.
         EHAction::Cleanup(lpad)
-    } else if foreign_exception {
-        // catch_unwind should not catch foreign exceptions, only Rust panics.
-        // Instead just continue unwinding.
-        EHAction::None
     } else {
         // Stop unwinding Rust panics at catch_unwind.
         EHAction::Catch(lpad)
diff --git a/sgx_panic_unwind/dwarf/mod.rs b/sgx_panic_unwind/src/dwarf/mod.rs
similarity index 94%
rename from sgx_panic_unwind/dwarf/mod.rs
rename to sgx_panic_unwind/src/dwarf/mod.rs
index e350580..a392538 100644
--- a/sgx_panic_unwind/dwarf/mod.rs
+++ b/sgx_panic_unwind/src/dwarf/mod.rs
@@ -50,7 +50,7 @@
     }
 
     pub unsafe fn read_sleb128(&mut self) -> i64 {
-        let mut shift: usize = 0;
+        let mut shift: u32 = 0;
         let mut result: u64 = 0;
         let mut byte: u8;
         loop {
@@ -62,9 +62,9 @@
             }
         }
         // sign-extend
-        if shift < 8 * mem::size_of::<u64>() && (byte & 0x40) != 0 {
+        if shift < u64::BITS && (byte & 0x40) != 0 {
             result |= (!0 as u64) << shift;
         }
         result as i64
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_panic_unwind/gcc.rs b/sgx_panic_unwind/src/gcc.rs
similarity index 78%
rename from sgx_panic_unwind/gcc.rs
rename to sgx_panic_unwind/src/gcc.rs
index 8eb1d6b..4f507f5 100644
--- a/sgx_panic_unwind/gcc.rs
+++ b/sgx_panic_unwind/src/gcc.rs
@@ -4,9 +4,9 @@
 //! "Exception Handling in LLVM" (llvm.org/docs/ExceptionHandling.html) and
 //! documents linked from it.
 //! These are also good reads:
-//!     https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
-//!     http://monoinfinito.wordpress.com/series/exception-handling-in-c/
-//!     http://www.airs.com/blog/index.php?s=exception+frames
+//!  * <https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html>
+//!  * <https://monoinfinito.wordpress.com/series/exception-handling-in-c/>
+//!  * <https://www.airs.com/blog/index.php?s=exception+frames>
 //!
 //! ## A brief summary
 //!
@@ -35,14 +35,6 @@
 //!
 //! Once stack has been unwound down to the handler frame level, unwinding stops
 //! and the last personality routine transfers control to the catch block.
-//!
-//! ## `eh_personality` and `eh_unwind_resume`
-//!
-//! These language items are used by the compiler when generating unwind info.
-//! The first one is the personality routine described above. The second one
-//! allows compilation target to customize the process of resuming unwind at the
-//! end of the landing pads. `eh_unwind_resume` is used only if
-//! `custom_unwind_resume` flag in the target options is set.
 
 use alloc::boxed::Box;
 use core::any::Any;
@@ -81,8 +73,14 @@
 }
 
 pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
-    let exception = Box::from_raw(ptr as *mut Exception);
-    exception.cause
+    let exception = ptr as *mut uw::_Unwind_Exception;
+    if (*exception).exception_class != rust_exception_class() {
+        uw::_Unwind_DeleteException(exception);
+        super::__rust_foreign_exception();
+    } else {
+        let exception = Box::from_raw(exception as *mut Exception);
+        exception.cause
+    }
 }
 
 // Rust's exception class identifier.  This is used by personality routines to
@@ -96,7 +94,7 @@
 // and TargetLowering::getExceptionSelectorRegister() for each architecture,
 // then mapped to DWARF register numbers via register definition tables
 // (typically <arch>RegisterInfo.td, search for "DwarfRegNum").
-// See also http://llvm.org/docs/WritingAnLLVMBackend.html#defining-a-register.
+// See also https://llvm.org/docs/WritingAnLLVMBackend.html#defining-a-register.
 
 #[cfg(target_arch = "x86")]
 const UNWIND_DATA_REG: (i32, i32) = (0, 2); // EAX, EDX
@@ -111,15 +109,14 @@
 unsafe extern "C" fn rust_eh_personality_impl(
     version: c_int,
     actions: uw::_Unwind_Action,
-    exception_class: uw::_Unwind_Exception_Class,
+    _exception_class: uw::_Unwind_Exception_Class,
     exception_object: *mut uw::_Unwind_Exception,
-    context: *mut uw::_Unwind_Context)
--> uw::_Unwind_Reason_Code {
+    context: *mut uw::_Unwind_Context,
+) -> uw::_Unwind_Reason_Code {
     if version != 1 {
         return uw::_URC_FATAL_PHASE1_ERROR;
     }
-    let foreign_exception = exception_class != rust_exception_class();
-    let eh_action = match find_eh_action(context, foreign_exception) {
+    let eh_action = match find_eh_action(context) {
         Ok(action) => action,
         Err(_) => return uw::_URC_FATAL_PHASE1_ERROR,
     };
@@ -135,8 +132,7 @@
             EHAction::None => uw::_URC_CONTINUE_UNWIND,
             EHAction::Cleanup(lpad) |
             EHAction::Catch(lpad) => {
-                uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0,
-                    exception_object as uintptr_t);
+                uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0, exception_object as uintptr_t);
                 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, 0);
                 uw::_Unwind_SetIP(context, lpad);
                 uw::_URC_INSTALL_CONTEXT
@@ -152,20 +148,12 @@
     actions: uw::_Unwind_Action,
     exception_class: uw::_Unwind_Exception_Class,
     exception_object: *mut uw::_Unwind_Exception,
-    context: *mut uw::_Unwind_Context)
--> uw::_Unwind_Reason_Code {
-    rust_eh_personality_impl(
-        version,
-        actions,
-        exception_class,
-        exception_object,
-        context)
+    context: *mut uw::_Unwind_Context,
+) -> uw::_Unwind_Reason_Code {
+    rust_eh_personality_impl(version, actions, exception_class, exception_object, context)
 }
 
-unsafe fn find_eh_action(
-    context: *mut uw::_Unwind_Context,
-    foreign_exception: bool,
-) -> Result<EHAction, ()> {
+unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> Result<EHAction, ()> {
     let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
     let mut ip_before_instr: c_int = 0;
     let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);
@@ -177,5 +165,6 @@
         get_text_start: &|| uw::_Unwind_GetTextRelBase(context),
         get_data_start: &|| uw::_Unwind_GetDataRelBase(context),
     };
-    eh::find_eh_action(lsda, &eh_context, foreign_exception)
+    eh::find_eh_action(lsda, &eh_context)
 }
+
diff --git a/sgx_panic_unwind/lib.rs b/sgx_panic_unwind/src/lib.rs
similarity index 74%
rename from sgx_panic_unwind/lib.rs
rename to sgx_panic_unwind/src/lib.rs
index 428b754..fb98924 100644
--- a/sgx_panic_unwind/lib.rs
+++ b/sgx_panic_unwind/src/lib.rs
@@ -5,20 +5,22 @@
 
 #![no_std]
 #![unstable(feature = "panic_unwind", issue = "32837")]
-#![cfg_attr(all(target_env = "sgx", target_vendor = "mesalock"), feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
 
 #![feature(core_intrinsics)]
 #![feature(lang_items)]
 #![feature(nll)]
 #![feature(panic_unwind)]
-#![feature(raw)]
 #![feature(staged_api)]
 #![feature(std_internals)]
-#![feature(unwind_attributes)]
 #![feature(abi_thiscall)]
+#![feature(rustc_attrs)]
 #![panic_runtime]
 #![feature(panic_runtime)]
-#![feature(rustc_attrs)]
+#![feature(c_unwind)]
 
 extern crate alloc;
 extern crate sgx_libc;
@@ -35,6 +37,9 @@
     /// Handler in libstd called when a panic object is dropped outside of
     /// `catch_unwind`.
     fn __rust_drop_panic() -> !;
+
+    /// Handler in libstd called when a foreign exception is caught.
+    fn __rust_foreign_exception() -> !;
 }
 
 mod dwarf;
@@ -48,9 +53,8 @@
 // Entry point for raising an exception, just delegates to the platform-specific
 // implementation.
 #[rustc_std_internal_symbol]
-#[unwind(allowed)]
-pub unsafe extern "C" fn __rust_start_panic(payload: usize) -> u32 {
-    let payload = payload as *mut &mut dyn BoxMeUp;
-    let payload = (*payload).take_box();
-    imp::panic(Box::from_raw(payload))
+pub unsafe extern "C-unwind" fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32 {
+    let payload = Box::from_raw((*payload).take_box());
+
+    imp::panic(payload)
 }
diff --git a/sgx_rand/Cargo.toml b/sgx_rand/Cargo.toml
index c3e7a57..09a7545 100644
--- a/sgx_rand/Cargo.toml
+++ b/sgx_rand/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_rand"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_rand_derive/Cargo.toml b/sgx_rand_derive/Cargo.toml
index 6576993..be5026c 100644
--- a/sgx_rand_derive/Cargo.toml
+++ b/sgx_rand_derive/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_rand_derive"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_serialize/Cargo.toml b/sgx_serialize/Cargo.toml
index c3bb5f3..f415fa5 100644
--- a/sgx_serialize/Cargo.toml
+++ b/sgx_serialize/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_serialize"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_serialize_derive/Cargo.toml b/sgx_serialize_derive/Cargo.toml
index 03feede..9619005 100644
--- a/sgx_serialize_derive/Cargo.toml
+++ b/sgx_serialize_derive/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_serialize_derive"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_serialize_derive_internals/Cargo.toml b/sgx_serialize_derive_internals/Cargo.toml
index b4588c5..b499f09 100644
--- a/sgx_serialize_derive_internals/Cargo.toml
+++ b/sgx_serialize_derive_internals/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_serialize_derive_internals"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_signal/Cargo.toml b/sgx_signal/Cargo.toml
index 18051de..0db8992 100644
--- a/sgx_signal/Cargo.toml
+++ b/sgx_signal/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_signal"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_signal/src/exception.rs b/sgx_signal/src/exception.rs
index 3cb482e..0f9f7a8 100644
--- a/sgx_signal/src/exception.rs
+++ b/sgx_signal/src/exception.rs
@@ -23,6 +23,7 @@
 use sgx_types::{sgx_exception_info_t, sgx_exception_vector_t};
 use sgx_types::{EXCEPTION_CONTINUE_EXECUTION, EXCEPTION_CONTINUE_SEARCH};
 use std::collections::LinkedList;
+use std::convert::From;
 use std::num::NonZeroU64;
 use std::ops::Drop;
 use std::sync::{Arc, Once, SgxRwLock, SgxThreadMutex, ONCE_INIT};
@@ -35,9 +36,9 @@
     Execution,
 }
 
-impl Into<i32> for ContinueType {
-    fn into(self) -> i32 {
-        match self {
+impl From<ContinueType> for i32 {
+    fn from(continue_type: ContinueType) -> i32 {
+        match continue_type {
             ContinueType::Search => EXCEPTION_CONTINUE_SEARCH,
             ContinueType::Execution => EXCEPTION_CONTINUE_EXECUTION,
         }
@@ -136,7 +137,7 @@
 
     exception_info.cpu_context.rdi = exception_info.exception_vector as u32 as u64;
     exception_info.cpu_context.rsi = exception_info.cpu_context.rip;
-    exception_info.cpu_context.rip = exception_panic as u64;
+    exception_info.cpu_context.rip = exception_panic as usize as u64;
 
     ContinueType::Execution
 }
diff --git a/sgx_signal/src/manager.rs b/sgx_signal/src/manager.rs
index 81a9008..9484e2d 100644
--- a/sgx_signal/src/manager.rs
+++ b/sgx_signal/src/manager.rs
@@ -230,11 +230,7 @@
     }
 
     pub fn get_action(&self, signo: SigNum) -> Option<ActionSlot> {
-        if let Some(slot) = self.action_set.lock().unwrap().get(&signo) {
-            Some(slot.clone())
-        } else {
-            None
-        }
+        self.action_set.lock().unwrap().get(&signo).cloned()
     }
 
     pub fn remove_action(&self, signo: SigNum, id: ActionId) -> bool {
diff --git a/sgx_signal/src/signal.rs b/sgx_signal/src/signal.rs
index 9af87a2..3efe9c1 100644
--- a/sgx_signal/src/signal.rs
+++ b/sgx_signal/src/signal.rs
@@ -28,6 +28,7 @@
 use std::enclave::get_enclave_id;
 use std::io::Error;
 use std::mem;
+use std::ptr;
 use std::rt::*;
 use std::sync::{Arc, Once, SgxMutex};
 
@@ -64,7 +65,7 @@
                 signal_action_lock: SgxMutex::new(()),
             });
 
-            let _r = at_exit(|| Self::clear());
+            let _r = at_exit(Self::clear);
         });
         Self::get()
     }
@@ -76,28 +77,28 @@
     }
 }
 
+/// # Safety
 #[no_mangle]
-pub extern "C" fn t_signal_handler_ecall(info: *const siginfo_t) -> c_int {
+pub unsafe extern "C" fn t_signal_handler_ecall(info: *const siginfo_t) -> c_int {
     if info.is_null() {
         return -1;
     }
-    let si_info = unsafe { &*(info) };
+
+    let si_info = &*(info);
     let global = GlobalData::get();
     let mask = manager::get_block_mask();
     // If the signal is blocked and still passed into the enclave. The signal
     // masks inside the enclave is out of sync with the untrusted signal mask.
-    unsafe {
-        let signo = SigNum::from_raw_uncheck(si_info.si_signo);
-        if mask.is_member(signo) {
-            -1
-        } else {
-            global.signal_manager.handler(
-                si_info.si_signo,
-                info as *const siginfo_t,
-                0 as *const c_void,
-            );
-            0
-        }
+    let signo = SigNum::from_raw_uncheck(si_info.si_signo);
+    if mask.is_member(signo) {
+        -1
+    } else {
+        global.signal_manager.handler(
+            si_info.si_signo,
+            info as *const siginfo_t,
+            ptr::null::<c_void>(),
+        );
+        0
     }
 }
 
@@ -262,7 +263,7 @@
     // Unblock signals inside the enclave before unblocking signals on the host.
     // |oldset| is already filled with the signal mask inside the enclave.
     manager::unblock(&signals_to_unblock);
-    let result = unsafe { sigprocmask(how, set as *const sigset_t, 0 as *mut sigset_t) };
+    let result = unsafe { sigprocmask(how, set as *const sigset_t, ptr::null_mut::<sigset_t>()) };
     // Block signals inside the enclave after the host.
     manager::block(&signals_to_block);
     result
@@ -326,7 +327,7 @@
             signal: signo,
             action: action_id,
         })
-        .map_err(|err| Error::from_raw_os_error(err))
+        .map_err(Error::from_raw_os_error)
 }
 
 pub fn unregister(id: SignalId) -> bool {
diff --git a/sgx_tcrypto/Cargo.toml b/sgx_tcrypto/Cargo.toml
index 6795f75..2d010cb 100644
--- a/sgx_tcrypto/Cargo.toml
+++ b/sgx_tcrypto/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tcrypto"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tcrypto_helper/Cargo.toml b/sgx_tcrypto_helper/Cargo.toml
index 6f092db..e85af17 100644
--- a/sgx_tcrypto_helper/Cargo.toml
+++ b/sgx_tcrypto_helper/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tcrypto_helper"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tdh/Cargo.toml b/sgx_tdh/Cargo.toml
index 18f2941..cf30524 100644
--- a/sgx_tdh/Cargo.toml
+++ b/sgx_tdh/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tdh"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tdh/src/dh.rs b/sgx_tdh/src/dh.rs
index bcc7e2a..433ca12 100644
--- a/sgx_tdh/src/dh.rs
+++ b/sgx_tdh/src/dh.rs
@@ -175,9 +175,13 @@
             return None;
         }
 
-        let mut dh_msg3 = SgxDhMsg3::default();
-        dh_msg3.cmac = raw_msg3.cmac;
-        dh_msg3.msg3_body.report = raw_msg3.msg3_body.report;
+        let mut dh_msg3 = SgxDhMsg3 {
+            cmac: raw_msg3.cmac,
+            msg3_body: SgxDhMsg3Body {
+                report: raw_msg3.msg3_body.report,
+                additional_prop: Default::default(),
+            },
+        };
 
         if additional_prop_len > 0 {
             let mut additional_prop: Vec<u8> = vec![0_u8; additional_prop_len as usize];
@@ -809,9 +813,11 @@
         }
 
         #[cfg(feature = "use_lav2")]
-        self.lav2_verify_message3(msg3).map_err(|ret| self.set_error(ret))?;
+        self.lav2_verify_message3(msg3)
+            .map_err(|ret| self.set_error(ret))?;
         #[cfg(not(feature = "use_lav2"))]
-        self.dh_verify_message3(msg3).map_err(|ret| self.set_error(ret))?;
+        self.dh_verify_message3(msg3)
+            .map_err(|ret| self.set_error(ret))?;
 
         let align_aek =
             derive_key(&self.shared_key.key, &EC_AEK_LABEL).map_err(|ret| self.set_error(ret))?;
@@ -965,6 +971,7 @@
 unsafe impl ContiguousMemory for SgxLAv2ProtoSpec {}
 
 impl SgxLAv2ProtoSpec {
+    #[allow(clippy::wrong_self_convention)]
     pub unsafe fn to_report_data(&self) -> sgx_report_data_t {
         mem::transmute::<SgxLAv2ProtoSpec, sgx_report_data_t>(*self)
     }
@@ -1011,12 +1018,10 @@
                         size as usize,
                     );
                 }
+            } else if from == -1 {
+                break;
             } else {
-                if from == -1 {
-                    break;
-                } else {
-                    return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
-                }
+                return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
             }
             to += size;
         }
diff --git a/sgx_tdh/src/lib.rs b/sgx_tdh/src/lib.rs
index 94b96e4..ad9d554 100644
--- a/sgx_tdh/src/lib.rs
+++ b/sgx_tdh/src/lib.rs
@@ -21,10 +21,14 @@
 //!
 
 #![no_std]
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
 #![allow(non_camel_case_types)]
 #![allow(unused_assignments)]
 #![allow(dead_code)]
+#![allow(clippy::missing_safety_doc)]
 
 #[macro_use]
 extern crate alloc;
diff --git a/sgx_tkey_exchange/Cargo.toml b/sgx_tkey_exchange/Cargo.toml
index a989624..fae39e2 100644
--- a/sgx_tkey_exchange/Cargo.toml
+++ b/sgx_tkey_exchange/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tkey_exchange"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tprotected_fs/Cargo.toml b/sgx_tprotected_fs/Cargo.toml
index 575b360..4808968 100644
--- a/sgx_tprotected_fs/Cargo.toml
+++ b/sgx_tprotected_fs/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tprotected_fs"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_trts/Cargo.toml b/sgx_trts/Cargo.toml
index 3cf3f97..79a0919 100644
--- a/sgx_trts/Cargo.toml
+++ b/sgx_trts/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_trts"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_trts/src/ascii.rs b/sgx_trts/src/ascii.rs
index 9f12c3f..313eeb5 100644
--- a/sgx_trts/src/ascii.rs
+++ b/sgx_trts/src/ascii.rs
@@ -27,12 +27,9 @@
 //!
 //! The [`escape_default`] function provides an iterator over the bytes of an
 //! escaped version of the character given.
-//!
-//! [`AsciiExt`]: trait.AsciiExt.html
-//! [`escape_default`]: fn.escape_default.html
 
-use alloc::vec::Vec;
 use alloc::string::String;
+use alloc::vec::Vec;
 
 pub use core::ascii::{escape_default, EscapeDefault};
 
@@ -41,6 +38,23 @@
 /// Be aware that operations on seemingly non-ASCII characters can sometimes
 /// have unexpected results. Consider this example:
 ///
+/// ```
+/// use std::ascii::AsciiExt;
+///
+/// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFÉ");
+/// assert_eq!(AsciiExt::to_ascii_uppercase("café"), "CAFé");
+/// ```
+///
+/// In the first example, the lowercased string is represented `"cafe\u{301}"`
+/// (the last character is an acute accent [combining character]). Unlike the
+/// other characters in the string, the combining character will not get mapped
+/// to an uppercase variant, resulting in `"CAFE\u{301}"`. In the second
+/// example, the lowercased string is represented `"caf\u{e9}"` (the last
+/// character is a single Unicode character representing an 'e' with an acute
+/// accent). Since the last character is defined outside the scope of ASCII,
+/// it will not get mapped to an uppercase variant, resulting in `"CAF\u{e9}"`.
+///
+/// [combining character]: https://en.wikipedia.org/wiki/Combining_character
 pub trait AsciiExt {
     /// Container type for copied ASCII characters.
     type Owned;
@@ -49,7 +63,7 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     fn is_ascii(&self) -> bool;
 
@@ -65,9 +79,10 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     ///
+    /// [`make_ascii_uppercase`]: AsciiExt::make_ascii_uppercase
     #[allow(deprecated)]
     fn to_ascii_uppercase(&self) -> Self::Owned;
 
@@ -83,9 +98,10 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     ///
+    /// [`make_ascii_lowercase`]: AsciiExt::make_ascii_lowercase
     #[allow(deprecated)]
     fn to_ascii_lowercase(&self) -> Self::Owned;
 
@@ -96,7 +112,7 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     fn eq_ignore_ascii_case(&self, other: &Self) -> bool;
 
@@ -110,9 +126,10 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     ///
+    /// [`to_ascii_uppercase`]: AsciiExt::to_ascii_uppercase
     fn make_ascii_uppercase(&mut self);
 
     /// Converts this type to its ASCII lower case equivalent in-place.
@@ -125,32 +142,45 @@
     ///
     /// # Note
     ///
-    /// This method will be deprecated in favor of the identically-named
+    /// This method is deprecated in favor of the identically-named
     /// inherent methods on `u8`, `char`, `[u8]` and `str`.
     ///
+    /// [`to_ascii_lowercase`]: AsciiExt::to_ascii_lowercase
     fn make_ascii_lowercase(&mut self);
 }
 
 macro_rules! delegating_ascii_methods {
     () => {
         #[inline]
-        fn is_ascii(&self) -> bool { self.is_ascii() }
+        fn is_ascii(&self) -> bool {
+            self.is_ascii()
+        }
 
         #[inline]
-        fn to_ascii_uppercase(&self) -> Self::Owned { self.to_ascii_uppercase() }
+        fn to_ascii_uppercase(&self) -> Self::Owned {
+            self.to_ascii_uppercase()
+        }
 
         #[inline]
-        fn to_ascii_lowercase(&self) -> Self::Owned { self.to_ascii_lowercase() }
+        fn to_ascii_lowercase(&self) -> Self::Owned {
+            self.to_ascii_lowercase()
+        }
 
         #[inline]
-        fn eq_ignore_ascii_case(&self, o: &Self) -> bool { self.eq_ignore_ascii_case(o) }
+        fn eq_ignore_ascii_case(&self, o: &Self) -> bool {
+            self.eq_ignore_ascii_case(o)
+        }
 
         #[inline]
-        fn make_ascii_uppercase(&mut self) { self.make_ascii_uppercase(); }
+        fn make_ascii_uppercase(&mut self) {
+            self.make_ascii_uppercase();
+        }
 
         #[inline]
-        fn make_ascii_lowercase(&mut self) { self.make_ascii_lowercase(); }
-    }
+        fn make_ascii_lowercase(&mut self) {
+            self.make_ascii_lowercase();
+        }
+    };
 }
 
 #[allow(deprecated)]
@@ -179,4 +209,4 @@
     type Owned = String;
 
     delegating_ascii_methods!();
-}
\ No newline at end of file
+}
diff --git a/sgx_trts/src/c_str.rs b/sgx_trts/src/c_str.rs
index 8e55c92..0f9e561 100644
--- a/sgx_trts/src/c_str.rs
+++ b/sgx_trts/src/c_str.rs
@@ -15,6 +15,25 @@
 // specific language governing permissions and limitations
 // under the License..
 
+use alloc::borrow::{Borrow, Cow, ToOwned};
+use alloc::boxed::Box;
+use alloc::rc::Rc;
+use alloc::slice;
+use alloc::str::{self, Utf8Error};
+use alloc::string::String;
+use alloc::sync::Arc;
+use alloc::vec::Vec;
+use core::cmp::Ordering;
+use core::fmt::{self, Write};
+use core::mem;
+use core::num::NonZeroU8;
+use core::ops;
+use core::ptr;
+use crate::ascii;
+use crate::libc;
+use crate::memchr;
+use sgx_types::c_char;
+
 /// A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the
 /// middle.
 ///
@@ -23,30 +42,86 @@
 /// type is a static guarantee that the underlying bytes contain no interior 0
 /// bytes ("nul characters") and that the final byte is 0 ("nul terminator").
 ///
-use sgx_types::c_char;
-use crate::libc;
-use crate::memchr;
-use crate::ascii;
-
-use core::ops;
-use core::cmp::Ordering;
-use core::mem;
-use core::ptr;
-use core::fmt::{self, Write};
-use core::num::NonZeroU8;
-use alloc::boxed::Box;
-use alloc::borrow::{Cow, Borrow, ToOwned};
-use alloc::vec::Vec;
-use alloc::string::String;
-use alloc::slice;
-use alloc::rc::Rc;
-use alloc::sync::Arc;
-
-use alloc::str::{self, Utf8Error};
-
-/// A type representing an owned C-compatible string
+/// `CString` is to [`&CStr`] as [`String`] is to [`&str`]: the former
+/// in each pair are owned strings; the latter are borrowed
+/// references.
 ///
+/// # Creating a `CString`
+///
+/// A `CString` is created from either a byte slice or a byte vector,
+/// or anything that implements [`Into`]`<`[`Vec`]`<`[`u8`]`>>` (for
+/// example, you can build a `CString` straight out of a [`String`] or
+/// a [`&str`], since both implement that trait).
+///
+/// The [`CString::new`] method will actually check that the provided `&[u8]`
+/// does not have 0 bytes in the middle, and return an error if it
+/// finds one.
+///
+/// # Extracting a raw pointer to the whole C string
+///
+/// `CString` implements an [`as_ptr`][`CStr::as_ptr`] method through the [`Deref`]
+/// trait. This method will give you a `*const c_char` which you can
+/// feed directly to extern functions that expect a nul-terminated
+/// string, like C's `strdup()`. Notice that [`as_ptr`][`CStr::as_ptr`] returns a
+/// read-only pointer; if the C code writes to it, that causes
+/// undefined behavior.
+///
+/// # Extracting a slice of the whole C string
+///
+/// Alternatively, you can obtain a `&[`[`u8`]`]` slice from a
+/// `CString` with the [`CString::as_bytes`] method. Slices produced in this
+/// way do *not* contain the trailing nul terminator. This is useful
+/// when you will be calling an extern function that takes a `*const
+/// u8` argument which is not necessarily nul-terminated, plus another
+/// argument with the length of the string — like C's `strndup()`.
+/// You can of course get the slice's length with its
+/// [`len`][slice::len] method.
+///
+/// If you need a `&[`[`u8`]`]` slice *with* the nul terminator, you
+/// can use [`CString::as_bytes_with_nul`] instead.
+///
+/// Once you have the kind of slice you need (with or without a nul
+/// terminator), you can call the slice's own
+/// [`as_ptr`][slice::as_ptr] method to get a read-only raw pointer to pass to
+/// extern functions. See the documentation for that function for a
+/// discussion on ensuring the lifetime of the raw pointer.
+///
+/// [`&str`]: prim@str
+/// [`Deref`]: ops::Deref
+/// [`&CStr`]: CStr
+///
+/// # Examples
+///
+/// ```ignore (extern-declaration)
+/// # fn main() {
+/// use std::ffi::CString;
+/// use std::os::raw::c_char;
+///
+/// extern "C" {
+///     fn my_printer(s: *const c_char);
+/// }
+///
+/// // We are certain that our string doesn't have 0 bytes in the middle,
+/// // so we can .expect()
+/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
+/// unsafe {
+///     my_printer(c_to_print.as_ptr());
+/// }
+/// # }
+/// ```
+///
+/// # Safety
+///
+/// `CString` is intended for working with traditional C-style strings
+/// (a sequence of non-nul bytes terminated by a single nul byte); the
+/// primary use case for these kinds of strings is interoperating with C-like
+/// code. Often you will need to transfer ownership to/from that external
+/// code. It is strongly recommended that you thoroughly read through the
+/// documentation of `CString` before use, as improper ownership management
+/// of `CString` instances can lead to invalid memory accesses, memory leaks,
+/// and other memory errors.
 #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "cstring_type")]
 pub struct CString {
     // Invariant 1: the slice ends with a zero byte and has a length of at least one.
     // Invariant 2: the slice contains only one zero byte.
@@ -68,10 +143,67 @@
 ///
 /// Note that this structure is **not** `repr(C)` and is not recommended to be
 /// placed in the signatures of FFI functions. Instead, safe wrappers of FFI
-/// functions may leverage the unsafe [`from_ptr`] constructor to provide a safe
-/// interface to other consumers.
+/// functions may leverage the unsafe [`CStr::from_ptr`] constructor to provide
+/// a safe interface to other consumers.
 ///
+/// # Examples
+///
+/// Inspecting a foreign C string:
+///
+/// ```ignore (extern-declaration)
+/// use std::ffi::CStr;
+/// use std::os::raw::c_char;
+///
+/// extern "C" { fn my_string() -> *const c_char; }
+///
+/// unsafe {
+///     let slice = CStr::from_ptr(my_string());
+///     println!("string buffer size without nul terminator: {}", slice.to_bytes().len());
+/// }
+/// ```
+///
+/// Passing a Rust-originating C string:
+///
+/// ```ignore (extern-declaration)
+/// use std::ffi::{CString, CStr};
+/// use std::os::raw::c_char;
+///
+/// fn work(data: &CStr) {
+///     extern "C" { fn work_with(data: *const c_char); }
+///
+///     unsafe { work_with(data.as_ptr()) }
+/// }
+///
+/// let s = CString::new("data data data data").expect("CString::new failed");
+/// work(&s);
+/// ```
+///
+/// Converting a foreign C string into a Rust [`String`]:
+///
+/// ```ignore (extern-declaration)
+/// use std::ffi::CStr;
+/// use std::os::raw::c_char;
+///
+/// extern "C" { fn my_string() -> *const c_char; }
+///
+/// fn my_string_safe() -> String {
+///     unsafe {
+///         CStr::from_ptr(my_string()).to_string_lossy().into_owned()
+///     }
+/// }
+///
+/// println!("string: {}", my_string_safe());
+/// ```
+///
+/// [`&str`]: prim@str
 #[derive(Hash)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "CStr")]
+// FIXME:
+// `fn from` in `impl From<&CStr> for Box<CStr>` current implementation relies
+// on `CStr` being layout-compatible with `[u8]`.
+// When attribute privacy is implemented, `CStr` should be annotated as `#[repr(transparent)]`.
+// Anyway, `CStr` representation and layout are considered implementation detail, are
+// not documented and must not be relied upon.
 pub struct CStr {
     // FIXME: this should not be represented with a DST slice but rather with
     //        just a raw `c_char` along with some form of marker to make
@@ -88,9 +220,13 @@
 /// This error is created by the [`new`][`CString::new`] method on
 /// [`CString`]. See its documentation for more.
 ///
-/// [`CString`]: struct.CString.html
-/// [`CString::new`]: struct.CString.html#method.new
+/// # Examples
 ///
+/// ```
+/// use std::ffi::{CString, NulError};
+///
+/// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err();
+/// ```
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct NulError(usize, Vec<u8>);
 
@@ -102,33 +238,46 @@
 
 /// An error indicating that a nul byte was not in the expected position.
 ///
-/// The slice used to create a [`CStr`] must have one and only one nul
-/// byte at the end of the slice.
+/// The slice used to create a [`CStr`] must have one and only one nul byte,
+/// positioned at the end.
 ///
-/// This error is created by the
-/// [`from_bytes_with_nul`][`CStr::from_bytes_with_nul`] method on
-/// [`CStr`]. See its documentation for more.
+/// This error is created by the [`CStr::from_bytes_with_nul`] method.
+/// See its documentation for more.
 ///
-/// [`CStr`]: struct.CStr.html
-/// [`CStr::from_bytes_with_nul`]: struct.CStr.html#method.from_bytes_with_nul
+/// # Examples
 ///
+/// ```
+/// use std::ffi::{CStr, FromBytesWithNulError};
+///
+/// let _: FromBytesWithNulError = CStr::from_bytes_with_nul(b"f\0oo").unwrap_err();
+/// ```
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct FromBytesWithNulError {
     kind: FromBytesWithNulErrorKind,
 }
 
-#[derive(Clone, PartialEq, Eq, Debug)]
-enum FromBytesWithNulErrorKind {
-    InteriorNul(usize),
-    NotNulTerminated,
+impl fmt::Display for FromBytesWithNulError {
+    #[allow(deprecated, deprecated_in_future)]
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.write_str(self.__description())?;
+        if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
+            write!(f, " at byte pos {}", pos)?;
+        }
+        Ok(())
+    }
 }
 
 impl FromBytesWithNulError {
     fn interior_nul(pos: usize) -> FromBytesWithNulError {
-        FromBytesWithNulError { kind: FromBytesWithNulErrorKind::InteriorNul(pos) }
+        FromBytesWithNulError {
+            kind: FromBytesWithNulErrorKind::InteriorNul(pos),
+        }
     }
+
     fn not_nul_terminated() -> FromBytesWithNulError {
-        FromBytesWithNulError { kind: FromBytesWithNulErrorKind::NotNulTerminated }
+        FromBytesWithNulError {
+            kind: FromBytesWithNulErrorKind::NotNulTerminated,
+        }
     }
 
     pub fn __description(&self) -> &str {
@@ -141,29 +290,105 @@
     }
 }
 
-impl fmt::Display for FromBytesWithNulError {
+/// An error indicating that a nul byte was not in the expected position.
+///
+/// The vector used to create a [`CString`] must have one and only one nul byte,
+/// positioned at the end.
+///
+/// This error is created by the [`CString::from_vec_with_nul`] method.
+/// See its documentation for more.
+///
+/// # Examples
+///
+/// ```
+/// #![feature(cstring_from_vec_with_nul)]
+/// use std::ffi::{CString, FromVecWithNulError};
+///
+/// let _: FromVecWithNulError = CString::from_vec_with_nul(b"f\0oo".to_vec()).unwrap_err();
+/// ```
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct FromVecWithNulError {
+    error_kind: FromBytesWithNulErrorKind,
+    bytes: Vec<u8>,
+}
+
+impl fmt::Display for FromVecWithNulError {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.write_str(self.__description())?;
-        if let FromBytesWithNulErrorKind::InteriorNul(pos) = self.kind {
-            write!(f, " at byte pos {}", pos)?;
+        match self.error_kind {
+            FromBytesWithNulErrorKind::InteriorNul(pos) => write!(
+                f,
+                "data provided contains an interior nul byte at pos {}",
+                pos
+            ),
+            FromBytesWithNulErrorKind::NotNulTerminated => {
+                write!(f, "data provided is not nul terminated")
+            }
         }
-        Ok(())
+    }
+}
+
+#[derive(Clone, PartialEq, Eq, Debug)]
+enum FromBytesWithNulErrorKind {
+    InteriorNul(usize),
+    NotNulTerminated,
+}
+
+impl FromVecWithNulError {
+    /// Returns a slice of [`u8`]s bytes that were attempted to convert to a [`CString`].
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// #![feature(cstring_from_vec_with_nul)]
+    /// use std::ffi::CString;
+    ///
+    /// // Some invalid bytes in a vector
+    /// let bytes = b"f\0oo".to_vec();
+    ///
+    /// let value = CString::from_vec_with_nul(bytes.clone());
+    ///
+    /// assert_eq!(&bytes[..], value.unwrap_err().as_bytes());
+    /// ```
+    pub fn as_bytes(&self) -> &[u8] {
+        &self.bytes[..]
+    }
+
+    /// Returns the bytes that were attempted to convert to a [`CString`].
+    ///
+    /// This method is carefully constructed to avoid allocation. It will
+    /// consume the error, moving out the bytes, so that a copy of the bytes
+    /// does not need to be made.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// #![feature(cstring_from_vec_with_nul)]
+    /// use std::ffi::CString;
+    ///
+    /// // Some invalid bytes in a vector
+    /// let bytes = b"f\0oo".to_vec();
+    ///
+    /// let value = CString::from_vec_with_nul(bytes.clone());
+    ///
+    /// assert_eq!(bytes, value.unwrap_err().into_bytes());
+    /// ```
+    pub fn into_bytes(self) -> Vec<u8> {
+        self.bytes
     }
 }
 
 /// An error indicating invalid UTF-8 when converting a [`CString`] into a [`String`].
 ///
-/// `CString` is just a wrapper over a buffer of bytes with a nul
-/// terminator; [`into_string`][`CString::into_string`] performs UTF-8
-/// validation on those bytes and may return this error.
+/// `CString` is just a wrapper over a buffer of bytes with a nul terminator;
+/// [`CString::into_string`] performs UTF-8 validation on those bytes and may
+/// return this error.
 ///
-/// This `struct` is created by the
-/// [`into_string`][`CString::into_string`] method on [`CString`]. See
+/// This `struct` is created by [`CString::into_string()`]. See
 /// its documentation for more.
-///
-/// [`String`]: ../string/struct.String.html
-/// [`CString`]: struct.CString.html
-/// [`CString::into_string`]: struct.CString.html#method.into_string
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct IntoStringError {
     inner: CString,
@@ -171,6 +396,7 @@
 }
 
 impl IntoStringError {
+    #[allow(deprecated)]
     pub fn __description(&self) -> &str {
         "C string contained non-utf8 bytes"
     }
@@ -181,12 +407,12 @@
 }
 
 impl fmt::Display for IntoStringError {
+    #[allow(deprecated, deprecated_in_future)]
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         self.__description().fmt(f)
     }
 }
 
-
 impl CString {
     /// Creates a new C-compatible string from a container of bytes.
     ///
@@ -196,6 +422,25 @@
     /// appended by this function; the provided data should *not*
     /// contain any 0 bytes in it.
     ///
+    /// # Examples
+    ///
+    /// ```ignore (extern-declaration)
+    /// use std::ffi::CString;
+    /// use std::os::raw::c_char;
+    ///
+    /// extern "C" { fn puts(s: *const c_char); }
+    ///
+    /// let to_print = CString::new("Hello!").expect("CString::new failed");
+    /// unsafe {
+    ///     puts(to_print.as_ptr());
+    /// }
+    /// ```
+    ///
+    /// # Errors
+    ///
+    /// This function will return an error if the supplied bytes contain an
+    /// internal 0 byte. The [`NulError`] returned will contain the bytes as well as
+    /// the position of the nul byte.
     pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
         trait SpecIntoVec {
             fn into_vec(self) -> Vec<u8>;
@@ -234,55 +479,118 @@
     /// Creates a C-compatible string by consuming a byte vector,
     /// without checking for interior 0 bytes.
     ///
-    /// This method is equivalent to [`new`] except that no runtime assertion
-    /// is made that `v` contains no 0 bytes, and it requires an actual
-    /// byte vector, not anything that can be converted to one with Into.
+    /// This method is equivalent to [`CString::new`] except that no runtime
+    /// assertion is made that `v` contains no 0 bytes, and it requires an
+    /// actual byte vector, not anything that can be converted to one with Into.
     ///
-    /// [`new`]: #method.new
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let raw = b"foo".to_vec();
+    /// unsafe {
+    ///     let c_string = CString::from_vec_unchecked(raw);
+    /// }
+    /// ```
     pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
         v.reserve_exact(1);
         v.push(0);
-        CString { inner: v.into_boxed_slice() }
+        CString {
+            inner: v.into_boxed_slice(),
+        }
     }
 
-    /// Retakes ownership of a `CString` that was transferred to C via [`into_raw`].
+    /// Retakes ownership of a `CString` that was transferred to C via
+    /// [`CString::into_raw`].
     ///
     /// Additionally, the length of the string will be recalculated from the pointer.
     ///
     /// # Safety
     ///
     /// This should only ever be called with a pointer that was earlier
-    /// obtained by calling [`into_raw`] on a `CString`. Other usage (e.g., trying to take
+    /// obtained by calling [`CString::into_raw`]. Other usage (e.g., trying to take
     /// ownership of a string that was allocated by foreign code) is likely to lead
     /// to undefined behavior or allocator corruption.
     ///
+    /// It should be noted that the length isn't just "recomputed," but that
+    /// the recomputed length must match the original length from the
+    /// [`CString::into_raw`] call. This means the [`CString::into_raw`]/`from_raw`
+    /// methods should not be used when passing the string to C functions that can
+    /// modify the string's length.
+    ///
     /// > **Note:** If you need to borrow a string that was allocated by
     /// > foreign code, use [`CStr`]. If you need to take ownership of
     /// > a string that was allocated by foreign code, you will need to
     /// > make your own provisions for freeing it appropriately, likely
     /// > with the foreign code's API to do that.
     ///
-    /// [`into_raw`]: #method.into_raw
-    /// [`CStr`]: struct.CStr.html
+    /// # Examples
     ///
+    /// Creates a `CString`, pass ownership to an `extern` function (via raw pointer), then retake
+    /// ownership with `from_raw`:
+    ///
+    /// ```ignore (extern-declaration)
+    /// use std::ffi::CString;
+    /// use std::os::raw::c_char;
+    ///
+    /// extern "C" {
+    ///     fn some_extern_function(s: *mut c_char);
+    /// }
+    ///
+    /// let c_string = CString::new("Hello!").expect("CString::new failed");
+    /// let raw = c_string.into_raw();
+    /// unsafe {
+    ///     some_extern_function(raw);
+    ///     let c_string = CString::from_raw(raw);
+    /// }
+    /// ```
     pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
+        // SAFETY: This is called with a pointer that was obtained from a call
+        // to `CString::into_raw` and the length has not been modified. As such,
+        // we know there is a NUL byte (and only one) at the end and that the
+        // information about the size of the allocation is correct on Rust's
+        // side.
         let len = libc::strlen(ptr) + 1; // Including the NUL byte
         let slice = slice::from_raw_parts_mut(ptr, len as usize);
-        CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
+        CString {
+            inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]),
+        }
     }
 
     /// Consumes the `CString` and transfers ownership of the string to a C caller.
     ///
     /// The pointer which this function returns must be returned to Rust and reconstituted using
-    /// [`from_raw`] to be properly deallocated. Specifically, one
+    /// [`CString::from_raw`] to be properly deallocated. Specifically, one
     /// should *not* use the standard C `free()` function to deallocate
     /// this string.
     ///
-    /// Failure to call [`from_raw`] will lead to a memory leak.
+    /// Failure to call [`CString::from_raw`] will lead to a memory leak.
     ///
-    /// [`from_raw`]: #method.from_raw
+    /// The C side must **not** modify the length of the string (by writing a
+    /// `null` somewhere inside the string or removing the final one) before
+    /// it makes it back into Rust using [`CString::from_raw`]. See the safety section
+    /// in [`CString::from_raw`].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new("foo").expect("CString::new failed");
+    ///
+    /// let ptr = c_string.into_raw();
+    ///
+    /// unsafe {
+    ///     assert_eq!(b'f', *ptr as u8);
+    ///     assert_eq!(b'o', *ptr.offset(1) as u8);
+    ///     assert_eq!(b'o', *ptr.offset(2) as u8);
+    ///     assert_eq!(b'\0', *ptr.offset(3) as u8);
+    ///
+    ///     // retake pointer to free memory
+    ///     let _ = CString::from_raw(ptr);
+    /// }
+    /// ```
     #[inline]
     pub fn into_raw(self) -> *mut c_char {
         Box::into_raw(self.into_inner()) as *mut c_char
@@ -292,8 +600,21 @@
     ///
     /// On failure, ownership of the original `CString` is returned.
     ///
-    /// [`String`]: ../string/struct.String.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let valid_utf8 = vec![b'f', b'o', b'o'];
+    /// let cstring = CString::new(valid_utf8).expect("CString::new failed");
+    /// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
+    ///
+    /// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
+    /// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
+    /// let err = cstring.into_string().err().expect("into_string().err() failed");
+    /// assert_eq!(err.utf8_error().valid_up_to(), 1);
+    /// ```
+
     pub fn into_string(self) -> Result<String, IntoStringError> {
         String::from_utf8(self.into_bytes()).map_err(|e| IntoStringError {
             error: e.utf8_error(),
@@ -307,6 +628,15 @@
     /// terminator, and it is guaranteed to not have any interior nul
     /// bytes.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new("foo").expect("CString::new failed");
+    /// let bytes = c_string.into_bytes();
+    /// assert_eq!(bytes, vec![b'f', b'o', b'o']);
+    /// ```
     pub fn into_bytes(self) -> Vec<u8> {
         let mut vec = self.into_inner().into_vec();
         let _nul = vec.pop();
@@ -314,11 +644,18 @@
         vec
     }
 
-    /// Equivalent to the [`into_bytes`] function except that the returned vector
-    /// includes the trailing nul terminator.
+    /// Equivalent to [`CString::into_bytes()`] except that the
+    /// returned vector includes the trailing nul terminator.
     ///
-    /// [`into_bytes`]: #method.into_bytes
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new("foo").expect("CString::new failed");
+    /// let bytes = c_string.into_bytes_with_nul();
+    /// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
+    /// ```
     pub fn into_bytes_with_nul(self) -> Vec<u8> {
         self.into_inner().into_vec()
     }
@@ -328,23 +665,52 @@
     /// The returned slice does **not** contain the trailing nul
     /// terminator, and it is guaranteed to not have any interior nul
     /// bytes. If you need the nul terminator, use
-    /// [`as_bytes_with_nul`] instead.
+    /// [`CString::as_bytes_with_nul`] instead.
     ///
-    /// [`as_bytes_with_nul`]: #method.as_bytes_with_nul
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new("foo").expect("CString::new failed");
+    /// let bytes = c_string.as_bytes();
+    /// assert_eq!(bytes, &[b'f', b'o', b'o']);
+    /// ```
     #[inline]
     pub fn as_bytes(&self) -> &[u8] {
-        &self.inner[..self.inner.len() - 1]
+        // SAFETY: CString has a length at least 1
+        unsafe { self.inner.get_unchecked(..self.inner.len() - 1) }
     }
 
-    /// Equivalent to the [`as_bytes`] function except that the returned slice
-    /// includes the trailing nul terminator.
+    /// Equivalent to [`CString::as_bytes()`] except that the
+    /// returned slice includes the trailing nul terminator.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new("foo").expect("CString::new failed");
+    /// let bytes = c_string.as_bytes_with_nul();
+    /// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
+    /// ```
     #[inline]
     pub fn as_bytes_with_nul(&self) -> &[u8] {
         &self.inner
     }
 
     /// Extracts a [`CStr`] slice containing the entire string.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::{CString, CStr};
+    ///
+    /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
+    /// let cstr = c_string.as_c_str();
+    /// assert_eq!(cstr,
+    ///            CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
+    /// ```
     #[inline]
     pub fn as_c_str(&self) -> &CStr {
         &*self
@@ -352,15 +718,22 @@
 
     /// Converts this `CString` into a boxed [`CStr`].
     ///
-    /// [`CStr`]: struct.CStr.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::{CString, CStr};
+    ///
+    /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
+    /// let boxed = c_string.into_boxed_c_str();
+    /// assert_eq!(&*boxed,
+    ///            CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
+    /// ```
     pub fn into_boxed_c_str(self) -> Box<CStr> {
         unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
     }
 
     /// Bypass "move out of struct which implements [`Drop`] trait" restriction.
-    ///
-    /// [`Drop`]: ../ops/trait.Drop.html
+    #[inline]
     fn into_inner(self) -> Box<[u8]> {
         // Rationale: `mem::forget(self)` invalidates the previous call to `ptr::read(&self.inner)`
         // so we use `ManuallyDrop` to ensure `self` is not dropped.
@@ -369,6 +742,84 @@
         let this = mem::ManuallyDrop::new(self);
         unsafe { ptr::read(&this.inner) }
     }
+
+    /// Converts a [`Vec`]`<u8>` to a [`CString`] without checking the
+    /// invariants on the given [`Vec`].
+    ///
+    /// # Safety
+    ///
+    /// The given [`Vec`] **must** have one nul byte as its last element.
+    /// This means it cannot be empty nor have any other nul byte anywhere else.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// #![feature(cstring_from_vec_with_nul)]
+    /// use std::ffi::CString;
+    /// assert_eq!(
+    ///     unsafe { CString::from_vec_with_nul_unchecked(b"abc\0".to_vec()) },
+    ///     unsafe { CString::from_vec_unchecked(b"abc".to_vec()) }
+    /// );
+    /// ```
+    pub unsafe fn from_vec_with_nul_unchecked(v: Vec<u8>) -> Self {
+        Self {
+            inner: v.into_boxed_slice(),
+        }
+    }
+
+    /// Attempts to converts a [`Vec`]`<u8>` to a [`CString`].
+    ///
+    /// Runtime checks are present to ensure there is only one nul byte in the
+    /// [`Vec`], its last element.
+    ///
+    /// # Errors
+    ///
+    /// If a nul byte is present and not the last element or no nul bytes
+    /// is present, an error will be returned.
+    ///
+    /// # Examples
+    ///
+    /// A successful conversion will produce the same result as [`CString::new`]
+    /// when called without the ending nul byte.
+    ///
+    /// ```
+    /// #![feature(cstring_from_vec_with_nul)]
+    /// use std::ffi::CString;
+    /// assert_eq!(
+    ///     CString::from_vec_with_nul(b"abc\0".to_vec())
+    ///         .expect("CString::from_vec_with_nul failed"),
+    ///     CString::new(b"abc".to_vec()).expect("CString::new failed")
+    /// );
+    /// ```
+    ///
+    /// An incorrectly formatted [`Vec`] will produce an error.
+    ///
+    /// ```
+    /// #![feature(cstring_from_vec_with_nul)]
+    /// use std::ffi::{CString, FromVecWithNulError};
+    /// // Interior nul byte
+    /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"a\0bc".to_vec()).unwrap_err();
+    /// // No nul byte
+    /// let _: FromVecWithNulError = CString::from_vec_with_nul(b"abc".to_vec()).unwrap_err();
+    /// ```
+    pub fn from_vec_with_nul(v: Vec<u8>) -> Result<Self, FromVecWithNulError> {
+        let nul_pos = memchr::memchr(0, &v);
+        match nul_pos {
+            Some(nul_pos) if nul_pos + 1 == v.len() => {
+                // SAFETY: We know there is only one nul byte, at the end
+                // of the vec.
+                Ok(unsafe { Self::from_vec_with_nul_unchecked(v) })
+            }
+            Some(nul_pos) => Err(FromVecWithNulError {
+                error_kind: FromBytesWithNulErrorKind::InteriorNul(nul_pos),
+                bytes: v,
+            }),
+            None => Err(FromVecWithNulError {
+                error_kind: FromBytesWithNulErrorKind::NotNulTerminated,
+                bytes: v,
+            }),
+        }
+    }
 }
 
 // Turns this `CString` into an empty string to prevent
@@ -402,9 +853,6 @@
     /// Converts a [`CString`] into a [`Vec`]`<u8>`.
     ///
     /// The conversion consumes the [`CString`], and removes the terminating NUL byte.
-    ///
-    /// [`Vec`]: ../vec/struct.Vec.html
-    /// [`CString`]: ../ffi/struct.CString.html
     #[inline]
     fn from(s: CString) -> Vec<u8> {
         s.into_bytes()
@@ -414,7 +862,11 @@
 impl fmt::Debug for CStr {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "\"")?;
-        for byte in self.to_bytes().iter().flat_map(|&b| ascii::escape_default(b)) {
+        for byte in self
+            .to_bytes()
+            .iter()
+            .flat_map(|&b| ascii::escape_default(b))
+        {
             f.write_char(byte as char)?;
         }
         write!(f, "\"")
@@ -457,11 +909,18 @@
     }
 }
 
+impl From<Cow<'_, CStr>> for Box<CStr> {
+    #[inline]
+    fn from(cow: Cow<'_, CStr>) -> Box<CStr> {
+        match cow {
+            Cow::Borrowed(s) => Box::from(s),
+            Cow::Owned(s) => Box::from(s),
+        }
+    }
+}
+
 impl From<Box<CStr>> for CString {
     /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
-    ///
-    /// [`Box`]: ../boxed/struct.Box.html
-    /// [`CString`]: ../ffi/struct.CString.html
     #[inline]
     fn from(s: Box<CStr>) -> CString {
         s.into_c_string()
@@ -471,22 +930,18 @@
 impl From<Vec<NonZeroU8>> for CString {
     /// Converts a [`Vec`]`<`[`NonZeroU8`]`>` into a [`CString`] without
     /// copying nor checking for inner null bytes.
-    ///
-    /// [`CString`]: ../ffi/struct.CString.html
-    /// [`NonZeroU8`]: ../num/struct.NonZeroU8.html
-    /// [`Vec`]: ../vec/struct.Vec.html
     #[inline]
     fn from(v: Vec<NonZeroU8>) -> CString {
         unsafe {
             // Transmute `Vec<NonZeroU8>` to `Vec<u8>`.
             let v: Vec<u8> = {
-                // Safety:
+                // SAFETY:
                 //   - transmuting between `NonZeroU8` and `u8` is sound;
                 //   - `alloc::Layout<NonZeroU8> == alloc::Layout<u8>`.
                 let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v);
                 Vec::from_raw_parts(ptr.cast::<u8>(), len, cap)
             };
-            // Safety: `v` cannot contain null bytes, given the type-level
+            // SAFETY: `v` cannot contain null bytes, given the type-level
             // invariant of `NonZeroU8`.
             CString::from_vec_unchecked(v)
         }
@@ -502,9 +957,6 @@
 
 impl From<CString> for Box<CStr> {
     /// Converts a [`CString`] into a [`Box`]`<CStr>` without copying or allocating.
-    ///
-    /// [`CString`]: ../ffi/struct.CString.html
-    /// [`Box`]: ../boxed/struct.Box.html
     #[inline]
     fn from(s: CString) -> Box<CStr> {
         s.into_boxed_c_str()
@@ -533,10 +985,7 @@
 }
 
 impl From<CString> for Arc<CStr> {
-    /// Converts a [`CString`] into a [`Arc`]`<CStr>` without copying or allocating.
-    ///
-    /// [`CString`]: ../ffi/struct.CString.html
-    /// [`Arc`]: ../sync/struct.Arc.html
+    /// Converts a [`CString`] into an [`Arc`]`<CStr>` without copying or allocating.
     #[inline]
     fn from(s: CString) -> Arc<CStr> {
         let arc: Arc<[u8]> = Arc::from(s.into_inner());
@@ -553,10 +1002,7 @@
 }
 
 impl From<CString> for Rc<CStr> {
-    /// Converts a [`CString`] into a [`Rc`]`<CStr>` without copying or allocating.
-    ///
-    /// [`CString`]: ../ffi/struct.CString.html
-    /// [`Rc`]: ../rc/struct.Rc.html
+    /// Converts a [`CString`] into an [`Rc`]`<CStr>` without copying or allocating.
     #[inline]
     fn from(s: CString) -> Rc<CStr> {
         let rc: Rc<[u8]> = Rc::from(s.into_inner());
@@ -583,8 +1029,17 @@
     /// Returns the position of the nul byte in the slice that caused
     /// [`CString::new`] to fail.
     ///
-    /// [`CString::new`]: struct.CString.html#method.new
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let nul_error = CString::new("foo\0bar").unwrap_err();
+    /// assert_eq!(nul_error.nul_position(), 3);
+    ///
+    /// let nul_error = CString::new("foo bar\0").unwrap_err();
+    /// assert_eq!(nul_error.nul_position(), 7);
+    /// ```
     pub fn nul_position(&self) -> usize {
         self.0
     }
@@ -592,6 +1047,14 @@
     /// Consumes this error, returning the underlying vector of bytes which
     /// generated the error in the first place.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let nul_error = CString::new("foo\0bar").unwrap_err();
+    /// assert_eq!(nul_error.into_vec(), b"foo\0bar");
+    /// ```
     pub fn into_vec(self) -> Vec<u8> {
         self.1
     }
@@ -600,8 +1063,6 @@
 impl IntoStringError {
     /// Consumes this error, returning original [`CString`] which generated the
     /// error.
-    ///
-    /// [`CString`]: struct.CString.html
     pub fn into_cstring(self) -> CString {
         self.inner
     }
@@ -633,7 +1094,34 @@
     /// > currently implemented with an up-front calculation of the length of
     /// > the string. This is not guaranteed to always be the case.
     ///
+    /// # Examples
+    ///
+    /// ```ignore (extern-declaration)
+    /// # fn main() {
+    /// use std::ffi::CStr;
+    /// use std::os::raw::c_char;
+    ///
+    /// extern "C" {
+    ///     fn my_string() -> *const c_char;
+    /// }
+    ///
+    /// unsafe {
+    ///     let slice = CStr::from_ptr(my_string());
+    ///     println!("string returned: {}", slice.to_str().unwrap());
+    /// }
+    /// # }
+    /// ```
     pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
+        // SAFETY: The caller has provided a pointer that points to a valid C
+        // string with a NUL terminator of size less than `isize::MAX`, whose
+        // content remain valid and doesn't change for the lifetime of the
+        // returned `CStr`.
+        //
+        // Thus computing the length is fine (a NUL byte exists), the call to
+        // from_raw_parts is safe because we know the length is at most `isize::MAX`, meaning
+        // the call to `from_bytes_with_nul_unchecked` is correct.
+        //
+        // The cast from c_char to u8 is ok because a c_char is always one byte.
         let len = libc::strlen(ptr);
         let ptr = ptr as *const u8;
         CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
@@ -645,6 +1133,32 @@
     /// wrapper after ensuring that the byte slice is nul-terminated
     /// and does not contain any interior nul bytes.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"hello\0");
+    /// assert!(cstr.is_ok());
+    /// ```
+    ///
+    /// Creating a `CStr` without a trailing nul terminator is an error:
+    ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"hello");
+    /// assert!(cstr.is_err());
+    /// ```
+    ///
+    /// Creating a `CStr` with an interior nul byte is an error:
+    ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"he\0llo\0");
+    /// assert!(cstr.is_err());
+    /// ```
     pub fn from_bytes_with_nul(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError> {
         let nul_pos = memchr::memchr(0, bytes);
         if let Some(nul_pos) = nul_pos {
@@ -663,8 +1177,24 @@
     /// performing any sanity checks. The provided slice **must** be nul-terminated
     /// and not contain any interior nul bytes.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::{CStr, CString};
+    ///
+    /// unsafe {
+    ///     let cstring = CString::new("hello").expect("CString::new failed");
+    ///     let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
+    ///     assert_eq!(cstr, &*cstring);
+    /// }
+    /// ```
     #[inline]
     pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
+        // SAFETY: Casting to CStr is safe because its internal representation
+        // is a [u8] too (safe only inside std).
+        // Dereferencing the obtained pointer is safe because it comes from a
+        // reference. Making a reference is then safe because its lifetime
+        // is bound by the lifetime of the given `bytes`.
         &*(bytes as *const [u8] as *const CStr)
     }
 
@@ -674,6 +1204,46 @@
     /// to a contiguous region of memory terminated with a 0 byte to represent
     /// the end of the string.
     ///
+    /// **WARNING**
+    ///
+    /// The returned pointer is read-only; writing to it (including passing it
+    /// to C code that writes to it) causes undefined behavior.
+    ///
+    /// It is your responsibility to make sure that the underlying memory is not
+    /// freed too early. For example, the following code will cause undefined
+    /// behavior when `ptr` is used inside the `unsafe` block:
+    ///
+    /// ```no_run
+    /// # #![allow(unused_must_use)] #![allow(temporary_cstring_as_ptr)]
+    /// use std::ffi::CString;
+    ///
+    /// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
+    /// unsafe {
+    ///     // `ptr` is dangling
+    ///     *ptr;
+    /// }
+    /// ```
+    ///
+    /// This happens because the pointer returned by `as_ptr` does not carry any
+    /// lifetime information and the [`CString`] is deallocated immediately after
+    /// the `CString::new("Hello").expect("CString::new failed").as_ptr()`
+    /// expression is evaluated.
+    /// To fix the problem, bind the `CString` to a local variable:
+    ///
+    /// ```no_run
+    /// # #![allow(unused_must_use)]
+    /// use std::ffi::CString;
+    ///
+    /// let hello = CString::new("Hello").expect("CString::new failed");
+    /// let ptr = hello.as_ptr();
+    /// unsafe {
+    ///     // `ptr` is valid because `hello` is in scope
+    ///     *ptr;
+    /// }
+    /// ```
+    ///
+    /// This way, the lifetime of the [`CString`] in `hello` encompasses
+    /// the lifetime of `ptr` and the `unsafe` block.
     #[inline]
     pub const fn as_ptr(&self) -> *const c_char {
         self.inner.as_ptr()
@@ -688,23 +1258,38 @@
     /// > cast, but it is planned to alter its definition in the future to
     /// > perform the length calculation whenever this method is called.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
+    /// assert_eq!(cstr.to_bytes(), b"foo");
+    /// ```
     #[inline]
     pub fn to_bytes(&self) -> &[u8] {
         let bytes = self.to_bytes_with_nul();
-        &bytes[..bytes.len() - 1]
+        // SAFETY: to_bytes_with_nul returns slice with length at least 1
+        unsafe { bytes.get_unchecked(..bytes.len() - 1) }
     }
 
     /// Converts this C string to a byte slice containing the trailing 0 byte.
     ///
-    /// This function is the equivalent of [`to_bytes`] except that it will retain
-    /// the trailing nul terminator instead of chopping it off.
+    /// This function is the equivalent of [`CStr::to_bytes`] except that it
+    /// will retain the trailing nul terminator instead of chopping it off.
     ///
     /// > **Note**: This method is currently implemented as a 0-cost cast, but
     /// > it is planned to alter its definition in the future to perform the
     /// > length calculation whenever this method is called.
     ///
-    /// [`to_bytes`]: #method.to_bytes
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
+    /// assert_eq!(cstr.to_bytes_with_nul(), b"foo\0");
+    /// ```
     #[inline]
     pub fn to_bytes_with_nul(&self) -> &[u8] {
         unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
@@ -716,8 +1301,16 @@
     /// function will return the corresponding [`&str`] slice. Otherwise,
     /// it will return an error with details of where UTF-8 validation failed.
     ///
-    /// [`&str`]: ../primitive.str.html
+    /// [`&str`]: prim@str
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
+    /// assert_eq!(cstr.to_str(), Ok("foo"));
+    /// ```
     pub fn to_str(&self) -> Result<&str, str::Utf8Error> {
         // N.B., when `CStr` is changed to perform the length check in `.to_bytes()`
         // instead of in `from_ptr()`, it may be worth considering if this should
@@ -735,25 +1328,58 @@
     /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD] and return a
     /// [`Cow`]`::`[`Owned`]`(`[`String`]`)` with the result.
     ///
-    /// [`Cow`]: ../borrow/enum.Cow.html
-    /// [`Borrowed`]: ../borrow/enum.Cow.html#variant.Borrowed
-    /// [`Owned`]: ../borrow/enum.Cow.html#variant.Owned
-    /// [`str`]: ../primitive.str.html
-    /// [`String`]: ../string/struct.String.html
-    /// [U+FFFD]: ../char/constant.REPLACEMENT_CHARACTER.html
+    /// [`str`]: primitive@str
+    /// [`&str`]: primitive@str
+    /// [`Borrowed`]: Cow::Borrowed
+    /// [`Owned`]: Cow::Owned
+    /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
     ///
+    /// # Examples
+    ///
+    /// Calling `to_string_lossy` on a `CStr` containing valid UTF-8:
+    ///
+    /// ```
+    /// use std::borrow::Cow;
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"Hello World\0")
+    ///                  .expect("CStr::from_bytes_with_nul failed");
+    /// assert_eq!(cstr.to_string_lossy(), Cow::Borrowed("Hello World"));
+    /// ```
+    ///
+    /// Calling `to_string_lossy` on a `CStr` containing invalid UTF-8:
+    ///
+    /// ```
+    /// use std::borrow::Cow;
+    /// use std::ffi::CStr;
+    ///
+    /// let cstr = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
+    ///                  .expect("CStr::from_bytes_with_nul failed");
+    /// assert_eq!(
+    ///     cstr.to_string_lossy(),
+    ///     Cow::Owned(String::from("Hello �World")) as Cow<'_, str>
+    /// );
+    /// ```
     pub fn to_string_lossy(&self) -> Cow<'_, str> {
         String::from_utf8_lossy(self.to_bytes())
     }
 
     /// Converts a [`Box`]`<CStr>` into a [`CString`] without copying or allocating.
     ///
-    /// [`Box`]: ../boxed/struct.Box.html
-    /// [`CString`]: struct.CString.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::CString;
+    ///
+    /// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
+    /// let boxed = c_string.into_boxed_c_str();
+    /// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
+    /// ```
     pub fn into_c_string(self: Box<CStr>) -> CString {
         let raw = Box::into_raw(self) as *mut [u8];
-        CString { inner: unsafe { Box::from_raw(raw) } }
+        CString {
+            inner: unsafe { Box::from_raw(raw) },
+        }
     }
 }
 
@@ -767,7 +1393,7 @@
 
 impl PartialOrd for CStr {
     fn partial_cmp(&self, other: &CStr) -> Option<Ordering> {
-        self.to_bytes().partial_cmp(&other.to_bytes())
+        self.to_bytes().partial_cmp(other.to_bytes())
     }
 }
 
@@ -781,7 +1407,9 @@
     type Owned = CString;
 
     fn to_owned(&self) -> CString {
-        CString { inner: self.to_bytes_with_nul().into() }
+        CString {
+            inner: self.to_bytes_with_nul().into(),
+        }
     }
 
     fn clone_into(&self, target: &mut CString) {
@@ -806,6 +1434,26 @@
     }
 }
 
+impl ops::Index<ops::RangeFrom<usize>> for CStr {
+    type Output = CStr;
+
+    fn index(&self, index: ops::RangeFrom<usize>) -> &CStr {
+        let bytes = self.to_bytes_with_nul();
+        // we need to manually check the starting index to account for the null
+        // byte, since otherwise we could get an empty string that doesn't end
+        // in a null.
+        if index.start < bytes.len() {
+            unsafe { CStr::from_bytes_with_nul_unchecked(&bytes[index.start..]) }
+        } else {
+            panic!(
+                "index out of bounds: the len is {} but the index is {}",
+                bytes.len(),
+                index.start
+            );
+        }
+    }
+}
+
 impl AsRef<CStr> for CStr {
     #[inline]
     fn as_ref(&self) -> &CStr {
@@ -819,4 +1467,3 @@
         self
     }
 }
-
diff --git a/sgx_trts/src/lib.rs b/sgx_trts/src/lib.rs
index e22093d..af78272 100644
--- a/sgx_trts/src/lib.rs
+++ b/sgx_trts/src/lib.rs
@@ -65,15 +65,17 @@
 #![no_std]
 #![cfg_attr(target_env = "sgx", feature(rustc_private))]
 
+#![allow(incomplete_features)]
 #![feature(allocator_api)]
 #![feature(asm)]
 #![feature(const_raw_ptr_deref)]
 #![allow(non_camel_case_types)]
 #![allow(non_upper_case_globals)]
 #![allow(non_snake_case)]
-#![feature(min_specialization)]
+#![feature(specialization)]
 #![feature(vec_into_raw_parts)]
 #![feature(toowned_clone_into)]
+#![feature(rustc_attrs)]
 
 #[cfg(target_env = "sgx")]
 extern crate sgx_types;
diff --git a/sgx_trts/src/memchr.rs b/sgx_trts/src/memchr.rs
index ea27c07..d876a36 100644
--- a/sgx_trts/src/memchr.rs
+++ b/sgx_trts/src/memchr.rs
@@ -15,28 +15,19 @@
 // specific language governing permissions and limitations
 // under the License..
 
-/// A safe interface to `memchr`.
-///
-/// Returns the index corresponding to the first occurrence of `needle` in
-/// `haystack`, or `None` if one is not found.
-///
-// #[inline]
-// pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-//     ::sys::memchr::memchr(needle, haystack)
-// }
-
 /// A safe interface to `memrchr`.
 ///
 /// Returns the index corresponding to the last occurrence of `needle` in
 /// `haystack`, or `None` if one is not found.
 ///
-// #[inline]
-// pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-//     ::sys::memchr::memrchr(needle, haystack)
-// }
-
 pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    let p = unsafe { sgx_libc::memchr(haystack.as_ptr(), needle, haystack.len()) };
+    let p = unsafe {
+        sgx_libc::memchr(
+            haystack.as_ptr(),
+            needle,
+            haystack.len(),
+        )
+    };
     if p.is_null() {
         None
     } else {
@@ -50,7 +41,13 @@
         if haystack.is_empty() {
             return None;
         }
-        let p = unsafe { sgx_libc::memrchr(haystack.as_ptr(), needle, haystack.len()) };
+        let p = unsafe {
+            sgx_libc::memrchr(
+                haystack.as_ptr(),
+                needle,
+                haystack.len(),
+            )
+        };
         if p.is_null() {
             None
         } else {
@@ -59,4 +56,4 @@
     }
 
     memrchr_specific(needle, haystack)
-}
+}
\ No newline at end of file
diff --git a/sgx_tse/Cargo.toml b/sgx_tse/Cargo.toml
index b824cd4..2ece588 100644
--- a/sgx_tse/Cargo.toml
+++ b/sgx_tse/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tse"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tseal/Cargo.toml b/sgx_tseal/Cargo.toml
index 6d712f3..180edd0 100644
--- a/sgx_tseal/Cargo.toml
+++ b/sgx_tseal/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tseal"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tseal/src/internal.rs b/sgx_tseal/src/internal.rs
index bf63b14..cdf17a6 100644
--- a/sgx_tseal/src/internal.rs
+++ b/sgx_tseal/src/internal.rs
@@ -62,7 +62,7 @@
 #[derive(Clone, Default)]
 struct SgxPayload {
     payload_size: u32,
-    reserved: [u8; 12],
+    _reserved: [u8; 12],
     payload_tag: [u8; SGX_SEAL_TAG_SIZE],
     encrypt: Box<[u8]>,
     additional: Box<[u8]>,
@@ -110,9 +110,7 @@
 
     pub fn get_add_mac_txt_len(&self) -> u32 {
         let data_size = self.payload_data.additional.len();
-        if data_size > self.payload_data.payload_size as usize
-            || data_size >= u32::MAX as usize
-        {
+        if data_size > self.payload_data.payload_size as usize || data_size >= u32::MAX as usize {
             u32::MAX
         } else {
             data_size as u32
@@ -121,9 +119,7 @@
 
     pub fn get_encrypt_txt_len(&self) -> u32 {
         let data_size = self.payload_data.encrypt.len();
-        if data_size > self.payload_data.payload_size as usize
-            || data_size >= u32::MAX as usize
-        {
+        if data_size > self.payload_data.payload_size as usize || data_size >= u32::MAX as usize {
             u32::MAX
         } else {
             data_size as u32
@@ -255,14 +251,16 @@
             Vec::new()
         };
 
-        let mut sealed_data = Self::default();
-        sealed_data.key_request = raw_sealed_data.key_request;
-        sealed_data.payload_data.payload_size = raw_sealed_data.aes_data.payload_size;
-        sealed_data.payload_data.payload_tag = raw_sealed_data.aes_data.payload_tag;
-        sealed_data.payload_data.additional = additional.into_boxed_slice();
-        sealed_data.payload_data.encrypt = encrypt.into_boxed_slice();
-
-        Some(sealed_data)
+        Some(Self {
+            key_request: raw_sealed_data.key_request,
+            payload_data: SgxPayload {
+                payload_size: raw_sealed_data.aes_data.payload_size,
+                _reserved: [0; 12],
+                payload_tag: raw_sealed_data.aes_data.payload_tag,
+                additional: additional.into_boxed_slice(),
+                encrypt: encrypt.into_boxed_slice(),
+            },
+        })
     }
 
     pub fn seal_data(additional_text: &[u8], encrypt_text: &[u8]) -> SgxResult<Self> {
@@ -298,14 +296,10 @@
         let additional_len = additional_text.len();
         let encrypt_len = encrypt_text.len();
 
-        if (additional_len >= u32::MAX as usize)
-            || (encrypt_len >= u32::MAX as usize)
-        {
+        if (additional_len >= u32::MAX as usize) || (encrypt_len >= u32::MAX as usize) {
             return Err(sgx_status_t::SGX_ERROR_INVALID_PARAMETER);
         }
-        if Self::calc_raw_sealed_data_size(additional_len as u32, encrypt_len as u32)
-            == u32::MAX
-        {
+        if Self::calc_raw_sealed_data_size(additional_len as u32, encrypt_len as u32) == u32::MAX {
             return Err(sgx_status_t::SGX_ERROR_INVALID_PARAMETER);
         }
         if encrypt_len == 0 {
@@ -347,10 +341,10 @@
         let mut report = rsgx_self_report();
 
         let error = rsgx_read_rand(&mut key_id.id);
-        if error.is_err() {
+        if let Err(e) = error {
             report = sgx_report_t::default();
             key_id = sgx_key_id_t::default();
-            return Err(error.unwrap_err());
+            return Err(e);
         }
 
         let key_request = sgx_key_request_t {
@@ -476,10 +470,10 @@
         let mut report = rsgx_self_report();
 
         let error = rsgx_read_rand(&mut key_id.id);
-        if error.is_err() {
+        if let Err(e) = error {
             report = sgx_report_t::default();
             key_id = sgx_key_id_t::default();
-            return Err(error.unwrap_err());
+            return Err(e);
         }
 
         let key_request = sgx_key_request_t {
@@ -558,13 +552,13 @@
             &seal_key.key,
             encrypt_text,
             payload_iv,
-            &additional_text,
+            additional_text,
             &mut sealed_data.payload_data.encrypt,
             &mut sealed_data.payload_data.payload_tag,
         );
-        if error.is_err() {
+        if let Err(e) = error {
             seal_key.key = sgx_key_128bit_t::default();
-            return Err(error.unwrap_err());
+            return Err(e);
         }
 
         sealed_data.payload_data.payload_size = (encrypt_text.len() + additional_text.len()) as u32;
@@ -598,8 +592,10 @@
         rsgx_lfence();
 
         let payload_iv = [0_u8; SGX_SEAL_IV_SIZE];
-        let mut unsealed_data: SgxInternalUnsealedData = SgxInternalUnsealedData::default();
-        unsealed_data.decrypt = vec![0_u8; self.payload_data.encrypt.len()].into_boxed_slice();
+        let mut unsealed_data = SgxInternalUnsealedData {
+            decrypt: vec![0_u8; self.payload_data.encrypt.len()].into_boxed_slice(),
+            ..Default::default()
+        };
 
         let error = rsgx_rijndael128GCM_decrypt(
             &seal_key.key,
@@ -609,9 +605,9 @@
             self.get_payload_tag(),
             &mut unsealed_data.decrypt,
         );
-        if error.is_err() {
+        if let Err(e) = error {
             seal_key.key = sgx_key_128bit_t::default();
-            return Err(error.unwrap_err());
+            return Err(e);
         }
 
         if self.payload_data.additional.len() > 0 {
diff --git a/sgx_tseal/src/lib.rs b/sgx_tseal/src/lib.rs
index e944626..a582f68 100644
--- a/sgx_tseal/src/lib.rs
+++ b/sgx_tseal/src/lib.rs
@@ -72,9 +72,14 @@
 //!
 
 #![no_std]
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
+
 #![allow(non_camel_case_types)]
 #![allow(unused_assignments)]
+#![allow(clippy::missing_safety_doc)]
 
 #[macro_use]
 extern crate alloc;
diff --git a/sgx_tstd/Cargo.toml b/sgx_tstd/Cargo.toml
index cb42c1b..2d27a6e 100644
--- a/sgx_tstd/Cargo.toml
+++ b/sgx_tstd/Cargo.toml
@@ -1,11 +1,10 @@
 [package]
 name = "sgx_tstd"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
-build = "build.rs"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tstd/build.rs b/sgx_tstd/build.rs
index 2dc2664..e387d71 100644
--- a/sgx_tstd/build.rs
+++ b/sgx_tstd/build.rs
@@ -17,7 +17,6 @@
 
 use std::env;
 use std::path::Path;
-use std::process::Command;
 
 fn main() {
     if cfg!(feature = "backtrace") {
@@ -38,74 +37,4 @@
         println!("cargo:rustc-link-search=native={}/lib64", sdk_dir);
         println!("cargo:rustc-link-lib=static=sgx_pthread");
     }
-
-    // since nightly-2020-11-26 (rustc 2020-11-25), auto_traits replaced
-    // optin_builtin_traits
-    // see https://github.com/rust-lang/rust/commit/810324d1f31eb8d75e8f0044df720652986ef133
-    if let Some(true) = is_min_date("2020-11-25") {
-        println!("cargo:rustc-cfg=enable_auto_traits");
-    }
-
-    // nightly-2021-02-08 (rustc 2021-02-07)
-    // https://github.com/rust-lang/rust/commit/dbdbd30bf2cb0d48c8bbce83c2458592664dbb18
-    if let Some(true) = is_min_date("2021-02-07") {
-        println!("cargo:rustc-cfg=derive_macros");
-    }
-
-    // nightly-2021-03-11 (rustc 2021-03-10)
-    // https://github.com/rust-lang/rust/commit/1ab9fe5d44860050232438967bbbf9bdc35dbde1
-    if let Some(true) = is_min_date("2021-03-10") {
-        println!("cargo:rustc-cfg=enable_prelude_version");
-    }
-}
-
-// code below copied from crate version_check
-// we want to remove the build dependencies to make the dependency tree
-// as clean as possible. the following codes credit to SergioBenitez
-#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
-struct Date(u32);
-
-impl Date {
-    fn read() -> Option<Date> {
-        get_version_and_date()
-            .and_then(|(_, date)| date)
-            .and_then(|date| Date::parse(&date))
-    }
-
-    fn parse(date: &str) -> Option<Date> {
-        let ymd: Vec<u32> = date.split("-")
-            .filter_map(|s| s.parse::<u32>().ok())
-            .collect();
-    
-        if ymd.len() != 3 {
-            return None
-        }
-    
-        let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
-        Some(Date((y << 9) | ((m & 0xF) << 5) | (d & 0x1F)))
-    }
-}
-
-fn get_version_and_date() -> Option<(Option<String>, Option<String>)> {
-    env::var("RUSTC").ok()
-        .and_then(|rustc| Command::new(rustc).arg("--version").output().ok())
-        .or_else(|| Command::new("rustc").arg("--version").output().ok())
-        .and_then(|output| String::from_utf8(output.stdout).ok())
-        .map(|s| version_and_date_from_rustc_version(&s))
-}
-
-fn version_and_date_from_rustc_version(s: &str) -> (Option<String>, Option<String>) {
-    let last_line = s.lines().last().unwrap_or(s);
-    let mut components = last_line.trim().split(" ");
-    let version = components.nth(1);
-    let date = components.filter(|c| c.ends_with(')')).next()
-        .map(|s| s.trim_end().trim_end_matches(")").trim_start().trim_start_matches('('));
-    (version.map(|s| s.to_string()), date.map(|s| s.to_string()))
-}
-
-fn is_min_date(min_date: &str) -> Option<bool> {
-    match (Date::read(), Date::parse(min_date)) {
-        (Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date),
-        _ => None
-    }
 }
diff --git a/sgx_tstd/hashbrown/.cargo_vcs_info.json b/sgx_tstd/hashbrown/.cargo_vcs_info.json
deleted file mode 100644
index ec4027b..0000000
--- a/sgx_tstd/hashbrown/.cargo_vcs_info.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-  "git": {
-    "sha1": "d4bb3ea3321a73549aaf6bcc06e9e5e3e68f4063"
-  }
-}
diff --git a/sgx_tstd/hashbrown/.gitignore b/sgx_tstd/hashbrown/.gitignore
deleted file mode 100644
index 6936990..0000000
--- a/sgx_tstd/hashbrown/.gitignore
+++ /dev/null
@@ -1,3 +0,0 @@
-/target
-**/*.rs.bk
-Cargo.lock
diff --git a/sgx_tstd/hashbrown/CHANGELOG.md b/sgx_tstd/hashbrown/CHANGELOG.md
index e349925..c88d3e0 100644
--- a/sgx_tstd/hashbrown/CHANGELOG.md
+++ b/sgx_tstd/hashbrown/CHANGELOG.md
@@ -7,6 +7,64 @@
 
 ## [Unreleased]
 
+## [v0.11.2] - 2021-03-25
+
+## Fixed
+
+- Added missing allocator type parameter to `HashMap`'s and `HashSet`'s `Clone` impls. (#252)
+
+## [v0.11.1] - 2021-03-20
+
+## Fixed
+
+- Added missing `pub` modifier to `BumpWrapper`. (#251)
+
+## [v0.11.0] - 2021-03-14
+
+## Added
+- Added safe `try_insert_no_grow` method to `RawTable`. (#229)
+- Added support for `bumpalo` as an allocator without the `nightly` feature. (#231)
+- Implemented `Default` for `RawTable`. (#237)
+- Added new safe methods `RawTable::get_each_mut`, `HashMap::get_each_mut`, and
+  `HashMap::get_each_key_value_mut`. (#239)
+- Added `From<HashMap<T, ()>>` for `HashSet<T>`. (#235)
+- Added `try_insert` method to `HashMap`. (#247)
+
+## Changed
+- The minimum Rust version has been bumped to 1.49.0. (#230)
+- Significantly improved compilation times by reducing the amount of generated IR. (#205)
+
+## Removed
+- We no longer re-export the unstable allocator items from the standard library, nor the stable shims approximating the same. (#227)
+- Removed hasher specialization support from `aHash`, which was resulting in inconsistent hashes being generated for a key. (#248)
+
+## Fixed
+- Fixed union length comparison. (#228)
+
+## ~~[v0.10.0] - 2021-01-16~~
+
+This release was _yanked_ due to inconsistent hashes being generated with the `nightly` feature. (#248)
+
+## Changed
+- Parametrized `RawTable`, `HashSet` and `HashMap` over an allocator. (#133)
+- Improved branch prediction hints on stable. (#209)
+- Optimized hashing of primitive types with AHash using specialization. (#207)
+- Only instantiate `RawTable`'s reserve functions once per key-value. (#204)
+
+## [v0.9.1] - 2020-09-28
+
+## Added
+- Added safe methods to `RawTable` (#202):
+  - `get`: `find` and `as_ref`
+  - `get_mut`: `find` and `as_mut`
+  - `insert_entry`: `insert` and `as_mut`
+  - `remove_entry`: `find` and `remove`
+  - `erase_entry`: `find` and `erase`
+
+## Changed
+- Removed `from_key_hashed_nocheck`'s `Q: Hash`. (#200)
+- Made `RawTable::drain` safe. (#201)
+
 ## [v0.9.0] - 2020-09-03
 
 ### Fixed
@@ -249,7 +307,12 @@
 
 - Initial release
 
-[Unreleased]: https://github.com/rust-lang/hashbrown/compare/v0.9.0...HEAD
+[Unreleased]: https://github.com/rust-lang/hashbrown/compare/v0.11.2...HEAD
+[v0.11.2]: https://github.com/rust-lang/hashbrown/compare/v0.11.1...v0.11.2
+[v0.11.1]: https://github.com/rust-lang/hashbrown/compare/v0.11.0...v0.11.1
+[v0.11.0]: https://github.com/rust-lang/hashbrown/compare/v0.10.0...v0.11.0
+[v0.10.0]: https://github.com/rust-lang/hashbrown/compare/v0.9.1...v0.10.0
+[v0.9.1]: https://github.com/rust-lang/hashbrown/compare/v0.9.0...v0.9.1
 [v0.9.0]: https://github.com/rust-lang/hashbrown/compare/v0.8.2...v0.9.0
 [v0.8.2]: https://github.com/rust-lang/hashbrown/compare/v0.8.1...v0.8.2
 [v0.8.1]: https://github.com/rust-lang/hashbrown/compare/v0.8.0...v0.8.1
diff --git a/sgx_tstd/hashbrown/Cargo.toml b/sgx_tstd/hashbrown/Cargo.toml
index 6ed051b..7bcb6ba 100644
--- a/sgx_tstd/hashbrown/Cargo.toml
+++ b/sgx_tstd/hashbrown/Cargo.toml
@@ -13,7 +13,7 @@
 [package]
 edition = "2018"
 name = "hashbrown_tstd"
-version = "0.9.0"
+version = "0.11.2"
 authors = ["Amanieu d'Antras <amanieu@gmail.com>"]
 exclude = [".travis.yml", "bors.toml", "/ci/*"]
 description = "A Rust port of Google's SwissTable hash map"
@@ -25,23 +25,27 @@
 [package.metadata.docs.rs]
 features = ["nightly", "rayon", "serde", "raw"]
 [dependencies.ahash]
-version = "0.4.4"
+version = "0.7.0"
 optional = true
 default-features = false
 
-#[dependencies.alloc]
-#version = "1.0.0"
-#optional = true
-#package = "rustc-std-workspace-alloc"
+# [dependencies.alloc]
+# version = "1.0.0"
+# optional = true
+# package = "rustc-std-workspace-alloc"
 
-#[dependencies.compiler_builtins]
-#version = "0.1.2"
-#optional = true
+[dependencies.bumpalo]
+version = "3.5.0"
+optional = true
 
-#[dependencies.core]
-#version = "1.0.0"
-#optional = true
-#package = "rustc-std-workspace-core"
+# [dependencies.compiler_builtins]
+# version = "0.1.2"
+# optional = true
+
+# [dependencies.core]
+# version = "1.0.0"
+# optional = true
+# package = "rustc-std-workspace-core"
 
 [dependencies.rayon]
 version = "1.0"
@@ -54,8 +58,11 @@
 [dev-dependencies.doc-comment]
 version = "0.3.1"
 
+[dev-dependencies.fnv]
+version = "1.0.7"
+
 [dev-dependencies.lazy_static]
-version = "1.2"
+version = "1.4"
 
 [dev-dependencies.rand]
 version = "0.7.3"
@@ -64,9 +71,6 @@
 [dev-dependencies.rayon]
 version = "1.0"
 
-[dev-dependencies.rustc-hash]
-version = "=1.0"
-
 [dev-dependencies.serde_test]
 version = "1.0"
 
@@ -76,8 +80,6 @@
 inline-more = []
 nightly = []
 raw = []
-rustc-dep-of-std = ["nightly", "core", "compiler_builtins", "alloc", "rustc-internal-api"]
+# rustc-dep-of-std = ["nightly", "core", "compiler_builtins", "alloc", "rustc-internal-api"]
+rustc-dep-of-std = ["nightly", "rustc-internal-api"]
 rustc-internal-api = []
-core = []
-compiler_builtins = []
-alloc = []
diff --git a/sgx_tstd/hashbrown/Cargo.toml.orig b/sgx_tstd/hashbrown/Cargo.toml.orig
index 25a1709..a056c3c 100644
--- a/sgx_tstd/hashbrown/Cargo.toml.orig
+++ b/sgx_tstd/hashbrown/Cargo.toml.orig
@@ -1,6 +1,6 @@
 [package]
 name = "hashbrown"
-version = "0.9.0"
+version = "0.11.2"
 authors = ["Amanieu d'Antras <amanieu@gmail.com>"]
 description = "A Rust port of Google's SwissTable hash map"
 license = "Apache-2.0/MIT"
@@ -13,7 +13,7 @@
 
 [dependencies]
 # For the default hasher
-ahash = { version = "0.4.4", optional = true, default-features = false }
+ahash = { version = "0.7.0", default-features = false, optional = true }
 
 # For external trait impls
 rayon = { version = "1.0", optional = true }
@@ -24,11 +24,14 @@
 compiler_builtins = { version = "0.1.2", optional = true }
 alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
 
+# Optional support for bumpalo
+bumpalo = { version = "3.5.0", optional = true }
+
 [dev-dependencies]
-lazy_static = "1.2"
+lazy_static = "1.4"
 rand = { version = "0.7.3", features = ["small_rng"] }
 rayon = "1.0"
-rustc-hash = "=1.0"
+fnv = "1.0.7"
 serde_test = "1.0"
 doc-comment = "0.3.1"
 
diff --git a/sgx_tstd/hashbrown/README.md b/sgx_tstd/hashbrown/README.md
index f3de05a..86664c4 100644
--- a/sgx_tstd/hashbrown/README.md
+++ b/sgx_tstd/hashbrown/README.md
@@ -4,6 +4,7 @@
 [![Build Status](https://travis-ci.com/rust-lang/hashbrown.svg?branch=master)](https://travis-ci.com/rust-lang/hashbrown)
 [![Crates.io](https://img.shields.io/crates/v/hashbrown.svg)](https://crates.io/crates/hashbrown)
 [![Documentation](https://docs.rs/hashbrown/badge.svg)](https://docs.rs/hashbrown)
+[![Rust](https://img.shields.io/badge/rust-1.49.0%2B-blue.svg?maxAge=3600)](https://github.com/rust-lang/hashbrown)
 
 This crate is a Rust port of Google's high-performance [SwissTable] hash
 map, adapted to make it a drop-in replacement for Rust's standard `HashMap`
@@ -25,7 +26,8 @@
 ## Features
 
 - Drop-in replacement for the standard library `HashMap` and `HashSet` types.
-- Uses `AHash` as the default hasher, which is much faster than SipHash.
+- Uses [AHash](https://github.com/tkaitchuck/aHash) as the default hasher, which is much faster than SipHash.
+  However, AHash does *not provide the same level of HashDoS resistance* as SipHash, so if that is important to you, you might want to consider using a different hasher.
 - Around 2x faster than the previous standard library `HashMap`.
 - Lower memory usage: only 1 byte of overhead per entry instead of 8.
 - Compatible with `#[no_std]` (but requires a global allocator with the `alloc` crate).
@@ -36,47 +38,46 @@
 
 Compared to the previous implementation of `std::collections::HashMap` (Rust 1.35).
 
-With the hashbrown default AHash hasher (not HashDoS-resistant):
+With the hashbrown default AHash hasher:
 
-```text
- name                       oldstdhash ns/iter  hashbrown ns/iter  diff ns/iter   diff %  speedup 
- insert_ahash_highbits        20,846              7,397                   -13,449  -64.52%   x 2.82 
- insert_ahash_random          20,515              7,796                   -12,719  -62.00%   x 2.63 
- insert_ahash_serial          21,668              7,264                   -14,404  -66.48%   x 2.98 
- insert_erase_ahash_highbits  29,570              17,498                  -12,072  -40.83%   x 1.69 
- insert_erase_ahash_random    39,569              17,474                  -22,095  -55.84%   x 2.26 
- insert_erase_ahash_serial    32,073              17,332                  -14,741  -45.96%   x 1.85 
- iter_ahash_highbits          1,572               2,087                       515   32.76%   x 0.75 
- iter_ahash_random            1,609               2,074                       465   28.90%   x 0.78 
- iter_ahash_serial            2,293               2,120                      -173   -7.54%   x 1.08 
- lookup_ahash_highbits        3,460               4,403                       943   27.25%   x 0.79 
- lookup_ahash_random          6,377               3,911                    -2,466  -38.67%   x 1.63 
- lookup_ahash_serial          3,629               3,586                       -43   -1.18%   x 1.01 
- lookup_fail_ahash_highbits   5,286               3,411                    -1,875  -35.47%   x 1.55 
- lookup_fail_ahash_random     12,365              4,171                    -8,194  -66.27%   x 2.96 
- lookup_fail_ahash_serial     4,902               3,240                    -1,662  -33.90%   x 1.51 
-```
+| name                    |  oldstdhash ns/iter | hashbrown ns/iter | diff ns/iter |  diff %  | speedup |
+|:------------------------|:-------------------:|------------------:|:------------:|---------:|---------|
+| insert_ahash_highbits       | 18,865      | 8,020         |               -10,845 | -57.49%  | x 2.35 |
+| insert_ahash_random         | 19,711      | 8,019         |               -11,692 | -59.32%  | x 2.46 |
+| insert_ahash_serial         | 19,365      | 6,463         |               -12,902 | -66.63%  | x 3.00 |
+| insert_erase_ahash_highbits | 51,136      | 17,916        |               -33,220 | -64.96%  | x 2.85 |
+| insert_erase_ahash_random   | 51,157      | 17,688        |               -33,469 | -65.42%  | x 2.89 |
+| insert_erase_ahash_serial   | 45,479      | 14,895        |               -30,584 | -67.25%  | x 3.05 |
+| iter_ahash_highbits         | 1,399       | 1,092         |                  -307 | -21.94%  | x 1.28 |
+| iter_ahash_random           | 1,586       | 1,059         |                  -527 | -33.23%  | x 1.50 |
+| iter_ahash_serial           | 3,168       | 1,079         |                -2,089 | -65.94%  | x 2.94 |
+| lookup_ahash_highbits       | 32,351      | 4,792         |               -27,559 | -85.19%  | x 6.75 |
+| lookup_ahash_random         | 17,419      | 4,817         |               -12,602 | -72.35%  | x 3.62 |
+| lookup_ahash_serial         | 15,254      | 3,606         |               -11,648 | -76.36%  | x 4.23 |
+| lookup_fail_ahash_highbits  | 21,187      | 4,369         |               -16,818 | -79.38%  | x 4.85 |
+| lookup_fail_ahash_random    | 21,550      | 4,395         |               -17,155 | -79.61%  | x 4.90 |
+| lookup_fail_ahash_serial    | 19,450      | 3,176         |               -16,274 | -83.67%  | x 6.12 |
 
-With the libstd default SipHash hasher (HashDoS-resistant):
 
-```text
- name                       oldstdhash ns/iter  hashbrown ns/iter  diff ns/iter   diff %  speedup 
- insert_std_highbits        32,598              20,199                  -12,399  -38.04%   x 1.61 
- insert_std_random          29,824              20,760                   -9,064  -30.39%   x 1.44 
- insert_std_serial          33,151              17,256                  -15,895  -47.95%   x 1.92 
- insert_erase_std_highbits  74,731              48,735                  -25,996  -34.79%   x 1.53 
- insert_erase_std_random    73,828              47,649                  -26,179  -35.46%   x 1.55 
- insert_erase_std_serial    73,864              40,147                  -33,717  -45.65%   x 1.84 
- iter_std_highbits          1,518               2,264                       746   49.14%   x 0.67 
- iter_std_random            1,502               2,414                       912   60.72%   x 0.62 
- iter_std_serial            6,361               2,118                    -4,243  -66.70%   x 3.00 
- lookup_std_highbits        21,705              16,962                   -4,743  -21.85%   x 1.28 
- lookup_std_random          21,654              17,158                   -4,496  -20.76%   x 1.26 
- lookup_std_serial          18,726              14,509                   -4,217  -22.52%   x 1.29 
- lookup_fail_std_highbits   25,852              17,323                   -8,529  -32.99%   x 1.49 
- lookup_fail_std_random     25,913              17,760                   -8,153  -31.46%   x 1.46 
- lookup_fail_std_serial     22,648              14,839                   -7,809  -34.48%   x 1.53 
-```
+With the libstd default SipHash hasher:
+
+|name                     |  oldstdhash ns/iter | hashbrown ns/iter | diff ns/iter |  diff %  | speedup |
+|:------------------------|:-------------------:|------------------:|:------------:|---------:|---------|
+|insert_std_highbits       |19,216      |16,885           |            -2,331    |   -12.13%  | x 1.14 |
+|insert_std_random         |19,179      |17,034           |            -2,145    |   -11.18%  | x 1.13 |
+|insert_std_serial         |19,462      |17,493           |            -1,969    |   -10.12%  | x 1.11 |
+|insert_erase_std_highbits |50,825      |35,847           |            -14,978   |   -29.47%  | x 1.42 |
+|insert_erase_std_random   |51,448      |35,392           |            -16,056   |   -31.21%  | x 1.45 |
+|insert_erase_std_serial   |87,711      |38,091           |            -49,620   |   -56.57%  | x 2.30 |
+|iter_std_highbits         |1,378       |1,159            |            -219      |   -15.89%  | x 1.19 |
+|iter_std_random           |1,395       |1,132            |            -263      |   -18.85%  | x 1.23 |
+|iter_std_serial           |1,704       |1,105            |            -599      |   -35.15%  | x 1.54 |
+|lookup_std_highbits       |17,195      |13,642           |            -3,553    |   -20.66%  | x 1.26 |
+|lookup_std_random         |17,181      |13,773           |            -3,408    |   -19.84%  | x 1.25 |
+|lookup_std_serial         |15,483      |13,651           |            -1,832    |   -11.83%  | x 1.13 |
+|lookup_fail_std_highbits  |20,926      |13,474           |            -7,452    |   -35.61%  | x 1.55 |
+|lookup_fail_std_random    |21,766      |13,505           |            -8,261    |   -37.95%  | x 1.61 |
+|lookup_fail_std_serial    |19,336      |13,519           |            -5,817    |   -30.08%  | x 1.43 |
 
 ## Usage
 
@@ -84,7 +85,7 @@
 
 ```toml
 [dependencies]
-hashbrown = "0.8"
+hashbrown = "0.11"
 ```
 
 Then:
@@ -95,19 +96,19 @@
 let mut map = HashMap::new();
 map.insert(1, "one");
 ```
-
+## Flags
 This crate has the following Cargo features:
 
-- `nightly`: Enables nightly-only features: `#[may_dangle]`.
+- `nightly`: Enables nightly-only features including: `#[may_dangle]`.
 - `serde`: Enables serde serialization support.
 - `rayon`: Enables rayon parallel iterator support.
 - `raw`: Enables access to the experimental and unsafe `RawTable` API.
 - `inline-more`: Adds inline hints to most functions, improving run-time performance at the cost
   of compilation time. (enabled by default)
+- `bumpalo`: Provides a `BumpWrapper` type which allows `bumpalo` to be used for memory allocation.
 - `ahash`: Compiles with ahash as default hasher. (enabled by default)
-- `ahash-compile-time-rng`: Activates the `compile-time-rng` feature of ahash, to increase the
-   DOS-resistance, but can result in issues for `no_std` builds. More details in
-   [issue#124](https://github.com/rust-lang/hashbrown/issues/124). (enabled by default)
+- `ahash-compile-time-rng`: Activates the `compile-time-rng` feature of ahash. For targets with no random number generator
+this pre-generates seeds at compile time and embeds them as constants. See [aHash's documentation](https://github.com/tkaitchuck/aHash#flags) (disabled by default)
 
 ## License
 
diff --git a/sgx_tstd/hashbrown/benches/bench.rs b/sgx_tstd/hashbrown/benches/bench.rs
index 771e716..568c513 100644
--- a/sgx_tstd/hashbrown/benches/bench.rs
+++ b/sgx_tstd/hashbrown/benches/bench.rs
@@ -9,8 +9,11 @@
 use test::{black_box, Bencher};
 
 use hashbrown::hash_map::DefaultHashBuilder;
-use hashbrown::HashMap;
-use std::collections::hash_map::RandomState;
+use hashbrown::{HashMap, HashSet};
+use std::{
+    collections::hash_map::RandomState,
+    sync::atomic::{self, AtomicUsize},
+};
 
 const SIZE: usize = 1000;
 
@@ -40,6 +43,20 @@
     }
 }
 
+// Just an arbitrary side effect to make the maps not shortcircuit to the non-dropping path
+// when dropping maps/entries (most real world usages likely have drop in the key or value)
+lazy_static::lazy_static! {
+    static ref SIDE_EFFECT: AtomicUsize = AtomicUsize::new(0);
+}
+
+#[derive(Clone)]
+struct DropType(usize);
+impl Drop for DropType {
+    fn drop(&mut self) {
+        SIDE_EFFECT.fetch_add(self.0, atomic::Ordering::SeqCst);
+    }
+}
+
 macro_rules! bench_suite {
     ($bench_macro:ident, $bench_ahash_serial:ident, $bench_std_serial:ident,
      $bench_ahash_highbits:ident, $bench_std_highbits:ident,
@@ -69,10 +86,11 @@
             b.iter(|| {
                 m.clear();
                 for i in ($keydist).take(SIZE) {
-                    m.insert(i, i);
+                    m.insert(i, (DropType(i), [i; 20]));
                 }
                 black_box(&mut m);
-            })
+            });
+            eprintln!("{}", SIDE_EFFECT.load(atomic::Ordering::SeqCst));
         }
     };
 }
@@ -87,13 +105,38 @@
     insert_std_random
 );
 
+macro_rules! bench_grow_insert {
+    ($name:ident, $maptype:ident, $keydist:expr) => {
+        #[bench]
+        fn $name(b: &mut Bencher) {
+            b.iter(|| {
+                let mut m = $maptype::default();
+                for i in ($keydist).take(SIZE) {
+                    m.insert(i, DropType(i));
+                }
+                black_box(&mut m);
+            })
+        }
+    };
+}
+
+bench_suite!(
+    bench_grow_insert,
+    grow_insert_ahash_serial,
+    grow_insert_std_serial,
+    grow_insert_ahash_highbits,
+    grow_insert_std_highbits,
+    grow_insert_ahash_random,
+    grow_insert_std_random
+);
+
 macro_rules! bench_insert_erase {
     ($name:ident, $maptype:ident, $keydist:expr) => {
         #[bench]
         fn $name(b: &mut Bencher) {
             let mut base = $maptype::default();
             for i in ($keydist).take(SIZE) {
-                base.insert(i, i);
+                base.insert(i, DropType(i));
             }
             let skip = $keydist.skip(SIZE);
             b.iter(|| {
@@ -103,11 +146,12 @@
                 // While keeping the size constant,
                 // replace the first keydist with the second.
                 for (add, remove) in (&mut add_iter).zip(&mut remove_iter).take(SIZE) {
-                    m.insert(add, add);
+                    m.insert(add, DropType(add));
                     black_box(m.remove(&remove));
                 }
                 black_box(m);
-            })
+            });
+            eprintln!("{}", SIDE_EFFECT.load(atomic::Ordering::SeqCst));
         }
     };
 }
@@ -128,14 +172,15 @@
         fn $name(b: &mut Bencher) {
             let mut m = $maptype::default();
             for i in $keydist.take(SIZE) {
-                m.insert(i, i);
+                m.insert(i, DropType(i));
             }
 
             b.iter(|| {
                 for i in $keydist.take(SIZE) {
                     black_box(m.get(&i));
                 }
-            })
+            });
+            eprintln!("{}", SIDE_EFFECT.load(atomic::Ordering::SeqCst));
         }
     };
 }
@@ -157,7 +202,7 @@
             let mut m = $maptype::default();
             let mut iter = $keydist;
             for i in (&mut iter).take(SIZE) {
-                m.insert(i, i);
+                m.insert(i, DropType(i));
             }
 
             b.iter(|| {
@@ -185,7 +230,7 @@
         fn $name(b: &mut Bencher) {
             let mut m = $maptype::default();
             for i in ($keydist).take(SIZE) {
-                m.insert(i, i);
+                m.insert(i, DropType(i));
             }
 
             b.iter(|| {
@@ -211,7 +256,7 @@
 fn clone_small(b: &mut Bencher) {
     let mut m = HashMap::new();
     for i in 0..10 {
-        m.insert(i, i);
+        m.insert(i, DropType(i));
     }
 
     b.iter(|| {
@@ -224,7 +269,7 @@
     let mut m = HashMap::new();
     let mut m2 = HashMap::new();
     for i in 0..10 {
-        m.insert(i, i);
+        m.insert(i, DropType(i));
     }
 
     b.iter(|| {
@@ -237,7 +282,7 @@
 fn clone_large(b: &mut Bencher) {
     let mut m = HashMap::new();
     for i in 0..1000 {
-        m.insert(i, i);
+        m.insert(i, DropType(i));
     }
 
     b.iter(|| {
@@ -250,7 +295,7 @@
     let mut m = HashMap::new();
     let mut m2 = HashMap::new();
     for i in 0..1000 {
-        m.insert(i, i);
+        m.insert(i, DropType(i));
     }
 
     b.iter(|| {
@@ -258,3 +303,29 @@
         black_box(&mut m2);
     })
 }
+
+#[bench]
+fn rehash_in_place(b: &mut Bencher) {
+    b.iter(|| {
+        let mut set = HashSet::new();
+
+        // Each loop triggers one rehash
+        for _ in 0..10 {
+            for i in 0..224 {
+                set.insert(i);
+            }
+
+            assert_eq!(
+                set.capacity(),
+                224,
+                "The set must be at or close to capacity to trigger a re hashing"
+            );
+
+            for i in 100..1400 {
+                set.remove(&(i - 100));
+                set.insert(i);
+            }
+            set.clear();
+        }
+    });
+}
diff --git a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/map.rs b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/map.rs
index 334f8bb..61b7380 100644
--- a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/map.rs
+++ b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/map.rs
@@ -1,8 +1,11 @@
 //! Rayon extensions for `HashMap`.
 
+use super::raw::{RawIntoParIter, RawParDrain, RawParIter};
 use crate::hash_map::HashMap;
+use crate::raw::{Allocator, Global};
 use core::fmt;
 use core::hash::{BuildHasher, Hash};
+use core::marker::PhantomData;
 use rayon::iter::plumbing::UnindexedConsumer;
 use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
 
@@ -15,11 +18,12 @@
 /// [`par_iter`]: /hashbrown/struct.HashMap.html#method.par_iter
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
 /// [`IntoParallelRefIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefIterator.html
-pub struct ParIter<'a, K, V, S> {
-    map: &'a HashMap<K, V, S>,
+pub struct ParIter<'a, K, V> {
+    inner: RawParIter<(K, V)>,
+    marker: PhantomData<(&'a K, &'a V)>,
 }
 
-impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParIter<'a, K, V, S> {
+impl<'a, K: Sync, V: Sync> ParallelIterator for ParIter<'a, K, V> {
     type Item = (&'a K, &'a V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -27,7 +31,7 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        unsafe { self.map.table.par_iter() }
+        self.inner
             .map(|x| unsafe {
                 let r = x.as_ref();
                 (&r.0, &r.1)
@@ -36,16 +40,23 @@
     }
 }
 
-impl<K, V, S> Clone for ParIter<'_, K, V, S> {
+impl<K, V> Clone for ParIter<'_, K, V> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
-        ParIter { map: self.map }
+        Self {
+            inner: self.inner.clone(),
+            marker: PhantomData,
+        }
     }
 }
 
-impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParIter<'_, K, V, S> {
+impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug> fmt::Debug for ParIter<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.iter().fmt(f)
+        let iter = unsafe { self.inner.iter() }.map(|x| unsafe {
+            let r = x.as_ref();
+            (&r.0, &r.1)
+        });
+        f.debug_list().entries(iter).finish()
     }
 }
 
@@ -56,11 +67,12 @@
 ///
 /// [`par_keys`]: /hashbrown/struct.HashMap.html#method.par_keys
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
-pub struct ParKeys<'a, K, V, S> {
-    map: &'a HashMap<K, V, S>,
+pub struct ParKeys<'a, K, V> {
+    inner: RawParIter<(K, V)>,
+    marker: PhantomData<(&'a K, &'a V)>,
 }
 
-impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParKeys<'a, K, V, S> {
+impl<'a, K: Sync, V: Sync> ParallelIterator for ParKeys<'a, K, V> {
     type Item = &'a K;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -68,22 +80,26 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        unsafe { self.map.table.par_iter() }
+        self.inner
             .map(|x| unsafe { &x.as_ref().0 })
             .drive_unindexed(consumer)
     }
 }
 
-impl<K, V, S> Clone for ParKeys<'_, K, V, S> {
+impl<K, V> Clone for ParKeys<'_, K, V> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
-        ParKeys { map: self.map }
+        Self {
+            inner: self.inner.clone(),
+            marker: PhantomData,
+        }
     }
 }
 
-impl<K: fmt::Debug + Eq + Hash, V, S: BuildHasher> fmt::Debug for ParKeys<'_, K, V, S> {
+impl<K: fmt::Debug + Eq + Hash, V> fmt::Debug for ParKeys<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.keys().fmt(f)
+        let iter = unsafe { self.inner.iter() }.map(|x| unsafe { &x.as_ref().0 });
+        f.debug_list().entries(iter).finish()
     }
 }
 
@@ -94,11 +110,12 @@
 ///
 /// [`par_values`]: /hashbrown/struct.HashMap.html#method.par_values
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
-pub struct ParValues<'a, K, V, S> {
-    map: &'a HashMap<K, V, S>,
+pub struct ParValues<'a, K, V> {
+    inner: RawParIter<(K, V)>,
+    marker: PhantomData<(&'a K, &'a V)>,
 }
 
-impl<'a, K: Sync, V: Sync, S: Sync> ParallelIterator for ParValues<'a, K, V, S> {
+impl<'a, K: Sync, V: Sync> ParallelIterator for ParValues<'a, K, V> {
     type Item = &'a V;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -106,22 +123,26 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        unsafe { self.map.table.par_iter() }
+        self.inner
             .map(|x| unsafe { &x.as_ref().1 })
             .drive_unindexed(consumer)
     }
 }
 
-impl<K, V, S> Clone for ParValues<'_, K, V, S> {
+impl<K, V> Clone for ParValues<'_, K, V> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
-        ParValues { map: self.map }
+        Self {
+            inner: self.inner.clone(),
+            marker: PhantomData,
+        }
     }
 }
 
-impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValues<'_, K, V, S> {
+impl<K: Eq + Hash, V: fmt::Debug> fmt::Debug for ParValues<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.values().fmt(f)
+        let iter = unsafe { self.inner.iter() }.map(|x| unsafe { &x.as_ref().1 });
+        f.debug_list().entries(iter).finish()
     }
 }
 
@@ -134,11 +155,12 @@
 /// [`par_iter_mut`]: /hashbrown/struct.HashMap.html#method.par_iter_mut
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
 /// [`IntoParallelRefMutIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefMutIterator.html
-pub struct ParIterMut<'a, K, V, S> {
-    map: &'a mut HashMap<K, V, S>,
+pub struct ParIterMut<'a, K, V> {
+    inner: RawParIter<(K, V)>,
+    marker: PhantomData<(&'a K, &'a mut V)>,
 }
 
-impl<'a, K: Send + Sync, V: Send, S: Send> ParallelIterator for ParIterMut<'a, K, V, S> {
+impl<'a, K: Sync, V: Send> ParallelIterator for ParIterMut<'a, K, V> {
     type Item = (&'a K, &'a mut V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -146,7 +168,7 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        unsafe { self.map.table.par_iter() }
+        self.inner
             .map(|x| unsafe {
                 let r = x.as_mut();
                 (&r.0, &mut r.1)
@@ -155,11 +177,13 @@
     }
 }
 
-impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
-    for ParIterMut<'_, K, V, S>
-{
+impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug> fmt::Debug for ParIterMut<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.iter().fmt(f)
+        ParIter {
+            inner: self.inner.clone(),
+            marker: PhantomData,
+        }
+        .fmt(f)
     }
 }
 
@@ -170,11 +194,12 @@
 ///
 /// [`par_values_mut`]: /hashbrown/struct.HashMap.html#method.par_values_mut
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
-pub struct ParValuesMut<'a, K, V, S> {
-    map: &'a mut HashMap<K, V, S>,
+pub struct ParValuesMut<'a, K, V> {
+    inner: RawParIter<(K, V)>,
+    marker: PhantomData<(&'a K, &'a mut V)>,
 }
 
-impl<'a, K: Send, V: Send, S: Send> ParallelIterator for ParValuesMut<'a, K, V, S> {
+impl<'a, K: Sync, V: Send> ParallelIterator for ParValuesMut<'a, K, V> {
     type Item = &'a mut V;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -182,15 +207,19 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        unsafe { self.map.table.par_iter() }
+        self.inner
             .map(|x| unsafe { &mut x.as_mut().1 })
             .drive_unindexed(consumer)
     }
 }
 
-impl<K: Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for ParValuesMut<'_, K, V, S> {
+impl<K: Eq + Hash, V: fmt::Debug> fmt::Debug for ParValuesMut<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.values().fmt(f)
+        ParValues {
+            inner: self.inner.clone(),
+            marker: PhantomData,
+        }
+        .fmt(f)
     }
 }
 
@@ -203,11 +232,11 @@
 /// [`into_par_iter`]: /hashbrown/struct.HashMap.html#method.into_par_iter
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
 /// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
-pub struct IntoParIter<K, V, S> {
-    map: HashMap<K, V, S>,
+pub struct IntoParIter<K, V, A: Allocator + Clone = Global> {
+    inner: RawIntoParIter<(K, V), A>,
 }
 
-impl<K: Send, V: Send, S: Send> ParallelIterator for IntoParIter<K, V, S> {
+impl<K: Send, V: Send, A: Allocator + Clone + Send> ParallelIterator for IntoParIter<K, V, A> {
     type Item = (K, V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -215,13 +244,19 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.map.table.into_par_iter().drive_unindexed(consumer)
+        self.inner.drive_unindexed(consumer)
     }
 }
 
-impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug for IntoParIter<K, V, S> {
+impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
+    for IntoParIter<K, V, A>
+{
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.iter().fmt(f)
+        ParIter {
+            inner: unsafe { self.inner.par_iter() },
+            marker: PhantomData,
+        }
+        .fmt(f)
     }
 }
 
@@ -232,11 +267,11 @@
 ///
 /// [`par_drain`]: /hashbrown/struct.HashMap.html#method.par_drain
 /// [`HashMap`]: /hashbrown/struct.HashMap.html
-pub struct ParDrain<'a, K, V, S> {
-    map: &'a mut HashMap<K, V, S>,
+pub struct ParDrain<'a, K, V, A: Allocator + Clone = Global> {
+    inner: RawParDrain<'a, (K, V), A>,
 }
 
-impl<K: Send, V: Send, S: Send> ParallelIterator for ParDrain<'_, K, V, S> {
+impl<K: Send, V: Send, A: Allocator + Clone + Sync> ParallelIterator for ParDrain<'_, K, V, A> {
     type Item = (K, V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -244,52 +279,68 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.map.table.par_drain().drive_unindexed(consumer)
+        self.inner.drive_unindexed(consumer)
     }
 }
 
-impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, S: BuildHasher> fmt::Debug
-    for ParDrain<'_, K, V, S>
+impl<K: fmt::Debug + Eq + Hash, V: fmt::Debug, A: Allocator + Clone> fmt::Debug
+    for ParDrain<'_, K, V, A>
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.map.iter().fmt(f)
+        ParIter {
+            inner: unsafe { self.inner.par_iter() },
+            marker: PhantomData,
+        }
+        .fmt(f)
     }
 }
 
-impl<K: Sync, V: Sync, S: Sync> HashMap<K, V, S> {
+impl<K: Sync, V: Sync, S, A: Allocator + Clone> HashMap<K, V, S, A> {
     /// Visits (potentially in parallel) immutably borrowed keys in an arbitrary order.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_keys(&self) -> ParKeys<'_, K, V, S> {
-        ParKeys { map: self }
+    pub fn par_keys(&self) -> ParKeys<'_, K, V> {
+        ParKeys {
+            inner: unsafe { self.table.par_iter() },
+            marker: PhantomData,
+        }
     }
 
     /// Visits (potentially in parallel) immutably borrowed values in an arbitrary order.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_values(&self) -> ParValues<'_, K, V, S> {
-        ParValues { map: self }
+    pub fn par_values(&self) -> ParValues<'_, K, V> {
+        ParValues {
+            inner: unsafe { self.table.par_iter() },
+            marker: PhantomData,
+        }
     }
 }
 
-impl<K: Send, V: Send, S: Send> HashMap<K, V, S> {
+impl<K: Send, V: Send, S, A: Allocator + Clone> HashMap<K, V, S, A> {
     /// Visits (potentially in parallel) mutably borrowed values in an arbitrary order.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V, S> {
-        ParValuesMut { map: self }
+    pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
+        ParValuesMut {
+            inner: unsafe { self.table.par_iter() },
+            marker: PhantomData,
+        }
     }
 
     /// Consumes (potentially in parallel) all values in an arbitrary order,
     /// while preserving the map's allocated memory for reuse.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_drain(&mut self) -> ParDrain<'_, K, V, S> {
-        ParDrain { map: self }
+    pub fn par_drain(&mut self) -> ParDrain<'_, K, V, A> {
+        ParDrain {
+            inner: self.table.par_drain(),
+        }
     }
 }
 
-impl<K, V, S> HashMap<K, V, S>
+impl<K, V, S, A> HashMap<K, V, S, A>
 where
     K: Eq + Hash + Sync,
     V: PartialEq + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
     /// Returns `true` if the map is equal to another,
     /// i.e. both maps contain the same keys mapped to the same values.
@@ -303,33 +354,47 @@
     }
 }
 
-impl<K: Send, V: Send, S: Send> IntoParallelIterator for HashMap<K, V, S> {
+impl<K: Send, V: Send, S, A: Allocator + Clone + Send> IntoParallelIterator
+    for HashMap<K, V, S, A>
+{
     type Item = (K, V);
-    type Iter = IntoParIter<K, V, S>;
+    type Iter = IntoParIter<K, V, A>;
 
     #[cfg_attr(feature = "inline-more", inline)]
     fn into_par_iter(self) -> Self::Iter {
-        IntoParIter { map: self }
+        IntoParIter {
+            inner: self.table.into_par_iter(),
+        }
     }
 }
 
-impl<'a, K: Sync, V: Sync, S: Sync> IntoParallelIterator for &'a HashMap<K, V, S> {
+impl<'a, K: Sync, V: Sync, S, A: Allocator + Clone> IntoParallelIterator
+    for &'a HashMap<K, V, S, A>
+{
     type Item = (&'a K, &'a V);
-    type Iter = ParIter<'a, K, V, S>;
+    type Iter = ParIter<'a, K, V>;
 
     #[cfg_attr(feature = "inline-more", inline)]
     fn into_par_iter(self) -> Self::Iter {
-        ParIter { map: self }
+        ParIter {
+            inner: unsafe { self.table.par_iter() },
+            marker: PhantomData,
+        }
     }
 }
 
-impl<'a, K: Send + Sync, V: Send, S: Send> IntoParallelIterator for &'a mut HashMap<K, V, S> {
+impl<'a, K: Sync, V: Send, S, A: Allocator + Clone> IntoParallelIterator
+    for &'a mut HashMap<K, V, S, A>
+{
     type Item = (&'a K, &'a mut V);
-    type Iter = ParIterMut<'a, K, V, S>;
+    type Iter = ParIterMut<'a, K, V>;
 
     #[cfg_attr(feature = "inline-more", inline)]
     fn into_par_iter(self) -> Self::Iter {
-        ParIterMut { map: self }
+        ParIterMut {
+            inner: unsafe { self.table.par_iter() },
+            marker: PhantomData,
+        }
     }
 }
 
@@ -337,7 +402,7 @@
 /// hashmap. If multiple pairs correspond to the same key, then the
 /// ones produced earlier in the parallel iterator will be
 /// overwritten, just as with a sequential iterator.
-impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S>
+impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S, Global>
 where
     K: Eq + Hash + Send,
     V: Send,
@@ -354,11 +419,12 @@
 }
 
 /// Extend a hash map with items from a parallel iterator.
-impl<K, V, S> ParallelExtend<(K, V)> for HashMap<K, V, S>
+impl<K, V, S, A> ParallelExtend<(K, V)> for HashMap<K, V, S, A>
 where
     K: Eq + Hash + Send,
     V: Send,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn par_extend<I>(&mut self, par_iter: I)
     where
@@ -369,11 +435,12 @@
 }
 
 /// Extend a hash map with copied items from a parallel iterator.
-impl<'a, K, V, S> ParallelExtend<(&'a K, &'a V)> for HashMap<K, V, S>
+impl<'a, K, V, S, A> ParallelExtend<(&'a K, &'a V)> for HashMap<K, V, S, A>
 where
     K: Copy + Eq + Hash + Sync,
     V: Copy + Sync,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn par_extend<I>(&mut self, par_iter: I)
     where
@@ -384,12 +451,13 @@
 }
 
 // This is equal to the normal `HashMap` -- no custom advantage.
-fn extend<K, V, S, I>(map: &mut HashMap<K, V, S>, par_iter: I)
+fn extend<K, V, S, A, I>(map: &mut HashMap<K, V, S, A>, par_iter: I)
 where
     K: Eq + Hash,
     S: BuildHasher,
     I: IntoParallelIterator,
-    HashMap<K, V, S>: Extend<I::Item>,
+    A: Allocator + Clone,
+    HashMap<K, V, S, A>: Extend<I::Item>,
 {
     let (list, len) = super::helpers::collect(par_iter);
 
diff --git a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/raw.rs b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/raw.rs
index 1bd2c17..18da1ea 100644
--- a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/raw.rs
+++ b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/raw.rs
@@ -1,5 +1,5 @@
 use crate::raw::Bucket;
-use crate::raw::{RawIter, RawIterRange, RawTable};
+use crate::raw::{Allocator, Global, RawIter, RawIterRange, RawTable};
 use crate::scopeguard::guard;
 use alloc::alloc::dealloc;
 use core::marker::PhantomData;
@@ -15,6 +15,22 @@
     iter: RawIterRange<T>,
 }
 
+impl<T> RawParIter<T> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub(super) unsafe fn iter(&self) -> RawIterRange<T> {
+        self.iter.clone()
+    }
+}
+
+impl<T> Clone for RawParIter<T> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    fn clone(&self) -> Self {
+        Self {
+            iter: self.iter.clone(),
+        }
+    }
+}
+
 impl<T> From<RawIter<T>> for RawParIter<T> {
     fn from(it: RawIter<T>) -> Self {
         RawParIter { iter: it.iter }
@@ -60,11 +76,18 @@
 }
 
 /// Parallel iterator which consumes a table and returns elements.
-pub struct RawIntoParIter<T> {
-    table: RawTable<T>,
+pub struct RawIntoParIter<T, A: Allocator + Clone = Global> {
+    table: RawTable<T, A>,
 }
 
-impl<T: Send> ParallelIterator for RawIntoParIter<T> {
+impl<T, A: Allocator + Clone> RawIntoParIter<T, A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub(super) unsafe fn par_iter(&self) -> RawParIter<T> {
+        self.table.par_iter()
+    }
+}
+
+impl<T: Send, A: Allocator + Clone> ParallelIterator for RawIntoParIter<T, A> {
     type Item = T;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -73,7 +96,7 @@
         C: UnindexedConsumer<Self::Item>,
     {
         let iter = unsafe { self.table.iter().iter };
-        let _guard = guard(self.table.into_alloc(), |alloc| {
+        let _guard = guard(self.table.into_allocation(), |alloc| {
             if let Some((ptr, layout)) = *alloc {
                 unsafe {
                     dealloc(ptr.as_ptr(), layout);
@@ -86,16 +109,23 @@
 }
 
 /// Parallel iterator which consumes elements without freeing the table storage.
-pub struct RawParDrain<'a, T> {
+pub struct RawParDrain<'a, T, A: Allocator + Clone = Global> {
     // We don't use a &'a mut RawTable<T> because we want RawParDrain to be
     // covariant over T.
-    table: NonNull<RawTable<T>>,
-    marker: PhantomData<&'a RawTable<T>>,
+    table: NonNull<RawTable<T, A>>,
+    marker: PhantomData<&'a RawTable<T, A>>,
 }
 
-unsafe impl<T> Send for RawParDrain<'_, T> {}
+unsafe impl<T, A: Allocator + Clone> Send for RawParDrain<'_, T, A> {}
 
-impl<T: Send> ParallelIterator for RawParDrain<'_, T> {
+impl<T, A: Allocator + Clone> RawParDrain<'_, T, A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub(super) unsafe fn par_iter(&self) -> RawParIter<T> {
+        self.table.as_ref().par_iter()
+    }
+}
+
+impl<T: Send, A: Allocator + Clone> ParallelIterator for RawParDrain<'_, T, A> {
     type Item = T;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -113,7 +143,7 @@
     }
 }
 
-impl<T> Drop for RawParDrain<'_, T> {
+impl<T, A: Allocator + Clone> Drop for RawParDrain<'_, T, A> {
     fn drop(&mut self) {
         // If drive_unindexed is not called then simply clear the table.
         unsafe { self.table.as_mut().clear() }
@@ -172,7 +202,7 @@
     }
 }
 
-impl<T> RawTable<T> {
+impl<T, A: Allocator + Clone> RawTable<T, A> {
     /// Returns a parallel iterator over the elements in a `RawTable`.
     #[cfg_attr(feature = "inline-more", inline)]
     pub unsafe fn par_iter(&self) -> RawParIter<T> {
@@ -183,14 +213,14 @@
 
     /// Returns a parallel iterator over the elements in a `RawTable`.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn into_par_iter(self) -> RawIntoParIter<T> {
+    pub fn into_par_iter(self) -> RawIntoParIter<T, A> {
         RawIntoParIter { table: self }
     }
 
     /// Returns a parallel iterator which consumes all elements of a `RawTable`
     /// without freeing its memory allocation.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_drain(&mut self) -> RawParDrain<'_, T> {
+    pub fn par_drain(&mut self) -> RawParDrain<'_, T, A> {
         RawParDrain {
             table: NonNull::from(self),
             marker: PhantomData,
diff --git a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/set.rs b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/set.rs
index 53d2660..ee4f6e6 100644
--- a/sgx_tstd/hashbrown/src/external_trait_impls/rayon/set.rs
+++ b/sgx_tstd/hashbrown/src/external_trait_impls/rayon/set.rs
@@ -1,6 +1,8 @@
 //! Rayon extensions for `HashSet`.
 
+use super::map;
 use crate::hash_set::HashSet;
+use crate::raw::{Allocator, Global};
 use core::hash::{BuildHasher, Hash};
 use rayon::iter::plumbing::UnindexedConsumer;
 use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
@@ -14,22 +16,18 @@
 /// [`into_par_iter`]: /hashbrown/struct.HashSet.html#method.into_par_iter
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
 /// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html
-pub struct IntoParIter<T, S> {
-    set: HashSet<T, S>,
+pub struct IntoParIter<T, A: Allocator + Clone = Global> {
+    inner: map::IntoParIter<T, (), A>,
 }
 
-impl<T: Send, S: Send> ParallelIterator for IntoParIter<T, S> {
+impl<T: Send, A: Allocator + Clone + Send> ParallelIterator for IntoParIter<T, A> {
     type Item = T;
 
     fn drive_unindexed<C>(self, consumer: C) -> C::Result
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.set
-            .map
-            .into_par_iter()
-            .map(|(k, _)| k)
-            .drive_unindexed(consumer)
+        self.inner.map(|(k, _)| k).drive_unindexed(consumer)
     }
 }
 
@@ -40,22 +38,18 @@
 ///
 /// [`par_drain`]: /hashbrown/struct.HashSet.html#method.par_drain
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
-pub struct ParDrain<'a, T, S> {
-    set: &'a mut HashSet<T, S>,
+pub struct ParDrain<'a, T, A: Allocator + Clone = Global> {
+    inner: map::ParDrain<'a, T, (), A>,
 }
 
-impl<T: Send, S: Send> ParallelIterator for ParDrain<'_, T, S> {
+impl<T: Send, A: Allocator + Clone + Send + Sync> ParallelIterator for ParDrain<'_, T, A> {
     type Item = T;
 
     fn drive_unindexed<C>(self, consumer: C) -> C::Result
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.set
-            .map
-            .par_drain()
-            .map(|(k, _)| k)
-            .drive_unindexed(consumer)
+        self.inner.map(|(k, _)| k).drive_unindexed(consumer)
     }
 }
 
@@ -68,18 +62,18 @@
 /// [`par_iter`]: /hashbrown/struct.HashSet.html#method.par_iter
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
 /// [`IntoParallelRefIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefIterator.html
-pub struct ParIter<'a, T, S> {
-    set: &'a HashSet<T, S>,
+pub struct ParIter<'a, T> {
+    inner: map::ParKeys<'a, T, ()>,
 }
 
-impl<'a, T: Sync, S: Sync> ParallelIterator for ParIter<'a, T, S> {
+impl<'a, T: Sync> ParallelIterator for ParIter<'a, T> {
     type Item = &'a T;
 
     fn drive_unindexed<C>(self, consumer: C) -> C::Result
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.set.map.par_keys().drive_unindexed(consumer)
+        self.inner.drive_unindexed(consumer)
     }
 }
 
@@ -91,15 +85,16 @@
 ///
 /// [`par_difference`]: /hashbrown/struct.HashSet.html#method.par_difference
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
-pub struct ParDifference<'a, T, S> {
-    a: &'a HashSet<T, S>,
-    b: &'a HashSet<T, S>,
+pub struct ParDifference<'a, T, S, A: Allocator + Clone = Global> {
+    a: &'a HashSet<T, S, A>,
+    b: &'a HashSet<T, S, A>,
 }
 
-impl<'a, T, S> ParallelIterator for ParDifference<'a, T, S>
+impl<'a, T, S, A> ParallelIterator for ParDifference<'a, T, S, A>
 where
     T: Eq + Hash + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
     type Item = &'a T;
 
@@ -123,15 +118,16 @@
 ///
 /// [`par_symmetric_difference`]: /hashbrown/struct.HashSet.html#method.par_symmetric_difference
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
-pub struct ParSymmetricDifference<'a, T, S> {
-    a: &'a HashSet<T, S>,
-    b: &'a HashSet<T, S>,
+pub struct ParSymmetricDifference<'a, T, S, A: Allocator + Clone = Global> {
+    a: &'a HashSet<T, S, A>,
+    b: &'a HashSet<T, S, A>,
 }
 
-impl<'a, T, S> ParallelIterator for ParSymmetricDifference<'a, T, S>
+impl<'a, T, S, A> ParallelIterator for ParSymmetricDifference<'a, T, S, A>
 where
     T: Eq + Hash + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
     type Item = &'a T;
 
@@ -154,15 +150,16 @@
 ///
 /// [`par_intersection`]: /hashbrown/struct.HashSet.html#method.par_intersection
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
-pub struct ParIntersection<'a, T, S> {
-    a: &'a HashSet<T, S>,
-    b: &'a HashSet<T, S>,
+pub struct ParIntersection<'a, T, S, A: Allocator + Clone = Global> {
+    a: &'a HashSet<T, S, A>,
+    b: &'a HashSet<T, S, A>,
 }
 
-impl<'a, T, S> ParallelIterator for ParIntersection<'a, T, S>
+impl<'a, T, S, A> ParallelIterator for ParIntersection<'a, T, S, A>
 where
     T: Eq + Hash + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
     type Item = &'a T;
 
@@ -184,15 +181,16 @@
 ///
 /// [`par_union`]: /hashbrown/struct.HashSet.html#method.par_union
 /// [`HashSet`]: /hashbrown/struct.HashSet.html
-pub struct ParUnion<'a, T, S> {
-    a: &'a HashSet<T, S>,
-    b: &'a HashSet<T, S>,
+pub struct ParUnion<'a, T, S, A: Allocator + Clone = Global> {
+    a: &'a HashSet<T, S, A>,
+    b: &'a HashSet<T, S, A>,
 }
 
-impl<'a, T, S> ParallelIterator for ParUnion<'a, T, S>
+impl<'a, T, S, A> ParallelIterator for ParUnion<'a, T, S, A>
 where
     T: Eq + Hash + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
     type Item = &'a T;
 
@@ -200,22 +198,37 @@
     where
         C: UnindexedConsumer<Self::Item>,
     {
-        self.a
+        // We'll iterate one set in full, and only the remaining difference from the other.
+        // Use the smaller set for the difference in order to reduce hash lookups.
+        let (smaller, larger) = if self.a.len() <= self.b.len() {
+            (self.a, self.b)
+        } else {
+            (self.b, self.a)
+        };
+        larger
             .into_par_iter()
-            .chain(self.b.par_difference(self.a))
+            .chain(smaller.par_difference(larger))
             .drive_unindexed(consumer)
     }
 }
 
-impl<T, S> HashSet<T, S>
+impl<T, S, A> HashSet<T, S, A>
 where
     T: Eq + Hash + Sync,
     S: BuildHasher + Sync,
+    A: Allocator + Clone + Sync,
 {
+    /// Visits (potentially in parallel) the values representing the union,
+    /// i.e. all the values in `self` or `other`, without duplicates.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn par_union<'a>(&'a self, other: &'a Self) -> ParUnion<'a, T, S, A> {
+        ParUnion { a: self, b: other }
+    }
+
     /// Visits (potentially in parallel) the values representing the difference,
     /// i.e. the values that are in `self` but not in `other`.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_difference<'a>(&'a self, other: &'a Self) -> ParDifference<'a, T, S> {
+    pub fn par_difference<'a>(&'a self, other: &'a Self) -> ParDifference<'a, T, S, A> {
         ParDifference { a: self, b: other }
     }
 
@@ -225,24 +238,17 @@
     pub fn par_symmetric_difference<'a>(
         &'a self,
         other: &'a Self,
-    ) -> ParSymmetricDifference<'a, T, S> {
+    ) -> ParSymmetricDifference<'a, T, S, A> {
         ParSymmetricDifference { a: self, b: other }
     }
 
     /// Visits (potentially in parallel) the values representing the
     /// intersection, i.e. the values that are both in `self` and `other`.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_intersection<'a>(&'a self, other: &'a Self) -> ParIntersection<'a, T, S> {
+    pub fn par_intersection<'a>(&'a self, other: &'a Self) -> ParIntersection<'a, T, S, A> {
         ParIntersection { a: self, b: other }
     }
 
-    /// Visits (potentially in parallel) the values representing the union,
-    /// i.e. all the values in `self` or `other`, without duplicates.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_union<'a>(&'a self, other: &'a Self) -> ParUnion<'a, T, S> {
-        ParUnion { a: self, b: other }
-    }
-
     /// Returns `true` if `self` has no elements in common with `other`.
     /// This is equivalent to checking for an empty intersection.
     ///
@@ -280,41 +286,47 @@
     }
 }
 
-impl<T, S> HashSet<T, S>
+impl<T, S, A> HashSet<T, S, A>
 where
     T: Eq + Hash + Send,
-    S: BuildHasher + Send,
+    A: Allocator + Clone + Send,
 {
     /// Consumes (potentially in parallel) all values in an arbitrary order,
     /// while preserving the set's allocated memory for reuse.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn par_drain(&mut self) -> ParDrain<'_, T, S> {
-        ParDrain { set: self }
+    pub fn par_drain(&mut self) -> ParDrain<'_, T, A> {
+        ParDrain {
+            inner: self.map.par_drain(),
+        }
     }
 }
 
-impl<T: Send, S: Send> IntoParallelIterator for HashSet<T, S> {
+impl<T: Send, S, A: Allocator + Clone + Send> IntoParallelIterator for HashSet<T, S, A> {
     type Item = T;
-    type Iter = IntoParIter<T, S>;
+    type Iter = IntoParIter<T, A>;
 
     #[cfg_attr(feature = "inline-more", inline)]
     fn into_par_iter(self) -> Self::Iter {
-        IntoParIter { set: self }
+        IntoParIter {
+            inner: self.map.into_par_iter(),
+        }
     }
 }
 
-impl<'a, T: Sync, S: Sync> IntoParallelIterator for &'a HashSet<T, S> {
+impl<'a, T: Sync, S, A: Allocator + Clone> IntoParallelIterator for &'a HashSet<T, S, A> {
     type Item = &'a T;
-    type Iter = ParIter<'a, T, S>;
+    type Iter = ParIter<'a, T>;
 
     #[cfg_attr(feature = "inline-more", inline)]
     fn into_par_iter(self) -> Self::Iter {
-        ParIter { set: self }
+        ParIter {
+            inner: self.map.par_keys(),
+        }
     }
 }
 
 /// Collect values from a parallel iterator into a hashset.
-impl<T, S> FromParallelIterator<T> for HashSet<T, S>
+impl<T, S> FromParallelIterator<T> for HashSet<T, S, Global>
 where
     T: Eq + Hash + Send,
     S: BuildHasher + Default,
@@ -330,7 +342,7 @@
 }
 
 /// Extend a hash set with items from a parallel iterator.
-impl<T, S> ParallelExtend<T> for HashSet<T, S>
+impl<T, S> ParallelExtend<T> for HashSet<T, S, Global>
 where
     T: Eq + Hash + Send,
     S: BuildHasher,
@@ -344,7 +356,7 @@
 }
 
 /// Extend a hash set with copied items from a parallel iterator.
-impl<'a, T, S> ParallelExtend<&'a T> for HashSet<T, S>
+impl<'a, T, S> ParallelExtend<&'a T> for HashSet<T, S, Global>
 where
     T: 'a + Copy + Eq + Hash + Sync,
     S: BuildHasher,
@@ -358,12 +370,13 @@
 }
 
 // This is equal to the normal `HashSet` -- no custom advantage.
-fn extend<T, S, I>(set: &mut HashSet<T, S>, par_iter: I)
+fn extend<T, S, I, A>(set: &mut HashSet<T, S, A>, par_iter: I)
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
     I: IntoParallelIterator,
-    HashSet<T, S>: Extend<I::Item>,
+    HashSet<T, S, A>: Extend<I::Item>,
 {
     let (list, len) = super::helpers::collect(par_iter);
 
diff --git a/sgx_tstd/hashbrown/src/lib.rs b/sgx_tstd/hashbrown/src/lib.rs
index 3aff40a..b2d6584 100644
--- a/sgx_tstd/hashbrown/src/lib.rs
+++ b/sgx_tstd/hashbrown/src/lib.rs
@@ -12,13 +12,25 @@
 #![no_std]
 #![cfg_attr(
     feature = "nightly",
-    feature(test, core_intrinsics, dropck_eyepatch, min_specialization, extend_one)
+    feature(
+        test,
+        core_intrinsics,
+        dropck_eyepatch,
+        min_specialization,
+        extend_one,
+        allocator_api,
+        slice_ptr_get,
+        nonnull_slice_from_raw_parts,
+        maybe_uninit_array_assume_init
+    )
 )]
 #![allow(
     clippy::doc_markdown,
     clippy::module_name_repetitions,
     clippy::must_use_candidate,
-    clippy::option_if_let_else
+    clippy::option_if_let_else,
+    clippy::redundant_else,
+    clippy::manual_map
 )]
 #![warn(missing_docs)]
 #![warn(rust_2018_idioms)]
@@ -48,6 +60,11 @@
     pub use inner::*;
 
     #[cfg(feature = "rayon")]
+    /// [rayon]-based parallel iterator types for hash maps.
+    /// You will rarely need to interact with it directly unless you have need
+    /// to name one of the iterator types.
+    ///
+    /// [rayon]: https://docs.rs/rayon/1.0/rayon
     pub mod rayon {
         pub use crate::external_trait_impls::rayon::raw::*;
     }
@@ -110,3 +127,35 @@
         layout: alloc::alloc::Layout,
     },
 }
+
+/// The error type for [`RawTable::get_each_mut`](crate::raw::RawTable::get_each_mut),
+/// [`HashMap::get_each_mut`], and [`HashMap::get_each_key_value_mut`].
+#[cfg(feature = "nightly")]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub enum UnavailableMutError {
+    /// The requested entry is not present in the table.
+    Absent,
+    /// The requested entry is present, but a mutable reference to it was already created and
+    /// returned from this call to `get_each_mut` or `get_each_key_value_mut`.
+    ///
+    /// Includes the index of the existing mutable reference in the returned array.
+    Duplicate(usize),
+}
+
+/// Wrapper around `Bump` which allows it to be used as an allocator for
+/// `HashMap`, `HashSet` and `RawTable`.
+///
+/// `Bump` can be used directly without this wrapper on nightly if you enable
+/// the `allocator-api` feature of the `bumpalo` crate.
+#[cfg(feature = "bumpalo")]
+#[derive(Clone, Copy, Debug)]
+pub struct BumpWrapper<'a>(pub &'a bumpalo::Bump);
+
+#[cfg(feature = "bumpalo")]
+#[test]
+fn test_bumpalo() {
+    use bumpalo::Bump;
+    let bump = Bump::new();
+    let mut map = HashMap::new_in(BumpWrapper(&bump));
+    map.insert(0, 1);
+}
diff --git a/sgx_tstd/hashbrown/src/map.rs b/sgx_tstd/hashbrown/src/map.rs
index 380d71a..ab11288 100644
--- a/sgx_tstd/hashbrown/src/map.rs
+++ b/sgx_tstd/hashbrown/src/map.rs
@@ -1,11 +1,15 @@
-use crate::raw::{Bucket, RawDrain, RawIntoIter, RawIter, RawTable};
+use crate::raw::{Allocator, Bucket, Global, RawDrain, RawIntoIter, RawIter, RawTable};
 use crate::TryReserveError;
+#[cfg(feature = "nightly")]
+use crate::UnavailableMutError;
 use core::borrow::Borrow;
 use core::fmt::{self, Debug};
-use core::hash::{BuildHasher, Hash, Hasher};
+use core::hash::{BuildHasher, Hash};
 use core::iter::{FromIterator, FusedIterator};
 use core::marker::PhantomData;
 use core::mem;
+#[cfg(feature = "nightly")]
+use core::mem::MaybeUninit;
 use core::ops::Index;
 
 /// Default hasher for `HashMap`.
@@ -185,12 +189,12 @@
 ///     .iter().cloned().collect();
 /// // use the values stored in map
 /// ```
-pub struct HashMap<K, V, S = DefaultHashBuilder> {
+pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
     pub(crate) hash_builder: S,
-    pub(crate) table: RawTable<(K, V)>,
+    pub(crate) table: RawTable<(K, V), A>,
 }
 
-impl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S> {
+impl<K: Clone, V: Clone, S: Clone, A: Allocator + Clone> Clone for HashMap<K, V, S, A> {
     fn clone(&self) -> Self {
         HashMap {
             hash_builder: self.hash_builder.clone(),
@@ -206,8 +210,60 @@
     }
 }
 
+/// Ensures that a single closure type across uses of this which, in turn prevents multiple
+/// instances of any functions like RawTable::reserve from being generated
 #[cfg_attr(feature = "inline-more", inline)]
-pub(crate) fn make_hash<K: Hash + ?Sized>(hash_builder: &impl BuildHasher, val: &K) -> u64 {
+pub(crate) fn make_hasher<K, Q, V, S>(hash_builder: &S) -> impl Fn(&(Q, V)) -> u64 + '_
+where
+    K: Borrow<Q>,
+    Q: Hash,
+    S: BuildHasher,
+{
+    move |val| make_hash::<K, Q, S>(hash_builder, &val.0)
+}
+
+/// Ensures that a single closure type across uses of this which, in turn prevents multiple
+/// instances of any functions like RawTable::reserve from being generated
+#[cfg_attr(feature = "inline-more", inline)]
+fn equivalent_key<Q, K, V>(k: &Q) -> impl Fn(&(K, V)) -> bool + '_
+where
+    K: Borrow<Q>,
+    Q: ?Sized + Eq,
+{
+    move |x| k.eq(x.0.borrow())
+}
+
+/// Ensures that a single closure type across uses of this which, in turn prevents multiple
+/// instances of any functions like RawTable::reserve from being generated
+#[cfg_attr(feature = "inline-more", inline)]
+fn equivalent<Q, K>(k: &Q) -> impl Fn(&K) -> bool + '_
+where
+    K: Borrow<Q>,
+    Q: ?Sized + Eq,
+{
+    move |x| k.eq(x.borrow())
+}
+
+#[cfg_attr(feature = "inline-more", inline)]
+pub(crate) fn make_hash<K, Q, S>(hash_builder: &S, val: &Q) -> u64
+where
+    K: Borrow<Q>,
+    Q: Hash + ?Sized,
+    S: BuildHasher,
+{
+    use core::hash::Hasher;
+    let mut state = hash_builder.build_hasher();
+    val.hash(&mut state);
+    state.finish()
+}
+
+#[cfg_attr(feature = "inline-more", inline)]
+pub(crate) fn make_insert_hash<K, S>(hash_builder: &S, val: &K) -> u64
+where
+    K: Hash,
+    S: BuildHasher,
+{
+    use core::hash::Hasher;
     let mut state = hash_builder.build_hasher();
     val.hash(&mut state);
     state.finish()
@@ -248,6 +304,27 @@
     }
 }
 
+#[cfg(feature = "ahash")]
+impl<K, V, A: Allocator + Clone> HashMap<K, V, DefaultHashBuilder, A> {
+    /// Creates an empty `HashMap` using the given allocator.
+    ///
+    /// The hash map is initially created with a capacity of 0, so it will not allocate until it
+    /// is first inserted into.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn new_in(alloc: A) -> Self {
+        Self::with_hasher_in(DefaultHashBuilder::default(), alloc)
+    }
+
+    /// Creates an empty `HashMap` with the specified capacity using the given allocator.
+    ///
+    /// The hash map will be able to hold at least `capacity` elements without
+    /// reallocating. If `capacity` is 0, the hash map will not allocate.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
+        Self::with_capacity_and_hasher_in(capacity, DefaultHashBuilder::default(), alloc)
+    }
+}
+
 impl<K, V, S> HashMap<K, V, S> {
     /// Creates an empty `HashMap` which will use the given hash builder to hash
     /// keys.
@@ -315,6 +392,65 @@
             table: RawTable::with_capacity(capacity),
         }
     }
+}
+
+impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
+    /// Creates an empty `HashMap` which will use the given hash builder to hash
+    /// keys. It will be allocated with the given allocator.
+    ///
+    /// The created map has the default initial capacity.
+    ///
+    /// Warning: `hash_builder` is normally randomly generated, and
+    /// is designed to allow HashMaps to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashMap;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut map = HashMap::with_hasher(s);
+    /// map.insert(1, 2);
+    /// ```
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
+        Self {
+            hash_builder,
+            table: RawTable::new_in(alloc),
+        }
+    }
+
+    /// Creates an empty `HashMap` with the specified capacity, using `hash_builder`
+    /// to hash the keys. It will be allocated with the given allocator.
+    ///
+    /// The hash map will be able to hold at least `capacity` elements without
+    /// reallocating. If `capacity` is 0, the hash map will not allocate.
+    ///
+    /// Warning: `hash_builder` is normally randomly generated, and
+    /// is designed to allow HashMaps to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashMap;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut map = HashMap::with_capacity_and_hasher(10, s);
+    /// map.insert(1, 2);
+    /// ```
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
+        Self {
+            hash_builder,
+            table: RawTable::with_capacity_in(capacity, alloc),
+        }
+    }
 
     /// Returns a reference to the map's [`BuildHasher`].
     ///
@@ -547,12 +683,9 @@
     /// assert!(a.is_empty());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn drain(&mut self) -> Drain<'_, K, V> {
-        // Here we tie the lifetime of self to the iter.
-        unsafe {
-            Drain {
-                inner: self.table.drain(),
-            }
+    pub fn drain(&mut self) -> Drain<'_, K, V, A> {
+        Drain {
+            inner: self.table.drain(),
         }
     }
 
@@ -610,7 +743,7 @@
     /// assert_eq!(odds, vec![1, 3, 5, 7]);
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F>
+    pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, K, V, F, A>
     where
         F: FnMut(&K, &mut V) -> bool,
     {
@@ -642,10 +775,11 @@
     }
 }
 
-impl<K, V, S> HashMap<K, V, S>
+impl<K, V, S, A> HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     /// Reserves capacity for at least `additional` more elements to be inserted
     /// in the `HashMap`. The collection may reserve more space to avoid
@@ -666,9 +800,8 @@
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn reserve(&mut self, additional: usize) {
-        let hash_builder = &self.hash_builder;
         self.table
-            .reserve(additional, |x| make_hash(hash_builder, &x.0));
+            .reserve(additional, make_hasher::<K, _, V, S>(&self.hash_builder));
     }
 
     /// Tries to reserve capacity for at least `additional` more elements to be inserted
@@ -689,9 +822,8 @@
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
-        let hash_builder = &self.hash_builder;
         self.table
-            .try_reserve(additional, |x| make_hash(hash_builder, &x.0))
+            .try_reserve(additional, make_hasher::<K, _, V, S>(&self.hash_builder))
     }
 
     /// Shrinks the capacity of the map as much as possible. It will drop
@@ -712,8 +844,8 @@
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn shrink_to_fit(&mut self) {
-        let hash_builder = &self.hash_builder;
-        self.table.shrink_to(0, |x| make_hash(hash_builder, &x.0));
+        self.table
+            .shrink_to(0, make_hasher::<K, _, V, S>(&self.hash_builder));
     }
 
     /// Shrinks the capacity of the map with a lower limit. It will drop
@@ -741,9 +873,8 @@
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn shrink_to(&mut self, min_capacity: usize) {
-        let hash_builder = &self.hash_builder;
         self.table
-            .shrink_to(min_capacity, |x| make_hash(hash_builder, &x.0));
+            .shrink_to(min_capacity, make_hasher::<K, _, V, S>(&self.hash_builder));
     }
 
     /// Gets the given key's corresponding entry in the map for in-place manipulation.
@@ -766,9 +897,9 @@
     /// assert_eq!(letters.get(&'y'), None);
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S> {
-        let hash = make_hash(&self.hash_builder, &key);
-        if let Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) {
+    pub fn entry(&mut self, key: K) -> Entry<'_, K, V, S, A> {
+        let hash = make_insert_hash::<K, S>(&self.hash_builder, &key);
+        if let Some(elem) = self.table.find(hash, equivalent_key(&key)) {
             Entry::Occupied(OccupiedEntry {
                 hash,
                 key: Some(key),
@@ -810,8 +941,8 @@
         Q: Hash + Eq,
     {
         // Avoid `Option::map` because it bloats LLVM IR.
-        match self.get_key_value(k) {
-            Some((_, v)) => Some(v),
+        match self.get_inner(k) {
+            Some(&(_, ref v)) => Some(v),
             None => None,
         }
     }
@@ -841,17 +972,23 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        let hash = make_hash(&self.hash_builder, k);
         // Avoid `Option::map` because it bloats LLVM IR.
-        match self.table.find(hash, |x| k.eq(x.0.borrow())) {
-            Some(item) => unsafe {
-                let &(ref key, ref value) = item.as_ref();
-                Some((key, value))
-            },
+        match self.get_inner(k) {
+            Some(&(ref key, ref value)) => Some((key, value)),
             None => None,
         }
     }
 
+    #[inline]
+    fn get_inner<Q: ?Sized>(&self, k: &Q) -> Option<&(K, V)>
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
+    {
+        let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
+        self.table.get(hash, equivalent_key(k))
+    }
+
     /// Returns the key-value pair corresponding to the supplied key, with a mutable reference to value.
     ///
     /// The supplied key may be any borrowed form of the map's key type, but
@@ -881,13 +1018,9 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        let hash = make_hash(&self.hash_builder, k);
         // Avoid `Option::map` because it bloats LLVM IR.
-        match self.table.find(hash, |x| k.eq(x.0.borrow())) {
-            Some(item) => unsafe {
-                let &mut (ref key, ref mut value) = item.as_mut();
-                Some((key, value))
-            },
+        match self.get_inner_mut(k) {
+            Some(&mut (ref key, ref mut value)) => Some((key, value)),
             None => None,
         }
     }
@@ -917,7 +1050,7 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        self.get(k).is_some()
+        self.get_inner(k).is_some()
     }
 
     /// Returns a mutable reference to the value corresponding to the key.
@@ -947,14 +1080,158 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        let hash = make_hash(&self.hash_builder, k);
         // Avoid `Option::map` because it bloats LLVM IR.
-        match self.table.find(hash, |x| k.eq(x.0.borrow())) {
-            Some(item) => Some(unsafe { &mut item.as_mut().1 }),
+        match self.get_inner_mut(k) {
+            Some(&mut (_, ref mut v)) => Some(v),
             None => None,
         }
     }
 
+    #[inline]
+    fn get_inner_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut (K, V)>
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
+    {
+        let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
+        self.table.get_mut(hash, equivalent_key(k))
+    }
+
+    /// Attempts to get mutable references to `N` values in the map at once.
+    ///
+    /// Returns an array of length `N` with the results of each query. For soundness,
+    /// at most one mutable reference will be returned to any value. An
+    /// `Err(UnavailableMutError::Duplicate(i))` in the returned array indicates that a suitable
+    /// key-value pair exists, but a mutable reference to the value already occurs at index `i` in
+    /// the returned array.
+    ///
+    /// This method is available only if the `nightly` feature is enabled.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::{HashMap, UnavailableMutError};
+    ///
+    /// let mut libraries = HashMap::new();
+    /// libraries.insert("Bodleian Library".to_string(), 1602);
+    /// libraries.insert("Athenæum".to_string(), 1807);
+    /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
+    /// libraries.insert("Library of Congress".to_string(), 1800);
+    ///
+    /// let got = libraries.get_each_mut([
+    ///     "Athenæum",
+    ///     "New York Public Library",
+    ///     "Athenæum",
+    ///     "Library of Congress",
+    /// ]);
+    /// assert_eq!(
+    ///     got,
+    ///     [
+    ///         Ok(&mut 1807),
+    ///         Err(UnavailableMutError::Absent),
+    ///         Err(UnavailableMutError::Duplicate(0)),
+    ///         Ok(&mut 1800),
+    ///     ]
+    /// );
+    /// ```
+    #[cfg(feature = "nightly")]
+    pub fn get_each_mut<Q: ?Sized, const N: usize>(
+        &mut self,
+        ks: [&Q; N],
+    ) -> [Result<&'_ mut V, UnavailableMutError>; N]
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
+    {
+        let mut pairs = self.get_each_inner_mut(ks);
+        // TODO use `MaybeUninit::uninit_array` here instead once that's stable.
+        let mut out: [MaybeUninit<Result<&'_ mut V, UnavailableMutError>>; N] =
+            unsafe { MaybeUninit::uninit().assume_init() };
+        for i in 0..N {
+            out[i] = MaybeUninit::new(
+                mem::replace(&mut pairs[i], Err(UnavailableMutError::Absent)).map(|(_, v)| v),
+            );
+        }
+        unsafe { MaybeUninit::array_assume_init(out) }
+    }
+
+    /// Attempts to get mutable references to `N` values in the map at once, with immutable
+    /// references to the corresponding keys.
+    ///
+    /// Returns an array of length `N` with the results of each query. For soundness,
+    /// at most one mutable reference will be returned to any value. An
+    /// `Err(UnavailableMutError::Duplicate(i))` in the returned array indicates that a suitable
+    /// key-value pair exists, but a mutable reference to the value already occurs at index `i` in
+    /// the returned array.
+    ///
+    /// This method is available only if the `nightly` feature is enabled.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::{HashMap, UnavailableMutError};
+    ///
+    /// let mut libraries = HashMap::new();
+    /// libraries.insert("Bodleian Library".to_string(), 1602);
+    /// libraries.insert("Athenæum".to_string(), 1807);
+    /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
+    /// libraries.insert("Library of Congress".to_string(), 1800);
+    ///
+    /// let got = libraries.get_each_key_value_mut([
+    ///     "Bodleian Library",
+    ///     "Herzogin-Anna-Amalia-Bibliothek",
+    ///     "Herzogin-Anna-Amalia-Bibliothek",
+    ///     "Gewandhaus",
+    /// ]);
+    /// assert_eq!(
+    ///     got,
+    ///     [
+    ///         Ok((&"Bodleian Library".to_string(), &mut 1602)),
+    ///         Ok((&"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691)),
+    ///         Err(UnavailableMutError::Duplicate(1)),
+    ///         Err(UnavailableMutError::Absent),
+    ///     ]
+    /// );
+    /// ```
+    #[cfg(feature = "nightly")]
+    pub fn get_each_key_value_mut<Q: ?Sized, const N: usize>(
+        &mut self,
+        ks: [&Q; N],
+    ) -> [Result<(&'_ K, &'_ mut V), UnavailableMutError>; N]
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
+    {
+        let mut pairs = self.get_each_inner_mut(ks);
+        // TODO use `MaybeUninit::uninit_array` here instead once that's stable.
+        let mut out: [MaybeUninit<Result<(&'_ K, &'_ mut V), UnavailableMutError>>; N] =
+            unsafe { MaybeUninit::uninit().assume_init() };
+        for i in 0..N {
+            out[i] = MaybeUninit::new(
+                mem::replace(&mut pairs[i], Err(UnavailableMutError::Absent))
+                    .map(|(k, v)| (&*k, v)),
+            );
+        }
+        unsafe { MaybeUninit::array_assume_init(out) }
+    }
+
+    #[cfg(feature = "nightly")]
+    fn get_each_inner_mut<Q: ?Sized, const N: usize>(
+        &mut self,
+        ks: [&Q; N],
+    ) -> [Result<&'_ mut (K, V), UnavailableMutError>; N]
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
+    {
+        let mut hashes = [0_u64; N];
+        for i in 0..N {
+            hashes[i] = make_hash::<K, Q, S>(&self.hash_builder, ks[i]);
+        }
+        self.table
+            .get_each_mut(hashes, |i, (k, _)| ks[i].eq(k.borrow()))
+    }
+
     /// Inserts a key-value pair into the map.
     ///
     /// If the map did not have this key present, [`None`] is returned.
@@ -982,16 +1259,48 @@
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn insert(&mut self, k: K, v: V) -> Option<V> {
-        unsafe {
-            let hash = make_hash(&self.hash_builder, &k);
-            if let Some(item) = self.table.find(hash, |x| k.eq(&x.0)) {
-                Some(mem::replace(&mut item.as_mut().1, v))
-            } else {
-                let hash_builder = &self.hash_builder;
-                self.table
-                    .insert(hash, (k, v), |x| make_hash(hash_builder, &x.0));
-                None
-            }
+        let hash = make_insert_hash::<K, S>(&self.hash_builder, &k);
+        if let Some((_, item)) = self.table.get_mut(hash, equivalent_key(&k)) {
+            Some(mem::replace(item, v))
+        } else {
+            self.table
+                .insert(hash, (k, v), make_hasher::<K, _, V, S>(&self.hash_builder));
+            None
+        }
+    }
+
+    /// Tries to insert a key-value pair into the map, and returns
+    /// a mutable reference to the value in the entry.
+    ///
+    /// # Errors
+    ///
+    /// If the map already had this key present, nothing is updated, and
+    /// an error containing the occupied entry and the value is returned.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// use hashbrown::HashMap;
+    ///
+    /// let mut map = HashMap::new();
+    /// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
+    ///
+    /// let err = map.try_insert(37, "b").unwrap_err();
+    /// assert_eq!(err.entry.key(), &37);
+    /// assert_eq!(err.entry.get(), &"a");
+    /// assert_eq!(err.value, "b");
+    /// ```
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn try_insert(
+        &mut self,
+        key: K,
+        value: V,
+    ) -> Result<&mut V, OccupiedError<'_, K, V, S, A>> {
+        match self.entry(key) {
+            Entry::Occupied(entry) => Err(OccupiedError { entry, value }),
+            Entry::Vacant(entry) => Ok(entry.insert(value)),
         }
     }
 
@@ -1054,18 +1363,12 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        unsafe {
-            let hash = make_hash(&self.hash_builder, &k);
-            if let Some(item) = self.table.find(hash, |x| k.eq(x.0.borrow())) {
-                Some(self.table.remove(item))
-            } else {
-                None
-            }
-        }
+        let hash = make_hash::<K, Q, S>(&self.hash_builder, k);
+        self.table.remove_entry(hash, equivalent_key(k))
     }
 }
 
-impl<K, V, S> HashMap<K, V, S> {
+impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
     /// Creates a raw entry builder for the HashMap.
     ///
     /// Raw entries provide the lowest level of control for searching and
@@ -1098,7 +1401,7 @@
     /// acting erratically, with two keys randomly masking each other. Implementations
     /// are free to assume this doesn't happen (within the limits of memory-safety).
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S> {
+    pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S, A> {
         RawEntryBuilderMut { map: self }
     }
 
@@ -1118,16 +1421,17 @@
     ///
     /// Immutable raw entries have very limited use; you might instead want `raw_entry_mut`.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S> {
+    pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S, A> {
         RawEntryBuilder { map: self }
     }
 }
 
-impl<K, V, S> PartialEq for HashMap<K, V, S>
+impl<K, V, S, A> PartialEq for HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     V: PartialEq,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn eq(&self, other: &Self) -> bool {
         if self.len() != other.len() {
@@ -1139,40 +1443,44 @@
     }
 }
 
-impl<K, V, S> Eq for HashMap<K, V, S>
+impl<K, V, S, A> Eq for HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     V: Eq,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<K, V, S> Debug for HashMap<K, V, S>
+impl<K, V, S, A> Debug for HashMap<K, V, S, A>
 where
     K: Debug,
     V: Debug,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_map().entries(self.iter()).finish()
     }
 }
 
-impl<K, V, S> Default for HashMap<K, V, S>
+impl<K, V, S, A> Default for HashMap<K, V, S, A>
 where
     S: Default,
+    A: Default + Allocator + Clone,
 {
-    /// Creates an empty `HashMap<K, V, S>`, with the `Default` value for the hasher.
+    /// Creates an empty `HashMap<K, V, S, A>`, with the `Default` value for the hasher and allocator.
     #[cfg_attr(feature = "inline-more", inline)]
     fn default() -> Self {
-        Self::with_hasher(Default::default())
+        Self::with_hasher_in(Default::default(), Default::default())
     }
 }
 
-impl<K, Q: ?Sized, V, S> Index<&Q> for HashMap<K, V, S>
+impl<K, Q: ?Sized, V, S, A> Index<&Q> for HashMap<K, V, S, A>
 where
     K: Eq + Hash + Borrow<Q>,
     Q: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     type Output = V;
 
@@ -1252,11 +1560,11 @@
 ///
 /// [`into_iter`]: struct.HashMap.html#method.into_iter
 /// [`HashMap`]: struct.HashMap.html
-pub struct IntoIter<K, V> {
-    inner: RawIntoIter<(K, V)>,
+pub struct IntoIter<K, V, A: Allocator + Clone = Global> {
+    inner: RawIntoIter<(K, V), A>,
 }
 
-impl<K, V> IntoIter<K, V> {
+impl<K, V, A: Allocator + Clone> IntoIter<K, V, A> {
     /// Returns a iterator of references over the remaining items.
     #[cfg_attr(feature = "inline-more", inline)]
     pub(super) fn iter(&self) -> Iter<'_, K, V> {
@@ -1328,11 +1636,11 @@
 ///
 /// [`drain`]: struct.HashMap.html#method.drain
 /// [`HashMap`]: struct.HashMap.html
-pub struct Drain<'a, K, V> {
-    inner: RawDrain<'a, (K, V)>,
+pub struct Drain<'a, K, V, A: Allocator + Clone = Global> {
+    inner: RawDrain<'a, (K, V), A>,
 }
 
-impl<K, V> Drain<'_, K, V> {
+impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
     /// Returns a iterator of references over the remaining items.
     #[cfg_attr(feature = "inline-more", inline)]
     pub(super) fn iter(&self) -> Iter<'_, K, V> {
@@ -1350,17 +1658,18 @@
 ///
 /// [`drain_filter`]: struct.HashMap.html#method.drain_filter
 /// [`HashMap`]: struct.HashMap.html
-pub struct DrainFilter<'a, K, V, F>
+pub struct DrainFilter<'a, K, V, F, A: Allocator + Clone = Global>
 where
     F: FnMut(&K, &mut V) -> bool,
 {
     f: F,
-    inner: DrainFilterInner<'a, K, V>,
+    inner: DrainFilterInner<'a, K, V, A>,
 }
 
-impl<'a, K, V, F> Drop for DrainFilter<'a, K, V, F>
+impl<'a, K, V, F, A> Drop for DrainFilter<'a, K, V, F, A>
 where
     F: FnMut(&K, &mut V) -> bool,
+    A: Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
@@ -1381,9 +1690,10 @@
     }
 }
 
-impl<K, V, F> Iterator for DrainFilter<'_, K, V, F>
+impl<K, V, F, A> Iterator for DrainFilter<'_, K, V, F, A>
 where
     F: FnMut(&K, &mut V) -> bool,
+    A: Allocator + Clone,
 {
     type Item = (K, V);
 
@@ -1401,12 +1711,12 @@
 impl<K, V, F> FusedIterator for DrainFilter<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
 
 /// Portions of `DrainFilter` shared with `set::DrainFilter`
-pub(super) struct DrainFilterInner<'a, K, V> {
+pub(super) struct DrainFilterInner<'a, K, V, A: Allocator + Clone> {
     pub iter: RawIter<(K, V)>,
-    pub table: &'a mut RawTable<(K, V)>,
+    pub table: &'a mut RawTable<(K, V), A>,
 }
 
-impl<K, V> DrainFilterInner<'_, K, V> {
+impl<K, V, A: Allocator + Clone> DrainFilterInner<'_, K, V, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     pub(super) fn next<F>(&mut self, f: &mut F) -> Option<(K, V)>
     where
@@ -1440,8 +1750,8 @@
 /// See the [`HashMap::raw_entry_mut`] docs for usage examples.
 ///
 /// [`HashMap::raw_entry_mut`]: struct.HashMap.html#method.raw_entry_mut
-pub struct RawEntryBuilderMut<'a, K, V, S> {
-    map: &'a mut HashMap<K, V, S>,
+pub struct RawEntryBuilderMut<'a, K, V, S, A: Allocator + Clone = Global> {
+    map: &'a mut HashMap<K, V, S, A>,
 }
 
 /// A view into a single entry in a map, which may either be vacant or occupied.
@@ -1455,35 +1765,35 @@
 /// [`Entry`]: enum.Entry.html
 /// [`raw_entry_mut`]: struct.HashMap.html#method.raw_entry_mut
 /// [`RawEntryBuilderMut`]: struct.RawEntryBuilderMut.html
-pub enum RawEntryMut<'a, K, V, S> {
+pub enum RawEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
     /// An occupied entry.
-    Occupied(RawOccupiedEntryMut<'a, K, V, S>),
+    Occupied(RawOccupiedEntryMut<'a, K, V, S, A>),
     /// A vacant entry.
-    Vacant(RawVacantEntryMut<'a, K, V, S>),
+    Vacant(RawVacantEntryMut<'a, K, V, S, A>),
 }
 
 /// A view into an occupied entry in a `HashMap`.
 /// It is part of the [`RawEntryMut`] enum.
 ///
 /// [`RawEntryMut`]: enum.RawEntryMut.html
-pub struct RawOccupiedEntryMut<'a, K, V, S> {
+pub struct RawOccupiedEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
     elem: Bucket<(K, V)>,
-    table: &'a mut RawTable<(K, V)>,
+    table: &'a mut RawTable<(K, V), A>,
     hash_builder: &'a S,
 }
 
-unsafe impl<K, V, S> Send for RawOccupiedEntryMut<'_, K, V, S>
+unsafe impl<K, V, S, A> Send for RawOccupiedEntryMut<'_, K, V, S, A>
 where
     K: Send,
     V: Send,
-    S: Sync,
+    A: Send + Allocator + Clone,
 {
 }
-unsafe impl<K, V, S> Sync for RawOccupiedEntryMut<'_, K, V, S>
+unsafe impl<K, V, S, A> Sync for RawOccupiedEntryMut<'_, K, V, S, A>
 where
     K: Sync,
     V: Sync,
-    S: Sync,
+    A: Send + Allocator + Clone,
 {
 }
 
@@ -1491,8 +1801,8 @@
 /// It is part of the [`RawEntryMut`] enum.
 ///
 /// [`RawEntryMut`]: enum.RawEntryMut.html
-pub struct RawVacantEntryMut<'a, K, V, S> {
-    table: &'a mut RawTable<(K, V)>,
+pub struct RawVacantEntryMut<'a, K, V, S, A: Allocator + Clone = Global> {
+    table: &'a mut RawTable<(K, V), A>,
     hash_builder: &'a S,
 }
 
@@ -1501,42 +1811,41 @@
 /// See the [`HashMap::raw_entry`] docs for usage examples.
 ///
 /// [`HashMap::raw_entry`]: struct.HashMap.html#method.raw_entry
-pub struct RawEntryBuilder<'a, K, V, S> {
-    map: &'a HashMap<K, V, S>,
+pub struct RawEntryBuilder<'a, K, V, S, A: Allocator + Clone = Global> {
+    map: &'a HashMap<K, V, S, A>,
 }
 
-impl<'a, K, V, S> RawEntryBuilderMut<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
     /// Creates a `RawEntryMut` from the given key.
     #[cfg_attr(feature = "inline-more", inline)]
     #[allow(clippy::wrong_self_convention)]
-    pub fn from_key<Q: ?Sized>(self, k: &Q) -> RawEntryMut<'a, K, V, S>
+    pub fn from_key<Q: ?Sized>(self, k: &Q) -> RawEntryMut<'a, K, V, S, A>
     where
         S: BuildHasher,
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        let mut hasher = self.map.hash_builder.build_hasher();
-        k.hash(&mut hasher);
-        self.from_key_hashed_nocheck(hasher.finish(), k)
+        let hash = make_hash::<K, Q, S>(&self.map.hash_builder, k);
+        self.from_key_hashed_nocheck(hash, k)
     }
 
     /// Creates a `RawEntryMut` from the given key and its hash.
     #[inline]
     #[allow(clippy::wrong_self_convention)]
-    pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S>
+    pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S, A>
     where
         K: Borrow<Q>,
         Q: Eq,
     {
-        self.from_hash(hash, |q| q.borrow().eq(k))
+        self.from_hash(hash, equivalent(k))
     }
 }
 
-impl<'a, K, V, S> RawEntryBuilderMut<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilderMut<'a, K, V, S, A> {
     /// Creates a `RawEntryMut` from the given hash.
     #[cfg_attr(feature = "inline-more", inline)]
     #[allow(clippy::wrong_self_convention)]
-    pub fn from_hash<F>(self, hash: u64, is_match: F) -> RawEntryMut<'a, K, V, S>
+    pub fn from_hash<F>(self, hash: u64, is_match: F) -> RawEntryMut<'a, K, V, S, A>
     where
         for<'b> F: FnMut(&'b K) -> bool,
     {
@@ -1544,7 +1853,7 @@
     }
 
     #[cfg_attr(feature = "inline-more", inline)]
-    fn search<F>(self, hash: u64, mut is_match: F) -> RawEntryMut<'a, K, V, S>
+    fn search<F>(self, hash: u64, mut is_match: F) -> RawEntryMut<'a, K, V, S, A>
     where
         for<'b> F: FnMut(&'b K) -> bool,
     {
@@ -1562,7 +1871,7 @@
     }
 }
 
-impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawEntryBuilder<'a, K, V, S, A> {
     /// Access an entry by key.
     #[cfg_attr(feature = "inline-more", inline)]
     #[allow(clippy::wrong_self_convention)]
@@ -1572,9 +1881,8 @@
         K: Borrow<Q>,
         Q: Hash + Eq,
     {
-        let mut hasher = self.map.hash_builder.build_hasher();
-        k.hash(&mut hasher);
-        self.from_key_hashed_nocheck(hasher.finish(), k)
+        let hash = make_hash::<K, Q, S>(&self.map.hash_builder, k);
+        self.from_key_hashed_nocheck(hash, k)
     }
 
     /// Access an entry by a key and its hash.
@@ -1583,9 +1891,9 @@
     pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
     where
         K: Borrow<Q>,
-        Q: Hash + Eq,
+        Q: Eq,
     {
-        self.from_hash(hash, |q| q.borrow().eq(k))
+        self.from_hash(hash, equivalent(k))
     }
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1593,11 +1901,8 @@
     where
         F: FnMut(&K) -> bool,
     {
-        match self.map.table.find(hash, |(k, _)| is_match(k)) {
-            Some(item) => unsafe {
-                let &(ref key, ref value) = item.as_ref();
-                Some((key, value))
-            },
+        match self.map.table.get(hash, |(k, _)| is_match(k)) {
+            Some(&(ref key, ref value)) => Some((key, value)),
             None => None,
         }
     }
@@ -1613,7 +1918,7 @@
     }
 }
 
-impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawEntryMut<'a, K, V, S, A> {
     /// Sets the value of the entry, and returns a RawOccupiedEntryMut.
     ///
     /// # Examples
@@ -1627,7 +1932,7 @@
     /// assert_eq!(entry.remove_entry(), ("horseyland", 37));
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn insert(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, S>
+    pub fn insert(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, S, A>
     where
         K: Hash,
         S: BuildHasher,
@@ -1807,7 +2112,7 @@
     }
 }
 
-impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawOccupiedEntryMut<'a, K, V, S, A> {
     /// Gets a reference to the key in the entry.
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn key(&self) -> &K {
@@ -1902,7 +2207,7 @@
     /// the entry and allows to replace or remove it based on the
     /// value of the returned option.
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn replace_entry_with<F>(self, f: F) -> RawEntryMut<'a, K, V, S>
+    pub fn replace_entry_with<F>(self, f: F) -> RawEntryMut<'a, K, V, S, A>
     where
         F: FnOnce(&K, V) -> Option<V>,
     {
@@ -1925,7 +2230,7 @@
     }
 }
 
-impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> RawVacantEntryMut<'a, K, V, S, A> {
     /// Sets the value of the entry with the VacantEntry's key,
     /// and returns a mutable reference to it.
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1934,9 +2239,8 @@
         K: Hash,
         S: BuildHasher,
     {
-        let mut hasher = self.hash_builder.build_hasher();
-        key.hash(&mut hasher);
-        self.insert_hashed_nocheck(hasher.finish(), key, value)
+        let hash = make_insert_hash::<K, S>(self.hash_builder, &key);
+        self.insert_hashed_nocheck(hash, key, value)
     }
 
     /// Sets the value of the entry with the VacantEntry's key,
@@ -1948,8 +2252,12 @@
         K: Hash,
         S: BuildHasher,
     {
-        let hash_builder = self.hash_builder;
-        self.insert_with_hasher(hash, key, value, |k| make_hash(hash_builder, k))
+        let &mut (ref mut k, ref mut v) = self.table.insert_entry(
+            hash,
+            (key, value),
+            make_hasher::<K, _, V, S>(self.hash_builder),
+        );
+        (k, v)
     }
 
     /// Set the value of an entry with a custom hasher function.
@@ -1964,26 +2272,24 @@
     where
         H: Fn(&K) -> u64,
     {
-        unsafe {
-            let elem = self.table.insert(hash, (key, value), |x| hasher(&x.0));
-            let &mut (ref mut k, ref mut v) = elem.as_mut();
-            (k, v)
-        }
+        let &mut (ref mut k, ref mut v) = self
+            .table
+            .insert_entry(hash, (key, value), |x| hasher(&x.0));
+        (k, v)
     }
 
     #[cfg_attr(feature = "inline-more", inline)]
-    fn insert_entry(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, S>
+    fn insert_entry(self, key: K, value: V) -> RawOccupiedEntryMut<'a, K, V, S, A>
     where
         K: Hash,
         S: BuildHasher,
     {
-        let hash_builder = self.hash_builder;
-        let mut hasher = self.hash_builder.build_hasher();
-        key.hash(&mut hasher);
-
-        let elem = self.table.insert(hasher.finish(), (key, value), |k| {
-            make_hash(hash_builder, &k.0)
-        });
+        let hash = make_insert_hash::<K, S>(self.hash_builder, &key);
+        let elem = self.table.insert(
+            hash,
+            (key, value),
+            make_hasher::<K, _, V, S>(self.hash_builder),
+        );
         RawOccupiedEntryMut {
             elem,
             table: self.table,
@@ -1992,13 +2298,13 @@
     }
 }
 
-impl<K, V, S> Debug for RawEntryBuilderMut<'_, K, V, S> {
+impl<K, V, S, A: Allocator + Clone> Debug for RawEntryBuilderMut<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("RawEntryBuilder").finish()
     }
 }
 
-impl<K: Debug, V: Debug, S> Debug for RawEntryMut<'_, K, V, S> {
+impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for RawEntryMut<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
             RawEntryMut::Vacant(ref v) => f.debug_tuple("RawEntry").field(v).finish(),
@@ -2007,7 +2313,7 @@
     }
 }
 
-impl<K: Debug, V: Debug, S> Debug for RawOccupiedEntryMut<'_, K, V, S> {
+impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for RawOccupiedEntryMut<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("RawOccupiedEntryMut")
             .field("key", self.key())
@@ -2016,13 +2322,13 @@
     }
 }
 
-impl<K, V, S> Debug for RawVacantEntryMut<'_, K, V, S> {
+impl<K, V, S, A: Allocator + Clone> Debug for RawVacantEntryMut<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("RawVacantEntryMut").finish()
     }
 }
 
-impl<K, V, S> Debug for RawEntryBuilder<'_, K, V, S> {
+impl<K, V, S, A: Allocator + Clone> Debug for RawEntryBuilder<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("RawEntryBuilder").finish()
     }
@@ -2034,15 +2340,18 @@
 ///
 /// [`HashMap`]: struct.HashMap.html
 /// [`entry`]: struct.HashMap.html#method.entry
-pub enum Entry<'a, K, V, S> {
+pub enum Entry<'a, K, V, S, A = Global>
+where
+    A: Allocator + Clone,
+{
     /// An occupied entry.
-    Occupied(OccupiedEntry<'a, K, V, S>),
+    Occupied(OccupiedEntry<'a, K, V, S, A>),
 
     /// A vacant entry.
-    Vacant(VacantEntry<'a, K, V, S>),
+    Vacant(VacantEntry<'a, K, V, S, A>),
 }
 
-impl<K: Debug, V: Debug, S> Debug for Entry<'_, K, V, S> {
+impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for Entry<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
             Entry::Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
@@ -2055,29 +2364,31 @@
 /// It is part of the [`Entry`] enum.
 ///
 /// [`Entry`]: enum.Entry.html
-pub struct OccupiedEntry<'a, K, V, S> {
+pub struct OccupiedEntry<'a, K, V, S, A: Allocator + Clone = Global> {
     hash: u64,
     key: Option<K>,
     elem: Bucket<(K, V)>,
-    table: &'a mut HashMap<K, V, S>,
+    table: &'a mut HashMap<K, V, S, A>,
 }
 
-unsafe impl<K, V, S> Send for OccupiedEntry<'_, K, V, S>
+unsafe impl<K, V, S, A> Send for OccupiedEntry<'_, K, V, S, A>
 where
     K: Send,
     V: Send,
     S: Send,
+    A: Send + Allocator + Clone,
 {
 }
-unsafe impl<K, V, S> Sync for OccupiedEntry<'_, K, V, S>
+unsafe impl<K, V, S, A> Sync for OccupiedEntry<'_, K, V, S, A>
 where
     K: Sync,
     V: Sync,
     S: Sync,
+    A: Sync + Allocator + Clone,
 {
 }
 
-impl<K: Debug, V: Debug, S> Debug for OccupiedEntry<'_, K, V, S> {
+impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for OccupiedEntry<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("OccupiedEntry")
             .field("key", self.key())
@@ -2090,19 +2401,53 @@
 /// It is part of the [`Entry`] enum.
 ///
 /// [`Entry`]: enum.Entry.html
-pub struct VacantEntry<'a, K, V, S> {
+pub struct VacantEntry<'a, K, V, S, A: Allocator + Clone = Global> {
     hash: u64,
     key: K,
-    table: &'a mut HashMap<K, V, S>,
+    table: &'a mut HashMap<K, V, S, A>,
 }
 
-impl<K: Debug, V, S> Debug for VacantEntry<'_, K, V, S> {
+impl<K: Debug, V, S, A: Allocator + Clone> Debug for VacantEntry<'_, K, V, S, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_tuple("VacantEntry").field(self.key()).finish()
     }
 }
 
-impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {
+/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
+///
+/// Contains the occupied entry, and the value that was not inserted.
+pub struct OccupiedError<'a, K, V, S, A: Allocator + Clone = Global> {
+    /// The entry in the map that was already occupied.
+    pub entry: OccupiedEntry<'a, K, V, S, A>,
+    /// The value which was not inserted, because the entry was already occupied.
+    pub value: V,
+}
+
+impl<K: Debug, V: Debug, S, A: Allocator + Clone> Debug for OccupiedError<'_, K, V, S, A> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("OccupiedError")
+            .field("key", self.entry.key())
+            .field("old_value", self.entry.get())
+            .field("new_value", &self.value)
+            .finish()
+    }
+}
+
+impl<'a, K: Debug, V: Debug, S, A: Allocator + Clone> fmt::Display
+    for OccupiedError<'a, K, V, S, A>
+{
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "failed to insert {:?}, key {:?} already exists with value {:?}",
+            self.value,
+            self.entry.key(),
+            self.entry.get(),
+        )
+    }
+}
+
+impl<'a, K, V, S, A: Allocator + Clone> IntoIterator for &'a HashMap<K, V, S, A> {
     type Item = (&'a K, &'a V);
     type IntoIter = Iter<'a, K, V>;
 
@@ -2112,7 +2457,7 @@
     }
 }
 
-impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> IntoIterator for &'a mut HashMap<K, V, S, A> {
     type Item = (&'a K, &'a mut V);
     type IntoIter = IterMut<'a, K, V>;
 
@@ -2122,9 +2467,9 @@
     }
 }
 
-impl<K, V, S> IntoIterator for HashMap<K, V, S> {
+impl<K, V, S, A: Allocator + Clone> IntoIterator for HashMap<K, V, S, A> {
     type Item = (K, V);
-    type IntoIter = IntoIter<K, V>;
+    type IntoIter = IntoIter<K, V, A>;
 
     /// Creates a consuming iterator, that is, one that moves each key-value
     /// pair out of the map in arbitrary order. The map cannot be used after
@@ -2144,7 +2489,7 @@
     /// let vec: Vec<(&str, i32)> = map.into_iter().collect();
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    fn into_iter(self) -> IntoIter<K, V> {
+    fn into_iter(self) -> IntoIter<K, V, A> {
         IntoIter {
             inner: self.table.into_iter(),
         }
@@ -2216,7 +2561,7 @@
     }
 }
 
-impl<K, V> Iterator for IntoIter<K, V> {
+impl<K, V, A: Allocator + Clone> Iterator for IntoIter<K, V, A> {
     type Item = (K, V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -2228,15 +2573,15 @@
         self.inner.size_hint()
     }
 }
-impl<K, V> ExactSizeIterator for IntoIter<K, V> {
+impl<K, V, A: Allocator + Clone> ExactSizeIterator for IntoIter<K, V, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-impl<K, V> FusedIterator for IntoIter<K, V> {}
+impl<K, V, A: Allocator + Clone> FusedIterator for IntoIter<K, V, A> {}
 
-impl<K: Debug, V: Debug> fmt::Debug for IntoIter<K, V> {
+impl<K: Debug, V: Debug, A: Allocator + Clone> fmt::Debug for IntoIter<K, V, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.iter()).finish()
     }
@@ -2324,7 +2669,7 @@
     }
 }
 
-impl<'a, K, V> Iterator for Drain<'a, K, V> {
+impl<'a, K, V, A: Allocator + Clone> Iterator for Drain<'a, K, V, A> {
     type Item = (K, V);
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -2336,25 +2681,26 @@
         self.inner.size_hint()
     }
 }
-impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
+impl<K, V, A: Allocator + Clone> ExactSizeIterator for Drain<'_, K, V, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-impl<K, V> FusedIterator for Drain<'_, K, V> {}
+impl<K, V, A: Allocator + Clone> FusedIterator for Drain<'_, K, V, A> {}
 
-impl<K, V> fmt::Debug for Drain<'_, K, V>
+impl<K, V, A> fmt::Debug for Drain<'_, K, V, A>
 where
     K: fmt::Debug,
     V: fmt::Debug,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.iter()).finish()
     }
 }
 
-impl<'a, K, V, S> Entry<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> Entry<'a, K, V, S, A> {
     /// Sets the value of the entry, and returns an OccupiedEntry.
     ///
     /// # Examples
@@ -2368,7 +2714,7 @@
     /// assert_eq!(entry.key(), &"horseyland");
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V, S>
+    pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
     where
         K: Hash,
         S: BuildHasher,
@@ -2437,9 +2783,12 @@
         }
     }
 
-    /// Ensures a value is in the entry by inserting, if empty, the result of the default function,
-    /// which takes the key as its argument, and returns a mutable reference to the value in the
-    /// entry.
+    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
+    /// This method allows for generating key-derived values for insertion by providing the default
+    /// function a reference to the key that was moved during the `.entry(key)` method call.
+    ///
+    /// The reference to the moved key is provided so that cloning or copying the key is
+    /// unnecessary, unlike with `.or_insert_with(|| ... )`.
     ///
     /// # Examples
     ///
@@ -2585,7 +2934,7 @@
     }
 }
 
-impl<'a, K, V: Default, S> Entry<'a, K, V, S> {
+impl<'a, K, V: Default, S, A: Allocator + Clone> Entry<'a, K, V, S, A> {
     /// Ensures a value is in the entry by inserting the default value if empty,
     /// and returns a mutable reference to the value in the entry.
     ///
@@ -2612,7 +2961,7 @@
     }
 }
 
-impl<'a, K, V, S> OccupiedEntry<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> OccupiedEntry<'a, K, V, S, A> {
     /// Gets a reference to the key in the entry.
     ///
     /// # Examples
@@ -2781,6 +3130,10 @@
     /// Replaces the entry, returning the old key and value. The new key in the hash map will be
     /// the key used to create this entry.
     ///
+    /// # Panics
+    ///
+    /// Will panic if this OccupiedEntry was created through [`Entry::insert`].
+    ///
     /// # Examples
     ///
     /// ```
@@ -2810,6 +3163,10 @@
 
     /// Replaces the key in the hash map with the key used to create this entry.
     ///
+    /// # Panics
+    ///
+    /// Will panic if this OccupiedEntry was created through [`Entry::insert`].
+    ///
     /// # Examples
     ///
     /// ```
@@ -2887,7 +3244,7 @@
     /// assert!(!map.contains_key("poneyland"));
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn replace_entry_with<F>(self, f: F) -> Entry<'a, K, V, S>
+    pub fn replace_entry_with<F>(self, f: F) -> Entry<'a, K, V, S, A>
     where
         F: FnOnce(&K, V) -> Option<V>,
     {
@@ -2918,7 +3275,7 @@
     }
 }
 
-impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
+impl<'a, K, V, S, A: Allocator + Clone> VacantEntry<'a, K, V, S, A> {
     /// Gets a reference to the key that would be used when inserting a value
     /// through the `VacantEntry`.
     ///
@@ -2976,23 +3333,26 @@
         K: Hash,
         S: BuildHasher,
     {
-        let hash_builder = &self.table.hash_builder;
-        let bucket = self.table.table.insert(self.hash, (self.key, value), |x| {
-            make_hash(hash_builder, &x.0)
-        });
-        unsafe { &mut bucket.as_mut().1 }
+        let table = &mut self.table.table;
+        let entry = table.insert_entry(
+            self.hash,
+            (self.key, value),
+            make_hasher::<K, _, V, S>(&self.table.hash_builder),
+        );
+        &mut entry.1
     }
 
     #[cfg_attr(feature = "inline-more", inline)]
-    fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S>
+    fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V, S, A>
     where
         K: Hash,
         S: BuildHasher,
     {
-        let hash_builder = &self.table.hash_builder;
-        let elem = self.table.table.insert(self.hash, (self.key, value), |x| {
-            make_hash(hash_builder, &x.0)
-        });
+        let elem = self.table.table.insert(
+            self.hash,
+            (self.key, value),
+            make_hasher::<K, _, V, S>(&self.table.hash_builder),
+        );
         OccupiedEntry {
             hash: self.hash,
             key: None,
@@ -3002,15 +3362,17 @@
     }
 }
 
-impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
+impl<K, V, S, A> FromIterator<(K, V)> for HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     S: BuildHasher + Default,
+    A: Default + Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
         let iter = iter.into_iter();
-        let mut map = Self::with_capacity_and_hasher(iter.size_hint().0, S::default());
+        let mut map =
+            Self::with_capacity_and_hasher_in(iter.size_hint().0, S::default(), A::default());
         iter.for_each(|(k, v)| {
             map.insert(k, v);
         });
@@ -3020,10 +3382,11 @@
 
 /// Inserts all new key-values from the iterator and replaces values with existing
 /// keys with new values returned from the iterator.
-impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
+impl<K, V, S, A> Extend<(K, V)> for HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
@@ -3065,11 +3428,12 @@
     }
 }
 
-impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
+impl<'a, K, V, S, A> Extend<(&'a K, &'a V)> for HashMap<K, V, S, A>
 where
     K: Eq + Hash + Copy,
     V: Copy,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
@@ -3103,10 +3467,14 @@
     fn iter_val<'a, 'new>(v: Iter<'a, u8, &'static str>) -> Iter<'a, u8, &'new str> {
         v
     }
-    fn into_iter_key<'new>(v: IntoIter<&'static str, u8>) -> IntoIter<&'new str, u8> {
+    fn into_iter_key<'new, A: Allocator + Clone>(
+        v: IntoIter<&'static str, u8, A>,
+    ) -> IntoIter<&'new str, u8, A> {
         v
     }
-    fn into_iter_val<'new>(v: IntoIter<u8, &'static str>) -> IntoIter<u8, &'new str> {
+    fn into_iter_val<'new, A: Allocator + Clone>(
+        v: IntoIter<u8, &'static str, A>,
+    ) -> IntoIter<u8, &'new str, A> {
         v
     }
     fn keys_key<'a, 'new>(v: Keys<'a, &'static str, u8>) -> Keys<'a, &'new str, u8> {
@@ -3135,6 +3503,7 @@
     use super::{HashMap, RawEntryMut};
     use crate::TryReserveError::*;
     use rand::{rngs::SmallRng, Rng, SeedableRng};
+    use std::borrow::ToOwned;
     use std::cell::RefCell;
     use std::usize;
     use std::vec::Vec;
@@ -4290,11 +4659,7 @@
         let mut map: HashMap<_, _> = xs.iter().cloned().collect();
 
         let compute_hash = |map: &HashMap<i32, i32>, k: i32| -> u64 {
-            use core::hash::{BuildHasher, Hash, Hasher};
-
-            let mut hasher = map.hasher().build_hasher();
-            k.hash(&mut hasher);
-            hasher.finish()
+            super::make_insert_hash::<i32, _>(map.hasher(), &k)
         };
 
         // Existing key (insert)
@@ -4471,9 +4836,11 @@
                             left -= 1;
                         } else {
                             assert!(removed.contains(&(i, 2 * i)), "{} not in {:?}", i, removed);
-                            let e = m
-                                .table
-                                .insert(hash, (i, 2 * i), |x| super::make_hash(&hasher, &x.0));
+                            let e = m.table.insert(
+                                hash,
+                                (i, 2 * i),
+                                super::make_hasher::<usize, _, usize, _>(&hasher),
+                            );
                             it.reflect_insert(&e);
                             if let Some(p) = removed.iter().position(|e| e == &(i, 2 * i)) {
                                 removed.swap_remove(p);
@@ -4504,7 +4871,6 @@
     #[test]
     fn test_const_with_hasher() {
         use core::hash::BuildHasher;
-        use std::borrow::ToOwned;
         use std::collections::hash_map::DefaultHasher;
 
         #[derive(Clone)]
@@ -4524,4 +4890,33 @@
         map.insert(17, "seventeen".to_owned());
         assert_eq!("seventeen", map[&17]);
     }
+
+    #[test]
+    #[cfg(feature = "nightly")]
+    fn test_get_each_mut() {
+        use crate::UnavailableMutError::*;
+
+        let mut map = HashMap::new();
+        map.insert("foo".to_owned(), 0);
+        map.insert("bar".to_owned(), 10);
+        map.insert("baz".to_owned(), 20);
+        map.insert("qux".to_owned(), 30);
+
+        let xs = map.get_each_mut(["foo", "dud", "foo", "qux"]);
+        assert_eq!(
+            xs,
+            [Ok(&mut 0), Err(Absent), Err(Duplicate(0)), Ok(&mut 30)]
+        );
+
+        let ys = map.get_each_key_value_mut(["bar", "baz", "baz", "dip"]);
+        assert_eq!(
+            ys,
+            [
+                Ok((&"bar".to_owned(), &mut 10)),
+                Ok((&"baz".to_owned(), &mut 20)),
+                Err(Duplicate(1)),
+                Err(Absent),
+            ]
+        );
+    }
 }
diff --git a/sgx_tstd/hashbrown/src/raw/alloc.rs b/sgx_tstd/hashbrown/src/raw/alloc.rs
new file mode 100644
index 0000000..de6c455
--- /dev/null
+++ b/sgx_tstd/hashbrown/src/raw/alloc.rs
@@ -0,0 +1,72 @@
+pub(crate) use self::inner::{do_alloc, Allocator, Global};
+
+#[cfg(feature = "nightly")]
+mod inner {
+    use crate::alloc::alloc::Layout;
+    pub use crate::alloc::alloc::{Allocator, Global};
+    use core::ptr::NonNull;
+
+    #[allow(clippy::map_err_ignore)]
+    pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
+        alloc
+            .allocate(layout)
+            .map(|ptr| ptr.as_non_null_ptr())
+            .map_err(|_| ())
+    }
+
+    #[cfg(feature = "bumpalo")]
+    unsafe impl Allocator for crate::BumpWrapper<'_> {
+        #[inline]
+        fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
+            match self.0.try_alloc_layout(layout) {
+                Ok(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
+                Err(_) => Err(core::alloc::AllocError),
+            }
+        }
+        #[inline]
+        unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
+    }
+}
+
+#[cfg(not(feature = "nightly"))]
+mod inner {
+    use crate::alloc::alloc::{alloc, dealloc, Layout};
+    use core::ptr::NonNull;
+
+    pub unsafe trait Allocator {
+        fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()>;
+        unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
+    }
+
+    #[derive(Copy, Clone)]
+    pub struct Global;
+    unsafe impl Allocator for Global {
+        #[inline]
+        fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> {
+            unsafe { NonNull::new(alloc(layout)).ok_or(()) }
+        }
+        #[inline]
+        unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
+            dealloc(ptr.as_ptr(), layout)
+        }
+    }
+    impl Default for Global {
+        #[inline]
+        fn default() -> Self {
+            Global
+        }
+    }
+
+    pub fn do_alloc<A: Allocator>(alloc: &A, layout: Layout) -> Result<NonNull<u8>, ()> {
+        alloc.allocate(layout)
+    }
+
+    #[cfg(feature = "bumpalo")]
+    unsafe impl Allocator for crate::BumpWrapper<'_> {
+        #[allow(clippy::map_err_ignore)]
+        fn allocate(&self, layout: Layout) -> Result<NonNull<u8>, ()> {
+            self.0.try_alloc_layout(layout).map_err(|_| ())
+        }
+        unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
+    }
+}
diff --git a/sgx_tstd/hashbrown/src/raw/generic.rs b/sgx_tstd/hashbrown/src/raw/generic.rs
index 26f8c58..ef066e8 100644
--- a/sgx_tstd/hashbrown/src/raw/generic.rs
+++ b/sgx_tstd/hashbrown/src/raw/generic.rs
@@ -55,7 +55,7 @@
         struct AlignedBytes {
             _align: [Group; 0],
             bytes: [u8; Group::WIDTH],
-        };
+        }
         const ALIGNED_BYTES: AlignedBytes = AlignedBytes {
             _align: [],
             bytes: [EMPTY; Group::WIDTH],
@@ -67,7 +67,7 @@
     #[inline]
     #[allow(clippy::cast_ptr_alignment)] // unaligned load
     pub unsafe fn load(ptr: *const u8) -> Self {
-        Group(ptr::read_unaligned(ptr as *const _))
+        Group(ptr::read_unaligned(ptr.cast()))
     }
 
     /// Loads a group of bytes starting at the given address, which must be
@@ -77,7 +77,7 @@
     pub unsafe fn load_aligned(ptr: *const u8) -> Self {
         // FIXME: use align_offset once it stabilizes
         debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
-        Group(ptr::read(ptr as *const _))
+        Group(ptr::read(ptr.cast()))
     }
 
     /// Stores the group of bytes to the given address, which must be
@@ -87,7 +87,7 @@
     pub unsafe fn store_aligned(self, ptr: *mut u8) {
         // FIXME: use align_offset once it stabilizes
         debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
-        ptr::write(ptr as *mut _, self.0);
+        ptr::write(ptr.cast(), self.0);
     }
 
     /// Returns a `BitMask` indicating all bytes in the group which *may*
diff --git a/sgx_tstd/hashbrown/src/raw/mod.rs b/sgx_tstd/hashbrown/src/raw/mod.rs
index fe95932..3ae6980 100644
--- a/sgx_tstd/hashbrown/src/raw/mod.rs
+++ b/sgx_tstd/hashbrown/src/raw/mod.rs
@@ -1,12 +1,15 @@
-use crate::alloc::alloc::{alloc, dealloc, handle_alloc_error};
+use crate::alloc::alloc::{handle_alloc_error, Layout};
 use crate::scopeguard::guard;
 use crate::TryReserveError;
-use core::alloc::Layout;
+#[cfg(feature = "nightly")]
+use crate::UnavailableMutError;
 use core::hint;
 use core::iter::FusedIterator;
 use core::marker::PhantomData;
 use core::mem;
 use core::mem::ManuallyDrop;
+#[cfg(feature = "nightly")]
+use core::mem::MaybeUninit;
 use core::ptr::NonNull;
 
 cfg_if! {
@@ -32,6 +35,9 @@
     }
 }
 
+mod alloc;
+pub(crate) use self::alloc::{do_alloc, Allocator, Global};
+
 mod bitmask;
 
 use self::bitmask::{BitMask, BitMaskIter};
@@ -41,14 +47,28 @@
 // consistently improves performance by 10-15%.
 #[cfg(feature = "nightly")]
 use core::intrinsics::{likely, unlikely};
+
+// On stable we can use #[cold] to get a equivalent effect: this attributes
+// suggests that the function is unlikely to be called
+#[cfg(not(feature = "nightly"))]
+#[inline]
+#[cold]
+fn cold() {}
+
 #[cfg(not(feature = "nightly"))]
 #[inline]
 fn likely(b: bool) -> bool {
+    if !b {
+        cold()
+    }
     b
 }
 #[cfg(not(feature = "nightly"))]
 #[inline]
 fn unlikely(b: bool) -> bool {
+    if b {
+        cold()
+    }
     b
 }
 
@@ -145,27 +165,22 @@
 /// Proof that the probe will visit every group in the table:
 /// <https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/>
 struct ProbeSeq {
-    bucket_mask: usize,
     pos: usize,
     stride: usize,
 }
 
-impl Iterator for ProbeSeq {
-    type Item = usize;
-
+impl ProbeSeq {
     #[inline]
-    fn next(&mut self) -> Option<usize> {
+    fn move_next(&mut self, bucket_mask: usize) {
         // We should have found an empty bucket by now and ended the probe.
         debug_assert!(
-            self.stride <= self.bucket_mask,
+            self.stride <= bucket_mask,
             "Went past end of probe sequence"
         );
 
-        let result = self.pos;
         self.stride += Group::WIDTH;
         self.pos += self.stride;
-        self.pos &= self.bucket_mask;
-        Some(result)
+        self.pos &= bucket_mask;
     }
 }
 
@@ -214,30 +229,39 @@
     }
 }
 
-/// Returns a Layout which describes the allocation required for a hash table,
-/// and the offset of the control bytes in the allocation.
-/// (the offset is also one past last element of buckets)
-///
-/// Returns `None` if an overflow occurs.
-#[cfg_attr(feature = "inline-more", inline)]
-#[cfg(feature = "nightly")]
-fn calculate_layout<T>(buckets: usize) -> Option<(Layout, usize)> {
-    debug_assert!(buckets.is_power_of_two());
+/// Helper which allows the max calculation for ctrl_align to be statically computed for each T
+/// while keeping the rest of `calculate_layout_for` independent of `T`
+#[derive(Copy, Clone)]
+struct TableLayout {
+    size: usize,
+    ctrl_align: usize,
+}
 
-    // Array of buckets
-    let data = Layout::array::<T>(buckets).ok()?;
+impl TableLayout {
+    #[inline]
+    fn new<T>() -> Self {
+        let layout = Layout::new::<T>();
+        Self {
+            size: layout.size(),
+            ctrl_align: usize::max(layout.align(), Group::WIDTH),
+        }
+    }
 
-    // Array of control bytes. This must be aligned to the group size.
-    //
-    // We add `Group::WIDTH` control bytes at the end of the array which
-    // replicate the bytes at the start of the array and thus avoids the need to
-    // perform bounds-checking while probing.
-    //
-    // There is no possible overflow here since buckets is a power of two and
-    // Group::WIDTH is a small number.
-    let ctrl = unsafe { Layout::from_size_align_unchecked(buckets + Group::WIDTH, Group::WIDTH) };
+    #[inline]
+    fn calculate_layout_for(self, buckets: usize) -> Option<(Layout, usize)> {
+        debug_assert!(buckets.is_power_of_two());
 
-    data.extend(ctrl).ok()
+        let TableLayout { size, ctrl_align } = self;
+        // Manual layout calculation since Layout methods are not yet stable.
+        let ctrl_offset =
+            size.checked_mul(buckets)?.checked_add(ctrl_align - 1)? & !(ctrl_align - 1);
+        let len = ctrl_offset.checked_add(buckets + Group::WIDTH)?;
+
+        Some((
+            unsafe { Layout::from_size_align_unchecked(len, ctrl_align) },
+            ctrl_offset,
+        ))
+    }
 }
 
 /// Returns a Layout which describes the allocation required for a hash table,
@@ -246,22 +270,8 @@
 ///
 /// Returns `None` if an overflow occurs.
 #[cfg_attr(feature = "inline-more", inline)]
-#[cfg(not(feature = "nightly"))]
 fn calculate_layout<T>(buckets: usize) -> Option<(Layout, usize)> {
-    debug_assert!(buckets.is_power_of_two());
-
-    // Manual layout calculation since Layout methods are not yet stable.
-    let ctrl_align = usize::max(mem::align_of::<T>(), Group::WIDTH);
-    let ctrl_offset = mem::size_of::<T>()
-        .checked_mul(buckets)?
-        .checked_add(ctrl_align - 1)?
-        & !(ctrl_align - 1);
-    let len = ctrl_offset.checked_add(buckets + Group::WIDTH)?;
-
-    Some((
-        unsafe { Layout::from_size_align_unchecked(len, ctrl_align) },
-        ctrl_offset,
-    ))
+    TableLayout::new::<T>().calculate_layout_for(buckets)
 }
 
 /// A reference to a hash table bucket containing a `T`.
@@ -310,12 +320,12 @@
         }
     }
     #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn as_ptr(&self) -> *mut T {
+    pub fn as_ptr(&self) -> *mut T {
         if mem::size_of::<T>() == 0 {
             // Just return an arbitrary ZST pointer which is properly aligned
             mem::align_of::<T>() as *mut T
         } else {
-            self.ptr.as_ptr().sub(1)
+            unsafe { self.ptr.as_ptr().sub(1) }
         }
     }
     #[cfg_attr(feature = "inline-more", inline)]
@@ -356,7 +366,15 @@
 }
 
 /// A raw hash table with an unsafe API.
-pub struct RawTable<T> {
+pub struct RawTable<T, A: Allocator + Clone = Global> {
+    table: RawTableInner<A>,
+    // Tell dropck that we own instances of T.
+    marker: PhantomData<T>,
+}
+
+/// Non-generic part of `RawTable` which allows functions to be instantiated only once regardless
+/// of how many different key-value types are used.
+struct RawTableInner<A> {
     // Mask to get an index from a hash value. The value is one less than the
     // number of buckets in the table.
     bucket_mask: usize,
@@ -371,11 +389,10 @@
     // Number of elements in the table, only really used by len()
     items: usize,
 
-    // Tell dropck that we own instances of T.
-    marker: PhantomData<T>,
+    alloc: A,
 }
 
-impl<T> RawTable<T> {
+impl<T> RawTable<T, Global> {
     /// Creates a new empty hash table without allocating any memory.
     ///
     /// In effect this returns a table with exactly 1 bucket. However we can
@@ -384,11 +401,36 @@
     #[cfg_attr(feature = "inline-more", inline)]
     pub const fn new() -> Self {
         Self {
-            // Be careful to cast the entire slice to a raw pointer.
-            ctrl: unsafe { NonNull::new_unchecked(Group::static_empty() as *const _ as *mut u8) },
-            bucket_mask: 0,
-            items: 0,
-            growth_left: 0,
+            table: RawTableInner::new_in(Global),
+            marker: PhantomData,
+        }
+    }
+
+    /// Attempts to allocate a new hash table with at least enough capacity
+    /// for inserting the given number of elements without reallocating.
+    #[cfg(feature = "raw")]
+    pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
+        Self::try_with_capacity_in(capacity, Global)
+    }
+
+    /// Allocates a new hash table with at least enough capacity for inserting
+    /// the given number of elements without reallocating.
+    pub fn with_capacity(capacity: usize) -> Self {
+        Self::with_capacity_in(capacity, Global)
+    }
+}
+
+impl<T, A: Allocator + Clone> RawTable<T, A> {
+    /// Creates a new empty hash table without allocating any memory, using the
+    /// given allocator.
+    ///
+    /// In effect this returns a table with exactly 1 bucket. However we can
+    /// leave the data pointer dangling since that bucket is never written to
+    /// due to our load factor forcing us to always have at least 1 free bucket.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn new_in(alloc: A) -> Self {
+        Self {
+            table: RawTableInner::new_in(alloc),
             marker: PhantomData,
         }
     }
@@ -398,26 +440,19 @@
     /// The control bytes are left uninitialized.
     #[cfg_attr(feature = "inline-more", inline)]
     unsafe fn new_uninitialized(
+        alloc: A,
         buckets: usize,
-        fallability: Fallibility,
+        fallibility: Fallibility,
     ) -> Result<Self, TryReserveError> {
         debug_assert!(buckets.is_power_of_two());
 
-        // Avoid `Option::ok_or_else` because it bloats LLVM IR.
-        let (layout, ctrl_offset) = match calculate_layout::<T>(buckets) {
-            Some(lco) => lco,
-            None => return Err(fallability.capacity_overflow()),
-        };
-        let ptr = match NonNull::new(alloc(layout)) {
-            Some(ptr) => ptr,
-            None => return Err(fallability.alloc_err(layout)),
-        };
-        let ctrl = NonNull::new_unchecked(ptr.as_ptr().add(ctrl_offset));
         Ok(Self {
-            ctrl,
-            bucket_mask: buckets - 1,
-            items: 0,
-            growth_left: bucket_mask_to_capacity(buckets - 1),
+            table: RawTableInner::new_uninitialized(
+                alloc,
+                TableLayout::new::<T>(),
+                buckets,
+                fallibility,
+            )?,
             marker: PhantomData,
         })
     }
@@ -425,38 +460,33 @@
     /// Attempts to allocate a new hash table with at least enough capacity
     /// for inserting the given number of elements without reallocating.
     fn fallible_with_capacity(
+        alloc: A,
         capacity: usize,
-        fallability: Fallibility,
+        fallibility: Fallibility,
     ) -> Result<Self, TryReserveError> {
-        if capacity == 0 {
-            Ok(Self::new())
-        } else {
-            unsafe {
-                // Avoid `Option::ok_or_else` because it bloats LLVM IR.
-                let buckets = match capacity_to_buckets(capacity) {
-                    Some(buckets) => buckets,
-                    None => return Err(fallability.capacity_overflow()),
-                };
-                let result = Self::new_uninitialized(buckets, fallability)?;
-                result.ctrl(0).write_bytes(EMPTY, result.num_ctrl_bytes());
-
-                Ok(result)
-            }
-        }
+        Ok(Self {
+            table: RawTableInner::fallible_with_capacity(
+                alloc,
+                TableLayout::new::<T>(),
+                capacity,
+                fallibility,
+            )?,
+            marker: PhantomData,
+        })
     }
 
-    /// Attempts to allocate a new hash table with at least enough capacity
-    /// for inserting the given number of elements without reallocating.
+    /// Attempts to allocate a new hash table using the given allocator, with at least enough
+    /// capacity for inserting the given number of elements without reallocating.
     #[cfg(feature = "raw")]
-    pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
-        Self::fallible_with_capacity(capacity, Fallibility::Fallible)
+    pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
+        Self::fallible_with_capacity(alloc, capacity, Fallibility::Fallible)
     }
 
-    /// Allocates a new hash table with at least enough capacity for inserting
-    /// the given number of elements without reallocating.
-    pub fn with_capacity(capacity: usize) -> Self {
+    /// Allocates a new hash table using the given allocator, with at least enough capacity for
+    /// inserting the given number of elements without reallocating.
+    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
         // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
-        match Self::fallible_with_capacity(capacity, Fallibility::Infallible) {
+        match Self::fallible_with_capacity(alloc, capacity, Fallibility::Infallible) {
             Ok(capacity) => capacity,
             Err(_) => unsafe { hint::unreachable_unchecked() },
         }
@@ -465,18 +495,13 @@
     /// Deallocates the table without dropping any entries.
     #[cfg_attr(feature = "inline-more", inline)]
     unsafe fn free_buckets(&mut self) {
-        // Avoid `Option::unwrap_or_else` because it bloats LLVM IR.
-        let (layout, ctrl_offset) = match calculate_layout::<T>(self.buckets()) {
-            Some(lco) => lco,
-            None => hint::unreachable_unchecked(),
-        };
-        dealloc(self.ctrl.as_ptr().sub(ctrl_offset), layout);
+        self.table.free_buckets(TableLayout::new::<T>())
     }
 
     /// Returns pointer to one past last element of data table.
     #[cfg_attr(feature = "inline-more", inline)]
     pub unsafe fn data_end(&self) -> NonNull<T> {
-        NonNull::new_unchecked(self.ctrl.as_ptr() as *mut T)
+        NonNull::new_unchecked(self.table.ctrl.as_ptr().cast())
     }
 
     /// Returns pointer to start of data table.
@@ -492,17 +517,10 @@
         bucket.to_base_index(self.data_end())
     }
 
-    /// Returns a pointer to a control byte.
-    #[cfg_attr(feature = "inline-more", inline)]
-    unsafe fn ctrl(&self, index: usize) -> *mut u8 {
-        debug_assert!(index < self.num_ctrl_bytes());
-        self.ctrl.as_ptr().add(index)
-    }
-
     /// Returns a pointer to an element in the table.
     #[cfg_attr(feature = "inline-more", inline)]
     pub unsafe fn bucket(&self, index: usize) -> Bucket<T> {
-        debug_assert_ne!(self.bucket_mask, 0);
+        debug_assert_ne!(self.table.bucket_mask, 0);
         debug_assert!(index < self.buckets());
         Bucket::from_base_index(self.data_end(), index)
     }
@@ -512,27 +530,7 @@
     #[deprecated(since = "0.8.1", note = "use erase or remove instead")]
     pub unsafe fn erase_no_drop(&mut self, item: &Bucket<T>) {
         let index = self.bucket_index(item);
-        debug_assert!(is_full(*self.ctrl(index)));
-        let index_before = index.wrapping_sub(Group::WIDTH) & self.bucket_mask;
-        let empty_before = Group::load(self.ctrl(index_before)).match_empty();
-        let empty_after = Group::load(self.ctrl(index)).match_empty();
-
-        // If we are inside a continuous block of Group::WIDTH full or deleted
-        // cells then a probe window may have seen a full block when trying to
-        // insert. We therefore need to keep that block non-empty so that
-        // lookups will continue searching to the next probe window.
-        //
-        // Note that in this context `leading_zeros` refers to the bytes at the
-        // end of a group, while `trailing_zeros` refers to the bytes at the
-        // begining of a group.
-        let ctrl = if empty_before.leading_zeros() + empty_after.trailing_zeros() >= Group::WIDTH {
-            DELETED
-        } else {
-            self.growth_left += 1;
-            EMPTY
-        };
-        self.set_ctrl(index, ctrl);
-        self.items -= 1;
+        self.table.erase(index)
     }
 
     /// Erases an element from the table, dropping it in place.
@@ -545,6 +543,20 @@
         item.drop();
     }
 
+    /// Finds and erases an element from the table, dropping it in place.
+    /// Returns true if an element was found.
+    #[cfg(feature = "raw")]
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn erase_entry(&mut self, hash: u64, eq: impl FnMut(&T) -> bool) -> bool {
+        // Avoid `Option::map` because it bloats LLVM IR.
+        if let Some(bucket) = self.find(hash, eq) {
+            unsafe { self.erase(bucket) };
+            true
+        } else {
+            false
+        }
+    }
+
     /// Removes an element from the table, returning it.
     #[cfg_attr(feature = "inline-more", inline)]
     #[allow(clippy::needless_pass_by_value)]
@@ -554,23 +566,775 @@
         item.read()
     }
 
-    /// Returns an iterator for a probe sequence on the table.
+    /// Finds and removes an element from the table, returning it.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn remove_entry(&mut self, hash: u64, eq: impl FnMut(&T) -> bool) -> Option<T> {
+        // Avoid `Option::map` because it bloats LLVM IR.
+        match self.find(hash, eq) {
+            Some(bucket) => Some(unsafe { self.remove(bucket) }),
+            None => None,
+        }
+    }
+
+    /// Marks all table buckets as empty without dropping their contents.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn clear_no_drop(&mut self) {
+        self.table.clear_no_drop()
+    }
+
+    /// Removes all elements from the table without freeing the backing memory.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn clear(&mut self) {
+        // Ensure that the table is reset even if one of the drops panic
+        let mut self_ = guard(self, |self_| self_.clear_no_drop());
+        unsafe {
+            self_.drop_elements();
+        }
+    }
+
+    unsafe fn drop_elements(&mut self) {
+        if mem::needs_drop::<T>() && self.len() != 0 {
+            for item in self.iter() {
+                item.drop();
+            }
+        }
+    }
+
+    /// Shrinks the table to fit `max(self.len(), min_size)` elements.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn shrink_to(&mut self, min_size: usize, hasher: impl Fn(&T) -> u64) {
+        // Calculate the minimal number of elements that we need to reserve
+        // space for.
+        let min_size = usize::max(self.table.items, min_size);
+        if min_size == 0 {
+            *self = Self::new_in(self.table.alloc.clone());
+            return;
+        }
+
+        // Calculate the number of buckets that we need for this number of
+        // elements. If the calculation overflows then the requested bucket
+        // count must be larger than what we have right and nothing needs to be
+        // done.
+        let min_buckets = match capacity_to_buckets(min_size) {
+            Some(buckets) => buckets,
+            None => return,
+        };
+
+        // If we have more buckets than we need, shrink the table.
+        if min_buckets < self.buckets() {
+            // Fast path if the table is empty
+            if self.table.items == 0 {
+                *self = Self::with_capacity_in(min_size, self.table.alloc.clone())
+            } else {
+                // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
+                if self
+                    .resize(min_size, hasher, Fallibility::Infallible)
+                    .is_err()
+                {
+                    unsafe { hint::unreachable_unchecked() }
+                }
+            }
+        }
+    }
+
+    /// Ensures that at least `additional` items can be inserted into the table
+    /// without reallocation.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn reserve(&mut self, additional: usize, hasher: impl Fn(&T) -> u64) {
+        if additional > self.table.growth_left {
+            // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
+            if self
+                .reserve_rehash(additional, hasher, Fallibility::Infallible)
+                .is_err()
+            {
+                unsafe { hint::unreachable_unchecked() }
+            }
+        }
+    }
+
+    /// Tries to ensure that at least `additional` items can be inserted into
+    /// the table without reallocation.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn try_reserve(
+        &mut self,
+        additional: usize,
+        hasher: impl Fn(&T) -> u64,
+    ) -> Result<(), TryReserveError> {
+        if additional > self.table.growth_left {
+            self.reserve_rehash(additional, hasher, Fallibility::Fallible)
+        } else {
+            Ok(())
+        }
+    }
+
+    /// Out-of-line slow path for `reserve` and `try_reserve`.
+    #[cold]
+    #[inline(never)]
+    fn reserve_rehash(
+        &mut self,
+        additional: usize,
+        hasher: impl Fn(&T) -> u64,
+        fallibility: Fallibility,
+    ) -> Result<(), TryReserveError> {
+        // Avoid `Option::ok_or_else` because it bloats LLVM IR.
+        let new_items = match self.table.items.checked_add(additional) {
+            Some(new_items) => new_items,
+            None => return Err(fallibility.capacity_overflow()),
+        };
+        let full_capacity = bucket_mask_to_capacity(self.table.bucket_mask);
+        if new_items <= full_capacity / 2 {
+            // Rehash in-place without re-allocating if we have plenty of spare
+            // capacity that is locked up due to DELETED entries.
+            self.rehash_in_place(hasher);
+            Ok(())
+        } else {
+            // Otherwise, conservatively resize to at least the next size up
+            // to avoid churning deletes into frequent rehashes.
+            self.resize(
+                usize::max(new_items, full_capacity + 1),
+                hasher,
+                fallibility,
+            )
+        }
+    }
+
+    /// Rehashes the contents of the table in place (i.e. without changing the
+    /// allocation).
+    ///
+    /// If `hasher` panics then some the table's contents may be lost.
+    fn rehash_in_place(&mut self, hasher: impl Fn(&T) -> u64) {
+        unsafe {
+            // If the hash function panics then properly clean up any elements
+            // that we haven't rehashed yet. We unfortunately can't preserve the
+            // element since we lost their hash and have no way of recovering it
+            // without risking another panic.
+            self.table.prepare_rehash_in_place();
+
+            let mut guard = guard(&mut self.table, move |self_| {
+                if mem::needs_drop::<T>() {
+                    for i in 0..self_.buckets() {
+                        if *self_.ctrl(i) == DELETED {
+                            self_.set_ctrl(i, EMPTY);
+                            self_.bucket::<T>(i).drop();
+                            self_.items -= 1;
+                        }
+                    }
+                }
+                self_.growth_left = bucket_mask_to_capacity(self_.bucket_mask) - self_.items;
+            });
+
+            // At this point, DELETED elements are elements that we haven't
+            // rehashed yet. Find them and re-insert them at their ideal
+            // position.
+            'outer: for i in 0..guard.buckets() {
+                if *guard.ctrl(i) != DELETED {
+                    continue;
+                }
+
+                'inner: loop {
+                    // Hash the current item
+                    let item = guard.bucket(i);
+                    let hash = hasher(item.as_ref());
+
+                    // Search for a suitable place to put it
+                    let new_i = guard.find_insert_slot(hash);
+
+                    // Probing works by scanning through all of the control
+                    // bytes in groups, which may not be aligned to the group
+                    // size. If both the new and old position fall within the
+                    // same unaligned group, then there is no benefit in moving
+                    // it and we can just continue to the next item.
+                    if likely(guard.is_in_same_group(i, new_i, hash)) {
+                        guard.set_ctrl_h2(i, hash);
+                        continue 'outer;
+                    }
+
+                    // We are moving the current item to a new position. Write
+                    // our H2 to the control byte of the new position.
+                    let prev_ctrl = guard.replace_ctrl_h2(new_i, hash);
+                    if prev_ctrl == EMPTY {
+                        guard.set_ctrl(i, EMPTY);
+                        // If the target slot is empty, simply move the current
+                        // element into the new slot and clear the old control
+                        // byte.
+                        guard.bucket(new_i).copy_from_nonoverlapping(&item);
+                        continue 'outer;
+                    } else {
+                        // If the target slot is occupied, swap the two elements
+                        // and then continue processing the element that we just
+                        // swapped into the old slot.
+                        debug_assert_eq!(prev_ctrl, DELETED);
+                        mem::swap(guard.bucket(new_i).as_mut(), item.as_mut());
+                        continue 'inner;
+                    }
+                }
+            }
+
+            guard.growth_left = bucket_mask_to_capacity(guard.bucket_mask) - guard.items;
+            mem::forget(guard);
+        }
+    }
+
+    /// Allocates a new table of a different size and moves the contents of the
+    /// current table into it.
+    fn resize(
+        &mut self,
+        capacity: usize,
+        hasher: impl Fn(&T) -> u64,
+        fallibility: Fallibility,
+    ) -> Result<(), TryReserveError> {
+        unsafe {
+            let mut new_table =
+                self.table
+                    .prepare_resize(TableLayout::new::<T>(), capacity, fallibility)?;
+
+            // Copy all elements to the new table.
+            for item in self.iter() {
+                // This may panic.
+                let hash = hasher(item.as_ref());
+
+                // We can use a simpler version of insert() here since:
+                // - there are no DELETED entries.
+                // - we know there is enough space in the table.
+                // - all elements are unique.
+                let (index, _) = new_table.prepare_insert_slot(hash);
+                new_table.bucket(index).copy_from_nonoverlapping(&item);
+            }
+
+            // We successfully copied all elements without panicking. Now replace
+            // self with the new table. The old table will have its memory freed but
+            // the items will not be dropped (since they have been moved into the
+            // new table).
+            mem::swap(&mut self.table, &mut new_table);
+
+            Ok(())
+        }
+    }
+
+    /// Inserts a new element into the table, and returns its raw bucket.
+    ///
+    /// This does not check if the given element already exists in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn insert(&mut self, hash: u64, value: T, hasher: impl Fn(&T) -> u64) -> Bucket<T> {
+        unsafe {
+            let mut index = self.table.find_insert_slot(hash);
+
+            // We can avoid growing the table once we have reached our load
+            // factor if we are replacing a tombstone. This works since the
+            // number of EMPTY slots does not change in this case.
+            let old_ctrl = *self.table.ctrl(index);
+            if unlikely(self.table.growth_left == 0 && special_is_empty(old_ctrl)) {
+                self.reserve(1, hasher);
+                index = self.table.find_insert_slot(hash);
+            }
+
+            self.table.record_item_insert_at(index, old_ctrl, hash);
+
+            let bucket = self.bucket(index);
+            bucket.write(value);
+            bucket
+        }
+    }
+
+    /// Attempts to insert a new element without growing the table and return its raw bucket.
+    ///
+    /// Returns an `Err` containing the given element if inserting it would require growing the
+    /// table.
+    ///
+    /// This does not check if the given element already exists in the table.
+    #[cfg(feature = "raw")]
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn try_insert_no_grow(&mut self, hash: u64, value: T) -> Result<Bucket<T>, T> {
+        unsafe {
+            match self.table.prepare_insert_no_grow(hash) {
+                Ok(index) => {
+                    let bucket = self.bucket(index);
+                    bucket.write(value);
+                    Ok(bucket)
+                }
+                Err(()) => Err(value),
+            }
+        }
+    }
+
+    /// Inserts a new element into the table, and returns a mutable reference to it.
+    ///
+    /// This does not check if the given element already exists in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn insert_entry(&mut self, hash: u64, value: T, hasher: impl Fn(&T) -> u64) -> &mut T {
+        unsafe { self.insert(hash, value, hasher).as_mut() }
+    }
+
+    /// Inserts a new element into the table, without growing the table.
+    ///
+    /// There must be enough space in the table to insert the new element.
+    ///
+    /// This does not check if the given element already exists in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    #[cfg(any(feature = "raw", feature = "rustc-internal-api"))]
+    pub fn insert_no_grow(&mut self, hash: u64, value: T) -> Bucket<T> {
+        unsafe {
+            let (index, old_ctrl) = self.table.prepare_insert_slot(hash);
+            let bucket = self.table.bucket(index);
+
+            // If we are replacing a DELETED entry then we don't need to update
+            // the load counter.
+            self.table.growth_left -= special_is_empty(old_ctrl) as usize;
+
+            bucket.write(value);
+            self.table.items += 1;
+            bucket
+        }
+    }
+
+    /// Temporary removes a bucket, applying the given function to the removed
+    /// element and optionally put back the returned value in the same bucket.
+    ///
+    /// Returns `true` if the bucket still contains an element
+    ///
+    /// This does not check if the given bucket is actually occupied.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> bool
+    where
+        F: FnOnce(T) -> Option<T>,
+    {
+        let index = self.bucket_index(&bucket);
+        let old_ctrl = *self.table.ctrl(index);
+        debug_assert!(is_full(old_ctrl));
+        let old_growth_left = self.table.growth_left;
+        let item = self.remove(bucket);
+        if let Some(new_item) = f(item) {
+            self.table.growth_left = old_growth_left;
+            self.table.set_ctrl(index, old_ctrl);
+            self.table.items += 1;
+            self.bucket(index).write(new_item);
+            true
+        } else {
+            false
+        }
+    }
+
+    /// Searches for an element in the table.
+    #[inline]
+    pub fn find(&self, hash: u64, mut eq: impl FnMut(&T) -> bool) -> Option<Bucket<T>> {
+        unsafe {
+            for bucket in self.iter_hash(hash) {
+                let elm = bucket.as_ref();
+                if likely(eq(elm)) {
+                    return Some(bucket);
+                }
+            }
+            None
+        }
+    }
+
+    /// Gets a reference to an element in the table.
+    #[inline]
+    pub fn get(&self, hash: u64, eq: impl FnMut(&T) -> bool) -> Option<&T> {
+        // Avoid `Option::map` because it bloats LLVM IR.
+        match self.find(hash, eq) {
+            Some(bucket) => Some(unsafe { bucket.as_ref() }),
+            None => None,
+        }
+    }
+
+    /// Gets a mutable reference to an element in the table.
+    #[inline]
+    pub fn get_mut(&mut self, hash: u64, eq: impl FnMut(&T) -> bool) -> Option<&mut T> {
+        // Avoid `Option::map` because it bloats LLVM IR.
+        match self.find(hash, eq) {
+            Some(bucket) => Some(unsafe { bucket.as_mut() }),
+            None => None,
+        }
+    }
+
+    /// Attempts to get mutable references to `N` entries in the table at once.
+    ///
+    /// Returns an array of length `N` with the results of each query. For soundness,
+    /// at most one mutable reference will be returned to any entry. An
+    /// `Err(UnavailableMutError::Duplicate(i))` in the returned array indicates that a suitable
+    /// entry exists, but a mutable reference to it already occurs at index `i` in the returned
+    /// array.
+    ///
+    /// The `eq` argument should be a closure such that `eq(i, k)` returns true if `k` is equal to
+    /// the `i`th key to be looked up.
+    ///
+    /// This method is available only if the `nightly` feature is enabled.
+    #[cfg(feature = "nightly")]
+    pub fn get_each_mut<const N: usize>(
+        &mut self,
+        hashes: [u64; N],
+        mut eq: impl FnMut(usize, &T) -> bool,
+    ) -> [Result<&'_ mut T, UnavailableMutError>; N] {
+        // Collect the requested buckets.
+        // TODO use `MaybeUninit::uninit_array` here instead once that's stable.
+        let mut buckets: [MaybeUninit<Option<Bucket<T>>>; N] =
+            unsafe { MaybeUninit::uninit().assume_init() };
+        for i in 0..N {
+            buckets[i] = MaybeUninit::new(self.find(hashes[i], |k| eq(i, k)));
+        }
+        let buckets: [Option<Bucket<T>>; N] = unsafe { MaybeUninit::array_assume_init(buckets) };
+
+        // Walk through the buckets, checking for duplicates and building up the output array.
+        // TODO use `MaybeUninit::uninit_array` here instead once that's stable.
+        let mut out: [MaybeUninit<Result<&'_ mut T, UnavailableMutError>>; N] =
+            unsafe { MaybeUninit::uninit().assume_init() };
+        for i in 0..N {
+            out[i] = MaybeUninit::new(
+                #[allow(clippy::never_loop)]
+                'outer: loop {
+                    for j in 0..i {
+                        match (&buckets[j], &buckets[i]) {
+                            // These two buckets are the same, and we can't safely return a second
+                            // mutable reference to the same entry.
+                            (Some(prev), Some(cur)) if prev.as_ptr() == cur.as_ptr() => {
+                                break 'outer Err(UnavailableMutError::Duplicate(j));
+                            }
+                            _ => {}
+                        }
+                    }
+                    // This bucket is distinct from all previous buckets (or it doesn't exist), so
+                    // we're clear to return the result of the lookup.
+                    break match &buckets[i] {
+                        None => Err(UnavailableMutError::Absent),
+                        Some(bkt) => unsafe { Ok(bkt.as_mut()) },
+                    };
+                },
+            )
+        }
+
+        unsafe { MaybeUninit::array_assume_init(out) }
+    }
+
+    /// Returns the number of elements the map can hold without reallocating.
+    ///
+    /// This number is a lower bound; the table might be able to hold
+    /// more, but is guaranteed to be able to hold at least this many.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn capacity(&self) -> usize {
+        self.table.items + self.table.growth_left
+    }
+
+    /// Returns the number of elements in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn len(&self) -> usize {
+        self.table.items
+    }
+
+    /// Returns the number of buckets in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn buckets(&self) -> usize {
+        self.table.bucket_mask + 1
+    }
+
+    /// Returns an iterator over every element in the table. It is up to
+    /// the caller to ensure that the `RawTable` outlives the `RawIter`.
+    /// Because we cannot make the `next` method unsafe on the `RawIter`
+    /// struct, we have to make the `iter` method unsafe.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub unsafe fn iter(&self) -> RawIter<T> {
+        let data = Bucket::from_base_index(self.data_end(), 0);
+        RawIter {
+            iter: RawIterRange::new(self.table.ctrl.as_ptr(), data, self.table.buckets()),
+            items: self.table.items,
+        }
+    }
+
+    /// Returns an iterator over occupied buckets that could match a given hash.
+    ///
+    /// In rare cases, the iterator may return a bucket with a different hash.
+    ///
+    /// It is up to the caller to ensure that the `RawTable` outlives the
+    /// `RawIterHash`. Because we cannot make the `next` method unsafe on the
+    /// `RawIterHash` struct, we have to make the `iter_hash` method unsafe.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub unsafe fn iter_hash(&self, hash: u64) -> RawIterHash<'_, T, A> {
+        RawIterHash::new(self, hash)
+    }
+
+    /// Returns an iterator which removes all elements from the table without
+    /// freeing the memory.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn drain(&mut self) -> RawDrain<'_, T, A> {
+        unsafe {
+            let iter = self.iter();
+            self.drain_iter_from(iter)
+        }
+    }
+
+    /// Returns an iterator which removes all elements from the table without
+    /// freeing the memory.
+    ///
+    /// Iteration starts at the provided iterator's current location.
+    ///
+    /// It is up to the caller to ensure that the iterator is valid for this
+    /// `RawTable` and covers all items that remain in the table.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub unsafe fn drain_iter_from(&mut self, iter: RawIter<T>) -> RawDrain<'_, T, A> {
+        debug_assert_eq!(iter.len(), self.len());
+        RawDrain {
+            iter,
+            table: ManuallyDrop::new(mem::replace(self, Self::new_in(self.table.alloc.clone()))),
+            orig_table: NonNull::from(self),
+            marker: PhantomData,
+        }
+    }
+
+    /// Returns an iterator which consumes all elements from the table.
+    ///
+    /// Iteration starts at the provided iterator's current location.
+    ///
+    /// It is up to the caller to ensure that the iterator is valid for this
+    /// `RawTable` and covers all items that remain in the table.
+    pub unsafe fn into_iter_from(self, iter: RawIter<T>) -> RawIntoIter<T, A> {
+        debug_assert_eq!(iter.len(), self.len());
+
+        let alloc = self.table.alloc.clone();
+        let allocation = self.into_allocation();
+        RawIntoIter {
+            iter,
+            allocation,
+            marker: PhantomData,
+            alloc,
+        }
+    }
+
+    /// Converts the table into a raw allocation. The contents of the table
+    /// should be dropped using a `RawIter` before freeing the allocation.
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub(crate) fn into_allocation(self) -> Option<(NonNull<u8>, Layout)> {
+        let alloc = if self.table.is_empty_singleton() {
+            None
+        } else {
+            // Avoid `Option::unwrap_or_else` because it bloats LLVM IR.
+            let (layout, ctrl_offset) = match calculate_layout::<T>(self.table.buckets()) {
+                Some(lco) => lco,
+                None => unsafe { hint::unreachable_unchecked() },
+            };
+            Some((
+                unsafe { NonNull::new_unchecked(self.table.ctrl.as_ptr().sub(ctrl_offset)) },
+                layout,
+            ))
+        };
+        mem::forget(self);
+        alloc
+    }
+}
+
+unsafe impl<T, A: Allocator + Clone> Send for RawTable<T, A> where T: Send {}
+unsafe impl<T, A: Allocator + Clone> Sync for RawTable<T, A> where T: Sync {}
+
+impl<A> RawTableInner<A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    const fn new_in(alloc: A) -> Self {
+        Self {
+            // Be careful to cast the entire slice to a raw pointer.
+            ctrl: unsafe { NonNull::new_unchecked(Group::static_empty() as *const _ as *mut u8) },
+            bucket_mask: 0,
+            items: 0,
+            growth_left: 0,
+            alloc,
+        }
+    }
+}
+
+impl<A: Allocator + Clone> RawTableInner<A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    unsafe fn new_uninitialized(
+        alloc: A,
+        table_layout: TableLayout,
+        buckets: usize,
+        fallibility: Fallibility,
+    ) -> Result<Self, TryReserveError> {
+        debug_assert!(buckets.is_power_of_two());
+
+        // Avoid `Option::ok_or_else` because it bloats LLVM IR.
+        let (layout, ctrl_offset) = match table_layout.calculate_layout_for(buckets) {
+            Some(lco) => lco,
+            None => return Err(fallibility.capacity_overflow()),
+        };
+
+        let ptr: NonNull<u8> = match do_alloc(&alloc, layout) {
+            Ok(block) => block.cast(),
+            Err(_) => return Err(fallibility.alloc_err(layout)),
+        };
+
+        let ctrl = NonNull::new_unchecked(ptr.as_ptr().add(ctrl_offset));
+        Ok(Self {
+            ctrl,
+            bucket_mask: buckets - 1,
+            items: 0,
+            growth_left: bucket_mask_to_capacity(buckets - 1),
+            alloc,
+        })
+    }
+
+    #[inline]
+    fn fallible_with_capacity(
+        alloc: A,
+        table_layout: TableLayout,
+        capacity: usize,
+        fallibility: Fallibility,
+    ) -> Result<Self, TryReserveError> {
+        if capacity == 0 {
+            Ok(Self::new_in(alloc))
+        } else {
+            unsafe {
+                let buckets =
+                    capacity_to_buckets(capacity).ok_or_else(|| fallibility.capacity_overflow())?;
+
+                let result = Self::new_uninitialized(alloc, table_layout, buckets, fallibility)?;
+                result.ctrl(0).write_bytes(EMPTY, result.num_ctrl_bytes());
+
+                Ok(result)
+            }
+        }
+    }
+
+    /// Searches for an empty or deleted bucket which is suitable for inserting
+    /// a new element and sets the hash for that slot.
+    ///
+    /// There must be at least 1 empty bucket in the table.
+    #[inline]
+    unsafe fn prepare_insert_slot(&self, hash: u64) -> (usize, u8) {
+        let index = self.find_insert_slot(hash);
+        let old_ctrl = *self.ctrl(index);
+        self.set_ctrl_h2(index, hash);
+        (index, old_ctrl)
+    }
+
+    /// Searches for an empty or deleted bucket which is suitable for inserting
+    /// a new element.
+    ///
+    /// There must be at least 1 empty bucket in the table.
+    #[inline]
+    fn find_insert_slot(&self, hash: u64) -> usize {
+        let mut probe_seq = self.probe_seq(hash);
+        loop {
+            unsafe {
+                let group = Group::load(self.ctrl(probe_seq.pos));
+                if let Some(bit) = group.match_empty_or_deleted().lowest_set_bit() {
+                    let result = (probe_seq.pos + bit) & self.bucket_mask;
+
+                    // In tables smaller than the group width, trailing control
+                    // bytes outside the range of the table are filled with
+                    // EMPTY entries. These will unfortunately trigger a
+                    // match, but once masked may point to a full bucket that
+                    // is already occupied. We detect this situation here and
+                    // perform a second scan starting at the begining of the
+                    // table. This second scan is guaranteed to find an empty
+                    // slot (due to the load factor) before hitting the trailing
+                    // control bytes (containing EMPTY).
+                    if unlikely(is_full(*self.ctrl(result))) {
+                        debug_assert!(self.bucket_mask < Group::WIDTH);
+                        debug_assert_ne!(probe_seq.pos, 0);
+                        return Group::load_aligned(self.ctrl(0))
+                            .match_empty_or_deleted()
+                            .lowest_set_bit_nonzero();
+                    }
+
+                    return result;
+                }
+            }
+            probe_seq.move_next(self.bucket_mask);
+        }
+    }
+
+    #[allow(clippy::mut_mut)]
+    #[inline]
+    unsafe fn prepare_rehash_in_place(&mut self) {
+        // Bulk convert all full control bytes to DELETED, and all DELETED
+        // control bytes to EMPTY. This effectively frees up all buckets
+        // containing a DELETED entry.
+        for i in (0..self.buckets()).step_by(Group::WIDTH) {
+            let group = Group::load_aligned(self.ctrl(i));
+            let group = group.convert_special_to_empty_and_full_to_deleted();
+            group.store_aligned(self.ctrl(i));
+        }
+
+        // Fix up the trailing control bytes. See the comments in set_ctrl
+        // for the handling of tables smaller than the group width.
+        if self.buckets() < Group::WIDTH {
+            self.ctrl(0)
+                .copy_to(self.ctrl(Group::WIDTH), self.buckets());
+        } else {
+            self.ctrl(0)
+                .copy_to(self.ctrl(self.buckets()), Group::WIDTH);
+        }
+    }
+
+    #[cfg_attr(feature = "inline-more", inline)]
+    unsafe fn bucket<T>(&self, index: usize) -> Bucket<T> {
+        debug_assert_ne!(self.bucket_mask, 0);
+        debug_assert!(index < self.buckets());
+        Bucket::from_base_index(self.data_end(), index)
+    }
+
+    #[cfg_attr(feature = "inline-more", inline)]
+    unsafe fn data_end<T>(&self) -> NonNull<T> {
+        NonNull::new_unchecked(self.ctrl.as_ptr().cast())
+    }
+
+    /// Returns an iterator-like object for a probe sequence on the table.
     ///
     /// This iterator never terminates, but is guaranteed to visit each bucket
     /// group exactly once. The loop using `probe_seq` must terminate upon
     /// reaching a group containing an empty bucket.
-    #[cfg_attr(feature = "inline-more", inline)]
+    #[inline]
     fn probe_seq(&self, hash: u64) -> ProbeSeq {
         ProbeSeq {
-            bucket_mask: self.bucket_mask,
             pos: h1(hash) & self.bucket_mask,
             stride: 0,
         }
     }
 
+    /// Returns the index of a bucket for which a value must be inserted if there is enough rooom
+    /// in the table, otherwise returns error
+    #[cfg(feature = "raw")]
+    #[inline]
+    unsafe fn prepare_insert_no_grow(&mut self, hash: u64) -> Result<usize, ()> {
+        let index = self.find_insert_slot(hash);
+        let old_ctrl = *self.ctrl(index);
+        if unlikely(self.growth_left == 0 && special_is_empty(old_ctrl)) {
+            Err(())
+        } else {
+            self.record_item_insert_at(index, old_ctrl, hash);
+            Ok(index)
+        }
+    }
+
+    #[inline]
+    unsafe fn record_item_insert_at(&mut self, index: usize, old_ctrl: u8, hash: u64) {
+        self.growth_left -= special_is_empty(old_ctrl) as usize;
+        self.set_ctrl_h2(index, hash);
+        self.items += 1;
+    }
+
+    #[inline]
+    fn is_in_same_group(&self, i: usize, new_i: usize, hash: u64) -> bool {
+        let probe_seq_pos = self.probe_seq(hash).pos;
+        let probe_index =
+            |pos: usize| (pos.wrapping_sub(probe_seq_pos) & self.bucket_mask) / Group::WIDTH;
+        probe_index(i) == probe_index(new_i)
+    }
+
+    /// Sets a control byte to the hash, and possibly also the replicated control byte at
+    /// the end of the array.
+    #[inline]
+    unsafe fn set_ctrl_h2(&self, index: usize, hash: u64) {
+        self.set_ctrl(index, h2(hash))
+    }
+
+    #[inline]
+    unsafe fn replace_ctrl_h2(&self, index: usize, hash: u64) -> u8 {
+        let prev_ctrl = *self.ctrl(index);
+        self.set_ctrl_h2(index, hash);
+        prev_ctrl
+    }
+
     /// Sets a control byte, and possibly also the replicated control byte at
     /// the end of the array.
-    #[cfg_attr(feature = "inline-more", inline)]
+    #[inline]
     unsafe fn set_ctrl(&self, index: usize, ctrl: u8) {
         // Replicate the first Group::WIDTH control bytes at the end of
         // the array without using a branch:
@@ -596,47 +1360,77 @@
         *self.ctrl(index2) = ctrl;
     }
 
-    /// Searches for an empty or deleted bucket which is suitable for inserting
-    /// a new element.
-    ///
-    /// There must be at least 1 empty bucket in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    fn find_insert_slot(&self, hash: u64) -> usize {
-        for pos in self.probe_seq(hash) {
-            unsafe {
-                let group = Group::load(self.ctrl(pos));
-                if let Some(bit) = group.match_empty_or_deleted().lowest_set_bit() {
-                    let result = (pos + bit) & self.bucket_mask;
+    /// Returns a pointer to a control byte.
+    #[inline]
+    unsafe fn ctrl(&self, index: usize) -> *mut u8 {
+        debug_assert!(index < self.num_ctrl_bytes());
+        self.ctrl.as_ptr().add(index)
+    }
 
-                    // In tables smaller than the group width, trailing control
-                    // bytes outside the range of the table are filled with
-                    // EMPTY entries. These will unfortunately trigger a
-                    // match, but once masked may point to a full bucket that
-                    // is already occupied. We detect this situation here and
-                    // perform a second scan starting at the begining of the
-                    // table. This second scan is guaranteed to find an empty
-                    // slot (due to the load factor) before hitting the trailing
-                    // control bytes (containing EMPTY).
-                    if unlikely(is_full(*self.ctrl(result))) {
-                        debug_assert!(self.bucket_mask < Group::WIDTH);
-                        debug_assert_ne!(pos, 0);
-                        return Group::load_aligned(self.ctrl(0))
-                            .match_empty_or_deleted()
-                            .lowest_set_bit_nonzero();
-                    } else {
-                        return result;
-                    }
-                }
+    #[inline]
+    fn buckets(&self) -> usize {
+        self.bucket_mask + 1
+    }
+
+    #[inline]
+    fn num_ctrl_bytes(&self) -> usize {
+        self.bucket_mask + 1 + Group::WIDTH
+    }
+
+    #[inline]
+    fn is_empty_singleton(&self) -> bool {
+        self.bucket_mask == 0
+    }
+
+    #[allow(clippy::mut_mut)]
+    #[inline]
+    unsafe fn prepare_resize(
+        &self,
+        table_layout: TableLayout,
+        capacity: usize,
+        fallibility: Fallibility,
+    ) -> Result<crate::scopeguard::ScopeGuard<Self, impl FnMut(&mut Self)>, TryReserveError> {
+        debug_assert!(self.items <= capacity);
+
+        // Allocate and initialize the new table.
+        let mut new_table = RawTableInner::fallible_with_capacity(
+            self.alloc.clone(),
+            table_layout,
+            capacity,
+            fallibility,
+        )?;
+        new_table.growth_left -= self.items;
+        new_table.items = self.items;
+
+        // The hash function may panic, in which case we simply free the new
+        // table without dropping any elements that may have been copied into
+        // it.
+        //
+        // This guard is also used to free the old table on success, see
+        // the comment at the bottom of this function.
+        Ok(guard(new_table, move |self_| {
+            if !self_.is_empty_singleton() {
+                self_.free_buckets(table_layout);
             }
-        }
+        }))
+    }
 
-        // probe_seq never returns.
-        unreachable!();
+    #[inline]
+    unsafe fn free_buckets(&mut self, table_layout: TableLayout) {
+        // Avoid `Option::unwrap_or_else` because it bloats LLVM IR.
+        let (layout, ctrl_offset) = match table_layout.calculate_layout_for(self.buckets()) {
+            Some(lco) => lco,
+            None => hint::unreachable_unchecked(),
+        };
+        self.alloc.deallocate(
+            NonNull::new_unchecked(self.ctrl.as_ptr().sub(ctrl_offset)),
+            layout,
+        );
     }
 
     /// Marks all table buckets as empty without dropping their contents.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn clear_no_drop(&mut self) {
+    #[inline]
+    fn clear_no_drop(&mut self) {
         if !self.is_empty_singleton() {
             unsafe {
                 self.ctrl(0).write_bytes(EMPTY, self.num_ctrl_bytes());
@@ -646,505 +1440,45 @@
         self.growth_left = bucket_mask_to_capacity(self.bucket_mask);
     }
 
-    /// Removes all elements from the table without freeing the backing memory.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn clear(&mut self) {
-        // Ensure that the table is reset even if one of the drops panic
-        let self_ = guard(self, |self_| self_.clear_no_drop());
-
-        if mem::needs_drop::<T>() && self_.len() != 0 {
-            unsafe {
-                for item in self_.iter() {
-                    item.drop();
-                }
-            }
-        }
-    }
-
-    /// Shrinks the table to fit `max(self.len(), min_size)` elements.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn shrink_to(&mut self, min_size: usize, hasher: impl Fn(&T) -> u64) {
-        // Calculate the minimal number of elements that we need to reserve
-        // space for.
-        let min_size = usize::max(self.items, min_size);
-        if min_size == 0 {
-            *self = Self::new();
-            return;
-        }
-
-        // Calculate the number of buckets that we need for this number of
-        // elements. If the calculation overflows then the requested bucket
-        // count must be larger than what we have right and nothing needs to be
-        // done.
-        let min_buckets = match capacity_to_buckets(min_size) {
-            Some(buckets) => buckets,
-            None => return,
-        };
-
-        // If we have more buckets than we need, shrink the table.
-        if min_buckets < self.buckets() {
-            // Fast path if the table is empty
-            if self.items == 0 {
-                *self = Self::with_capacity(min_size)
-            } else {
-                // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
-                if self
-                    .resize(min_size, hasher, Fallibility::Infallible)
-                    .is_err()
-                {
-                    unsafe { hint::unreachable_unchecked() }
-                }
-            }
-        }
-    }
-
-    /// Ensures that at least `additional` items can be inserted into the table
-    /// without reallocation.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn reserve(&mut self, additional: usize, hasher: impl Fn(&T) -> u64) {
-        if additional > self.growth_left {
-            // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
-            if self
-                .reserve_rehash(additional, hasher, Fallibility::Infallible)
-                .is_err()
-            {
-                unsafe { hint::unreachable_unchecked() }
-            }
-        }
-    }
-
-    /// Tries to ensure that at least `additional` items can be inserted into
-    /// the table without reallocation.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn try_reserve(
-        &mut self,
-        additional: usize,
-        hasher: impl Fn(&T) -> u64,
-    ) -> Result<(), TryReserveError> {
-        if additional > self.growth_left {
-            self.reserve_rehash(additional, hasher, Fallibility::Fallible)
-        } else {
-            Ok(())
-        }
-    }
-
-    /// Out-of-line slow path for `reserve` and `try_reserve`.
-    #[cold]
-    #[inline(never)]
-    fn reserve_rehash(
-        &mut self,
-        additional: usize,
-        hasher: impl Fn(&T) -> u64,
-        fallability: Fallibility,
-    ) -> Result<(), TryReserveError> {
-        // Avoid `Option::ok_or_else` because it bloats LLVM IR.
-        let new_items = match self.items.checked_add(additional) {
-            Some(new_items) => new_items,
-            None => return Err(fallability.capacity_overflow()),
-        };
-        let full_capacity = bucket_mask_to_capacity(self.bucket_mask);
-        if new_items <= full_capacity / 2 {
-            // Rehash in-place without re-allocating if we have plenty of spare
-            // capacity that is locked up due to DELETED entries.
-            self.rehash_in_place(hasher);
-            Ok(())
-        } else {
-            // Otherwise, conservatively resize to at least the next size up
-            // to avoid churning deletes into frequent rehashes.
-            self.resize(
-                usize::max(new_items, full_capacity + 1),
-                hasher,
-                fallability,
-            )
-        }
-    }
-
-    /// Rehashes the contents of the table in place (i.e. without changing the
-    /// allocation).
-    ///
-    /// If `hasher` panics then some the table's contents may be lost.
-    fn rehash_in_place(&mut self, hasher: impl Fn(&T) -> u64) {
-        unsafe {
-            // Bulk convert all full control bytes to DELETED, and all DELETED
-            // control bytes to EMPTY. This effectively frees up all buckets
-            // containing a DELETED entry.
-            for i in (0..self.buckets()).step_by(Group::WIDTH) {
-                let group = Group::load_aligned(self.ctrl(i));
-                let group = group.convert_special_to_empty_and_full_to_deleted();
-                group.store_aligned(self.ctrl(i));
-            }
-
-            // Fix up the trailing control bytes. See the comments in set_ctrl
-            // for the handling of tables smaller than the group width.
-            if self.buckets() < Group::WIDTH {
-                self.ctrl(0)
-                    .copy_to(self.ctrl(Group::WIDTH), self.buckets());
-            } else {
-                self.ctrl(0)
-                    .copy_to(self.ctrl(self.buckets()), Group::WIDTH);
-            }
-
-            // If the hash function panics then properly clean up any elements
-            // that we haven't rehashed yet. We unfortunately can't preserve the
-            // element since we lost their hash and have no way of recovering it
-            // without risking another panic.
-            let mut guard = guard(self, |self_| {
-                if mem::needs_drop::<T>() {
-                    for i in 0..self_.buckets() {
-                        if *self_.ctrl(i) == DELETED {
-                            self_.set_ctrl(i, EMPTY);
-                            self_.bucket(i).drop();
-                            self_.items -= 1;
-                        }
-                    }
-                }
-                self_.growth_left = bucket_mask_to_capacity(self_.bucket_mask) - self_.items;
-            });
-
-            // At this point, DELETED elements are elements that we haven't
-            // rehashed yet. Find them and re-insert them at their ideal
-            // position.
-            'outer: for i in 0..guard.buckets() {
-                if *guard.ctrl(i) != DELETED {
-                    continue;
-                }
-                'inner: loop {
-                    // Hash the current item
-                    let item = guard.bucket(i);
-                    let hash = hasher(item.as_ref());
-
-                    // Search for a suitable place to put it
-                    let new_i = guard.find_insert_slot(hash);
-
-                    // Probing works by scanning through all of the control
-                    // bytes in groups, which may not be aligned to the group
-                    // size. If both the new and old position fall within the
-                    // same unaligned group, then there is no benefit in moving
-                    // it and we can just continue to the next item.
-                    let probe_index = |pos: usize| {
-                        (pos.wrapping_sub(guard.probe_seq(hash).pos) & guard.bucket_mask)
-                            / Group::WIDTH
-                    };
-                    if likely(probe_index(i) == probe_index(new_i)) {
-                        guard.set_ctrl(i, h2(hash));
-                        continue 'outer;
-                    }
-
-                    // We are moving the current item to a new position. Write
-                    // our H2 to the control byte of the new position.
-                    let prev_ctrl = *guard.ctrl(new_i);
-                    guard.set_ctrl(new_i, h2(hash));
-
-                    if prev_ctrl == EMPTY {
-                        // If the target slot is empty, simply move the current
-                        // element into the new slot and clear the old control
-                        // byte.
-                        guard.set_ctrl(i, EMPTY);
-                        guard.bucket(new_i).copy_from_nonoverlapping(&item);
-                        continue 'outer;
-                    } else {
-                        // If the target slot is occupied, swap the two elements
-                        // and then continue processing the element that we just
-                        // swapped into the old slot.
-                        debug_assert_eq!(prev_ctrl, DELETED);
-                        mem::swap(guard.bucket(new_i).as_mut(), item.as_mut());
-                        continue 'inner;
-                    }
-                }
-            }
-
-            guard.growth_left = bucket_mask_to_capacity(guard.bucket_mask) - guard.items;
-            mem::forget(guard);
-        }
-    }
-
-    /// Allocates a new table of a different size and moves the contents of the
-    /// current table into it.
-    fn resize(
-        &mut self,
-        capacity: usize,
-        hasher: impl Fn(&T) -> u64,
-        fallability: Fallibility,
-    ) -> Result<(), TryReserveError> {
-        unsafe {
-            debug_assert!(self.items <= capacity);
-
-            // Allocate and initialize the new table.
-            let mut new_table = Self::fallible_with_capacity(capacity, fallability)?;
-            new_table.growth_left -= self.items;
-            new_table.items = self.items;
-
-            // The hash function may panic, in which case we simply free the new
-            // table without dropping any elements that may have been copied into
-            // it.
-            //
-            // This guard is also used to free the old table on success, see
-            // the comment at the bottom of this function.
-            let mut new_table = guard(ManuallyDrop::new(new_table), |new_table| {
-                if !new_table.is_empty_singleton() {
-                    new_table.free_buckets();
-                }
-            });
-
-            // Copy all elements to the new table.
-            for item in self.iter() {
-                // This may panic.
-                let hash = hasher(item.as_ref());
-
-                // We can use a simpler version of insert() here since:
-                // - there are no DELETED entries.
-                // - we know there is enough space in the table.
-                // - all elements are unique.
-                let index = new_table.find_insert_slot(hash);
-                new_table.set_ctrl(index, h2(hash));
-                new_table.bucket(index).copy_from_nonoverlapping(&item);
-            }
-
-            // We successfully copied all elements without panicking. Now replace
-            // self with the new table. The old table will have its memory freed but
-            // the items will not be dropped (since they have been moved into the
-            // new table).
-            mem::swap(self, &mut new_table);
-
-            Ok(())
-        }
-    }
-
-    /// Inserts a new element into the table.
-    ///
-    /// This does not check if the given element already exists in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn insert(&mut self, hash: u64, value: T, hasher: impl Fn(&T) -> u64) -> Bucket<T> {
-        unsafe {
-            let mut index = self.find_insert_slot(hash);
-
-            // We can avoid growing the table once we have reached our load
-            // factor if we are replacing a tombstone. This works since the
-            // number of EMPTY slots does not change in this case.
-            let old_ctrl = *self.ctrl(index);
-            if unlikely(self.growth_left == 0 && special_is_empty(old_ctrl)) {
-                self.reserve(1, hasher);
-                index = self.find_insert_slot(hash);
-            }
-
-            let bucket = self.bucket(index);
-            self.growth_left -= special_is_empty(old_ctrl) as usize;
-            self.set_ctrl(index, h2(hash));
-            bucket.write(value);
-            self.items += 1;
-            bucket
-        }
-    }
-
-    /// Inserts a new element into the table, without growing the table.
-    ///
-    /// There must be enough space in the table to insert the new element.
-    ///
-    /// This does not check if the given element already exists in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    #[cfg(any(feature = "raw", feature = "rustc-internal-api"))]
-    pub fn insert_no_grow(&mut self, hash: u64, value: T) -> Bucket<T> {
-        unsafe {
-            let index = self.find_insert_slot(hash);
-            let bucket = self.bucket(index);
-
-            // If we are replacing a DELETED entry then we don't need to update
-            // the load counter.
-            let old_ctrl = *self.ctrl(index);
-            self.growth_left -= special_is_empty(old_ctrl) as usize;
-
-            self.set_ctrl(index, h2(hash));
-            bucket.write(value);
-            self.items += 1;
-            bucket
-        }
-    }
-
-    /// Temporary removes a bucket, applying the given function to the removed
-    /// element and optionally put back the returned value in the same bucket.
-    ///
-    /// Returns `true` if the bucket still contains an element
-    ///
-    /// This does not check if the given bucket is actually occupied.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> bool
-    where
-        F: FnOnce(T) -> Option<T>,
-    {
-        let index = self.bucket_index(&bucket);
-        let old_ctrl = *self.ctrl(index);
-        debug_assert!(is_full(old_ctrl));
-        let old_growth_left = self.growth_left;
-        let item = self.remove(bucket);
-        if let Some(new_item) = f(item) {
-            self.growth_left = old_growth_left;
-            self.set_ctrl(index, old_ctrl);
-            self.items += 1;
-            self.bucket(index).write(new_item);
-            true
-        } else {
-            false
-        }
-    }
-
-    /// Searches for an element in the table.
     #[inline]
-    pub fn find(&self, hash: u64, mut eq: impl FnMut(&T) -> bool) -> Option<Bucket<T>> {
-        unsafe {
-            for bucket in self.iter_hash(hash) {
-                let elm = bucket.as_ref();
-                if likely(eq(elm)) {
-                    return Some(bucket);
-                }
-            }
-            None
-        }
-    }
+    unsafe fn erase(&mut self, index: usize) {
+        debug_assert!(is_full(*self.ctrl(index)));
+        let index_before = index.wrapping_sub(Group::WIDTH) & self.bucket_mask;
+        let empty_before = Group::load(self.ctrl(index_before)).match_empty();
+        let empty_after = Group::load(self.ctrl(index)).match_empty();
 
-    /// Returns the number of elements the map can hold without reallocating.
-    ///
-    /// This number is a lower bound; the table might be able to hold
-    /// more, but is guaranteed to be able to hold at least this many.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn capacity(&self) -> usize {
-        self.items + self.growth_left
-    }
-
-    /// Returns the number of elements in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn len(&self) -> usize {
-        self.items
-    }
-
-    /// Returns the number of buckets in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub fn buckets(&self) -> usize {
-        self.bucket_mask + 1
-    }
-
-    /// Returns the number of control bytes in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    fn num_ctrl_bytes(&self) -> usize {
-        self.bucket_mask + 1 + Group::WIDTH
-    }
-
-    /// Returns whether this table points to the empty singleton with a capacity
-    /// of 0.
-    #[cfg_attr(feature = "inline-more", inline)]
-    fn is_empty_singleton(&self) -> bool {
-        self.bucket_mask == 0
-    }
-
-    /// Returns an iterator over every element in the table. It is up to
-    /// the caller to ensure that the `RawTable` outlives the `RawIter`.
-    /// Because we cannot make the `next` method unsafe on the `RawIter`
-    /// struct, we have to make the `iter` method unsafe.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn iter(&self) -> RawIter<T> {
-        let data = Bucket::from_base_index(self.data_end(), 0);
-        RawIter {
-            iter: RawIterRange::new(self.ctrl.as_ptr(), data, self.buckets()),
-            items: self.items,
-        }
-    }
-
-    /// Returns an iterator over occupied buckets that could match a given hash.
-    ///
-    /// In rare cases, the iterator may return a bucket with a different hash.
-    ///
-    /// It is up to the caller to ensure that the `RawTable` outlives the
-    /// `RawIterHash`. Because we cannot make the `next` method unsafe on the
-    /// `RawIterHash` struct, we have to make the `iter_hash` method unsafe.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn iter_hash(&self, hash: u64) -> RawIterHash<'_, T> {
-        RawIterHash::new(self, hash)
-    }
-
-    /// Returns an iterator which removes all elements from the table without
-    /// freeing the memory.
-    ///
-    /// It is up to the caller to ensure that the `RawTable` outlives the `RawDrain`.
-    /// Because we cannot make the `next` method unsafe on the `RawDrain`,
-    /// we have to make the `drain` method unsafe.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn drain(&mut self) -> RawDrain<'_, T> {
-        let iter = self.iter();
-        self.drain_iter_from(iter)
-    }
-
-    /// Returns an iterator which removes all elements from the table without
-    /// freeing the memory.
-    ///
-    /// It is up to the caller to ensure that the `RawTable` outlives the `RawDrain`.
-    /// Because we cannot make the `next` method unsafe on the `RawDrain`,
-    /// we have to make the `drain` method unsafe.
-    ///
-    /// Iteration starts at the provided iterator's current location.
-    /// You must ensure that the iterator covers all items that remain in the table.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub unsafe fn drain_iter_from(&mut self, iter: RawIter<T>) -> RawDrain<'_, T> {
-        debug_assert_eq!(iter.len(), self.len());
-        RawDrain {
-            iter,
-            table: ManuallyDrop::new(mem::replace(self, Self::new())),
-            orig_table: NonNull::from(self),
-            marker: PhantomData,
-        }
-    }
-
-    /// Returns an iterator which consumes all elements from the table.
-    ///
-    /// It is up to the caller to ensure that the `RawTable` outlives the `RawIntoIter`.
-    /// Because we cannot make the `next` method unsafe on the `RawIntoIter`,
-    /// we have to make the `into_iter_from` method unsafe.
-    ///
-    /// Iteration starts at the provided iterator's current location.
-    /// You must ensure that the iterator covers all items that remain in the table.
-    pub unsafe fn into_iter_from(self, iter: RawIter<T>) -> RawIntoIter<T> {
-        debug_assert_eq!(iter.len(), self.len());
-
-        let alloc = self.into_alloc();
-        RawIntoIter {
-            iter,
-            alloc,
-            marker: PhantomData,
-        }
-    }
-
-    /// Converts the table into a raw allocation. The contents of the table
-    /// should be dropped using a `RawIter` before freeing the allocation.
-    #[cfg_attr(feature = "inline-more", inline)]
-    pub(crate) fn into_alloc(self) -> Option<(NonNull<u8>, Layout)> {
-        let alloc = if self.is_empty_singleton() {
-            None
+        // If we are inside a continuous block of Group::WIDTH full or deleted
+        // cells then a probe window may have seen a full block when trying to
+        // insert. We therefore need to keep that block non-empty so that
+        // lookups will continue searching to the next probe window.
+        //
+        // Note that in this context `leading_zeros` refers to the bytes at the
+        // end of a group, while `trailing_zeros` refers to the bytes at the
+        // begining of a group.
+        let ctrl = if empty_before.leading_zeros() + empty_after.trailing_zeros() >= Group::WIDTH {
+            DELETED
         } else {
-            // Avoid `Option::unwrap_or_else` because it bloats LLVM IR.
-            let (layout, ctrl_offset) = match calculate_layout::<T>(self.buckets()) {
-                Some(lco) => lco,
-                None => unsafe { hint::unreachable_unchecked() },
-            };
-            Some((
-                unsafe { NonNull::new_unchecked(self.ctrl.as_ptr().sub(ctrl_offset)) },
-                layout,
-            ))
+            self.growth_left += 1;
+            EMPTY
         };
-        mem::forget(self);
-        alloc
+        self.set_ctrl(index, ctrl);
+        self.items -= 1;
     }
 }
 
-unsafe impl<T> Send for RawTable<T> where T: Send {}
-unsafe impl<T> Sync for RawTable<T> where T: Sync {}
-
-impl<T: Clone> Clone for RawTable<T> {
+impl<T: Clone, A: Allocator + Clone> Clone for RawTable<T, A> {
     fn clone(&self) -> Self {
-        if self.is_empty_singleton() {
-            Self::new()
+        if self.table.is_empty_singleton() {
+            Self::new_in(self.table.alloc.clone())
         } else {
             unsafe {
                 let mut new_table = ManuallyDrop::new(
                     // Avoid `Result::ok_or_else` because it bloats LLVM IR.
-                    match Self::new_uninitialized(self.buckets(), Fallibility::Infallible) {
+                    match Self::new_uninitialized(
+                        self.table.alloc.clone(),
+                        self.table.buckets(),
+                        Fallibility::Infallible,
+                    ) {
                         Ok(table) => table,
                         Err(_) => hint::unreachable_unchecked(),
                     },
@@ -1162,26 +1496,26 @@
     }
 
     fn clone_from(&mut self, source: &Self) {
-        if source.is_empty_singleton() {
-            *self = Self::new();
+        if source.table.is_empty_singleton() {
+            *self = Self::new_in(self.table.alloc.clone());
         } else {
             unsafe {
                 // First, drop all our elements without clearing the control bytes.
-                if mem::needs_drop::<T>() && self.len() != 0 {
-                    for item in self.iter() {
-                        item.drop();
-                    }
-                }
+                self.drop_elements();
 
                 // If necessary, resize our table to match the source.
                 if self.buckets() != source.buckets() {
                     // Skip our drop by using ptr::write.
-                    if !self.is_empty_singleton() {
+                    if !self.table.is_empty_singleton() {
                         self.free_buckets();
                     }
                     (self as *mut Self).write(
                         // Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
-                        match Self::new_uninitialized(source.buckets(), Fallibility::Infallible) {
+                        match Self::new_uninitialized(
+                            self.table.alloc.clone(),
+                            source.buckets(),
+                            Fallibility::Infallible,
+                        ) {
                             Ok(table) => table,
                             Err(_) => hint::unreachable_unchecked(),
                         },
@@ -1201,7 +1535,7 @@
 trait RawTableClone {
     unsafe fn clone_from_spec(&mut self, source: &Self, on_panic: impl FnMut(&mut Self));
 }
-impl<T: Clone> RawTableClone for RawTable<T> {
+impl<T: Clone, A: Allocator + Clone> RawTableClone for RawTable<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     default_fn! {
         unsafe fn clone_from_spec(&mut self, source: &Self, on_panic: impl FnMut(&mut Self)) {
@@ -1210,29 +1544,31 @@
     }
 }
 #[cfg(feature = "nightly")]
-impl<T: Copy> RawTableClone for RawTable<T> {
+impl<T: Copy, A: Allocator + Clone> RawTableClone for RawTable<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     unsafe fn clone_from_spec(&mut self, source: &Self, _on_panic: impl FnMut(&mut Self)) {
         source
+            .table
             .ctrl(0)
-            .copy_to_nonoverlapping(self.ctrl(0), self.num_ctrl_bytes());
+            .copy_to_nonoverlapping(self.table.ctrl(0), self.table.num_ctrl_bytes());
         source
             .data_start()
-            .copy_to_nonoverlapping(self.data_start(), self.buckets());
+            .copy_to_nonoverlapping(self.data_start(), self.table.buckets());
 
-        self.items = source.items;
-        self.growth_left = source.growth_left;
+        self.table.items = source.table.items;
+        self.table.growth_left = source.table.growth_left;
     }
 }
 
-impl<T: Clone> RawTable<T> {
+impl<T: Clone, A: Allocator + Clone> RawTable<T, A> {
     /// Common code for clone and clone_from. Assumes `self.buckets() == source.buckets()`.
     #[cfg_attr(feature = "inline-more", inline)]
     unsafe fn clone_from_impl(&mut self, source: &Self, mut on_panic: impl FnMut(&mut Self)) {
         // Copy the control bytes unchanged. We do this in a single pass
         source
+            .table
             .ctrl(0)
-            .copy_to_nonoverlapping(self.ctrl(0), self.num_ctrl_bytes());
+            .copy_to_nonoverlapping(self.table.ctrl(0), self.table.num_ctrl_bytes());
 
         // The cloning of elements may panic, in which case we need
         // to make sure we drop only the elements that have been
@@ -1240,7 +1576,7 @@
         let mut guard = guard((0, &mut *self), |(index, self_)| {
             if mem::needs_drop::<T>() && self_.len() != 0 {
                 for i in 0..=*index {
-                    if is_full(*self_.ctrl(i)) {
+                    if is_full(*self_.table.ctrl(i)) {
                         self_.bucket(i).drop();
                     }
                 }
@@ -1264,8 +1600,8 @@
         // Successfully cloned all items, no need to clean up.
         mem::forget(guard);
 
-        self.items = source.items;
-        self.growth_left = source.growth_left;
+        self.table.items = source.table.items;
+        self.table.growth_left = source.table.growth_left;
     }
 
     /// Variant of `clone_from` to use when a hasher is available.
@@ -1275,8 +1611,8 @@
         // elements one by one. We don't do this if we have the same number of
         // buckets as the source since we can just copy the contents directly
         // in that case.
-        if self.buckets() != source.buckets()
-            && bucket_mask_to_capacity(self.bucket_mask) >= source.len()
+        if self.table.buckets() != source.table.buckets()
+            && bucket_mask_to_capacity(self.table.bucket_mask) >= source.len()
         {
             self.clear();
 
@@ -1297,8 +1633,7 @@
                     // - there are no DELETED entries.
                     // - we know there is enough space in the table.
                     // - all elements are unique.
-                    let index = guard_self.find_insert_slot(hash);
-                    guard_self.set_ctrl(index, h2(hash));
+                    let (index, _) = guard_self.table.prepare_insert_slot(hash);
                     guard_self.bucket(index).write(item);
                 }
             }
@@ -1306,53 +1641,52 @@
             // Successfully cloned all items, no need to clean up.
             mem::forget(guard_self);
 
-            self.items = source.items;
-            self.growth_left -= source.items;
+            self.table.items = source.table.items;
+            self.table.growth_left -= source.table.items;
         } else {
             self.clone_from(source);
         }
     }
 }
 
+impl<T, A: Allocator + Clone + Default> Default for RawTable<T, A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    fn default() -> Self {
+        Self::new_in(Default::default())
+    }
+}
+
 #[cfg(feature = "nightly")]
-unsafe impl<#[may_dangle] T> Drop for RawTable<T> {
+unsafe impl<#[may_dangle] T, A: Allocator + Clone> Drop for RawTable<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
-        if !self.is_empty_singleton() {
+        if !self.table.is_empty_singleton() {
             unsafe {
-                if mem::needs_drop::<T>() && self.len() != 0 {
-                    for item in self.iter() {
-                        item.drop();
-                    }
-                }
+                self.drop_elements();
                 self.free_buckets();
             }
         }
     }
 }
 #[cfg(not(feature = "nightly"))]
-impl<T> Drop for RawTable<T> {
+impl<T, A: Allocator + Clone> Drop for RawTable<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
-        if !self.is_empty_singleton() {
+        if !self.table.is_empty_singleton() {
             unsafe {
-                if mem::needs_drop::<T>() && self.len() != 0 {
-                    for item in self.iter() {
-                        item.drop();
-                    }
-                }
+                self.drop_elements();
                 self.free_buckets();
             }
         }
     }
 }
 
-impl<T> IntoIterator for RawTable<T> {
+impl<T, A: Allocator + Clone> IntoIterator for RawTable<T, A> {
     type Item = T;
-    type IntoIter = RawIntoIter<T>;
+    type IntoIter = RawIntoIter<T, A>;
 
     #[cfg_attr(feature = "inline-more", inline)]
-    fn into_iter(self) -> RawIntoIter<T> {
+    fn into_iter(self) -> RawIntoIter<T, A> {
         unsafe {
             let iter = self.iter();
             self.into_iter_from(iter)
@@ -1635,6 +1969,14 @@
             }
         }
     }
+
+    unsafe fn drop_elements(&mut self) {
+        if mem::needs_drop::<T>() && self.len() != 0 {
+            for item in self {
+                item.drop();
+            }
+        }
+    }
 }
 
 impl<T> Clone for RawIter<T> {
@@ -1674,62 +2016,55 @@
 impl<T> FusedIterator for RawIter<T> {}
 
 /// Iterator which consumes a table and returns elements.
-pub struct RawIntoIter<T> {
+pub struct RawIntoIter<T, A: Allocator + Clone = Global> {
     iter: RawIter<T>,
-    alloc: Option<(NonNull<u8>, Layout)>,
+    allocation: Option<(NonNull<u8>, Layout)>,
     marker: PhantomData<T>,
+    alloc: A,
 }
 
-impl<T> RawIntoIter<T> {
+impl<T, A: Allocator + Clone> RawIntoIter<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn iter(&self) -> RawIter<T> {
         self.iter.clone()
     }
 }
 
-unsafe impl<T> Send for RawIntoIter<T> where T: Send {}
-unsafe impl<T> Sync for RawIntoIter<T> where T: Sync {}
+unsafe impl<T, A: Allocator + Clone> Send for RawIntoIter<T, A> where T: Send {}
+unsafe impl<T, A: Allocator + Clone> Sync for RawIntoIter<T, A> where T: Sync {}
 
 #[cfg(feature = "nightly")]
-unsafe impl<#[may_dangle] T> Drop for RawIntoIter<T> {
+unsafe impl<#[may_dangle] T, A: Allocator + Clone> Drop for RawIntoIter<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
         unsafe {
             // Drop all remaining elements
-            if mem::needs_drop::<T>() && self.iter.len() != 0 {
-                while let Some(item) = self.iter.next() {
-                    item.drop();
-                }
-            }
+            self.iter.drop_elements();
 
             // Free the table
-            if let Some((ptr, layout)) = self.alloc {
-                dealloc(ptr.as_ptr(), layout);
+            if let Some((ptr, layout)) = self.allocation {
+                self.alloc.deallocate(ptr, layout);
             }
         }
     }
 }
 #[cfg(not(feature = "nightly"))]
-impl<T> Drop for RawIntoIter<T> {
+impl<T, A: Allocator + Clone> Drop for RawIntoIter<T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
         unsafe {
             // Drop all remaining elements
-            if mem::needs_drop::<T>() && self.iter.len() != 0 {
-                while let Some(item) = self.iter.next() {
-                    item.drop();
-                }
-            }
+            self.iter.drop_elements();
 
             // Free the table
-            if let Some((ptr, layout)) = self.alloc {
-                dealloc(ptr.as_ptr(), layout);
+            if let Some((ptr, layout)) = self.allocation {
+                self.alloc.deallocate(ptr, layout);
             }
         }
     }
 }
 
-impl<T> Iterator for RawIntoIter<T> {
+impl<T, A: Allocator + Clone> Iterator for RawIntoIter<T, A> {
     type Item = T;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1743,44 +2078,40 @@
     }
 }
 
-impl<T> ExactSizeIterator for RawIntoIter<T> {}
-impl<T> FusedIterator for RawIntoIter<T> {}
+impl<T, A: Allocator + Clone> ExactSizeIterator for RawIntoIter<T, A> {}
+impl<T, A: Allocator + Clone> FusedIterator for RawIntoIter<T, A> {}
 
 /// Iterator which consumes elements without freeing the table storage.
-pub struct RawDrain<'a, T> {
+pub struct RawDrain<'a, T, A: Allocator + Clone = Global> {
     iter: RawIter<T>,
 
     // The table is moved into the iterator for the duration of the drain. This
     // ensures that an empty table is left if the drain iterator is leaked
     // without dropping.
-    table: ManuallyDrop<RawTable<T>>,
-    orig_table: NonNull<RawTable<T>>,
+    table: ManuallyDrop<RawTable<T, A>>,
+    orig_table: NonNull<RawTable<T, A>>,
 
     // We don't use a &'a mut RawTable<T> because we want RawDrain to be
     // covariant over T.
-    marker: PhantomData<&'a RawTable<T>>,
+    marker: PhantomData<&'a RawTable<T, A>>,
 }
 
-impl<T> RawDrain<'_, T> {
+impl<T, A: Allocator + Clone> RawDrain<'_, T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     pub fn iter(&self) -> RawIter<T> {
         self.iter.clone()
     }
 }
 
-unsafe impl<T> Send for RawDrain<'_, T> where T: Send {}
-unsafe impl<T> Sync for RawDrain<'_, T> where T: Sync {}
+unsafe impl<T, A: Allocator + Copy> Send for RawDrain<'_, T, A> where T: Send {}
+unsafe impl<T, A: Allocator + Copy> Sync for RawDrain<'_, T, A> where T: Sync {}
 
-impl<T> Drop for RawDrain<'_, T> {
+impl<T, A: Allocator + Clone> Drop for RawDrain<'_, T, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn drop(&mut self) {
         unsafe {
             // Drop all remaining elements. Note that this may panic.
-            if mem::needs_drop::<T>() && self.iter.len() != 0 {
-                while let Some(item) = self.iter.next() {
-                    item.drop();
-                }
-            }
+            self.iter.drop_elements();
 
             // Reset the contents of the table now that all elements have been
             // dropped.
@@ -1794,7 +2125,7 @@
     }
 }
 
-impl<T> Iterator for RawDrain<'_, T> {
+impl<T, A: Allocator + Clone> Iterator for RawDrain<'_, T, A> {
     type Item = T;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1811,14 +2142,19 @@
     }
 }
 
-impl<T> ExactSizeIterator for RawDrain<'_, T> {}
-impl<T> FusedIterator for RawDrain<'_, T> {}
+impl<T, A: Allocator + Clone> ExactSizeIterator for RawDrain<'_, T, A> {}
+impl<T, A: Allocator + Clone> FusedIterator for RawDrain<'_, T, A> {}
 
 /// Iterator over occupied buckets that could match a given hash.
 ///
 /// In rare cases, the iterator may return a bucket with a different hash.
-pub struct RawIterHash<'a, T> {
-    table: &'a RawTable<T>,
+pub struct RawIterHash<'a, T, A: Allocator + Clone = Global> {
+    inner: RawIterHashInner<'a, A>,
+    _marker: PhantomData<T>,
+}
+
+struct RawIterHashInner<'a, A: Allocator + Clone> {
+    table: &'a RawTableInner<A>,
 
     // The top 7 bits of the hash.
     h2_hash: u8,
@@ -1826,28 +2162,34 @@
     // The sequence of groups to probe in the search.
     probe_seq: ProbeSeq,
 
-    // The current group and its position.
-    pos: usize,
     group: Group,
 
     // The elements within the group with a matching h2-hash.
     bitmask: BitMaskIter,
 }
 
-impl<'a, T> RawIterHash<'a, T> {
-    fn new(table: &'a RawTable<T>, hash: u64) -> Self {
+impl<'a, T, A: Allocator + Clone> RawIterHash<'a, T, A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    fn new(table: &'a RawTable<T, A>, hash: u64) -> Self {
+        RawIterHash {
+            inner: RawIterHashInner::new(&table.table, hash),
+            _marker: PhantomData,
+        }
+    }
+}
+impl<'a, A: Allocator + Clone> RawIterHashInner<'a, A> {
+    #[cfg_attr(feature = "inline-more", inline)]
+    fn new(table: &'a RawTableInner<A>, hash: u64) -> Self {
         unsafe {
             let h2_hash = h2(hash);
-            let mut probe_seq = table.probe_seq(hash);
-            let pos = probe_seq.next().unwrap();
-            let group = Group::load(table.ctrl(pos));
+            let probe_seq = table.probe_seq(hash);
+            let group = Group::load(table.ctrl(probe_seq.pos));
             let bitmask = group.match_byte(h2_hash).into_iter();
 
-            RawIterHash {
+            RawIterHashInner {
                 table,
                 h2_hash,
                 probe_seq,
-                pos,
                 group,
                 bitmask,
             }
@@ -1855,24 +2197,66 @@
     }
 }
 
-impl<'a, T> Iterator for RawIterHash<'a, T> {
+impl<'a, T, A: Allocator + Clone> Iterator for RawIterHash<'a, T, A> {
     type Item = Bucket<T>;
 
     fn next(&mut self) -> Option<Bucket<T>> {
         unsafe {
+            match self.inner.next() {
+                Some(index) => Some(self.inner.table.bucket(index)),
+                None => None,
+            }
+        }
+    }
+}
+
+impl<'a, A: Allocator + Clone> Iterator for RawIterHashInner<'a, A> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        unsafe {
             loop {
                 if let Some(bit) = self.bitmask.next() {
-                    let index = (self.pos + bit) & self.table.bucket_mask;
-                    let bucket = self.table.bucket(index);
-                    return Some(bucket);
+                    let index = (self.probe_seq.pos + bit) & self.table.bucket_mask;
+                    return Some(index);
                 }
                 if likely(self.group.match_empty().any_bit_set()) {
                     return None;
                 }
-                self.pos = self.probe_seq.next().unwrap();
-                self.group = Group::load(self.table.ctrl(self.pos));
+                self.probe_seq.move_next(self.table.bucket_mask);
+                self.group = Group::load(self.table.ctrl(self.probe_seq.pos));
                 self.bitmask = self.group.match_byte(self.h2_hash).into_iter();
             }
         }
     }
 }
+
+#[cfg(test)]
+mod test_map {
+    use super::*;
+
+    #[test]
+    fn rehash() {
+        let mut table = RawTable::new();
+        let hasher = |i: &u64| *i;
+        for i in 0..100 {
+            table.insert(i, i, hasher);
+        }
+
+        for i in 0..100 {
+            unsafe {
+                assert_eq!(table.find(i, |x| *x == i).map(|b| b.read()), Some(i));
+            }
+            assert!(table.find(i + 100, |x| *x == i + 100).is_none());
+        }
+
+        table.rehash_in_place(hasher);
+
+        for i in 0..100 {
+            unsafe {
+                assert_eq!(table.find(i, |x| *x == i).map(|b| b.read()), Some(i));
+            }
+            assert!(table.find(i + 100, |x| *x == i + 100).is_none());
+        }
+    }
+}
diff --git a/sgx_tstd/hashbrown/src/raw/sse2.rs b/sgx_tstd/hashbrown/src/raw/sse2.rs
index dbe61d8..eed9684 100644
--- a/sgx_tstd/hashbrown/src/raw/sse2.rs
+++ b/sgx_tstd/hashbrown/src/raw/sse2.rs
@@ -28,6 +28,7 @@
     /// value for an empty hash table.
     ///
     /// This is guaranteed to be aligned to the group size.
+    #[allow(clippy::items_after_statements)]
     pub const fn static_empty() -> &'static [u8; Group::WIDTH] {
         #[repr(C)]
         struct AlignedBytes {
@@ -45,7 +46,7 @@
     #[inline]
     #[allow(clippy::cast_ptr_alignment)] // unaligned load
     pub unsafe fn load(ptr: *const u8) -> Self {
-        Group(x86::_mm_loadu_si128(ptr as *const _))
+        Group(x86::_mm_loadu_si128(ptr.cast()))
     }
 
     /// Loads a group of bytes starting at the given address, which must be
@@ -55,7 +56,7 @@
     pub unsafe fn load_aligned(ptr: *const u8) -> Self {
         // FIXME: use align_offset once it stabilizes
         debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
-        Group(x86::_mm_load_si128(ptr as *const _))
+        Group(x86::_mm_load_si128(ptr.cast()))
     }
 
     /// Stores the group of bytes to the given address, which must be
@@ -65,7 +66,7 @@
     pub unsafe fn store_aligned(self, ptr: *mut u8) {
         // FIXME: use align_offset once it stabilizes
         debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
-        x86::_mm_store_si128(ptr as *mut _, self.0);
+        x86::_mm_store_si128(ptr.cast(), self.0);
     }
 
     /// Returns a `BitMask` indicating all bytes in the group which have
diff --git a/sgx_tstd/hashbrown/src/rustc_entry.rs b/sgx_tstd/hashbrown/src/rustc_entry.rs
index b6ea7bc..1793c4a 100644
--- a/sgx_tstd/hashbrown/src/rustc_entry.rs
+++ b/sgx_tstd/hashbrown/src/rustc_entry.rs
@@ -1,14 +1,15 @@
 use self::RustcEntry::*;
-use crate::map::{make_hash, Drain, HashMap, IntoIter, Iter, IterMut};
-use crate::raw::{Bucket, RawTable};
+use crate::map::{make_insert_hash, Drain, HashMap, IntoIter, Iter, IterMut};
+use crate::raw::{Allocator, Bucket, Global, RawTable};
 use core::fmt::{self, Debug};
 use core::hash::{BuildHasher, Hash};
 use core::mem;
 
-impl<K, V, S> HashMap<K, V, S>
+impl<K, V, S, A> HashMap<K, V, S, A>
 where
     K: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     /// Gets the given key's corresponding entry in the map for in-place manipulation.
     ///
@@ -30,8 +31,8 @@
     /// assert_eq!(letters.get(&'y'), None);
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn rustc_entry(&mut self, key: K) -> RustcEntry<'_, K, V> {
-        let hash = make_hash(&self.hash_builder, &key);
+    pub fn rustc_entry(&mut self, key: K) -> RustcEntry<'_, K, V, A> {
+        let hash = make_insert_hash(&self.hash_builder, &key);
         if let Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) {
             RustcEntry::Occupied(RustcOccupiedEntry {
                 key: Some(key),
@@ -59,15 +60,18 @@
 ///
 /// [`HashMap`]: struct.HashMap.html
 /// [`entry`]: struct.HashMap.html#method.rustc_entry
-pub enum RustcEntry<'a, K, V> {
+pub enum RustcEntry<'a, K, V, A = Global>
+where
+    A: Allocator + Clone,
+{
     /// An occupied entry.
-    Occupied(RustcOccupiedEntry<'a, K, V>),
+    Occupied(RustcOccupiedEntry<'a, K, V, A>),
 
     /// A vacant entry.
-    Vacant(RustcVacantEntry<'a, K, V>),
+    Vacant(RustcVacantEntry<'a, K, V, A>),
 }
 
-impl<K: Debug, V: Debug> Debug for RustcEntry<'_, K, V> {
+impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for RustcEntry<'_, K, V, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
             Vacant(ref v) => f.debug_tuple("Entry").field(v).finish(),
@@ -80,26 +84,31 @@
 /// It is part of the [`RustcEntry`] enum.
 ///
 /// [`RustcEntry`]: enum.RustcEntry.html
-pub struct RustcOccupiedEntry<'a, K, V> {
+pub struct RustcOccupiedEntry<'a, K, V, A = Global>
+where
+    A: Allocator + Clone,
+{
     key: Option<K>,
     elem: Bucket<(K, V)>,
-    table: &'a mut RawTable<(K, V)>,
+    table: &'a mut RawTable<(K, V), A>,
 }
 
-unsafe impl<K, V> Send for RustcOccupiedEntry<'_, K, V>
+unsafe impl<K, V, A> Send for RustcOccupiedEntry<'_, K, V, A>
 where
     K: Send,
     V: Send,
+    A: Allocator + Clone + Send,
 {
 }
-unsafe impl<K, V> Sync for RustcOccupiedEntry<'_, K, V>
+unsafe impl<K, V, A> Sync for RustcOccupiedEntry<'_, K, V, A>
 where
     K: Sync,
     V: Sync,
+    A: Allocator + Clone + Sync,
 {
 }
 
-impl<K: Debug, V: Debug> Debug for RustcOccupiedEntry<'_, K, V> {
+impl<K: Debug, V: Debug, A: Allocator + Clone> Debug for RustcOccupiedEntry<'_, K, V, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("OccupiedEntry")
             .field("key", self.key())
@@ -112,19 +121,22 @@
 /// It is part of the [`RustcEntry`] enum.
 ///
 /// [`RustcEntry`]: enum.RustcEntry.html
-pub struct RustcVacantEntry<'a, K, V> {
+pub struct RustcVacantEntry<'a, K, V, A = Global>
+where
+    A: Allocator + Clone,
+{
     hash: u64,
     key: K,
-    table: &'a mut RawTable<(K, V)>,
+    table: &'a mut RawTable<(K, V), A>,
 }
 
-impl<K: Debug, V> Debug for RustcVacantEntry<'_, K, V> {
+impl<K: Debug, V, A: Allocator + Clone> Debug for RustcVacantEntry<'_, K, V, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_tuple("VacantEntry").field(self.key()).finish()
     }
 }
 
-impl<'a, K, V> RustcEntry<'a, K, V> {
+impl<'a, K, V, A: Allocator + Clone> RustcEntry<'a, K, V, A> {
     /// Sets the value of the entry, and returns a RustcOccupiedEntry.
     ///
     /// # Examples
@@ -137,7 +149,7 @@
     ///
     /// assert_eq!(entry.key(), &"horseyland");
     /// ```
-    pub fn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V> {
+    pub fn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> {
         match self {
             Vacant(entry) => entry.insert_entry(value),
             Occupied(mut entry) => {
@@ -253,7 +265,7 @@
     }
 }
 
-impl<'a, K, V: Default> RustcEntry<'a, K, V> {
+impl<'a, K, V: Default, A: Allocator + Clone> RustcEntry<'a, K, V, A> {
     /// Ensures a value is in the entry by inserting the default value if empty,
     /// and returns a mutable reference to the value in the entry.
     ///
@@ -281,7 +293,7 @@
     }
 }
 
-impl<'a, K, V> RustcOccupiedEntry<'a, K, V> {
+impl<'a, K, V, A: Allocator + Clone> RustcOccupiedEntry<'a, K, V, A> {
     /// Gets a reference to the key in the entry.
     ///
     /// # Examples
@@ -508,7 +520,7 @@
     }
 }
 
-impl<'a, K, V> RustcVacantEntry<'a, K, V> {
+impl<'a, K, V, A: Allocator + Clone> RustcVacantEntry<'a, K, V, A> {
     /// Gets a reference to the key that would be used when inserting a value
     /// through the `RustcVacantEntry`.
     ///
@@ -583,7 +595,7 @@
     /// }
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn insert_entry(self, value: V) -> RustcOccupiedEntry<'a, K, V> {
+    pub fn insert_entry(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> {
         let bucket = self.table.insert_no_grow(self.hash, (self.key, value));
         RustcOccupiedEntry {
             key: None,
diff --git a/sgx_tstd/hashbrown/src/scopeguard.rs b/sgx_tstd/hashbrown/src/scopeguard.rs
index 32c9694..4e9bf04 100644
--- a/sgx_tstd/hashbrown/src/scopeguard.rs
+++ b/sgx_tstd/hashbrown/src/scopeguard.rs
@@ -9,7 +9,7 @@
     value: T,
 }
 
-#[cfg_attr(feature = "inline-more", inline)]
+#[inline]
 pub fn guard<T, F>(value: T, dropfn: F) -> ScopeGuard<T, F>
 where
     F: FnMut(&mut T),
@@ -22,7 +22,7 @@
     F: FnMut(&mut T),
 {
     type Target = T;
-    #[cfg_attr(feature = "inline-more", inline)]
+    #[inline]
     fn deref(&self) -> &T {
         &self.value
     }
@@ -32,7 +32,7 @@
 where
     F: FnMut(&mut T),
 {
-    #[cfg_attr(feature = "inline-more", inline)]
+    #[inline]
     fn deref_mut(&mut self) -> &mut T {
         &mut self.value
     }
@@ -42,7 +42,7 @@
 where
     F: FnMut(&mut T),
 {
-    #[cfg_attr(feature = "inline-more", inline)]
+    #[inline]
     fn drop(&mut self) {
         (self.dropfn)(&mut self.value)
     }
diff --git a/sgx_tstd/hashbrown/src/set.rs b/sgx_tstd/hashbrown/src/set.rs
index b8460fd..d59183b 100644
--- a/sgx_tstd/hashbrown/src/set.rs
+++ b/sgx_tstd/hashbrown/src/set.rs
@@ -8,6 +8,7 @@
 use core::ops::{BitAnd, BitOr, BitXor, Sub};
 
 use super::map::{self, ConsumeAllOnDrop, DefaultHashBuilder, DrainFilterInner, HashMap, Keys};
+use crate::raw::{Allocator, Global};
 
 // Future Optimization (FIXME!)
 // =============================
@@ -71,7 +72,7 @@
 /// ```
 ///
 /// The easiest way to use `HashSet` with a custom type is to derive
-/// [`Eq`] and [`Hash`]. We must also derive [`PartialEq`], this will in the
+/// [`Eq`] and [`Hash`]. We must also derive [`PartialEq`]. This will in the
 /// future be implied by [`Eq`].
 ///
 /// ```
@@ -111,11 +112,11 @@
 /// [`HashMap`]: struct.HashMap.html
 /// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
 /// [`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
-pub struct HashSet<T, S = DefaultHashBuilder> {
-    pub(crate) map: HashMap<T, (), S>,
+pub struct HashSet<T, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
+    pub(crate) map: HashMap<T, (), S, A>,
 }
 
-impl<T: Clone, S: Clone> Clone for HashSet<T, S> {
+impl<T: Clone, S: Clone, A: Allocator + Clone> Clone for HashSet<T, S, A> {
     fn clone(&self) -> Self {
         HashSet {
             map: self.map.clone(),
@@ -167,73 +168,47 @@
     }
 }
 
-impl<T, S> HashSet<T, S> {
-    /// Creates a new empty hash set which will use the given hasher to hash
-    /// keys.
+#[cfg(feature = "ahash")]
+impl<T: Hash + Eq, A: Allocator + Clone> HashSet<T, DefaultHashBuilder, A> {
+    /// Creates an empty `HashSet`.
     ///
-    /// The hash set is also created with the default initial capacity.
-    ///
-    /// Warning: `hasher` is normally randomly generated, and
-    /// is designed to allow `HashSet`s to be resistant to attacks that
-    /// cause many collisions and very poor performance. Setting it
-    /// manually using this function can expose a DoS attack vector.
-    ///
-    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
-    /// the HashMap to be useful, see its documentation for details.
-    ///
+    /// The hash set is initially created with a capacity of 0, so it will not allocate until it
+    /// is first inserted into.
     ///
     /// # Examples
     ///
     /// ```
     /// use hashbrown::HashSet;
-    /// use hashbrown::hash_map::DefaultHashBuilder;
-    ///
-    /// let s = DefaultHashBuilder::default();
-    /// let mut set = HashSet::with_hasher(s);
-    /// set.insert(2);
+    /// let set: HashSet<i32> = HashSet::new();
     /// ```
-    ///
-    /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
     #[cfg_attr(feature = "inline-more", inline)]
-    pub const fn with_hasher(hasher: S) -> Self {
+    pub fn new_in(alloc: A) -> Self {
         Self {
-            map: HashMap::with_hasher(hasher),
+            map: HashMap::new_in(alloc),
         }
     }
 
-    /// Creates an empty `HashSet` with the specified capacity, using
-    /// `hasher` to hash the keys.
+    /// Creates an empty `HashSet` with the specified capacity.
     ///
     /// The hash set will be able to hold at least `capacity` elements without
     /// reallocating. If `capacity` is 0, the hash set will not allocate.
     ///
-    /// Warning: `hasher` is normally randomly generated, and
-    /// is designed to allow `HashSet`s to be resistant to attacks that
-    /// cause many collisions and very poor performance. Setting it
-    /// manually using this function can expose a DoS attack vector.
-    ///
-    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
-    /// the HashMap to be useful, see its documentation for details.
-    ///
     /// # Examples
     ///
     /// ```
     /// use hashbrown::HashSet;
-    /// use hashbrown::hash_map::DefaultHashBuilder;
-    ///
-    /// let s = DefaultHashBuilder::default();
-    /// let mut set = HashSet::with_capacity_and_hasher(10, s);
-    /// set.insert(1);
+    /// let set: HashSet<i32> = HashSet::with_capacity(10);
+    /// assert!(set.capacity() >= 10);
     /// ```
-    ///
-    /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
+    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
         Self {
-            map: HashMap::with_capacity_and_hasher(capacity, hasher),
+            map: HashMap::with_capacity_in(capacity, alloc),
         }
     }
+}
 
+impl<T, S, A: Allocator + Clone> HashSet<T, S, A> {
     /// Returns the number of elements the set can hold without reallocating.
     ///
     /// # Examples
@@ -323,7 +298,7 @@
     /// assert!(set.is_empty());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn drain(&mut self) -> Drain<'_, T> {
+    pub fn drain(&mut self) -> Drain<'_, T, A> {
         Drain {
             iter: self.map.drain(),
         }
@@ -376,7 +351,7 @@
     /// assert_eq!(odds, vec![1, 3, 5, 7]);
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, T, F>
+    pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, T, F, A>
     where
         F: FnMut(&T) -> bool,
     {
@@ -405,6 +380,134 @@
     pub fn clear(&mut self) {
         self.map.clear()
     }
+}
+
+impl<T, S> HashSet<T, S, Global> {
+    /// Creates a new empty hash set which will use the given hasher to hash
+    /// keys.
+    ///
+    /// The hash set is also created with the default initial capacity.
+    ///
+    /// Warning: `hasher` is normally randomly generated, and
+    /// is designed to allow `HashSet`s to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
+    /// the HashMap to be useful, see its documentation for details.
+    ///
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashSet;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut set = HashSet::with_hasher(s);
+    /// set.insert(2);
+    /// ```
+    ///
+    /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub const fn with_hasher(hasher: S) -> Self {
+        Self {
+            map: HashMap::with_hasher(hasher),
+        }
+    }
+
+    /// Creates an empty `HashSet` with the specified capacity, using
+    /// `hasher` to hash the keys.
+    ///
+    /// The hash set will be able to hold at least `capacity` elements without
+    /// reallocating. If `capacity` is 0, the hash set will not allocate.
+    ///
+    /// Warning: `hasher` is normally randomly generated, and
+    /// is designed to allow `HashSet`s to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
+    /// the HashMap to be useful, see its documentation for details.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashSet;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut set = HashSet::with_capacity_and_hasher(10, s);
+    /// set.insert(1);
+    /// ```
+    ///
+    /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
+        Self {
+            map: HashMap::with_capacity_and_hasher(capacity, hasher),
+        }
+    }
+}
+
+impl<T, S, A> HashSet<T, S, A>
+where
+    A: Allocator + Clone,
+{
+    /// Creates a new empty hash set which will use the given hasher to hash
+    /// keys.
+    ///
+    /// The hash set is also created with the default initial capacity.
+    ///
+    /// Warning: `hasher` is normally randomly generated, and
+    /// is designed to allow `HashSet`s to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashSet;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut set = HashSet::with_hasher(s);
+    /// set.insert(2);
+    /// ```
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_hasher_in(hasher: S, alloc: A) -> Self {
+        Self {
+            map: HashMap::with_hasher_in(hasher, alloc),
+        }
+    }
+
+    /// Creates an empty `HashSet` with the specified capacity, using
+    /// `hasher` to hash the keys.
+    ///
+    /// The hash set will be able to hold at least `capacity` elements without
+    /// reallocating. If `capacity` is 0, the hash set will not allocate.
+    ///
+    /// Warning: `hasher` is normally randomly generated, and
+    /// is designed to allow `HashSet`s to be resistant to attacks that
+    /// cause many collisions and very poor performance. Setting it
+    /// manually using this function can expose a DoS attack vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use hashbrown::HashSet;
+    /// use hashbrown::hash_map::DefaultHashBuilder;
+    ///
+    /// let s = DefaultHashBuilder::default();
+    /// let mut set = HashSet::with_capacity_and_hasher(10, s);
+    /// set.insert(1);
+    /// ```
+    #[cfg_attr(feature = "inline-more", inline)]
+    pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self {
+        Self {
+            map: HashMap::with_capacity_and_hasher_in(capacity, hasher, alloc),
+        }
+    }
 
     /// Returns a reference to the set's [`BuildHasher`].
     ///
@@ -426,10 +529,11 @@
     }
 }
 
-impl<T, S> HashSet<T, S>
+impl<T, S, A> HashSet<T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     /// Reserves capacity for at least `additional` more elements to be inserted
     /// in the `HashSet`. The collection may reserve more space to avoid
@@ -544,7 +648,7 @@
     /// assert_eq!(diff, [4].iter().collect());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T, S> {
+    pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T, S, A> {
         Difference {
             iter: self.iter(),
             other,
@@ -573,7 +677,7 @@
     /// assert_eq!(diff1, [1, 4].iter().collect());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T, S> {
+    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T, S, A> {
         SymmetricDifference {
             iter: self.difference(other).chain(other.difference(self)),
         }
@@ -598,7 +702,7 @@
     /// assert_eq!(intersection, [2, 3].iter().collect());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T, S> {
+    pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T, S, A> {
         let (smaller, larger) = if self.len() <= other.len() {
             (self, other)
         } else {
@@ -629,8 +733,10 @@
     /// assert_eq!(union, [1, 2, 3, 4].iter().collect());
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, S> {
-        let (smaller, larger) = if self.len() >= other.len() {
+    pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, S, A> {
+        // We'll iterate one set in full, and only the remaining difference from the other.
+        // Use the smaller set for the difference in order to reduce hash lookups.
+        let (smaller, larger) = if self.len() <= other.len() {
             (self, other)
         } else {
             (other, self)
@@ -967,10 +1073,11 @@
     }
 }
 
-impl<T, S> PartialEq for HashSet<T, S>
+impl<T, S, A> PartialEq for HashSet<T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn eq(&self, other: &Self) -> bool {
         if self.len() != other.len() {
@@ -981,40 +1088,53 @@
     }
 }
 
-impl<T, S> Eq for HashSet<T, S>
+impl<T, S, A> Eq for HashSet<T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<T, S> fmt::Debug for HashSet<T, S>
+impl<T, S, A> fmt::Debug for HashSet<T, S, A>
 where
     T: Eq + Hash + fmt::Debug,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_set().entries(self.iter()).finish()
     }
 }
 
-impl<T, S> FromIterator<T> for HashSet<T, S>
+impl<T, S, A> From<HashMap<T, (), S, A>> for HashSet<T, S, A>
+where
+    A: Allocator + Clone,
+{
+    fn from(map: HashMap<T, (), S, A>) -> Self {
+        Self { map }
+    }
+}
+
+impl<T, S, A> FromIterator<T> for HashSet<T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher + Default,
+    A: Default + Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
-        let mut set = Self::with_hasher(Default::default());
+        let mut set = Self::with_hasher_in(Default::default(), Default::default());
         set.extend(iter);
         set
     }
 }
 
-impl<T, S> Extend<T> for HashSet<T, S>
+impl<T, S, A> Extend<T> for HashSet<T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
@@ -1034,10 +1154,11 @@
     }
 }
 
-impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
+impl<'a, T, S, A> Extend<&'a T> for HashSet<T, S, A>
 where
     T: 'a + Eq + Hash + Copy,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     #[cfg_attr(feature = "inline-more", inline)]
     fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
@@ -1057,9 +1178,10 @@
     }
 }
 
-impl<T, S> Default for HashSet<T, S>
+impl<T, S, A> Default for HashSet<T, S, A>
 where
     S: Default,
+    A: Default + Allocator + Clone,
 {
     /// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher.
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1070,10 +1192,11 @@
     }
 }
 
-impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S>
+impl<T, S, A> BitOr<&HashSet<T, S, A>> for &HashSet<T, S, A>
 where
     T: Eq + Hash + Clone,
     S: BuildHasher + Default,
+    A: Allocator + Clone,
 {
     type Output = HashSet<T, S>;
 
@@ -1097,15 +1220,16 @@
     /// }
     /// assert_eq!(i, expected.len());
     /// ```
-    fn bitor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
+    fn bitor(self, rhs: &HashSet<T, S, A>) -> HashSet<T, S> {
         self.union(rhs).cloned().collect()
     }
 }
 
-impl<T, S> BitAnd<&HashSet<T, S>> for &HashSet<T, S>
+impl<T, S, A> BitAnd<&HashSet<T, S, A>> for &HashSet<T, S, A>
 where
     T: Eq + Hash + Clone,
     S: BuildHasher + Default,
+    A: Allocator + Clone,
 {
     type Output = HashSet<T, S>;
 
@@ -1129,7 +1253,7 @@
     /// }
     /// assert_eq!(i, expected.len());
     /// ```
-    fn bitand(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
+    fn bitand(self, rhs: &HashSet<T, S, A>) -> HashSet<T, S> {
         self.intersection(rhs).cloned().collect()
     }
 }
@@ -1216,8 +1340,8 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`into_iter`]: struct.HashSet.html#method.into_iter
-pub struct IntoIter<K> {
-    iter: map::IntoIter<K, ()>,
+pub struct IntoIter<K, A: Allocator + Clone = Global> {
+    iter: map::IntoIter<K, (), A>,
 }
 
 /// A draining iterator over the items of a `HashSet`.
@@ -1227,8 +1351,8 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`drain`]: struct.HashSet.html#method.drain
-pub struct Drain<'a, K> {
-    iter: map::Drain<'a, K, ()>,
+pub struct Drain<'a, K, A: Allocator + Clone = Global> {
+    iter: map::Drain<'a, K, (), A>,
 }
 
 /// A draining iterator over entries of a `HashSet` which don't satisfy the predicate `f`.
@@ -1238,12 +1362,12 @@
 ///
 /// [`drain_filter`]: struct.HashSet.html#method.drain_filter
 /// [`HashSet`]: struct.HashSet.html
-pub struct DrainFilter<'a, K, F>
+pub struct DrainFilter<'a, K, F, A: Allocator + Clone = Global>
 where
     F: FnMut(&K) -> bool,
 {
     f: F,
-    inner: DrainFilterInner<'a, K, ()>,
+    inner: DrainFilterInner<'a, K, (), A>,
 }
 
 /// A lazy iterator producing elements in the intersection of `HashSet`s.
@@ -1253,11 +1377,11 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`intersection`]: struct.HashSet.html#method.intersection
-pub struct Intersection<'a, T, S> {
+pub struct Intersection<'a, T, S, A: Allocator + Clone = Global> {
     // iterator of the first set
     iter: Iter<'a, T>,
     // the second set
-    other: &'a HashSet<T, S>,
+    other: &'a HashSet<T, S, A>,
 }
 
 /// A lazy iterator producing elements in the difference of `HashSet`s.
@@ -1267,11 +1391,11 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`difference`]: struct.HashSet.html#method.difference
-pub struct Difference<'a, T, S> {
+pub struct Difference<'a, T, S, A: Allocator + Clone = Global> {
     // iterator of the first set
     iter: Iter<'a, T>,
     // the second set
-    other: &'a HashSet<T, S>,
+    other: &'a HashSet<T, S, A>,
 }
 
 /// A lazy iterator producing elements in the symmetric difference of `HashSet`s.
@@ -1281,8 +1405,8 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`symmetric_difference`]: struct.HashSet.html#method.symmetric_difference
-pub struct SymmetricDifference<'a, T, S> {
-    iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
+pub struct SymmetricDifference<'a, T, S, A: Allocator + Clone = Global> {
+    iter: Chain<Difference<'a, T, S, A>, Difference<'a, T, S, A>>,
 }
 
 /// A lazy iterator producing elements in the union of `HashSet`s.
@@ -1292,11 +1416,11 @@
 ///
 /// [`HashSet`]: struct.HashSet.html
 /// [`union`]: struct.HashSet.html#method.union
-pub struct Union<'a, T, S> {
-    iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
+pub struct Union<'a, T, S, A: Allocator + Clone = Global> {
+    iter: Chain<Iter<'a, T>, Difference<'a, T, S, A>>,
 }
 
-impl<'a, T, S> IntoIterator for &'a HashSet<T, S> {
+impl<'a, T, S, A: Allocator + Clone> IntoIterator for &'a HashSet<T, S, A> {
     type Item = &'a T;
     type IntoIter = Iter<'a, T>;
 
@@ -1306,9 +1430,9 @@
     }
 }
 
-impl<T, S> IntoIterator for HashSet<T, S> {
+impl<T, S, A: Allocator + Clone> IntoIterator for HashSet<T, S, A> {
     type Item = T;
-    type IntoIter = IntoIter<T>;
+    type IntoIter = IntoIter<T, A>;
 
     /// Creates a consuming iterator, that is, one that moves each value out
     /// of the set in arbitrary order. The set cannot be used after calling
@@ -1331,7 +1455,7 @@
     /// }
     /// ```
     #[cfg_attr(feature = "inline-more", inline)]
-    fn into_iter(self) -> IntoIter<T> {
+    fn into_iter(self) -> IntoIter<T, A> {
         IntoIter {
             iter: self.map.into_iter(),
         }
@@ -1372,7 +1496,7 @@
     }
 }
 
-impl<K> Iterator for IntoIter<K> {
+impl<K, A: Allocator + Clone> Iterator for IntoIter<K, A> {
     type Item = K;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1388,22 +1512,22 @@
         self.iter.size_hint()
     }
 }
-impl<K> ExactSizeIterator for IntoIter<K> {
+impl<K, A: Allocator + Clone> ExactSizeIterator for IntoIter<K, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn len(&self) -> usize {
         self.iter.len()
     }
 }
-impl<K> FusedIterator for IntoIter<K> {}
+impl<K, A: Allocator + Clone> FusedIterator for IntoIter<K, A> {}
 
-impl<K: fmt::Debug> fmt::Debug for IntoIter<K> {
+impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for IntoIter<K, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         let entries_iter = self.iter.iter().map(|(k, _)| k);
         f.debug_list().entries(entries_iter).finish()
     }
 }
 
-impl<K> Iterator for Drain<'_, K> {
+impl<K, A: Allocator + Clone> Iterator for Drain<'_, K, A> {
     type Item = K;
 
     #[cfg_attr(feature = "inline-more", inline)]
@@ -1419,22 +1543,22 @@
         self.iter.size_hint()
     }
 }
-impl<K> ExactSizeIterator for Drain<'_, K> {
+impl<K, A: Allocator + Clone> ExactSizeIterator for Drain<'_, K, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn len(&self) -> usize {
         self.iter.len()
     }
 }
-impl<K> FusedIterator for Drain<'_, K> {}
+impl<K, A: Allocator + Clone> FusedIterator for Drain<'_, K, A> {}
 
-impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> {
+impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for Drain<'_, K, A> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         let entries_iter = self.iter.iter().map(|(k, _)| k);
         f.debug_list().entries(entries_iter).finish()
     }
 }
 
-impl<'a, K, F> Drop for DrainFilter<'a, K, F>
+impl<'a, K, F, A: Allocator + Clone> Drop for DrainFilter<'a, K, F, A>
 where
     F: FnMut(&K) -> bool,
 {
@@ -1448,7 +1572,7 @@
     }
 }
 
-impl<K, F> Iterator for DrainFilter<'_, K, F>
+impl<K, F, A: Allocator + Clone> Iterator for DrainFilter<'_, K, F, A>
 where
     F: FnMut(&K) -> bool,
 {
@@ -1467,9 +1591,12 @@
     }
 }
 
-impl<K, F> FusedIterator for DrainFilter<'_, K, F> where F: FnMut(&K) -> bool {}
+impl<K, F, A: Allocator + Clone> FusedIterator for DrainFilter<'_, K, F, A> where
+    F: FnMut(&K) -> bool
+{
+}
 
-impl<T, S> Clone for Intersection<'_, T, S> {
+impl<T, S, A: Allocator + Clone> Clone for Intersection<'_, T, S, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
         Intersection {
@@ -1479,10 +1606,11 @@
     }
 }
 
-impl<'a, T, S> Iterator for Intersection<'a, T, S>
+impl<'a, T, S, A> Iterator for Intersection<'a, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     type Item = &'a T;
 
@@ -1503,24 +1631,26 @@
     }
 }
 
-impl<T, S> fmt::Debug for Intersection<'_, T, S>
+impl<T, S, A> fmt::Debug for Intersection<'_, T, S, A>
 where
     T: fmt::Debug + Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
     }
 }
 
-impl<T, S> FusedIterator for Intersection<'_, T, S>
+impl<T, S, A> FusedIterator for Intersection<'_, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<T, S> Clone for Difference<'_, T, S> {
+impl<T, S, A: Allocator + Clone> Clone for Difference<'_, T, S, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
         Difference {
@@ -1530,10 +1660,11 @@
     }
 }
 
-impl<'a, T, S> Iterator for Difference<'a, T, S>
+impl<'a, T, S, A> Iterator for Difference<'a, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     type Item = &'a T;
 
@@ -1554,24 +1685,26 @@
     }
 }
 
-impl<T, S> FusedIterator for Difference<'_, T, S>
+impl<T, S, A> FusedIterator for Difference<'_, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<T, S> fmt::Debug for Difference<'_, T, S>
+impl<T, S, A> fmt::Debug for Difference<'_, T, S, A>
 where
     T: fmt::Debug + Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
     }
 }
 
-impl<T, S> Clone for SymmetricDifference<'_, T, S> {
+impl<T, S, A: Allocator + Clone> Clone for SymmetricDifference<'_, T, S, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
         SymmetricDifference {
@@ -1580,10 +1713,11 @@
     }
 }
 
-impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
+impl<'a, T, S, A> Iterator for SymmetricDifference<'a, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     type Item = &'a T;
 
@@ -1597,24 +1731,26 @@
     }
 }
 
-impl<T, S> FusedIterator for SymmetricDifference<'_, T, S>
+impl<T, S, A> FusedIterator for SymmetricDifference<'_, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<T, S> fmt::Debug for SymmetricDifference<'_, T, S>
+impl<T, S, A> fmt::Debug for SymmetricDifference<'_, T, S, A>
 where
     T: fmt::Debug + Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
     }
 }
 
-impl<T, S> Clone for Union<'_, T, S> {
+impl<T, S, A: Allocator + Clone> Clone for Union<'_, T, S, A> {
     #[cfg_attr(feature = "inline-more", inline)]
     fn clone(&self) -> Self {
         Union {
@@ -1623,27 +1759,30 @@
     }
 }
 
-impl<T, S> FusedIterator for Union<'_, T, S>
+impl<T, S, A> FusedIterator for Union<'_, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
 }
 
-impl<T, S> fmt::Debug for Union<'_, T, S>
+impl<T, S, A> fmt::Debug for Union<'_, T, S, A>
 where
     T: fmt::Debug + Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
     }
 }
 
-impl<'a, T, S> Iterator for Union<'a, T, S>
+impl<'a, T, S, A> Iterator for Union<'a, T, S, A>
 where
     T: Eq + Hash,
     S: BuildHasher,
+    A: Allocator + Clone,
 {
     type Item = &'a T;
 
@@ -1665,30 +1804,34 @@
     fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> {
         v
     }
-    fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> {
+    fn into_iter<'new, A: Allocator + Clone>(
+        v: IntoIter<&'static str, A>,
+    ) -> IntoIter<&'new str, A> {
         v
     }
-    fn difference<'a, 'new>(
-        v: Difference<'a, &'static str, DefaultHashBuilder>,
-    ) -> Difference<'a, &'new str, DefaultHashBuilder> {
+    fn difference<'a, 'new, A: Allocator + Clone>(
+        v: Difference<'a, &'static str, DefaultHashBuilder, A>,
+    ) -> Difference<'a, &'new str, DefaultHashBuilder, A> {
         v
     }
-    fn symmetric_difference<'a, 'new>(
-        v: SymmetricDifference<'a, &'static str, DefaultHashBuilder>,
-    ) -> SymmetricDifference<'a, &'new str, DefaultHashBuilder> {
+    fn symmetric_difference<'a, 'new, A: Allocator + Clone>(
+        v: SymmetricDifference<'a, &'static str, DefaultHashBuilder, A>,
+    ) -> SymmetricDifference<'a, &'new str, DefaultHashBuilder, A> {
         v
     }
-    fn intersection<'a, 'new>(
-        v: Intersection<'a, &'static str, DefaultHashBuilder>,
-    ) -> Intersection<'a, &'new str, DefaultHashBuilder> {
+    fn intersection<'a, 'new, A: Allocator + Clone>(
+        v: Intersection<'a, &'static str, DefaultHashBuilder, A>,
+    ) -> Intersection<'a, &'new str, DefaultHashBuilder, A> {
         v
     }
-    fn union<'a, 'new>(
-        v: Union<'a, &'static str, DefaultHashBuilder>,
-    ) -> Union<'a, &'new str, DefaultHashBuilder> {
+    fn union<'a, 'new, A: Allocator + Clone>(
+        v: Union<'a, &'static str, DefaultHashBuilder, A>,
+    ) -> Union<'a, &'new str, DefaultHashBuilder, A> {
         v
     }
-    fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> {
+    fn drain<'new, A: Allocator + Clone>(
+        d: Drain<'static, &'static str, A>,
+    ) -> Drain<'new, &'new str, A> {
         d
     }
 }
@@ -1905,6 +2048,23 @@
     }
 
     #[test]
+    fn test_from_map() {
+        let mut a = crate::HashMap::new();
+        a.insert(1, ());
+        a.insert(2, ());
+        a.insert(3, ());
+        a.insert(4, ());
+
+        let a: HashSet<_> = a.into();
+
+        assert_eq!(a.len(), 4);
+        assert!(a.contains(&1));
+        assert!(a.contains(&2));
+        assert!(a.contains(&3));
+        assert!(a.contains(&4));
+    }
+
+    #[test]
     fn test_from_iter() {
         let xs = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9];
 
@@ -2116,4 +2276,24 @@
         set.insert(19);
         assert!(set.contains(&19));
     }
+
+    #[test]
+    fn rehash_in_place() {
+        let mut set = HashSet::new();
+
+        for i in 0..224 {
+            set.insert(i);
+        }
+
+        assert_eq!(
+            set.capacity(),
+            224,
+            "The set must be at or close to capacity to trigger a re hashing"
+        );
+
+        for i in 100..1400 {
+            set.remove(&(i - 100));
+            set.insert(i);
+        }
+    }
 }
diff --git a/sgx_tstd/hashbrown/tests/serde.rs b/sgx_tstd/hashbrown/tests/serde.rs
index 570bf70..a642348 100644
--- a/sgx_tstd/hashbrown/tests/serde.rs
+++ b/sgx_tstd/hashbrown/tests/serde.rs
@@ -1,24 +1,24 @@
 #![cfg(feature = "serde")]
 
 use core::hash::BuildHasherDefault;
+use fnv::FnvHasher;
 use hashbrown::{HashMap, HashSet};
-use rustc_hash::FxHasher;
 use serde_test::{assert_tokens, Token};
 
-// We use FxHash for this test because we rely on the ordering
-type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
-type FxHashSet<T> = HashSet<T, BuildHasherDefault<FxHasher>>;
+// We use FnvHash for this test because we rely on the ordering
+type FnvHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FnvHasher>>;
+type FnvHashSet<T> = HashSet<T, BuildHasherDefault<FnvHasher>>;
 
 #[test]
 fn map_serde_tokens_empty() {
-    let map = FxHashMap::<char, u32>::default();
+    let map = FnvHashMap::<char, u32>::default();
 
     assert_tokens(&map, &[Token::Map { len: Some(0) }, Token::MapEnd]);
 }
 
 #[test]
 fn map_serde_tokens() {
-    let mut map = FxHashMap::default();
+    let mut map = FnvHashMap::default();
     map.insert('b', 20);
     map.insert('a', 10);
     map.insert('c', 30);
@@ -29,10 +29,10 @@
             Token::Map { len: Some(3) },
             Token::Char('a'),
             Token::I32(10),
-            Token::Char('b'),
-            Token::I32(20),
             Token::Char('c'),
             Token::I32(30),
+            Token::Char('b'),
+            Token::I32(20),
             Token::MapEnd,
         ],
     );
@@ -40,14 +40,14 @@
 
 #[test]
 fn set_serde_tokens_empty() {
-    let set = FxHashSet::<u32>::default();
+    let set = FnvHashSet::<u32>::default();
 
     assert_tokens(&set, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
 }
 
 #[test]
 fn set_serde_tokens() {
-    let mut set = FxHashSet::default();
+    let mut set = FnvHashSet::default();
     set.insert(20);
     set.insert(10);
     set.insert(30);
@@ -56,9 +56,9 @@
         &set,
         &[
             Token::Seq { len: Some(3) },
+            Token::I32(30),
             Token::I32(20),
             Token::I32(10),
-            Token::I32(30),
             Token::SeqEnd,
         ],
     );
diff --git a/sgx_tstd/src/alloc.rs b/sgx_tstd/src/alloc.rs
index 29a84f0..1e15ff3 100644
--- a/sgx_tstd/src/alloc.rs
+++ b/sgx_tstd/src/alloc.rs
@@ -15,15 +15,65 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Memory allocation APIs
+//! Memory allocation APIs.
 //!
 //! In a given program, the standard library has one “global” memory allocator
 //! that is used for example by `Box<T>` and `Vec<T>`.
 //!
+//! Currently the default global allocator is unspecified. Libraries, however,
+//! like `cdylib`s and `staticlib`s are guaranteed to use the [`System`] by
+//! default.
+//!
+//! # The `#[global_allocator]` attribute
+//!
+//! This attribute allows configuring the choice of global allocator.
+//! You can use this to implement a completely custom global allocator
+//! to route all default allocation requests to a custom object.
+//!
+//! ```rust
+//! use std::alloc::{GlobalAlloc, System, Layout};
+//!
+//! struct MyAllocator;
+//!
+//! unsafe impl GlobalAlloc for MyAllocator {
+//!     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
+//!         System.alloc(layout)
+//!     }
+//!
+//!     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
+//!         System.dealloc(ptr, layout)
+//!     }
+//! }
+//!
+//! #[global_allocator]
+//! static GLOBAL: MyAllocator = MyAllocator;
+//!
+//! fn main() {
+//!     // This `Vec` will allocate memory through `GLOBAL` above
+//!     let mut v = Vec::new();
+//!     v.push(1);
+//! }
+//! ```
+//!
+//! The attribute is used on a `static` item whose type implements the
+//! [`GlobalAlloc`] trait. This type can be provided by an external library:
+//!
+//! ```rust,ignore (demonstrates crates.io usage)
+//! extern crate jemallocator;
+//!
+//! use jemallocator::Jemalloc;
+//!
+//! #[global_allocator]
+//! static GLOBAL: Jemalloc = Jemalloc;
+//!
+//! fn main() {}
+//! ```
+//!
+//! The `#[global_allocator]` can only be used once in a crate
+//! or its recursive dependencies.
 
 use core::sync::atomic::{AtomicPtr, Ordering};
 use core::{mem, ptr};
-use crate::sys_common::util::dumb_print;
 
 #[doc(inline)]
 pub use alloc_crate::alloc::*;
@@ -58,7 +108,7 @@
 }
 
 fn default_alloc_error_hook(layout: Layout) {
-    dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
+    rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
 }
 
 #[doc(hidden)]
@@ -68,7 +118,7 @@
     let hook: fn(Layout) =
         if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } };
     hook(layout);
-    unsafe { crate::sys::abort_internal() }
+    crate::sys::abort_internal()
 }
 
 #[doc(hidden)]
@@ -87,12 +137,16 @@
 
     #[rustc_std_internal_symbol]
     pub unsafe extern "C" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
+        // SAFETY: see the guarantees expected by `Layout::from_size_align` and
+        // `GlobalAlloc::alloc`.
         let layout = Layout::from_size_align_unchecked(size, align);
         System.alloc(layout)
     }
 
     #[rustc_std_internal_symbol]
     pub unsafe extern "C" fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize) {
+        // SAFETY: see the guarantees expected by `Layout::from_size_align` and
+        // `GlobalAlloc::dealloc`.
         System.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
     }
 
@@ -103,12 +157,16 @@
         align: usize,
         new_size: usize,
     ) -> *mut u8 {
+        // SAFETY: see the guarantees expected by `Layout::from_size_align` and
+        // `GlobalAlloc::realloc`.
         let old_layout = Layout::from_size_align_unchecked(old_size, align);
         System.realloc(ptr, old_layout, new_size)
     }
 
     #[rustc_std_internal_symbol]
     pub unsafe extern "C" fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
+        // SAFETY: see the guarantees expected by `Layout::from_size_align` and
+        // `GlobalAlloc::alloc_zeroed`.
         let layout = Layout::from_size_align_unchecked(size, align);
         System.alloc_zeroed(layout)
     }
diff --git a/sgx_tstd/src/backtrace.rs b/sgx_tstd/src/backtrace.rs
index 8c2bb04..8723a72 100644
--- a/sgx_tstd/src/backtrace.rs
+++ b/sgx_tstd/src/backtrace.rs
@@ -52,32 +52,58 @@
 //! `BacktraceStatus` enum as a result of `Backtrace::status`.
 //!
 //! Like above with accuracy platform support is done on a best effort basis.
-//! Sometimes libraries may not be available at runtime or something may go
+//! Sometimes libraries might not be available at runtime or something may go
 //! wrong which would cause a backtrace to not be captured. Please feel free to
 //! report issues with platforms where a backtrace cannot be captured though!
 //!
 
+// NB: A note on resolution of a backtrace:
+//
+// Backtraces primarily happen in two steps, one is where we actually capture
+// the stack backtrace, giving us a list of instruction pointers corresponding
+// to stack frames. Next we take these instruction pointers and, one-by-one,
+// turn them into a human readable name (like `main`).
+//
+// The first phase can be somewhat expensive (walking the stack), especially
+// on MSVC where debug information is consulted to return inline frames each as
+// their own frame. The second phase, however, is almost always extremely
+// expensive (on the order of milliseconds sometimes) when it's consulting debug
+// information.
+//
+// We attempt to amortize this cost as much as possible by delaying resolution
+// of an address to a human readable name for as long as possible. When
+// `Backtrace::create` is called to capture a backtrace it doesn't actually
+// perform any symbol resolution, but rather we lazily resolve symbols only just
+// before they're needed for printing. This way we can make capturing a
+// backtrace and throwing it away much cheaper, but actually printing a
+// backtrace is still basically the same cost.
+//
+// This strategy comes at the cost of some synchronization required inside of a
+// `Backtrace`, but that's a relatively small price to pay relative to capturing
+// a backtrace or actually symbolizing it.
+
 pub use crate::sys_common::backtrace::__rust_begin_short_backtrace;
 pub use crate::sys_common::backtrace::PrintFormat;
 
+use crate::cell::UnsafeCell;
+use crate::ffi::c_void;
+use crate::fmt;
 use crate::enclave;
-use crate::sys::backtrace::{self, BytesOrWideString};
-use crate::path::Path;
 use crate::io;
-use crate::sync::SgxMutex;
+use crate::path::Path;
+use crate::sync::Once;
+use crate::sys::backtrace::{self, BytesOrWideString};
 use crate::sys_common::backtrace::{
     lock,
     output_filename,
+    resolve_frame_unsynchronized,
     rust_backtrace_env,
     RustBacktrace,
     set_enabled,
     SymbolName,
-    resolve_frame_unsynchronized,
 };
 use crate::untrusted::fs;
-use core::ffi::c_void;
-use core::fmt;
-use alloc_crate::vec::Vec;
+use crate::vec::Vec;
 
 /// A captured OS thread stack backtrace.
 ///
@@ -94,9 +120,11 @@
 #[non_exhaustive]
 #[derive(Debug, PartialEq, Eq)]
 pub enum BacktraceStatus {
-    /// Capturing a backtrace is not supported.
+    /// Capturing a backtrace is not supported, likely because it's not
+    /// implemented for the current platform.
     Unsupported,
-    /// Capturing a backtrace has been disabled.
+    /// Capturing a backtrace has been disabled through either the
+    /// `RUST_LIB_BACKTRACE` or `RUST_BACKTRACE` environment variables.
     Disabled,
     /// A backtrace has been captured and the `Backtrace` should print
     /// reasonable information when rendered.
@@ -106,7 +134,7 @@
 enum Inner {
     Unsupported,
     Disabled,
-    Captured(SgxMutex<Capture>),
+    Captured(LazilyResolvedCapture),
 }
 
 struct Capture {
@@ -120,21 +148,22 @@
     _assert::<Backtrace>();
 }
 
-struct BacktraceFrame {
+/// A single frame of a backtrace.
+pub struct BacktraceFrame {
     frame: RawFrame,
     symbols: Vec<BacktraceSymbol>,
 }
 
+#[derive(Debug)]
 enum RawFrame {
     Actual(backtrace::Frame),
-    #[cfg(test)]
-    Fake,
 }
 
 struct BacktraceSymbol {
     name: Option<Vec<u8>>,
     filename: Option<BytesOrWide>,
     lineno: Option<u32>,
+    colno: Option<u32>,
 }
 
 enum BytesOrWide {
@@ -144,12 +173,11 @@
 
 impl fmt::Debug for Backtrace {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let mut capture = match &self.inner {
+        let capture = match &self.inner {
             Inner::Unsupported => return fmt.write_str("<unsupported>"),
             Inner::Disabled => return fmt.write_str("<disabled>"),
-            Inner::Captured(c) => c.lock().unwrap(),
+            Inner::Captured(c) => c.force(),
         };
-        capture.resolve();
 
         let frames = &capture.frames[capture.actual_start..];
 
@@ -169,8 +197,20 @@
     }
 }
 
+impl fmt::Debug for BacktraceFrame {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut dbg = fmt.debug_list();
+        dbg.entries(&self.symbols);
+        dbg.finish()
+    }
+}
+
 impl fmt::Debug for BacktraceSymbol {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        // FIXME: improve formatting: https://github.com/rust-lang/rust/issues/65280
+        // FIXME: Also, include column numbers into the debug format as Display already has them.
+        // Until there are stable per-frame accessors, the format shouldn't be changed:
+        // https://github.com/rust-lang/rust/issues/65280#issuecomment-638966585
         write!(fmt, "{{ ")?;
 
         if let Some(fn_name) = self.name.as_ref().map(|b| SymbolName::new(b)) {
@@ -183,7 +223,7 @@
             write!(fmt, ", file: \"{:?}\"", fname)?;
         }
 
-        if let Some(line) = self.lineno.as_ref() {
+        if let Some(line) = self.lineno {
             write!(fmt, ", line: {:?}", line)?;
         }
 
@@ -206,12 +246,9 @@
 }
 
 impl Backtrace {
-    /// Returns whether backtrace captures are enabled.
+    /// Returns whether backtrace captures are enabled
     fn enabled() -> bool {
-        match rust_backtrace_env() {
-            RustBacktrace::Print(_) => true,
-            _ => false,
-        }
+        matches!(rust_backtrace_env(), RustBacktrace::Print(_))
     }
 
     /// Capture a stack backtrace of the current thread.
@@ -228,8 +265,16 @@
         Backtrace::create(Backtrace::capture as usize)
     }
 
-    /// Forcibly captures a full backtrace.
+    /// Forcibly captures a full backtrace, regardless of environment variable
+    /// configuration.
     ///
+    /// This function behaves the same as `capture` except that it ignores the
+    /// values of the `RUST_BACKTRACE` and `RUST_LIB_BACKTRACE` environment
+    /// variables, always capturing a backtrace.
+    ///
+    /// Note that capturing a backtrace can be an expensive operation on some
+    /// platforms, so this should be used with caution in performance-sensitive
+    /// parts of code.
     #[inline(never)] // want to make sure there's a frame here to remove
     pub fn force_capture() -> Backtrace {
         if !Backtrace::enabled() {
@@ -238,10 +283,17 @@
         Backtrace::create(Backtrace::force_capture as usize)
     }
 
+    /// Forcibly captures a disabled backtrace, regardless of environment
+    /// variable configuration.
+    pub const fn disabled() -> Backtrace {
+        Backtrace { inner: Inner::Disabled }
+    }
+
     // Capture a backtrace which start just before the function addressed by
     // `ip`
     fn create(ip: usize) -> Backtrace {
-        let _lock = lock();
+        // SAFETY: We don't attempt to lock this reentrantly.
+        let _lock = unsafe { lock() };
         let mut frames = Vec::new();
         let mut actual_start = None;
         unsafe {
@@ -263,7 +315,7 @@
         let inner = if frames.is_empty() {
             Inner::Unsupported
         } else {
-            Inner::Captured(SgxMutex::new(Capture {
+            Inner::Captured(LazilyResolvedCapture::new(Capture {
                 actual_start: actual_start.unwrap_or(0),
                 frames,
                 resolved: false,
@@ -285,14 +337,20 @@
     }
 }
 
+impl<'a> Backtrace {
+    /// Returns an iterator over the backtrace frames.
+    pub fn frames(&'a self) -> &'a [BacktraceFrame] {
+        if let Inner::Captured(c) = &self.inner { &c.force().frames } else { &[] }
+    }
+}
+
 impl fmt::Display for Backtrace {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let mut capture = match &self.inner {
+        let capture = match &self.inner {
             Inner::Unsupported => return fmt.write_str("unsupported backtrace"),
             Inner::Disabled => return fmt.write_str("disabled backtrace"),
-            Inner::Captured(c) => c.lock().unwrap(),
+            Inner::Captured(c) => c.force(),
         };
-        capture.resolve();
 
         let full = fmt.alternate();
         let (frames, style) = if full {
@@ -313,12 +371,11 @@
         let mut f = backtrace::BacktraceFmt::new(fmt, style, &mut print_path);
         f.add_context()?;
         for frame in frames {
-            let mut f = f.frame();
             if frame.symbols.is_empty() {
-                f.print_raw(frame.frame.ip(), None, None, None)?;
+                f.frame().print_raw(frame.frame.ip(), None, None, None)?;
             } else {
                 for symbol in frame.symbols.iter() {
-                    f.print_raw(
+                    f.frame().print_raw_with_column(
                         frame.frame.ip(),
                         symbol.name.as_ref().map(|b| SymbolName::new(b)),
                         symbol.filename.as_ref().map(|b| match b {
@@ -326,6 +383,7 @@
                             BytesOrWide::Wide(w) => BytesOrWideString::Wide(w),
                         }),
                         symbol.lineno,
+                        symbol.colno,
                     )?;
                 }
             }
@@ -335,6 +393,33 @@
     }
 }
 
+struct LazilyResolvedCapture {
+    sync: Once,
+    capture: UnsafeCell<Capture>,
+}
+
+impl LazilyResolvedCapture {
+    fn new(capture: Capture) -> Self {
+        LazilyResolvedCapture { sync: Once::new(), capture: UnsafeCell::new(capture) }
+    }
+
+    fn force(&self) -> &Capture {
+        self.sync.call_once(|| {
+            // SAFETY: This exclusive reference can't overlap with any others
+            // `Once` guarantees callers will block until this closure returns
+            // `Once` also guarantees only a single caller will enter this closure
+            unsafe { &mut *self.capture.get() }.resolve();
+        });
+
+        // SAFETY: This shared reference can't overlap with the exclusive reference above
+        unsafe { &*self.capture.get() }
+    }
+}
+
+// SAFETY: Access to the inner value is synchronized using a thread-safe `Once`
+// So long as `Capture` is `Sync`, `LazilyResolvedCapture` is too
+unsafe impl Sync for LazilyResolvedCapture where Capture: Sync {}
+
 impl Capture {
     fn resolve(&mut self) {
         // If we're already resolved, nothing to do!
@@ -346,14 +431,11 @@
         // Use the global backtrace lock to synchronize this as it's a
         // requirement of the `backtrace` crate, and then actually resolve
         // everything.
-        let _lock = lock();
+        // SAFETY: We don't attempt to lock this reentrantly.
+        let _lock = unsafe { lock() };
         for frame in self.frames.iter_mut() {
             let symbols = &mut frame.symbols;
-            let frame = match &frame.frame {
-                RawFrame::Actual(frame) => frame,
-                #[cfg(test)]
-                RawFrame::Fake => unimplemented!(),
-            };
+            let RawFrame::Actual(frame) = &frame.frame;
             unsafe {
                 resolve_frame_unsynchronized(frame, |symbol| {
                     symbols.push(BacktraceSymbol {
@@ -363,6 +445,7 @@
                             BytesOrWideString::Wide(b) => BytesOrWide::Wide(b.to_owned()),
                         }),
                         lineno: symbol.lineno(),
+                        colno: symbol.colno(),
                     });
                 });
             }
diff --git a/sgx_tstd/src/collections/hash/map.rs b/sgx_tstd/src/collections/hash/map.rs
index d74ccf3..392a349 100644
--- a/sgx_tstd/src/collections/hash/map.rs
+++ b/sgx_tstd/src/collections/hash/map.rs
@@ -1,5 +1,19 @@
-//#[cfg(test)]
-//mod tests;
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
 
 use self::Entry::*;
 
@@ -8,6 +22,7 @@
 use crate::borrow::Borrow;
 use crate::cell::Cell;
 use crate::collections::TryReserveError;
+use crate::collections::TryReserveErrorKind;
 use crate::fmt::{self, Debug};
 #[allow(deprecated)]
 use crate::hash::{BuildHasher, Hash, Hasher, SipHasher13};
@@ -15,7 +30,7 @@
 use crate::ops::Index;
 use crate::sys;
 
-/// A hash map implemented with quadratic probing and SIMD lookup.
+/// A [hash map] implemented with quadratic probing and SIMD lookup.
 ///
 /// By default, `HashMap` uses a hashing algorithm selected to provide
 /// resistance against HashDoS attacks. The algorithm is randomly seeded, and a
@@ -34,8 +49,8 @@
 /// attacks such as HashDoS.
 ///
 /// The hashing algorithm can be replaced on a per-`HashMap` basis using the
-/// [`default`], [`with_hasher`], and [`with_capacity_and_hasher`] methods. Many
-/// alternative algorithms are available on crates.io, such as the [`fnv`] crate.
+/// [`default`], [`with_hasher`], and [`with_capacity_and_hasher`] methods.
+/// There are many alternative [hashing algorithms available on crates.io].
 ///
 /// It is required that the keys implement the [`Eq`] and [`Hash`] traits, although
 /// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
@@ -52,11 +67,16 @@
 /// hash, as determined by the [`Hash`] trait, or its equality, as determined by
 /// the [`Eq`] trait, changes while it is in the map. This is normally only
 /// possible through [`Cell`], [`RefCell`], global state, I/O, or unsafe code.
+/// The behavior resulting from such a logic error is not specified, but will
+/// not result in undefined behavior. This could include panics, incorrect results,
+/// aborts, memory leaks, and non-termination.
 ///
 /// The hash table implementation is a Rust port of Google's [SwissTable].
 /// The original C++ version of SwissTable can be found [here], and this
 /// [CppCon talk] gives an overview of how the algorithm works.
 ///
+/// [hash map]: crate::collections#use-a-hashmap-when
+/// [hashing algorithms available on crates.io]: https://crates.io/keywords/hasher
 /// [SwissTable]: https://abseil.io/blog/20180927-swisstables
 /// [here]: https://github.com/abseil/abseil-cpp/blob/master/absl/container/internal/raw_hash_set.h
 /// [CppCon talk]: https://www.youtube.com/watch?v=ncHmEUmJZf4
@@ -117,8 +137,21 @@
 /// }
 /// ```
 ///
-/// `HashMap` also implements an [`Entry API`](#method.entry), which allows
-/// for more complex methods of getting, setting, updating and removing keys and
+/// A `HashMap` with a known list of items can be initialized from an array:
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let solar_distance = HashMap::from([
+///     ("Mercury", 0.4),
+///     ("Venus", 0.7),
+///     ("Earth", 1.0),
+///     ("Mars", 1.5),
+/// ]);
+/// ```
+///
+/// `HashMap` implements an [`Entry API`](#method.entry), which allows
+/// for complex methods of getting, setting, updating and removing keys and
 /// their values:
 ///
 /// ```
@@ -154,7 +187,6 @@
 /// [`default`]: Default::default
 /// [`with_hasher`]: Self::with_hasher
 /// [`with_capacity_and_hasher`]: Self::with_capacity_and_hasher
-/// [`fnv`]: https://crates.io/crates/fnv
 ///
 /// ```
 /// use std::collections::HashMap;
@@ -173,31 +205,19 @@
 /// }
 ///
 /// // Use a HashMap to store the vikings' health points.
-/// let mut vikings = HashMap::new();
-///
-/// vikings.insert(Viking::new("Einar", "Norway"), 25);
-/// vikings.insert(Viking::new("Olaf", "Denmark"), 24);
-/// vikings.insert(Viking::new("Harald", "Iceland"), 12);
+/// let vikings = HashMap::from([
+///     (Viking::new("Einar", "Norway"), 25),
+///     (Viking::new("Olaf", "Denmark"), 24),
+///     (Viking::new("Harald", "Iceland"), 12),
+/// ]);
 ///
 /// // Use derived implementation to print the status of the vikings.
 /// for (viking, health) in &vikings {
 ///     println!("{:?} has {} hp", viking, health);
 /// }
 /// ```
-///
-/// A `HashMap` with fixed list of elements can be initialized from an array:
-///
-/// ```
-/// use std::collections::HashMap;
-///
-/// let timber_resources: HashMap<&str, i32> = [("Norway", 100), ("Denmark", 50), ("Iceland", 10)]
-///     .iter().cloned().collect();
-/// // use the values stored in map
-/// ```
 
-#[derive(Clone)]
 #[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_type")]
-//#[stable(feature = "rust1", since = "1.0.0")]
 pub struct HashMap<K, V, S = RandomState> {
     base: base::HashMap<K, V, S>,
 }
@@ -215,7 +235,6 @@
     /// let mut map: HashMap<&str, i32> = HashMap::new();
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn new() -> HashMap<K, V, RandomState> {
         Default::default()
     }
@@ -232,7 +251,6 @@
     /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
         HashMap::with_capacity_and_hasher(capacity, Default::default())
     }
@@ -263,7 +281,6 @@
     /// map.insert(1, 2);
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
     pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
         HashMap { base: base::HashMap::with_hasher(hash_builder) }
     }
@@ -293,7 +310,6 @@
     /// map.insert(1, 2);
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
     pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
         HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hash_builder) }
     }
@@ -311,7 +327,6 @@
     /// assert!(map.capacity() >= 100);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn capacity(&self) -> usize {
         self.base.capacity()
     }
@@ -333,7 +348,6 @@
     ///     println!("{}", key);
     /// }
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn keys(&self) -> Keys<'_, K, V> {
         Keys { inner: self.iter() }
     }
@@ -355,7 +369,6 @@
     ///     println!("{}", val);
     /// }
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn values(&self) -> Values<'_, K, V> {
         Values { inner: self.iter() }
     }
@@ -382,7 +395,6 @@
     ///     println!("{}", val);
     /// }
     /// ```
-    //#[stable(feature = "map_values_mut", since = "1.10.0")]
     pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
         ValuesMut { inner: self.iter_mut() }
     }
@@ -404,7 +416,6 @@
     ///     println!("key: {} val: {}", key, val);
     /// }
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn iter(&self) -> Iter<'_, K, V> {
         Iter { base: self.base.iter() }
     }
@@ -432,7 +443,6 @@
     ///     println!("key: {} val: {}", key, val);
     /// }
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
         IterMut { base: self.base.iter_mut() }
     }
@@ -449,7 +459,7 @@
     /// a.insert(1, "a");
     /// assert_eq!(a.len(), 1);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
+    #[doc(alias = "length")]
     pub fn len(&self) -> usize {
         self.base.len()
     }
@@ -467,7 +477,6 @@
     /// assert!(!a.is_empty());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn is_empty(&self) -> bool {
         self.base.is_empty()
     }
@@ -492,7 +501,6 @@
     /// assert!(a.is_empty());
     /// ```
     #[inline]
-    //#[stable(feature = "drain", since = "1.6.0")]
     pub fn drain(&mut self) -> Drain<'_, K, V> {
         Drain { base: self.base.drain() }
     }
@@ -533,7 +541,6 @@
     /// assert_eq!(odds, vec![1, 3, 5, 7]);
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_drain_filter", issue = "59618")]
     pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, K, V, F>
     where
         F: FnMut(&K, &mut V) -> bool,
@@ -554,7 +561,6 @@
     /// a.clear();
     /// assert!(a.is_empty());
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn clear(&mut self) {
         self.base.clear();
@@ -573,7 +579,6 @@
     /// let hasher: &RandomState = map.hasher();
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_public_hasher", since = "1.9.0")]
     pub fn hasher(&self) -> &S {
         self.base.hasher()
     }
@@ -600,13 +605,12 @@
     /// map.reserve(10);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn reserve(&mut self, additional: usize) {
         self.base.reserve(additional)
     }
 
     /// Tries to reserve capacity for at least `additional` more elements to be inserted
-    /// in the given `HashMap<K,V>`. The collection may reserve more space to avoid
+    /// in the given `HashMap<K, V>`. The collection may reserve more space to avoid
     /// frequent reallocations.
     ///
     /// # Errors
@@ -619,11 +623,11 @@
     /// ```
     /// #![feature(try_reserve)]
     /// use std::collections::HashMap;
+    ///
     /// let mut map: HashMap<&str, isize> = HashMap::new();
     /// map.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
     /// ```
     #[inline]
-    //#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
         self.base.try_reserve(additional).map_err(map_try_reserve_error)
     }
@@ -645,7 +649,6 @@
     /// assert!(map.capacity() >= 2);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn shrink_to_fit(&mut self) {
         self.base.shrink_to_fit();
     }
@@ -654,13 +657,11 @@
     /// down no lower than the supplied limit while maintaining the internal rules
     /// and possibly leaving some space in accordance with the resize policy.
     ///
-    /// Panics if the current capacity is smaller than the supplied
-    /// minimum capacity.
+    /// If the current capacity is less than the lower limit, this is a no-op.
     ///
     /// # Examples
     ///
     /// ```
-    /// #![feature(shrink_to)]
     /// use std::collections::HashMap;
     ///
     /// let mut map: HashMap<i32, i32> = HashMap::with_capacity(100);
@@ -673,9 +674,7 @@
     /// assert!(map.capacity() >= 2);
     /// ```
     #[inline]
-    //#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
     pub fn shrink_to(&mut self, min_capacity: usize) {
-        assert!(self.capacity() >= min_capacity, "Tried to shrink to a larger capacity");
         self.base.shrink_to(min_capacity);
     }
 
@@ -699,7 +698,6 @@
     /// assert_eq!(letters.get(&'y'), None);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
         map_entry(self.base.rustc_entry(key))
     }
@@ -720,7 +718,6 @@
     /// assert_eq!(map.get(&1), Some(&"a"));
     /// assert_eq!(map.get(&2), None);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
     where
@@ -746,7 +743,6 @@
     /// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
     /// assert_eq!(map.get_key_value(&2), None);
     /// ```
-    //#[stable(feature = "map_get_key_value", since = "1.40.0")]
     #[inline]
     pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
     where
@@ -772,7 +768,6 @@
     /// assert_eq!(map.contains_key(&1), true);
     /// assert_eq!(map.contains_key(&2), false);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
     where
@@ -800,7 +795,6 @@
     /// }
     /// assert_eq!(map[&1], "b");
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
     where
@@ -834,12 +828,41 @@
     /// assert_eq!(map.insert(37, "c"), Some("b"));
     /// assert_eq!(map[&37], "c");
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn insert(&mut self, k: K, v: V) -> Option<V> {
         self.base.insert(k, v)
     }
 
+    /// Tries to insert a key-value pair into the map, and returns
+    /// a mutable reference to the value in the entry.
+    ///
+    /// If the map already had this key present, nothing is updated, and
+    /// an error containing the occupied entry and the value is returned.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// #![feature(map_try_insert)]
+    ///
+    /// use std::collections::HashMap;
+    ///
+    /// let mut map = HashMap::new();
+    /// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
+    ///
+    /// let err = map.try_insert(37, "b").unwrap_err();
+    /// assert_eq!(err.entry.key(), &37);
+    /// assert_eq!(err.entry.get(), &"a");
+    /// assert_eq!(err.value, "b");
+    /// ```
+    pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, OccupiedError<'_, K, V>> {
+        match self.entry(key) {
+            Occupied(entry) => Err(OccupiedError { entry, value }),
+            Vacant(entry) => Ok(entry.insert(value)),
+        }
+    }
+
     /// Removes a key from the map, returning the value at the key if the key
     /// was previously in the map.
     ///
@@ -857,7 +880,6 @@
     /// assert_eq!(map.remove(&1), Some("a"));
     /// assert_eq!(map.remove(&1), None);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
     where
@@ -886,7 +908,6 @@
     /// assert_eq!(map.remove(&1), None);
     /// # }
     /// ```
-    //#[stable(feature = "hash_map_remove_entry", since = "1.27.0")]
     #[inline]
     pub fn remove_entry<Q: ?Sized>(&mut self, k: &Q) -> Option<(K, V)>
     where
@@ -898,18 +919,18 @@
 
     /// Retains only the elements specified by the predicate.
     ///
-    /// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
+    /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
+    /// The elements are visited in unsorted (and unspecified) order.
     ///
     /// # Examples
     ///
     /// ```
     /// use std::collections::HashMap;
     ///
-    /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
+    /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
     /// map.retain(|&k, _| k % 2 == 0);
     /// assert_eq!(map.len(), 4);
     /// ```
-    //#[stable(feature = "retain_hash_collection", since = "1.18.0")]
     #[inline]
     pub fn retain<F>(&mut self, f: F)
     where
@@ -925,7 +946,6 @@
     /// # Examples
     ///
     /// ```
-    /// #![feature(map_into_keys_values)]
     /// use std::collections::HashMap;
     ///
     /// let mut map = HashMap::new();
@@ -933,10 +953,13 @@
     /// map.insert("b", 2);
     /// map.insert("c", 3);
     ///
-    /// let vec: Vec<&str> = map.into_keys().collect();
+    /// let mut vec: Vec<&str> = map.into_keys().collect();
+    /// // The `IntoKeys` iterator produces keys in arbitrary order, so the
+    /// // keys must be sorted to test them against a sorted array.
+    /// vec.sort_unstable();
+    /// assert_eq!(vec, ["a", "b", "c"]);
     /// ```
     #[inline]
-    //#[unstable(feature = "map_into_keys_values", issue = "75294")]
     pub fn into_keys(self) -> IntoKeys<K, V> {
         IntoKeys { inner: self.into_iter() }
     }
@@ -948,7 +971,6 @@
     /// # Examples
     ///
     /// ```
-    /// #![feature(map_into_keys_values)]
     /// use std::collections::HashMap;
     ///
     /// let mut map = HashMap::new();
@@ -956,10 +978,13 @@
     /// map.insert("b", 2);
     /// map.insert("c", 3);
     ///
-    /// let vec: Vec<i32> = map.into_values().collect();
+    /// let mut vec: Vec<i32> = map.into_values().collect();
+    /// // The `IntoValues` iterator produces values in arbitrary order, so
+    /// // the values must be sorted to test them against a sorted array.
+    /// vec.sort_unstable();
+    /// assert_eq!(vec, [1, 2, 3]);
     /// ```
     #[inline]
-    //#[unstable(feature = "map_into_keys_values", issue = "75294")]
     pub fn into_values(self) -> IntoValues<K, V> {
         IntoValues { inner: self.into_iter() }
     }
@@ -1001,7 +1026,6 @@
     /// acting erratically, with two keys randomly masking each other. Implementations
     /// are free to assume this doesn't happen (within the limits of memory-safety).
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn raw_entry_mut(&mut self) -> RawEntryBuilderMut<'_, K, V, S> {
         RawEntryBuilderMut { map: self }
     }
@@ -1022,13 +1046,28 @@
     ///
     /// Immutable raw entries have very limited use; you might instead want `raw_entry_mut`.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S> {
         RawEntryBuilder { map: self }
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
+impl<K, V, S> Clone for HashMap<K, V, S>
+where
+    K: Clone,
+    V: Clone,
+    S: Clone,
+{
+    #[inline]
+    fn clone(&self) -> Self {
+        Self { base: self.base.clone() }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, other: &Self) {
+        self.base.clone_from(&other.base);
+    }
+}
+
 impl<K, V, S> PartialEq for HashMap<K, V, S>
 where
     K: Eq + Hash,
@@ -1044,7 +1083,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> Eq for HashMap<K, V, S>
 where
     K: Eq + Hash,
@@ -1053,7 +1091,6 @@
 {
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> Debug for HashMap<K, V, S>
 where
     K: Debug,
@@ -1064,7 +1101,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> Default for HashMap<K, V, S>
 where
     S: Default,
@@ -1076,7 +1112,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, Q: ?Sized, V, S> Index<&Q> for HashMap<K, V, S>
 where
     K: Eq + Hash + Borrow<Q>,
@@ -1096,19 +1131,57 @@
     }
 }
 
+// Note: as what is currently the most convenient built-in way to construct
+// a HashMap, a simple usage of this function must not *require* the user
+// to provide a type annotation in order to infer the third type parameter
+// (the hasher parameter, conventionally "S").
+// To that end, this impl is defined using RandomState as the concrete
+// type of S, rather than being generic over `S: BuildHasher + Default`.
+// It is expected that users who want to specify a hasher will manually use
+// `with_capacity_and_hasher`.
+// If type parameter defaults worked on impls, and if type parameter
+// defaults could be mixed with const generics, then perhaps
+// this could be generalized.
+// See also the equivalent impl on HashSet.
+impl<K, V, const N: usize> From<[(K, V); N]> for HashMap<K, V, RandomState>
+where
+    K: Eq + Hash,
+{
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::HashMap;
+    ///
+    /// let map1 = HashMap::from([(1, 2), (3, 4)]);
+    /// let map2: HashMap<_, _> = [(1, 2), (3, 4)].into();
+    /// assert_eq!(map1, map2);
+    /// ```
+    fn from(arr: [(K, V); N]) -> Self {
+        crate::array::IntoIter::new(arr).collect()
+    }
+}
+
 /// An iterator over the entries of a `HashMap`.
 ///
 /// This `struct` is created by the [`iter`] method on [`HashMap`]. See its
 /// documentation for more.
 ///
 /// [`iter`]: HashMap::iter
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter = map.iter();
+/// ```
 pub struct Iter<'a, K: 'a, V: 'a> {
     base: base::Iter<'a, K, V>,
 }
 
 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V> Clone for Iter<'_, K, V> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1116,7 +1189,6 @@
     }
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
@@ -1129,13 +1201,22 @@
 /// documentation for more.
 ///
 /// [`iter_mut`]: HashMap::iter_mut
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter = map.iter_mut();
+/// ```
 pub struct IterMut<'a, K: 'a, V: 'a> {
     base: base::IterMut<'a, K, V>,
 }
 
 impl<'a, K, V> IterMut<'a, K, V> {
-    /// Returns a iterator of references over the remaining items.
+    /// Returns an iterator of references over the remaining items.
     #[inline]
     pub(super) fn iter(&self) -> Iter<'_, K, V> {
         Iter { base: self.base.rustc_iter() }
@@ -1148,13 +1229,22 @@
 /// (provided by the `IntoIterator` trait). See its documentation for more.
 ///
 /// [`into_iter`]: IntoIterator::into_iter
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter = map.into_iter();
+/// ```
 pub struct IntoIter<K, V> {
     base: base::IntoIter<K, V>,
 }
 
 impl<K, V> IntoIter<K, V> {
-    /// Returns a iterator of references over the remaining items.
+    /// Returns an iterator of references over the remaining items.
     #[inline]
     pub(super) fn iter(&self) -> Iter<'_, K, V> {
         Iter { base: self.base.rustc_iter() }
@@ -1167,13 +1257,21 @@
 /// documentation for more.
 ///
 /// [`keys`]: HashMap::keys
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter_keys = map.keys();
+/// ```
 pub struct Keys<'a, K: 'a, V: 'a> {
     inner: Iter<'a, K, V>,
 }
 
 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V> Clone for Keys<'_, K, V> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1181,7 +1279,6 @@
     }
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
@@ -1194,13 +1291,21 @@
 /// documentation for more.
 ///
 /// [`values`]: HashMap::values
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter_values = map.values();
+/// ```
 pub struct Values<'a, K: 'a, V: 'a> {
     inner: Iter<'a, K, V>,
 }
 
 // FIXME(#26925) Remove in favor of `#[derive(Clone)]`
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V> Clone for Values<'_, K, V> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1208,7 +1313,6 @@
     }
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
@@ -1221,13 +1325,22 @@
 /// documentation for more.
 ///
 /// [`drain`]: HashMap::drain
-//#[stable(feature = "drain", since = "1.6.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter = map.drain();
+/// ```
 pub struct Drain<'a, K: 'a, V: 'a> {
     base: base::Drain<'a, K, V>,
 }
 
 impl<'a, K, V> Drain<'a, K, V> {
-    /// Returns a iterator of references over the remaining items.
+    /// Returns an iterator of references over the remaining items.
     #[inline]
     pub(super) fn iter(&self) -> Iter<'_, K, V> {
         Iter { base: self.base.rustc_iter() }
@@ -1239,7 +1352,18 @@
 /// This `struct` is created by the [`drain_filter`] method on [`HashMap`].
 ///
 /// [`drain_filter`]: HashMap::drain_filter
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
+///
+/// # Example
+///
+/// ```
+/// #![feature(hash_drain_filter)]
+///
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter = map.drain_filter(|_k, v| *v % 2 == 0);
+/// ```
 pub struct DrainFilter<'a, K, V, F>
 where
     F: FnMut(&K, &mut V) -> bool,
@@ -1253,7 +1377,16 @@
 /// documentation for more.
 ///
 /// [`values_mut`]: HashMap::values_mut
-//#[stable(feature = "map_values_mut", since = "1.10.0")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter_values = map.values_mut();
+/// ```
 pub struct ValuesMut<'a, K: 'a, V: 'a> {
     inner: IterMut<'a, K, V>,
 }
@@ -1264,7 +1397,16 @@
 /// See its documentation for more.
 ///
 /// [`into_keys`]: HashMap::into_keys
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter_keys = map.into_keys();
+/// ```
 pub struct IntoKeys<K, V> {
     inner: IntoIter<K, V>,
 }
@@ -1275,7 +1417,16 @@
 /// See its documentation for more.
 ///
 /// [`into_values`]: HashMap::into_values
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+///
+/// # Example
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// let mut map = HashMap::new();
+/// map.insert("a", 1);
+/// let iter_keys = map.into_values();
+/// ```
 pub struct IntoValues<K, V> {
     inner: IntoIter<K, V>,
 }
@@ -1283,10 +1434,6 @@
 /// A builder for computing where in a HashMap a key-value pair would be stored.
 ///
 /// See the [`HashMap::raw_entry_mut`] docs for usage examples.
-///
-/// [`HashMap::raw_entry_mut`]: HashMap::raw_entry_mut
-
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 pub struct RawEntryBuilderMut<'a, K: 'a, V: 'a, S: 'a> {
     map: &'a mut HashMap<K, V, S>,
 }
@@ -1298,10 +1445,7 @@
 /// This `enum` is constructed through the [`raw_entry_mut`] method on [`HashMap`],
 /// then calling one of the methods of that [`RawEntryBuilderMut`].
 ///
-/// [`Entry`]: enum.Entry.html
 /// [`raw_entry_mut`]: HashMap::raw_entry_mut
-/// [`RawEntryBuilderMut`]: struct.RawEntryBuilderMut.html
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 pub enum RawEntryMut<'a, K: 'a, V: 'a, S: 'a> {
     /// An occupied entry.
     Occupied(RawOccupiedEntryMut<'a, K, V, S>),
@@ -1311,14 +1455,12 @@
 
 /// A view into an occupied entry in a `HashMap`.
 /// It is part of the [`RawEntryMut`] enum.
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 pub struct RawOccupiedEntryMut<'a, K: 'a, V: 'a, S: 'a> {
     base: base::RawOccupiedEntryMut<'a, K, V, S>,
 }
 
 /// A view into a vacant entry in a `HashMap`.
 /// It is part of the [`RawEntryMut`] enum.
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 pub struct RawVacantEntryMut<'a, K: 'a, V: 'a, S: 'a> {
     base: base::RawVacantEntryMut<'a, K, V, S>,
 }
@@ -1326,9 +1468,6 @@
 /// A builder for computing where in a HashMap a key-value pair would be stored.
 ///
 /// See the [`HashMap::raw_entry`] docs for usage examples.
-///
-/// [`HashMap::raw_entry`]: HashMap::raw_entry
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 pub struct RawEntryBuilder<'a, K: 'a, V: 'a, S: 'a> {
     map: &'a HashMap<K, V, S>,
 }
@@ -1339,7 +1478,6 @@
 {
     /// Creates a `RawEntryMut` from the given key.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_key<Q: ?Sized>(self, k: &Q) -> RawEntryMut<'a, K, V, S>
     where
         K: Borrow<Q>,
@@ -1350,7 +1488,6 @@
 
     /// Creates a `RawEntryMut` from the given key and its hash.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> RawEntryMut<'a, K, V, S>
     where
         K: Borrow<Q>,
@@ -1361,7 +1498,6 @@
 
     /// Creates a `RawEntryMut` from the given hash.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_hash<F>(self, hash: u64, is_match: F) -> RawEntryMut<'a, K, V, S>
     where
         for<'b> F: FnMut(&'b K) -> bool,
@@ -1376,7 +1512,6 @@
 {
     /// Access an entry by key.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_key<Q: ?Sized>(self, k: &Q) -> Option<(&'a K, &'a V)>
     where
         K: Borrow<Q>,
@@ -1387,7 +1522,6 @@
 
     /// Access an entry by a key and its hash.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_key_hashed_nocheck<Q: ?Sized>(self, hash: u64, k: &Q) -> Option<(&'a K, &'a V)>
     where
         K: Borrow<Q>,
@@ -1398,7 +1532,6 @@
 
     /// Access an entry by hash.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn from_hash<F>(self, hash: u64, is_match: F) -> Option<(&'a K, &'a V)>
     where
         F: FnMut(&K) -> bool,
@@ -1426,7 +1559,6 @@
     /// assert_eq!(map["poneyland"], 6);
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn or_insert(self, default_key: K, default_val: V) -> (&'a mut K, &'a mut V)
     where
         K: Hash,
@@ -1456,7 +1588,6 @@
     /// assert_eq!(map["poneyland"], "hoho".to_string());
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn or_insert_with<F>(self, default: F) -> (&'a mut K, &'a mut V)
     where
         F: FnOnce() -> (K, V),
@@ -1496,7 +1627,6 @@
     /// assert_eq!(map["poneyland"], 43);
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn and_modify<F>(self, f: F) -> Self
     where
         F: FnOnce(&mut K, &mut V),
@@ -1517,14 +1647,12 @@
 impl<'a, K, V, S> RawOccupiedEntryMut<'a, K, V, S> {
     /// Gets a reference to the key in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn key(&self) -> &K {
         self.base.key()
     }
 
     /// Gets a mutable reference to the key in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn key_mut(&mut self) -> &mut K {
         self.base.key_mut()
     }
@@ -1532,89 +1660,77 @@
     /// Converts the entry into a mutable reference to the key in the entry
     /// with a lifetime bound to the map itself.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn into_key(self) -> &'a mut K {
         self.base.into_key()
     }
 
     /// Gets a reference to the value in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn get(&self) -> &V {
         self.base.get()
     }
 
-    /// Converts the OccupiedEntry into a mutable reference to the value in the entry
+    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
     /// with a lifetime bound to the map itself.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn into_mut(self) -> &'a mut V {
         self.base.into_mut()
     }
 
     /// Gets a mutable reference to the value in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn get_mut(&mut self) -> &mut V {
         self.base.get_mut()
     }
 
     /// Gets a reference to the key and value in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn get_key_value(&mut self) -> (&K, &V) {
         self.base.get_key_value()
     }
 
     /// Gets a mutable reference to the key and value in the entry.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn get_key_value_mut(&mut self) -> (&mut K, &mut V) {
         self.base.get_key_value_mut()
     }
 
-    /// Converts the OccupiedEntry into a mutable reference to the key and value in the entry
+    /// Converts the `OccupiedEntry` into a mutable reference to the key and value in the entry
     /// with a lifetime bound to the map itself.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn into_key_value(self) -> (&'a mut K, &'a mut V) {
         self.base.into_key_value()
     }
 
     /// Sets the value of the entry, and returns the entry's old value.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn insert(&mut self, value: V) -> V {
         self.base.insert(value)
     }
 
     /// Sets the value of the entry, and returns the entry's old value.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn insert_key(&mut self, key: K) -> K {
         self.base.insert_key(key)
     }
 
     /// Takes the value out of the entry, and returns it.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn remove(self) -> V {
         self.base.remove()
     }
 
     /// Take the ownership of the key and value from the map.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn remove_entry(self) -> (K, V) {
         self.base.remove_entry()
     }
 }
 
 impl<'a, K, V, S> RawVacantEntryMut<'a, K, V, S> {
-    /// Sets the value of the entry with the VacantEntry's key,
+    /// Sets the value of the entry with the `VacantEntry`'s key,
     /// and returns a mutable reference to it.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn insert(self, key: K, value: V) -> (&'a mut K, &'a mut V)
     where
         K: Hash,
@@ -1626,7 +1742,6 @@
     /// Sets the value of the entry with the VacantEntry's key,
     /// and returns a mutable reference to it.
     #[inline]
-    //#[unstable(feature = "hash_raw_entry", issue = "56167")]
     pub fn insert_hashed_nocheck(self, hash: u64, key: K, value: V) -> (&'a mut K, &'a mut V)
     where
         K: Hash,
@@ -1636,14 +1751,12 @@
     }
 }
 
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 impl<K, V, S> Debug for RawEntryBuilderMut<'_, K, V, S> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("RawEntryBuilder").finish()
+        f.debug_struct("RawEntryBuilder").finish_non_exhaustive()
     }
 }
 
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 impl<K: Debug, V: Debug, S> Debug for RawEntryMut<'_, K, V, S> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
@@ -1653,27 +1766,24 @@
     }
 }
 
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 impl<K: Debug, V: Debug, S> Debug for RawOccupiedEntryMut<'_, K, V, S> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("RawOccupiedEntryMut")
             .field("key", self.key())
             .field("value", self.get())
-            .finish()
+            .finish_non_exhaustive()
     }
 }
 
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 impl<K, V, S> Debug for RawVacantEntryMut<'_, K, V, S> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("RawVacantEntryMut").finish()
+        f.debug_struct("RawVacantEntryMut").finish_non_exhaustive()
     }
 }
 
-//#[unstable(feature = "hash_raw_entry", issue = "56167")]
 impl<K, V, S> Debug for RawEntryBuilder<'_, K, V, S> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("RawEntryBuilder").finish()
+        f.debug_struct("RawEntryBuilder").finish_non_exhaustive()
     }
 }
 
@@ -1682,18 +1792,15 @@
 /// This `enum` is constructed from the [`entry`] method on [`HashMap`].
 ///
 /// [`entry`]: HashMap::entry
-//#[stable(feature = "rust1", since = "1.0.0")]
+#[cfg_attr(not(test), rustc_diagnostic_item = "HashMapEntry")]
 pub enum Entry<'a, K: 'a, V: 'a> {
     /// An occupied entry.
-    //#[stable(feature = "rust1", since = "1.0.0")]
     Occupied(OccupiedEntry<'a, K, V>),
 
     /// A vacant entry.
-    //#[stable(feature = "rust1", since = "1.0.0")]
     Vacant(VacantEntry<'a, K, V>),
 }
 
-//#[stable(feature = "debug_hash_map", since = "1.12.0")]
 impl<K: Debug, V: Debug> Debug for Entry<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
@@ -1705,37 +1812,63 @@
 
 /// A view into an occupied entry in a `HashMap`.
 /// It is part of the [`Entry`] enum.
-///
-/// [`Entry`]: enum.Entry.html
-//#[stable(feature = "rust1", since = "1.0.0")]
 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
     base: base::RustcOccupiedEntry<'a, K, V>,
 }
 
-//#[stable(feature = "debug_hash_map", since = "1.12.0")]
 impl<K: Debug, V: Debug> Debug for OccupiedEntry<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("OccupiedEntry").field("key", self.key()).field("value", self.get()).finish()
+        f.debug_struct("OccupiedEntry")
+            .field("key", self.key())
+            .field("value", self.get())
+            .finish_non_exhaustive()
     }
 }
 
 /// A view into a vacant entry in a `HashMap`.
 /// It is part of the [`Entry`] enum.
-///
-/// [`Entry`]: enum.Entry.html
-//#[stable(feature = "rust1", since = "1.0.0")]
 pub struct VacantEntry<'a, K: 'a, V: 'a> {
     base: base::RustcVacantEntry<'a, K, V>,
 }
 
-//#[stable(feature = "debug_hash_map", since = "1.12.0")]
 impl<K: Debug, V> Debug for VacantEntry<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_tuple("VacantEntry").field(self.key()).finish()
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
+/// The error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
+///
+/// Contains the occupied entry, and the value that was not inserted.
+pub struct OccupiedError<'a, K: 'a, V: 'a> {
+    /// The entry in the map that was already occupied.
+    pub entry: OccupiedEntry<'a, K, V>,
+    /// The value which was not inserted, because the entry was already occupied.
+    pub value: V,
+}
+
+impl<K: Debug, V: Debug> Debug for OccupiedError<'_, K, V> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("OccupiedError")
+            .field("key", self.entry.key())
+            .field("old_value", self.entry.get())
+            .field("new_value", &self.value)
+            .finish_non_exhaustive()
+    }
+}
+
+impl<'a, K: Debug, V: Debug> fmt::Display for OccupiedError<'a, K, V> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "failed to insert {:?}, key {:?} already exists with value {:?}",
+            self.value,
+            self.entry.key(),
+            self.entry.get(),
+        )
+    }
+}
+
 impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> {
     type Item = (&'a K, &'a V);
     type IntoIter = Iter<'a, K, V>;
@@ -1746,7 +1879,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> {
     type Item = (&'a K, &'a mut V);
     type IntoIter = IterMut<'a, K, V>;
@@ -1757,7 +1889,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> IntoIterator for HashMap<K, V, S> {
     type Item = (K, V);
     type IntoIter = IntoIter<K, V>;
@@ -1785,7 +1916,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K, V> Iterator for Iter<'a, K, V> {
     type Item = (&'a K, &'a V);
 
@@ -1798,7 +1928,7 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
@@ -1806,10 +1936,8 @@
     }
 }
 
-//#[stable(feature = "fused", since = "1.26.0")]
 impl<K, V> FusedIterator for Iter<'_, K, V> {}
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K, V> Iterator for IterMut<'a, K, V> {
     type Item = (&'a K, &'a mut V);
 
@@ -1822,17 +1950,16 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for IterMut<'_, K, V> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K, V> fmt::Debug for IterMut<'_, K, V>
 where
     K: fmt::Debug,
@@ -1843,7 +1970,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V> Iterator for IntoIter<K, V> {
     type Item = (K, V);
 
@@ -1856,24 +1982,22 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K, V> ExactSizeIterator for IntoIter<K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for IntoIter<K, V> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: Debug, V: Debug> fmt::Debug for IntoIter<K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.iter()).finish()
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K, V> Iterator for Keys<'a, K, V> {
     type Item = &'a K;
 
@@ -1886,17 +2010,16 @@
         self.inner.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for Keys<'_, K, V> {}
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K, V> Iterator for Values<'a, K, V> {
     type Item = &'a V;
 
@@ -1909,17 +2032,16 @@
         self.inner.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K, V> ExactSizeIterator for Values<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for Values<'_, K, V> {}
 
-//#[stable(feature = "map_values_mut", since = "1.10.0")]
 impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
     type Item = &'a mut V;
 
@@ -1932,28 +2054,22 @@
         self.inner.size_hint()
     }
 }
-//#[stable(feature = "map_values_mut", since = "1.10.0")]
+
 impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
-impl<K, V> fmt::Debug for ValuesMut<'_, K, V>
-where
-    K: fmt::Debug,
-    V: fmt::Debug,
-{
+impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_list().entries(self.inner.iter()).finish()
+        f.debug_list().entries(self.inner.iter().map(|(_, val)| val)).finish()
     }
 }
 
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
 impl<K, V> Iterator for IntoKeys<K, V> {
     type Item = K;
 
@@ -1966,24 +2082,22 @@
         self.inner.size_hint()
     }
 }
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+
 impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+
 impl<K, V> FusedIterator for IntoKeys<K, V> {}
 
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
-impl<K: Debug, V: Debug> fmt::Debug for IntoKeys<K, V> {
+impl<K: Debug, V> fmt::Debug for IntoKeys<K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.inner.iter().map(|(k, _)| k)).finish()
     }
 }
 
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
 impl<K, V> Iterator for IntoValues<K, V> {
     type Item = V;
 
@@ -1996,24 +2110,22 @@
         self.inner.size_hint()
     }
 }
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+
 impl<K, V> ExactSizeIterator for IntoValues<K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.inner.len()
     }
 }
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
+
 impl<K, V> FusedIterator for IntoValues<K, V> {}
 
-//#[unstable(feature = "map_into_keys_values", issue = "75294")]
-impl<K: Debug, V: Debug> fmt::Debug for IntoValues<K, V> {
+impl<K, V: Debug> fmt::Debug for IntoValues<K, V> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.inner.iter().map(|(_, v)| v)).finish()
     }
 }
 
-//#[stable(feature = "drain", since = "1.6.0")]
 impl<'a, K, V> Iterator for Drain<'a, K, V> {
     type Item = (K, V);
 
@@ -2026,17 +2138,16 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "drain", since = "1.6.0")]
+
 impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K, V> FusedIterator for Drain<'_, K, V> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K, V> fmt::Debug for Drain<'_, K, V>
 where
     K: fmt::Debug,
@@ -2047,7 +2158,6 @@
     }
 }
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<K, V, F> Iterator for DrainFilter<'_, K, V, F>
 where
     F: FnMut(&K, &mut V) -> bool,
@@ -2064,21 +2174,18 @@
     }
 }
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<K, V, F> FusedIterator for DrainFilter<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool {}
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<'a, K, V, F> fmt::Debug for DrainFilter<'a, K, V, F>
 where
     F: FnMut(&K, &mut V) -> bool,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("DrainFilter { .. }")
+        f.debug_struct("DrainFilter").finish_non_exhaustive()
     }
 }
 
 impl<'a, K, V> Entry<'a, K, V> {
-    //#[stable(feature = "rust1", since = "1.0.0")]
     /// Ensures a value is in the entry by inserting the default if empty, and returns
     /// a mutable reference to the value in the entry.
     ///
@@ -2103,7 +2210,6 @@
         }
     }
 
-    //#[stable(feature = "rust1", since = "1.0.0")]
     /// Ensures a value is in the entry by inserting the result of the default function if empty,
     /// and returns a mutable reference to the value in the entry.
     ///
@@ -2127,15 +2233,16 @@
         }
     }
 
-    //#[unstable(feature = "or_insert_with_key", issue = "71024")]
-    /// Ensures a value is in the entry by inserting, if empty, the result of the default function,
-    /// which takes the key as its argument, and returns a mutable reference to the value in the
-    /// entry.
+    /// Ensures a value is in the entry by inserting, if empty, the result of the default function.
+    /// This method allows for generating key-derived values for insertion by providing the default
+    /// function a reference to the key that was moved during the `.entry(key)` method call.
+    ///
+    /// The reference to the moved key is provided so that cloning or copying the key is
+    /// unnecessary, unlike with `.or_insert_with(|| ... )`.
     ///
     /// # Examples
     ///
     /// ```
-    /// #![feature(or_insert_with_key)]
     /// use std::collections::HashMap;
     ///
     /// let mut map: HashMap<&str, usize> = HashMap::new();
@@ -2166,7 +2273,6 @@
     /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
     /// ```
     #[inline]
-    //#[stable(feature = "map_entry_keys", since = "1.10.0")]
     pub fn key(&self) -> &K {
         match *self {
             Occupied(ref entry) => entry.key(),
@@ -2195,7 +2301,6 @@
     /// assert_eq!(map["poneyland"], 43);
     /// ```
     #[inline]
-    //#[stable(feature = "entry_and_modify", since = "1.26.0")]
     pub fn and_modify<F>(self, f: F) -> Self
     where
         F: FnOnce(&mut V),
@@ -2209,7 +2314,7 @@
         }
     }
 
-    /// Sets the value of the entry, and returns an OccupiedEntry.
+    /// Sets the value of the entry, and returns an `OccupiedEntry`.
     ///
     /// # Examples
     ///
@@ -2223,7 +2328,6 @@
     /// assert_eq!(entry.key(), &"poneyland");
     /// ```
     #[inline]
-    //#[unstable(feature = "entry_insert", issue = "65225")]
     pub fn insert(self, value: V) -> OccupiedEntry<'a, K, V> {
         match self {
             Occupied(mut entry) => {
@@ -2236,7 +2340,6 @@
 }
 
 impl<'a, K, V: Default> Entry<'a, K, V> {
-    //#[stable(feature = "entry_or_default", since = "1.28.0")]
     /// Ensures a value is in the entry by inserting the default value if empty,
     /// and returns a mutable reference to the value in the entry.
     ///
@@ -2274,7 +2377,6 @@
     /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
     /// ```
     #[inline]
-    //#[stable(feature = "map_entry_keys", since = "1.10.0")]
     pub fn key(&self) -> &K {
         self.base.key()
     }
@@ -2298,7 +2400,6 @@
     /// assert_eq!(map.contains_key("poneyland"), false);
     /// ```
     #[inline]
-    //#[stable(feature = "map_entry_recover_keys2", since = "1.12.0")]
     pub fn remove_entry(self) -> (K, V) {
         self.base.remove_entry()
     }
@@ -2319,7 +2420,6 @@
     /// }
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn get(&self) -> &V {
         self.base.get()
     }
@@ -2352,12 +2452,11 @@
     /// assert_eq!(map["poneyland"], 24);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn get_mut(&mut self) -> &mut V {
         self.base.get_mut()
     }
 
-    /// Converts the OccupiedEntry into a mutable reference to the value in the entry
+    /// Converts the `OccupiedEntry` into a mutable reference to the value in the entry
     /// with a lifetime bound to the map itself.
     ///
     /// If you need multiple references to the `OccupiedEntry`, see [`get_mut`].
@@ -2381,7 +2480,6 @@
     /// assert_eq!(map["poneyland"], 22);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn into_mut(self) -> &'a mut V {
         self.base.into_mut()
     }
@@ -2404,7 +2502,6 @@
     /// assert_eq!(map["poneyland"], 15);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn insert(&mut self, value: V) -> V {
         self.base.insert(value)
     }
@@ -2427,7 +2524,6 @@
     /// assert_eq!(map.contains_key("poneyland"), false);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn remove(self) -> V {
         self.base.remove()
     }
@@ -2454,7 +2550,6 @@
     ///
     /// ```
     #[inline]
-    //#[unstable(feature = "map_entry_replace", issue = "44286")]
     pub fn replace_entry(self, value: V) -> (K, V) {
         self.base.replace_entry(value)
     }
@@ -2485,7 +2580,6 @@
     /// }
     /// ```
     #[inline]
-    //#[unstable(feature = "map_entry_replace", issue = "44286")]
     pub fn replace_key(self) -> K {
         self.base.replace_key()
     }
@@ -2504,7 +2598,6 @@
     /// assert_eq!(map.entry("poneyland").key(), &"poneyland");
     /// ```
     #[inline]
-    //#[stable(feature = "map_entry_keys", since = "1.10.0")]
     pub fn key(&self) -> &K {
         self.base.key()
     }
@@ -2524,12 +2617,11 @@
     /// }
     /// ```
     #[inline]
-    //#[stable(feature = "map_entry_recover_keys2", since = "1.12.0")]
     pub fn into_key(self) -> K {
         self.base.into_key()
     }
 
-    /// Sets the value of the entry with the VacantEntry's key,
+    /// Sets the value of the entry with the `VacantEntry`'s key,
     /// and returns a mutable reference to it.
     ///
     /// # Examples
@@ -2546,13 +2638,12 @@
     /// assert_eq!(map["poneyland"], 37);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn insert(self, value: V) -> &'a mut V {
         self.base.insert(value)
     }
 
-    /// Sets the value of the entry with the VacantEntry's key,
-    /// and returns an OccupiedEntry.
+    /// Sets the value of the entry with the `VacantEntry`'s key,
+    /// and returns an `OccupiedEntry`.
     ///
     /// # Examples
     ///
@@ -2574,7 +2665,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
 where
     K: Eq + Hash,
@@ -2589,7 +2679,6 @@
 
 /// Inserts all new key-values from the iterator and replaces values with existing
 /// keys with new values returned from the iterator.
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
 where
     K: Eq + Hash,
@@ -2607,19 +2696,10 @@
 
     #[inline]
     fn extend_reserve(&mut self, additional: usize) {
-        // self.base.extend_reserve(additional);
-        // FIXME: hashbrown should implement this method.
-        // But until then, use the same reservation logic:
-
-        // Reserve the entire hint lower bound if the map is empty.
-        // Otherwise reserve half the hint (rounded up), so the map
-        // will only resize twice in the worst case.
-        let reserve = if self.is_empty() { additional } else { (additional + 1) / 2 };
-        self.base.reserve(reserve);
+        self.base.extend_reserve(additional);
     }
 }
 
-//#[stable(feature = "hash_extend_copy", since = "1.4.0")]
 impl<'a, K: 'a, V: 'a, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
 where
     K: Eq + Hash + Copy,
@@ -2659,7 +2739,6 @@
 /// map.insert(1, 2);
 /// ```
 #[derive(Clone)]
-//#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
 pub struct RandomState {
     k0: u64,
     k1: u64,
@@ -2678,7 +2757,6 @@
     #[inline]
     #[allow(deprecated)]
     // rand
-    //#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
     pub fn new() -> RandomState {
         // Historically this function did not cache keys from the OS and instead
         // simply always called `rand::thread_rng().gen()` twice. In #31356 it
@@ -2703,7 +2781,6 @@
     }
 }
 
-//#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
 impl BuildHasher for RandomState {
     type Hasher = DefaultHasher;
     #[inline]
@@ -2717,7 +2794,6 @@
 ///
 /// The internal algorithm is not specified, and so it and its hashes should
 /// not be relied upon over releases.
-//#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
 #[allow(deprecated)]
 #[derive(Clone, Debug)]
 pub struct DefaultHasher(SipHasher13);
@@ -2728,26 +2804,22 @@
     /// This hasher is not guaranteed to be the same as all other
     /// `DefaultHasher` instances, but is the same as all other `DefaultHasher`
     /// instances created through `new` or `default`.
-    //#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
     #[allow(deprecated)]
     pub fn new() -> DefaultHasher {
         DefaultHasher(SipHasher13::new_with_keys(0, 0))
     }
 }
 
-//#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
 impl Default for DefaultHasher {
-    // FIXME: here should link `new` to [DefaultHasher::new], but it occurs intra-doc link
-    // resolution failure when re-exporting libstd items. When #56922 fixed,
-    // link `new` to [DefaultHasher::new] again.
-    /// Creates a new `DefaultHasher` using `new`.
+    /// Creates a new `DefaultHasher` using [`new`].
     /// See its documentation for more.
+    ///
+    /// [`new`]: DefaultHasher::new
     fn default() -> DefaultHasher {
         DefaultHasher::new()
     }
 }
 
-//#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
 impl Hasher for DefaultHasher {
     #[inline]
     fn write(&mut self, msg: &[u8]) {
@@ -2760,7 +2832,6 @@
     }
 }
 
-//#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
 impl Default for RandomState {
     /// Constructs a new `RandomState`.
     #[inline]
@@ -2769,10 +2840,9 @@
     }
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl fmt::Debug for RandomState {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("RandomState { .. }")
+        f.debug_struct("RandomState").finish_non_exhaustive()
     }
 }
 
@@ -2787,9 +2857,11 @@
 #[inline]
 pub(super) fn map_try_reserve_error(err: hashbrown::TryReserveError) -> TryReserveError {
     match err {
-        hashbrown::TryReserveError::CapacityOverflow => TryReserveError::CapacityOverflow,
+        hashbrown::TryReserveError::CapacityOverflow => {
+            TryReserveErrorKind::CapacityOverflow.into()
+        }
         hashbrown::TryReserveError::AllocError { layout } => {
-            TryReserveError::AllocError { layout, non_exhaustive: () }
+            TryReserveErrorKind::AllocError { layout, non_exhaustive: () }.into()
         }
     }
 }
diff --git a/sgx_tstd/src/collections/hash/mod.rs b/sgx_tstd/src/collections/hash/mod.rs
index 348820a..2d01320 100644
--- a/sgx_tstd/src/collections/hash/mod.rs
+++ b/sgx_tstd/src/collections/hash/mod.rs
@@ -1,3 +1,20 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
 //! Unordered containers, implemented as hash-tables
 
 pub mod map;
diff --git a/sgx_tstd/src/collections/hash/set.rs b/sgx_tstd/src/collections/hash/set.rs
index 3f1edda..39ef7af 100644
--- a/sgx_tstd/src/collections/hash/set.rs
+++ b/sgx_tstd/src/collections/hash/set.rs
@@ -1,5 +1,19 @@
-//#[cfg(test)]
-//mod tests;
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
 
 use hashbrown::hash_set as base;
 
@@ -19,7 +33,7 @@
 // for `bucket.val` in the case of HashSet. I suppose we would need HKT
 // to get rid of it properly.
 
-/// A hash set implemented as a `HashMap` where the value is `()`.
+/// A [hash set] implemented as a `HashMap` where the value is `()`.
 ///
 /// As with the [`HashMap`] type, a `HashSet` requires that the elements
 /// implement the [`Eq`] and [`Hash`] traits. This can frequently be achieved by
@@ -37,7 +51,9 @@
 /// item's hash, as determined by the [`Hash`] trait, or its equality, as
 /// determined by the [`Eq`] trait, changes while it is in the set. This is
 /// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or
-/// unsafe code.
+/// unsafe code. The behavior resulting from such a logic error is not
+/// specified, but will not result in undefined behavior. This could include
+/// panics, incorrect results, aborts, memory leaks, and non-termination.
 ///
 /// # Examples
 ///
@@ -93,22 +109,19 @@
 /// }
 /// ```
 ///
-/// A `HashSet` with fixed list of elements can be initialized from an array:
+/// A `HashSet` with a known list of items can be initialized from an array:
 ///
 /// ```
 /// use std::collections::HashSet;
 ///
-/// let viking_names: HashSet<&'static str> =
-///     [ "Einar", "Olaf", "Harald" ].iter().cloned().collect();
-/// // use the values stored in the set
+/// let viking_names = HashSet::from(["Einar", "Olaf", "Harald"]);
 /// ```
 ///
+/// [hash set]: crate::collections#use-the-set-variant-of-any-of-these-maps-when
 /// [`HashMap`]: crate::collections::HashMap
 /// [`RefCell`]: crate::cell::RefCell
 /// [`Cell`]: crate::cell::Cell
-#[derive(Clone)]
 #[cfg_attr(not(test), rustc_diagnostic_item = "hashset_type")]
-//#[stable(feature = "rust1", since = "1.0.0")]
 pub struct HashSet<T, S = RandomState> {
     base: base::HashSet<T, S>,
 }
@@ -126,7 +139,6 @@
     /// let set: HashSet<i32> = HashSet::new();
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn new() -> HashSet<T, RandomState> {
         Default::default()
     }
@@ -144,7 +156,6 @@
     /// assert!(set.capacity() >= 10);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> {
         HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) }
     }
@@ -161,7 +172,6 @@
     /// assert!(set.capacity() >= 100);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn capacity(&self) -> usize {
         self.base.capacity()
     }
@@ -183,7 +193,6 @@
     /// }
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn iter(&self) -> Iter<'_, T> {
         Iter { base: self.base.iter() }
     }
@@ -201,7 +210,6 @@
     /// assert_eq!(v.len(), 1);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn len(&self) -> usize {
         self.base.len()
     }
@@ -219,7 +227,6 @@
     /// assert!(!v.is_empty());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn is_empty(&self) -> bool {
         self.base.is_empty()
     }
@@ -242,7 +249,6 @@
     /// assert!(set.is_empty());
     /// ```
     #[inline]
-    //#[stable(feature = "drain", since = "1.6.0")]
     pub fn drain(&mut self) -> Drain<'_, T> {
         Drain { base: self.base.drain() }
     }
@@ -280,7 +286,6 @@
     /// assert_eq!(odds, vec![1, 3, 5, 7]);
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_drain_filter", issue = "59618")]
     pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, T, F>
     where
         F: FnMut(&T) -> bool,
@@ -301,7 +306,6 @@
     /// assert!(v.is_empty());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn clear(&mut self) {
         self.base.clear()
     }
@@ -330,7 +334,6 @@
     /// set.insert(2);
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
     pub fn with_hasher(hasher: S) -> HashSet<T, S> {
         HashSet { base: base::HashSet::with_hasher(hasher) }
     }
@@ -360,7 +363,6 @@
     /// set.insert(1);
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
     pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
         HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, hasher) }
     }
@@ -378,7 +380,6 @@
     /// let hasher: &RandomState = set.hasher();
     /// ```
     #[inline]
-    //#[stable(feature = "hashmap_public_hasher", since = "1.9.0")]
     pub fn hasher(&self) -> &S {
         self.base.hasher()
     }
@@ -406,13 +407,12 @@
     /// assert!(set.capacity() >= 10);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn reserve(&mut self, additional: usize) {
         self.base.reserve(additional)
     }
 
     /// Tries to reserve capacity for at least `additional` more elements to be inserted
-    /// in the given `HashSet<K,V>`. The collection may reserve more space to avoid
+    /// in the given `HashSet<K, V>`. The collection may reserve more space to avoid
     /// frequent reallocations.
     ///
     /// # Errors
@@ -429,7 +429,6 @@
     /// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
     /// ```
     #[inline]
-    //#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
     pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
         self.base.try_reserve(additional).map_err(map_try_reserve_error)
     }
@@ -451,7 +450,6 @@
     /// assert!(set.capacity() >= 2);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn shrink_to_fit(&mut self) {
         self.base.shrink_to_fit()
     }
@@ -460,13 +458,10 @@
     /// down no lower than the supplied limit while maintaining the internal rules
     /// and possibly leaving some space in accordance with the resize policy.
     ///
-    /// Panics if the current capacity is smaller than the supplied
-    /// minimum capacity.
-    ///
+    /// If the current capacity is less than the lower limit, this is a no-op.
     /// # Examples
     ///
     /// ```
-    /// #![feature(shrink_to)]
     /// use std::collections::HashSet;
     ///
     /// let mut set = HashSet::with_capacity(100);
@@ -479,7 +474,6 @@
     /// assert!(set.capacity() >= 2);
     /// ```
     #[inline]
-    //#[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
     pub fn shrink_to(&mut self, min_capacity: usize) {
         self.base.shrink_to(min_capacity)
     }
@@ -508,7 +502,6 @@
     /// assert_eq!(diff, [4].iter().collect());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> {
         Difference { iter: self.iter(), other }
     }
@@ -535,7 +528,6 @@
     /// assert_eq!(diff1, [1, 4].iter().collect());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn symmetric_difference<'a>(
         &'a self,
         other: &'a HashSet<T, S>,
@@ -562,7 +554,6 @@
     /// assert_eq!(intersection, [2, 3].iter().collect());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S> {
         if self.len() <= other.len() {
             Intersection { iter: self.iter(), other }
@@ -590,7 +581,6 @@
     /// assert_eq!(union, [1, 2, 3, 4].iter().collect());
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S> {
         if self.len() >= other.len() {
             Union { iter: self.iter().chain(other.difference(self)) }
@@ -615,7 +605,6 @@
     /// assert_eq!(set.contains(&4), false);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
     where
         T: Borrow<Q>,
@@ -640,7 +629,6 @@
     /// assert_eq!(set.get(&4), None);
     /// ```
     #[inline]
-    //#[stable(feature = "set_recovery", since = "1.9.0")]
     pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
     where
         T: Borrow<Q>,
@@ -666,7 +654,6 @@
     /// assert_eq!(set.len(), 4); // 100 was inserted
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_set_entry", issue = "60896")]
     pub fn get_or_insert(&mut self, value: T) -> &T {
         // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
         // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
@@ -694,7 +681,6 @@
     /// assert_eq!(set.len(), 4); // a new "fish" was inserted
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_set_entry", issue = "60896")]
     pub fn get_or_insert_owned<Q: ?Sized>(&mut self, value: &Q) -> &T
     where
         T: Borrow<Q>,
@@ -726,7 +712,6 @@
     /// assert_eq!(set.len(), 4); // a new "fish" was inserted
     /// ```
     #[inline]
-    //#[unstable(feature = "hash_set_entry", issue = "60896")]
     pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
     where
         T: Borrow<Q>,
@@ -755,7 +740,6 @@
     /// b.insert(1);
     /// assert_eq!(a.is_disjoint(&b), false);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool {
         if self.len() <= other.len() {
             self.iter().all(|v| !other.contains(v))
@@ -781,7 +765,6 @@
     /// set.insert(4);
     /// assert_eq!(set.is_subset(&sup), false);
     /// ```
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
         if self.len() <= other.len() { self.iter().all(|v| other.contains(v)) } else { false }
     }
@@ -807,7 +790,6 @@
     /// assert_eq!(set.is_superset(&sub), true);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn is_superset(&self, other: &HashSet<T, S>) -> bool {
         other.is_subset(self)
     }
@@ -830,7 +812,6 @@
     /// assert_eq!(set.len(), 1);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn insert(&mut self, value: T) -> bool {
         self.base.insert(value)
     }
@@ -851,7 +832,6 @@
     /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
     /// ```
     #[inline]
-    //#[stable(feature = "set_recovery", since = "1.9.0")]
     pub fn replace(&mut self, value: T) -> Option<T> {
         self.base.replace(value)
     }
@@ -875,7 +855,6 @@
     /// assert_eq!(set.remove(&2), false);
     /// ```
     #[inline]
-    //#[stable(feature = "rust1", since = "1.0.0")]
     pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
     where
         T: Borrow<Q>,
@@ -900,7 +879,6 @@
     /// assert_eq!(set.take(&2), None);
     /// ```
     #[inline]
-    //#[stable(feature = "set_recovery", since = "1.9.0")]
     pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
     where
         T: Borrow<Q>,
@@ -912,18 +890,18 @@
     /// Retains only the elements specified by the predicate.
     ///
     /// In other words, remove all elements `e` such that `f(&e)` returns `false`.
+    /// The elements are visited in unsorted (and unspecified) order.
     ///
     /// # Examples
     ///
     /// ```
     /// use std::collections::HashSet;
     ///
-    /// let xs = [1,2,3,4,5,6];
+    /// let xs = [1, 2, 3, 4, 5, 6];
     /// let mut set: HashSet<i32> = xs.iter().cloned().collect();
     /// set.retain(|&k| k % 2 == 0);
     /// assert_eq!(set.len(), 3);
     /// ```
-    //#[stable(feature = "retain_hash_collection", since = "1.18.0")]
     pub fn retain<F>(&mut self, f: F)
     where
         F: FnMut(&T) -> bool,
@@ -932,7 +910,22 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
+impl<T, S> Clone for HashSet<T, S>
+where
+    T: Clone,
+    S: Clone,
+{
+    #[inline]
+    fn clone(&self) -> Self {
+        Self { base: self.base.clone() }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, other: &Self) {
+        self.base.clone_from(&other.base);
+    }
+}
+
 impl<T, S> PartialEq for HashSet<T, S>
 where
     T: Eq + Hash,
@@ -947,7 +940,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Eq for HashSet<T, S>
 where
     T: Eq + Hash,
@@ -955,7 +947,6 @@
 {
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> fmt::Debug for HashSet<T, S>
 where
     T: fmt::Debug,
@@ -965,7 +956,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> FromIterator<T> for HashSet<T, S>
 where
     T: Eq + Hash,
@@ -979,7 +969,36 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
+// Note: as what is currently the most convenient built-in way to construct
+// a HashSet, a simple usage of this function must not *require* the user
+// to provide a type annotation in order to infer the third type parameter
+// (the hasher parameter, conventionally "S").
+// To that end, this impl is defined using RandomState as the concrete
+// type of S, rather than being generic over `S: BuildHasher + Default`.
+// It is expected that users who want to specify a hasher will manually use
+// `with_capacity_and_hasher`.
+// If type parameter defaults worked on impls, and if type parameter
+// defaults could be mixed with const generics, then perhaps
+// this could be generalized.
+// See also the equivalent impl on HashMap.
+impl<T, const N: usize> From<[T; N]> for HashSet<T, RandomState>
+where
+    T: Eq + Hash,
+{
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::HashSet;
+    ///
+    /// let set1 = HashSet::from([1, 2, 3, 4]);
+    /// let set2: HashSet<_> = [1, 2, 3, 4].into();
+    /// assert_eq!(set1, set2);
+    /// ```
+    fn from(arr: [T; N]) -> Self {
+        crate::array::IntoIter::new(arr).collect()
+    }
+}
+
 impl<T, S> Extend<T> for HashSet<T, S>
 where
     T: Eq + Hash,
@@ -1001,7 +1020,6 @@
     }
 }
 
-//#[stable(feature = "hash_extend_copy", since = "1.4.0")]
 impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
 where
     T: 'a + Eq + Hash + Copy,
@@ -1023,7 +1041,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Default for HashSet<T, S>
 where
     S: Default,
@@ -1035,7 +1052,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S>
 where
     T: Eq + Hash + Clone,
@@ -1068,7 +1084,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> BitAnd<&HashSet<T, S>> for &HashSet<T, S>
 where
     T: Eq + Hash + Clone,
@@ -1101,7 +1116,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> BitXor<&HashSet<T, S>> for &HashSet<T, S>
 where
     T: Eq + Hash + Clone,
@@ -1134,7 +1148,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S>
 where
     T: Eq + Hash + Clone,
@@ -1173,7 +1186,16 @@
 /// See its documentation for more.
 ///
 /// [`iter`]: HashSet::iter
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+///
+/// let mut iter = a.iter();
+/// ```
 pub struct Iter<'a, K: 'a> {
     base: base::Iter<'a, K>,
 }
@@ -1184,7 +1206,16 @@
 /// (provided by the `IntoIterator` trait). See its documentation for more.
 ///
 /// [`into_iter`]: IntoIterator::into_iter
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+///
+/// let mut iter = a.into_iter();
+/// ```
 pub struct IntoIter<K> {
     base: base::IntoIter<K>,
 }
@@ -1195,7 +1226,16 @@
 /// See its documentation for more.
 ///
 /// [`drain`]: HashSet::drain
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+///
+/// let mut drain = a.drain();
+/// ```
 pub struct Drain<'a, K: 'a> {
     base: base::Drain<'a, K>,
 }
@@ -1205,7 +1245,18 @@
 /// This `struct` is created by the [`drain_filter`] method on [`HashSet`].
 ///
 /// [`drain_filter`]: HashSet::drain_filter
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
+///
+/// # Examples
+///
+/// ```
+/// #![feature(hash_drain_filter)]
+///
+/// use std::collections::HashSet;
+///
+/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+///
+/// let mut drain_filtered = a.drain_filter(|v| v % 2 == 0);
+/// ```
 pub struct DrainFilter<'a, K, F>
 where
     F: FnMut(&K) -> bool,
@@ -1219,7 +1270,17 @@
 /// See its documentation for more.
 ///
 /// [`intersection`]: HashSet::intersection
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
+///
+/// let mut intersection = a.intersection(&b);
+/// ```
 pub struct Intersection<'a, T: 'a, S: 'a> {
     // iterator of the first set
     iter: Iter<'a, T>,
@@ -1233,7 +1294,17 @@
 /// See its documentation for more.
 ///
 /// [`difference`]: HashSet::difference
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
+///
+/// let mut difference = a.difference(&b);
+/// ```
 pub struct Difference<'a, T: 'a, S: 'a> {
     // iterator of the first set
     iter: Iter<'a, T>,
@@ -1247,7 +1318,17 @@
 /// [`HashSet`]. See its documentation for more.
 ///
 /// [`symmetric_difference`]: HashSet::symmetric_difference
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
+///
+/// let mut intersection = a.symmetric_difference(&b);
+/// ```
 pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
     iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
 }
@@ -1258,12 +1339,21 @@
 /// See its documentation for more.
 ///
 /// [`union`]: HashSet::union
-//#[stable(feature = "rust1", since = "1.0.0")]
+///
+/// # Examples
+///
+/// ```
+/// use std::collections::HashSet;
+///
+/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
+/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
+///
+/// let mut union_iter = a.union(&b);
+/// ```
 pub struct Union<'a, T: 'a, S: 'a> {
     iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T, S> IntoIterator for &'a HashSet<T, S> {
     type Item = &'a T;
     type IntoIter = Iter<'a, T>;
@@ -1274,7 +1364,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> IntoIterator for HashSet<T, S> {
     type Item = T;
     type IntoIter = IntoIter<T>;
@@ -1305,14 +1394,13 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K> Clone for Iter<'_, K> {
     #[inline]
     fn clone(&self) -> Self {
         Iter { base: self.base.clone() }
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<'a, K> Iterator for Iter<'a, K> {
     type Item = &'a K;
 
@@ -1325,24 +1413,22 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K> ExactSizeIterator for Iter<'_, K> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K> FusedIterator for Iter<'_, K> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_list().entries(self.clone()).finish()
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<K> Iterator for IntoIter<K> {
     type Item = K;
 
@@ -1355,24 +1441,22 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K> ExactSizeIterator for IntoIter<K> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K> FusedIterator for IntoIter<K> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: fmt::Debug> fmt::Debug for IntoIter<K> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Debug::fmt(&self.base, f)
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, K> Iterator for Drain<'a, K> {
     type Item = K;
 
@@ -1385,24 +1469,22 @@
         self.base.size_hint()
     }
 }
-//#[stable(feature = "rust1", since = "1.0.0")]
+
 impl<K> ExactSizeIterator for Drain<'_, K> {
     #[inline]
     fn len(&self) -> usize {
         self.base.len()
     }
 }
-//#[stable(feature = "fused", since = "1.26.0")]
+
 impl<K> FusedIterator for Drain<'_, K> {}
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Debug::fmt(&self.base, f)
     }
 }
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<K, F> Iterator for DrainFilter<'_, K, F>
 where
     F: FnMut(&K) -> bool,
@@ -1419,20 +1501,17 @@
     }
 }
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<K, F> FusedIterator for DrainFilter<'_, K, F> where F: FnMut(&K) -> bool {}
 
-//#[unstable(feature = "hash_drain_filter", issue = "59618")]
 impl<'a, K, F> fmt::Debug for DrainFilter<'a, K, F>
 where
     F: FnMut(&K) -> bool,
 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("DrainFilter { .. }")
+        f.debug_struct("DrainFilter").finish_non_exhaustive()
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Clone for Intersection<'_, T, S> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1440,7 +1519,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T, S> Iterator for Intersection<'a, T, S>
 where
     T: Eq + Hash,
@@ -1465,7 +1543,6 @@
     }
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<T, S> fmt::Debug for Intersection<'_, T, S>
 where
     T: fmt::Debug + Eq + Hash,
@@ -1476,7 +1553,6 @@
     }
 }
 
-//#[stable(feature = "fused", since = "1.26.0")]
 impl<T, S> FusedIterator for Intersection<'_, T, S>
 where
     T: Eq + Hash,
@@ -1484,7 +1560,6 @@
 {
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Clone for Difference<'_, T, S> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1492,7 +1567,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T, S> Iterator for Difference<'a, T, S>
 where
     T: Eq + Hash,
@@ -1517,7 +1591,6 @@
     }
 }
 
-//#[stable(feature = "fused", since = "1.26.0")]
 impl<T, S> FusedIterator for Difference<'_, T, S>
 where
     T: Eq + Hash,
@@ -1525,7 +1598,6 @@
 {
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<T, S> fmt::Debug for Difference<'_, T, S>
 where
     T: fmt::Debug + Eq + Hash,
@@ -1536,7 +1608,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Clone for SymmetricDifference<'_, T, S> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1544,7 +1615,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
 where
     T: Eq + Hash,
@@ -1562,7 +1632,6 @@
     }
 }
 
-//#[stable(feature = "fused", since = "1.26.0")]
 impl<T, S> FusedIterator for SymmetricDifference<'_, T, S>
 where
     T: Eq + Hash,
@@ -1570,7 +1639,6 @@
 {
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<T, S> fmt::Debug for SymmetricDifference<'_, T, S>
 where
     T: fmt::Debug + Eq + Hash,
@@ -1581,7 +1649,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<T, S> Clone for Union<'_, T, S> {
     #[inline]
     fn clone(&self) -> Self {
@@ -1589,7 +1656,6 @@
     }
 }
 
-//#[stable(feature = "fused", since = "1.26.0")]
 impl<T, S> FusedIterator for Union<'_, T, S>
 where
     T: Eq + Hash,
@@ -1597,7 +1663,6 @@
 {
 }
 
-//#[stable(feature = "std_debug", since = "1.16.0")]
 impl<T, S> fmt::Debug for Union<'_, T, S>
 where
     T: fmt::Debug + Eq + Hash,
@@ -1608,7 +1673,6 @@
     }
 }
 
-//#[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, T, S> Iterator for Union<'a, T, S>
 where
     T: Eq + Hash,
diff --git a/sgx_tstd/src/collections/mod.rs b/sgx_tstd/src/collections/mod.rs
index cd15485..f53e4cc 100644
--- a/sgx_tstd/src/collections/mod.rs
+++ b/sgx_tstd/src/collections/mod.rs
@@ -103,7 +103,7 @@
 //! cost are suffixed with a `~`.
 //!
 //! All amortized costs are for the potential need to resize when capacity is
-//! exhausted. If a resize occurs it will take O(n) time. Our collections never
+//! exhausted. If a resize occurs it will take *O*(*n*) time. Our collections never
 //! automatically shrink, so removal operations aren't amortized. Over a
 //! sufficiently large series of operations, the average cost per operation will
 //! deterministically equal the given cost.
@@ -127,10 +127,10 @@
 //!
 //! For Sets, all operations have the cost of the equivalent Map operation.
 //!
-//! |              | get       | insert   | remove   | predecessor | append |
-//! |--------------|-----------|----------|----------|-------------|--------|
-//! | [`HashMap`]  | O(1)~     | O(1)~*   | O(1)~    | N/A         | N/A    |
-//! | [`BTreeMap`] | O(log n)  | O(log n) | O(log n) | O(log n)    | O(n+m) |
+//! |              | get       | insert    | remove    | range     | append |
+//! |--------------|-----------|-----------|-----------|-----------|--------|
+//! | [`HashMap`]  | O(1)~     | O(1)~*    | O(1)~     | N/A       | N/A    |
+//! | [`BTreeMap`] | O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) | O(n+m) |
 //!
 //! # Correct and Efficient Usage of Collections
 //!
@@ -256,7 +256,7 @@
 //! Iterators also provide a series of *adapter* methods for performing common
 //! threads to sequences. Among the adapters are functional favorites like `map`,
 //! `fold`, `skip` and `take`. Of particular interest to collections is the
-//! `rev` adapter, that reverses any iterator that supports this operation. Most
+//! `rev` adapter, which reverses any iterator that supports this operation. Most
 //! collections provide reversible iterators as the way to iterate over them in
 //! reverse order.
 //!
@@ -413,15 +413,7 @@
 //! assert_eq!(map.keys().next().unwrap().b, "baz");
 //! ```
 //!
-//! [`Vec`]: ../../std/vec/struct.Vec.html
-//! [`HashMap`]: ../../std/collections/struct.HashMap.html
-//! [`VecDeque`]: ../../std/collections/struct.VecDeque.html
-//! [`LinkedList`]: ../../std/collections/struct.LinkedList.html
-//! [`BTreeMap`]: ../../std/collections/struct.BTreeMap.html
-//! [`HashSet`]: ../../std/collections/struct.HashSet.html
-//! [`BTreeSet`]: ../../std/collections/struct.BTreeSet.html
-//! [`BinaryHeap`]: ../../std/collections/struct.BinaryHeap.html
-//! [`IntoIterator`]: ../../std/iter/trait.IntoIterator.html
+//! [`IntoIterator`]: crate::iter::IntoIterator
 
 pub use crate::ops::Bound;
 pub use alloc_crate::collections::{binary_heap, btree_map, btree_set};
@@ -433,6 +425,7 @@
 pub use self::hash_set::HashSet;
 
 pub use alloc_crate::collections::TryReserveError;
+pub use alloc_crate::collections::TryReserveErrorKind;
 
 mod hash;
 
diff --git a/sgx_tstd/src/debug.rs b/sgx_tstd/src/debug.rs
deleted file mode 100644
index 26eabe5..0000000
--- a/sgx_tstd/src/debug.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use sgx_trts::trts::rsgx_abort;
-
-#[no_mangle]
-pub extern "C" fn __assert_fail(
-    __assertion: *const u8,
-    __file: *const u8,
-    __line: u32,
-    __function: *const u8,
-) -> ! {
-    rsgx_abort()
-}
diff --git a/sgx_tstd/src/enclave.rs b/sgx_tstd/src/enclave.rs
index b572b58..1f26a95 100644
--- a/sgx_tstd/src/enclave.rs
+++ b/sgx_tstd/src/enclave.rs
@@ -15,13 +15,13 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::*;
-use sgx_trts::enclave;
-use crate::sync::SgxThreadSpinlock;
-use crate::path::{Path, PathBuf};
-use crate::untrusted::fs;
-use crate::io;
 use core::sync::atomic::{AtomicU64, Ordering};
+use crate::io;
+use crate::path::{Path, PathBuf};
+use crate::sync::SgxThreadSpinlock;
+use crate::untrusted::fs;
+use sgx_trts::enclave;
+use sgx_types::*;
 
 pub use sgx_trts::enclave::SgxThreadPolicy;
 
diff --git a/sgx_tstd/src/env.rs b/sgx_tstd/src/env.rs
index 92e62c0..930ebc9 100644
--- a/sgx_tstd/src/env.rs
+++ b/sgx_tstd/src/env.rs
@@ -16,13 +16,22 @@
 // under the License..
 
 //! Inspection and manipulation of the process's environment.
+//!
+//! This module contains functions to inspect various aspects such as
+//! environment variables, process arguments, the current directory, and various
+//! other important directories.
+//!
+//! There are several functions and structs in this module that have a
+//! counterpart ending in `os`. Those ending in `os` will return an [`OsString`]
+//! and those without will return a [`String`].
 
-use core::fmt;
+#![allow(clippy::needless_doctest_main)]
 use crate::error::Error;
 use crate::ffi::{OsStr, OsString};
+use crate::fmt;
+use crate::io;
 use crate::path::{Path, PathBuf};
 use crate::sys::os as os_imp;
-use crate::io;
 
 /// Returns the current working directory as a [`PathBuf`].
 ///
@@ -34,9 +43,17 @@
 /// * Current directory does not exist.
 /// * There are insufficient permissions to access the current directory.
 ///
-/// [`PathBuf`]: ../../std/path/struct.PathBuf.html
-/// [`Err`]: ../../std/result/enum.Result.html#method.err
+/// # Examples
 ///
+/// ```
+/// use std::env;
+///
+/// fn main() -> std::io::Result<()> {
+///     let path = env::current_dir()?;
+///     println!("The current directory is {}", path.display());
+///     Ok(())
+/// }
+/// ```
 pub fn current_dir() -> io::Result<PathBuf> {
     os_imp::getcwd()
 }
@@ -45,28 +62,34 @@
 ///
 /// Returns an [`Err`] if the operation fails.
 ///
-/// [`Err`]: ../../std/result/enum.Result.html#method.err
+/// # Examples
 ///
+/// ```
+/// use std::env;
+/// use std::path::Path;
+///
+/// let root = Path::new("/");
+/// assert!(env::set_current_dir(&root).is_ok());
+/// println!("Successfully changed working directory to {}!", root.display());
+/// ```
 pub fn set_current_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
     os_imp::chdir(path.as_ref())
 }
 
 /// An iterator over a snapshot of the environment variables of this process.
 ///
-/// This structure is created by the [`std::env::vars`] function. See its
-/// documentation for more.
+/// This structure is created by [`env::vars()`]. See its documentation for more.
 ///
-/// [`std::env::vars`]: fn.vars.html
+/// [`env::vars()`]: vars
 pub struct Vars {
     inner: VarsOs,
 }
 
 /// An iterator over a snapshot of the environment variables of this process.
 ///
-/// This structure is created by the [`std::env::vars_os`] function. See
-/// its documentation for more.
+/// This structure is created by [`env::vars_os()`]. See its documentation for more.
 ///
-/// [`std::env::vars_os`]: fn.vars_os.html
+/// [`env::vars_os()`]: vars_os
 pub struct VarsOs {
     inner: os_imp::Env,
 }
@@ -81,10 +104,22 @@
 /// # Panics
 ///
 /// While iterating, the returned iterator will panic if any key or value in the
-/// environment is not valid unicode. If this is not desired, consider using the
-/// [`env::vars_os`] function.
+/// environment is not valid unicode. If this is not desired, consider using
+/// [`env::vars_os()`].
 ///
-/// [`env::vars_os`]: fn.vars_os.html
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// // We will iterate through the references to the element returned by
+/// // env::vars();
+/// for (key, value) in env::vars() {
+///     println!("{}: {}", key, value);
+/// }
+/// ```
+///
+/// [`env::vars_os()`]: vars_os
 pub fn vars() -> Vars {
     Vars { inner: vars_os() }
 }
@@ -96,6 +131,21 @@
 /// variables at the time of this invocation. Modifications to environment
 /// variables afterwards will not be reflected in the returned iterator.
 ///
+/// Note that the returned iterator will not check if the environment variables
+/// are valid Unicode. If you want to panic on invalid UTF-8,
+/// use the [`vars`] function instead.
+///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// // We will iterate through the references to the element returned by
+/// // env::vars_os();
+/// for (key, value) in env::vars_os() {
+///     println!("{:?}: {:?}", key, value);
+/// }
+/// ```
 pub fn vars_os() -> VarsOs {
     VarsOs { inner: os_imp::env() }
 }
@@ -112,7 +162,7 @@
 
 impl fmt::Debug for Vars {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Vars { .. }")
+        f.debug_struct("Vars").finish_non_exhaustive()
     }
 }
 
@@ -128,7 +178,7 @@
 
 impl fmt::Debug for VarsOs {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("VarsOs { .. }")
+        f.debug_struct("VarOs").finish_non_exhaustive()
     }
 }
 
@@ -136,15 +186,25 @@
 ///
 /// # Errors
 ///
-/// * Environment variable is not present
-/// * Environment variable is not valid unicode
+/// This function will return an error if the environment variable isn't set.
 ///
-/// # Panics
+/// This function may return an error if the environment variable's name contains
+/// the equal sign character (`=`) or the NUL character.
 ///
-/// This function may panic if `key` is empty, contains an ASCII equals sign
-/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
-/// character.
+/// This function will return an error if the environment variable's value is
+/// not valid Unicode. If this is not desired, consider using [`var_os`].
 ///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// let key = "HOME";
+/// match env::var(key) {
+///     Ok(val) => println!("{}: {:?}", key, val),
+///     Err(e) => println!("couldn't interpret {}: {}", key, e),
+/// }
+/// ```
 pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
     _var(key.as_ref())
 }
@@ -157,16 +217,33 @@
 }
 
 /// Fetches the environment variable `key` from the current process, returning
-/// [`None`] if the variable isn't set.
+/// [`None`] if the variable isn't set or there's another error.
 ///
-/// [`None`]: ../option/enum.Option.html#variant.None
+/// Note that the method will not check if the environment variable
+/// is valid Unicode. If you want to have an error on invalid UTF-8,
+/// use the [`var`] function instead.
 ///
-/// # Panics
+/// # Errors
 ///
-/// This function may panic if `key` is empty, contains an ASCII equals sign
-/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
-/// character.
+/// This function returns an error if the environment variable isn't set.
 ///
+/// This function may return an error if the environment variable's name contains
+/// the equal sign character (`=`) or the NUL character.
+///
+/// This function may return an error if the environment variable's value contains
+/// the NUL character.
+///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// let key = "HOME";
+/// match env::var_os(key) {
+///     Some(val) => println!("{}: {:?}", key, val),
+///     None => println!("{} is not defined in the environment.", key)
+/// }
+/// ```
 pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
     _var_os(key.as_ref())
 }
@@ -177,9 +254,9 @@
 }
 
 /// The error type for operations interacting with environment variables.
-/// Possibly returned from the [`env::var`] function.
+/// Possibly returned from [`env::var()`].
 ///
-/// [`env::var`]: fn.var.html
+/// [`env::var()`]: var
 #[derive(Debug, PartialEq, Eq, Clone)]
 pub enum VarError {
     /// The specified environment variable was not present in the current
@@ -204,6 +281,7 @@
 }
 
 impl Error for VarError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         match *self {
             VarError::NotPresent => "environment variable not found",
@@ -212,7 +290,7 @@
     }
 }
 
-/// Sets the environment variable `k` to the value `v` for the currently running
+/// Sets the environment variable `key` to the value `value` for the currently running
 /// process.
 ///
 /// Note that while concurrent access to environment variables is safe in Rust,
@@ -223,22 +301,30 @@
 ///
 /// Discussion of this unsafety on Unix may be found in:
 ///
-///  - [Austin Group Bugzilla](http://austingroupbugs.net/view.php?id=188)
+///  - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
 ///  - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
 ///
 /// # Panics
 ///
-/// This function may panic if `key` is empty, contains an ASCII equals sign
-/// `'='` or the NUL character `'\0'`, or when the value contains the NUL
-/// character.
+/// This function may panic if `key` is empty, contains an ASCII equals sign `'='`
+/// or the NUL character `'\0'`, or when `value` contains the NUL character.
 ///
-pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(k: K, v: V) {
-    _set_var(k.as_ref(), v.as_ref())
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// let key = "KEY";
+/// env::set_var(key, "VALUE");
+/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
+/// ```
+pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
+    _set_var(key.as_ref(), value.as_ref())
 }
 
-fn _set_var(k: &OsStr, v: &OsStr) {
-    os_imp::setenv(k, v).unwrap_or_else(|e| {
-        panic!("failed to set environment variable `{:?}` to `{:?}`: {}", k, v, e)
+fn _set_var(key: &OsStr, value: &OsStr) {
+    os_imp::setenv(key, value).unwrap_or_else(|e| {
+        panic!("failed to set environment variable `{:?}` to `{:?}`: {}", key, value, e)
     })
 }
 
@@ -252,7 +338,7 @@
 ///
 /// Discussion of this unsafety on Unix may be found in:
 ///
-///  - [Austin Group Bugzilla](http://austingroupbugs.net/view.php?id=188)
+///  - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188)
 ///  - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
 ///
 /// # Panics
@@ -261,13 +347,25 @@
 /// `'='` or the NUL character `'\0'`, or when the value contains the NUL
 /// character.
 ///
-pub fn remove_var<K: AsRef<OsStr>>(k: K) {
-    _remove_var(k.as_ref())
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// let key = "KEY";
+/// env::set_var(key, "VALUE");
+/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
+///
+/// env::remove_var(key);
+/// assert!(env::var(key).is_err());
+/// ```
+pub fn remove_var<K: AsRef<OsStr>>(key: K) {
+    _remove_var(key.as_ref())
 }
 
-fn _remove_var(k: &OsStr) {
-    os_imp::unsetenv(k)
-        .unwrap_or_else(|e| panic!("failed to remove environment variable `{:?}`: {}", k, e))
+fn _remove_var(key: &OsStr) {
+    os_imp::unsetenv(key)
+        .unwrap_or_else(|e| panic!("failed to remove environment variable `{:?}`: {}", key, e))
 }
 
 /// An iterator that splits an environment variable into paths according to
@@ -275,11 +373,10 @@
 ///
 /// The iterator element type is [`PathBuf`].
 ///
-/// This structure is created by the [`std::env::split_paths`] function. See its
+/// This structure is created by [`env::split_paths()`]. See its
 /// documentation for more.
 ///
-/// [`PathBuf`]: ../../std/path/struct.PathBuf.html
-/// [`std::env::split_paths`]: fn.split_paths.html
+/// [`env::split_paths()`]: split_paths
 pub struct SplitPaths<'a> {
     inner: os_imp::SplitPaths<'a>,
 }
@@ -290,6 +387,21 @@
 /// Returns an iterator over the paths contained in `unparsed`. The iterator
 /// element type is [`PathBuf`].
 ///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// let key = "PATH";
+/// match env::var_os(key) {
+///     Some(paths) => {
+///         for path in env::split_paths(&paths) {
+///             println!("'{}'", path.display());
+///         }
+///     }
+///     None => println!("{} is not defined in the environment.", key)
+/// }
+/// ```
 pub fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths<'_> {
     SplitPaths { inner: os_imp::split_paths(unparsed.as_ref()) }
 }
@@ -306,14 +418,14 @@
 
 impl fmt::Debug for SplitPaths<'_> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("SplitPaths { .. }")
+        f.debug_struct("SplitPaths").finish_non_exhaustive()
     }
 }
 
 /// The error type for operations on the `PATH` variable. Possibly returned from
-/// the [`env::join_paths`] function.
+/// [`env::join_paths()`].
 ///
-/// [`env::join_paths`]: fn.join_paths.html
+/// [`env::join_paths()`]: join_paths
 #[derive(Debug)]
 pub struct JoinPathsError {
     inner: os_imp::JoinPathsError,
@@ -324,14 +436,62 @@
 ///
 /// # Errors
 ///
-/// Returns an [`Err`][err] (containing an error message) if one of the input
+/// Returns an [`Err`] (containing an error message) if one of the input
 /// [`Path`]s contains an invalid character for constructing the `PATH`
 /// variable (a double quote on Windows or a colon on Unix).
 ///
-/// [`Path`]: ../../std/path/struct.Path.html
-/// [`OsString`]: ../../std/ffi/struct.OsString.html
-/// [err]: ../../std/result/enum.Result.html#variant.Err
+/// # Examples
 ///
+/// Joining paths on a Unix-like platform:
+///
+/// ```
+/// use std::env;
+/// use std::ffi::OsString;
+/// use std::path::Path;
+///
+/// fn main() -> Result<(), env::JoinPathsError> {
+/// # if cfg!(unix) {
+///     let paths = [Path::new("/bin"), Path::new("/usr/bin")];
+///     let path_os_string = env::join_paths(paths.iter())?;
+///     assert_eq!(path_os_string, OsString::from("/bin:/usr/bin"));
+/// # }
+///     Ok(())
+/// }
+/// ```
+///
+/// Joining a path containing a colon on a Unix-like platform results in an
+/// error:
+///
+/// ```
+/// # if cfg!(unix) {
+/// use std::env;
+/// use std::path::Path;
+///
+/// let paths = [Path::new("/bin"), Path::new("/usr/bi:n")];
+/// assert!(env::join_paths(paths.iter()).is_err());
+/// # }
+/// ```
+///
+/// Using `env::join_paths()` with [`env::split_paths()`] to append an item to
+/// the `PATH` environment variable:
+///
+/// ```
+/// use std::env;
+/// use std::path::PathBuf;
+///
+/// fn main() -> Result<(), env::JoinPathsError> {
+///     if let Some(path) = env::var_os("PATH") {
+///         let mut paths = env::split_paths(&path).collect::<Vec<_>>();
+///         paths.push(PathBuf::from("/home/xyz/bin"));
+///         let new_path = env::join_paths(paths)?;
+///         env::set_var("PATH", &new_path);
+///     }
+///
+///     Ok(())
+/// }
+/// ```
+///
+/// [`env::split_paths()`]: split_paths
 pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
 where
     I: IntoIterator<Item = T>,
@@ -347,6 +507,7 @@
 }
 
 impl Error for JoinPathsError {
+    #[allow(deprecated, deprecated_in_future)]
     fn description(&self) -> &str {
         self.inner.description()
     }
@@ -373,12 +534,29 @@
 ///
 /// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/userenv/nf-userenv-getuserprofiledirectorya
 ///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// match env::home_dir() {
+///     Some(path) => println!("Your home directory, probably: {}", path.display()),
+///     None => println!("Impossible to get your home dir!"),
+/// }
+/// ```
 pub fn home_dir() -> Option<PathBuf> {
     os_imp::home_dir()
 }
 
 /// Returns the path of a temporary directory.
 ///
+/// The temporary directory may be shared among users, or between processes
+/// with different privileges; thus, the creation of any files or directories
+/// in the temporary directory must use a secure method to create a uniquely
+/// named file. Creating a file or directory with a fixed or predictable name
+/// may result in "insecure temporary file" security vulnerabilities. Consider
+/// using a crate that securely creates temporary files or directories.
+///
 /// # Unix
 ///
 /// Returns the value of the `TMPDIR` environment variable if it is
@@ -396,6 +574,14 @@
 ///
 /// [msdn]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha
 ///
+/// ```no_run
+/// use std::env;
+///
+/// fn main() {
+///     let mut dir = env::temp_dir();
+///     println!("Temporary directory: {}", dir.display());
+/// }
+/// ```
 pub fn temp_dir() -> PathBuf {
     os_imp::temp_dir()
 }
@@ -408,6 +594,9 @@
 /// return the path of the symbolic link and other platforms will return the
 /// path of the symbolic link’s target.
 ///
+/// If the executable is renamed while it is running, platforms may return the
+/// path at the time it was loaded instead of the new path.
+///
 /// # Errors
 ///
 /// Acquiring the path of the current executable is a platform-specific operation
@@ -452,6 +641,17 @@
 ///
 /// [lead to privilege escalation]: https://securityvulns.com/Wdocument183.html
 ///
+/// # Examples
+///
+/// ```
+/// use std::env;
+///
+/// match env::current_exe() {
+///     Ok(exe_path) => println!("Path of this executable is: {}",
+///                              exe_path.display()),
+///     Err(e) => println!("failed to get current exe path: {}", e),
+/// };
+/// ```
 pub fn current_exe() -> io::Result<PathBuf> {
     os_imp::current_exe()
 }
diff --git a/sgx_tstd/src/error.rs b/sgx_tstd/src/error.rs
index 7fd4d49..d521b84 100644
--- a/sgx_tstd/src/error.rs
+++ b/sgx_tstd/src/error.rs
@@ -15,46 +15,113 @@
 // specific language governing permissions and limitations
 // under the License..
 
+//! Traits for working with Errors.
+
+// A note about crates and the facade:
+//
+// Originally, the `Error` trait was defined in libcore, and the impls
+// were scattered about. However, coherence objected to this
+// arrangement, because to create the blanket impls for `Box` required
+// knowing that `&str: !Error`, and we have no means to deal with that
+// sort of conflict just now. Therefore, for the time being, we have
+// moved the `Error` trait into libstd. As we evolve a sol'n to the
+// coherence challenge (e.g., specialization, neg impls, etc) we can
+// reconsider what crate these items belong in.
+
+use core::array;
+use core::convert::Infallible;
+
+use crate::alloc::{AllocError, LayoutError};
+use crate::any::TypeId;
 #[cfg(feature = "backtrace")]
 use crate::backtrace::Backtrace;
-use core::alloc::{AllocError, LayoutErr};
-use core::array;
-use core::any::TypeId;
-use core::cell;
-use core::char;
-use core::fmt::{self, Debug, Display};
-use core::mem::transmute;
-use core::num;
-use alloc_crate::str;
-use alloc_crate::string::{self, String};
-use alloc_crate::boxed::Box;
-use alloc_crate::borrow::Cow;
-use alloc_crate::collections;
+use crate::borrow::Cow;
+use crate::cell;
+use crate::char;
+use crate::fmt::{self, Debug, Display};
+use crate::mem::transmute;
+use crate::num;
+use crate::str;
+use crate::string;
+use crate::sync::Arc;
+
+use sgx_types::sgx_status_t;
 
 /// `Error` is a trait representing the basic expectations for error values,
-/// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
-/// themselves through the [`Display`] and [`Debug`] traits, and may provide
-/// cause chain information:
+/// i.e., values of type `E` in [`Result<T, E>`].
 ///
-/// The [`source`] method is generally used when errors cross "abstraction
-/// boundaries". If one module must report an error that is caused by an error
-/// from a lower-level module, it can allow access to that error via the
-/// [`source`] method. This makes it possible for the high-level module to
-/// provide its own errors while also revealing some of the implementation for
-/// debugging via [`source`] chains.
+/// Errors must describe themselves through the [`Display`] and [`Debug`]
+/// traits. Error messages are typically concise lowercase sentences without
+/// trailing punctuation:
 ///
-/// [`Result<T, E>`]: ../result/enum.Result.html
-/// [`Display`]: ../fmt/trait.Display.html
-/// [`Debug`]: ../fmt/trait.Debug.html
-/// [`source`]: trait.Error.html#method.source
+/// ```
+/// let err = "NaN".parse::<u32>().unwrap_err();
+/// assert_eq!(err.to_string(), "invalid digit found in string");
+/// ```
+///
+/// Errors may provide cause chain information. [`Error::source()`] is generally
+/// used when errors cross "abstraction boundaries". If one module must report
+/// an error that is caused by an error from a lower-level module, it can allow
+/// accessing that error via [`Error::source()`]. This makes it possible for the
+/// high-level module to provide its own errors while also revealing some of the
+/// implementation for debugging via `source` chains.
 pub trait Error: Debug + Display {
     /// The lower-level source of this error, if any.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::fmt;
+    ///
+    /// #[derive(Debug)]
+    /// struct SuperError {
+    ///     side: SuperErrorSideKick,
+    /// }
+    ///
+    /// impl fmt::Display for SuperError {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f, "SuperError is here!")
+    ///     }
+    /// }
+    ///
+    /// impl Error for SuperError {
+    ///     fn source(&self) -> Option<&(dyn Error + 'static)> {
+    ///         Some(&self.side)
+    ///     }
+    /// }
+    ///
+    /// #[derive(Debug)]
+    /// struct SuperErrorSideKick;
+    ///
+    /// impl fmt::Display for SuperErrorSideKick {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f, "SuperErrorSideKick is here!")
+    ///     }
+    /// }
+    ///
+    /// impl Error for SuperErrorSideKick {}
+    ///
+    /// fn get_super_error() -> Result<(), SuperError> {
+    ///     Err(SuperError { side: SuperErrorSideKick })
+    /// }
+    ///
+    /// fn main() {
+    ///     match get_super_error() {
+    ///         Err(e) => {
+    ///             println!("Error: {}", e);
+    ///             println!("Caused by: {}", e.source().unwrap());
+    ///         }
+    ///         _ => println!("No error"),
+    ///     }
+    /// }
+    /// ```
     fn source(&self) -> Option<&(dyn Error + 'static)> {
         None
     }
 
     /// Gets the `TypeId` of `self`.
+    #[doc(hidden)]
     fn type_id(&self, _: private::Internal) -> TypeId
     where
         Self: 'static,
@@ -86,6 +153,7 @@
         "description() is deprecated; use Display"
     }
 
+    #[allow(missing_docs)]
     fn cause(&self) -> Option<&dyn Error> {
         self.source()
     }
@@ -101,8 +169,29 @@
 impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
     /// Converts a type of [`Error`] into a box of dyn [`Error`].
     ///
-    /// [`Error`]: ../error/trait.Error.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::fmt;
+    /// use std::mem;
+    ///
+    /// #[derive(Debug)]
+    /// struct AnError;
+    ///
+    /// impl fmt::Display for AnError {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f , "An error")
+    ///     }
+    /// }
+    ///
+    /// impl Error for AnError {}
+    ///
+    /// let an_error = AnError;
+    /// assert!(0 == mem::size_of_val(&an_error));
+    /// let a_boxed_error = Box::<dyn Error>::from(an_error);
+    /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(err: E) -> Box<dyn Error + 'a> {
         Box::new(err)
     }
@@ -112,8 +201,34 @@
     /// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of
     /// dyn [`Error`] + [`Send`] + [`Sync`].
     ///
-    /// [`Error`]: ../error/trait.Error.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::fmt;
+    /// use std::mem;
+    ///
+    /// #[derive(Debug)]
+    /// struct AnError;
+    ///
+    /// impl fmt::Display for AnError {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f , "An error")
+    ///     }
+    /// }
+    ///
+    /// impl Error for AnError {}
+    ///
+    /// unsafe impl Send for AnError {}
+    ///
+    /// unsafe impl Sync for AnError {}
+    ///
+    /// let an_error = AnError;
+    /// assert!(0 == mem::size_of_val(&an_error));
+    /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(an_error);
+    /// assert!(
+    ///     mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
         Box::new(err)
     }
@@ -122,10 +237,23 @@
 impl From<String> for Box<dyn Error + Send + Sync> {
     /// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    ///
+    /// let a_string_error = "a string error".to_string();
+    /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_string_error);
+    /// assert!(
+    ///     mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
+    #[inline]
     fn from(err: String) -> Box<dyn Error + Send + Sync> {
         struct StringError(String);
 
         impl Error for StringError {
+            #[allow(deprecated)]
             fn description(&self) -> &str {
                 &self.0
             }
@@ -151,6 +279,16 @@
 impl From<String> for Box<dyn Error> {
     /// Converts a [`String`] into a box of dyn [`Error`].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    ///
+    /// let a_string_error = "a string error".to_string();
+    /// let a_boxed_error = Box::<dyn Error>::from(a_string_error);
+    /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(str_err: String) -> Box<dyn Error> {
         let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
         let err2: Box<dyn Error> = err1;
@@ -161,6 +299,19 @@
 impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
     /// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
     ///
+    /// [`str`]: prim@str
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    ///
+    /// let a_str_error = "a str error";
+    /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_str_error);
+    /// assert!(
+    ///     mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     #[inline]
     fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
         From::from(String::from(err))
@@ -170,6 +321,18 @@
 impl From<&str> for Box<dyn Error> {
     /// Converts a [`str`] into a box of dyn [`Error`].
     ///
+    /// [`str`]: prim@str
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    ///
+    /// let a_str_error = "a str error";
+    /// let a_boxed_error = Box::<dyn Error>::from(a_str_error);
+    /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(err: &str) -> Box<dyn Error> {
         From::from(String::from(err))
     }
@@ -178,9 +341,18 @@
 impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
     /// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
     ///
-    /// [`Cow`]: ../borrow/enum.Cow.html
-    /// [`Error`]: ../error/trait.Error.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    /// use std::borrow::Cow;
+    ///
+    /// let a_cow_str_error = Cow::from("a str error");
+    /// let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error);
+    /// assert!(
+    ///     mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
         From::from(String::from(err))
     }
@@ -189,93 +361,122 @@
 impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
     /// Converts a [`Cow`] into a box of dyn [`Error`].
     ///
-    /// [`Cow`]: ../borrow/enum.Cow.html
-    /// [`Error`]: ../error/trait.Error.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::error::Error;
+    /// use std::mem;
+    /// use std::borrow::Cow;
+    ///
+    /// let a_cow_str_error = Cow::from("a str error");
+    /// let a_boxed_error = Box::<dyn Error>::from(a_cow_str_error);
+    /// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
+    /// ```
     fn from(err: Cow<'a, str>) -> Box<dyn Error> {
         From::from(String::from(err))
     }
 }
 
-impl Error for ! {
-    fn description(&self) -> &str {
-        *self
-    }
-}
+impl Error for ! {}
 
-impl Error for AllocError {
-    fn description(&self) -> &str {
-        "memory allocation failed"
-    }
-}
+impl Error for AllocError {}
 
-impl Error for LayoutErr {
-    fn description(&self) -> &str {
-        "invalid parameters to Layout::from_size_align"
-    }
-}
+impl Error for LayoutError {}
 
 impl Error for str::ParseBoolError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "failed to parse bool"
     }
 }
 
 impl Error for str::Utf8Error {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "invalid utf-8: corrupt contents"
     }
 }
 
 impl Error for num::ParseIntError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         self.__description()
     }
 }
 
 impl Error for num::TryFromIntError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         self.__description()
     }
 }
 
 impl Error for array::TryFromSliceError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         self.__description()
     }
 }
 
 impl Error for num::ParseFloatError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         self.__description()
     }
 }
 
 impl Error for string::FromUtf8Error {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "invalid utf-8"
     }
 }
 
 impl Error for string::FromUtf16Error {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "invalid utf-16"
     }
 }
 
-impl Error for string::ParseError {
+impl Error for Infallible {
     fn description(&self) -> &str {
         match *self {}
     }
 }
 
 impl Error for char::DecodeUtf16Error {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "unpaired surrogate found"
     }
 }
 
+impl<'a, K: Debug + Ord, V: Debug> Error
+    for crate::collections::btree_map::OccupiedError<'a, K, V>
+{
+    #[allow(deprecated)]
+    fn description(&self) -> &str {
+        "key already exists"
+    }
+}
+
+impl<'a, K: Debug, V: Debug> Error for crate::collections::hash_map::OccupiedError<'a, K, V> {
+    #[allow(deprecated)]
+    fn description(&self) -> &str {
+        "key already exists"
+    }
+}
+
+impl Error for sgx_status_t {
+    #[allow(deprecated)]
+    fn description(&self) -> &str {
+        self.__description()
+    }
+}
+
 impl<T: Error> Error for Box<T> {
+    #[allow(deprecated, deprecated_in_future)]
     fn description(&self) -> &str {
         Error::description(&**self)
     }
@@ -290,37 +491,86 @@
     }
 }
 
+impl<'a, T: Error + ?Sized> Error for &'a T {
+    #[allow(deprecated, deprecated_in_future)]
+    fn description(&self) -> &str {
+        Error::description(&**self)
+    }
+
+    #[allow(deprecated)]
+    fn cause(&self) -> Option<&dyn Error> {
+        Error::cause(&**self)
+    }
+
+    fn source(&self) -> Option<&(dyn Error + 'static)> {
+        Error::source(&**self)
+    }
+
+    #[cfg(feature = "backtrace")]
+    fn backtrace(&self) -> Option<&Backtrace> {
+        Error::backtrace(&**self)
+    }
+}
+
+impl<T: Error + ?Sized> Error for Arc<T> {
+    #[allow(deprecated, deprecated_in_future)]
+    fn description(&self) -> &str {
+        Error::description(&**self)
+    }
+
+    #[allow(deprecated)]
+    fn cause(&self) -> Option<&dyn Error> {
+        Error::cause(&**self)
+    }
+
+    fn source(&self) -> Option<&(dyn Error + 'static)> {
+        Error::source(&**self)
+    }
+
+    #[cfg(feature = "backtrace")]
+    fn backtrace(&self) -> Option<&Backtrace> {
+        Error::backtrace(&**self)
+    }
+}
+
 impl Error for fmt::Error {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "an error occurred when formatting an argument"
     }
 }
 
 impl Error for cell::BorrowError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "already mutably borrowed"
     }
 }
 
 impl Error for cell::BorrowMutError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "already borrowed"
     }
 }
 
 impl Error for char::CharTryFromError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "converted integer out of range for `char`"
     }
 }
 
 impl Error for char::ParseCharError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         self.__description()
     }
 }
 
-impl Error for collections::TryReserveError {}
+impl Error for alloc_crate::collections::TryReserveError {}
+
+impl Error for core::time::FromSecsError {}
 
 // Copied from `any.rs`.
 impl dyn Error + 'static {
@@ -415,43 +665,66 @@
     }
 
     /// Returns an iterator starting with the current error and continuing with
-    /// recursively calling [`source`].
+    /// recursively calling [`Error::source`].
     ///
     /// If you want to omit the current error and only use its sources,
     /// use `skip(1)`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(error_iter)]
+    /// use std::error::Error;
+    /// use std::fmt;
+    ///
+    /// #[derive(Debug)]
+    /// struct A;
+    ///
+    /// #[derive(Debug)]
+    /// struct B(Option<Box<dyn Error + 'static>>);
+    ///
+    /// impl fmt::Display for A {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f, "A")
+    ///     }
+    /// }
+    ///
+    /// impl fmt::Display for B {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f, "B")
+    ///     }
+    /// }
+    ///
+    /// impl Error for A {}
+    ///
+    /// impl Error for B {
+    ///     fn source(&self) -> Option<&(dyn Error + 'static)> {
+    ///         self.0.as_ref().map(|e| e.as_ref())
+    ///     }
+    /// }
+    ///
+    /// let b = B(Some(Box::new(A)));
+    ///
+    /// // let err : Box<Error> = b.into(); // or
+    /// let err = &b as &(dyn Error);
+    ///
+    /// let mut iter = err.chain();
+    ///
+    /// assert_eq!("B".to_string(), iter.next().unwrap().to_string());
+    /// assert_eq!("A".to_string(), iter.next().unwrap().to_string());
+    /// assert!(iter.next().is_none());
+    /// assert!(iter.next().is_none());
+    /// ```
     #[inline]
     pub fn chain(&self) -> Chain<'_> {
         Chain { current: Some(self) }
     }
-
-    /// Returns an iterator starting with the current error and continuing with
-    /// recursively calling [`source`].
-    ///
-    #[inline]
-    pub fn iter_chain(&self) -> ErrorIter<'_> {
-        ErrorIter {
-            current: Some(self),
-        }
-    }
-
-    /// Returns an iterator starting with the [`source`] of this error
-    /// and continuing with recursively calling [`source`].
-    ///
-    #[inline]
-    pub fn iter_sources(&self) -> ErrorIter<'_> {
-        ErrorIter {
-            current: self.source(),
-        }
-    }
 }
 
 /// An iterator over an [`Error`] and its sources.
 ///
 /// If you want to omit the initial error and only process
 /// its sources, use `skip(1)`.
-///
-/// [`Error`]: trait.Error.html
 #[derive(Clone, Debug)]
 pub struct Chain<'a> {
     current: Option<&'a (dyn Error + 'static)>,
@@ -467,24 +740,6 @@
     }
 }
 
-/// An iterator over an [`Error`] and its sources.
-///
-/// [`Error`]: trait.Error.html
-#[derive(Copy, Clone, Debug)]
-pub struct ErrorIter<'a> {
-    current: Option<&'a (dyn Error + 'static)>,
-}
-
-impl<'a> Iterator for ErrorIter<'a> {
-    type Item = &'a (dyn Error + 'static);
-
-    fn next(&mut self) -> Option<Self::Item> {
-        let current = self.current;
-        self.current = self.current.and_then(Error::source);
-        current
-    }
-}
-
 impl dyn Error + Send {
     #[inline]
     /// Attempts to downcast the box to a concrete type.
@@ -508,4 +763,3 @@
         })
     }
 }
-
diff --git a/sgx_tstd/src/f32.rs b/sgx_tstd/src/f32.rs
index 9b4fd12..813b10e 100644
--- a/sgx_tstd/src/f32.rs
+++ b/sgx_tstd/src/f32.rs
@@ -15,31 +15,44 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! This module provides constants which are specific to the implementation
-//! of the `f32` floating point data type.
+//! Constants specific to the `f32` single-precision floating point type.
 //!
-//! *[See also the `f32` primitive type](../../std/primitive.f32.html).*
+//! *[See also the `f32` primitive type](primitive@f32).*
 //!
 //! Mathematically significant numbers are provided in the `consts` sub-module.
 //!
-//! Although using these constants won't cause compilation warnings,
-//! new code should use the associated constants directly on the primitive type.
+//! For the constants defined directly in this module
+//! (as distinct from those defined in the `consts` sub-module),
+//! new code should instead use the associated constants
+//! defined directly on the `f32` type.
 
 #![allow(missing_docs)]
 
-use core::intrinsics;
+use crate::intrinsics;
 use crate::sys::cmath;
 
-pub use core::f32::consts;
-pub use core::f32::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX};
-pub use core::f32::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY};
-pub use core::f32::{MAX, MIN, MIN_POSITIVE};
-pub use core::f32::{MAX_EXP, MIN_10_EXP, MIN_EXP};
+#[allow(deprecated, deprecated_in_future)]
+pub use core::f32::{
+    consts, DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP,
+    MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX,
+};
 
 #[lang = "f32_runtime"]
 impl f32 {
     /// Returns the largest integer less than or equal to a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.7_f32;
+    /// let g = 3.0_f32;
+    /// let h = -3.7_f32;
+    ///
+    /// assert_eq!(f.floor(), 3.0);
+    /// assert_eq!(g.floor(), 3.0);
+    /// assert_eq!(h.floor(), -4.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn floor(self) -> f32 {
         unsafe { intrinsics::floorf32(self) }
@@ -47,6 +60,16 @@
 
     /// Returns the smallest integer greater than or equal to a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.01_f32;
+    /// let g = 4.0_f32;
+    ///
+    /// assert_eq!(f.ceil(), 4.0);
+    /// assert_eq!(g.ceil(), 4.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ceil(self) -> f32 {
         unsafe { intrinsics::ceilf32(self) }
@@ -55,6 +78,16 @@
     /// Returns the nearest integer to a number. Round half-way cases away from
     /// `0.0`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.3_f32;
+    /// let g = -3.3_f32;
+    ///
+    /// assert_eq!(f.round(), 3.0);
+    /// assert_eq!(g.round(), -3.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn round(self) -> f32 {
         unsafe { intrinsics::roundf32(self) }
@@ -62,6 +95,18 @@
 
     /// Returns the integer part of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.7_f32;
+    /// let g = 3.0_f32;
+    /// let h = -3.7_f32;
+    ///
+    /// assert_eq!(f.trunc(), 3.0);
+    /// assert_eq!(g.trunc(), 3.0);
+    /// assert_eq!(h.trunc(), -3.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn trunc(self) -> f32 {
         unsafe { intrinsics::truncf32(self) }
@@ -69,6 +114,18 @@
 
     /// Returns the fractional part of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.6_f32;
+    /// let y = -3.6_f32;
+    /// let abs_difference_x = (x.fract() - 0.6).abs();
+    /// let abs_difference_y = (y.fract() - (-0.6)).abs();
+    ///
+    /// assert!(abs_difference_x <= f32::EPSILON);
+    /// assert!(abs_difference_y <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn fract(self) -> f32 {
         self - self.trunc()
@@ -77,6 +134,21 @@
     /// Computes the absolute value of `self`. Returns `NAN` if the
     /// number is `NAN`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.5_f32;
+    /// let y = -3.5_f32;
+    ///
+    /// let abs_difference_x = (x.abs() - x).abs();
+    /// let abs_difference_y = (y.abs() - (-y)).abs();
+    ///
+    /// assert!(abs_difference_x <= f32::EPSILON);
+    /// assert!(abs_difference_y <= f32::EPSILON);
+    ///
+    /// assert!(f32::NAN.abs().is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn abs(self) -> f32 {
         unsafe { intrinsics::fabsf32(self) }
@@ -88,9 +160,20 @@
     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
     /// - `NAN` if the number is `NAN`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.5_f32;
+    ///
+    /// assert_eq!(f.signum(), 1.0);
+    /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
+    ///
+    /// assert!(f32::NAN.signum().is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn signum(self) -> f32 {
-        if self.is_nan() { NAN } else { 1.0_f32.copysign(self) }
+        if self.is_nan() { Self::NAN } else { 1.0_f32.copysign(self) }
     }
 
     /// Returns a number composed of the magnitude of `self` and the sign of
@@ -100,6 +183,19 @@
     /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
     /// `sign` is returned.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.5_f32;
+    ///
+    /// assert_eq!(f.copysign(0.42), 3.5_f32);
+    /// assert_eq!(f.copysign(-0.42), -3.5_f32);
+    /// assert_eq!((-f).copysign(0.42), 3.5_f32);
+    /// assert_eq!((-f).copysign(-0.42), -3.5_f32);
+    ///
+    /// assert!(f32::NAN.copysign(1.0).is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn copysign(self, sign: f32) -> f32 {
         unsafe { intrinsics::copysignf32(self, sign) }
@@ -108,9 +204,24 @@
     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
     /// error, yielding a more accurate result than an unfused multiply-add.
     ///
-    /// Using `mul_add` can be more performant than an unfused multiply-add if
-    /// the target architecture has a dedicated `fma` CPU instruction.
+    /// Using `mul_add` *may* be more performant than an unfused multiply-add if
+    /// the target architecture has a dedicated `fma` CPU instruction. However,
+    /// this is not always true, and will be heavily dependant on designing
+    /// algorithms with specific target hardware in mind.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let m = 10.0_f32;
+    /// let x = 4.0_f32;
+    /// let b = 60.0_f32;
+    ///
+    /// // 100.0
+    /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn mul_add(self, a: f32, b: f32) -> f32 {
         unsafe { intrinsics::fmaf32(self, a, b) }
@@ -123,6 +234,17 @@
     /// In other words, the result is `self / rhs` rounded to the integer `n`
     /// such that `self >= n * rhs`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let a: f32 = 7.0;
+    /// let b = 4.0;
+    /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
+    /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
+    /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
+    /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn div_euclid(self, rhs: f32) -> f32 {
         let q = (self / rhs).trunc();
@@ -143,6 +265,19 @@
     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
     /// approximatively.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let a: f32 = 7.0;
+    /// let b = 4.0;
+    /// assert_eq!(a.rem_euclid(b), 3.0);
+    /// assert_eq!((-a).rem_euclid(b), 1.0);
+    /// assert_eq!(a.rem_euclid(-b), 3.0);
+    /// assert_eq!((-a).rem_euclid(-b), 1.0);
+    /// // limitation due to round-off error
+    /// assert!((-f32::EPSILON).rem_euclid(3.0) != 0.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn rem_euclid(self, rhs: f32) -> f32 {
         let r = self % rhs;
@@ -153,6 +288,15 @@
     ///
     /// Using this function is generally faster than using `powf`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0_f32;
+    /// let abs_difference = (x.powi(2) - (x * x)).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn powi(self, n: i32) -> f32 {
         unsafe { intrinsics::powif32(self, n) }
@@ -160,6 +304,15 @@
 
     /// Raises a number to a floating point power.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0_f32;
+    /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn powf(self, n: f32) -> f32 {
         unsafe { intrinsics::powf32(self, n) }
@@ -167,8 +320,22 @@
 
     /// Returns the square root of a number.
     ///
-    /// Returns NaN if `self` is a negative number.
+    /// Returns NaN if `self` is a negative number other than `-0.0`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let positive = 4.0_f32;
+    /// let negative = -4.0_f32;
+    /// let negative_zero = -0.0_f32;
+    ///
+    /// let abs_difference = (positive.sqrt() - 2.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// assert!(negative.sqrt().is_nan());
+    /// assert!(negative_zero.sqrt() == negative_zero);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sqrt(self) -> f32 {
         unsafe { intrinsics::sqrtf32(self) }
@@ -176,6 +343,19 @@
 
     /// Returns `e^(self)`, (the exponential function).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let one = 1.0f32;
+    /// // e^1
+    /// let e = one.exp();
+    ///
+    /// // ln(e) - 1 == 0
+    /// let abs_difference = (e.ln() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp(self) -> f32 {
         unsafe { intrinsics::expf32(self) }
@@ -183,6 +363,17 @@
 
     /// Returns `2^(self)`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 2.0f32;
+    ///
+    /// // 2^2 - 4 == 0
+    /// let abs_difference = (f.exp2() - 4.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp2(self) -> f32 {
         unsafe { intrinsics::exp2f32(self) }
@@ -190,6 +381,19 @@
 
     /// Returns the natural logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let one = 1.0f32;
+    /// // e^1
+    /// let e = one.exp();
+    ///
+    /// // ln(e) - 1 == 0
+    /// let abs_difference = (e.ln() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ln(self) -> f32 {
         unsafe { intrinsics::logf32(self) }
@@ -197,10 +401,21 @@
 
     /// Returns the logarithm of the number with respect to an arbitrary base.
     ///
-    /// The result may not be correctly rounded owing to implementation details;
+    /// The result might not be correctly rounded owing to implementation details;
     /// `self.log2()` can produce more accurate results for base 2, and
     /// `self.log10()` can produce more accurate results for base 10.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let five = 5.0f32;
+    ///
+    /// // log5(5) - 1 == 0
+    /// let abs_difference = (five.log(5.0) - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log(self, base: f32) -> f32 {
         self.ln() / base.ln()
@@ -208,6 +423,17 @@
 
     /// Returns the base 2 logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let two = 2.0f32;
+    ///
+    /// // log2(2) - 1 == 0
+    /// let abs_difference = (two.log2() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log2(self) -> f32 {
         #[cfg(target_os = "android")]
@@ -218,6 +444,17 @@
 
     /// Returns the base 10 logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let ten = 10.0f32;
+    ///
+    /// // log10(10) - 1 == 0
+    /// let abs_difference = (ten.log10() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log10(self) -> f32 {
         unsafe { intrinsics::log10f32(self) }
@@ -228,13 +465,37 @@
     /// * If `self <= other`: `0:0`
     /// * Else: `self - other`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.0f32;
+    /// let y = -3.0f32;
+    ///
+    /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
+    /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
+    ///
+    /// assert!(abs_difference_x <= f32::EPSILON);
+    /// assert!(abs_difference_y <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn abs_sub(self, other: f32) -> f32 {
         unsafe { cmath::fdimf(self, other) }
     }
 
-    /// Returns the cubic root of a number.
+    /// Returns the cube root of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 8.0f32;
+    ///
+    /// // x^(1/3) - 2 == 0
+    /// let abs_difference = (x.cbrt() - 2.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cbrt(self) -> f32 {
         unsafe { cmath::cbrtf(self) }
@@ -243,6 +504,18 @@
     /// Calculates the length of the hypotenuse of a right-angle triangle given
     /// legs of length `x` and `y`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0f32;
+    /// let y = 3.0f32;
+    ///
+    /// // sqrt(x^2 + y^2)
+    /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn hypot(self, other: f32) -> f32 {
         unsafe { cmath::hypotf(self, other) }
@@ -250,6 +523,16 @@
 
     /// Computes the sine of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f32::consts::FRAC_PI_2;
+    ///
+    /// let abs_difference = (x.sin() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sin(self) -> f32 {
         unsafe { intrinsics::sinf32(self) }
@@ -257,6 +540,16 @@
 
     /// Computes the cosine of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0 * std::f32::consts::PI;
+    ///
+    /// let abs_difference = (x.cos() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cos(self) -> f32 {
         unsafe { intrinsics::cosf32(self) }
@@ -264,6 +557,15 @@
 
     /// Computes the tangent of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f32::consts::FRAC_PI_4;
+    /// let abs_difference = (x.tan() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn tan(self) -> f32 {
         unsafe { cmath::tanf(self) }
@@ -273,6 +575,17 @@
     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
     /// [-1, 1].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = std::f32::consts::FRAC_PI_2;
+    ///
+    /// // asin(sin(pi/2))
+    /// let abs_difference = (f.sin().asin() - std::f32::consts::FRAC_PI_2).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn asin(self) -> f32 {
         unsafe { cmath::asinf(self) }
@@ -282,6 +595,17 @@
     /// the range [0, pi] or NaN if the number is outside the range
     /// [-1, 1].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = std::f32::consts::FRAC_PI_4;
+    ///
+    /// // acos(cos(pi/4))
+    /// let abs_difference = (f.cos().acos() - std::f32::consts::FRAC_PI_4).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn acos(self) -> f32 {
         unsafe { cmath::acosf(self) }
@@ -290,6 +614,17 @@
     /// Computes the arctangent of a number. Return value is in radians in the
     /// range [-pi/2, pi/2];
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 1.0f32;
+    ///
+    /// // atan(tan(1))
+    /// let abs_difference = (f.tan().atan() - 1.0).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atan(self) -> f32 {
         unsafe { cmath::atanf(self) }
@@ -302,6 +637,26 @@
     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Positive angles measured counter-clockwise
+    /// // from positive x axis
+    /// // -pi/4 radians (45 deg clockwise)
+    /// let x1 = 3.0f32;
+    /// let y1 = -3.0f32;
+    ///
+    /// // 3pi/4 radians (135 deg counter-clockwise)
+    /// let x2 = -3.0f32;
+    /// let y2 = 3.0f32;
+    ///
+    /// let abs_difference_1 = (y1.atan2(x1) - (-std::f32::consts::FRAC_PI_4)).abs();
+    /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f32::consts::FRAC_PI_4)).abs();
+    ///
+    /// assert!(abs_difference_1 <= f32::EPSILON);
+    /// assert!(abs_difference_2 <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atan2(self, other: f32) -> f32 {
         unsafe { cmath::atan2f(self, other) }
@@ -310,6 +665,18 @@
     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
     /// `(sin(x), cos(x))`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f32::consts::FRAC_PI_4;
+    /// let f = x.sin_cos();
+    ///
+    /// let abs_difference_0 = (f.0 - x.sin()).abs();
+    /// let abs_difference_1 = (f.1 - x.cos()).abs();
+    ///
+    /// assert!(abs_difference_0 <= f32::EPSILON);
+    /// assert!(abs_difference_1 <= f32::EPSILON);
+    /// ```
     #[inline]
     pub fn sin_cos(self) -> (f32, f32) {
         (self.sin(), self.cos())
@@ -318,6 +685,18 @@
     /// Returns `e^(self) - 1` in a way that is accurate even if the
     /// number is close to zero.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1e-8_f32;
+    ///
+    /// // for very small x, e^x is approximately 1 + x + x^2 / 2
+    /// let approx = x + x * x / 2.0;
+    /// let abs_difference = (x.exp_m1() - approx).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp_m1(self) -> f32 {
         unsafe { cmath::expm1f(self) }
@@ -326,6 +705,18 @@
     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
     /// the operations were performed separately.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1e-8_f32;
+    ///
+    /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
+    /// let approx = x - x * x / 2.0;
+    /// let abs_difference = (x.ln_1p() - approx).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ln_1p(self) -> f32 {
         unsafe { cmath::log1pf(self) }
@@ -333,6 +724,20 @@
 
     /// Hyperbolic sine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f32::consts::E;
+    /// let x = 1.0f32;
+    ///
+    /// let f = x.sinh();
+    /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
+    /// let g = ((e * e) - 1.0) / (2.0 * e);
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sinh(self) -> f32 {
         unsafe { cmath::sinhf(self) }
@@ -340,6 +745,20 @@
 
     /// Hyperbolic cosine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f32::consts::E;
+    /// let x = 1.0f32;
+    /// let f = x.cosh();
+    /// // Solving cosh() at 1 gives this result
+    /// let g = ((e * e) + 1.0) / (2.0 * e);
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// // Same result
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cosh(self) -> f32 {
         unsafe { cmath::coshf(self) }
@@ -347,6 +766,20 @@
 
     /// Hyperbolic tangent function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f32::consts::E;
+    /// let x = 1.0f32;
+    ///
+    /// let f = x.tanh();
+    /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
+    /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn tanh(self) -> f32 {
         unsafe { cmath::tanhf(self) }
@@ -354,51 +787,91 @@
 
     /// Inverse hyperbolic sine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1.0f32;
+    /// let f = x.sinh().asinh();
+    ///
+    /// let abs_difference = (f - x).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn asinh(self) -> f32 {
-        if self == NEG_INFINITY {
-            NEG_INFINITY
-        } else {
-            (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
-        }
+        (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
     }
 
     /// Inverse hyperbolic cosine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1.0f32;
+    /// let f = x.cosh().acosh();
+    ///
+    /// let abs_difference = (f - x).abs();
+    ///
+    /// assert!(abs_difference <= f32::EPSILON);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn acosh(self) -> f32 {
-        if self < 1.0 { crate::f32::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
+        if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
     }
 
     /// Inverse hyperbolic tangent function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f32::consts::E;
+    /// let f = e.tanh().atanh();
+    ///
+    /// let abs_difference = (f - e).abs();
+    ///
+    /// assert!(abs_difference <= 1e-5);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atanh(self) -> f32 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
 
-    /// Restrict a value to a certain interval unless it is NaN.
+    /// Linear interpolation between `start` and `end`.
     ///
-    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
-    /// less than `min`. Otherwise this returns `self`.
+    /// This enables linear interpolation between `start` and `end`, where start is represented by
+    /// `self == 0.0` and `end` is represented by `self == 1.0`. This is the basis of all
+    /// "transition", "easing", or "step" functions; if you change `self` from 0.0 to 1.0
+    /// at a given rate, the result will change from `start` to `end` at a similar rate.
     ///
-    /// Not that this function returns NaN if the initial value was NaN as
-    /// well.
+    /// Values below 0.0 or above 1.0 are allowed, allowing you to extrapolate values outside the
+    /// range from `start` to `end`. This also is useful for transition functions which might
+    /// move slightly past the end or start for a desired effect. Mathematically, the values
+    /// returned are equivalent to `start + self * (end - start)`, although we make a few specific
+    /// guarantees that are useful specifically to linear interpolation.
     ///
-    /// # Panics
+    /// These guarantees are:
     ///
-    /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
+    /// * If `start` and `end` are [finite], the value at 0.0 is always `start` and the
+    ///   value at 1.0 is always `end`. (exactness)
+    /// * If `start` and `end` are [finite], the values will always move in the direction from
+    ///   `start` to `end` (monotonicity)
+    /// * If `self` is [finite] and `start == end`, the value at any point will always be
+    ///   `start == end`. (consistency)
     ///
-    #[inline]
-    pub fn clamp(self, min: f32, max: f32) -> f32 {
-        assert!(min <= max);
-        let mut x = self;
-        if x < min {
-            x = min;
+    /// [finite]: #method.is_finite
+    #[must_use = "method returns a new number and does not mutate the original value"]
+    #[allow(clippy::float_cmp)]
+    pub fn lerp(self, start: f32, end: f32) -> f32 {
+        // consistent
+        if start == end {
+            start
+
+        // exact/monotonic
+        } else {
+            self.mul_add(end, (-self).mul_add(start, start))
         }
-        if x > max {
-            x = max;
-        }
-        x
     }
 }
diff --git a/sgx_tstd/src/f64.rs b/sgx_tstd/src/f64.rs
index 6f4c8e9..cc67759 100644
--- a/sgx_tstd/src/f64.rs
+++ b/sgx_tstd/src/f64.rs
@@ -15,31 +15,44 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! This module provides constants which are specific to the implementation
-//! of the `f64` floating point data type.
+//! Constants specific to the `f64` double-precision floating point type.
 //!
-//! *[See also the `f64` primitive type](../../std/primitive.f64.html).*
+//! *[See also the `f64` primitive type](primitive@f64).*
 //!
 //! Mathematically significant numbers are provided in the `consts` sub-module.
 //!
-//! Although using these constants won’t cause compilation warnings,
-//! new code should use the associated constants directly on the primitive type.
+//! For the constants defined directly in this module
+//! (as distinct from those defined in the `consts` sub-module),
+//! new code should instead use the associated constants
+//! defined directly on the `f64` type.
 
 #![allow(missing_docs)]
 
-use core::intrinsics;
+use crate::intrinsics;
 use crate::sys::cmath;
 
-pub use core::f64::consts;
-pub use core::f64::{DIGITS, EPSILON, MANTISSA_DIGITS, RADIX};
-pub use core::f64::{INFINITY, MAX_10_EXP, NAN, NEG_INFINITY};
-pub use core::f64::{MAX, MIN, MIN_POSITIVE};
-pub use core::f64::{MAX_EXP, MIN_10_EXP, MIN_EXP};
+#[allow(deprecated, deprecated_in_future)]
+pub use core::f64::{
+    consts, DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP,
+    MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX,
+};
 
 #[lang = "f64_runtime"]
 impl f64 {
     /// Returns the largest integer less than or equal to a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.7_f64;
+    /// let g = 3.0_f64;
+    /// let h = -3.7_f64;
+    ///
+    /// assert_eq!(f.floor(), 3.0);
+    /// assert_eq!(g.floor(), 3.0);
+    /// assert_eq!(h.floor(), -4.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn floor(self) -> f64 {
         unsafe { intrinsics::floorf64(self) }
@@ -47,6 +60,16 @@
 
     /// Returns the smallest integer greater than or equal to a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.01_f64;
+    /// let g = 4.0_f64;
+    ///
+    /// assert_eq!(f.ceil(), 4.0);
+    /// assert_eq!(g.ceil(), 4.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ceil(self) -> f64 {
         unsafe { intrinsics::ceilf64(self) }
@@ -55,6 +78,16 @@
     /// Returns the nearest integer to a number. Round half-way cases away from
     /// `0.0`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.3_f64;
+    /// let g = -3.3_f64;
+    ///
+    /// assert_eq!(f.round(), 3.0);
+    /// assert_eq!(g.round(), -3.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn round(self) -> f64 {
         unsafe { intrinsics::roundf64(self) }
@@ -62,6 +95,18 @@
 
     /// Returns the integer part of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.7_f64;
+    /// let g = 3.0_f64;
+    /// let h = -3.7_f64;
+    ///
+    /// assert_eq!(f.trunc(), 3.0);
+    /// assert_eq!(g.trunc(), 3.0);
+    /// assert_eq!(h.trunc(), -3.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn trunc(self) -> f64 {
         unsafe { intrinsics::truncf64(self) }
@@ -69,6 +114,18 @@
 
     /// Returns the fractional part of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.6_f64;
+    /// let y = -3.6_f64;
+    /// let abs_difference_x = (x.fract() - 0.6).abs();
+    /// let abs_difference_y = (y.fract() - (-0.6)).abs();
+    ///
+    /// assert!(abs_difference_x < 1e-10);
+    /// assert!(abs_difference_y < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn fract(self) -> f64 {
         self - self.trunc()
@@ -77,6 +134,21 @@
     /// Computes the absolute value of `self`. Returns `NAN` if the
     /// number is `NAN`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.5_f64;
+    /// let y = -3.5_f64;
+    ///
+    /// let abs_difference_x = (x.abs() - x).abs();
+    /// let abs_difference_y = (y.abs() - (-y)).abs();
+    ///
+    /// assert!(abs_difference_x < 1e-10);
+    /// assert!(abs_difference_y < 1e-10);
+    ///
+    /// assert!(f64::NAN.abs().is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn abs(self) -> f64 {
         unsafe { intrinsics::fabsf64(self) }
@@ -88,9 +160,20 @@
     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
     /// - `NAN` if the number is `NAN`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.5_f64;
+    ///
+    /// assert_eq!(f.signum(), 1.0);
+    /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0);
+    ///
+    /// assert!(f64::NAN.signum().is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn signum(self) -> f64 {
-        if self.is_nan() { NAN } else { 1.0_f64.copysign(self) }
+        if self.is_nan() { Self::NAN } else { 1.0_f64.copysign(self) }
     }
 
     /// Returns a number composed of the magnitude of `self` and the sign of
@@ -100,6 +183,19 @@
     /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of
     /// `sign` is returned.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 3.5_f64;
+    ///
+    /// assert_eq!(f.copysign(0.42), 3.5_f64);
+    /// assert_eq!(f.copysign(-0.42), -3.5_f64);
+    /// assert_eq!((-f).copysign(0.42), 3.5_f64);
+    /// assert_eq!((-f).copysign(-0.42), -3.5_f64);
+    ///
+    /// assert!(f64::NAN.copysign(1.0).is_nan());
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn copysign(self, sign: f64) -> f64 {
         unsafe { intrinsics::copysignf64(self, sign) }
@@ -108,9 +204,24 @@
     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
     /// error, yielding a more accurate result than an unfused multiply-add.
     ///
-    /// Using `mul_add` can be more performant than an unfused multiply-add if
-    /// the target architecture has a dedicated `fma` CPU instruction.
+    /// Using `mul_add` *may* be more performant than an unfused multiply-add if
+    /// the target architecture has a dedicated `fma` CPU instruction. However,
+    /// this is not always true, and will be heavily dependant on designing
+    /// algorithms with specific target hardware in mind.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let m = 10.0_f64;
+    /// let x = 4.0_f64;
+    /// let b = 60.0_f64;
+    ///
+    /// // 100.0
+    /// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn mul_add(self, a: f64, b: f64) -> f64 {
         unsafe { intrinsics::fmaf64(self, a, b) }
@@ -123,6 +234,17 @@
     /// In other words, the result is `self / rhs` rounded to the integer `n`
     /// such that `self >= n * rhs`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let a: f64 = 7.0;
+    /// let b = 4.0;
+    /// assert_eq!(a.div_euclid(b), 1.0); // 7.0 > 4.0 * 1.0
+    /// assert_eq!((-a).div_euclid(b), -2.0); // -7.0 >= 4.0 * -2.0
+    /// assert_eq!(a.div_euclid(-b), -1.0); // 7.0 >= -4.0 * -1.0
+    /// assert_eq!((-a).div_euclid(-b), 2.0); // -7.0 >= -4.0 * 2.0
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn div_euclid(self, rhs: f64) -> f64 {
         let q = (self / rhs).trunc();
@@ -143,6 +265,19 @@
     /// property `self == self.div_euclid(rhs) * rhs + self.rem_euclid(rhs)`
     /// approximatively.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let a: f64 = 7.0;
+    /// let b = 4.0;
+    /// assert_eq!(a.rem_euclid(b), 3.0);
+    /// assert_eq!((-a).rem_euclid(b), 1.0);
+    /// assert_eq!(a.rem_euclid(-b), 3.0);
+    /// assert_eq!((-a).rem_euclid(-b), 1.0);
+    /// // limitation due to round-off error
+    /// assert!((-f64::EPSILON).rem_euclid(3.0) != 0.0);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn rem_euclid(self, rhs: f64) -> f64 {
         let r = self % rhs;
@@ -153,6 +288,15 @@
     ///
     /// Using this function is generally faster than using `powf`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0_f64;
+    /// let abs_difference = (x.powi(2) - (x * x)).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn powi(self, n: i32) -> f64 {
         unsafe { intrinsics::powif64(self, n) }
@@ -160,6 +304,15 @@
 
     /// Raises a number to a floating point power.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0_f64;
+    /// let abs_difference = (x.powf(2.0) - (x * x)).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn powf(self, n: f64) -> f64 {
         unsafe { intrinsics::powf64(self, n) }
@@ -167,8 +320,22 @@
 
     /// Returns the square root of a number.
     ///
-    /// Returns NaN if `self` is a negative number.
+    /// Returns NaN if `self` is a negative number other than `-0.0`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let positive = 4.0_f64;
+    /// let negative = -4.0_f64;
+    /// let negative_zero = -0.0_f64;
+    ///
+    /// let abs_difference = (positive.sqrt() - 2.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// assert!(negative.sqrt().is_nan());
+    /// assert!(negative_zero.sqrt() == negative_zero);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sqrt(self) -> f64 {
         unsafe { intrinsics::sqrtf64(self) }
@@ -176,6 +343,19 @@
 
     /// Returns `e^(self)`, (the exponential function).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let one = 1.0_f64;
+    /// // e^1
+    /// let e = one.exp();
+    ///
+    /// // ln(e) - 1 == 0
+    /// let abs_difference = (e.ln() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp(self) -> f64 {
         unsafe { intrinsics::expf64(self) }
@@ -183,6 +363,17 @@
 
     /// Returns `2^(self)`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 2.0_f64;
+    ///
+    /// // 2^2 - 4 == 0
+    /// let abs_difference = (f.exp2() - 4.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp2(self) -> f64 {
         unsafe { intrinsics::exp2f64(self) }
@@ -190,6 +381,19 @@
 
     /// Returns the natural logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let one = 1.0_f64;
+    /// // e^1
+    /// let e = one.exp();
+    ///
+    /// // ln(e) - 1 == 0
+    /// let abs_difference = (e.ln() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ln(self) -> f64 {
         self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
@@ -197,10 +401,21 @@
 
     /// Returns the logarithm of the number with respect to an arbitrary base.
     ///
-    /// The result may not be correctly rounded owing to implementation details;
+    /// The result might not be correctly rounded owing to implementation details;
     /// `self.log2()` can produce more accurate results for base 2, and
     /// `self.log10()` can produce more accurate results for base 10.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let twenty_five = 25.0_f64;
+    ///
+    /// // log5(25) - 2 == 0
+    /// let abs_difference = (twenty_five.log(5.0) - 2.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log(self, base: f64) -> f64 {
         self.ln() / base.ln()
@@ -208,6 +423,17 @@
 
     /// Returns the base 2 logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let four = 4.0_f64;
+    ///
+    /// // log2(4) - 2 == 0
+    /// let abs_difference = (four.log2() - 2.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log2(self) -> f64 {
         self.log_wrapper(|n| {
@@ -220,6 +446,17 @@
 
     /// Returns the base 10 logarithm of the number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let hundred = 100.0_f64;
+    ///
+    /// // log10(100) - 2 == 0
+    /// let abs_difference = (hundred.log10() - 2.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn log10(self) -> f64 {
         self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
@@ -230,13 +467,37 @@
     /// * If `self <= other`: `0:0`
     /// * Else: `self - other`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 3.0_f64;
+    /// let y = -3.0_f64;
+    ///
+    /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
+    /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
+    ///
+    /// assert!(abs_difference_x < 1e-10);
+    /// assert!(abs_difference_y < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn abs_sub(self, other: f64) -> f64 {
         unsafe { cmath::fdim(self, other) }
     }
 
-    /// Returns the cubic root of a number.
+    /// Returns the cube root of a number.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 8.0_f64;
+    ///
+    /// // x^(1/3) - 2 == 0
+    /// let abs_difference = (x.cbrt() - 2.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cbrt(self) -> f64 {
         unsafe { cmath::cbrt(self) }
@@ -245,6 +506,18 @@
     /// Calculates the length of the hypotenuse of a right-angle triangle given
     /// legs of length `x` and `y`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0_f64;
+    /// let y = 3.0_f64;
+    ///
+    /// // sqrt(x^2 + y^2)
+    /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn hypot(self, other: f64) -> f64 {
         unsafe { cmath::hypot(self, other) }
@@ -252,6 +525,16 @@
 
     /// Computes the sine of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f64::consts::FRAC_PI_2;
+    ///
+    /// let abs_difference = (x.sin() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sin(self) -> f64 {
         unsafe { intrinsics::sinf64(self) }
@@ -259,6 +542,16 @@
 
     /// Computes the cosine of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 2.0 * std::f64::consts::PI;
+    ///
+    /// let abs_difference = (x.cos() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cos(self) -> f64 {
         unsafe { intrinsics::cosf64(self) }
@@ -266,6 +559,15 @@
 
     /// Computes the tangent of a number (in radians).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f64::consts::FRAC_PI_4;
+    /// let abs_difference = (x.tan() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-14);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn tan(self) -> f64 {
         unsafe { cmath::tan(self) }
@@ -275,6 +577,17 @@
     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
     /// [-1, 1].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = std::f64::consts::FRAC_PI_2;
+    ///
+    /// // asin(sin(pi/2))
+    /// let abs_difference = (f.sin().asin() - std::f64::consts::FRAC_PI_2).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn asin(self) -> f64 {
         unsafe { cmath::asin(self) }
@@ -284,6 +597,17 @@
     /// the range [0, pi] or NaN if the number is outside the range
     /// [-1, 1].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = std::f64::consts::FRAC_PI_4;
+    ///
+    /// // acos(cos(pi/4))
+    /// let abs_difference = (f.cos().acos() - std::f64::consts::FRAC_PI_4).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn acos(self) -> f64 {
         unsafe { cmath::acos(self) }
@@ -292,6 +616,17 @@
     /// Computes the arctangent of a number. Return value is in radians in the
     /// range [-pi/2, pi/2];
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let f = 1.0_f64;
+    ///
+    /// // atan(tan(1))
+    /// let abs_difference = (f.tan().atan() - 1.0).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atan(self) -> f64 {
         unsafe { cmath::atan(self) }
@@ -304,6 +639,26 @@
     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Positive angles measured counter-clockwise
+    /// // from positive x axis
+    /// // -pi/4 radians (45 deg clockwise)
+    /// let x1 = 3.0_f64;
+    /// let y1 = -3.0_f64;
+    ///
+    /// // 3pi/4 radians (135 deg counter-clockwise)
+    /// let x2 = -3.0_f64;
+    /// let y2 = 3.0_f64;
+    ///
+    /// let abs_difference_1 = (y1.atan2(x1) - (-std::f64::consts::FRAC_PI_4)).abs();
+    /// let abs_difference_2 = (y2.atan2(x2) - (3.0 * std::f64::consts::FRAC_PI_4)).abs();
+    ///
+    /// assert!(abs_difference_1 < 1e-10);
+    /// assert!(abs_difference_2 < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atan2(self, other: f64) -> f64 {
         unsafe { cmath::atan2(self, other) }
@@ -312,6 +667,18 @@
     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
     /// `(sin(x), cos(x))`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = std::f64::consts::FRAC_PI_4;
+    /// let f = x.sin_cos();
+    ///
+    /// let abs_difference_0 = (f.0 - x.sin()).abs();
+    /// let abs_difference_1 = (f.1 - x.cos()).abs();
+    ///
+    /// assert!(abs_difference_0 < 1e-10);
+    /// assert!(abs_difference_1 < 1e-10);
+    /// ```
     #[inline]
     pub fn sin_cos(self) -> (f64, f64) {
         (self.sin(), self.cos())
@@ -320,6 +687,18 @@
     /// Returns `e^(self) - 1` in a way that is accurate even if the
     /// number is close to zero.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1e-16_f64;
+    ///
+    /// // for very small x, e^x is approximately 1 + x + x^2 / 2
+    /// let approx = x + x * x / 2.0;
+    /// let abs_difference = (x.exp_m1() - approx).abs();
+    ///
+    /// assert!(abs_difference < 1e-20);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn exp_m1(self) -> f64 {
         unsafe { cmath::expm1(self) }
@@ -328,6 +707,18 @@
     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
     /// the operations were performed separately.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1e-16_f64;
+    ///
+    /// // for very small x, ln(1 + x) is approximately x - x^2 / 2
+    /// let approx = x - x * x / 2.0;
+    /// let abs_difference = (x.ln_1p() - approx).abs();
+    ///
+    /// assert!(abs_difference < 1e-20);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn ln_1p(self) -> f64 {
         unsafe { cmath::log1p(self) }
@@ -335,6 +726,20 @@
 
     /// Hyperbolic sine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f64::consts::E;
+    /// let x = 1.0_f64;
+    ///
+    /// let f = x.sinh();
+    /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
+    /// let g = ((e * e) - 1.0) / (2.0 * e);
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// assert!(abs_difference < 1e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn sinh(self) -> f64 {
         unsafe { cmath::sinh(self) }
@@ -342,6 +747,20 @@
 
     /// Hyperbolic cosine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f64::consts::E;
+    /// let x = 1.0_f64;
+    /// let f = x.cosh();
+    /// // Solving cosh() at 1 gives this result
+    /// let g = ((e * e) + 1.0) / (2.0 * e);
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// // Same result
+    /// assert!(abs_difference < 1.0e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn cosh(self) -> f64 {
         unsafe { cmath::cosh(self) }
@@ -349,6 +768,20 @@
 
     /// Hyperbolic tangent function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f64::consts::E;
+    /// let x = 1.0_f64;
+    ///
+    /// let f = x.tanh();
+    /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
+    /// let g = (1.0 - e.powi(-2)) / (1.0 + e.powi(-2));
+    /// let abs_difference = (f - g).abs();
+    ///
+    /// assert!(abs_difference < 1.0e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn tanh(self) -> f64 {
         unsafe { cmath::tanh(self) }
@@ -356,76 +789,115 @@
 
     /// Inverse hyperbolic sine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1.0_f64;
+    /// let f = x.sinh().asinh();
+    ///
+    /// let abs_difference = (f - x).abs();
+    ///
+    /// assert!(abs_difference < 1.0e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn asinh(self) -> f64 {
-        if self == NEG_INFINITY {
-            NEG_INFINITY
-        } else {
-            (self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
-        }
+        (self.abs() + ((self * self) + 1.0).sqrt()).ln().copysign(self)
     }
 
     /// Inverse hyperbolic cosine function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x = 1.0_f64;
+    /// let f = x.cosh().acosh();
+    ///
+    /// let abs_difference = (f - x).abs();
+    ///
+    /// assert!(abs_difference < 1.0e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn acosh(self) -> f64 {
-        if self < 1.0 { NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
+        if self < 1.0 { Self::NAN } else { (self + ((self * self) - 1.0).sqrt()).ln() }
     }
 
     /// Inverse hyperbolic tangent function.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// let e = std::f64::consts::E;
+    /// let f = e.tanh().atanh();
+    ///
+    /// let abs_difference = (f - e).abs();
+    ///
+    /// assert!(abs_difference < 1.0e-10);
+    /// ```
+    #[must_use = "method returns a new number and does not mutate the original value"]
     #[inline]
     pub fn atanh(self) -> f64 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
 
-    /// Restrict a value to a certain interval unless it is NaN.
+    /// Linear interpolation between `start` and `end`.
     ///
-    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is
-    /// less than `min`. Otherwise this returns `self`.
+    /// This enables linear interpolation between `start` and `end`, where start is represented by
+    /// `self == 0.0` and `end` is represented by `self == 1.0`. This is the basis of all
+    /// "transition", "easing", or "step" functions; if you change `self` from 0.0 to 1.0
+    /// at a given rate, the result will change from `start` to `end` at a similar rate.
     ///
-    /// Not that this function returns NaN if the initial value was NaN as
-    /// well.
+    /// Values below 0.0 or above 1.0 are allowed, allowing you to extrapolate values outside the
+    /// range from `start` to `end`. This also is useful for transition functions which might
+    /// move slightly past the end or start for a desired effect. Mathematically, the values
+    /// returned are equivalent to `start + self * (end - start)`, although we make a few specific
+    /// guarantees that are useful specifically to linear interpolation.
     ///
-    /// # Panics
+    /// These guarantees are:
     ///
-    /// Panics if `min > max`, `min` is NaN, or `max` is NaN.
+    /// * If `start` and `end` are [finite], the value at 0.0 is always `start` and the
+    ///   value at 1.0 is always `end`. (exactness)
+    /// * If `start` and `end` are [finite], the values will always move in the direction from
+    ///   `start` to `end` (monotonicity)
+    /// * If `self` is [finite] and `start == end`, the value at any point will always be
+    ///   `start == end`. (consistency)
     ///
-    #[inline]
-    pub fn clamp(self, min: f64, max: f64) -> f64 {
-        assert!(min <= max);
-        let mut x = self;
-        if x < min {
-            x = min;
+    /// [finite]: #method.is_finite
+    #[must_use = "method returns a new number and does not mutate the original value"]
+    #[allow(clippy::float_cmp)]
+    pub fn lerp(self, start: f64, end: f64) -> f64 {
+        // consistent
+        if start == end {
+            start
+
+        // exact/monotonic
+        } else {
+            self.mul_add(end, (-self).mul_add(start, start))
         }
-        if x > max {
-            x = max;
-        }
-        x
     }
 
     // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
     // because of their non-standard behavior (e.g., log(-n) returns -Inf instead
     // of expected NaN).
+    #[allow(clippy::if_same_then_else)]
     fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
-        if !cfg!(target_os = "solaris") {
+        if !cfg!(any(target_os = "solaris", target_os = "illumos")) {
             log_fn(self)
-        } else {
-            if self.is_finite() {
-                if self > 0.0 {
-                    log_fn(self)
-                } else if self == 0.0 {
-                    NEG_INFINITY // log(0) = -Inf
-                } else {
-                    NAN // log(-n) = NaN
-                }
-            } else if self.is_nan() {
-                self // log(NaN) = NaN
-            } else if self > 0.0 {
-                self // log(Inf) = Inf
+        } else if self.is_finite() {
+            if self > 0.0 {
+                log_fn(self)
+            } else if self == 0.0 {
+                Self::NEG_INFINITY // log(0) = -Inf
             } else {
-                NAN // log(-Inf) = NaN
+                Self::NAN // log(-n) = NaN
             }
+        } else if self.is_nan() {
+            self // log(NaN) = NaN
+        } else if self > 0.0 {
+            self // log(Inf) = Inf
+        } else {
+            Self::NAN // log(-Inf) = NaN
         }
     }
 }
diff --git a/sgx_tstd/src/ffi/c_str.rs b/sgx_tstd/src/ffi/c_str.rs
index 77d867c..be5f18e 100644
--- a/sgx_tstd/src/ffi/c_str.rs
+++ b/sgx_tstd/src/ffi/c_str.rs
@@ -28,11 +28,8 @@
 
 impl From<NulError> for io::Error {
     /// Converts a [`NulError`] into a [`io::Error`].
-    ///
-    /// [`NulError`]: ../ffi/struct.NulError.html
-    /// [`io::Error`]: ../io/struct.Error.html
     fn from(_: NulError) -> io::Error {
-        io::Error::new(io::ErrorKind::InvalidInput, "data provided contains a nul byte")
+        io::Error::new_const(io::ErrorKind::InvalidInput, &"data provided contains a nul byte")
     }
 }
 
@@ -42,6 +39,8 @@
     }
 }
 
+impl Error for FromVecWithNulError {}
+
 impl Error for IntoStringError {
     fn description(&self) -> &str {
         self.__description()
diff --git a/sgx_tstd/src/ffi/mod.rs b/sgx_tstd/src/ffi/mod.rs
index 3e490d0..883c296 100644
--- a/sgx_tstd/src/ffi/mod.rs
+++ b/sgx_tstd/src/ffi/mod.rs
@@ -60,14 +60,14 @@
 //! terminator, so the buffer length is really `len+1` characters.
 //! Rust strings don't have a nul terminator; their length is always
 //! stored and does not need to be calculated. While in Rust
-//! accessing a string's length is a O(1) operation (because the
-//! length is stored); in C it is an O(length) operation because the
+//! accessing a string's length is a `O(1)` operation (because the
+//! length is stored); in C it is an `O(length)` operation because the
 //! length needs to be computed by scanning the string for the nul
 //! terminator.
 //!
 //! * **Internal nul characters** - When C strings have a nul
 //! terminator character, this usually means that they cannot have nul
-//! characters in the middle a nul character would essentially
+//! characters in the middle — a nul character would essentially
 //! truncate the string. Rust strings *can* have nul characters in
 //! the middle, because nul does not have to mark the end of the
 //! string in Rust.
@@ -105,13 +105,13 @@
 //! [`env::var_os()`] is used to query environment variables; it
 //! returns an [`Option`]`<`[`OsString`]`>`. If the environment variable
 //! exists you will get a [`Some`]`(os_string)`, which you can *then* try to
-//! convert to a Rust string. This yields a [`Result<>`], so that
+//! convert to a Rust string. This yields a [`Result`], so that
 //! your code can detect errors in case the environment variable did
 //! not in fact contain valid Unicode data.
 //!
 //! * [`OsStr`] represents a borrowed reference to a string in a
 //! format that can be passed to the operating system. It can be
-//! converted into an UTF-8 Rust string slice in a similar way to
+//! converted into a UTF-8 Rust string slice in a similar way to
 //! [`OsString`].
 //!
 //! # Conversions
@@ -141,38 +141,27 @@
 //! method is an [`OsString`] which can be round-tripped to a Windows
 //! string losslessly.
 //!
-//! [`String`]: ../string/struct.String.html
-//! [`str`]: ../primitive.str.html
-//! [`char`]: ../primitive.char.html
-//! [`u8`]: ../primitive.u8.html
-//! [`u16`]: ../primitive.u16.html
-//! [Unicode scalar value]: http://www.unicode.org/glossary/#unicode_scalar_value
-//! [Unicode code point]: http://www.unicode.org/glossary/#code_point
-//! [`CString`]: struct.CString.html
-//! [`CStr`]: struct.CStr.html
-//! [`OsString`]: struct.OsString.html
-//! [`OsStr`]: struct.OsStr.html
-//! [`env::set_var()`]: ../env/fn.set_var.html
-//! [`env::var_os()`]: ../env/fn.var_os.html
-//! [`Result<>`]: ../result/enum.Result.html
-//! [unix.OsStringExt]: ../os/unix/ffi/trait.OsStringExt.html
-//! [`from_vec`]: ../os/unix/ffi/trait.OsStringExt.html#tymethod.from_vec
-//! [`into_vec`]: ../os/unix/ffi/trait.OsStringExt.html#tymethod.into_vec
-//! [unix.OsStrExt]: ../os/unix/ffi/trait.OsStrExt.html
-//! [`from_bytes`]: ../os/unix/ffi/trait.OsStrExt.html#tymethod.from_bytes
-//! [`as_bytes`]: ../os/unix/ffi/trait.OsStrExt.html#tymethod.as_bytes
-//! [`OsStrExt`]: ../os/unix/ffi/trait.OsStrExt.html
-//! [windows.OsStrExt]: ../os/windows/ffi/trait.OsStrExt.html
-//! [`encode_wide`]: ../os/windows/ffi/trait.OsStrExt.html#tymethod.encode_wide
-//! [`collect`]: ../iter/trait.Iterator.html#method.collect
-//! [windows.OsStringExt]: ../os/windows/ffi/trait.OsStringExt.html
-//! [`from_wide`]: ../os/windows/ffi/trait.OsStringExt.html#tymethod.from_wide
-//! [`Option`]: ../option/enum.Option.html
-//! [`Some`]: ../option/enum.Option.html#variant.Some
+//! [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value
+//! [Unicode code point]: https://www.unicode.org/glossary/#code_point
+//! [`env::set_var()`]: crate::env::set_var
+//! [`env::var_os()`]: crate::env::var_os
+//! [unix.OsStringExt]: crate::os::unix::ffi::OsStringExt
+//! [`from_vec`]: crate::os::unix::ffi::OsStringExt::from_vec
+//! [`into_vec`]: crate::os::unix::ffi::OsStringExt::into_vec
+//! [unix.OsStrExt]: crate::os::unix::ffi::OsStrExt
+//! [`from_bytes`]: crate::os::unix::ffi::OsStrExt::from_bytes
+//! [`as_bytes`]: crate::os::unix::ffi::OsStrExt::as_bytes
+//! [`OsStrExt`]: crate::os::unix::ffi::OsStrExt
+//! [windows.OsStrExt]: crate::os::windows::ffi::OsStrExt
+//! [`encode_wide`]: crate::os::windows::ffi::OsStrExt::encode_wide
+//! [`collect`]: crate::iter::Iterator::collect
+//! [windows.OsStringExt]: crate::os::windows::ffi::OsStringExt
+//! [`from_wide`]: crate::os::windows::ffi::OsStringExt::from_wide
 
-pub use self::c_str::{CString, CStr, NulError, IntoStringError};
-pub use self::c_str::{FromBytesWithNulError};
-pub use self::os_str::{OsString, OsStr};
+pub use self::c_str::FromBytesWithNulError;
+pub use self::c_str::FromVecWithNulError;
+pub use self::c_str::{CStr, CString, IntoStringError, NulError};
+pub use self::os_str::{OsStr, OsString};
 
 pub use core::ffi::c_void;
 pub use core::ffi::{VaList, VaListImpl};
diff --git a/sgx_tstd/src/ffi/os_str.rs b/sgx_tstd/src/ffi/os_str.rs
index e67cc54..f778930 100644
--- a/sgx_tstd/src/ffi/os_str.rs
+++ b/sgx_tstd/src/ffi/os_str.rs
@@ -15,27 +15,86 @@
 // specific language governing permissions and limitations
 // under the License..
 
+#![allow(clippy::op_ref)]
+
+use crate::borrow::{Borrow, Cow};
+use crate::cmp;
+use crate::fmt;
+use crate::hash::{Hash, Hasher};
+use crate::iter::{Extend, FromIterator};
+use crate::ops;
+use crate::rc::Rc;
+use crate::str::FromStr;
+use crate::sync::Arc;
+
 use crate::sys::os_str::{Buf, Slice};
 use crate::sys_common::{AsInner, FromInner, IntoInner};
-use core::ops;
-use core::cmp;
-use core::hash::{Hash, Hasher};
-use core::fmt;
-use alloc_crate::borrow::{Borrow, Cow, ToOwned};
-use alloc_crate::string::String;
-use alloc_crate::boxed::Box;
-use alloc_crate::rc::Rc;
-use alloc_crate::sync::Arc;
-
 
 /// A type that can represent owned, mutable platform-native strings, but is
 /// cheaply inter-convertible with Rust strings.
 ///
-#[derive(Clone)]
+/// The need for this type arises from the fact that:
+///
+/// * On Unix systems, strings are often arbitrary sequences of non-zero
+///   bytes, in many cases interpreted as UTF-8.
+///
+/// * On Windows, strings are often arbitrary sequences of non-zero 16-bit
+///   values, interpreted as UTF-16 when it is valid to do so.
+///
+/// * In Rust, strings are always valid UTF-8, which may contain zeros.
+///
+/// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
+/// and platform-native string values, and in particular allowing a Rust string
+/// to be converted into an "OS" string with no cost if possible. A consequence
+/// of this is that `OsString` instances are *not* `NUL` terminated; in order
+/// to pass to e.g., Unix system call, you should create a [`CStr`].
+///
+/// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former
+/// in each pair are owned strings; the latter are borrowed
+/// references.
+///
+/// Note, `OsString` and [`OsStr`] internally do not necessarily hold strings in
+/// the form native to the platform; While on Unix, strings are stored as a
+/// sequence of 8-bit values, on Windows, where strings are 16-bit value based
+/// as just discussed, strings are also actually stored as a sequence of 8-bit
+/// values, encoded in a less-strict variant of UTF-8. This is useful to
+/// understand when handling capacity and length values.
+///
+/// # Creating an `OsString`
+///
+/// **From a Rust string**: `OsString` implements
+/// [`From`]`<`[`String`]`>`, so you can use `my_string.from` to
+/// create an `OsString` from a normal Rust string.
+///
+/// **From slices:** Just like you can start with an empty Rust
+/// [`String`] and then [`String::push_str`] `&str`
+/// sub-string slices into it, you can create an empty `OsString` with
+/// the [`OsString::new`] method and then push string slices into it with the
+/// [`OsString::push`] method.
+///
+/// # Extracting a borrowed reference to the whole OS string
+///
+/// You can use the [`OsString::as_os_str`] method to get an `&`[`OsStr`] from
+/// an `OsString`; this is effectively a borrowed reference to the
+/// whole string.
+///
+/// # Conversions
+///
+/// See the [module's toplevel documentation about conversions][conversions] for a discussion on
+/// the traits which `OsString` implements for [conversions] from/to native representations.
+///
+/// [`&OsStr`]: OsStr
+/// [`&str`]: str
+/// [`CStr`]: crate::ffi::CStr
+/// [conversions]: super#conversions
+#[cfg_attr(not(test), rustc_diagnostic_item = "OsString")]
 pub struct OsString {
     inner: Buf,
 }
 
+/// Allows extension traits within `std`.
+impl crate::sealed::Sealed for OsString {}
+
 /// Borrowed reference to an OS string (see [`OsString`]).
 ///
 /// This type represents a borrowed reference to a string in the operating system's preferred
@@ -47,25 +106,49 @@
 /// See the [module's toplevel documentation about conversions][conversions] for a discussion on
 /// the traits which `OsStr` implements for [conversions] from/to native representations.
 ///
-/// [`OsString`]: struct.OsString.html
-/// [`&str`]: ../primitive.str.html
-/// [`String`]: ../string/struct.String.html
-/// [conversions]: index.html#conversions
+/// [`&str`]: str
+/// [conversions]: super#conversions
+#[cfg_attr(not(test), rustc_diagnostic_item = "OsStr")]
+// FIXME:
+// `OsStr::from_inner` current implementation relies
+// on `OsStr` being layout-compatible with `Slice`.
+// When attribute privacy is implemented, `OsStr` should be annotated as `#[repr(transparent)]`.
+// Anyway, `OsStr` representation and layout are considered implementation details, are
+// not documented and must not be relied upon.
 pub struct OsStr {
     inner: Slice,
 }
 
+/// Allows extension traits within `std`.
+impl crate::sealed::Sealed for OsStr {}
+
 impl OsString {
     /// Constructs a new empty `OsString`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let os_string = OsString::new();
+    /// ```
+    #[inline]
     pub fn new() -> OsString {
         OsString { inner: Buf::from_string(String::new()) }
     }
 
     /// Converts to an [`OsStr`] slice.
     ///
-    /// [`OsStr`]: struct.OsStr.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::{OsString, OsStr};
+    ///
+    /// let os_string = OsString::from("foo");
+    /// let os_str = OsStr::new("foo");
+    /// assert_eq!(os_string.as_os_str(), os_str);
+    /// ```
+    #[inline]
     pub fn as_os_str(&self) -> &OsStr {
         self
     }
@@ -74,16 +157,34 @@
     ///
     /// On failure, ownership of the original `OsString` is returned.
     ///
-    /// [`String`]: ../../std/string/struct.String.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let os_string = OsString::from("foo");
+    /// let string = os_string.into_string();
+    /// assert_eq!(string, Ok(String::from("foo")));
+    /// ```
+    #[inline]
     pub fn into_string(self) -> Result<String, OsString> {
         self.inner.into_string().map_err(|buf| OsString { inner: buf })
     }
 
     /// Extends the string with the given [`&OsStr`] slice.
     ///
-    /// [`&OsStr`]: struct.OsStr.html
+    /// [`&OsStr`]: OsStr
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut os_string = OsString::from("foo");
+    /// os_string.push("bar");
+    /// assert_eq!(&os_string, "foobar");
+    /// ```
+    #[inline]
     pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
         self.inner.push_slice(&s.as_ref().inner)
     }
@@ -96,12 +197,38 @@
     ///
     /// See main `OsString` documentation information about encoding.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut os_string = OsString::with_capacity(10);
+    /// let capacity = os_string.capacity();
+    ///
+    /// // This push is done without reallocating
+    /// os_string.push("foo");
+    ///
+    /// assert_eq!(capacity, os_string.capacity());
+    /// ```
+    #[inline]
     pub fn with_capacity(capacity: usize) -> OsString {
         OsString { inner: Buf::with_capacity(capacity) }
     }
 
     /// Truncates the `OsString` to zero length.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut os_string = OsString::from("foo");
+    /// assert_eq!(&os_string, "foo");
+    ///
+    /// os_string.clear();
+    /// assert_eq!(&os_string, "");
+    /// ```
+    #[inline]
     pub fn clear(&mut self) {
         self.inner.clear()
     }
@@ -110,6 +237,15 @@
     ///
     /// See `OsString` introduction for information about encoding.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let os_string = OsString::with_capacity(10);
+    /// assert!(os_string.capacity() >= 10);
+    /// ```
+    #[inline]
     pub fn capacity(&self) -> usize {
         self.inner.capacity()
     }
@@ -119,6 +255,16 @@
     ///
     /// The collection may reserve more space to avoid frequent reallocations.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::new();
+    /// s.reserve(10);
+    /// assert!(s.capacity() >= 10);
+    /// ```
+    #[inline]
     pub fn reserve(&mut self, additional: usize) {
         self.inner.reserve(additional)
     }
@@ -129,14 +275,40 @@
     ///
     /// Note that the allocator may give the collection more space than it
     /// requests. Therefore, capacity can not be relied upon to be precisely
-    /// minimal. Prefer reserve if future insertions are expected.
+    /// minimal. Prefer [`reserve`] if future insertions are expected.
     ///
+    /// [`reserve`]: OsString::reserve
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::new();
+    /// s.reserve_exact(10);
+    /// assert!(s.capacity() >= 10);
+    /// ```
+    #[inline]
     pub fn reserve_exact(&mut self, additional: usize) {
         self.inner.reserve_exact(additional)
     }
 
     /// Shrinks the capacity of the `OsString` to match its length.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::from("foo");
+    ///
+    /// s.reserve(100);
+    /// assert!(s.capacity() >= 100);
+    ///
+    /// s.shrink_to_fit();
+    /// assert_eq!(3, s.capacity());
+    /// ```
+    #[inline]
     pub fn shrink_to_fit(&mut self) {
         self.inner.shrink_to_fit()
     }
@@ -146,9 +318,23 @@
     /// The capacity will remain at least as large as both the length
     /// and the supplied value.
     ///
-    /// Panics if the current capacity is smaller than the supplied
-    /// minimum capacity.
+    /// If the current capacity is less than the lower limit, this is a no-op.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::from("foo");
+    ///
+    /// s.reserve(100);
+    /// assert!(s.capacity() >= 100);
+    ///
+    /// s.shrink_to(10);
+    /// assert!(s.capacity() >= 10);
+    /// s.shrink_to(0);
+    /// assert!(s.capacity() >= 3);
+    /// ```
     #[inline]
     pub fn shrink_to(&mut self, min_capacity: usize) {
         self.inner.shrink_to(min_capacity)
@@ -156,8 +342,15 @@
 
     /// Converts this `OsString` into a boxed [`OsStr`].
     ///
-    /// [`OsStr`]: struct.OsStr.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::{OsString, OsStr};
+    ///
+    /// let s = OsString::from("hello");
+    ///
+    /// let b: Box<OsStr> = s.into_boxed_os_str();
+    /// ```
     pub fn into_boxed_os_str(self) -> Box<OsStr> {
         let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
         unsafe { Box::from_raw(rw) }
@@ -165,11 +358,10 @@
 }
 
 impl From<String> for OsString {
-    /// Converts a [`String`] into a [`OsString`].
+    /// Converts a [`String`] into an [`OsString`].
     ///
-    /// The conversion copies the data, and includes an allocation on the heap.
-    ///
-    /// [`OsString`]: ../../std/ffi/struct.OsString.html
+    /// This conversion does not allocate or copy memory.
+    #[inline]
     fn from(s: String) -> OsString {
         OsString { inner: Buf::from_string(s) }
     }
@@ -221,6 +413,18 @@
     }
 }
 
+impl Clone for OsString {
+    #[inline]
+    fn clone(&self) -> Self {
+        OsString { inner: self.inner.clone() }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, source: &Self) {
+        self.inner.clone_from(&source.inner)
+    }
+}
+
 impl fmt::Debug for OsString {
     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt::Debug::fmt(&**self, formatter)
@@ -228,30 +432,35 @@
 }
 
 impl PartialEq for OsString {
+    #[inline]
     fn eq(&self, other: &OsString) -> bool {
         &**self == &**other
     }
 }
 
 impl PartialEq<str> for OsString {
+    #[inline]
     fn eq(&self, other: &str) -> bool {
         &**self == other
     }
 }
 
 impl PartialEq<OsString> for str {
+    #[inline]
     fn eq(&self, other: &OsString) -> bool {
         &**other == self
     }
 }
 
 impl PartialEq<&str> for OsString {
+    #[inline]
     fn eq(&self, other: &&str) -> bool {
         **self == **other
     }
 }
 
 impl<'a> PartialEq<OsString> for &'a str {
+    #[inline]
     fn eq(&self, other: &OsString) -> bool {
         **other == **self
     }
@@ -306,20 +515,28 @@
 impl OsStr {
     /// Coerces into an `OsStr` slice.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsStr;
+    ///
+    /// let os_str = OsStr::new("foo");
+    /// ```
+    #[inline]
     pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &OsStr {
         s.as_ref()
     }
 
     #[inline]
     fn from_inner(inner: &Slice) -> &OsStr {
-        // Safety: OsStr is just a wrapper of Slice,
+        // SAFETY: OsStr is just a wrapper of Slice,
         // therefore converting &Slice to &OsStr is safe.
         unsafe { &*(inner as *const Slice as *const OsStr) }
     }
 
     #[inline]
     fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
-        // Safety: OsStr is just a wrapper of Slice,
+        // SAFETY: OsStr is just a wrapper of Slice,
         // therefore converting &mut Slice to &mut OsStr is safe.
         // Any method that mutates OsStr must be careful not to
         // break platform-specific encoding, in particular Wtf8 on Windows.
@@ -330,8 +547,17 @@
     ///
     /// This conversion may entail doing a check for UTF-8 validity.
     ///
-    /// [`&str`]: ../../std/primitive.str.html
+    /// [`&str`]: str
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsStr;
+    ///
+    /// let os_str = OsStr::new("foo");
+    /// assert_eq!(os_str.to_str(), Some("foo"));
+    /// ```
+    #[inline]
     pub fn to_str(&self) -> Option<&str> {
         self.inner.to_str()
     }
@@ -341,24 +567,81 @@
     /// Any non-Unicode sequences are replaced with
     /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
     ///
-    /// [`Cow`]: ../../std/borrow/enum.Cow.html
-    /// [`str`]: ../../std/primitive.str.html
-    /// [U+FFFD]: ../../std/char/constant.REPLACEMENT_CHARACTER.html
+    /// [U+FFFD]: crate::char::REPLACEMENT_CHARACTER
     ///
+    /// # Examples
+    ///
+    /// Calling `to_string_lossy` on an `OsStr` with invalid unicode:
+    ///
+    /// ```
+    /// // Note, due to differences in how Unix and Windows represent strings,
+    /// // we are forced to complicate this example, setting up example `OsStr`s
+    /// // with different source data and via different platform extensions.
+    /// // Understand that in reality you could end up with such example invalid
+    /// // sequences simply through collecting user command line arguments, for
+    /// // example.
+    ///
+    /// #[cfg(unix)] {
+    ///     use std::ffi::OsStr;
+    ///     use std::os::unix::ffi::OsStrExt;
+    ///
+    ///     // Here, the values 0x66 and 0x6f correspond to 'f' and 'o'
+    ///     // respectively. The value 0x80 is a lone continuation byte, invalid
+    ///     // in a UTF-8 sequence.
+    ///     let source = [0x66, 0x6f, 0x80, 0x6f];
+    ///     let os_str = OsStr::from_bytes(&source[..]);
+    ///
+    ///     assert_eq!(os_str.to_string_lossy(), "fo�o");
+    /// }
+    /// #[cfg(windows)] {
+    ///     use std::ffi::OsString;
+    ///     use std::os::windows::prelude::*;
+    ///
+    ///     // Here the values 0x0066 and 0x006f correspond to 'f' and 'o'
+    ///     // respectively. The value 0xD800 is a lone surrogate half, invalid
+    ///     // in a UTF-16 sequence.
+    ///     let source = [0x0066, 0x006f, 0xD800, 0x006f];
+    ///     let os_string = OsString::from_wide(&source[..]);
+    ///     let os_str = os_string.as_os_str();
+    ///
+    ///     assert_eq!(os_str.to_string_lossy(), "fo�o");
+    /// }
+    /// ```
+    #[inline]
     pub fn to_string_lossy(&self) -> Cow<'_, str> {
         self.inner.to_string_lossy()
     }
 
     /// Copies the slice into an owned [`OsString`].
     ///
-    /// [`OsString`]: struct.OsString.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::{OsStr, OsString};
+    ///
+    /// let os_str = OsStr::new("foo");
+    /// let os_string = os_str.to_os_string();
+    /// assert_eq!(os_string, OsString::from("foo"));
+    /// ```
+    #[inline]
     pub fn to_os_string(&self) -> OsString {
         OsString { inner: self.inner.to_owned() }
     }
 
     /// Checks whether the `OsStr` is empty.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsStr;
+    ///
+    /// let os_str = OsStr::new("");
+    /// assert!(os_str.is_empty());
+    ///
+    /// let os_str = OsStr::new("foo");
+    /// assert!(!os_str.is_empty());
+    /// ```
+    #[inline]
     pub fn is_empty(&self) -> bool {
         self.inner.inner.is_empty()
     }
@@ -377,17 +660,23 @@
     /// This number is simply useful for passing to other methods, like
     /// [`OsString::with_capacity`] to avoid reallocations.
     ///
-    /// [`OsString`]: struct.OsString.html
-    /// [`OsString::with_capacity`]: struct.OsString.html#method.with_capacity
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsStr;
+    ///
+    /// let os_str = OsStr::new("");
+    /// assert_eq!(os_str.len(), 0);
+    ///
+    /// let os_str = OsStr::new("foo");
+    /// assert_eq!(os_str.len(), 3);
+    /// ```
+    #[inline]
     pub fn len(&self) -> usize {
         self.inner.inner.len()
     }
 
     /// Converts a [`Box`]`<OsStr>` into an [`OsString`] without copying or allocating.
-    ///
-    /// [`Box`]: ../boxed/struct.Box.html
-    /// [`OsString`]: struct.OsString.html
     pub fn into_os_string(self: Box<OsStr>) -> OsString {
         let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
         OsString { inner: Buf::from_box(boxed) }
@@ -395,10 +684,10 @@
 
     /// Gets the underlying byte representation.
     ///
-    /// Note: it is *crucial* that this API is private, to avoid
+    /// Note: it is *crucial* that this API is not externally public, to avoid
     /// revealing the internal, platform-specific encodings.
     #[inline]
-    fn bytes(&self) -> &[u8] {
+    pub(crate) fn bytes(&self) -> &[u8] {
         unsafe { &*(&self.inner as *const _ as *const [u8]) }
     }
 
@@ -408,10 +697,20 @@
     /// but non-ASCII letters are unchanged.
     ///
     /// To return a new lowercased value without modifying the existing one, use
-    /// [`to_ascii_lowercase`].
+    /// [`OsStr::to_ascii_lowercase`].
     ///
-    /// [`to_ascii_lowercase`]: #method.to_ascii_lowercase
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::from("GRÜßE, JÜRGEN ❤");
+    ///
+    /// s.make_ascii_lowercase();
+    ///
+    /// assert_eq!("grÜße, jÜrgen ❤", s);
+    /// ```
+    #[inline]
     pub fn make_ascii_lowercase(&mut self) {
         self.inner.make_ascii_lowercase()
     }
@@ -422,10 +721,20 @@
     /// but non-ASCII letters are unchanged.
     ///
     /// To return a new uppercased value without modifying the existing one, use
-    /// [`to_ascii_uppercase`].
+    /// [`OsStr::to_ascii_uppercase`].
     ///
-    /// [`to_ascii_uppercase`]: #method.to_ascii_uppercase
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let mut s = OsString::from("Grüße, Jürgen ❤");
+    ///
+    /// s.make_ascii_uppercase();
+    ///
+    /// assert_eq!("GRüßE, JüRGEN ❤", s);
+    /// ```
+    #[inline]
     pub fn make_ascii_uppercase(&mut self) {
         self.inner.make_ascii_uppercase()
     }
@@ -436,10 +745,16 @@
     /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
     /// but non-ASCII letters are unchanged.
     ///
-    /// To lowercase the value in-place, use [`make_ascii_lowercase`].
+    /// To lowercase the value in-place, use [`OsStr::make_ascii_lowercase`].
     ///
-    /// [`make_ascii_lowercase`]: #method.make_ascii_lowercase
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsString;
+    /// let s = OsString::from("Grüße, Jürgen ❤");
+    ///
+    /// assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
+    /// ```
     pub fn to_ascii_lowercase(&self) -> OsString {
         OsString::from_inner(self.inner.to_ascii_lowercase())
     }
@@ -450,16 +765,34 @@
     /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
     /// but non-ASCII letters are unchanged.
     ///
-    /// To uppercase the value in-place, use [`make_ascii_uppercase`].
+    /// To uppercase the value in-place, use [`OsStr::make_ascii_uppercase`].
     ///
-    /// [`make_ascii_uppercase`]: #method.make_ascii_uppercase
+    /// # Examples
     ///
+    /// ```
+    /// use std::ffi::OsString;
+    /// let s = OsString::from("Grüße, Jürgen ❤");
+    ///
+    /// assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
+    /// ```
     pub fn to_ascii_uppercase(&self) -> OsString {
         OsString::from_inner(self.inner.to_ascii_uppercase())
     }
 
     /// Checks if all characters in this string are within the ASCII range.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// let ascii = OsString::from("hello!\n");
+    /// let non_ascii = OsString::from("Grüße, Jürgen ❤");
+    ///
+    /// assert!(ascii.is_ascii());
+    /// assert!(!non_ascii.is_ascii());
+    /// ```
+    #[inline]
     pub fn is_ascii(&self) -> bool {
         self.inner.is_ascii()
     }
@@ -469,34 +802,50 @@
     /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`,
     /// but without allocating and copying temporaries.
     ///
-    pub fn eq_ignore_ascii_case<S: ?Sized + AsRef<OsStr>>(&self, other: &S) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::ffi::OsString;
+    ///
+    /// assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
+    /// assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
+    /// assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
+    /// ```
+    pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool {
         self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
     }
 }
 
 impl From<&OsStr> for Box<OsStr> {
+    #[inline]
     fn from(s: &OsStr) -> Box<OsStr> {
         let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
         unsafe { Box::from_raw(rw) }
     }
 }
 
+impl From<Cow<'_, OsStr>> for Box<OsStr> {
+    #[inline]
+    fn from(cow: Cow<'_, OsStr>) -> Box<OsStr> {
+        match cow {
+            Cow::Borrowed(s) => Box::from(s),
+            Cow::Owned(s) => Box::from(s),
+        }
+    }
+}
+
 impl From<Box<OsStr>> for OsString {
-    /// Converts a [`Box`]`<`[`OsStr`]`>` into a `OsString` without copying or
+    /// Converts a [`Box`]`<`[`OsStr`]`>` into an [`OsString`] without copying or
     /// allocating.
-    ///
-    /// [`Box`]: ../boxed/struct.Box.html
-    /// [`OsStr`]: ../ffi/struct.OsStr.html
+    #[inline]
     fn from(boxed: Box<OsStr>) -> OsString {
         boxed.into_os_string()
     }
 }
 
 impl From<OsString> for Box<OsStr> {
-    /// Converts a [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
-    ///
-    /// [`Box`]: ../boxed/struct.Box.html
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// Converts an [`OsString`] into a [`Box`]`<OsStr>` without copying or allocating.
+    #[inline]
     fn from(s: OsString) -> Box<OsStr> {
         s.into_boxed_os_str()
     }
@@ -510,10 +859,7 @@
 }
 
 impl From<OsString> for Arc<OsStr> {
-    /// Converts a [`OsString`] into a [`Arc`]`<OsStr>` without copying or allocating.
-    ///
-    /// [`Arc`]: ../sync/struct.Arc.html
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// Converts an [`OsString`] into an [`Arc`]`<OsStr>` without copying or allocating.
     #[inline]
     fn from(s: OsString) -> Arc<OsStr> {
         let arc = s.inner.into_arc();
@@ -530,10 +876,7 @@
 }
 
 impl From<OsString> for Rc<OsStr> {
-    /// Converts a [`OsString`] into a [`Rc`]`<OsStr>` without copying or allocating.
-    ///
-    /// [`Rc`]: ../rc/struct.Rc.html
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// Converts an [`OsString`] into an [`Rc`]`<OsStr>` without copying or allocating.
     #[inline]
     fn from(s: OsString) -> Rc<OsStr> {
         let rc = s.inner.into_rc();
@@ -578,6 +921,7 @@
 }
 
 impl Default for Box<OsStr> {
+    #[inline]
     fn default() -> Box<OsStr> {
         let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
         unsafe { Box::from_raw(rw) }
@@ -593,6 +937,7 @@
 }
 
 impl PartialEq for OsStr {
+    #[inline]
     fn eq(&self, other: &OsStr) -> bool {
         self.bytes().eq(other.bytes())
     }
@@ -712,6 +1057,7 @@
 }
 
 impl Borrow<OsStr> for OsString {
+    #[inline]
     fn borrow(&self) -> &OsStr {
         &self[..]
     }
@@ -719,15 +1065,18 @@
 
 impl ToOwned for OsStr {
     type Owned = OsString;
+    #[inline]
     fn to_owned(&self) -> OsString {
         self.to_os_string()
     }
+    #[inline]
     fn clone_into(&self, target: &mut OsString) {
         self.inner.clone_into(&mut target.inner)
     }
 }
 
 impl AsRef<OsStr> for OsStr {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         self
     }
@@ -755,12 +1104,14 @@
 }
 
 impl FromInner<Buf> for OsString {
+    #[inline]
     fn from_inner(buf: Buf) -> OsString {
         OsString { inner: buf }
     }
 }
 
 impl IntoInner<Buf> for OsString {
+    #[inline]
     fn into_inner(self) -> Buf {
         self.inner
     }
@@ -771,4 +1122,92 @@
     fn as_inner(&self) -> &Slice {
         &self.inner
     }
-}
\ No newline at end of file
+}
+
+impl FromStr for OsString {
+    type Err = core::convert::Infallible;
+
+    #[inline]
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        Ok(OsString::from(s))
+    }
+}
+
+impl Extend<OsString> for OsString {
+    #[inline]
+    fn extend<T: IntoIterator<Item = OsString>>(&mut self, iter: T) {
+        for s in iter {
+            self.push(&s);
+        }
+    }
+}
+
+impl<'a> Extend<&'a OsStr> for OsString {
+    #[inline]
+    fn extend<T: IntoIterator<Item = &'a OsStr>>(&mut self, iter: T) {
+        for s in iter {
+            self.push(s);
+        }
+    }
+}
+
+impl<'a> Extend<Cow<'a, OsStr>> for OsString {
+    #[inline]
+    fn extend<T: IntoIterator<Item = Cow<'a, OsStr>>>(&mut self, iter: T) {
+        for s in iter {
+            self.push(&s);
+        }
+    }
+}
+
+impl FromIterator<OsString> for OsString {
+    #[inline]
+    fn from_iter<I: IntoIterator<Item = OsString>>(iter: I) -> Self {
+        let mut iterator = iter.into_iter();
+
+        // Because we're iterating over `OsString`s, we can avoid at least
+        // one allocation by getting the first string from the iterator
+        // and appending to it all the subsequent strings.
+        match iterator.next() {
+            None => OsString::new(),
+            Some(mut buf) => {
+                buf.extend(iterator);
+                buf
+            }
+        }
+    }
+}
+
+impl<'a> FromIterator<&'a OsStr> for OsString {
+    #[inline]
+    fn from_iter<I: IntoIterator<Item = &'a OsStr>>(iter: I) -> Self {
+        let mut buf = Self::new();
+        for s in iter {
+            buf.push(s);
+        }
+        buf
+    }
+}
+
+impl<'a> FromIterator<Cow<'a, OsStr>> for OsString {
+    #[inline]
+    fn from_iter<I: IntoIterator<Item = Cow<'a, OsStr>>>(iter: I) -> Self {
+        let mut iterator = iter.into_iter();
+
+        // Because we're iterating over `OsString`s, we can avoid at least
+        // one allocation by getting the first owned string from the iterator
+        // and appending to it all the subsequent strings.
+        match iterator.next() {
+            None => OsString::new(),
+            Some(Cow::Owned(mut buf)) => {
+                buf.extend(iterator);
+                buf
+            }
+            Some(Cow::Borrowed(buf)) => {
+                let mut buf = OsString::from(buf);
+                buf.extend(iterator);
+                buf
+            }
+        }
+    }
+}
diff --git a/sgx_tstd/src/fs.rs b/sgx_tstd/src/fs.rs
index f60c1c3..1462ffe 100644
--- a/sgx_tstd/src/fs.rs
+++ b/sgx_tstd/src/fs.rs
@@ -17,13 +17,14 @@
 
 //! Filesystem manipulation operations.
 
-use core::fmt;
+#![deny(unsafe_op_in_unsafe_fn)]
+use crate::ffi::OsString;
+use crate::fmt;
 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
 use crate::path::{Path, PathBuf};
 use crate::sys::fs as fs_imp;
 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
 use crate::time::SystemTime;
-use crate::ffi::OsString;
 #[cfg(not(feature = "untrusted_fs"))]
 use crate::untrusted::path::PathEx;
 
@@ -37,6 +38,64 @@
 /// on closing are ignored by the implementation of `Drop`.  Use the method
 /// [`sync_all`] if these errors must be manually handled.
 ///
+/// # Examples
+///
+/// Creates a new file and write bytes to it (you can also use [`write()`]):
+///
+/// ```no_run
+/// use std::fs::File;
+/// use std::io::prelude::*;
+///
+/// fn main() -> std::io::Result<()> {
+///     let mut file = File::create("foo.txt")?;
+///     file.write_all(b"Hello, world!")?;
+///     Ok(())
+/// }
+/// ```
+///
+/// Read the contents of a file into a [`String`] (you can also use [`read`]):
+///
+/// ```no_run
+/// use std::fs::File;
+/// use std::io::prelude::*;
+///
+/// fn main() -> std::io::Result<()> {
+///     let mut file = File::open("foo.txt")?;
+///     let mut contents = String::new();
+///     file.read_to_string(&mut contents)?;
+///     assert_eq!(contents, "Hello, world!");
+///     Ok(())
+/// }
+/// ```
+///
+/// It can be more efficient to read the contents of a file with a buffered
+/// [`Read`]er. This can be accomplished with [`BufReader<R>`]:
+///
+/// ```no_run
+/// use std::fs::File;
+/// use std::io::BufReader;
+/// use std::io::prelude::*;
+///
+/// fn main() -> std::io::Result<()> {
+///     let file = File::open("foo.txt")?;
+///     let mut buf_reader = BufReader::new(file);
+///     let mut contents = String::new();
+///     buf_reader.read_to_string(&mut contents)?;
+///     assert_eq!(contents, "Hello, world!");
+///     Ok(())
+/// }
+/// ```
+///
+/// Note that, although read and write methods require a `&mut File`, because
+/// of the interfaces for [`Read`] and [`Write`], the holder of a `&File` can
+/// still modify the file, either through methods that take `&File` or by
+/// retrieving the underlying OS object and modifying the file that way.
+/// Additionally, many operating systems allow concurrent modification of files
+/// by different processes. Avoid assuming that holding a `&File` means that the
+/// file will not change.
+///
+/// [`BufReader<R>`]: io::BufReader
+/// [`sync_all`]: File::sync_all
 pub struct File {
     inner: fs_imp::File,
 }
@@ -47,7 +106,6 @@
 /// [`symlink_metadata`] function or method and represents known
 /// metadata about a file such as its permissions, size, modification
 /// times, etc.
-///
 #[derive(Clone)]
 pub struct Metadata(fs_imp::FileAttr);
 
@@ -65,18 +123,11 @@
 ///
 /// This [`io::Result`] will be an [`Err`] if there's some sort of intermittent
 /// IO error during iteration.
-///
-/// [`read_dir`]: fn.read_dir.html
-/// [`DirEntry`]: struct.DirEntry.html
-/// [`io::Result`]: ../io/type.Result.html
-/// [`Err`]: ../result/enum.Result.html#variant.Err
 #[derive(Debug)]
 pub struct ReadDir(fs_imp::ReadDir);
 
 /// Entries returned by the [`ReadDir`] iterator.
 ///
-/// [`ReadDir`]: struct.ReadDir.html
-///
 /// An instance of `DirEntry` represents an entry inside of a directory on the
 /// filesystem. Each entry can be inspected via methods to learn about the full
 /// path or possibly other metadata through per-platform extension traits.
@@ -89,46 +140,58 @@
 /// [`File::create`] methods are aliases for commonly used options using this
 /// builder.
 ///
-/// [`File`]: struct.File.html
-/// [`File::open`]: struct.File.html#method.open
-/// [`File::create`]: struct.File.html#method.create
+/// Generally speaking, when using `OpenOptions`, you'll first call
+/// [`OpenOptions::new`], then chain calls to methods to set each option, then
+/// call [`OpenOptions::open`], passing the path of the file you're trying to
+/// open. This will give you a [`io::Result`] with a [`File`] inside that you
+/// can further operate on.
 ///
-/// Generally speaking, when using `OpenOptions`, you'll first call [`new`],
-/// then chain calls to methods to set each option, then call [`open`],
-/// passing the path of the file you're trying to open. This will give you a
-/// [`io::Result`][result] with a [`File`][file] inside that you can further
-/// operate on.
+/// # Examples
 ///
-/// [`new`]: struct.OpenOptions.html#method.new
-/// [`open`]: struct.OpenOptions.html#method.open
-/// [result]: ../io/type.Result.html
-/// [file]: struct.File.html
+/// Opening a file to read:
 ///
+/// ```no_run
+/// use std::fs::OpenOptions;
+///
+/// let file = OpenOptions::new().read(true).open("foo.txt");
+/// ```
+///
+/// Opening a file for both reading and writing, as well as creating it if it
+/// doesn't exist:
+///
+/// ```no_run
+/// use std::fs::OpenOptions;
+///
+/// let file = OpenOptions::new()
+///             .read(true)
+///             .write(true)
+///             .create(true)
+///             .open("foo.txt");
+/// ```
 #[derive(Clone, Debug)]
 pub struct OpenOptions(fs_imp::OpenOptions);
 
 /// Representation of the various permissions on a file.
 ///
-/// This module only currently provides one bit of information, [`readonly`],
-/// which is exposed on all currently supported platforms. Unix-specific
-/// functionality, such as mode bits, is available through the
-/// [`PermissionsExt`] trait.
+/// This module only currently provides one bit of information,
+/// [`Permissions::readonly`], which is exposed on all currently supported
+/// platforms. Unix-specific functionality, such as mode bits, is available
+/// through the [`PermissionsExt`] trait.
 ///
-/// [`readonly`]: struct.Permissions.html#method.readonly
-/// [`PermissionsExt`]: ../os/unix/fs/trait.PermissionsExt.html
+/// [`PermissionsExt`]: crate::os::unix::fs::PermissionsExt
 #[derive(Clone, PartialEq, Eq, Debug)]
 pub struct Permissions(fs_imp::FilePermissions);
 
 /// A structure representing a type of file with accessors for each file type.
 /// It is returned by [`Metadata::file_type`] method.
-///
-/// [`Metadata::file_type`]: struct.Metadata.html#method.file_type
 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "FileType")]
 pub struct FileType(fs_imp::FileType);
 
 /// A builder used to create directories in various manners.
 ///
 /// This builder also supports platform-specific options.
+#[cfg_attr(not(test), rustc_diagnostic_item = "DirBuilder")]
 #[derive(Debug)]
 pub struct DirBuilder {
     inner: fs_imp::DirBuilder,
@@ -148,23 +211,29 @@
 /// This is a convenience function for using [`File::open`] and [`read_to_end`]
 /// with fewer imports and without an intermediate variable. It pre-allocates a
 /// buffer based on the file size when available, so it is generally faster than
-/// reading into a vector created with `Vec::new()`.
+/// reading into a vector created with [`Vec::new()`].
 ///
-/// [`File::open`]: struct.File.html#method.open
-/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
+/// [`read_to_end`]: Read::read_to_end
 ///
 /// # Errors
 ///
 /// This function will return an error if `path` does not already exist.
 /// Other errors may also be returned according to [`OpenOptions::open`].
 ///
-/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
-///
 /// It will also return an error if it encounters while reading an error
-/// of a kind other than [`ErrorKind::Interrupted`].
+/// of a kind other than [`io::ErrorKind::Interrupted`].
 ///
-/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
+/// # Examples
 ///
+/// ```no_run
+/// use std::fs;
+/// use std::net::SocketAddr;
+///
+/// fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
+///     let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
+///     Ok(())
+/// }
+/// ```
 pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
     fn inner(path: &Path) -> io::Result<Vec<u8>> {
         let mut file = File::open(path)?;
@@ -180,24 +249,31 @@
 /// This is a convenience function for using [`File::open`] and [`read_to_string`]
 /// with fewer imports and without an intermediate variable. It pre-allocates a
 /// buffer based on the file size when available, so it is generally faster than
-/// reading into a string created with `String::new()`.
+/// reading into a string created with [`String::new()`].
 ///
-/// [`File::open`]: struct.File.html#method.open
-/// [`read_to_string`]: ../io/trait.Read.html#method.read_to_string
+/// [`read_to_string`]: Read::read_to_string
 ///
 /// # Errors
 ///
 /// This function will return an error if `path` does not already exist.
 /// Other errors may also be returned according to [`OpenOptions::open`].
 ///
-/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
-///
 /// It will also return an error if it encounters while reading an error
-/// of a kind other than [`ErrorKind::Interrupted`],
+/// of a kind other than [`io::ErrorKind::Interrupted`],
 /// or if the contents of the file are not valid UTF-8.
 ///
-/// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
+/// # Examples
 ///
+/// ```no_run
+/// use std::fs;
+/// use std::net::SocketAddr;
+/// use std::error::Error;
+///
+/// fn main() -> Result<(), Box<dyn Error>> {
+///     let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
+///     Ok(())
+/// }
+/// ```
 pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
     fn inner(path: &Path) -> io::Result<String> {
         let mut file = File::open(path)?;
@@ -216,9 +292,19 @@
 /// This is a convenience function for using [`File::create`] and [`write_all`]
 /// with fewer imports.
 ///
-/// [`File::create`]: struct.File.html#method.create
-/// [`write_all`]: ../io/trait.Write.html#method.write_all
+/// [`write_all`]: Write::write_all
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::write("foo.txt", b"Lorem ipsum")?;
+///     fs::write("bar.txt", "dolor sit")?;
+///     Ok(())
+/// }
+/// ```
 pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
     fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
         File::create(path)?.write_all(contents)
@@ -236,8 +322,16 @@
     /// This function will return an error if `path` does not already exist.
     /// Other errors may also be returned according to [`OpenOptions::open`].
     ///
-    /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn open<P: AsRef<Path>>(path: P) -> io::Result<File> {
         OpenOptions::new().read(true).open(path.as_ref())
     }
@@ -249,8 +343,16 @@
     ///
     /// See the [`OpenOptions::open`] function for more details.
     ///
-    /// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create("foo.txt")?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn create<P: AsRef<Path>>(path: P) -> io::Result<File> {
         OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
     }
@@ -268,8 +370,17 @@
     ///
     /// See the [`OpenOptions::new`] function for more details.
     ///
-    /// [`OpenOptions::new`]: struct.OpenOptions.html#method.new
+    /// # Examples
     ///
+    /// ```no_run
+    /// #![feature(with_options)]
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::with_options().read(true).open("foo.txt")?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn with_options() -> OpenOptions {
         OpenOptions::new()
     }
@@ -283,11 +394,25 @@
     /// when the `File` is closed.  Dropping a file will ignore errors in
     /// synchronizing this in-memory data.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::prelude::*;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create("foo.txt")?;
+    ///     f.write_all(b"Hello, world!")?;
+    ///
+    ///     f.sync_all()?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn sync_all(&self) -> io::Result<()> {
         self.inner.fsync()
     }
 
-    /// This function is similar to [`sync_all`], except that it may not
+    /// This function is similar to [`sync_all`], except that it might not
     /// synchronize file metadata to the filesystem.
     ///
     /// This is intended for use cases that must synchronize content, but don't
@@ -297,8 +422,22 @@
     /// Note that some platforms may simply implement this in terms of
     /// [`sync_all`].
     ///
-    /// [`sync_all`]: struct.File.html#method.sync_all
+    /// [`sync_all`]: File::sync_all
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::prelude::*;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create("foo.txt")?;
+    ///     f.write_all(b"Hello, world!")?;
+    ///
+    ///     f.sync_data()?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn sync_data(&self) -> io::Result<()> {
         self.inner.datasync()
     }
@@ -321,12 +460,37 @@
     /// Also, std::io::ErrorKind::InvalidInput will be returned if the desired
     /// length would cause an overflow due to the implementation specifics.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create("foo.txt")?;
+    ///     f.set_len(10)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// Note that this method alters the content of the underlying file, even
+    /// though it takes `&self` rather than `&mut self`.
     pub fn set_len(&self, size: u64) -> io::Result<()> {
         self.inner.truncate(size)
     }
 
     /// Queries metadata about the underlying file.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let metadata = f.metadata()?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn metadata(&self) -> io::Result<Metadata> {
         self.inner.file_attr().map(Metadata)
     }
@@ -335,6 +499,41 @@
     /// as the existing `File` instance. Reads, writes, and seeks will affect
     /// both `File` instances simultaneously.
     ///
+    /// # Examples
+    ///
+    /// Creates two handles for a file named `foo.txt`:
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut file = File::open("foo.txt")?;
+    ///     let file_copy = file.try_clone()?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// Assuming there’s a file named `foo.txt` with contents `abcdef\n`, create
+    /// two handles, seek one of them, and read the remaining bytes from the
+    /// other handle:
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::SeekFrom;
+    /// use std::io::prelude::*;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut file = File::open("foo.txt")?;
+    ///     let mut file_copy = file.try_clone()?;
+    ///
+    ///     file.seek(SeekFrom::Start(3))?;
+    ///
+    ///     let mut contents = vec![];
+    ///     file_copy.read_to_end(&mut contents)?;
+    ///     assert_eq!(contents, b"def\n");
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn try_clone(&self) -> io::Result<File> {
         Ok(File { inner: self.inner.duplicate()? })
     }
@@ -347,7 +546,7 @@
     /// the `SetFileInformationByHandle` function on Windows. Note that, this
     /// [may change in the future][changes].
     ///
-    /// [changes]: ../io/index.html#platform-specific-behavior
+    /// [changes]: io#platform-specific-behavior
     ///
     /// # Errors
     ///
@@ -355,11 +554,33 @@
     /// attributes on the underlying file. It may also return an error in other
     /// os-specific unspecified cases.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// fn main() -> std::io::Result<()> {
+    ///     use std::fs::File;
+    ///
+    ///     let file = File::open("foo.txt")?;
+    ///     let mut perms = file.metadata()?.permissions();
+    ///     perms.set_readonly(true);
+    ///     file.set_permissions(perms)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// Note that this method alters the permissions of the underlying file,
+    /// even though it takes `&self` rather than `&mut self`.
     pub fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
         self.inner.set_permissions(perm.0)
     }
 }
 
+// In addition to the `impl`s here, `File` also has `impl`s for
+// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
+// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
+// `AsHandle`/`From<OwnedHandle>`/`Into<OwnedHandle>` and
+// `AsRawHandle`/`IntoRawHandle`/`FromRawHandle` on Windows.
+
 impl AsInner<fs_imp::File> for File {
     fn as_inner(&self) -> &fs_imp::File {
         &self.inner
@@ -392,8 +613,14 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.inner.is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
-        Initializer::nop()
+        // SAFETY: Read is guaranteed to work on uninitialized memory
+        unsafe { Initializer::nop() }
     }
 }
 
@@ -406,6 +633,11 @@
         self.inner.write_vectored(bufs)
     }
 
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.inner.is_write_vectored()
+    }
+
     fn flush(&mut self) -> io::Result<()> {
         self.inner.flush()
     }
@@ -427,8 +659,14 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.inner.is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
-        Initializer::nop()
+        // SAFETY: Read is guaranteed to work on uninitialized memory
+        unsafe { Initializer::nop() }
     }
 }
 
@@ -441,6 +679,11 @@
         self.inner.write_vectored(bufs)
     }
 
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.inner.is_write_vectored()
+    }
+
     fn flush(&mut self) -> io::Result<()> {
         self.inner.flush()
     }
@@ -457,6 +700,14 @@
     ///
     /// All options are initially set to `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let mut options = OpenOptions::new();
+    /// let file = options.read(true).open("foo.txt");
+    /// ```
     pub fn new() -> Self {
         OpenOptions(fs_imp::OpenOptions::new())
     }
@@ -466,6 +717,13 @@
     /// This option, when true, will indicate that the file should be
     /// `read`-able if opened.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().read(true).open("foo.txt");
+    /// ```
     pub fn read(&mut self, read: bool) -> &mut Self {
         self.0.read(read);
         self
@@ -479,6 +737,13 @@
     /// If the file already exists, any write calls on it will overwrite its
     /// contents, without truncating it.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().write(true).open("foo.txt");
+    /// ```
     pub fn write(&mut self, write: bool) -> &mut Self {
         self.0.write(write);
         self
@@ -508,16 +773,21 @@
     ///
     /// ## Note
     ///
-    /// This function doesn't create the file if it doesn't exist. Use the [`create`]
-    /// method to do so.
+    /// This function doesn't create the file if it doesn't exist. Use the
+    /// [`OpenOptions::create`] method to do so.
     ///
-    /// [`write()`]: ../../std/fs/struct.File.html#method.write
-    /// [`flush()`]: ../../std/fs/struct.File.html#method.flush
-    /// [`seek`]: ../../std/fs/struct.File.html#method.seek
-    /// [`SeekFrom`]: ../../std/io/enum.SeekFrom.html
-    /// [`Current`]: ../../std/io/enum.SeekFrom.html#variant.Current
-    /// [`create`]: #method.create
+    /// [`write()`]: Write::write
+    /// [`flush()`]: Write::flush
+    /// [`seek`]: Seek::seek
+    /// [`Current`]: SeekFrom::Current
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().append(true).open("foo.txt");
+    /// ```
     pub fn append(&mut self, append: bool) -> &mut Self {
         self.0.append(append);
         self
@@ -530,6 +800,13 @@
     ///
     /// The file must be opened with write access for truncate to work.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().write(true).truncate(true).open("foo.txt");
+    /// ```
     pub fn truncate(&mut self, truncate: bool) -> &mut Self {
         self.0.truncate(truncate);
         self
@@ -537,12 +814,16 @@
 
     /// Sets the option to create a new file, or open it if it already exists.
     ///
-    /// In order for the file to be created, [`write`] or [`append`] access must
-    /// be used.
+    /// In order for the file to be created, [`OpenOptions::write`] or
+    /// [`OpenOptions::append`] access must be used.
     ///
-    /// [`write`]: #method.write
-    /// [`append`]: #method.append
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().write(true).create(true).open("foo.txt");
+    /// ```
     pub fn create(&mut self, create: bool) -> &mut Self {
         self.0.create(create);
         self
@@ -563,9 +844,18 @@
     /// The file must be opened with write or append access in order to create
     /// a new file.
     ///
-    /// [`.create()`]: #method.create
-    /// [`.truncate()`]: #method.truncate
+    /// [`.create()`]: OpenOptions::create
+    /// [`.truncate()`]: OpenOptions::truncate
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().write(true)
+    ///                              .create_new(true)
+    ///                              .open("foo.txt");
+    /// ```
     pub fn create_new(&mut self, create_new: bool) -> &mut Self {
         self.0.create_new(create_new);
         self
@@ -577,9 +867,8 @@
     ///
     /// This function will return an error under a number of different
     /// circumstances. Some of these error conditions are listed here, together
-    /// with their [`ErrorKind`]. The mapping to [`ErrorKind`]s is not part of
-    /// the compatibility contract of the function, especially the `Other` kind
-    /// might change to more specific kinds in the future.
+    /// with their [`io::ErrorKind`]. The mapping to [`io::ErrorKind`]s is not
+    /// part of the compatibility contract of the function.
     ///
     /// * [`NotFound`]: The specified file does not exist and neither `create`
     ///   or `create_new` is set.
@@ -593,13 +882,27 @@
     ///   exists.
     /// * [`InvalidInput`]: Invalid combinations of open options (truncate
     ///   without write access, no access mode set, etc.).
-    /// * [`Other`]: One of the directory components of the specified file path
+    ///
+    /// The following errors don't match any existing [`io::ErrorKind`] at the moment:
+    /// * One of the directory components of the specified file path
     ///   was not, in fact, a directory.
-    /// * [`Other`]: Filesystem-level errors: full disk, write permission
+    /// * Filesystem-level errors: full disk, write permission
     ///   requested on a read-only file system, exceeded disk quota, too many
     ///   open files, too long filename, too many symbolic links in the
     ///   specified path (Unix-like systems only), etc.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    ///
+    /// let file = OpenOptions::new().read(true).open("foo.txt");
+    /// ```
+    ///
+    /// [`AlreadyExists`]: io::ErrorKind::AlreadyExists
+    /// [`InvalidInput`]: io::ErrorKind::InvalidInput
+    /// [`NotFound`]: io::ErrorKind::NotFound
+    /// [`PermissionDenied`]: io::ErrorKind::PermissionDenied
     pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
         self._open(path.as_ref())
     }
@@ -624,42 +927,127 @@
 impl Metadata {
     /// Returns the file type for this metadata.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// fn main() -> std::io::Result<()> {
+    ///     use std::fs;
+    ///
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     println!("{:?}", metadata.file_type());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn file_type(&self) -> FileType {
         FileType(self.0.file_type())
     }
 
     /// Returns `true` if this metadata is for a directory. The
     /// result is mutually exclusive to the result of
-    /// [`is_file`], and will be false for symlink metadata
+    /// [`Metadata::is_file`], and will be false for symlink metadata
     /// obtained from [`symlink_metadata`].
     ///
-    /// [`is_file`]: struct.Metadata.html#method.is_file
-    /// [`symlink_metadata`]: fn.symlink_metadata.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// fn main() -> std::io::Result<()> {
+    ///     use std::fs;
+    ///
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     assert!(!metadata.is_dir());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn is_dir(&self) -> bool {
         self.file_type().is_dir()
     }
 
     /// Returns `true` if this metadata is for a regular file. The
     /// result is mutually exclusive to the result of
-    /// [`is_dir`], and will be false for symlink metadata
+    /// [`Metadata::is_dir`], and will be false for symlink metadata
     /// obtained from [`symlink_metadata`].
     ///
-    /// [`is_dir`]: struct.Metadata.html#method.is_dir
-    /// [`symlink_metadata`]: fn.symlink_metadata.html
+    /// When the goal is simply to read from (or write to) the source, the most
+    /// reliable way to test the source can be read (or written to) is to open
+    /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
+    /// a Unix-like system for example. See [`File::open`] or
+    /// [`OpenOptions::open`] for more information.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     assert!(metadata.is_file());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn is_file(&self) -> bool {
         self.file_type().is_file()
     }
 
+    /// Returns `true` if this metadata is for a symbolic link.
+    ///
+    /// # Examples
+    ///
+    #[cfg_attr(unix, doc = "```no_run")]
+    #[cfg_attr(not(unix), doc = "```ignore")]
+    /// #![feature(is_symlink)]
+    /// use std::fs;
+    /// use std::path::Path;
+    /// use std::os::unix::fs::symlink;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let link_path = Path::new("link");
+    ///     symlink("/origin_does_not_exists/", link_path)?;
+    ///
+    ///     let metadata = fs::symlink_metadata(link_path)?;
+    ///
+    ///     assert!(metadata.is_symlink());
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn is_symlink(&self) -> bool {
+        self.file_type().is_symlink()
+    }
+
     /// Returns the size of the file, in bytes, this metadata is for.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     assert_eq!(0, metadata.len());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn len(&self) -> u64 {
         self.0.size()
     }
 
     /// Returns the permissions of the file this metadata is for.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     assert!(!metadata.permissions().readonly());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn permissions(&self) -> Permissions {
         Permissions(self.0.perm())
     }
@@ -671,9 +1059,25 @@
     ///
     /// # Errors
     ///
-    /// This field may not be available on all platforms, and will return an
+    /// This field might not be available on all platforms, and will return an
     /// `Err` on platforms where it is not available.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     if let Ok(time) = metadata.modified() {
+    ///         println!("{:?}", time);
+    ///     } else {
+    ///         println!("Not supported on this platform");
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn modified(&self) -> io::Result<SystemTime> {
         self.0.modified().map(FromInner::from_inner)
     }
@@ -689,9 +1093,25 @@
     ///
     /// # Errors
     ///
-    /// This field may not be available on all platforms, and will return an
+    /// This field might not be available on all platforms, and will return an
     /// `Err` on platforms where it is not available.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     if let Ok(time) = metadata.accessed() {
+    ///         println!("{:?}", time);
+    ///     } else {
+    ///         println!("Not supported on this platform");
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn accessed(&self) -> io::Result<SystemTime> {
         self.0.accessed().map(FromInner::from_inner)
     }
@@ -704,9 +1124,25 @@
     ///
     /// # Errors
     ///
-    /// This field may not be available on all platforms, and will return an
+    /// This field might not be available on all platforms, and will return an
     /// `Err` on platforms or filesystems where it is not available.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///
+    ///     if let Ok(time) = metadata.created() {
+    ///         println!("{:?}", time);
+    ///     } else {
+    ///         println!("Not supported on this platform or filesystem");
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn created(&self) -> io::Result<SystemTime> {
         self.0.created().map(FromInner::from_inner)
     }
@@ -722,7 +1158,7 @@
             .field("modified", &self.modified())
             .field("accessed", &self.accessed())
             .field("created", &self.created())
-            .finish()
+            .finish_non_exhaustive()
     }
 }
 
@@ -741,6 +1177,19 @@
 impl Permissions {
     /// Returns `true` if these permissions describe a readonly (unwritable) file.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut f = File::create("foo.txt")?;
+    ///     let metadata = f.metadata()?;
+    ///
+    ///     assert_eq!(false, metadata.permissions().readonly());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn readonly(&self) -> bool {
         self.0.readonly()
     }
@@ -752,10 +1201,28 @@
     /// writing.
     ///
     /// This operation does **not** modify the filesystem. To modify the
-    /// filesystem use the [`fs::set_permissions`] function.
+    /// filesystem use the [`set_permissions`] function.
     ///
-    /// [`fs::set_permissions`]: fn.set_permissions.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::create("foo.txt")?;
+    ///     let metadata = f.metadata()?;
+    ///     let mut permissions = metadata.permissions();
+    ///
+    ///     permissions.set_readonly(true);
+    ///
+    ///     // filesystem doesn't change
+    ///     assert_eq!(false, metadata.permissions().readonly());
+    ///
+    ///     // just this particular `permissions`.
+    ///     assert_eq!(true, permissions.readonly());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn set_readonly(&mut self, readonly: bool) {
         self.0.set_readonly(readonly)
     }
@@ -767,9 +1234,22 @@
     /// [`is_file`] and [`is_symlink`]; only zero or one of these
     /// tests may pass.
     ///
-    /// [`is_file`]: struct.FileType.html#method.is_file
-    /// [`is_symlink`]: struct.FileType.html#method.is_symlink
+    /// [`is_file`]: FileType::is_file
+    /// [`is_symlink`]: FileType::is_symlink
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// fn main() -> std::io::Result<()> {
+    ///     use std::fs;
+    ///
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///     let file_type = metadata.file_type();
+    ///
+    ///     assert_eq!(file_type.is_dir(), false);
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn is_dir(&self) -> bool {
         self.0.is_dir()
     }
@@ -779,9 +1259,28 @@
     /// [`is_dir`] and [`is_symlink`]; only zero or one of these
     /// tests may pass.
     ///
-    /// [`is_dir`]: struct.FileType.html#method.is_dir
-    /// [`is_symlink`]: struct.FileType.html#method.is_symlink
+    /// When the goal is simply to read from (or write to) the source, the most
+    /// reliable way to test the source can be read (or written to) is to open
+    /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
+    /// a Unix-like system for example. See [`File::open`] or
+    /// [`OpenOptions::open`] for more information.
     ///
+    /// [`is_dir`]: FileType::is_dir
+    /// [`is_symlink`]: FileType::is_symlink
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// fn main() -> std::io::Result<()> {
+    ///     use std::fs;
+    ///
+    ///     let metadata = fs::metadata("foo.txt")?;
+    ///     let file_type = metadata.file_type();
+    ///
+    ///     assert_eq!(file_type.is_file(), true);
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn is_file(&self) -> bool {
         self.0.is_file()
     }
@@ -797,13 +1296,25 @@
     /// follows symbolic links, so [`is_symlink`] would always
     /// return `false` for the target file.
     ///
-    /// [`Metadata`]: struct.Metadata.html
-    /// [`fs::metadata`]: fn.metadata.html
-    /// [`fs::symlink_metadata`]: fn.symlink_metadata.html
-    /// [`is_dir`]: struct.FileType.html#method.is_dir
-    /// [`is_file`]: struct.FileType.html#method.is_file
-    /// [`is_symlink`]: struct.FileType.html#method.is_symlink
+    /// [`fs::metadata`]: metadata
+    /// [`fs::symlink_metadata`]: symlink_metadata
+    /// [`is_dir`]: FileType::is_dir
+    /// [`is_file`]: FileType::is_file
+    /// [`is_symlink`]: FileType::is_symlink
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let metadata = fs::symlink_metadata("foo.txt")?;
+    ///     let file_type = metadata.file_type();
+    ///
+    ///     assert_eq!(file_type.is_symlink(), false);
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn is_symlink(&self) -> bool {
         self.0.is_symlink()
     }
@@ -841,6 +1352,29 @@
     /// The full path is created by joining the original path to `read_dir`
     /// with the filename of this entry.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     for entry in fs::read_dir(".")? {
+    ///         let dir = entry?;
+    ///         println!("{:?}", dir.path());
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// This prints output like:
+    ///
+    /// ```text
+    /// "./whatever.txt"
+    /// "./foo.html"
+    /// "./hello_world.rs"
+    /// ```
+    ///
+    /// The exact text, of course, depends on what files you have in `.`.
     pub fn path(&self) -> PathBuf {
         self.0.path()
     }
@@ -848,7 +1382,10 @@
     /// Returns the metadata for the file that this entry points at.
     ///
     /// This function will not traverse symlinks if this entry points at a
-    /// symlink.
+    /// symlink. To traverse symlinks use [`fs::metadata`] or [`fs::File::metadata`].
+    ///
+    /// [`fs::metadata`]: metadata
+    /// [`fs::File::metadata`]: File::metadata
     ///
     /// # Platform-specific behavior
     ///
@@ -856,6 +1393,25 @@
     /// needed), but on Unix platforms this function is the equivalent of
     /// calling `symlink_metadata` on the path.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::fs;
+    ///
+    /// if let Ok(entries) = fs::read_dir(".") {
+    ///     for entry in entries {
+    ///         if let Ok(entry) = entry {
+    ///             // Here, `entry` is a `DirEntry`.
+    ///             if let Ok(metadata) = entry.metadata() {
+    ///                 // Now let's show our entry's permissions!
+    ///                 println!("{:?}: {:?}", entry.path(), metadata.permissions());
+    ///             } else {
+    ///                 println!("Couldn't get metadata for {:?}", entry.path());
+    ///             }
+    ///         }
+    ///     }
+    /// }
+    /// ```
     pub fn metadata(&self) -> io::Result<Metadata> {
         self.0.metadata().map(Metadata)
     }
@@ -871,6 +1427,25 @@
     /// system calls needed), but some Unix platforms may require the equivalent
     /// call to `symlink_metadata` to learn about the target file type.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::fs;
+    ///
+    /// if let Ok(entries) = fs::read_dir(".") {
+    ///     for entry in entries {
+    ///         if let Ok(entry) = entry {
+    ///             // Here, `entry` is a `DirEntry`.
+    ///             if let Ok(file_type) = entry.file_type() {
+    ///                 // Now let's show our entry's file type!
+    ///                 println!("{:?}: {:?}", entry.path(), file_type);
+    ///             } else {
+    ///                 println!("Couldn't get file type for {:?}", entry.path());
+    ///             }
+    ///         }
+    ///     }
+    /// }
+    /// ```
     pub fn file_type(&self) -> io::Result<FileType> {
         self.0.file_type().map(FileType)
     }
@@ -878,6 +1453,20 @@
     /// Returns the bare file name of this directory entry without any other
     /// leading path component.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::fs;
+    ///
+    /// if let Ok(entries) = fs::read_dir(".") {
+    ///     for entry in entries {
+    ///         if let Ok(entry) = entry {
+    ///             // Here, `entry` is a `DirEntry`.
+    ///             println!("{:?}", entry.file_name());
+    ///         }
+    ///     }
+    /// }
+    /// ```
     pub fn file_name(&self) -> OsString {
         self.0.file_name()
     }
@@ -907,7 +1496,7 @@
 /// and the `DeleteFile` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -915,8 +1504,20 @@
 /// limited to just these cases:
 ///
 /// * `path` points to a directory.
+/// * The file doesn't exist.
 /// * The user lacks permissions to remove the file.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::remove_file("a.txt")?;
+///     Ok(())
+/// }
+/// ```
+#[doc(alias = "delete")]
 pub fn remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
     fs_imp::unlink(path.as_ref())
 }
@@ -933,7 +1534,7 @@
 /// and the `GetFileAttributesEx` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -943,6 +1544,17 @@
 /// * The user lacks permissions to perform `metadata` call on `path`.
 /// * `path` does not exist.
 ///
+/// # Examples
+///
+/// ```rust,no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     let attr = fs::metadata("/some/file/path.txt")?;
+///     // inspect attr ...
+///     Ok(())
+/// }
+/// ```
 pub fn metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
     fs_imp::stat(path.as_ref()).map(Metadata)
 }
@@ -955,7 +1567,7 @@
 /// and the `GetFileAttributesEx` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -965,6 +1577,17 @@
 /// * The user lacks permissions to perform `metadata` call on `path`.
 /// * `path` does not exist.
 ///
+/// # Examples
+///
+/// ```rust,no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     let attr = fs::symlink_metadata("/some/file/path.txt")?;
+///     // inspect attr ...
+///     Ok(())
+/// }
+/// ```
 pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> io::Result<Metadata> {
     fs_imp::lstat(path.as_ref()).map(Metadata)
 }
@@ -986,7 +1609,7 @@
 ///
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -997,6 +1620,16 @@
 /// * The user lacks permissions to view contents.
 /// * `from` and `to` are on separate filesystems.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
+///     Ok(())
+/// }
+/// ```
 pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
     fs_imp::rename(from.as_ref(), to.as_ref())
 }
@@ -1013,10 +1646,7 @@
 /// the length of the `to` file as reported by `metadata`.
 ///
 /// If you’re wanting to copy the contents of one file to another and you’re
-/// working with [`File`]s, see the [`io::copy`] function.
-///
-/// [`io::copy`]: ../io/fn.copy.html
-/// [`File`]: ./struct.File.html
+/// working with [`File`]s, see the [`io::copy()`] function.
 ///
 /// # Platform-specific behavior
 ///
@@ -1029,61 +1659,99 @@
 /// `fcopyfile`.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
 /// This function will return an error in the following situations, but is not
 /// limited to just these cases:
 ///
-/// * The `from` path is not a file.
-/// * The `from` file does not exist.
-/// * The current process does not have the permission rights to access
+/// * `from` is neither a regular file nor a symlink to a regular file.
+/// * `from` does not exist.
+/// * The current process does not have the permission rights to read
 ///   `from` or write `to`.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::copy("foo.txt", "bar.txt")?;  // Copy foo.txt to bar.txt
+///     Ok(())
+/// }
+/// ```
 pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
     fs_imp::copy(from.as_ref(), to.as_ref())
 }
 
 /// Creates a new hard link on the filesystem.
 ///
-/// The `dst` path will be a link pointing to the `src` path. Note that systems
-/// often require these two paths to both be located on the same filesystem.
+/// The `link` path will be a link pointing to the `original` path. Note that
+/// systems often require these two paths to both be located on the same
+/// filesystem.
+///
+/// If `original` names a symbolic link, it is platform-specific whether the
+/// symbolic link is followed. On platforms where it's possible to not follow
+/// it, it is not followed, and the created hard link points to the symbolic
+/// link itself.
 ///
 /// # Platform-specific behavior
 ///
-/// This function currently corresponds to the `link` function on Unix
-/// and the `CreateHardLink` function on Windows.
+/// This function currently corresponds the `CreateHardLink` function on Windows.
+/// On most Unix systems, it corresponds to the `linkat` function with no flags.
+/// On Android, VxWorks, and Redox, it instead corresponds to the `link` function.
+/// On MacOS, it uses the `linkat` function if it is available, but on very old
+/// systems where `linkat` is not available, `link` is selected at runtime instead.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
 /// This function will return an error in the following situations, but is not
 /// limited to just these cases:
 ///
-/// * The `src` path is not a file or doesn't exist.
+/// * The `original` path is not a file or doesn't exist.
 ///
-pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
-    fs_imp::link(src.as_ref(), dst.as_ref())
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::hard_link("a.txt", "b.txt")?; // Hard link a.txt to b.txt
+///     Ok(())
+/// }
+/// ```
+pub fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
+    fs_imp::link(original.as_ref(), link.as_ref())
 }
 
 /// Creates a new symbolic link on the filesystem.
 ///
-/// The `dst` path will be a symbolic link pointing to the `src` path.
+/// The `link` path will be a symbolic link pointing to the `original` path.
 /// On Windows, this will be a file symlink, not a directory symlink;
 /// for this reason, the platform-specific [`std::os::unix::fs::symlink`]
 /// and [`std::os::windows::fs::symlink_file`] or [`symlink_dir`] should be
 /// used instead to make the intent explicit.
 ///
-/// [`std::os::unix::fs::symlink`]: ../os/unix/fs/fn.symlink.html
-/// [`std::os::windows::fs::symlink_file`]: ../os/windows/fs/fn.symlink_file.html
-/// [`symlink_dir`]: ../os/windows/fs/fn.symlink_dir.html
+/// [`std::os::unix::fs::symlink`]: crate::os::unix::fs::symlink
+/// [`std::os::windows::fs::symlink_file`]: crate::os::windows::fs::symlink_file
+/// [`symlink_dir`]: crate::os::windows::fs::symlink_dir
 ///
+/// # Examples
 ///
-pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
-    fs_imp::symlink(src.as_ref(), dst.as_ref())
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::soft_link("a.txt", "b.txt")?;
+///     Ok(())
+/// }
+/// ```
+pub fn soft_link<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
+    fs_imp::symlink(original.as_ref(), link.as_ref())
 }
 
 /// Reads a symbolic link, returning the file that the link points to.
@@ -1095,7 +1763,7 @@
 /// `FILE_FLAG_BACKUP_SEMANTICS` flags on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -1105,6 +1773,16 @@
 /// * `path` is not a symbolic link.
 /// * `path` does not exist.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     let path = fs::read_link("a.txt")?;
+///     Ok(())
+/// }
+/// ```
 pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
     fs_imp::readlink(path.as_ref())
 }
@@ -1124,7 +1802,7 @@
 /// with other applications (if passed to the application on the command-line,
 /// or written to a file another application may read).
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 /// [path]: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
 ///
 /// # Errors
@@ -1135,6 +1813,16 @@
 /// * `path` does not exist.
 /// * A non-final component in path is not a directory.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     let path = fs::canonicalize("../a/../foo.txt")?;
+///     Ok(())
+/// }
+/// ```
 pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
     fs_imp::canonicalize(path.as_ref())
 }
@@ -1147,7 +1835,7 @@
 /// and the `CreateDirectory` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// **NOTE**: If a parent of the given path doesn't exist, this function will
 /// return an error. To create a directory and all its missing parents at the
@@ -1164,8 +1852,17 @@
 ///   function.)
 /// * `path` already exists.
 ///
-/// [`create_dir_all`]: fn.create_dir_all.html
+/// # Examples
 ///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::create_dir("/some/dir")?;
+///     Ok(())
+/// }
+/// ```
+#[doc(alias = "mkdir")]
 pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
     DirBuilder::new().create(path.as_ref())
 }
@@ -1179,7 +1876,7 @@
 /// and the `CreateDirectory` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -1197,13 +1894,23 @@
 /// concurrently from multiple threads or processes is guaranteed not to fail
 /// due to a race condition with itself.
 ///
-/// [`fs::create_dir`]: fn.create_dir.html
+/// [`fs::create_dir`]: create_dir
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::create_dir_all("/some/dir")?;
+///     Ok(())
+/// }
+/// ```
 pub fn create_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
     DirBuilder::new().recursive(true).create(path.as_ref())
 }
 
-/// Removes an existing, empty directory.
+/// Removes an empty directory.
 ///
 /// # Platform-specific behavior
 ///
@@ -1211,16 +1918,29 @@
 /// and the `RemoveDirectory` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
 /// This function will return an error in the following situations, but is not
 /// limited to just these cases:
 ///
+/// * `path` doesn't exist.
+/// * `path` isn't a directory.
 /// * The user lacks permissions to remove the directory at the provided `path`.
 /// * The directory isn't empty.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::remove_dir("/some/dir")?;
+///     Ok(())
+/// }
+/// ```
+#[doc(alias = "rmdir")]
 pub fn remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
     fs_imp::rmdir(path.as_ref())
 }
@@ -1238,15 +1958,26 @@
 /// on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
 /// See [`fs::remove_file`] and [`fs::remove_dir`].
 ///
-/// [`fs::remove_file`]:  fn.remove_file.html
-/// [`fs::remove_dir`]: fn.remove_dir.html
+/// [`fs::remove_file`]: remove_file
+/// [`fs::remove_dir`]: remove_dir
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::remove_dir_all("/some/dir")?;
+///     Ok(())
+/// }
+/// ```
+#[doc(alias = "delete")]
 pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
     fs_imp::remove_dir_all(path.as_ref())
 }
@@ -1255,9 +1986,8 @@
 ///
 /// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`.
 /// New errors may be encountered after an iterator is initially constructed.
-///
-/// [`io::Result`]: ../io/type.Result.html
-/// [`DirEntry`]: struct.DirEntry.html
+/// Entries for the current and parent directories (typically `.` and `..`) are
+/// skipped.
 ///
 /// # Platform-specific behavior
 ///
@@ -1266,7 +1996,7 @@
 /// currently corresponds to `readdir` on Unix and `FindNextFile` on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// The order in which this iterator returns entries is platform and filesystem
 /// dependent.
@@ -1280,6 +2010,48 @@
 /// * The process lacks permissions to view the contents.
 /// * The `path` points at a non-directory file.
 ///
+/// # Examples
+///
+/// ```
+/// use std::io;
+/// use std::fs::{self, DirEntry};
+/// use std::path::Path;
+///
+/// // one possible implementation of walking a directory only visiting files
+/// fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
+///     if dir.is_dir() {
+///         for entry in fs::read_dir(dir)? {
+///             let entry = entry?;
+///             let path = entry.path();
+///             if path.is_dir() {
+///                 visit_dirs(&path, cb)?;
+///             } else {
+///                 cb(&entry);
+///             }
+///         }
+///     }
+///     Ok(())
+/// }
+/// ```
+///
+/// ```rust,no_run
+/// use std::{fs, io};
+///
+/// fn main() -> io::Result<()> {
+///     let mut entries = fs::read_dir(".")?
+///         .map(|res| res.map(|e| e.path()))
+///         .collect::<Result<Vec<_>, io::Error>>()?;
+///
+///     // The order in which `read_dir` returns entries is not guaranteed. If reproducible
+///     // ordering is required the entries should be explicitly sorted.
+///
+///     entries.sort();
+///
+///     // The entries have now been sorted by their path.
+///
+///     Ok(())
+/// }
+/// ```
 pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
     fs_imp::readdir(path.as_ref()).map(ReadDir)
 }
@@ -1292,7 +2064,7 @@
 /// and the `SetFileAttributes` function on Windows.
 /// Note that, this [may change in the future][changes].
 ///
-/// [changes]: ../io/index.html#platform-specific-behavior
+/// [changes]: io#platform-specific-behavior
 ///
 /// # Errors
 ///
@@ -1302,6 +2074,18 @@
 /// * `path` does not exist.
 /// * The user lacks the permission to change attributes of the file.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     let mut perms = fs::metadata("foo.txt")?.permissions();
+///     perms.set_readonly(true);
+///     fs::set_permissions("foo.txt", perms)?;
+///     Ok(())
+/// }
+/// ```
 pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result<()> {
     fs_imp::set_perm(path.as_ref(), perm.0)
 }
@@ -1380,7 +2164,10 @@
         match path.parent() {
             Some(p) => self.create_dir_all(p)?,
             None => {
-                return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree"));
+                return Err(io::Error::new_const(
+                    io::ErrorKind::Uncategorized,
+                    &"failed to create whole tree",
+                ));
             }
         }
         match self.inner.mkdir(path) {
@@ -1396,3 +2183,28 @@
         &mut self.inner
     }
 }
+
+/// Returns `Ok(true)` if the path points at an existing entity.
+///
+/// This function will traverse symbolic links to query information about the
+/// destination file. In case of broken symbolic links this will return `Ok(false)`.
+///
+/// As opposed to the `exists()` method, this one doesn't silently ignore errors
+/// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
+/// denied on some of the parent directories.)
+///
+/// # Examples
+///
+/// ```no_run
+/// #![feature(path_try_exists)]
+/// use std::fs;
+///
+/// assert!(!fs::try_exists("does_not_exist.txt").expect("Can't check existence of file does_not_exist.txt"));
+/// assert!(fs::try_exists("/root/secret_file.txt").is_err());
+/// ```
+// FIXME: stabilization should modify documentation of `exists()` to recommend this method
+// instead.
+#[inline]
+pub fn try_exists<P: AsRef<Path>>(path: P) -> io::Result<bool> {
+    fs_imp::try_exists(path.as_ref())
+}
diff --git a/sgx_tstd/src/future.rs b/sgx_tstd/src/future.rs
deleted file mode 100644
index e76e41c..0000000
--- a/sgx_tstd/src/future.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-//! Asynchronous values.
-
-#[doc(inline)]
-pub use core::future::Future;
-
-#[doc(inline)]
-pub use core::future::{from_generator, get_context, ResumeTy};
-
-#[doc(inline)]
-pub use core::future::{pending, ready, Pending, Ready};
-
-#[doc(inline)]
-pub use core::future::IntoFuture;
diff --git a/sgx_tstd/src/io/buffered.rs b/sgx_tstd/src/io/buffered.rs
deleted file mode 100644
index bf1280b..0000000
--- a/sgx_tstd/src/io/buffered.rs
+++ /dev/null
@@ -1,686 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-//! Buffering wrappers for I/O traits
-
-use crate::io::prelude::*;
-use crate::error;
-use crate::io::{
-    self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom, DEFAULT_BUF_SIZE,
-};
-use crate::memchr;
-use core::cmp;
-use core::fmt;
-
-/// The `BufReader` struct adds buffering to any reader.
-///
-/// It can be excessively inefficient to work directly with a [`Read`] instance.
-/// For example, every call to [`read`][`TcpStream::read`] on [`TcpStream`]
-/// results in a system call. A `BufReader` performs large, infrequent reads on
-/// the underlying [`Read`] and maintains an in-memory buffer of the results.
-///
-/// `BufReader` can improve the speed of programs that make *small* and
-/// *repeated* read calls to the same file or network socket. It does not
-/// help when reading very large amounts at once, or reading just one or a few
-/// times. It also provides no advantage when reading from a source that is
-/// already in memory, like a `Vec<u8>`.
-///
-/// When the `BufReader<R>` is dropped, the contents of its buffer will be
-/// discarded. Creating multiple instances of a `BufReader<R>` on the same
-/// stream can cause data loss. Reading from the underlying reader after
-/// unwrapping the `BufReader<R>` with `BufReader::into_inner` can also cause
-/// data loss.
-///
-/// [`Read`]: ../../std/io/trait.Read.html
-/// [`TcpStream::read`]: ../../std/net/struct.TcpStream.html#method.read
-/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
-///
-pub struct BufReader<R> {
-    inner: R,
-    buf: Box<[u8]>,
-    pos: usize,
-    cap: usize,
-}
-
-impl<R: Read> BufReader<R> {
-    /// Creates a new `BufReader<R>` with a default buffer capacity. The default is currently 8 KB,
-    /// but may change in the future.
-    ///
-    pub fn new(inner: R) -> BufReader<R> {
-        BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
-    }
-
-    /// Creates a new `BufReader<R>` with the specified buffer capacity.
-    ///
-    pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
-        unsafe {
-            let mut buffer = Vec::with_capacity(capacity);
-            buffer.set_len(capacity);
-            inner.initializer().initialize(&mut buffer);
-            BufReader { inner, buf: buffer.into_boxed_slice(), pos: 0, cap: 0 }
-        }
-    }
-}
-
-impl<R> BufReader<R> {
-    /// Gets a reference to the underlying reader.
-    ///
-    /// It is inadvisable to directly read from the underlying reader.
-    ///
-    pub fn get_ref(&self) -> &R {
-        &self.inner
-    }
-
-    /// Gets a mutable reference to the underlying reader.
-    ///
-    /// It is inadvisable to directly read from the underlying reader.
-    ///
-    pub fn get_mut(&mut self) -> &mut R {
-        &mut self.inner
-    }
-
-    /// Returns a reference to the internally buffered data.
-    ///
-    /// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty.
-    ///
-    pub fn buffer(&self) -> &[u8] {
-        &self.buf[self.pos..self.cap]
-    }
-
-    /// Returns the number of bytes the internal buffer can hold at once.
-    ///
-    pub fn capacity(&self) -> usize {
-        self.buf.len()
-    }
-
-    /// Unwraps this `BufReader<R>`, returning the underlying reader.
-    ///
-    /// Note that any leftover data in the internal buffer is lost. Therefore,
-    /// a following read from the underlying reader may lead to data loss.
-    ///
-    pub fn into_inner(self) -> R {
-        self.inner
-    }
-
-    /// Invalidates all data in the internal buffer.
-    #[inline]
-    fn discard_buffer(&mut self) {
-        self.pos = 0;
-        self.cap = 0;
-    }
-}
-
-impl<R: Seek> BufReader<R> {
-    /// Seeks relative to the current position. If the new position lies within the buffer,
-    /// the buffer will not be flushed, allowing for more efficient seeks.
-    /// This method does not return the location of the underlying reader, so the caller
-    /// must track this information themselves if it is required.
-    pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
-        let pos = self.pos as u64;
-        if offset < 0 {
-            if let Some(new_pos) = pos.checked_sub((-offset) as u64) {
-                self.pos = new_pos as usize;
-                return Ok(());
-            }
-        } else {
-            if let Some(new_pos) = pos.checked_add(offset as u64) {
-                if new_pos <= self.cap as u64 {
-                    self.pos = new_pos as usize;
-                    return Ok(());
-                }
-            }
-        }
-        self.seek(SeekFrom::Current(offset)).map(drop)
-    }
-}
-
-impl<R: Read> Read for BufReader<R> {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        // If we don't have any buffered data and we're doing a massive read
-        // (larger than our internal buffer), bypass our internal buffer
-        // entirely.
-        if self.pos == self.cap && buf.len() >= self.buf.len() {
-            self.discard_buffer();
-            return self.inner.read(buf);
-        }
-        let nread = {
-            let mut rem = self.fill_buf()?;
-            rem.read(buf)?
-        };
-        self.consume(nread);
-        Ok(nread)
-    }
-
-    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
-        if self.pos == self.cap && total_len >= self.buf.len() {
-            self.discard_buffer();
-            return self.inner.read_vectored(bufs);
-        }
-        let nread = {
-            let mut rem = self.fill_buf()?;
-            rem.read_vectored(bufs)?
-        };
-        self.consume(nread);
-        Ok(nread)
-    }
-
-    // we can't skip unconditionally because of the large buffer case in read.
-    unsafe fn initializer(&self) -> Initializer {
-        self.inner.initializer()
-    }
-}
-
-impl<R: Read> BufRead for BufReader<R> {
-    fn fill_buf(&mut self) -> io::Result<&[u8]> {
-        // If we've reached the end of our internal buffer then we need to fetch
-        // some more data from the underlying reader.
-        // Branch using `>=` instead of the more correct `==`
-        // to tell the compiler that the pos..cap slice is always valid.
-        if self.pos >= self.cap {
-            debug_assert!(self.pos == self.cap);
-            self.cap = self.inner.read(&mut self.buf)?;
-            self.pos = 0;
-        }
-        Ok(&self.buf[self.pos..self.cap])
-    }
-
-    fn consume(&mut self, amt: usize) {
-        self.pos = cmp::min(self.pos + amt, self.cap);
-    }
-}
-
-impl<R> fmt::Debug for BufReader<R>
-where
-    R: fmt::Debug,
-{
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt.debug_struct("BufReader")
-            .field("reader", &self.inner)
-            .field("buffer", &format_args!("{}/{}", self.cap - self.pos, self.buf.len()))
-            .finish()
-    }
-}
-
-impl<R: Seek> Seek for BufReader<R> {
-    /// Seek to an offset, in bytes, in the underlying reader.
-    ///
-    /// The position used for seeking with `SeekFrom::Current(_)` is the
-    /// position the underlying reader would be at if the `BufReader<R>` had no
-    /// internal buffer.
-    ///
-    /// Seeking always discards the internal buffer, even if the seek position
-    /// would otherwise fall within it. This guarantees that calling
-    /// `.into_inner()` immediately after a seek yields the underlying reader
-    /// at the same position.
-    ///
-    /// To seek without discarding the internal buffer, use [`BufReader::seek_relative`].
-    ///
-    /// See [`std::io::Seek`] for more details.
-    ///
-    /// Note: In the edge case where you're seeking with `SeekFrom::Current(n)`
-    /// where `n` minus the internal buffer length overflows an `i64`, two
-    /// seeks will be performed instead of one. If the second seek returns
-    /// `Err`, the underlying reader will be left at the same position it would
-    /// have if you called `seek` with `SeekFrom::Current(0)`.
-    ///
-    /// [`BufReader::seek_relative`]: struct.BufReader.html#method.seek_relative
-    /// [`std::io::Seek`]: trait.Seek.html
-    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
-        let result: u64;
-        if let SeekFrom::Current(n) = pos {
-            let remainder = (self.cap - self.pos) as i64;
-            // it should be safe to assume that remainder fits within an i64 as the alternative
-            // means we managed to allocate 8 exbibytes and that's absurd.
-            // But it's not out of the realm of possibility for some weird underlying reader to
-            // support seeking by i64::min_value() so we need to handle underflow when subtracting
-            // remainder.
-            if let Some(offset) = n.checked_sub(remainder) {
-                result = self.inner.seek(SeekFrom::Current(offset))?;
-            } else {
-                // seek backwards by our remainder, and then by the offset
-                self.inner.seek(SeekFrom::Current(-remainder))?;
-                self.discard_buffer();
-                result = self.inner.seek(SeekFrom::Current(n))?;
-            }
-        } else {
-            // Seeking with Start/End doesn't care about our buffer length.
-            result = self.inner.seek(pos)?;
-        }
-        self.discard_buffer();
-        Ok(result)
-    }
-}
-
-/// Wraps a writer and buffers its output.
-///
-/// It can be excessively inefficient to work directly with something that
-/// implements [`Write`]. For example, every call to
-/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
-/// `BufWriter<W>` keeps an in-memory buffer of data and writes it to an underlying
-/// writer in large, infrequent batches.
-///
-/// `BufWriter<W>` can improve the speed of programs that make *small* and
-/// *repeated* write calls to the same file or network socket. It does not
-/// help when writing very large amounts at once, or writing just one or a few
-/// times. It also provides no advantage when writing to a destination that is
-/// in memory, like a `Vec<u8>`.
-///
-/// It is critical to call [`flush`] before `BufWriter<W>` is dropped. Though
-/// dropping will attempt to flush the the contents of the buffer, any errors
-/// that happen in the process of dropping will be ignored. Calling [`flush`]
-/// ensures that the buffer is empty and thus dropping will not even attempt
-/// file operations.
-///
-/// By wrapping the stream with a `BufWriter<W>`, these ten writes are all grouped
-/// together by the buffer and will all be written out in one system call when
-/// the `stream` is flushed.
-///
-/// [`Write`]: ../../std/io/trait.Write.html
-/// [`TcpStream::write`]: ../../std/net/struct.TcpStream.html#method.write
-/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
-/// [`flush`]: #method.flush
-pub struct BufWriter<W: Write> {
-    inner: Option<W>,
-    buf: Vec<u8>,
-    // #30888: If the inner writer panics in a call to write, we don't want to
-    // write the buffered data a second time in BufWriter's destructor. This
-    // flag tells the Drop impl if it should skip the flush.
-    panicked: bool,
-}
-
-/// An error returned by `into_inner` which combines an error that
-/// happened while writing out the buffer, and the buffered writer object
-/// which may be used to recover from the condition.
-///
-#[derive(Debug)]
-pub struct IntoInnerError<W>(W, Error);
-
-impl<W: Write> BufWriter<W> {
-    /// Creates a new `BufWriter<W>` with a default buffer capacity. The default is currently 8 KB,
-    /// but may change in the future.
-    ///
-    pub fn new(inner: W) -> BufWriter<W> {
-        BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
-    }
-
-    /// Creates a new `BufWriter<W>` with the specified buffer capacity.
-    ///
-    pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
-        BufWriter { inner: Some(inner), buf: Vec::with_capacity(capacity), panicked: false }
-    }
-
-    fn flush_buf(&mut self) -> io::Result<()> {
-        let mut written = 0;
-        let len = self.buf.len();
-        let mut ret = Ok(());
-        while written < len {
-            self.panicked = true;
-            let r = self.inner.as_mut().unwrap().write(&self.buf[written..]);
-            self.panicked = false;
-
-            match r {
-                Ok(0) => {
-                    ret =
-                        Err(Error::new(ErrorKind::WriteZero, "failed to write the buffered data"));
-                    break;
-                }
-                Ok(n) => written += n,
-                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
-                Err(e) => {
-                    ret = Err(e);
-                    break;
-                }
-            }
-        }
-        if written > 0 {
-            self.buf.drain(..written);
-        }
-        ret
-    }
-
-    /// Gets a reference to the underlying writer.
-    ///
-    pub fn get_ref(&self) -> &W {
-        self.inner.as_ref().unwrap()
-    }
-
-    /// Gets a mutable reference to the underlying writer.
-    ///
-    /// It is inadvisable to directly write to the underlying writer.
-    ///
-    pub fn get_mut(&mut self) -> &mut W {
-        self.inner.as_mut().unwrap()
-    }
-
-    /// Returns a reference to the internally buffered data.
-    ///
-    pub fn buffer(&self) -> &[u8] {
-        &self.buf
-    }
-
-    /// Returns the number of bytes the internal buffer can hold without flushing.
-    ///
-    pub fn capacity(&self) -> usize {
-        self.buf.capacity()
-    }
-
-    /// Unwraps this `BufWriter<W>`, returning the underlying writer.
-    ///
-    /// The buffer is written out before returning the writer.
-    ///
-    /// # Errors
-    ///
-    /// An `Err` will be returned if an error occurs while flushing the buffer.
-    ///
-    pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
-        match self.flush_buf() {
-            Err(e) => Err(IntoInnerError(self, e)),
-            Ok(()) => Ok(self.inner.take().unwrap()),
-        }
-    }
-}
-
-impl<W: Write> Write for BufWriter<W> {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        if self.buf.len() + buf.len() > self.buf.capacity() {
-            self.flush_buf()?;
-        }
-        if buf.len() >= self.buf.capacity() {
-            self.panicked = true;
-            let r = self.get_mut().write(buf);
-            self.panicked = false;
-            r
-        } else {
-            self.buf.write(buf)
-        }
-    }
-
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
-        if self.buf.len() + total_len > self.buf.capacity() {
-            self.flush_buf()?;
-        }
-        if total_len >= self.buf.capacity() {
-            self.panicked = true;
-            let r = self.get_mut().write_vectored(bufs);
-            self.panicked = false;
-            r
-        } else {
-            self.buf.write_vectored(bufs)
-        }
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        self.flush_buf().and_then(|()| self.get_mut().flush())
-    }
-}
-
-impl<W: Write> fmt::Debug for BufWriter<W>
-where
-    W: fmt::Debug,
-{
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt.debug_struct("BufWriter")
-            .field("writer", &self.inner.as_ref().unwrap())
-            .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
-            .finish()
-    }
-}
-
-impl<W: Write + Seek> Seek for BufWriter<W> {
-    /// Seek to the offset, in bytes, in the underlying writer.
-    ///
-    /// Seeking always writes out the internal buffer before seeking.
-    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
-        self.flush_buf().and_then(|_| self.get_mut().seek(pos))
-    }
-}
-
-impl<W: Write> Drop for BufWriter<W> {
-    fn drop(&mut self) {
-        if self.inner.is_some() && !self.panicked {
-            // dtors should not panic, so we ignore a failed flush
-            let _r = self.flush_buf();
-        }
-    }
-}
-
-impl<W> IntoInnerError<W> {
-    /// Returns the error which caused the call to `into_inner()` to fail.
-    ///
-    /// This error was returned when attempting to write the internal buffer.
-    ///
-    pub fn error(&self) -> &Error {
-        &self.1
-    }
-
-    /// Returns the buffered writer instance which generated the error.
-    ///
-    /// The returned object can be used for error recovery, such as
-    /// re-inspecting the buffer.
-    ///
-    pub fn into_inner(self) -> W {
-        self.0
-    }
-}
-
-impl<W> From<IntoInnerError<W>> for Error {
-    fn from(iie: IntoInnerError<W>) -> Error {
-        iie.1
-    }
-}
-
-impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
-    fn description(&self) -> &str {
-        error::Error::description(self.error())
-    }
-}
-
-impl<W> fmt::Display for IntoInnerError<W> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.error().fmt(f)
-    }
-}
-
-/// Wraps a writer and buffers output to it, flushing whenever a newline
-/// (`0x0a`, `'\n'`) is detected.
-///
-/// The [`BufWriter`][bufwriter] struct wraps a writer and buffers its output.
-/// But it only does this batched write when it goes out of scope, or when the
-/// internal buffer is full. Sometimes, you'd prefer to write each line as it's
-/// completed, rather than the entire buffer at once. Enter `LineWriter`. It
-/// does exactly that.
-///
-/// Like [`BufWriter`][bufwriter], a `LineWriter`’s buffer will also be flushed when the
-/// `LineWriter` goes out of scope or when its internal buffer is full.
-///
-/// [bufwriter]: struct.BufWriter.html
-///
-/// If there's still a partial line in the buffer when the `LineWriter` is
-/// dropped, it will flush those contents.
-///
-pub struct LineWriter<W: Write> {
-    inner: BufWriter<W>,
-    need_flush: bool,
-}
-
-impl<W: Write> LineWriter<W> {
-    /// Creates a new `LineWriter`.
-    ///
-    pub fn new(inner: W) -> LineWriter<W> {
-        // Lines typically aren't that long, don't use a giant buffer
-        LineWriter::with_capacity(1024, inner)
-    }
-
-    /// Creates a new `LineWriter` with a specified capacity for the internal
-    /// buffer.
-    ///
-    pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
-        LineWriter { inner: BufWriter::with_capacity(capacity, inner), need_flush: false }
-    }
-
-    /// Gets a reference to the underlying writer.
-    ///
-    pub fn get_ref(&self) -> &W {
-        self.inner.get_ref()
-    }
-
-    /// Gets a mutable reference to the underlying writer.
-    ///
-    /// Caution must be taken when calling methods on the mutable reference
-    /// returned as extra writes could corrupt the output stream.
-    ///
-    pub fn get_mut(&mut self) -> &mut W {
-        self.inner.get_mut()
-    }
-
-    /// Unwraps this `LineWriter`, returning the underlying writer.
-    ///
-    /// The internal buffer is written out before returning the writer.
-    ///
-    /// # Errors
-    ///
-    /// An `Err` will be returned if an error occurs while flushing the buffer.
-    ///
-    pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
-        self.inner.into_inner().map_err(|IntoInnerError(buf, e)| {
-            IntoInnerError(LineWriter { inner: buf, need_flush: false }, e)
-        })
-    }
-}
-
-impl<W: Write> Write for LineWriter<W> {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        if self.need_flush {
-            self.flush()?;
-        }
-
-        // Find the last newline character in the buffer provided. If found then
-        // we're going to write all the data up to that point and then flush,
-        // otherwise we just write the whole block to the underlying writer.
-        let i = match memchr::memrchr(b'\n', buf) {
-            Some(i) => i,
-            None => return self.inner.write(buf),
-        };
-
-        // Ok, we're going to write a partial amount of the data given first
-        // followed by flushing the newline. After we've successfully written
-        // some data then we *must* report that we wrote that data, so future
-        // errors are ignored. We set our internal `need_flush` flag, though, in
-        // case flushing fails and we need to try it first next time.
-        let n = self.inner.write(&buf[..=i])?;
-        self.need_flush = true;
-        if self.flush().is_err() || n != i + 1 {
-            return Ok(n);
-        }
-
-        // At this point we successfully wrote `i + 1` bytes and flushed it out,
-        // meaning that the entire line is now flushed out on the screen. While
-        // we can attempt to finish writing the rest of the data provided.
-        // Remember though that we ignore errors here as we've successfully
-        // written data, so we need to report that.
-        match self.inner.write(&buf[i + 1..]) {
-            Ok(i) => Ok(n + i),
-            Err(_) => Ok(n),
-        }
-    }
-
-    // Vectored writes are very similar to the writes above, but adjusted for
-    // the list of buffers that we have to write.
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        if self.need_flush {
-            self.flush()?;
-        }
-
-        // Find the last newline, and failing that write the whole buffer
-        let last_newline = bufs
-            .iter()
-            .enumerate()
-            .rev()
-            .filter_map(|(i, buf)| {
-                let pos = memchr::memrchr(b'\n', buf)?;
-                Some((i, pos))
-            })
-            .next();
-        let (i, j) = match last_newline {
-            Some(pair) => pair,
-            None => return self.inner.write_vectored(bufs),
-        };
-        let (prefix, suffix) = bufs.split_at(i);
-        let (buf, suffix) = suffix.split_at(1);
-        let buf = &buf[0];
-
-        // Write everything up to the last newline, flushing afterwards. Note
-        // that only if we finished our entire `write_vectored` do we try the
-        // subsequent
-        // `write`
-        let mut n = 0;
-        let prefix_amt = prefix.iter().map(|i| i.len()).sum();
-        if prefix_amt > 0 {
-            n += self.inner.write_vectored(prefix)?;
-            self.need_flush = true;
-        }
-        if n == prefix_amt {
-            match self.inner.write(&buf[..=j]) {
-                Ok(m) => n += m,
-                Err(e) if n == 0 => return Err(e),
-                Err(_) => return Ok(n),
-            }
-            self.need_flush = true;
-        }
-        if self.flush().is_err() || n != j + 1 + prefix_amt {
-            return Ok(n);
-        }
-
-        // ... and now write out everything remaining
-        match self.inner.write(&buf[j + 1..]) {
-            Ok(i) => n += i,
-            Err(_) => return Ok(n),
-        }
-
-        if suffix.iter().map(|s| s.len()).sum::<usize>() == 0 {
-            return Ok(n);
-        }
-        match self.inner.write_vectored(suffix) {
-            Ok(i) => Ok(n + i),
-            Err(_) => Ok(n),
-        }
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        self.inner.flush()?;
-        self.need_flush = false;
-        Ok(())
-    }
-}
-
-impl<W: Write> fmt::Debug for LineWriter<W>
-where
-    W: fmt::Debug,
-{
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt.debug_struct("LineWriter")
-            .field("writer", &self.inner.inner)
-            .field(
-                "buffer",
-                &format_args!("{}/{}", self.inner.buf.len(), self.inner.buf.capacity()),
-            )
-            .finish()
-    }
-}
-
diff --git a/sgx_tstd/src/io/buffered/bufreader.rs b/sgx_tstd/src/io/buffered/bufreader.rs
new file mode 100644
index 0000000..f41ca9b
--- /dev/null
+++ b/sgx_tstd/src/io/buffered/bufreader.rs
@@ -0,0 +1,455 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::cmp;
+use crate::fmt;
+use crate::io::{
+    self, BufRead, Initializer, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE,
+};
+
+/// The `BufReader<R>` struct adds buffering to any reader.
+///
+/// It can be excessively inefficient to work directly with a [`Read`] instance.
+/// For example, every call to [`read`][`TcpStream::read`] on [`TcpStream`]
+/// results in a system call. A `BufReader<R>` performs large, infrequent reads on
+/// the underlying [`Read`] and maintains an in-memory buffer of the results.
+///
+/// `BufReader<R>` can improve the speed of programs that make *small* and
+/// *repeated* read calls to the same file or network socket. It does not
+/// help when reading very large amounts at once, or reading just one or a few
+/// times. It also provides no advantage when reading from a source that is
+/// already in memory, like a [`Vec`]`<u8>`.
+///
+/// When the `BufReader<R>` is dropped, the contents of its buffer will be
+/// discarded. Creating multiple instances of a `BufReader<R>` on the same
+/// stream can cause data loss. Reading from the underlying reader after
+/// unwrapping the `BufReader<R>` with [`BufReader::into_inner`] can also cause
+/// data loss.
+///
+// HACK(#78696): can't use `crate` for associated items
+/// [`TcpStream::read`]: super::super::super::net::TcpStream::read
+/// [`TcpStream`]: crate::net::TcpStream
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::BufReader;
+/// use std::fs::File;
+///
+/// fn main() -> std::io::Result<()> {
+///     let f = File::open("log.txt")?;
+///     let mut reader = BufReader::new(f);
+///
+///     let mut line = String::new();
+///     let len = reader.read_line(&mut line)?;
+///     println!("First line is {} bytes long", len);
+///     Ok(())
+/// }
+/// ```
+pub struct BufReader<R> {
+    inner: R,
+    buf: Box<[u8]>,
+    pos: usize,
+    cap: usize,
+}
+
+impl<R: Read> BufReader<R> {
+    /// Creates a new `BufReader<R>` with a default buffer capacity. The default is currently 8 KB,
+    /// but may change in the future.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufReader;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::open("log.txt")?;
+    ///     let reader = BufReader::new(f);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn new(inner: R) -> BufReader<R> {
+        BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
+    }
+
+    /// Creates a new `BufReader<R>` with the specified buffer capacity.
+    ///
+    /// # Examples
+    ///
+    /// Creating a buffer with ten bytes of capacity:
+    ///
+    /// ```no_run
+    /// use std::io::BufReader;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::open("log.txt")?;
+    ///     let reader = BufReader::with_capacity(10, f);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
+        unsafe {
+            let mut buf = Box::new_uninit_slice(capacity).assume_init();
+            inner.initializer().initialize(&mut buf);
+            BufReader { inner, buf, pos: 0, cap: 0 }
+        }
+    }
+}
+
+impl<R> BufReader<R> {
+    /// Gets a reference to the underlying reader.
+    ///
+    /// It is inadvisable to directly read from the underlying reader.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufReader;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f1 = File::open("log.txt")?;
+    ///     let reader = BufReader::new(f1);
+    ///
+    ///     let f2 = reader.get_ref();
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn get_ref(&self) -> &R {
+        &self.inner
+    }
+
+    /// Gets a mutable reference to the underlying reader.
+    ///
+    /// It is inadvisable to directly read from the underlying reader.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufReader;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f1 = File::open("log.txt")?;
+    ///     let mut reader = BufReader::new(f1);
+    ///
+    ///     let f2 = reader.get_mut();
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn get_mut(&mut self) -> &mut R {
+        &mut self.inner
+    }
+
+    /// Returns a reference to the internally buffered data.
+    ///
+    /// Unlike [`fill_buf`], this will not attempt to fill the buffer if it is empty.
+    ///
+    /// [`fill_buf`]: BufRead::fill_buf
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::{BufReader, BufRead};
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::open("log.txt")?;
+    ///     let mut reader = BufReader::new(f);
+    ///     assert!(reader.buffer().is_empty());
+    ///
+    ///     if reader.fill_buf()?.len() > 0 {
+    ///         assert!(!reader.buffer().is_empty());
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn buffer(&self) -> &[u8] {
+        &self.buf[self.pos..self.cap]
+    }
+
+    /// Returns the number of bytes the internal buffer can hold at once.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::{BufReader, BufRead};
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::open("log.txt")?;
+    ///     let mut reader = BufReader::new(f);
+    ///
+    ///     let capacity = reader.capacity();
+    ///     let buffer = reader.fill_buf()?;
+    ///     assert!(buffer.len() <= capacity);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn capacity(&self) -> usize {
+        self.buf.len()
+    }
+
+    /// Unwraps this `BufReader<R>`, returning the underlying reader.
+    ///
+    /// Note that any leftover data in the internal buffer is lost. Therefore,
+    /// a following read from the underlying reader may lead to data loss.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufReader;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f1 = File::open("log.txt")?;
+    ///     let reader = BufReader::new(f1);
+    ///
+    ///     let f2 = reader.into_inner();
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn into_inner(self) -> R {
+        self.inner
+    }
+
+    /// Invalidates all data in the internal buffer.
+    #[inline]
+    fn discard_buffer(&mut self) {
+        self.pos = 0;
+        self.cap = 0;
+    }
+}
+
+impl<R: Seek> BufReader<R> {
+    /// Seeks relative to the current position. If the new position lies within the buffer,
+    /// the buffer will not be flushed, allowing for more efficient seeks.
+    /// This method does not return the location of the underlying reader, so the caller
+    /// must track this information themselves if it is required.
+    #[allow(clippy::collapsible_else_if)]
+    pub fn seek_relative(&mut self, offset: i64) -> io::Result<()> {
+        let pos = self.pos as u64;
+        if offset < 0 {
+            if let Some(new_pos) = pos.checked_sub((-offset) as u64) {
+                self.pos = new_pos as usize;
+                return Ok(());
+            }
+        } else {
+            if let Some(new_pos) = pos.checked_add(offset as u64) {
+                if new_pos <= self.cap as u64 {
+                    self.pos = new_pos as usize;
+                    return Ok(());
+                }
+            }
+        }
+        self.seek(SeekFrom::Current(offset)).map(drop)
+    }
+}
+
+impl<R: Read> Read for BufReader<R> {
+    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+        // If we don't have any buffered data and we're doing a massive read
+        // (larger than our internal buffer), bypass our internal buffer
+        // entirely.
+        if self.pos == self.cap && buf.len() >= self.buf.len() {
+            self.discard_buffer();
+            return self.inner.read(buf);
+        }
+        let nread = {
+            let mut rem = self.fill_buf()?;
+            rem.read(buf)?
+        };
+        self.consume(nread);
+        Ok(nread)
+    }
+
+    // Small read_exacts from a BufReader are extremely common when used with a deserializer.
+    // The default implementation calls read in a loop, which results in surprisingly poor code
+    // generation for the common path where the buffer has enough bytes to fill the passed-in
+    // buffer.
+    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+        if self.buffer().len() >= buf.len() {
+            buf.copy_from_slice(&self.buffer()[..buf.len()]);
+            self.consume(buf.len());
+            return Ok(());
+        }
+
+        crate::io::default_read_exact(self, buf)
+    }
+
+    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+        let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
+        if self.pos == self.cap && total_len >= self.buf.len() {
+            self.discard_buffer();
+            return self.inner.read_vectored(bufs);
+        }
+        let nread = {
+            let mut rem = self.fill_buf()?;
+            rem.read_vectored(bufs)?
+        };
+        self.consume(nread);
+        Ok(nread)
+    }
+
+    fn is_read_vectored(&self) -> bool {
+        self.inner.is_read_vectored()
+    }
+
+    // we can't skip unconditionally because of the large buffer case in read.
+    unsafe fn initializer(&self) -> Initializer {
+        self.inner.initializer()
+    }
+}
+
+impl<R: Read> BufRead for BufReader<R> {
+    fn fill_buf(&mut self) -> io::Result<&[u8]> {
+        // If we've reached the end of our internal buffer then we need to fetch
+        // some more data from the underlying reader.
+        // Branch using `>=` instead of the more correct `==`
+        // to tell the compiler that the pos..cap slice is always valid.
+        if self.pos >= self.cap {
+            debug_assert!(self.pos == self.cap);
+            self.cap = self.inner.read(&mut self.buf)?;
+            self.pos = 0;
+        }
+        Ok(&self.buf[self.pos..self.cap])
+    }
+
+    fn consume(&mut self, amt: usize) {
+        self.pos = cmp::min(self.pos + amt, self.cap);
+    }
+}
+
+impl<R> fmt::Debug for BufReader<R>
+where
+    R: fmt::Debug,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt.debug_struct("BufReader")
+            .field("reader", &self.inner)
+            .field("buffer", &format_args!("{}/{}", self.cap - self.pos, self.buf.len()))
+            .finish()
+    }
+}
+
+impl<R: Seek> Seek for BufReader<R> {
+    /// Seek to an offset, in bytes, in the underlying reader.
+    ///
+    /// The position used for seeking with [`SeekFrom::Current`]`(_)` is the
+    /// position the underlying reader would be at if the `BufReader<R>` had no
+    /// internal buffer.
+    ///
+    /// Seeking always discards the internal buffer, even if the seek position
+    /// would otherwise fall within it. This guarantees that calling
+    /// [`BufReader::into_inner()`] immediately after a seek yields the underlying reader
+    /// at the same position.
+    ///
+    /// To seek without discarding the internal buffer, use [`BufReader::seek_relative`].
+    ///
+    /// See [`std::io::Seek`] for more details.
+    ///
+    /// Note: In the edge case where you're seeking with [`SeekFrom::Current`]`(n)`
+    /// where `n` minus the internal buffer length overflows an `i64`, two
+    /// seeks will be performed instead of one. If the second seek returns
+    /// [`Err`], the underlying reader will be left at the same position it would
+    /// have if you called `seek` with [`SeekFrom::Current`]`(0)`.
+    ///
+    /// [`std::io::Seek`]: Seek
+    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+        let result: u64;
+        if let SeekFrom::Current(n) = pos {
+            let remainder = (self.cap - self.pos) as i64;
+            // it should be safe to assume that remainder fits within an i64 as the alternative
+            // means we managed to allocate 8 exbibytes and that's absurd.
+            // But it's not out of the realm of possibility for some weird underlying reader to
+            // support seeking by i64::MIN so we need to handle underflow when subtracting
+            // remainder.
+            if let Some(offset) = n.checked_sub(remainder) {
+                result = self.inner.seek(SeekFrom::Current(offset))?;
+            } else {
+                // seek backwards by our remainder, and then by the offset
+                self.inner.seek(SeekFrom::Current(-remainder))?;
+                self.discard_buffer();
+                result = self.inner.seek(SeekFrom::Current(n))?;
+            }
+        } else {
+            // Seeking with Start/End doesn't care about our buffer length.
+            result = self.inner.seek(pos)?;
+        }
+        self.discard_buffer();
+        Ok(result)
+    }
+
+    /// Returns the current seek position from the start of the stream.
+    ///
+    /// The value returned is equivalent to `self.seek(SeekFrom::Current(0))`
+    /// but does not flush the internal buffer. Due to this optimization the
+    /// function does not guarantee that calling `.into_inner()` immediately
+    /// afterwards will yield the underlying reader at the same position. Use
+    /// [`BufReader::seek`] instead if you require that guarantee.
+    ///
+    /// # Panics
+    ///
+    /// This function will panic if the position of the inner reader is smaller
+    /// than the amount of buffered data. That can happen if the inner reader
+    /// has an incorrect implementation of [`Seek::stream_position`], or if the
+    /// position has gone out of sync due to calling [`Seek::seek`] directly on
+    /// the underlying reader.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::{
+    ///     io::{self, BufRead, BufReader, Seek},
+    ///     fs::File,
+    /// };
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = BufReader::new(File::open("foo.txt")?);
+    ///
+    ///     let before = f.stream_position()?;
+    ///     f.read_line(&mut String::new())?;
+    ///     let after = f.stream_position()?;
+    ///
+    ///     println!("The first line was {} bytes long", after - before);
+    ///     Ok(())
+    /// }
+    /// ```
+    fn stream_position(&mut self) -> io::Result<u64> {
+        let remainder = (self.cap - self.pos) as u64;
+        self.inner.stream_position().map(|pos| {
+            pos.checked_sub(remainder).expect(
+                "overflow when subtracting remaining buffer size from inner stream position",
+            )
+        })
+    }
+}
+
+impl<T> SizeHint for BufReader<T> {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        SizeHint::lower_bound(self.get_ref()) + self.buffer().len()
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        SizeHint::upper_bound(self.get_ref()).and_then(|up| self.buffer().len().checked_add(up))
+    }
+}
diff --git a/sgx_tstd/src/io/buffered/bufwriter.rs b/sgx_tstd/src/io/buffered/bufwriter.rs
new file mode 100644
index 0000000..981b942
--- /dev/null
+++ b/sgx_tstd/src/io/buffered/bufwriter.rs
@@ -0,0 +1,672 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::error;
+use crate::fmt;
+use crate::io::{
+    self, Error, ErrorKind, IntoInnerError, IoSlice, Seek, SeekFrom, Write, DEFAULT_BUF_SIZE,
+};
+use crate::mem;
+use crate::ptr;
+
+/// Wraps a writer and buffers its output.
+///
+/// It can be excessively inefficient to work directly with something that
+/// implements [`Write`]. For example, every call to
+/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
+/// `BufWriter<W>` keeps an in-memory buffer of data and writes it to an underlying
+/// writer in large, infrequent batches.
+///
+/// `BufWriter<W>` can improve the speed of programs that make *small* and
+/// *repeated* write calls to the same file or network socket. It does not
+/// help when writing very large amounts at once, or writing just one or a few
+/// times. It also provides no advantage when writing to a destination that is
+/// in memory, like a [`Vec`]`<u8>`.
+///
+/// It is critical to call [`flush`] before `BufWriter<W>` is dropped. Though
+/// dropping will attempt to flush the contents of the buffer, any errors
+/// that happen in the process of dropping will be ignored. Calling [`flush`]
+/// ensures that the buffer is empty and thus dropping will not even attempt
+/// file operations.
+///
+/// # Examples
+///
+/// Let's write the numbers one through ten to a [`TcpStream`]:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::net::TcpStream;
+///
+/// let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+///
+/// for i in 0..10 {
+///     stream.write(&[i+1]).unwrap();
+/// }
+/// ```
+///
+/// Because we're not buffering, we write each one in turn, incurring the
+/// overhead of a system call per byte written. We can fix this with a
+/// `BufWriter<W>`:
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// for i in 0..10 {
+///     stream.write(&[i+1]).unwrap();
+/// }
+/// stream.flush().unwrap();
+/// ```
+///
+/// By wrapping the stream with a `BufWriter<W>`, these ten writes are all grouped
+/// together by the buffer and will all be written out in one system call when
+/// the `stream` is flushed.
+///
+// HACK(#78696): can't use `crate` for associated items
+/// [`TcpStream::write`]: super::super::super::net::TcpStream::write
+/// [`TcpStream`]: crate::net::TcpStream
+/// [`flush`]: BufWriter::flush
+pub struct BufWriter<W: Write> {
+    inner: W,
+    // The buffer. Avoid using this like a normal `Vec` in common code paths.
+    // That is, don't use `buf.push`, `buf.extend_from_slice`, or any other
+    // methods that require bounds checking or the like. This makes an enormous
+    // difference to performance (we may want to stop using a `Vec` entirely).
+    buf: Vec<u8>,
+    // #30888: If the inner writer panics in a call to write, we don't want to
+    // write the buffered data a second time in BufWriter's destructor. This
+    // flag tells the Drop impl if it should skip the flush.
+    panicked: bool,
+}
+
+impl<W: Write> BufWriter<W> {
+    /// Creates a new `BufWriter<W>` with a default buffer capacity. The default is currently 8 KB,
+    /// but may change in the future.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    /// ```
+    pub fn new(inner: W) -> BufWriter<W> {
+        BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
+    }
+
+    /// Creates a new `BufWriter<W>` with the specified buffer capacity.
+    ///
+    /// # Examples
+    ///
+    /// Creating a buffer with a buffer of a hundred bytes.
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
+    /// let mut buffer = BufWriter::with_capacity(100, stream);
+    /// ```
+    pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
+        BufWriter { inner, buf: Vec::with_capacity(capacity), panicked: false }
+    }
+
+    /// Send data in our local buffer into the inner writer, looping as
+    /// necessary until either it's all been sent or an error occurs.
+    ///
+    /// Because all the data in the buffer has been reported to our owner as
+    /// "successfully written" (by returning nonzero success values from
+    /// `write`), any 0-length writes from `inner` must be reported as i/o
+    /// errors from this method.
+    pub(in crate::io) fn flush_buf(&mut self) -> io::Result<()> {
+        /// Helper struct to ensure the buffer is updated after all the writes
+        /// are complete. It tracks the number of written bytes and drains them
+        /// all from the front of the buffer when dropped.
+        struct BufGuard<'a> {
+            buffer: &'a mut Vec<u8>,
+            written: usize,
+        }
+
+        impl<'a> BufGuard<'a> {
+            fn new(buffer: &'a mut Vec<u8>) -> Self {
+                Self { buffer, written: 0 }
+            }
+
+            /// The unwritten part of the buffer
+            fn remaining(&self) -> &[u8] {
+                &self.buffer[self.written..]
+            }
+
+            /// Flag some bytes as removed from the front of the buffer
+            fn consume(&mut self, amt: usize) {
+                self.written += amt;
+            }
+
+            /// true if all of the bytes have been written
+            fn done(&self) -> bool {
+                self.written >= self.buffer.len()
+            }
+        }
+
+        impl Drop for BufGuard<'_> {
+            fn drop(&mut self) {
+                if self.written > 0 {
+                    self.buffer.drain(..self.written);
+                }
+            }
+        }
+
+        let mut guard = BufGuard::new(&mut self.buf);
+        while !guard.done() {
+            self.panicked = true;
+            let r = self.inner.write(guard.remaining());
+            self.panicked = false;
+
+            match r {
+                Ok(0) => {
+                    return Err(Error::new_const(
+                        ErrorKind::WriteZero,
+                        &"failed to write the buffered data",
+                    ));
+                }
+                Ok(n) => guard.consume(n),
+                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
+                Err(e) => return Err(e),
+            }
+        }
+        Ok(())
+    }
+
+    /// Buffer some data without flushing it, regardless of the size of the
+    /// data. Writes as much as possible without exceeding capacity. Returns
+    /// the number of bytes written.
+    pub(super) fn write_to_buf(&mut self, buf: &[u8]) -> usize {
+        let available = self.spare_capacity();
+        let amt_to_buffer = available.min(buf.len());
+
+        // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
+        unsafe {
+            self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
+        }
+
+        amt_to_buffer
+    }
+
+    /// Gets a reference to the underlying writer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // we can use reference just like buffer
+    /// let reference = buffer.get_ref();
+    /// ```
+    pub fn get_ref(&self) -> &W {
+        &self.inner
+    }
+
+    /// Gets a mutable reference to the underlying writer.
+    ///
+    /// It is inadvisable to directly write to the underlying writer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // we can use reference just like buffer
+    /// let reference = buffer.get_mut();
+    /// ```
+    pub fn get_mut(&mut self) -> &mut W {
+        &mut self.inner
+    }
+
+    /// Returns a reference to the internally buffered data.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // See how many bytes are currently buffered
+    /// let bytes_buffered = buf_writer.buffer().len();
+    /// ```
+    pub fn buffer(&self) -> &[u8] {
+        &self.buf
+    }
+
+    /// Returns a mutable reference to the internal buffer.
+    ///
+    /// This can be used to write data directly into the buffer without triggering writers
+    /// to the underlying writer.
+    ///
+    /// That the buffer is a `Vec` is an implementation detail.
+    /// Callers should not modify the capacity as there currently is no public API to do so
+    /// and thus any capacity changes would be unexpected by the user.
+    pub(in crate::io) fn buffer_mut(&mut self) -> &mut Vec<u8> {
+        &mut self.buf
+    }
+
+    /// Returns the number of bytes the internal buffer can hold without flushing.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // Check the capacity of the inner buffer
+    /// let capacity = buf_writer.capacity();
+    /// // Calculate how many bytes can be written without flushing
+    /// let without_flush = capacity - buf_writer.buffer().len();
+    /// ```
+    pub fn capacity(&self) -> usize {
+        self.buf.capacity()
+    }
+
+    /// Unwraps this `BufWriter<W>`, returning the underlying writer.
+    ///
+    /// The buffer is written out before returning the writer.
+    ///
+    /// # Errors
+    ///
+    /// An [`Err`] will be returned if an error occurs while flushing the buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // unwrap the TcpStream and flush the buffer
+    /// let stream = buffer.into_inner().unwrap();
+    /// ```
+    pub fn into_inner(mut self) -> Result<W, IntoInnerError<BufWriter<W>>> {
+        match self.flush_buf() {
+            Err(e) => Err(IntoInnerError::new(self, e)),
+            Ok(()) => Ok(self.into_parts().0),
+        }
+    }
+
+    /// Disassembles this `BufWriter<W>`, returning the underlying writer, and any buffered but
+    /// unwritten data.
+    ///
+    /// If the underlying writer panicked, it is not known what portion of the data was written.
+    /// In this case, we return `WriterPanicked` for the buffered data (from which the buffer
+    /// contents can still be recovered).
+    ///
+    /// `into_parts` makes no attempt to flush data and cannot fail.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{BufWriter, Write};
+    ///
+    /// let mut buffer = [0u8; 10];
+    /// let mut stream = BufWriter::new(buffer.as_mut());
+    /// write!(stream, "too much data").unwrap();
+    /// stream.flush().expect_err("it doesn't fit");
+    /// let (recovered_writer, buffered_data) = stream.into_parts();
+    /// assert_eq!(recovered_writer.len(), 0);
+    /// assert_eq!(&buffered_data.unwrap(), b"ata");
+    /// ```
+    pub fn into_parts(mut self) -> (W, Result<Vec<u8>, WriterPanicked>) {
+        let buf = mem::take(&mut self.buf);
+        let buf = if !self.panicked { Ok(buf) } else { Err(WriterPanicked { buf }) };
+
+        // SAFETY: forget(self) prevents double dropping inner
+        let inner = unsafe { ptr::read(&self.inner) };
+        mem::forget(self);
+
+        (inner, buf)
+    }
+
+    // Ensure this function does not get inlined into `write`, so that it
+    // remains inlineable and its common path remains as short as possible.
+    // If this function ends up being called frequently relative to `write`,
+    // it's likely a sign that the client is using an improperly sized buffer
+    // or their write patterns are somewhat pathological.
+    #[cold]
+    #[inline(never)]
+    fn write_cold(&mut self, buf: &[u8]) -> io::Result<usize> {
+        if buf.len() > self.spare_capacity() {
+            self.flush_buf()?;
+        }
+
+        // Why not len > capacity? To avoid a needless trip through the buffer when the input
+        // exactly fills it. We'd just need to flush it to the underlying writer anyway.
+        if buf.len() >= self.buf.capacity() {
+            self.panicked = true;
+            let r = self.get_mut().write(buf);
+            self.panicked = false;
+            r
+        } else {
+            // Write to the buffer. In this case, we write to the buffer even if it fills it
+            // exactly. Doing otherwise would mean flushing the buffer, then writing this
+            // input to the inner writer, which in many cases would be a worse strategy.
+
+            // SAFETY: There was either enough spare capacity already, or there wasn't and we
+            // flushed the buffer to ensure that there is. In the latter case, we know that there
+            // is because flushing ensured that our entire buffer is spare capacity, and we entered
+            // this block because the input buffer length is less than that capacity. In either
+            // case, it's safe to write the input buffer to our buffer.
+            unsafe {
+                self.write_to_buffer_unchecked(buf);
+            }
+
+            Ok(buf.len())
+        }
+    }
+
+    // Ensure this function does not get inlined into `write_all`, so that it
+    // remains inlineable and its common path remains as short as possible.
+    // If this function ends up being called frequently relative to `write_all`,
+    // it's likely a sign that the client is using an improperly sized buffer
+    // or their write patterns are somewhat pathological.
+    #[cold]
+    #[inline(never)]
+    fn write_all_cold(&mut self, buf: &[u8]) -> io::Result<()> {
+        // Normally, `write_all` just calls `write` in a loop. We can do better
+        // by calling `self.get_mut().write_all()` directly, which avoids
+        // round trips through the buffer in the event of a series of partial
+        // writes in some circumstances.
+
+        if buf.len() > self.spare_capacity() {
+            self.flush_buf()?;
+        }
+
+        // Why not len > capacity? To avoid a needless trip through the buffer when the input
+        // exactly fills it. We'd just need to flush it to the underlying writer anyway.
+        if buf.len() >= self.buf.capacity() {
+            self.panicked = true;
+            let r = self.get_mut().write_all(buf);
+            self.panicked = false;
+            r
+        } else {
+            // Write to the buffer. In this case, we write to the buffer even if it fills it
+            // exactly. Doing otherwise would mean flushing the buffer, then writing this
+            // input to the inner writer, which in many cases would be a worse strategy.
+
+            // SAFETY: There was either enough spare capacity already, or there wasn't and we
+            // flushed the buffer to ensure that there is. In the latter case, we know that there
+            // is because flushing ensured that our entire buffer is spare capacity, and we entered
+            // this block because the input buffer length is less than that capacity. In either
+            // case, it's safe to write the input buffer to our buffer.
+            unsafe {
+                self.write_to_buffer_unchecked(buf);
+            }
+
+            Ok(())
+        }
+    }
+
+    // SAFETY: Requires `buf.len() <= self.buf.capacity() - self.buf.len()`,
+    // i.e., that input buffer length is less than or equal to spare capacity.
+    #[inline]
+    unsafe fn write_to_buffer_unchecked(&mut self, buf: &[u8]) {
+        debug_assert!(buf.len() <= self.spare_capacity());
+        let old_len = self.buf.len();
+        let buf_len = buf.len();
+        let src = buf.as_ptr();
+        let dst = self.buf.as_mut_ptr().add(old_len);
+        ptr::copy_nonoverlapping(src, dst, buf_len);
+        self.buf.set_len(old_len + buf_len);
+    }
+
+    #[inline]
+    fn spare_capacity(&self) -> usize {
+        self.buf.capacity() - self.buf.len()
+    }
+}
+
+/// Error returned for the buffered data from `BufWriter::into_parts`, when the underlying
+/// writer has previously panicked.  Contains the (possibly partly written) buffered data.
+///
+/// # Example
+///
+/// ```
+/// use std::io::{self, BufWriter, Write};
+/// use std::panic::{catch_unwind, AssertUnwindSafe};
+///
+/// struct PanickingWriter;
+/// impl Write for PanickingWriter {
+///   fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
+///   fn flush(&mut self) -> io::Result<()> { panic!() }
+/// }
+///
+/// let mut stream = BufWriter::new(PanickingWriter);
+/// write!(stream, "some data").unwrap();
+/// let result = catch_unwind(AssertUnwindSafe(|| {
+///     stream.flush().unwrap()
+/// }));
+/// assert!(result.is_err());
+/// let (recovered_writer, buffered_data) = stream.into_parts();
+/// assert!(matches!(recovered_writer, PanickingWriter));
+/// assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");
+/// ```
+pub struct WriterPanicked {
+    buf: Vec<u8>,
+}
+
+impl WriterPanicked {
+    /// Returns the perhaps-unwritten data.  Some of this data may have been written by the
+    /// panicking call(s) to the underlying writer, so simply writing it again is not a good idea.
+    pub fn into_inner(self) -> Vec<u8> {
+        self.buf
+    }
+
+    const DESCRIPTION: &'static str =
+        "BufWriter inner writer panicked, what data remains unwritten is not known";
+}
+
+impl error::Error for WriterPanicked {
+    #[allow(deprecated, deprecated_in_future)]
+    fn description(&self) -> &str {
+        Self::DESCRIPTION
+    }
+}
+
+impl fmt::Display for WriterPanicked {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", Self::DESCRIPTION)
+    }
+}
+
+impl fmt::Debug for WriterPanicked {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("WriterPanicked")
+            .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
+            .finish()
+    }
+}
+
+impl<W: Write> Write for BufWriter<W> {
+    #[inline]
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        // Use < instead of <= to avoid a needless trip through the buffer in some cases.
+        // See `write_cold` for details.
+        if buf.len() < self.spare_capacity() {
+            // SAFETY: safe by above conditional.
+            unsafe {
+                self.write_to_buffer_unchecked(buf);
+            }
+
+            Ok(buf.len())
+        } else {
+            self.write_cold(buf)
+        }
+    }
+
+    #[inline]
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        // Use < instead of <= to avoid a needless trip through the buffer in some cases.
+        // See `write_all_cold` for details.
+        if buf.len() < self.spare_capacity() {
+            // SAFETY: safe by above conditional.
+            unsafe {
+                self.write_to_buffer_unchecked(buf);
+            }
+
+            Ok(())
+        } else {
+            self.write_all_cold(buf)
+        }
+    }
+
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        // FIXME: Consider applying `#[inline]` / `#[inline(never)]` optimizations already applied
+        // to `write` and `write_all`. The performance benefits can be significant. See #79930.
+        if self.get_ref().is_write_vectored() {
+            // We have to handle the possibility that the total length of the buffers overflows
+            // `usize` (even though this can only happen if multiple `IoSlice`s reference the
+            // same underlying buffer, as otherwise the buffers wouldn't fit in memory). If the
+            // computation overflows, then surely the input cannot fit in our buffer, so we forward
+            // to the inner writer's `write_vectored` method to let it handle it appropriately.
+            let saturated_total_len =
+                bufs.iter().fold(0usize, |acc, b| acc.saturating_add(b.len()));
+
+            if saturated_total_len > self.spare_capacity() {
+                // Flush if the total length of the input exceeds our buffer's spare capacity.
+                // If we would have overflowed, this condition also holds, and we need to flush.
+                self.flush_buf()?;
+            }
+
+            if saturated_total_len >= self.buf.capacity() {
+                // Forward to our inner writer if the total length of the input is greater than or
+                // equal to our buffer capacity. If we would have overflowed, this condition also
+                // holds, and we punt to the inner writer.
+                self.panicked = true;
+                let r = self.get_mut().write_vectored(bufs);
+                self.panicked = false;
+                r
+            } else {
+                // `saturated_total_len < self.buf.capacity()` implies that we did not saturate.
+
+                // SAFETY: We checked whether or not the spare capacity was large enough above. If
+                // it was, then we're safe already. If it wasn't, we flushed, making sufficient
+                // room for any input <= the buffer size, which includes this input.
+                unsafe {
+                    bufs.iter().for_each(|b| self.write_to_buffer_unchecked(b));
+                };
+
+                Ok(saturated_total_len)
+            }
+        } else {
+            let mut iter = bufs.iter();
+            let mut total_written = if let Some(buf) = iter.by_ref().find(|&buf| !buf.is_empty()) {
+                // This is the first non-empty slice to write, so if it does
+                // not fit in the buffer, we still get to flush and proceed.
+                if buf.len() > self.spare_capacity() {
+                    self.flush_buf()?;
+                }
+                if buf.len() >= self.buf.capacity() {
+                    // The slice is at least as large as the buffering capacity,
+                    // so it's better to write it directly, bypassing the buffer.
+                    self.panicked = true;
+                    let r = self.get_mut().write(buf);
+                    self.panicked = false;
+                    return r;
+                } else {
+                    // SAFETY: We checked whether or not the spare capacity was large enough above.
+                    // If it was, then we're safe already. If it wasn't, we flushed, making
+                    // sufficient room for any input <= the buffer size, which includes this input.
+                    unsafe {
+                        self.write_to_buffer_unchecked(buf);
+                    }
+
+                    buf.len()
+                }
+            } else {
+                return Ok(0);
+            };
+            debug_assert!(total_written != 0);
+            for buf in iter {
+                if buf.len() <= self.spare_capacity() {
+                    // SAFETY: safe by above conditional.
+                    unsafe {
+                        self.write_to_buffer_unchecked(buf);
+                    }
+
+                    // This cannot overflow `usize`. If we are here, we've written all of the bytes
+                    // so far to our buffer, and we've ensured that we never exceed the buffer's
+                    // capacity. Therefore, `total_written` <= `self.buf.capacity()` <= `usize::MAX`.
+                    total_written += buf.len();
+                } else {
+                    break;
+                }
+            }
+            Ok(total_written)
+        }
+    }
+
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        self.flush_buf().and_then(|()| self.get_mut().flush())
+    }
+}
+
+impl<W: Write> fmt::Debug for BufWriter<W>
+where
+    W: fmt::Debug,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt.debug_struct("BufWriter")
+            .field("writer", &self.inner)
+            .field("buffer", &format_args!("{}/{}", self.buf.len(), self.buf.capacity()))
+            .finish()
+    }
+}
+
+impl<W: Write + Seek> Seek for BufWriter<W> {
+    /// Seek to the offset, in bytes, in the underlying writer.
+    ///
+    /// Seeking always writes out the internal buffer before seeking.
+    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
+        self.flush_buf()?;
+        self.get_mut().seek(pos)
+    }
+}
+
+impl<W: Write> Drop for BufWriter<W> {
+    fn drop(&mut self) {
+        if !self.panicked {
+            // dtors should not panic, so we ignore a failed flush
+            let _r = self.flush_buf();
+        }
+    }
+}
diff --git a/sgx_tstd/src/io/buffered/linewriter.rs b/sgx_tstd/src/io/buffered/linewriter.rs
new file mode 100644
index 0000000..8fbdad5
--- /dev/null
+++ b/sgx_tstd/src/io/buffered/linewriter.rs
@@ -0,0 +1,241 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::fmt;
+use crate::io::{self, buffered::LineWriterShim, BufWriter, IntoInnerError, IoSlice, Write};
+
+/// Wraps a writer and buffers output to it, flushing whenever a newline
+/// (`0x0a`, `'\n'`) is detected.
+///
+/// The [`BufWriter`] struct wraps a writer and buffers its output.
+/// But it only does this batched write when it goes out of scope, or when the
+/// internal buffer is full. Sometimes, you'd prefer to write each line as it's
+/// completed, rather than the entire buffer at once. Enter `LineWriter`. It
+/// does exactly that.
+///
+/// Like [`BufWriter`], a `LineWriter`’s buffer will also be flushed when the
+/// `LineWriter` goes out of scope or when its internal buffer is full.
+///
+/// If there's still a partial line in the buffer when the `LineWriter` is
+/// dropped, it will flush those contents.
+///
+/// # Examples
+///
+/// We can use `LineWriter` to write one line at a time, significantly
+/// reducing the number of actual writes to the file.
+///
+/// ```no_run
+/// use std::fs::{self, File};
+/// use std::io::prelude::*;
+/// use std::io::LineWriter;
+///
+/// fn main() -> std::io::Result<()> {
+///     let road_not_taken = b"I shall be telling this with a sigh
+/// Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.";
+///
+///     let file = File::create("poem.txt")?;
+///     let mut file = LineWriter::new(file);
+///
+///     file.write_all(b"I shall be telling this with a sigh")?;
+///
+///     // No bytes are written until a newline is encountered (or
+///     // the internal buffer is filled).
+///     assert_eq!(fs::read_to_string("poem.txt")?, "");
+///     file.write_all(b"\n")?;
+///     assert_eq!(
+///         fs::read_to_string("poem.txt")?,
+///         "I shall be telling this with a sigh\n",
+///     );
+///
+///     // Write the rest of the poem.
+///     file.write_all(b"Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.")?;
+///
+///     // The last line of the poem doesn't end in a newline, so
+///     // we have to flush or drop the `LineWriter` to finish
+///     // writing.
+///     file.flush()?;
+///
+///     // Confirm the whole poem was written.
+///     assert_eq!(fs::read("poem.txt")?, &road_not_taken[..]);
+///     Ok(())
+/// }
+/// ```
+pub struct LineWriter<W: Write> {
+    inner: BufWriter<W>,
+}
+
+impl<W: Write> LineWriter<W> {
+    /// Creates a new `LineWriter`.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::LineWriter;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let file = File::create("poem.txt")?;
+    ///     let file = LineWriter::new(file);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn new(inner: W) -> LineWriter<W> {
+        // Lines typically aren't that long, don't use a giant buffer
+        LineWriter::with_capacity(1024, inner)
+    }
+
+    /// Creates a new `LineWriter` with a specified capacity for the internal
+    /// buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::LineWriter;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let file = File::create("poem.txt")?;
+    ///     let file = LineWriter::with_capacity(100, file);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
+        LineWriter { inner: BufWriter::with_capacity(capacity, inner) }
+    }
+
+    /// Gets a reference to the underlying writer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::LineWriter;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let file = File::create("poem.txt")?;
+    ///     let file = LineWriter::new(file);
+    ///
+    ///     let reference = file.get_ref();
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn get_ref(&self) -> &W {
+        self.inner.get_ref()
+    }
+
+    /// Gets a mutable reference to the underlying writer.
+    ///
+    /// Caution must be taken when calling methods on the mutable reference
+    /// returned as extra writes could corrupt the output stream.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::LineWriter;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let file = File::create("poem.txt")?;
+    ///     let mut file = LineWriter::new(file);
+    ///
+    ///     // we can use reference just like file
+    ///     let reference = file.get_mut();
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn get_mut(&mut self) -> &mut W {
+        self.inner.get_mut()
+    }
+
+    /// Unwraps this `LineWriter`, returning the underlying writer.
+    ///
+    /// The internal buffer is written out before returning the writer.
+    ///
+    /// # Errors
+    ///
+    /// An [`Err`] will be returned if an error occurs while flushing the buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io::LineWriter;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let file = File::create("poem.txt")?;
+    ///
+    ///     let writer: LineWriter<File> = LineWriter::new(file);
+    ///
+    ///     let file: File = writer.into_inner()?;
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn into_inner(self) -> Result<W, IntoInnerError<LineWriter<W>>> {
+        self.inner.into_inner().map_err(|err| err.new_wrapped(|inner| LineWriter { inner }))
+    }
+}
+
+impl<W: Write> Write for LineWriter<W> {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        LineWriterShim::new(&mut self.inner).write(buf)
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        self.inner.flush()
+    }
+
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        LineWriterShim::new(&mut self.inner).write_vectored(bufs)
+    }
+
+    fn is_write_vectored(&self) -> bool {
+        self.inner.is_write_vectored()
+    }
+
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        LineWriterShim::new(&mut self.inner).write_all(buf)
+    }
+
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        LineWriterShim::new(&mut self.inner).write_all_vectored(bufs)
+    }
+
+    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+        LineWriterShim::new(&mut self.inner).write_fmt(fmt)
+    }
+}
+
+impl<W: Write> fmt::Debug for LineWriter<W>
+where
+    W: fmt::Debug,
+{
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt.debug_struct("LineWriter")
+            .field("writer", &self.get_ref())
+            .field(
+                "buffer",
+                &format_args!("{}/{}", self.inner.buffer().len(), self.inner.capacity()),
+            )
+            .finish_non_exhaustive()
+    }
+}
diff --git a/sgx_tstd/src/io/buffered/linewritershim.rs b/sgx_tstd/src/io/buffered/linewritershim.rs
new file mode 100644
index 0000000..09bca48
--- /dev/null
+++ b/sgx_tstd/src/io/buffered/linewritershim.rs
@@ -0,0 +1,293 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::io::{self, BufWriter, IoSlice, Write};
+use crate::sys_common::memchr;
+
+/// Private helper struct for implementing the line-buffered writing logic.
+/// This shim temporarily wraps a BufWriter, and uses its internals to
+/// implement a line-buffered writer (specifically by using the internal
+/// methods like write_to_buf and flush_buf). In this way, a more
+/// efficient abstraction can be created than one that only had access to
+/// `write` and `flush`, without needlessly duplicating a lot of the
+/// implementation details of BufWriter. This also allows existing
+/// `BufWriters` to be temporarily given line-buffering logic; this is what
+/// enables Stdout to be alternately in line-buffered or block-buffered mode.
+#[derive(Debug)]
+pub struct LineWriterShim<'a, W: Write> {
+    buffer: &'a mut BufWriter<W>,
+}
+
+impl<'a, W: Write> LineWriterShim<'a, W> {
+    pub fn new(buffer: &'a mut BufWriter<W>) -> Self {
+        Self { buffer }
+    }
+
+    /// Get a reference to the inner writer (that is, the writer
+    /// wrapped by the BufWriter).
+    fn inner(&self) -> &W {
+        self.buffer.get_ref()
+    }
+
+    /// Get a mutable reference to the inner writer (that is, the writer
+    /// wrapped by the BufWriter). Be careful with this writer, as writes to
+    /// it will bypass the buffer.
+    fn inner_mut(&mut self) -> &mut W {
+        self.buffer.get_mut()
+    }
+
+    /// Get the content currently buffered in self.buffer
+    fn buffered(&self) -> &[u8] {
+        self.buffer.buffer()
+    }
+
+    /// Flush the buffer iff the last byte is a newline (indicating that an
+    /// earlier write only succeeded partially, and we want to retry flushing
+    /// the buffered line before continuing with a subsequent write)
+    fn flush_if_completed_line(&mut self) -> io::Result<()> {
+        match self.buffered().last().copied() {
+            Some(b'\n') => self.buffer.flush_buf(),
+            _ => Ok(()),
+        }
+    }
+}
+
+impl<'a, W: Write> Write for LineWriterShim<'a, W> {
+    /// Write some data into this BufReader with line buffering. This means
+    /// that, if any newlines are present in the data, the data up to the last
+    /// newline is sent directly to the underlying writer, and data after it
+    /// is buffered. Returns the number of bytes written.
+    ///
+    /// This function operates on a "best effort basis"; in keeping with the
+    /// convention of `Write::write`, it makes at most one attempt to write
+    /// new data to the underlying writer. If that write only reports a partial
+    /// success, the remaining data will be buffered.
+    ///
+    /// Because this function attempts to send completed lines to the underlying
+    /// writer, it will also flush the existing buffer if it ends with a
+    /// newline, even if the incoming data does not contain any newlines.
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        let newline_idx = match memchr::memrchr(b'\n', buf) {
+            // If there are no new newlines (that is, if this write is less than
+            // one line), just do a regular buffered write (which may flush if
+            // we exceed the inner buffer's size)
+            None => {
+                self.flush_if_completed_line()?;
+                return self.buffer.write(buf);
+            }
+            // Otherwise, arrange for the lines to be written directly to the
+            // inner writer.
+            Some(newline_idx) => newline_idx + 1,
+        };
+
+        // Flush existing content to prepare for our write. We have to do this
+        // before attempting to write `buf` in order to maintain consistency;
+        // if we add `buf` to the buffer then try to flush it all at once,
+        // we're obligated to return Ok(), which would mean suppressing any
+        // errors that occur during flush.
+        self.buffer.flush_buf()?;
+
+        // This is what we're going to try to write directly to the inner
+        // writer. The rest will be buffered, if nothing goes wrong.
+        let lines = &buf[..newline_idx];
+
+        // Write `lines` directly to the inner writer. In keeping with the
+        // `write` convention, make at most one attempt to add new (unbuffered)
+        // data. Because this write doesn't touch the BufWriter state directly,
+        // and the buffer is known to be empty, we don't need to worry about
+        // self.buffer.panicked here.
+        let flushed = self.inner_mut().write(lines)?;
+
+        // If buffer returns Ok(0), propagate that to the caller without
+        // doing additional buffering; otherwise we're just guaranteeing
+        // an "ErrorKind::WriteZero" later.
+        if flushed == 0 {
+            return Ok(0);
+        }
+
+        // Now that the write has succeeded, buffer the rest (or as much of
+        // the rest as possible). If there were any unwritten newlines, we
+        // only buffer out to the last unwritten newline that fits in the
+        // buffer; this helps prevent flushing partial lines on subsequent
+        // calls to LineWriterShim::write.
+
+        // Handle the cases in order of most-common to least-common, under
+        // the presumption that most writes succeed in totality, and that most
+        // writes are smaller than the buffer.
+        // - Is this a partial line (ie, no newlines left in the unwritten tail)
+        // - If not, does the data out to the last unwritten newline fit in
+        //   the buffer?
+        // - If not, scan for the last newline that *does* fit in the buffer
+        let tail = if flushed >= newline_idx {
+            &buf[flushed..]
+        } else if newline_idx - flushed <= self.buffer.capacity() {
+            &buf[flushed..newline_idx]
+        } else {
+            let scan_area = &buf[flushed..];
+            let scan_area = &scan_area[..self.buffer.capacity()];
+            match memchr::memrchr(b'\n', scan_area) {
+                Some(newline_idx) => &scan_area[..newline_idx + 1],
+                None => scan_area,
+            }
+        };
+
+        let buffered = self.buffer.write_to_buf(tail);
+        Ok(flushed + buffered)
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        self.buffer.flush()
+    }
+
+    /// Write some vectored data into this BufReader with line buffering. This
+    /// means that, if any newlines are present in the data, the data up to
+    /// and including the buffer containing the last newline is sent directly
+    /// to the inner writer, and the data after it is buffered. Returns the
+    /// number of bytes written.
+    ///
+    /// This function operates on a "best effort basis"; in keeping with the
+    /// convention of `Write::write`, it makes at most one attempt to write
+    /// new data to the underlying writer.
+    ///
+    /// Because this function attempts to send completed lines to the underlying
+    /// writer, it will also flush the existing buffer if it contains any
+    /// newlines.
+    ///
+    /// Because sorting through an array of `IoSlice` can be a bit convoluted,
+    /// This method differs from write in the following ways:
+    ///
+    /// - It attempts to write the full content of all the buffers up to and
+    ///   including the one containing the last newline. This means that it
+    ///   may attempt to write a partial line, that buffer has data past the
+    ///   newline.
+    /// - If the write only reports partial success, it does not attempt to
+    ///   find the precise location of the written bytes and buffer the rest.
+    ///
+    /// If the underlying vector doesn't support vectored writing, we instead
+    /// simply write the first non-empty buffer with `write`. This way, we
+    /// get the benefits of more granular partial-line handling without losing
+    /// anything in efficiency
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        // If there's no specialized behavior for write_vectored, just use
+        // write. This has the benefit of more granular partial-line handling.
+        if !self.is_write_vectored() {
+            return match bufs.iter().find(|buf| !buf.is_empty()) {
+                Some(buf) => self.write(buf),
+                None => Ok(0),
+            };
+        }
+
+        // Find the buffer containing the last newline
+        let last_newline_buf_idx = bufs
+            .iter()
+            .enumerate()
+            .rev()
+            .find_map(|(i, buf)| memchr::memchr(b'\n', buf).map(|_| i));
+
+        // If there are no new newlines (that is, if this write is less than
+        // one line), just do a regular buffered write
+        let last_newline_buf_idx = match last_newline_buf_idx {
+            // No newlines; just do a normal buffered write
+            None => {
+                self.flush_if_completed_line()?;
+                return self.buffer.write_vectored(bufs);
+            }
+            Some(i) => i,
+        };
+
+        // Flush existing content to prepare for our write
+        self.buffer.flush_buf()?;
+
+        // This is what we're going to try to write directly to the inner
+        // writer. The rest will be buffered, if nothing goes wrong.
+        let (lines, tail) = bufs.split_at(last_newline_buf_idx + 1);
+
+        // Write `lines` directly to the inner writer. In keeping with the
+        // `write` convention, make at most one attempt to add new (unbuffered)
+        // data. Because this write doesn't touch the BufWriter state directly,
+        // and the buffer is known to be empty, we don't need to worry about
+        // self.panicked here.
+        let flushed = self.inner_mut().write_vectored(lines)?;
+
+        // If inner returns Ok(0), propagate that to the caller without
+        // doing additional buffering; otherwise we're just guaranteeing
+        // an "ErrorKind::WriteZero" later.
+        if flushed == 0 {
+            return Ok(0);
+        }
+
+        // Don't try to reconstruct the exact amount written; just bail
+        // in the event of a partial write
+        let lines_len = lines.iter().map(|buf| buf.len()).sum();
+        if flushed < lines_len {
+            return Ok(flushed);
+        }
+
+        // Now that the write has succeeded, buffer the rest (or as much of the
+        // rest as possible)
+        let buffered: usize = tail
+            .iter()
+            .filter(|buf| !buf.is_empty())
+            .map(|buf| self.buffer.write_to_buf(buf))
+            .take_while(|&n| n > 0)
+            .sum();
+
+        Ok(flushed + buffered)
+    }
+
+    fn is_write_vectored(&self) -> bool {
+        self.inner().is_write_vectored()
+    }
+
+    /// Write some data into this BufReader with line buffering. This means
+    /// that, if any newlines are present in the data, the data up to the last
+    /// newline is sent directly to the underlying writer, and data after it
+    /// is buffered.
+    ///
+    /// Because this function attempts to send completed lines to the underlying
+    /// writer, it will also flush the existing buffer if it contains any
+    /// newlines, even if the incoming data does not contain any newlines.
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        match memchr::memrchr(b'\n', buf) {
+            // If there are no new newlines (that is, if this write is less than
+            // one line), just do a regular buffered write (which may flush if
+            // we exceed the inner buffer's size)
+            None => {
+                self.flush_if_completed_line()?;
+                self.buffer.write_all(buf)
+            }
+            Some(newline_idx) => {
+                let (lines, tail) = buf.split_at(newline_idx + 1);
+
+                if self.buffered().is_empty() {
+                    self.inner_mut().write_all(lines)?;
+                } else {
+                    // If there is any buffered data, we add the incoming lines
+                    // to that buffer before flushing, which saves us at least
+                    // one write call. We can't really do this with `write`,
+                    // since we can't do this *and* not suppress errors *and*
+                    // report a consistent state to the caller in a return
+                    // value, but here in write_all it's fine.
+                    self.buffer.write_all(lines)?;
+                    self.buffer.flush_buf()?;
+                }
+
+                self.buffer.write_all(tail)
+            }
+        }
+    }
+}
diff --git a/sgx_tstd/src/io/buffered/mod.rs b/sgx_tstd/src/io/buffered/mod.rs
new file mode 100644
index 0000000..c01cb1c
--- /dev/null
+++ b/sgx_tstd/src/io/buffered/mod.rs
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Buffering wrappers for I/O traits
+
+mod bufreader;
+mod bufwriter;
+mod linewriter;
+mod linewritershim;
+
+use crate::error;
+use crate::fmt;
+use crate::io::Error;
+
+pub use bufreader::BufReader;
+pub use bufwriter::BufWriter;
+pub use bufwriter::WriterPanicked;
+pub use linewriter::LineWriter;
+use linewritershim::LineWriterShim;
+
+/// An error returned by [`BufWriter::into_inner`] which combines an error that
+/// happened while writing out the buffer, and the buffered writer object
+/// which may be used to recover from the condition.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::BufWriter;
+/// use std::net::TcpStream;
+///
+/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+///
+/// // do stuff with the stream
+///
+/// // we want to get our `TcpStream` back, so let's try:
+///
+/// let stream = match stream.into_inner() {
+///     Ok(s) => s,
+///     Err(e) => {
+///         // Here, e is an IntoInnerError
+///         panic!("An error occurred");
+///     }
+/// };
+/// ```
+#[derive(Debug)]
+pub struct IntoInnerError<W>(W, Error);
+
+impl<W> IntoInnerError<W> {
+    /// Construct a new IntoInnerError
+    fn new(writer: W, error: Error) -> Self {
+        Self(writer, error)
+    }
+
+    /// Helper to construct a new IntoInnerError; intended to help with
+    /// adapters that wrap other adapters
+    fn new_wrapped<W2>(self, f: impl FnOnce(W) -> W2) -> IntoInnerError<W2> {
+        let Self(writer, error) = self;
+        IntoInnerError::new(f(writer), error)
+    }
+
+    /// Returns the error which caused the call to [`BufWriter::into_inner()`]
+    /// to fail.
+    ///
+    /// This error was returned when attempting to write the internal buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // do stuff with the stream
+    ///
+    /// // we want to get our `TcpStream` back, so let's try:
+    ///
+    /// let stream = match stream.into_inner() {
+    ///     Ok(s) => s,
+    ///     Err(e) => {
+    ///         // Here, e is an IntoInnerError, let's log the inner error.
+    ///         //
+    ///         // We'll just 'log' to stdout for this example.
+    ///         println!("{}", e.error());
+    ///
+    ///         panic!("An unexpected error occurred.");
+    ///     }
+    /// };
+    /// ```
+    pub fn error(&self) -> &Error {
+        &self.1
+    }
+
+    /// Returns the buffered writer instance which generated the error.
+    ///
+    /// The returned object can be used for error recovery, such as
+    /// re-inspecting the buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::BufWriter;
+    /// use std::net::TcpStream;
+    ///
+    /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
+    ///
+    /// // do stuff with the stream
+    ///
+    /// // we want to get our `TcpStream` back, so let's try:
+    ///
+    /// let stream = match stream.into_inner() {
+    ///     Ok(s) => s,
+    ///     Err(e) => {
+    ///         // Here, e is an IntoInnerError, let's re-examine the buffer:
+    ///         let buffer = e.into_inner();
+    ///
+    ///         // do stuff to try to recover
+    ///
+    ///         // afterwards, let's just return the stream
+    ///         buffer.into_inner().unwrap()
+    ///     }
+    /// };
+    /// ```
+    pub fn into_inner(self) -> W {
+        self.0
+    }
+
+    /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
+    /// [`BufWriter::into_inner()`] to fail.  Unlike `error`, this can be used to
+    /// obtain ownership of the underlying error.
+    ///
+    /// # Example
+    /// ```
+    /// use std::io::{BufWriter, ErrorKind, Write};
+    ///
+    /// let mut not_enough_space = [0u8; 10];
+    /// let mut stream = BufWriter::new(not_enough_space.as_mut());
+    /// write!(stream, "this cannot be actually written").unwrap();
+    /// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
+    /// let err = into_inner_err.into_error();
+    /// assert_eq!(err.kind(), ErrorKind::WriteZero);
+    /// ```
+    pub fn into_error(self) -> Error {
+        self.1
+    }
+
+    /// Consumes the [`IntoInnerError`] and returns the error which caused the call to
+    /// [`BufWriter::into_inner()`] to fail, and the underlying writer.
+    ///
+    /// This can be used to simply obtain ownership of the underlying error; it can also be used for
+    /// advanced error recovery.
+    ///
+    /// # Example
+    /// ```
+    /// use std::io::{BufWriter, ErrorKind, Write};
+    ///
+    /// let mut not_enough_space = [0u8; 10];
+    /// let mut stream = BufWriter::new(not_enough_space.as_mut());
+    /// write!(stream, "this cannot be actually written").unwrap();
+    /// let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
+    /// let (err, recovered_writer) = into_inner_err.into_parts();
+    /// assert_eq!(err.kind(), ErrorKind::WriteZero);
+    /// assert_eq!(recovered_writer.buffer(), b"t be actually written");
+    /// ```
+    pub fn into_parts(self) -> (Error, W) {
+        (self.1, self.0)
+    }
+}
+
+impl<W> From<IntoInnerError<W>> for Error {
+    fn from(iie: IntoInnerError<W>) -> Error {
+        iie.1
+    }
+}
+
+impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
+    #[allow(deprecated, deprecated_in_future)]
+    fn description(&self) -> &str {
+        error::Error::description(self.error())
+    }
+}
+
+impl<W> fmt::Display for IntoInnerError<W> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        self.error().fmt(f)
+    }
+}
diff --git a/sgx_tstd/src/io/copy.rs b/sgx_tstd/src/io/copy.rs
new file mode 100644
index 0000000..f3f0a93
--- /dev/null
+++ b/sgx_tstd/src/io/copy.rs
@@ -0,0 +1,156 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use super::{BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
+use crate::mem::MaybeUninit;
+
+/// Copies the entire contents of a reader into a writer.
+///
+/// This function will continuously read data from `reader` and then
+/// write it into `writer` in a streaming fashion until `reader`
+/// returns EOF.
+///
+/// On success, the total number of bytes that were copied from
+/// `reader` to `writer` is returned.
+///
+/// If you’re wanting to copy the contents of one file to another and you’re
+/// working with filesystem paths, see the [`fs::copy`] function.
+///
+/// [`fs::copy`]: crate::fs::copy
+///
+/// # Errors
+///
+/// This function will return an error immediately if any call to [`read`] or
+/// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
+/// handled by this function and the underlying operation is retried.
+///
+/// [`read`]: Read::read
+/// [`write`]: Write::write
+///
+/// # Examples
+///
+/// ```
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+///     let mut reader: &[u8] = b"hello";
+///     let mut writer: Vec<u8> = vec![];
+///
+///     io::copy(&mut reader, &mut writer)?;
+///
+///     assert_eq!(&b"hello"[..], &writer[..]);
+///     Ok(())
+/// }
+/// ```
+pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
+where
+    R: Read,
+    W: Write,
+{
+    BufferedCopySpec::copy_to(reader, writer)
+}
+
+/// Specialization of the read-write loop that either uses a stack buffer
+/// or reuses the internal buffer of a BufWriter
+trait BufferedCopySpec: Write {
+    fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
+}
+
+impl<W: Write + ?Sized> BufferedCopySpec for W {
+    default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
+        stack_buffer_copy(reader, writer)
+    }
+}
+
+impl<I: Write> BufferedCopySpec for BufWriter<I> {
+    fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
+        if writer.capacity() < DEFAULT_BUF_SIZE {
+            return stack_buffer_copy(reader, writer);
+        }
+
+        // FIXME: #42788
+        //
+        //   - This creates a (mut) reference to a slice of
+        //     _uninitialized_ integers, which is **undefined behavior**
+        //
+        //   - Only the standard library gets to soundly "ignore" this,
+        //     based on its privileged knowledge of unstable rustc
+        //     internals;
+        unsafe {
+            let spare_cap = writer.buffer_mut().spare_capacity_mut();
+            reader.initializer().initialize(MaybeUninit::slice_assume_init_mut(spare_cap));
+        }
+
+        let mut len = 0;
+
+        loop {
+            let buf = writer.buffer_mut();
+            let spare_cap = buf.spare_capacity_mut();
+
+            if spare_cap.len() >= DEFAULT_BUF_SIZE {
+                match reader.read(unsafe { MaybeUninit::slice_assume_init_mut(spare_cap) }) {
+                    Ok(0) => return Ok(len), // EOF reached
+                    Ok(bytes_read) => {
+                        assert!(bytes_read <= spare_cap.len());
+                        // SAFETY: The initializer contract guarantees that either it or `read`
+                        // will have initialized these bytes. And we just checked that the number
+                        // of bytes is within the buffer capacity.
+                        unsafe { buf.set_len(buf.len() + bytes_read) };
+                        len += bytes_read as u64;
+                        // Read again if the buffer still has enough capacity, as BufWriter itself would do
+                        // This will occur if the reader returns short reads
+                        continue;
+                    }
+                    Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+                    Err(e) => return Err(e),
+                }
+            }
+
+            writer.flush_buf()?;
+        }
+    }
+}
+
+fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
+    reader: &mut R,
+    writer: &mut W,
+) -> Result<u64> {
+    let mut buf = MaybeUninit::<[u8; DEFAULT_BUF_SIZE]>::uninit();
+    // FIXME: #42788
+    //
+    //   - This creates a (mut) reference to a slice of
+    //     _uninitialized_ integers, which is **undefined behavior**
+    //
+    //   - Only the standard library gets to soundly "ignore" this,
+    //     based on its privileged knowledge of unstable rustc
+    //     internals;
+    unsafe {
+        reader.initializer().initialize(buf.assume_init_mut());
+    }
+
+    let mut written = 0;
+    loop {
+        let len = match reader.read(unsafe { buf.assume_init_mut() }) {
+            Ok(0) => return Ok(written),
+            Ok(len) => len,
+            Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
+            Err(e) => return Err(e),
+        };
+        writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
+        written += len as u64;
+    }
+}
diff --git a/sgx_tstd/src/io/cursor.rs b/sgx_tstd/src/io/cursor.rs
index 68a1c43..ff470a5 100644
--- a/sgx_tstd/src/io/cursor.rs
+++ b/sgx_tstd/src/io/cursor.rs
@@ -16,15 +16,17 @@
 // under the License..
 
 use crate::io::prelude::*;
+
+use crate::cmp;
 use crate::io::{self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom};
-use core::cmp;
+
 use core::convert::TryInto;
 
 /// A `Cursor` wraps an in-memory buffer and provides it with a
 /// [`Seek`] implementation.
 ///
 /// `Cursor`s are used with in-memory buffers, anything implementing
-/// `AsRef<[u8]>`, to allow them to implement [`Read`] and/or [`Write`],
+/// [`AsRef`]`<[u8]>`, to allow them to implement [`Read`] and/or [`Write`],
 /// allowing these buffers to be used anywhere you might use a reader or writer
 /// that does actual I/O.
 ///
@@ -32,8 +34,57 @@
 /// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
 /// `Cursor<`[`&[u8]`][bytes]`>`.
 ///
-
-#[derive(Clone, Debug, Default, Eq, PartialEq)]
+/// # Examples
+///
+/// We may want to write bytes to a [`File`] in our production
+/// code, but use an in-memory buffer in our tests. We can do this with
+/// `Cursor`:
+///
+/// [bytes]: crate::slice
+/// [`File`]: crate::fs::File
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::io::{self, SeekFrom};
+/// use std::fs::File;
+///
+/// // a library function we've written
+/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
+///     writer.seek(SeekFrom::End(-10))?;
+///
+///     for i in 0..10 {
+///         writer.write(&[i])?;
+///     }
+///
+///     // all went well
+///     Ok(())
+/// }
+///
+/// # fn foo() -> io::Result<()> {
+/// // Here's some code that uses this library function.
+/// //
+/// // We might want to use a BufReader here for efficiency, but let's
+/// // keep this example focused.
+/// let mut file = File::create("foo.txt")?;
+///
+/// write_ten_bytes_at_end(&mut file)?;
+/// # Ok(())
+/// # }
+///
+/// // now let's write a test
+/// #[test]
+/// fn test_writes_bytes() {
+///     // setting up a real File is much slower than an in-memory buffer,
+///     // let's use a cursor instead
+///     use std::io::Cursor;
+///     let mut buff = Cursor::new(vec![0; 15]);
+///
+///     write_ten_bytes_at_end(&mut buff).unwrap();
+///
+///     assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
+/// }
+/// ```
+#[derive(Debug, Default, Eq, PartialEq)]
 pub struct Cursor<T> {
     inner: T,
     pos: u64,
@@ -42,21 +93,54 @@
 impl<T> Cursor<T> {
     /// Creates a new cursor wrapping the provided underlying in-memory buffer.
     ///
-    /// Cursor initial position is `0` even if underlying buffer (e.g., `Vec`)
-    /// is not empty. So writing to cursor starts with overwriting `Vec`
+    /// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
+    /// is not empty. So writing to cursor starts with overwriting [`Vec`]
     /// content, not with appending to it.
     ///
-    pub fn new(inner: T) -> Cursor<T> {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    ///
+    /// let buff = Cursor::new(Vec::new());
+    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+    /// # force_inference(&buff);
+    /// ```
+    pub const fn new(inner: T) -> Cursor<T> {
         Cursor { pos: 0, inner }
     }
 
     /// Consumes this cursor, returning the underlying value.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    ///
+    /// let buff = Cursor::new(Vec::new());
+    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+    /// # force_inference(&buff);
+    ///
+    /// let vec = buff.into_inner();
+    /// ```
     pub fn into_inner(self) -> T {
         self.inner
     }
 
     /// Gets a reference to the underlying value in this cursor.
-    pub fn get_ref(&self) -> &T {
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    ///
+    /// let buff = Cursor::new(Vec::new());
+    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+    /// # force_inference(&buff);
+    ///
+    /// let reference = buff.get_ref();
+    /// ```
+    pub const fn get_ref(&self) -> &T {
         &self.inner
     }
 
@@ -64,21 +148,137 @@
     ///
     /// Care should be taken to avoid modifying the internal I/O state of the
     /// underlying value as it may corrupt this cursor's position.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    ///
+    /// let mut buff = Cursor::new(Vec::new());
+    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
+    /// # force_inference(&buff);
+    ///
+    /// let reference = buff.get_mut();
+    /// ```
     pub fn get_mut(&mut self) -> &mut T {
         &mut self.inner
     }
 
     /// Returns the current position of this cursor.
-    pub fn position(&self) -> u64 {
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    /// use std::io::prelude::*;
+    /// use std::io::SeekFrom;
+    ///
+    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+    ///
+    /// assert_eq!(buff.position(), 0);
+    ///
+    /// buff.seek(SeekFrom::Current(2)).unwrap();
+    /// assert_eq!(buff.position(), 2);
+    ///
+    /// buff.seek(SeekFrom::Current(-1)).unwrap();
+    /// assert_eq!(buff.position(), 1);
+    /// ```
+    pub const fn position(&self) -> u64 {
         self.pos
     }
 
     /// Sets the position of this cursor.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Cursor;
+    ///
+    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+    ///
+    /// assert_eq!(buff.position(), 0);
+    ///
+    /// buff.set_position(2);
+    /// assert_eq!(buff.position(), 2);
+    ///
+    /// buff.set_position(4);
+    /// assert_eq!(buff.position(), 4);
+    /// ```
     pub fn set_position(&mut self, pos: u64) {
         self.pos = pos;
     }
 }
 
+impl<T> Cursor<T>
+where
+    T: AsRef<[u8]>,
+{
+    /// Returns the remaining slice.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(cursor_remaining)]
+    /// use std::io::Cursor;
+    ///
+    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+    ///
+    /// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
+    ///
+    /// buff.set_position(2);
+    /// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
+    ///
+    /// buff.set_position(4);
+    /// assert_eq!(buff.remaining_slice(), &[5]);
+    ///
+    /// buff.set_position(6);
+    /// assert_eq!(buff.remaining_slice(), &[]);
+    /// ```
+    pub fn remaining_slice(&self) -> &[u8] {
+        let len = self.pos.min(self.inner.as_ref().len() as u64);
+        &self.inner.as_ref()[(len as usize)..]
+    }
+
+    /// Returns `true` if the remaining slice is empty.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(cursor_remaining)]
+    /// use std::io::Cursor;
+    ///
+    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
+    ///
+    /// buff.set_position(2);
+    /// assert!(!buff.is_empty());
+    ///
+    /// buff.set_position(5);
+    /// assert!(buff.is_empty());
+    ///
+    /// buff.set_position(10);
+    /// assert!(buff.is_empty());
+    /// ```
+    pub fn is_empty(&self) -> bool {
+        self.pos >= self.inner.as_ref().len() as u64
+    }
+}
+
+impl<T> Clone for Cursor<T>
+where
+    T: Clone,
+{
+    #[inline]
+    fn clone(&self) -> Self {
+        Cursor { inner: self.inner.clone(), pos: self.pos }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, other: &Self) {
+        self.inner.clone_from(&other.inner);
+        self.pos = other.pos;
+    }
+}
+
 impl<T> io::Seek for Cursor<T>
 where
     T: AsRef<[u8]>,
@@ -102,9 +302,9 @@
                 self.pos = n;
                 Ok(self.pos)
             }
-            None => Err(Error::new(
+            None => Err(Error::new_const(
                 ErrorKind::InvalidInput,
-                "invalid seek to a negative or overflowing position",
+                &"invalid seek to a negative or overflowing position",
             )),
         }
     }
@@ -123,7 +323,7 @@
     T: AsRef<[u8]>,
 {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        let n = Read::read(&mut self.fill_buf()?, buf)?;
+        let n = Read::read(&mut self.remaining_slice(), buf)?;
         self.pos += n as u64;
         Ok(n)
     }
@@ -140,9 +340,13 @@
         Ok(nread)
     }
 
+    fn is_read_vectored(&self) -> bool {
+        true
+    }
+
     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
         let n = buf.len();
-        Read::read_exact(&mut self.fill_buf()?, buf)?;
+        Read::read_exact(&mut self.remaining_slice(), buf)?;
         self.pos += n as u64;
         Ok(())
     }
@@ -158,8 +362,7 @@
     T: AsRef<[u8]>,
 {
     fn fill_buf(&mut self) -> io::Result<&[u8]> {
-        let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
-        Ok(&self.inner.as_ref()[(amt as usize)..])
+        Ok(self.remaining_slice())
     }
     fn consume(&mut self, amt: usize) {
         self.pos += amt as u64;
@@ -195,9 +398,9 @@
 // Resizing write implementation
 fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
     let pos: usize = (*pos_mut).try_into().map_err(|_| {
-        Error::new(
+        Error::new_const(
             ErrorKind::InvalidInput,
-            "cursor position exceeds maximum possible vector length",
+            &"cursor position exceeds maximum possible vector length",
         )
     })?;
     // Make sure the internal buffer is as least as big as where we
@@ -245,6 +448,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -260,6 +468,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -275,6 +488,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -292,6 +510,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
diff --git a/sgx_tstd/src/io/error.rs b/sgx_tstd/src/io/error.rs
index 1f1ccaf..7335a9d 100644
--- a/sgx_tstd/src/io/error.rs
+++ b/sgx_tstd/src/io/error.rs
@@ -15,18 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::sgx_status_t;
+use crate::convert::From;
 use crate::error;
+use crate::fmt;
+use crate::result;
 use crate::sys;
-use core::fmt;
-use core::result;
-use core::convert::From;
-use alloc_crate::str;
-use alloc_crate::boxed::Box;
 
+use sgx_types::sgx_status_t;
 
-/// A specialized [`Result`](../result/enum.Result.html) type for I/O
-/// operations.
+/// A specialized [`Result`] type for I/O operations.
 ///
 /// This type is broadly used across [`std::io`] for any operation which may
 /// produce an error.
@@ -37,9 +34,29 @@
 /// While usual Rust style is to import types directly, aliases of [`Result`]
 /// often are not, to make it easier to distinguish between them. [`Result`] is
 /// generally assumed to be [`std::result::Result`][`Result`], and so users of this alias
-/// will generally use `io::Result` instead of shadowing the prelude's import
+/// will generally use `io::Result` instead of shadowing the [prelude]'s import
 /// of [`std::result::Result`][`Result`].
 ///
+/// [`std::io`]: crate::io
+/// [`io::Error`]: Error
+/// [`Result`]: crate::result::Result
+/// [prelude]: crate::prelude
+///
+/// # Examples
+///
+/// A convenience function that bubbles an `io::Result` to its caller:
+///
+/// ```
+/// use std::io;
+///
+/// fn get_string() -> io::Result<String> {
+///     let mut buffer = String::new();
+///
+///     io::stdin().read_line(&mut buffer)?;
+///
+///     Ok(buffer)
+/// }
+/// ```
 pub type Result<T> = result::Result<T, Error>;
 
 /// The error type for I/O operations of the [`Read`], [`Write`], [`Seek`], and
@@ -49,7 +66,9 @@
 /// `Error` can be created with crafted error messages and a particular value of
 /// [`ErrorKind`].
 ///
-/// [`ErrorKind`]: enum.ErrorKind.html
+/// [`Read`]: crate::io::Read
+/// [`Write`]: crate::io::Write
+/// [`Seek`]: crate::io::Seek
 pub struct Error {
     repr: Repr,
 }
@@ -63,6 +82,8 @@
 enum Repr {
     Os(i32),
     Simple(ErrorKind),
+    // &str is a fat pointer, but &&str is a thin pointer.
+    SimpleMessage(ErrorKind, &'static &'static str),
     Custom(Box<Custom>),
     SgxStatus(sgx_status_t),
 }
@@ -80,7 +101,7 @@
 ///
 /// It is used with the [`io::Error`] type.
 ///
-/// [`io::Error`]: struct.Error.html
+/// [`io::Error`]: Error
 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
 #[allow(deprecated)]
 #[non_exhaustive]
@@ -93,6 +114,10 @@
     ConnectionRefused,
     /// The connection was reset by the remote server.
     ConnectionReset,
+    /// The remote host is not reachable.
+    HostUnreachable,
+    /// The network containing the remote host is not reachable.
+    NetworkUnreachable,
     /// The connection was aborted (terminated) by the remote server.
     ConnectionAborted,
     /// The network operation failed because it was not connected yet.
@@ -103,6 +128,8 @@
     /// A nonexistent interface was requested or the requested address was not
     /// local.
     AddrNotAvailable,
+    /// The system's networking is down.
+    NetworkDown,
     /// The operation failed because a pipe was closed.
     BrokenPipe,
     /// An entity already exists, often a file.
@@ -110,6 +137,32 @@
     /// The operation needs to block to complete, but the blocking operation was
     /// requested to not occur.
     WouldBlock,
+    /// A filesystem object is, unexpectedly, not a directory.
+    ///
+    /// For example, a filesystem path was specified where one of the intermediate directory
+    /// components was, in fact, a plain file.
+    NotADirectory,
+    /// The filesystem object is, unexpectedly, a directory.
+    ///
+    /// A directory was specified when a non-directory was expected.
+    IsADirectory,
+    /// A non-empty directory was specified where an empty directory was expected.
+    DirectoryNotEmpty,
+    /// The filesystem or storage medium is read-only, but a write operation was attempted.
+    ReadOnlyFilesystem,
+    /// Loop in the filesystem or IO subsystem; often, too many levels of symbolic links.
+    ///
+    /// There was a loop (or excessively long chain) resolving a filesystem object
+    /// or file IO object.
+    ///
+    /// On Unix this is usually the result of a symbolic link loop; or, of exceeding the
+    /// system-specific limit on the depth of symlink traversal.
+    FilesystemLoop,
+    /// Stale network file handle.
+    ///
+    /// With some network filesystems, notably NFS, an open file (or directory) can be invalidated
+    /// by problems with the network or server.
+    StaleNetworkFileHandle,
     /// A parameter was incorrect.
     InvalidInput,
     /// Data not valid for the operation were encountered.
@@ -121,7 +174,7 @@
     /// For example, a function that reads a file into a string will error with
     /// `InvalidData` if the file's contents are not valid UTF-8.
     ///
-    /// [`InvalidInput`]: #variant.InvalidInput
+    /// [`InvalidInput`]: ErrorKind::InvalidInput
     InvalidData,
     /// The I/O operation's timeout expired, causing it to be canceled.
     TimedOut,
@@ -132,16 +185,67 @@
     /// particular number of bytes but only a smaller number of bytes could be
     /// written.
     ///
-    /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
-    /// [`Ok(0)`]: ../../std/io/type.Result.html
+    /// [`write`]: crate::io::Write::write
+    /// [`Ok(0)`]: Ok
     WriteZero,
+    /// The underlying storage (typically, a filesystem) is full.
+    ///
+    /// This does not include out of quota errors.
+    StorageFull,
+    /// Seek on unseekable file.
+    ///
+    /// Seeking was attempted on an open file handle which is not suitable for seeking - for
+    /// example, on Unix, a named pipe opened with `File::open`.
+    NotSeekable,
+    /// Filesystem quota was exceeded.
+    FilesystemQuotaExceeded,
+    /// File larger than allowed or supported.
+    ///
+    /// This might arise from a hard limit of the underlying filesystem or file access API, or from
+    /// an administratively imposed resource limitation.  Simple disk full, and out of quota, have
+    /// their own errors.
+    FileTooLarge,
+    /// Resource is busy.
+    ResourceBusy,
+    /// Executable file is busy.
+    ///
+    /// An attempt was made to write to a file which is also in use as a running program.  (Not all
+    /// operating systems detect this situation.)
+    ExecutableFileBusy,
+    /// Deadlock (avoided).
+    ///
+    /// A file locking operation would result in deadlock.  This situation is typically detected, if
+    /// at all, on a best-effort basis.
+    Deadlock,
+    /// Cross-device or cross-filesystem (hard) link or rename.
+    CrossesDevices,
+    /// Too many (hard) links to the same filesystem object.
+    ///
+    /// The filesystem does not support making so many hardlinks to the same file.
+    TooManyLinks,
+    /// Filename too long.
+    ///
+    /// The limit might be from the underlying filesystem or API, or an administratively imposed
+    /// resource limit.
+    FilenameTooLong,
+    /// Program argument list too long.
+    ///
+    /// When trying to run an external program, a system or process limit on the size of the
+    /// arguments would have been exceeded.
+    ArgumentListTooLong,
     /// This operation was interrupted.
     ///
     /// Interrupted operations can typically be retried.
     Interrupted,
-    /// Any I/O error not part of this list.
-    Other,
 
+    /// This operation is unsupported on this platform.
+    ///
+    /// This means that the operation can never succeed.
+    Unsupported,
+
+    // ErrorKinds which are primarily categorisations for OS error
+    // codes should be added above.
+    //
     /// An error returned when an operation could not be completed because an
     /// "end of file" was reached prematurely.
     ///
@@ -150,32 +254,84 @@
     /// read.
     UnexpectedEof,
 
+    /// An operation could not be completed, because it failed
+    /// to allocate enough memory.
+    OutOfMemory,
+
+    // "Unusual" error kinds which do not correspond simply to (sets
+    // of) OS error codes, should be added just above this comment.
+    // `Other` and `Uncategorised` should remain at the end:
+    //
+    /// A custom error that does not fall under any other I/O error kind.
     ///
+    /// This can be used to construct your own [`Error`]s that do not match any
+    /// [`ErrorKind`].
+    ///
+    /// This [`ErrorKind`] is not used by the standard library.
+    ///
+    /// Errors from the standard library that do not fall under any of the I/O
+    /// error kinds cannot be `match`ed on, and will only match a wildcard (`_`) pattern.
+    /// New [`ErrorKind`]s might be added in the future for some of those.
+    Other,
+
+    /// Any I/O error from the standard library that's not part of this list.
+    ///
+    /// Errors that are `Uncategorized` now may move to a different or a new
+    /// [`ErrorKind`] variant in the future. It is not recommended to match
+    /// an error against `Uncategorized`; use a wildcard match (`_`) instead.
+    #[doc(hidden)]
+    Uncategorized,
+
     /// SGX error status
     SgxError,
 }
 
 impl ErrorKind {
     pub(crate) fn as_str(&self) -> &'static str {
+        use ErrorKind::*;
+        // Strictly alphabetical, please.  (Sadly rustfmt cannot do this yet.)
         match *self {
-            ErrorKind::NotFound => "entity not found",
-            ErrorKind::PermissionDenied => "permission denied",
-            ErrorKind::ConnectionRefused => "connection refused",
-            ErrorKind::ConnectionReset => "connection reset",
-            ErrorKind::ConnectionAborted => "connection aborted",
-            ErrorKind::NotConnected => "not connected",
-            ErrorKind::AddrInUse => "address in use",
-            ErrorKind::AddrNotAvailable => "address not available",
-            ErrorKind::BrokenPipe => "broken pipe",
-            ErrorKind::AlreadyExists => "entity already exists",
-            ErrorKind::WouldBlock => "operation would block",
-            ErrorKind::InvalidInput => "invalid input parameter",
-            ErrorKind::InvalidData => "invalid data",
-            ErrorKind::TimedOut => "timed out",
-            ErrorKind::WriteZero => "write zero",
-            ErrorKind::Interrupted => "operation interrupted",
-            ErrorKind::Other => "other os error",
-            ErrorKind::UnexpectedEof => "unexpected end of file",
+            AddrInUse => "address in use",
+            AddrNotAvailable => "address not available",
+            AlreadyExists => "entity already exists",
+            ArgumentListTooLong => "argument list too long",
+            BrokenPipe => "broken pipe",
+            ConnectionAborted => "connection aborted",
+            ConnectionRefused => "connection refused",
+            ConnectionReset => "connection reset",
+            CrossesDevices => "cross-device link or rename",
+            Deadlock => "deadlock",
+            DirectoryNotEmpty => "directory not empty",
+            ExecutableFileBusy => "executable file busy",
+            FileTooLarge => "file too large",
+            FilenameTooLong => "filename too long",
+            FilesystemLoop => "filesystem loop or indirection limit (e.g. symlink loop)",
+            FilesystemQuotaExceeded => "filesystem quota exceeded",
+            HostUnreachable => "host unreachable",
+            Interrupted => "operation interrupted",
+            InvalidData => "invalid data",
+            InvalidInput => "invalid input parameter",
+            IsADirectory => "is a directory",
+            NetworkDown => "network down",
+            NetworkUnreachable => "network unreachable",
+            NotADirectory => "not a directory",
+            NotConnected => "not connected",
+            NotFound => "entity not found",
+            NotSeekable => "seek on unseekable file",
+            Other => "other error",
+            OutOfMemory => "out of memory",
+            PermissionDenied => "permission denied",
+            ReadOnlyFilesystem => "read-only filesystem or storage medium",
+            ResourceBusy => "resource busy",
+            StaleNetworkFileHandle => "stale network file handle",
+            StorageFull => "no storage space",
+            TimedOut => "timed out",
+            TooManyLinks => "too many links",
+            Uncategorized => "uncategorized error",
+            UnexpectedEof => "unexpected end of file",
+            Unsupported => "unsupported",
+            WouldBlock => "operation would block",
+            WriteZero => "write zero",
             ErrorKind::SgxError => "Sgx error status",
         }
     }
@@ -186,6 +342,17 @@
 impl From<ErrorKind> for Error {
     /// Converts an [`ErrorKind`] into an [`Error`].
     ///
+    /// This conversion allocates a new error with a simple representation of error kind.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// let not_found = ErrorKind::NotFound;
+    /// let error = Error::from(not_found);
+    /// assert_eq!("entity not found", format!("{}", error));
+    /// ```
     #[inline]
     fn from(kind: ErrorKind) -> Error {
         Error { repr: Repr::Simple(kind) }
@@ -205,8 +372,19 @@
     ///
     /// This function is used to generically create I/O errors which do not
     /// originate from the OS itself. The `error` argument is an arbitrary
-    /// payload which will be contained in this `Error`.
+    /// payload which will be contained in this [`Error`].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// // errors can be created from strings
+    /// let custom_error = Error::new(ErrorKind::Other, "oh no!");
+    ///
+    /// // errors can also be created from other errors
+    /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error);
+    /// ```
     pub fn new<E>(kind: ErrorKind, error: E) -> Error
     where
         E: Into<Box<dyn error::Error + Send + Sync>>,
@@ -218,39 +396,108 @@
         Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
     }
 
+    /// Creates a new I/O error from a known kind of error as well as a
+    /// constant message.
+    ///
+    /// This function does not allocate.
+    ///
+    /// This function should maybe change to
+    /// `new_const<const MSG: &'static str>(kind: ErrorKind)`
+    /// in the future, when const generics allow that.
+    #[inline]
+    pub(crate) const fn new_const(kind: ErrorKind, message: &'static &'static str) -> Error {
+        Self { repr: Repr::SimpleMessage(kind, message) }
+    }
+
     /// Returns an error representing the last OS error which occurred.
     ///
     /// This function reads the value of `errno` for the target platform (e.g.
     /// `GetLastError` on Windows) and will return a corresponding instance of
-    /// `Error` for the error code.
+    /// [`Error`] for the error code.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::Error;
+    ///
+    /// println!("last OS error: {:?}", Error::last_os_error());
+    /// ```
+    #[inline]
     pub fn last_os_error() -> Error {
         Error::from_raw_os_error(sys::os::errno() as i32)
     }
 
-    /// Creates a new instance of an `Error` from a particular OS error code.
+    /// Creates a new instance of an [`Error`] from a particular OS error code.
     ///
+    /// # Examples
+    ///
+    /// On Linux:
+    ///
+    /// ```
+    /// # if cfg!(target_os = "linux") {
+    /// use std::io;
+    ///
+    /// let error = io::Error::from_raw_os_error(22);
+    /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
+    /// # }
+    /// ```
+    ///
+    /// On Windows:
+    ///
+    /// ```
+    /// # if cfg!(windows) {
+    /// use std::io;
+    ///
+    /// let error = io::Error::from_raw_os_error(10022);
+    /// assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
+    /// # }
+    /// ```
+    #[inline]
     pub fn from_raw_os_error(code: i32) -> Error {
         Error { repr: Repr::Os(code) }
     }
 
     /// Returns the OS error that this error represents (if any).
     ///
-    /// If this `Error` was constructed via `last_os_error` or
-    /// `from_raw_os_error`, then this function will return `Some`, otherwise
-    /// it will return `None`.
+    /// If this [`Error`] was constructed via [`last_os_error`] or
+    /// [`from_raw_os_error`], then this function will return [`Some`], otherwise
+    /// it will return [`None`].
     ///
+    /// [`last_os_error`]: Error::last_os_error
+    /// [`from_raw_os_error`]: Error::from_raw_os_error
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// fn print_os_error(err: &Error) {
+    ///     if let Some(raw_os_err) = err.raw_os_error() {
+    ///         println!("raw OS error: {:?}", raw_os_err);
+    ///     } else {
+    ///         println!("Not an OS error");
+    ///     }
+    /// }
+    ///
+    /// fn main() {
+    ///     // Will print "raw OS error: ...".
+    ///     print_os_error(&Error::last_os_error());
+    ///     // Will print "Not an OS error".
+    ///     print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
+    /// }
+    /// ```
+    #[inline]
     pub fn raw_os_error(&self) -> Option<i32> {
         match self.repr {
             Repr::Os(i) => Some(i),
             Repr::Custom(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::SgxStatus(..) => None,
         }
     }
 
     /// Creates a new instance of an `Error` from a particular SGX error status.
-    ///
     pub fn from_sgx_error(status: sgx_status_t) -> Error {
         Error { repr: Repr::SgxStatus(status) }
     }
@@ -260,25 +507,48 @@
     /// If this `Error` was constructed via `from_sgx_error` or
     /// then this function will return `Some`, otherwise
     /// it will return `None`.
-    ///
     pub fn raw_sgx_error(&self) -> Option<sgx_status_t> {
         match self.repr {
             Repr::Os(..) => None,
             Repr::Custom(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::SgxStatus(status) => Some(status),
         }
     }
 
     /// Returns a reference to the inner error wrapped by this error (if any).
     ///
-    /// If this `Error` was constructed via `new` then this function will
-    /// return `Some`, otherwise it will return `None`.
+    /// If this [`Error`] was constructed via [`new`] then this function will
+    /// return [`Some`], otherwise it will return [`None`].
     ///
-    pub fn get_ref(&self) -> Option<&(dyn error::Error+Send+Sync+'static)> {
+    /// [`new`]: Error::new
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// fn print_error(err: &Error) {
+    ///     if let Some(inner_err) = err.get_ref() {
+    ///         println!("Inner error: {:?}", inner_err);
+    ///     } else {
+    ///         println!("No inner error");
+    ///     }
+    /// }
+    ///
+    /// fn main() {
+    ///     // Will print "No inner error".
+    ///     print_error(&Error::last_os_error());
+    ///     // Will print "Inner error: ...".
+    ///     print_error(&Error::new(ErrorKind::Other, "oh no!"));
+    /// }
+    /// ```
+    pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
         match self.repr {
             Repr::Os(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::Custom(ref c) => Some(&*c.error),
             Repr::SgxStatus(ref s) => Some(s),
         }
@@ -287,13 +557,70 @@
     /// Returns a mutable reference to the inner error wrapped by this error
     /// (if any).
     ///
-    /// If this `Error` was constructed via `new` then this function will
-    /// return `Some`, otherwise it will return `None`.
+    /// If this [`Error`] was constructed via [`new`] then this function will
+    /// return [`Some`], otherwise it will return [`None`].
     ///
+    /// [`new`]: Error::new
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    /// use std::{error, fmt};
+    /// use std::fmt::Display;
+    ///
+    /// #[derive(Debug)]
+    /// struct MyError {
+    ///     v: String,
+    /// }
+    ///
+    /// impl MyError {
+    ///     fn new() -> MyError {
+    ///         MyError {
+    ///             v: "oh no!".to_string()
+    ///         }
+    ///     }
+    ///
+    ///     fn change_message(&mut self, new_message: &str) {
+    ///         self.v = new_message.to_string();
+    ///     }
+    /// }
+    ///
+    /// impl error::Error for MyError {}
+    ///
+    /// impl Display for MyError {
+    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+    ///         write!(f, "MyError: {}", &self.v)
+    ///     }
+    /// }
+    ///
+    /// fn change_error(mut err: Error) -> Error {
+    ///     if let Some(inner_err) = err.get_mut() {
+    ///         inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
+    ///     }
+    ///     err
+    /// }
+    ///
+    /// fn print_error(err: &Error) {
+    ///     if let Some(inner_err) = err.get_ref() {
+    ///         println!("Inner error: {}", inner_err);
+    ///     } else {
+    ///         println!("No inner error");
+    ///     }
+    /// }
+    ///
+    /// fn main() {
+    ///     // Will print "No inner error".
+    ///     print_error(&change_error(Error::last_os_error()));
+    ///     // Will print "Inner error: ...".
+    ///     print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
+    /// }
+    /// ```
     pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
         match self.repr {
             Repr::Os(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::Custom(ref mut c) => Some(&mut *c.error),
             Repr::SgxStatus(ref mut s) => Some(s),
         }
@@ -301,25 +628,65 @@
 
     /// Consumes the `Error`, returning its inner error (if any).
     ///
-    /// If this `Error` was constructed via `new` then this function will
-    /// return `Some`, otherwise it will return `None`.
+    /// If this [`Error`] was constructed via [`new`] then this function will
+    /// return [`Some`], otherwise it will return [`None`].
     ///
+    /// [`new`]: Error::new
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// fn print_error(err: Error) {
+    ///     if let Some(inner_err) = err.into_inner() {
+    ///         println!("Inner error: {}", inner_err);
+    ///     } else {
+    ///         println!("No inner error");
+    ///     }
+    /// }
+    ///
+    /// fn main() {
+    ///     // Will print "No inner error".
+    ///     print_error(Error::last_os_error());
+    ///     // Will print "Inner error: ...".
+    ///     print_error(Error::new(ErrorKind::Other, "oh no!"));
+    /// }
+    /// ```
     pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
         match self.repr {
             Repr::Os(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::Custom(c) => Some(c.error),
             Repr::SgxStatus(s) => Some(Box::new(s)),
         }
     }
 
-    /// Returns the corresponding `ErrorKind` for this error.
+    /// Returns the corresponding [`ErrorKind`] for this error.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{Error, ErrorKind};
+    ///
+    /// fn print_error(err: Error) {
+    ///     println!("{:?}", err.kind());
+    /// }
+    ///
+    /// fn main() {
+    ///     // Will print "Uncategorized".
+    ///     print_error(Error::last_os_error());
+    ///     // Will print "AddrInUse".
+    ///     print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
+    /// }
+    /// ```
     pub fn kind(&self) -> ErrorKind {
         match self.repr {
             Repr::Os(code) => sys::decode_error_kind(code),
             Repr::Custom(ref c) => c.kind,
             Repr::Simple(kind) => kind,
+            Repr::SimpleMessage(kind, _) => kind,
             Repr::SgxStatus(..) => ErrorKind::SgxError,
         }
     }
@@ -336,6 +703,9 @@
                 .finish(),
             Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
             Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
+            Repr::SimpleMessage(kind, &message) => {
+                fmt.debug_struct("Error").field("kind", &kind).field("message", &message).finish()
+            }
             Repr::SgxStatus(status) => fmt
                 .debug_struct("Sgx")
                 .field("code", &status)
@@ -350,10 +720,11 @@
         match self.repr {
             Repr::Os(code) => {
                 let detail = sys::os::error_string(code);
-                write!(fmt, "{} (os error: {})", detail, code)
+                write!(fmt, "{} (os error {})", detail, code)
             }
             Repr::Custom(ref c) => c.error.fmt(fmt),
             Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
+            Repr::SimpleMessage(_, &msg) => msg.fmt(fmt),
             Repr::SgxStatus(status) => {
                 let detail = status.__description();
                 write!(fmt, "{} (sgx error: {})", detail, status)
@@ -363,9 +734,11 @@
 }
 
 impl error::Error for Error {
+    #[allow(deprecated, deprecated_in_future)]
     fn description(&self) -> &str {
         match self.repr {
             Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
+            Repr::SimpleMessage(_, &msg) => msg,
             Repr::Custom(ref c) => c.error.description(),
             Repr::SgxStatus(ref s) => s.description(),
         }
@@ -376,6 +749,7 @@
         match self.repr {
             Repr::Os(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::Custom(ref c) => c.error.cause(),
             Repr::SgxStatus(ref s) => Some(s),
         }
@@ -385,6 +759,7 @@
         match self.repr {
             Repr::Os(..) => None,
             Repr::Simple(..) => None,
+            Repr::SimpleMessage(..) => None,
             Repr::Custom(ref c) => c.error.source(),
             Repr::SgxStatus(ref s) => Some(s),
         }
@@ -392,12 +767,6 @@
 }
 
 fn _assert_error_is_sync_send() {
-    fn _is_sync_send<T: Sync+Send>() {}
+    fn _is_sync_send<T: Sync + Send>() {}
     _is_sync_send::<Error>();
 }
-
-impl error::Error for sgx_status_t {
-    fn description(&self) -> &str {
-        self.__description()
-    }
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/io/impls.rs b/sgx_tstd/src/io/impls.rs
index db9c454..f30b868 100644
--- a/sgx_tstd/src/io/impls.rs
+++ b/sgx_tstd/src/io/impls.rs
@@ -15,12 +15,13 @@
 // specific language governing permissions and limitations
 // under the License..
 
+use crate::alloc::Allocator;
+use crate::cmp;
+use crate::fmt;
 use crate::io::{
     self, BufRead, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
 };
-use core::cmp;
-use core::fmt;
-use core::mem;
+use crate::mem;
 
 // =============================================================================
 // Forwarding implementations
@@ -37,6 +38,11 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        (**self).is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         (**self).initializer()
     }
@@ -69,6 +75,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        (**self).is_write_vectored()
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         (**self).flush()
     }
@@ -89,6 +100,11 @@
     fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
         (**self).seek(pos)
     }
+
+    #[inline]
+    fn stream_position(&mut self) -> io::Result<u64> {
+        (**self).stream_position()
+    }
 }
 
 impl<B: BufRead + ?Sized> BufRead for &mut B {
@@ -125,6 +141,11 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        (**self).is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         (**self).initializer()
     }
@@ -157,6 +178,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        (**self).is_write_vectored()
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         (**self).flush()
     }
@@ -177,6 +203,11 @@
     fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
         (**self).seek(pos)
     }
+
+    #[inline]
+    fn stream_position(&mut self) -> io::Result<u64> {
+        (**self).stream_position()
+    }
 }
 
 impl<B: BufRead + ?Sized> BufRead for Box<B> {
@@ -241,6 +272,11 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::nop()
     }
@@ -248,7 +284,7 @@
     #[inline]
     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
         if buf.len() > self.len() {
-            return Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"));
+            return Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"));
         }
         let (a, b) = self.split_at(buf.len());
 
@@ -291,6 +327,10 @@
 ///
 /// Note that writing updates the slice to point to the yet unwritten part.
 /// The slice will be empty when it has been completely overwritten.
+///
+/// If the number of bytes to be written exceeds the size of the slice, write operations will
+/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
+/// kind `ErrorKind::WriteZero`.
 impl Write for &mut [u8] {
     #[inline]
     fn write(&mut self, data: &[u8]) -> io::Result<usize> {
@@ -315,11 +355,16 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
         if self.write(data)? == data.len() {
             Ok(())
         } else {
-            Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"))
+            Err(Error::new_const(ErrorKind::WriteZero, &"failed to write whole buffer"))
         }
     }
 
@@ -331,7 +376,7 @@
 
 /// Write is implemented for `Vec<u8>` by appending to the vector.
 /// The vector will grow as needed.
-impl Write for Vec<u8> {
+impl<A: Allocator> Write for Vec<u8, A> {
     #[inline]
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
         self.extend_from_slice(buf);
@@ -349,6 +394,11 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
         self.extend_from_slice(buf);
         Ok(())
diff --git a/sgx_tstd/src/io/lazy.rs b/sgx_tstd/src/io/lazy.bak.rs
similarity index 90%
rename from sgx_tstd/src/io/lazy.rs
rename to sgx_tstd/src/io/lazy.bak.rs
index 04334df..941f4e9 100644
--- a/sgx_tstd/src/io/lazy.rs
+++ b/sgx_tstd/src/io/lazy.bak.rs
@@ -15,15 +15,16 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::sync::SgxThreadMutex;
-use crate::sys_common;
 use alloc_crate::sync::Arc;
-use core::cell::{Cell, UnsafeCell};
+use core::cell::Cell;
+use core::cell::UnsafeCell;
 use core::ptr;
+use crate::sync::SgxThreadMutex as Mutex;
+use crate::sys_common;
 
 pub struct Lazy<T> {
     // We never call `lock.init()`, so it is UB to attempt to acquire this mutex reentrantly!
-    lock: SgxThreadMutex,
+    lock: Mutex,
     ptr: Cell<*mut Arc<T>>,
 }
 
@@ -36,10 +37,7 @@
 
 impl<T> Lazy<T> {
     pub const fn new() -> Lazy<T> {
-        Lazy {
-            lock: SgxThreadMutex::new(),
-            ptr: Cell::new(ptr::null_mut()),
-        }
+        Lazy { lock: Mutex::new(), ptr: Cell::new(ptr::null_mut()) }
     }
 }
 
@@ -88,7 +86,7 @@
 
 #[allow(dead_code)]
 pub struct LazyStatic<T> {
-    lock: SgxThreadMutex,
+    lock: Mutex,
     opt: UnsafeCell<Option<Arc<T>>>,
 }
 
@@ -97,16 +95,12 @@
 #[allow(dead_code)]
 impl<T> LazyStatic<T> {
     pub const fn new() -> LazyStatic<T> {
-        LazyStatic {
-            lock: SgxThreadMutex::new(),
-            opt: UnsafeCell::new(None),
-        }
+        LazyStatic { lock: Mutex::new(), opt: UnsafeCell::new(None) }
     }
 }
 
 #[allow(dead_code)]
 impl<T: Send + Sync + 'static> LazyStatic<T> {
-
     pub unsafe fn get(&'static self, init: fn() -> Arc<T>) -> Option<Arc<T>> {
         let r = self.lock.lock();
         if r.is_err() {
@@ -125,4 +119,4 @@
         *self.opt.get() = Some(ret.clone());
         ret
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/io/mod.rs b/sgx_tstd/src/io/mod.rs
index 471b40d..36306b0 100644
--- a/sgx_tstd/src/io/mod.rs
+++ b/sgx_tstd/src/io/mod.rs
@@ -15,44 +15,295 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::memchr;
-use core::cmp;
-use core::fmt;
-use core::ptr;
-use core::mem;
-use core::ops::{Deref, DerefMut};
-use alloc_crate::slice;
-use alloc_crate::vec::Vec;
-use alloc_crate::str;
-use alloc_crate::string::String;
+//! Traits, helpers, and type definitions for core I/O functionality.
+//!
+//! The `std::io` module contains a number of common things you'll need
+//! when doing input and output. The most core part of this module is
+//! the [`Read`] and [`Write`] traits, which provide the
+//! most general interface for reading and writing input and output.
+//!
+//! # Read and Write
+//!
+//! Because they are traits, [`Read`] and [`Write`] are implemented by a number
+//! of other types, and you can implement them for your types too. As such,
+//! you'll see a few different types of I/O throughout the documentation in
+//! this module: [`File`]s, [`TcpStream`]s, and sometimes even [`Vec<T>`]s. For
+//! example, [`Read`] adds a [`read`][`Read::read`] method, which we can use on
+//! [`File`]s:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//!     let mut f = File::open("foo.txt")?;
+//!     let mut buffer = [0; 10];
+//!
+//!     // read up to 10 bytes
+//!     let n = f.read(&mut buffer)?;
+//!
+//!     println!("The bytes: {:?}", &buffer[..n]);
+//!     Ok(())
+//! }
+//! ```
+//!
+//! [`Read`] and [`Write`] are so important, implementors of the two traits have a
+//! nickname: readers and writers. So you'll sometimes see 'a reader' instead
+//! of 'a type that implements the [`Read`] trait'. Much easier!
+//!
+//! ## Seek and BufRead
+//!
+//! Beyond that, there are two important traits that are provided: [`Seek`]
+//! and [`BufRead`]. Both of these build on top of a reader to control
+//! how the reading happens. [`Seek`] lets you control where the next byte is
+//! coming from:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::SeekFrom;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//!     let mut f = File::open("foo.txt")?;
+//!     let mut buffer = [0; 10];
+//!
+//!     // skip to the last 10 bytes of the file
+//!     f.seek(SeekFrom::End(-10))?;
+//!
+//!     // read up to 10 bytes
+//!     let n = f.read(&mut buffer)?;
+//!
+//!     println!("The bytes: {:?}", &buffer[..n]);
+//!     Ok(())
+//! }
+//! ```
+//!
+//! [`BufRead`] uses an internal buffer to provide a number of other ways to read, but
+//! to show it off, we'll need to talk about buffers in general. Keep reading!
+//!
+//! ## BufReader and BufWriter
+//!
+//! Byte-based interfaces are unwieldy and can be inefficient, as we'd need to be
+//! making near-constant calls to the operating system. To help with this,
+//! `std::io` comes with two structs, [`BufReader`] and [`BufWriter`], which wrap
+//! readers and writers. The wrapper uses a buffer, reducing the number of
+//! calls and providing nicer methods for accessing exactly what you want.
+//!
+//! For example, [`BufReader`] works with the [`BufRead`] trait to add extra
+//! methods to any reader:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//!     let f = File::open("foo.txt")?;
+//!     let mut reader = BufReader::new(f);
+//!     let mut buffer = String::new();
+//!
+//!     // read a line into buffer
+//!     reader.read_line(&mut buffer)?;
+//!
+//!     println!("{}", buffer);
+//!     Ok(())
+//! }
+//! ```
+//!
+//! [`BufWriter`] doesn't add any new ways of writing; it just buffers every call
+//! to [`write`][`Write::write`]:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufWriter;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//!     let f = File::create("foo.txt")?;
+//!     {
+//!         let mut writer = BufWriter::new(f);
+//!
+//!         // write a byte to the buffer
+//!         writer.write(&[42])?;
+//!
+//!     } // the buffer is flushed once writer goes out of scope
+//!
+//!     Ok(())
+//! }
+//! ```
+//!
+//! ## Standard input and output
+//!
+//! A very common source of input is standard input:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! fn main() -> io::Result<()> {
+//!     let mut input = String::new();
+//!
+//!     io::stdin().read_line(&mut input)?;
+//!
+//!     println!("You typed: {}", input.trim());
+//!     Ok(())
+//! }
+//! ```
+//!
+//! Note that you cannot use the [`?` operator] in functions that do not return
+//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
+//! or `match` on the return value to catch any possible errors:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! let mut input = String::new();
+//!
+//! io::stdin().read_line(&mut input).unwrap();
+//! ```
+//!
+//! And a very common source of output is standard output:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//!
+//! fn main() -> io::Result<()> {
+//!     io::stdout().write(&[42])?;
+//!     Ok(())
+//! }
+//! ```
+//!
+//! Of course, using [`io::stdout`] directly is less common than something like
+//! [`println!`].
+//!
+//! ## Iterator types
+//!
+//! A large number of the structures provided by `std::io` are for various
+//! ways of iterating over I/O. For example, [`Lines`] is used to split over
+//! lines:
+//!
+//! ```no_run
+//! use std::io;
+//! use std::io::prelude::*;
+//! use std::io::BufReader;
+//! use std::fs::File;
+//!
+//! fn main() -> io::Result<()> {
+//!     let f = File::open("foo.txt")?;
+//!     let reader = BufReader::new(f);
+//!
+//!     for line in reader.lines() {
+//!         println!("{}", line?);
+//!     }
+//!     Ok(())
+//! }
+//! ```
+//!
+//! ## Functions
+//!
+//! There are a number of [functions][functions-list] that offer access to various
+//! features. For example, we can use three of these functions to copy everything
+//! from standard input to standard output:
+//!
+//! ```no_run
+//! use std::io;
+//!
+//! fn main() -> io::Result<()> {
+//!     io::copy(&mut io::stdin(), &mut io::stdout())?;
+//!     Ok(())
+//! }
+//! ```
+//!
+//! [functions-list]: #functions-1
+//!
+//! ## io::Result
+//!
+//! Last, but certainly not least, is [`io::Result`]. This type is used
+//! as the return type of many `std::io` functions that can cause an error, and
+//! can be returned from your own functions as well. Many of the examples in this
+//! module use the [`?` operator]:
+//!
+//! ```
+//! use std::io;
+//!
+//! fn read_input() -> io::Result<()> {
+//!     let mut input = String::new();
+//!
+//!     io::stdin().read_line(&mut input)?;
+//!
+//!     println!("You typed: {}", input.trim());
+//!
+//!     Ok(())
+//! }
+//! ```
+//!
+//! The return type of `read_input()`, [`io::Result<()>`][`io::Result`], is a very
+//! common type for functions which don't have a 'real' return value, but do want to
+//! return errors if they happen. In this case, the only purpose of this function is
+//! to read the line and print it, so we use `()`.
+//!
+//! ## Platform-specific behavior
+//!
+//! Many I/O functions throughout the standard library are documented to indicate
+//! what various library or syscalls they are delegated to. This is done to help
+//! applications both understand what's happening under the hood as well as investigate
+//! any possibly unclear semantics. Note, however, that this is informative, not a binding
+//! contract. The implementation of many of these functions are subject to change over
+//! time and may call fewer or more syscalls/library functions.
+//!
+//! [`File`]: crate::fs::File
+//! [`TcpStream`]: crate::net::TcpStream
+//! [`io::stdout`]: stdout
+//! [`io::Result`]: self::Result
+//! [`?` operator]: ../../book/appendix-02-operators.html
+//! [`Result`]: crate::result::Result
+//! [`.unwrap()`]: crate::result::Result::unwrap
+
+use crate::cmp;
+use crate::convert::TryInto;
+use crate::fmt;
+use crate::mem::replace;
+use crate::ops::{Deref, DerefMut};
+use crate::ptr;
+use crate::slice;
+use crate::str;
 use crate::sys;
+use crate::sys_common::memchr;
 
 pub use self::buffered::IntoInnerError;
+pub use self::buffered::WriterPanicked;
 pub use self::buffered::{BufReader, BufWriter, LineWriter};
+pub use self::copy::copy;
 pub use self::cursor::Cursor;
 pub use self::error::{Error, ErrorKind, Result};
-pub use self::lazy::{Lazy};
-pub use sys::os::{errno, set_errno, error_string};
 #[cfg(feature = "stdio")]
 pub use self::stdio::{stderr, stdin, stdout, Stderr, Stdin, Stdout};
 #[cfg(feature = "stdio")]
 pub use self::stdio::{StderrLock, StdinLock, StdoutLock};
 #[cfg(feature = "stdio")]
 pub use self::stdio::{_eprint, _print};
-pub use self::util::{copy, empty, repeat, sink, Empty, Repeat, Sink};
+pub use self::util::{empty, repeat, sink, Empty, Repeat, Sink};
 
-pub mod prelude;
 mod buffered;
+pub(crate) mod copy;
 mod cursor;
 mod error;
 mod impls;
-mod lazy;
+pub mod prelude;
 #[cfg(feature = "stdio")]
 mod stdio;
 mod util;
 
 const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
 
+pub(crate) fn cleanup() {
+    stdio::cleanup()
+}
+
 struct Guard<'a> {
     buf: &'a mut Vec<u8>,
     len: usize,
@@ -93,7 +344,7 @@
         let ret = f(g.buf);
         if str::from_utf8(&g.buf[g.len..]).is_err() {
             ret.and_then(|_| {
-                Err(Error::new(ErrorKind::InvalidData, "stream did not contain valid UTF-8"))
+                Err(Error::new_const(ErrorKind::InvalidData, &"stream did not contain valid UTF-8"))
             })
         } else {
             g.len = g.buf.len();
@@ -126,7 +377,6 @@
 {
     let start_len = buf.len();
     let mut g = Guard { len: buf.len(), buf };
-    let ret;
     loop {
         if g.len == g.buf.len() {
             unsafe {
@@ -145,21 +395,20 @@
             }
         }
 
-        match r.read(&mut g.buf[g.len..]) {
-            Ok(0) => {
-                ret = Ok(g.len - start_len);
-                break;
+        let buf = &mut g.buf[g.len..];
+        match r.read(buf) {
+            Ok(0) => return Ok(g.len - start_len),
+            Ok(n) => {
+                // We can't allow bogus values from read. If it is too large, the returned vec could have its length
+                // set past its capacity, or if it overflows the vec could be shortened which could create an invalid
+                // string if this is called via read_to_string.
+                assert!(n <= buf.len());
+                g.len += n;
             }
-            Ok(n) => g.len += n,
             Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
-            Err(e) => {
-                ret = Err(e);
-                break;
-            }
+            Err(e) => return Err(e),
         }
     }
-
-    ret
 }
 
 pub(crate) fn default_read_vectored<F>(read: F, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
@@ -178,6 +427,25 @@
     write(buf)
 }
 
+pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [u8]) -> Result<()> {
+    while !buf.is_empty() {
+        match this.read(buf) {
+            Ok(0) => break,
+            Ok(n) => {
+                let tmp = buf;
+                buf = &mut tmp[n..];
+            }
+            Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
+            Err(e) => return Err(e),
+        }
+    }
+    if !buf.is_empty() {
+        Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"))
+    } else {
+        Ok(())
+    }
+}
+
 /// The `Read` trait allows for reading bytes from a source.
 ///
 /// Implementors of the `Read` trait are called 'readers'.
@@ -196,6 +464,58 @@
 /// therefore, using something that implements [`BufRead`], such as
 /// [`BufReader`], will be more efficient.
 ///
+/// # Examples
+///
+/// [`File`]s implement `Read`:
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> io::Result<()> {
+///     let mut f = File::open("foo.txt")?;
+///     let mut buffer = [0; 10];
+///
+///     // read up to 10 bytes
+///     f.read(&mut buffer)?;
+///
+///     let mut buffer = Vec::new();
+///     // read the whole file
+///     f.read_to_end(&mut buffer)?;
+///
+///     // read into a String, so that you don't need to do the conversion.
+///     let mut buffer = String::new();
+///     f.read_to_string(&mut buffer)?;
+///
+///     // and more! See the other methods for more details.
+///     Ok(())
+/// }
+/// ```
+///
+/// Read from [`&str`] because [`&[u8]`][prim@slice] implements `Read`:
+///
+/// ```no_run
+/// # use std::io;
+/// use std::io::prelude::*;
+///
+/// fn main() -> io::Result<()> {
+///     let mut b = "This string will be read".as_bytes();
+///     let mut buffer = [0; 10];
+///
+///     // read up to 10 bytes
+///     b.read(&mut buffer)?;
+///
+///     // etc... it works exactly as a File does!
+///     Ok(())
+/// }
+/// ```
+///
+/// [`read()`]: Read::read
+/// [`&str`]: prim@str
+/// [`std::io`]: self
+/// [`File`]: crate::fs::File
+#[cfg_attr(not(test), rustc_diagnostic_item = "IoRead")]
 pub trait Read {
     /// Pull some bytes from this source into the specified buffer, returning
     /// how many bytes were read.
@@ -204,29 +524,44 @@
     /// waiting for data, but if an object needs to block for a read and cannot,
     /// it will typically signal this via an [`Err`] return value.
     ///
-    /// If the return value of this method is [`Ok(n)`], then it must be
-    /// guaranteed that `0 <= n <= buf.len()`. A nonzero `n` value indicates
+    /// If the return value of this method is [`Ok(n)`], then implementations must
+    /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates
     /// that the buffer `buf` has been filled in with `n` bytes of data from this
     /// source. If `n` is `0`, then it can indicate one of two scenarios:
     ///
     /// 1. This reader has reached its "end of file" and will likely no longer
     ///    be able to produce bytes. Note that this does not mean that the
-    ///    reader will *always* no longer be able to produce bytes.
+    ///    reader will *always* no longer be able to produce bytes. As an example,
+    ///    on Linux, this method will call the `recv` syscall for a [`TcpStream`],
+    ///    where returning zero indicates the connection was shut down correctly. While
+    ///    for [`File`], it is possible to reach the end of file and get zero as result,
+    ///    but if more data is appended to the file, future calls to `read` will return
+    ///    more data.
     /// 2. The buffer specified was 0 bytes in length.
     ///
+    /// It is not an error if the returned value `n` is smaller than the buffer size,
+    /// even when the reader is not at the end of the stream yet.
+    /// This may happen for example because fewer bytes are actually available right now
+    /// (e. g. being close to end-of-file) or because read() was interrupted by a signal.
+    ///
+    /// As this trait is safe to implement, callers cannot rely on `n <= buf.len()` for safety.
+    /// Extra care needs to be taken when `unsafe` functions are used to access the read bytes.
+    /// Callers have to ensure that no unchecked out-of-bounds accesses are possible even if
+    /// `n > buf.len()`.
+    ///
     /// No guarantees are provided about the contents of `buf` when this
     /// function is called, implementations cannot rely on any property of the
     /// contents of `buf` being true. It is recommended that *implementations*
     /// only write data to `buf` instead of reading its contents.
     ///
-    /// Correspondingly, however, *callers* of this method may not assume any guarantees
+    /// Correspondingly, however, *callers* of this method must not assume any guarantees
     /// about how the implementation uses `buf`. The trait is safe to implement,
     /// so it is possible that the code that's supposed to write to the buffer might also read
     /// from it. It is your responsibility to make sure that `buf` is initialized
     /// before calling `read`. Calling `read` with an uninitialized `buf` (of the kind one
     /// obtains via [`MaybeUninit<T>`]) is not safe, and can lead to undefined behavior.
     ///
-    /// [`MaybeUninit<T>`]: ../mem/union.MaybeUninit.html
+    /// [`MaybeUninit<T>`]: crate::mem::MaybeUninit
     ///
     /// # Errors
     ///
@@ -237,13 +572,38 @@
     /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the read
     /// operation should be retried if there is nothing else to do.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`Ok(n)`]: Ok
+    /// [`File`]: crate::fs::File
+    /// [`TcpStream`]: crate::net::TcpStream
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = [0; 10];
+    ///
+    ///     // read up to 10 bytes
+    ///     let n = f.read(&mut buffer[..])?;
+    ///
+    ///     println!("The bytes: {:?}", &buffer[..n]);
+    ///     Ok(())
+    /// }
+    /// ```
     fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
 
     /// Like `read`, except that it reads into a slice of buffers.
     ///
     /// Data is copied to fill each buffer in order, with the final buffer
-    /// written to possibly being only partially filled. This method must behave
-    /// as a single call to `read` with the buffers concatenated would.
+    /// written to possibly being only partially filled. This method must
+    /// behave equivalently to a single call to `read` with concatenated
+    /// buffers.
     ///
     /// The default implementation calls `read` with either the first nonempty
     /// buffer provided, or an empty one if none exists.
@@ -251,6 +611,18 @@
         default_read_vectored(|b| self.read(b), bufs)
     }
 
+    /// Determines if this `Read`er has an efficient `read_vectored`
+    /// implementation.
+    ///
+    /// If a `Read`er does not override the default `read_vectored`
+    /// implementation, code using it may want to avoid the method all together
+    /// and coalesce writes into a single buffer for higher performance.
+    ///
+    /// The default implementation returns `false`.
+    fn is_read_vectored(&self) -> bool {
+        false
+    }
+
     /// Determines if this `Read`er can work with buffers of uninitialized
     /// memory.
     ///
@@ -270,9 +642,6 @@
     /// This method is unsafe because a `Read`er could otherwise return a
     /// non-zeroing `Initializer` from another `Read` type without an `unsafe`
     /// block.
-    ///
-    /// [`Initializer::nop()`]: ../../std/io/struct.Initializer.html#method.nop
-    /// [`Initializer`]: ../../std/io/struct.Initializer.html
     #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::zeroing()
@@ -297,6 +666,33 @@
     /// returns. Any bytes which have already been read will be appended to
     /// `buf`.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`read()`]: Read::read
+    /// [`Ok(0)`]: Ok
+    /// [`File`]: crate::fs::File
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = Vec::new();
+    ///
+    ///     // read the whole file
+    ///     f.read_to_end(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// (See also the [`std::fs::read`] convenience function for reading from a
+    /// file.)
+    ///
+    /// [`std::fs::read`]: crate::fs::read
     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
         read_to_end(self, buf)
     }
@@ -311,10 +707,34 @@
     /// If the data in this stream is *not* valid UTF-8 then an error is
     /// returned and `buf` is unchanged.
     ///
-    /// See [`read_to_end`][readtoend] for other error semantics.
+    /// See [`read_to_end`] for other error semantics.
     ///
-    /// [readtoend]: #method.read_to_end
+    /// [`read_to_end`]: Read::read_to_end
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`File`]: crate::fs::File
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = String::new();
+    ///
+    ///     f.read_to_string(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// (See also the [`std::fs::read_to_string`] convenience function for
+    /// reading from a file.)
+    ///
+    /// [`std::fs::read_to_string`]: crate::fs::read_to_string
     fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
         // Note that we do *not* call `.read_to_end()` here. We are passing
         // `&mut Vec<u8>` (the raw contents of `buf`) into the `read_to_end`
@@ -336,7 +756,9 @@
     /// No guarantees are provided about the contents of `buf` when this
     /// function is called, implementations cannot rely on any property of the
     /// contents of `buf` being true. It is recommended that implementations
-    /// only write data to `buf` instead of reading its contents.
+    /// only write data to `buf` instead of reading its contents. The
+    /// documentation on [`read`] has a more detailed explanation on this
+    /// subject.
     ///
     /// # Errors
     ///
@@ -355,30 +777,65 @@
     /// has read, but it will never read more than would be necessary to
     /// completely fill the buffer.
     ///
-    fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
-        while !buf.is_empty() {
-            match self.read(buf) {
-                Ok(0) => break,
-                Ok(n) => {
-                    let tmp = buf;
-                    buf = &mut tmp[n..];
-                }
-                Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
-                Err(e) => return Err(e),
-            }
-        }
-        if !buf.is_empty() {
-            Err(Error::new(ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
-        } else {
-            Ok(())
-        }
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`read`]: Read::read
+    /// [`File`]: crate::fs::File
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = [0; 10];
+    ///
+    ///     // read exactly 10 bytes
+    ///     f.read_exact(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
+        default_read_exact(self, buf)
     }
 
-    /// Creates a "by reference" adaptor for this instance of `Read`.
+    /// Creates a "by reference" adapter for this instance of `Read`.
     ///
-    /// The returned adaptor also implements `Read` and will simply borrow this
+    /// The returned adapter also implements `Read` and will simply borrow this
     /// current reader.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`File`]: crate::fs::File
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::Read;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = Vec::new();
+    ///     let mut other_buffer = Vec::new();
+    ///
+    ///     {
+    ///         let reference = f.by_ref();
+    ///
+    ///         // read at most 5 bytes
+    ///         reference.take(5).read_to_end(&mut buffer)?;
+    ///
+    ///     } // drop our &mut reference so we can use f again
+    ///
+    ///     // original file still usable, read the rest
+    ///     f.read_to_end(&mut other_buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn by_ref(&mut self) -> &mut Self
     where
         Self: Sized,
@@ -393,6 +850,28 @@
     /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
     /// otherwise. EOF is mapped to returning [`None`] from this iterator.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`File`]: crate::fs::File
+    /// [`Result`]: crate::result::Result
+    /// [`io::Error`]: self::Error
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///
+    ///     for byte in f.bytes() {
+    ///         println!("{}", byte.unwrap());
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
     fn bytes(self) -> Bytes<Self>
     where
         Self: Sized,
@@ -400,12 +879,36 @@
         Bytes { inner: self }
     }
 
-    /// Creates an adaptor which will chain this stream with another.
+    /// Creates an adapter which will chain this stream with another.
     ///
     /// The returned `Read` instance will first read all bytes from this object
     /// until EOF is encountered. Afterwards the output is equivalent to the
     /// output of `next`.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`File`]: crate::fs::File
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f1 = File::open("foo.txt")?;
+    ///     let mut f2 = File::open("bar.txt")?;
+    ///
+    ///     let mut handle = f1.chain(f2);
+    ///     let mut buffer = String::new();
+    ///
+    ///     // read the value into a String. We could use any Read method here,
+    ///     // this is just one example.
+    ///     handle.read_to_string(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn chain<R: Read>(self, next: R) -> Chain<Self, R>
     where
         Self: Sized,
@@ -413,13 +916,37 @@
         Chain { first: self, second: next, done_first: false }
     }
 
-    /// Creates an adaptor which will read at most `limit` bytes from it.
+    /// Creates an adapter which will read at most `limit` bytes from it.
     ///
     /// This function returns a new instance of `Read` which will read at most
     /// `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
     /// read errors will not count towards the number of bytes read and future
     /// calls to [`read()`] may succeed.
     ///
+    /// # Examples
+    ///
+    /// [`File`]s implement `Read`:
+    ///
+    /// [`File`]: crate::fs::File
+    /// [`Ok(0)`]: Ok
+    /// [`read()`]: Read::read
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///     let mut buffer = [0; 5];
+    ///
+    ///     // read at most five bytes
+    ///     let mut handle = f.take(5);
+    ///
+    ///     handle.read(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn take(self, limit: u64) -> Take<Self>
     where
         Self: Sized,
@@ -428,6 +955,53 @@
     }
 }
 
+/// Read all bytes from a [reader][Read] into a new [`String`].
+///
+/// This is a convenience function for [`Read::read_to_string`]. Using this
+/// function avoids having to create a variable first and provides more type
+/// safety since you can only get the buffer out if there were no errors. (If you
+/// use [`Read::read_to_string`] you have to remember to check whether the read
+/// succeeded because otherwise your buffer will be empty or only partially full.)
+///
+/// # Performance
+///
+/// The downside of this function's increased ease of use and type safety is
+/// that it gives you less control over performance. For example, you can't
+/// pre-allocate memory like you can using [`String::with_capacity`] and
+/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
+/// occurs while reading.
+///
+/// In many cases, this function's performance will be adequate and the ease of use
+/// and type safety tradeoffs will be worth it. However, there are cases where you
+/// need more control over performance, and in those cases you should definitely use
+/// [`Read::read_to_string`] directly.
+///
+/// # Errors
+///
+/// This function forces you to handle errors because the output (the `String`)
+/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
+/// that can occur. If any error occurs, you will get an [`Err`], so you
+/// don't have to worry about your buffer being empty or partially full.
+///
+/// # Examples
+///
+/// ```no_run
+/// #![feature(io_read_to_string)]
+///
+/// # use std::io;
+/// fn main() -> io::Result<()> {
+///     let stdin = io::read_to_string(&mut io::stdin())?;
+///     println!("Stdin was:");
+///     println!("{}", stdin);
+///     Ok(())
+/// }
+/// ```
+pub fn read_to_string<R: Read>(reader: &mut R) -> Result<String> {
+    let mut buf = String::new();
+    reader.read_to_string(&mut buf)?;
+    Ok(buf)
+}
+
 /// A buffer type used with `Read::read_vectored`.
 ///
 /// It is semantically a wrapper around an `&mut [u8]`, but is guaranteed to be
@@ -459,6 +1033,31 @@
 
     /// Advance the internal cursor of the slice.
     ///
+    /// Also see [`IoSliceMut::advance_slices`] to advance the cursors of
+    /// multiple buffers.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(io_slice_advance)]
+    ///
+    /// use std::io::IoSliceMut;
+    /// use std::ops::Deref;
+    ///
+    /// let mut data = [1; 8];
+    /// let mut buf = IoSliceMut::new(&mut data);
+    ///
+    /// // Mark 3 bytes as read.
+    /// buf.advance(3);
+    /// assert_eq!(buf.deref(), [1; 5].as_ref());
+    /// ```
+    #[inline]
+    pub fn advance(&mut self, n: usize) {
+        self.0.advance(n)
+    }
+
+    /// Advance the internal cursor of the slices.
+    ///
     /// # Notes
     ///
     /// Elements in the slice may be modified if the cursor is not advanced to
@@ -467,8 +1066,30 @@
     /// the first `IoSliceMut` will be untouched however the second will be
     /// modified to remove the first 2 bytes (10 - 8).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(io_slice_advance)]
+    ///
+    /// use std::io::IoSliceMut;
+    /// use std::ops::Deref;
+    ///
+    /// let mut buf1 = [1; 8];
+    /// let mut buf2 = [2; 16];
+    /// let mut buf3 = [3; 8];
+    /// let mut bufs = &mut [
+    ///     IoSliceMut::new(&mut buf1),
+    ///     IoSliceMut::new(&mut buf2),
+    ///     IoSliceMut::new(&mut buf3),
+    /// ][..];
+    ///
+    /// // Mark 10 bytes as read.
+    /// IoSliceMut::advance_slices(&mut bufs, 10);
+    /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
+    /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
+    /// ```
     #[inline]
-    pub fn advance<'b>(bufs: &'b mut [IoSliceMut<'a>], n: usize) -> &'b mut [IoSliceMut<'a>] {
+    pub fn advance_slices(bufs: &mut &mut [IoSliceMut<'a>], n: usize) {
         // Number of buffers to remove.
         let mut remove = 0;
         // Total length of all the to be removed buffers.
@@ -482,11 +1103,10 @@
             }
         }
 
-        let bufs = &mut bufs[remove..];
+        *bufs = &mut replace(bufs, &mut [])[remove..];
         if !bufs.is_empty() {
-            bufs[0].0.advance(n - accumulated_len)
+            bufs[0].advance(n - accumulated_len)
         }
-        bufs
     }
 }
 
@@ -508,7 +1128,7 @@
 
 /// A buffer type used with `Write::write_vectored`.
 ///
-/// It is semantically a wrapper around an `&[u8]`, but is guaranteed to be
+/// It is semantically a wrapper around a `&[u8]`, but is guaranteed to be
 /// ABI compatible with the `iovec` type on Unix platforms and `WSABUF` on
 /// Windows.
 #[derive(Copy, Clone)]
@@ -538,6 +1158,31 @@
 
     /// Advance the internal cursor of the slice.
     ///
+    /// Also see [`IoSlice::advance_slices`] to advance the cursors of multiple
+    /// buffers.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(io_slice_advance)]
+    ///
+    /// use std::io::IoSlice;
+    /// use std::ops::Deref;
+    ///
+    /// let mut data = [1; 8];
+    /// let mut buf = IoSlice::new(&mut data);
+    ///
+    /// // Mark 3 bytes as read.
+    /// buf.advance(3);
+    /// assert_eq!(buf.deref(), [1; 5].as_ref());
+    /// ```
+    #[inline]
+    pub fn advance(&mut self, n: usize) {
+        self.0.advance(n)
+    }
+
+    /// Advance the internal cursor of the slices.
+    ///
     /// # Notes
     ///
     /// Elements in the slice may be modified if the cursor is not advanced to
@@ -546,8 +1191,29 @@
     /// first `IoSlice` will be untouched however the second will be modified to
     /// remove the first 2 bytes (10 - 8).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(io_slice_advance)]
+    ///
+    /// use std::io::IoSlice;
+    /// use std::ops::Deref;
+    ///
+    /// let buf1 = [1; 8];
+    /// let buf2 = [2; 16];
+    /// let buf3 = [3; 8];
+    /// let mut bufs = &mut [
+    ///     IoSlice::new(&buf1),
+    ///     IoSlice::new(&buf2),
+    ///     IoSlice::new(&buf3),
+    /// ][..];
+    ///
+    /// // Mark 10 bytes as written.
+    /// IoSlice::advance_slices(&mut bufs, 10);
+    /// assert_eq!(bufs[0].deref(), [2; 14].as_ref());
+    /// assert_eq!(bufs[1].deref(), [3; 8].as_ref());
     #[inline]
-    pub fn advance<'b>(bufs: &'b mut [IoSlice<'a>], n: usize) -> &'b mut [IoSlice<'a>] {
+    pub fn advance_slices(bufs: &mut &mut [IoSlice<'a>], n: usize) {
         // Number of buffers to remove.
         let mut remove = 0;
         // Total length of all the to be removed buffers.
@@ -561,11 +1227,10 @@
             }
         }
 
-        let bufs = &mut bufs[remove..];
+        *bufs = &mut replace(bufs, &mut [])[remove..];
         if !bufs.is_empty() {
-            bufs[0].0.advance(n - accumulated_len)
+            bufs[0].advance(n - accumulated_len)
         }
-        bufs
     }
 }
 
@@ -626,7 +1291,7 @@
 /// * The [`write`] method will attempt to write some data into the object,
 ///   returning how many bytes were successfully written.
 ///
-/// * The [`flush`] method is useful for adaptors and explicit buffers
+/// * The [`flush`] method is useful for adapters and explicit buffers
 ///   themselves for ensuring that all buffered data has been pushed out to the
 ///   'true sink'.
 ///
@@ -634,15 +1299,40 @@
 /// throughout [`std::io`] take and provide types which implement the `Write`
 /// trait.
 ///
-/// [`write`]: #tymethod.write
-/// [`flush`]: #tymethod.flush
-/// [`std::io`]: index.html
+/// [`write`]: Write::write
+/// [`flush`]: Write::flush
+/// [`std::io`]: self
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> std::io::Result<()> {
+///     let data = b"some bytes";
+///
+///     let mut pos = 0;
+///     let mut buffer = File::create("foo.txt")?;
+///
+///     while pos < data.len() {
+///         let bytes_written = buffer.write(&data[pos..])?;
+///         pos += bytes_written;
+///     }
+///     Ok(())
+/// }
+/// ```
+///
+/// The trait also provides convenience methods like [`write_all`], which calls
+/// `write` in a loop until its entire input has been written.
+///
+/// [`write_all`]: Write::write_all
+#[cfg_attr(not(test), rustc_diagnostic_item = "IoWrite")]
 pub trait Write {
     /// Write a buffer into this writer, returning how many bytes were written.
     ///
     /// This function will attempt to write the entire contents of `buf`, but
-    /// the entire write may not succeed, or the write may also generate an
+    /// the entire write might not succeed, or the write may also generate an
     /// error. A call to `write` represents *at most one* attempt to write to
     /// any wrapped object.
     ///
@@ -667,24 +1357,73 @@
     /// An error of the [`ErrorKind::Interrupted`] kind is non-fatal and the
     /// write operation should be retried if there is nothing else to do.
     ///
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`Ok(n)`]:  ../../std/result/enum.Result.html#variant.Ok
-    /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut buffer = File::create("foo.txt")?;
+    ///
+    ///     // Writes some prefix of the byte string, not necessarily all of it.
+    ///     buffer.write(b"some bytes")?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// [`Ok(n)`]: Ok
     fn write(&mut self, buf: &[u8]) -> Result<usize>;
 
-    /// Like `write`, except that it writes from a slice of buffers.
+    /// Like [`write`], except that it writes from a slice of buffers.
     ///
     /// Data is copied from each buffer in order, with the final buffer
     /// read from possibly being only partially consumed. This method must
-    /// behave as a call to `write` with the buffers concatenated would.
+    /// behave as a call to [`write`] with the buffers concatenated would.
     ///
-    /// The default implementation calls `write` with either the first nonempty
+    /// The default implementation calls [`write`] with either the first nonempty
     /// buffer provided, or an empty one if none exists.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::IoSlice;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut data1 = [1; 8];
+    ///     let mut data2 = [15; 8];
+    ///     let io_slice1 = IoSlice::new(&mut data1);
+    ///     let io_slice2 = IoSlice::new(&mut data2);
+    ///
+    ///     let mut buffer = File::create("foo.txt")?;
+    ///
+    ///     // Writes some prefix of the byte string, not necessarily all of it.
+    ///     buffer.write_vectored(&[io_slice1, io_slice2])?;
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// [`write`]: Write::write
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
         default_write_vectored(|b| self.write(b), bufs)
     }
 
+    /// Determines if this `Write`r has an efficient [`write_vectored`]
+    /// implementation.
+    ///
+    /// If a `Write`r does not override the default [`write_vectored`]
+    /// implementation, code using it may want to avoid the method all together
+    /// and coalesce writes into a single buffer for higher performance.
+    ///
+    /// The default implementation returns `false`.
+    ///
+    /// [`write_vectored`]: Write::write_vectored
+    fn is_write_vectored(&self) -> bool {
+        false
+    }
+
     /// Flush this output stream, ensuring that all intermediately buffered
     /// contents reach their destination.
     ///
@@ -693,6 +1432,21 @@
     /// It is considered an error if not all bytes could be written due to
     /// I/O errors or EOF being reached.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::prelude::*;
+    /// use std::io::BufWriter;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut buffer = BufWriter::new(File::create("foo.txt")?);
+    ///
+    ///     buffer.write_all(b"some bytes")?;
+    ///     buffer.flush()?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn flush(&mut self) -> Result<()>;
 
     /// Attempts to write an entire buffer into this writer.
@@ -711,14 +1465,29 @@
     /// This function will return the first error of
     /// non-[`ErrorKind::Interrupted`] kind that [`write`] returns.
     ///
-    /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
-    /// [`write`]: #tymethod.write
+    /// [`write`]: Write::write
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut buffer = File::create("foo.txt")?;
+    ///
+    ///     buffer.write_all(b"some bytes")?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
         while !buf.is_empty() {
             match self.write(buf) {
                 Ok(0) => {
-                    return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
+                    return Err(Error::new_const(
+                        ErrorKind::WriteZero,
+                        &"failed to write whole buffer",
+                    ));
                 }
                 Ok(n) => buf = &buf[n..],
                 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
@@ -739,30 +1508,55 @@
     ///
     /// If the buffer contains no data, this will never call [`write_vectored`].
     ///
-    /// [`write_vectored`]: #method.write_vectored
-    /// [`ErrorKind::Interrupted`]: ../../std/io/enum.ErrorKind.html#variant.Interrupted
-    ///
     /// # Notes
     ///
-    ///
-    /// Unlike `io::Write::write_vectored`, this takes a *mutable* reference to
-    /// a slice of `IoSlice`s, not an immutable one. That's because we need to
+    /// Unlike [`write_vectored`], this takes a *mutable* reference to
+    /// a slice of [`IoSlice`]s, not an immutable one. That's because we need to
     /// modify the slice to keep track of the bytes already written.
     ///
     /// Once this function returns, the contents of `bufs` are unspecified, as
-    /// this depends on how many calls to `write_vectored` were necessary. It is
+    /// this depends on how many calls to [`write_vectored`] were necessary. It is
     /// best to understand this function as taking ownership of `bufs` and to
     /// not use `bufs` afterwards. The underlying buffers, to which the
-    /// `IoSlice`s point (but not the `IoSlice`s themselves), are unchanged and
+    /// [`IoSlice`]s point (but not the [`IoSlice`]s themselves), are unchanged and
     /// can be reused.
     ///
+    /// [`write_vectored`]: Write::write_vectored
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(write_all_vectored)]
+    /// # fn main() -> std::io::Result<()> {
+    ///
+    /// use std::io::{Write, IoSlice};
+    ///
+    /// let mut writer = Vec::new();
+    /// let bufs = &mut [
+    ///     IoSlice::new(&[1]),
+    ///     IoSlice::new(&[2, 3]),
+    ///     IoSlice::new(&[4, 5, 6]),
+    /// ];
+    ///
+    /// writer.write_all_vectored(bufs)?;
+    /// // Note: the contents of `bufs` is now undefined, see the Notes section.
+    ///
+    /// assert_eq!(writer, &[1, 2, 3, 4, 5, 6]);
+    /// # Ok(()) }
+    /// ```
     fn write_all_vectored(&mut self, mut bufs: &mut [IoSlice<'_>]) -> Result<()> {
+        // Guarantee that bufs is empty if it contains no data,
+        // to avoid calling write_vectored if there is no data to be written.
+        IoSlice::advance_slices(&mut bufs, 0);
         while !bufs.is_empty() {
             match self.write_vectored(bufs) {
                 Ok(0) => {
-                    return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
+                    return Err(Error::new_const(
+                        ErrorKind::WriteZero,
+                        &"failed to write whole buffer",
+                    ));
                 }
-                Ok(n) => bufs = IoSlice::advance(mem::take(&mut bufs), n),
+                Ok(n) => IoSlice::advance_slices(&mut bufs, n),
                 Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
                 Err(e) => return Err(e),
             }
@@ -774,33 +1568,46 @@
     /// encountered.
     ///
     /// This method is primarily used to interface with the
-    /// [`format_args!`][formatargs] macro, but it is rare that this should
-    /// explicitly be called. The [`write!`][write] macro should be favored to
+    /// [`format_args!()`] macro, and it is rare that this should
+    /// explicitly be called. The [`write!()`] macro should be favored to
     /// invoke this method instead.
     ///
-    /// [formatargs]: ../macro.format_args.html
-    /// [write]: ../macro.write.html
-    ///
-    /// This function internally uses the [`write_all`][writeall] method on
+    /// This function internally uses the [`write_all`] method on
     /// this trait and hence will continuously write data so long as no errors
     /// are received. This also means that partial writes are not indicated in
     /// this signature.
     ///
-    /// [writeall]: #method.write_all
+    /// [`write_all`]: Write::write_all
     ///
     /// # Errors
     ///
     /// This function will return any I/O error reported while formatting.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut buffer = File::create("foo.txt")?;
+    ///
+    ///     // this call
+    ///     write!(buffer, "{:.*}", 2, 1.234567)?;
+    ///     // turns into this:
+    ///     buffer.write_fmt(format_args!("{:.*}", 2, 1.234567))?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
         // Create a shim which translates a Write to a fmt::Write and saves
         // off I/O errors. instead of discarding them
-        struct Adaptor<'a, T: ?Sized + 'a> {
+        struct Adapter<'a, T: ?Sized + 'a> {
             inner: &'a mut T,
             error: Result<()>,
         }
 
-        impl<T: Write + ?Sized> fmt::Write for Adaptor<'_, T> {
+        impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
             fn write_str(&mut self, s: &str) -> fmt::Result {
                 match self.inner.write_all(s.as_bytes()) {
                     Ok(()) => Ok(()),
@@ -812,7 +1619,7 @@
             }
         }
 
-        let mut output = Adaptor { inner: self, error: Ok(()) };
+        let mut output = Adapter { inner: self, error: Ok(()) };
         match fmt::write(&mut output, fmt) {
             Ok(()) => Ok(()),
             Err(..) => {
@@ -820,17 +1627,33 @@
                 if output.error.is_err() {
                     output.error
                 } else {
-                    Err(Error::new(ErrorKind::Other, "formatter error"))
+                    Err(Error::new_const(ErrorKind::Uncategorized, &"formatter error"))
                 }
             }
         }
     }
 
-    /// Creates a "by reference" adaptor for this instance of `Write`.
+    /// Creates a "by reference" adapter for this instance of `Write`.
     ///
-    /// The returned adaptor also implements `Write` and will simply borrow this
+    /// The returned adapter also implements `Write` and will simply borrow this
     /// current writer.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::Write;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let mut buffer = File::create("foo.txt")?;
+    ///
+    ///     let reference = buffer.by_ref();
+    ///
+    ///     // we can use reference just like our original buffer
+    ///     reference.write_all(b"some bytes")?;
+    ///     Ok(())
+    /// }
+    /// ```
     fn by_ref(&mut self) -> &mut Self
     where
         Self: Sized,
@@ -845,6 +1668,26 @@
 /// The stream typically has a fixed size, allowing seeking relative to either
 /// end or the current offset.
 ///
+/// # Examples
+///
+/// [`File`]s implement `Seek`:
+///
+/// [`File`]: crate::fs::File
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+/// use std::fs::File;
+/// use std::io::SeekFrom;
+///
+/// fn main() -> io::Result<()> {
+///     let mut f = File::open("foo.txt")?;
+///
+///     // move the cursor 42 bytes from the start of the file
+///     f.seek(SeekFrom::Start(42))?;
+///     Ok(())
+/// }
+/// ```
 pub trait Seek {
     /// Seek to an offset, in bytes, in a stream.
     ///
@@ -857,11 +1700,44 @@
     ///
     /// # Errors
     ///
-    /// Seeking to a negative offset is considered an error.
+    /// Seeking can fail, for example because it might involve flushing a buffer.
     ///
-    /// [`SeekFrom::Start`]: enum.SeekFrom.html#variant.Start
+    /// Seeking to a negative offset is considered an error.
     fn seek(&mut self, pos: SeekFrom) -> Result<u64>;
 
+    /// Rewind to the beginning of a stream.
+    ///
+    /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`.
+    ///
+    /// # Errors
+    ///
+    /// Rewinding can fail, for example because it might involve flushing a buffer.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::io::{Read, Seek, Write};
+    /// use std::fs::OpenOptions;
+    ///
+    /// let mut f = OpenOptions::new()
+    ///     .write(true)
+    ///     .read(true)
+    ///     .create(true)
+    ///     .open("foo.txt").unwrap();
+    ///
+    /// let hello = "Hello!\n";
+    /// write!(f, "{}", hello).unwrap();
+    /// f.rewind().unwrap();
+    ///
+    /// let mut buf = String::new();
+    /// f.read_to_string(&mut buf).unwrap();
+    /// assert_eq!(&buf, hello);
+    /// ```
+    fn rewind(&mut self) -> Result<()> {
+        self.seek(SeekFrom::Start(0))?;
+        Ok(())
+    }
+
     /// Returns the length of this stream (in bytes).
     ///
     /// This method is implemented using up to three seek operations. If this
@@ -879,6 +1755,23 @@
     /// data is appended to a file). So calling this method multiple times does
     /// not necessarily return the same length each time.
     ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// #![feature(seek_stream_len)]
+    /// use std::{
+    ///     io::{self, Seek},
+    ///     fs::File,
+    /// };
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = File::open("foo.txt")?;
+    ///
+    ///     let len = f.stream_len()?;
+    ///     println!("The file is currently {} bytes long", len);
+    ///     Ok(())
+    /// }
+    /// ```
     fn stream_len(&mut self) -> Result<u64> {
         let old_pos = self.stream_position()?;
         let len = self.seek(SeekFrom::End(0))?;
@@ -896,6 +1789,25 @@
     ///
     /// This is equivalent to `self.seek(SeekFrom::Current(0))`.
     ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::{
+    ///     io::{self, BufRead, BufReader, Seek},
+    ///     fs::File,
+    /// };
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut f = BufReader::new(File::open("foo.txt")?);
+    ///
+    ///     let before = f.stream_position()?;
+    ///     f.read_line(&mut String::new())?;
+    ///     let after = f.stream_position()?;
+    ///
+    ///     println!("The first line was {} bytes long", after - before);
+    ///     Ok(())
+    /// }
+    /// ```
     fn stream_position(&mut self) -> Result<u64> {
         self.seek(SeekFrom::Current(0))
     }
@@ -904,8 +1816,6 @@
 /// Enumeration of possible methods to seek within an I/O object.
 ///
 /// It is used by the [`Seek`] trait.
-///
-/// [`Seek`]: trait.Seek.html
 #[derive(Copy, PartialEq, Eq, Clone, Debug)]
 pub enum SeekFrom {
     /// Sets the offset to the provided number of bytes.
@@ -961,6 +1871,46 @@
 /// if you want to read by line, you'll need `BufRead`, which includes a
 /// [`read_line`] method as well as a [`lines`] iterator.
 ///
+/// # Examples
+///
+/// A locked standard input implements `BufRead`:
+///
+/// ```no_run
+/// use std::io;
+/// use std::io::prelude::*;
+///
+/// let stdin = io::stdin();
+/// for line in stdin.lock().lines() {
+///     println!("{}", line.unwrap());
+/// }
+/// ```
+///
+/// If you have something that implements [`Read`], you can use the [`BufReader`
+/// type][`BufReader`] to turn it into a `BufRead`.
+///
+/// For example, [`File`] implements [`Read`], but not `BufRead`.
+/// [`BufReader`] to the rescue!
+///
+/// [`File`]: crate::fs::File
+/// [`read_line`]: BufRead::read_line
+/// [`lines`]: BufRead::lines
+///
+/// ```no_run
+/// use std::io::{self, BufReader};
+/// use std::io::prelude::*;
+/// use std::fs::File;
+///
+/// fn main() -> io::Result<()> {
+///     let f = File::open("foo.txt")?;
+///     let f = BufReader::new(f);
+///
+///     for line in f.lines() {
+///         println!("{}", line.unwrap());
+///     }
+///
+///     Ok(())
+/// }
+/// ```
 pub trait BufRead: Read {
     /// Returns the contents of the internal buffer, filling it with more data
     /// from the inner reader if it is empty.
@@ -972,7 +1922,7 @@
     /// be called with the number of bytes that are consumed from this buffer to
     /// ensure that the bytes are never returned twice.
     ///
-    /// [`consume`]: #tymethod.consume
+    /// [`consume`]: BufRead::consume
     ///
     /// An empty buffer returned indicates that the stream has reached EOF.
     ///
@@ -981,6 +1931,26 @@
     /// This function will return an I/O error if the underlying reader was
     /// read, but returned an error.
     ///
+    /// # Examples
+    ///
+    /// A locked standard input implements `BufRead`:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    ///
+    /// let stdin = io::stdin();
+    /// let mut stdin = stdin.lock();
+    ///
+    /// let buffer = stdin.fill_buf().unwrap();
+    ///
+    /// // work with buffer
+    /// println!("{:?}", buffer);
+    ///
+    /// // ensure the bytes we worked with aren't returned again later
+    /// let length = buffer.len();
+    /// stdin.consume(length);
+    /// ```
     fn fill_buf(&mut self) -> Result<&[u8]>;
 
     /// Tells this buffer that `amt` bytes have been consumed from the buffer,
@@ -996,8 +1966,44 @@
     /// The `amt` must be `<=` the number of bytes in the buffer returned by
     /// [`fill_buf`].
     ///
+    /// # Examples
+    ///
+    /// Since `consume()` is meant to be used with [`fill_buf`],
+    /// that method's example includes an example of `consume()`.
+    ///
+    /// [`fill_buf`]: BufRead::fill_buf
     fn consume(&mut self, amt: usize);
 
+    /// Check if the underlying `Read` has any data left to be read.
+    ///
+    /// This function may fill the buffer to check for data,
+    /// so this functions returns `Result<bool>`, not `bool`.
+    ///
+    /// Default implementation calls `fill_buf` and checks that
+    /// returned slice is empty (which means that there is no data left,
+    /// since EOF is reached).
+    ///
+    /// Examples
+    ///
+    /// ```
+    /// #![feature(buf_read_has_data_left)]
+    /// use std::io;
+    /// use std::io::prelude::*;
+    ///
+    /// let stdin = io::stdin();
+    /// let mut stdin = stdin.lock();
+    ///
+    /// while stdin.has_data_left().unwrap() {
+    ///     let mut line = String::new();
+    ///     stdin.read_line(&mut line).unwrap();
+    ///     // work with line
+    ///     println!("{:?}", line);
+    /// }
+    /// ```
+    fn has_data_left(&mut self) -> Result<bool> {
+        self.fill_buf().map(|b| !b.is_empty())
+    }
+
     /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached.
     ///
     /// This function will read bytes from the underlying stream until the
@@ -1006,6 +2012,10 @@
     ///
     /// If successful, this function will return the total number of bytes read.
     ///
+    /// This function is blocking and should be used carefully: it is possible for
+    /// an attacker to continuously send bytes without ever sending the delimiter
+    /// or EOF.
+    ///
     /// # Errors
     ///
     /// This function will ignore all instances of [`ErrorKind::Interrupted`] and
@@ -1014,24 +2024,61 @@
     /// If an I/O error is encountered then all bytes read so far will be
     /// present in `buf` and its length will have been adjusted appropriately.
     ///
-    /// [`fill_buf`]: #tymethod.fill_buf
-    /// [`ErrorKind::Interrupted`]: enum.ErrorKind.html#variant.Interrupted
+    /// [`fill_buf`]: BufRead::fill_buf
     ///
+    /// # Examples
+    ///
+    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+    /// this example, we use [`Cursor`] to read all the bytes in a byte slice
+    /// in hyphen delimited segments:
+    ///
+    /// ```
+    /// use std::io::{self, BufRead};
+    ///
+    /// let mut cursor = io::Cursor::new(b"lorem-ipsum");
+    /// let mut buf = vec![];
+    ///
+    /// // cursor is at 'l'
+    /// let num_bytes = cursor.read_until(b'-', &mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 6);
+    /// assert_eq!(buf, b"lorem-");
+    /// buf.clear();
+    ///
+    /// // cursor is at 'i'
+    /// let num_bytes = cursor.read_until(b'-', &mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 5);
+    /// assert_eq!(buf, b"ipsum");
+    /// buf.clear();
+    ///
+    /// // cursor is at EOF
+    /// let num_bytes = cursor.read_until(b'-', &mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 0);
+    /// assert_eq!(buf, b"");
+    /// ```
     fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
         read_until(self, byte, buf)
     }
 
-    /// Read all bytes until a newline (the 0xA byte) is reached, and append
+    /// Read all bytes until a newline (the `0xA` byte) is reached, and append
     /// them to the provided buffer.
     ///
     /// This function will read bytes from the underlying stream until the
-    /// newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
+    /// newline delimiter (the `0xA` byte) or EOF is found. Once found, all bytes
     /// up to, and including, the delimiter (if found) will be appended to
     /// `buf`.
     ///
     /// If successful, this function will return the total number of bytes read.
     ///
-    /// If this function returns `Ok(0)`, the stream has reached EOF.
+    /// If this function returns [`Ok(0)`], the stream has reached EOF.
+    ///
+    /// This function is blocking and should be used carefully: it is possible for
+    /// an attacker to continuously send bytes without ever sending a newline
+    /// or EOF.
+    ///
+    /// [`Ok(0)`]: Ok
     ///
     /// # Errors
     ///
@@ -1040,8 +2087,39 @@
     /// error is encountered then `buf` may contain some bytes already read in
     /// the event that all data read so far was valid UTF-8.
     ///
-    /// [`read_until`]: #method.read_until
+    /// [`read_until`]: BufRead::read_until
     ///
+    /// # Examples
+    ///
+    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+    /// this example, we use [`Cursor`] to read all the lines in a byte slice:
+    ///
+    /// ```
+    /// use std::io::{self, BufRead};
+    ///
+    /// let mut cursor = io::Cursor::new(b"foo\nbar");
+    /// let mut buf = String::new();
+    ///
+    /// // cursor is at 'f'
+    /// let num_bytes = cursor.read_line(&mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 4);
+    /// assert_eq!(buf, "foo\n");
+    /// buf.clear();
+    ///
+    /// // cursor is at 'b'
+    /// let num_bytes = cursor.read_line(&mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 3);
+    /// assert_eq!(buf, "bar");
+    /// buf.clear();
+    ///
+    /// // cursor is at EOF
+    /// let num_bytes = cursor.read_line(&mut buf)
+    ///     .expect("reading from cursor won't fail");
+    /// assert_eq!(num_bytes, 0);
+    /// assert_eq!(buf, "");
+    /// ```
     fn read_line(&mut self, buf: &mut String) -> Result<usize> {
         // Note that we are not calling the `.read_until` method here, but
         // rather our hardcoded implementation. For more details as to why, see
@@ -1059,10 +2137,26 @@
     /// This function will yield errors whenever [`read_until`] would have
     /// also yielded an error.
     ///
-    /// [`io::Result`]: type.Result.html
-    /// [`Vec<u8>`]: ../vec/struct.Vec.html
-    /// [`read_until`]: #method.read_until
+    /// [`io::Result`]: self::Result
+    /// [`read_until`]: BufRead::read_until
     ///
+    /// # Examples
+    ///
+    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+    /// this example, we use [`Cursor`] to iterate over all hyphen delimited
+    /// segments in a byte slice
+    ///
+    /// ```
+    /// use std::io::{self, BufRead};
+    ///
+    /// let cursor = io::Cursor::new(b"lorem-ipsum-dolor");
+    ///
+    /// let mut split_iter = cursor.split(b'-').map(|l| l.unwrap());
+    /// assert_eq!(split_iter.next(), Some(b"lorem".to_vec()));
+    /// assert_eq!(split_iter.next(), Some(b"ipsum".to_vec()));
+    /// assert_eq!(split_iter.next(), Some(b"dolor".to_vec()));
+    /// assert_eq!(split_iter.next(), None);
+    /// ```
     fn split(self, byte: u8) -> Split<Self>
     where
         Self: Sized,
@@ -1074,16 +2168,31 @@
     ///
     /// The iterator returned from this function will yield instances of
     /// [`io::Result`]`<`[`String`]`>`. Each string returned will *not* have a newline
-    /// byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
+    /// byte (the `0xA` byte) or `CRLF` (`0xD`, `0xA` bytes) at the end.
     ///
-    /// [`io::Result`]: type.Result.html
-    /// [`String`]: ../string/struct.String.html
+    /// [`io::Result`]: self::Result
+    ///
+    /// # Examples
+    ///
+    /// [`std::io::Cursor`][`Cursor`] is a type that implements `BufRead`. In
+    /// this example, we use [`Cursor`] to iterate over all the lines in a byte
+    /// slice.
+    ///
+    /// ```
+    /// use std::io::{self, BufRead};
+    ///
+    /// let cursor = io::Cursor::new(b"lorem\nipsum\r\ndolor");
+    ///
+    /// let mut lines_iter = cursor.lines().map(|l| l.unwrap());
+    /// assert_eq!(lines_iter.next(), Some(String::from("lorem")));
+    /// assert_eq!(lines_iter.next(), Some(String::from("ipsum")));
+    /// assert_eq!(lines_iter.next(), Some(String::from("dolor")));
+    /// assert_eq!(lines_iter.next(), None);
+    /// ```
     ///
     /// # Errors
     ///
     /// Each line of the iterator has the same error semantics as [`BufRead::read_line`].
-    ///
-    /// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
     fn lines(self) -> Lines<Self>
     where
         Self: Sized,
@@ -1092,12 +2201,13 @@
     }
 }
 
-/// Adaptor to chain together two readers.
+/// Adapter to chain together two readers.
 ///
 /// This struct is generally created by calling [`chain`] on a reader.
 /// Please see the documentation of [`chain`] for more details.
 ///
-/// [`chain`]: trait.Read.html#method.chain
+/// [`chain`]: Read::chain
+#[derive(Debug)]
 pub struct Chain<T, U> {
     first: T,
     second: U,
@@ -1107,12 +2217,44 @@
 impl<T, U> Chain<T, U> {
     /// Consumes the `Chain`, returning the wrapped readers.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut foo_file = File::open("foo.txt")?;
+    ///     let mut bar_file = File::open("bar.txt")?;
+    ///
+    ///     let chain = foo_file.chain(bar_file);
+    ///     let (foo_file, bar_file) = chain.into_inner();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn into_inner(self) -> (T, U) {
         (self.first, self.second)
     }
 
     /// Gets references to the underlying readers in this `Chain`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut foo_file = File::open("foo.txt")?;
+    ///     let mut bar_file = File::open("bar.txt")?;
+    ///
+    ///     let chain = foo_file.chain(bar_file);
+    ///     let (foo_file, bar_file) = chain.get_ref();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn get_ref(&self) -> (&T, &U) {
         (&self.first, &self.second)
     }
@@ -1123,17 +2265,27 @@
     /// underlying readers as doing so may corrupt the internal state of this
     /// `Chain`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut foo_file = File::open("foo.txt")?;
+    ///     let mut bar_file = File::open("bar.txt")?;
+    ///
+    ///     let mut chain = foo_file.chain(bar_file);
+    ///     let (foo_file, bar_file) = chain.get_mut();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn get_mut(&mut self) -> (&mut T, &mut U) {
         (&mut self.first, &mut self.second)
     }
 }
 
-impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("Chain").field("t", &self.first).field("u", &self.second).finish()
-    }
-}
-
 impl<T: Read, U: Read> Read for Chain<T, U> {
     fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
         if !self.done_first {
@@ -1179,12 +2331,27 @@
     }
 }
 
-/// Reader adaptor which limits the bytes read from an underlying reader.
+impl<T, U> SizeHint for Chain<T, U> {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        SizeHint::lower_bound(&self.first) + SizeHint::lower_bound(&self.second)
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        match (SizeHint::upper_bound(&self.first), SizeHint::upper_bound(&self.second)) {
+            (Some(first), Some(second)) => first.checked_add(second),
+            _ => None,
+        }
+    }
+}
+
+/// Reader adapter which limits the bytes read from an underlying reader.
 ///
 /// This struct is generally created by calling [`take`] on a reader.
 /// Please see the documentation of [`take`] for more details.
 ///
-/// [`take`]: trait.Read.html#method.take
+/// [`take`]: Read::take
 #[derive(Debug)]
 pub struct Take<T> {
     inner: T,
@@ -1200,8 +2367,23 @@
     /// This instance may reach `EOF` after reading fewer bytes than indicated by
     /// this method if the underlying [`Read`] instance reaches EOF.
     ///
-    /// [`Read`]: ../../std/io/trait.Read.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let f = File::open("foo.txt")?;
+    ///
+    ///     // read at most five bytes
+    ///     let handle = f.take(5);
+    ///
+    ///     println!("limit: {}", handle.limit());
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn limit(&self) -> u64 {
         self.limit
     }
@@ -1211,18 +2393,72 @@
     /// the amount of bytes read and the previous limit value don't matter when
     /// calling this method.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let f = File::open("foo.txt")?;
+    ///
+    ///     // read at most five bytes
+    ///     let mut handle = f.take(5);
+    ///     handle.set_limit(10);
+    ///
+    ///     assert_eq!(handle.limit(), 10);
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn set_limit(&mut self, limit: u64) {
         self.limit = limit;
     }
 
     /// Consumes the `Take`, returning the wrapped reader.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut file = File::open("foo.txt")?;
+    ///
+    ///     let mut buffer = [0; 5];
+    ///     let mut handle = file.take(5);
+    ///     handle.read(&mut buffer)?;
+    ///
+    ///     let file = handle.into_inner();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn into_inner(self) -> T {
         self.inner
     }
 
     /// Gets a reference to the underlying reader.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut file = File::open("foo.txt")?;
+    ///
+    ///     let mut buffer = [0; 5];
+    ///     let mut handle = file.take(5);
+    ///     handle.read(&mut buffer)?;
+    ///
+    ///     let file = handle.get_ref();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn get_ref(&self) -> &T {
         &self.inner
     }
@@ -1233,6 +2469,24 @@
     /// underlying reader as doing so may corrupt the internal limit of this
     /// `Take`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::io::prelude::*;
+    /// use std::fs::File;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut file = File::open("foo.txt")?;
+    ///
+    ///     let mut buffer = [0; 5];
+    ///     let mut handle = file.take(5);
+    ///     handle.read(&mut buffer)?;
+    ///
+    ///     let file = handle.get_mut();
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn get_mut(&mut self) -> &mut T {
         &mut self.inner
     }
@@ -1283,12 +2537,27 @@
     }
 }
 
+impl<T> SizeHint for Take<T> {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        cmp::min(SizeHint::lower_bound(&self.inner) as u64, self.limit) as usize
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        match SizeHint::upper_bound(&self.inner) {
+            Some(upper_bound) => Some(cmp::min(upper_bound as u64, self.limit) as usize),
+            None => self.limit.try_into().ok(),
+        }
+    }
+}
+
 /// An iterator over `u8` values of a reader.
 ///
 /// This struct is generally created by calling [`bytes`] on a reader.
 /// Please see the documentation of [`bytes`] for more details.
 ///
-/// [`bytes`]: trait.Read.html#method.bytes
+/// [`bytes`]: Read::bytes
 #[derive(Debug)]
 pub struct Bytes<R> {
     inner: R,
@@ -1308,6 +2577,68 @@
             };
         }
     }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        SizeHint::size_hint(&self.inner)
+    }
+}
+
+trait SizeHint {
+    fn lower_bound(&self) -> usize;
+
+    fn upper_bound(&self) -> Option<usize>;
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.lower_bound(), self.upper_bound())
+    }
+}
+
+impl<T> SizeHint for T {
+    #[inline]
+    default fn lower_bound(&self) -> usize {
+        0
+    }
+
+    #[inline]
+    default fn upper_bound(&self) -> Option<usize> {
+        None
+    }
+}
+
+impl<T> SizeHint for &mut T {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        SizeHint::lower_bound(*self)
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        SizeHint::upper_bound(*self)
+    }
+}
+
+impl<T> SizeHint for Box<T> {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        SizeHint::lower_bound(&**self)
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        SizeHint::upper_bound(&**self)
+    }
+}
+
+impl SizeHint for &[u8] {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        self.len()
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        Some(self.len())
+    }
 }
 
 /// An iterator over the contents of an instance of `BufRead` split on a
@@ -1316,7 +2647,7 @@
 /// This struct is generally created by calling [`split`] on a `BufRead`.
 /// Please see the documentation of [`split`] for more details.
 ///
-/// [`split`]: trait.BufRead.html#method.split
+/// [`split`]: BufRead::split
 #[derive(Debug)]
 pub struct Split<B> {
     buf: B,
@@ -1346,7 +2677,7 @@
 /// This struct is generally created by calling [`lines`] on a `BufRead`.
 /// Please see the documentation of [`lines`] for more details.
 ///
-/// [`lines`]: trait.BufRead.html#method.lines
+/// [`lines`]: BufRead::lines
 #[derive(Debug)]
 pub struct Lines<B> {
     buf: B,
diff --git a/sgx_tstd/src/io/prelude.rs b/sgx_tstd/src/io/prelude.rs
index 88896e5..9afeb92 100644
--- a/sgx_tstd/src/io/prelude.rs
+++ b/sgx_tstd/src/io/prelude.rs
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! The I/O Prelude
+//! The I/O Prelude.
 //!
 //! The purpose of this module is to alleviate imports of many common I/O traits
 //! by adding a glob import to the top of I/O heavy modules:
diff --git a/sgx_tstd/src/io/stdio.rs b/sgx_tstd/src/io/stdio.rs
index f472c2d..197ed9c 100644
--- a/sgx_tstd/src/io/stdio.rs
+++ b/sgx_tstd/src/io/stdio.rs
@@ -16,13 +16,16 @@
 // under the License..
 
 use crate::io::prelude::*;
-use crate::io::lazy::LazyStatic;
-use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter};
-use crate::sync::{SgxMutex, SgxMutexGuard, SgxReentrantMutex, SgxReentrantMutexGuard};
+
+use crate::cell::RefCell;
+use crate::fmt;
+use crate::io::{self, BufReader, Initializer, IoSlice, IoSliceMut, LineWriter, Lines, Split};
+use crate::lazy::SyncOnceCell;
+use crate::pin::Pin;
+use crate::sync::{SgxMutex as Mutex, SgxMutexGuard as MutexGuard};
 use crate::sys::stdio;
-use core::cell::RefCell;
-use core::fmt;
-use alloc_crate::sync::Arc;
+use crate::sys_common::remutex::{SgxReentrantMutex as ReentrantMutex, SgxReentrantMutexGuard as ReentrantMutexGuard};
+
 /// A handle to a raw instance of the standard input stream of this process.
 ///
 /// This handle is not synchronized or buffered in any fashion. Constructed via
@@ -48,8 +51,8 @@
 /// handles is **not** available to raw handles returned from this function.
 ///
 /// The returned handle has no external synchronization or buffering.
-fn stdin_raw() -> io::Result<StdinRaw> {
-    stdio::Stdin::new().map(StdinRaw)
+const fn stdin_raw() -> StdinRaw {
+    StdinRaw(stdio::Stdin::new())
 }
 
 /// Constructs a new raw handle to the standard output stream of this process.
@@ -61,8 +64,8 @@
 ///
 /// The returned handle has no external synchronization or buffering layered on
 /// top.
-fn stdout_raw() -> io::Result<StdoutRaw> {
-    stdio::Stdout::new().map(StdoutRaw)
+const fn stdout_raw() -> StdoutRaw {
+    StdoutRaw(stdio::Stdout::new())
 }
 
 /// Constructs a new raw handle to the standard error stream of this process.
@@ -72,93 +75,99 @@
 ///
 /// The returned handle has no external synchronization or buffering layered on
 /// top.
-fn stderr_raw() -> io::Result<StderrRaw> {
-    stdio::Stderr::new().map(StderrRaw)
+const fn stderr_raw() -> StderrRaw {
+    StderrRaw(stdio::Stderr::new())
 }
 
 impl Read for StdinRaw {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        self.0.read(buf)
+        handle_ebadf(self.0.read(buf), 0)
     }
 
     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        self.0.read_vectored(bufs)
+        handle_ebadf(self.0.read_vectored(bufs), 0)
+    }
+
+    #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
     }
 
     #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::nop()
     }
+
+    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+        handle_ebadf(self.0.read_to_end(buf), 0)
+    }
+
+    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+        handle_ebadf(self.0.read_to_string(buf), 0)
+    }
 }
+
 impl Write for StdoutRaw {
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        self.0.write(buf)
-    }
-
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        self.0.write_vectored(bufs)
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        self.0.flush()
-    }
-}
-impl Write for StderrRaw {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        self.0.write(buf)
-    }
-
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        self.0.write_vectored(bufs)
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        self.0.flush()
-    }
-}
-
-enum Maybe<T> {
-    Real(T),
-    Fake,
-}
-
-impl<W: io::Write> io::Write for Maybe<W> {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        match *self {
-            Maybe::Real(ref mut w) => handle_ebadf(w.write(buf), buf.len()),
-            Maybe::Fake => Ok(buf.len()),
-        }
+        handle_ebadf(self.0.write(buf), buf.len())
     }
 
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         let total = bufs.iter().map(|b| b.len()).sum();
-        match self {
-            Maybe::Real(w) => handle_ebadf(w.write_vectored(bufs), total),
-            Maybe::Fake => Ok(total),
-        }
+        handle_ebadf(self.0.write_vectored(bufs), total)
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
     }
 
     fn flush(&mut self) -> io::Result<()> {
-        match *self {
-            Maybe::Real(ref mut w) => handle_ebadf(w.flush(), ()),
-            Maybe::Fake => Ok(()),
-        }
+        handle_ebadf(self.0.flush(), ())
+    }
+
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        handle_ebadf(self.0.write_all(buf), ())
+    }
+
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        handle_ebadf(self.0.write_all_vectored(bufs), ())
+    }
+
+    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+        handle_ebadf(self.0.write_fmt(fmt), ())
     }
 }
 
-impl<R: io::Read> io::Read for Maybe<R> {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        match *self {
-            Maybe::Real(ref mut r) => handle_ebadf(r.read(buf), 0),
-            Maybe::Fake => Ok(0),
-        }
+impl Write for StderrRaw {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        handle_ebadf(self.0.write(buf), buf.len())
     }
 
-    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        match self {
-            Maybe::Real(r) => handle_ebadf(r.read_vectored(bufs), 0),
-            Maybe::Fake => Ok(0),
-        }
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        let total = bufs.iter().map(|b| b.len()).sum();
+        handle_ebadf(self.0.write_vectored(bufs), total)
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        handle_ebadf(self.0.flush(), ())
+    }
+
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        handle_ebadf(self.0.write_all(buf), ())
+    }
+
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        handle_ebadf(self.0.write_all_vectored(bufs), ())
+    }
+
+    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
+        handle_ebadf(self.0.write_fmt(fmt), ())
     }
 }
 
@@ -181,32 +190,58 @@
 ///
 /// Created by the [`io::stdin`] method.
 ///
-/// [`io::stdin`]: fn.stdin.html
-/// [`BufRead`]: trait.BufRead.html
+/// [`io::stdin`]: stdin
 ///
 /// ### Note: Windows Portability Consideration
+///
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
 /// an error.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+///     let mut buffer = String::new();
+///     let mut stdin = io::stdin(); // We get `Stdin` here.
+///     stdin.read_line(&mut buffer)?;
+///     Ok(())
+/// }
+/// ```
 pub struct Stdin {
-    inner: Arc<SgxMutex<BufReader<Maybe<StdinRaw>>>>,
+    inner: &'static Mutex<BufReader<StdinRaw>>,
 }
 
-/// A locked reference to the `Stdin` handle.
+/// A locked reference to the [`Stdin`] handle.
 ///
 /// This handle implements both the [`Read`] and [`BufRead`] traits, and
 /// is constructed via the [`Stdin::lock`] method.
 ///
-/// [`Read`]: trait.Read.html
-/// [`BufRead`]: trait.BufRead.html
-/// [`Stdin::lock`]: struct.Stdin.html#method.lock
-///
 /// ### Note: Windows Portability Consideration
+///
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
 /// an error.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::{self, BufRead};
+///
+/// fn main() -> io::Result<()> {
+///     let mut buffer = String::new();
+///     let stdin = io::stdin(); // We get `Stdin` here.
+///     {
+///         let mut handle = stdin.lock(); // We get `StdinLock` here.
+///         handle.read_line(&mut buffer)?;
+///     } // `StdinLock` is dropped here.
+///     Ok(())
+/// }
+/// ```
 pub struct StdinLock<'a> {
-    inner: SgxMutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
+    inner: MutexGuard<'a, BufReader<StdinRaw>>,
 }
 
 /// Constructs a new handle to the standard input of the current process.
@@ -215,28 +250,87 @@
 /// is synchronized via a mutex. If you need more explicit control over
 /// locking, see the [`Stdin::lock`] method.
 ///
-/// [`Stdin::lock`]: struct.Stdin.html#method.lock
+/// ### Note: Windows Portability Consideration
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io;
+///
+/// fn main() -> io::Result<()> {
+///     let mut buffer = String::new();
+///     io::stdin().read_line(&mut buffer)?;
+///     Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, BufRead};
+///
+/// fn main() -> io::Result<()> {
+///     let mut buffer = String::new();
+///     let stdin = io::stdin();
+///     let mut handle = stdin.lock();
+///
+///     handle.read_line(&mut buffer)?;
+///     Ok(())
+/// }
+/// ```
+pub fn stdin() -> Stdin {
+    static INSTANCE: SyncOnceCell<Mutex<BufReader<StdinRaw>>> = SyncOnceCell::new();
+    Stdin {
+        inner: INSTANCE.get_or_init(|| {
+            Mutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin_raw()))
+        }),
+    }
+}
+
+/// Constructs a new locked handle to the standard input of the current
+/// process.
+///
+/// Each handle returned is a guard granting locked access to a shared
+/// global buffer whose access is synchronized via a mutex. If you need
+/// more explicit control over locking, for example, in a multi-threaded
+/// program, use the [`io::stdin`] function to obtain an unlocked handle,
+/// along with the [`Stdin::lock`] method.
+///
+/// The lock is released when the returned guard goes out of scope. The
+/// returned guard also implements the [`Read`] and [`BufRead`] traits for
+/// accessing the underlying data.
+///
+/// **Note**: The mutex locked by this handle is not reentrant. Even in a
+/// single-threaded program, calling other code that accesses [`Stdin`]
+/// could cause a deadlock or panic, if this locked handle is held across
+/// that call.
 ///
 /// ### Note: Windows Portability Consideration
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
 /// an error.
 ///
-pub fn stdin() -> Stdin {
-    static INSTANCE: LazyStatic<SgxMutex<BufReader<Maybe<StdinRaw>>>> = LazyStatic::new();
-    return Stdin {
-        inner: unsafe { INSTANCE.get(stdin_init).expect("cannot access stdin during shutdown") },
-    };
-
-    fn stdin_init() -> Arc<SgxMutex<BufReader<Maybe<StdinRaw>>>> {
-        // This must not reentrantly access `INSTANCE`
-        let stdin = match stdin_raw() {
-            Ok(stdin) => Maybe::Real(stdin),
-            _ => Maybe::Fake,
-        };
-
-        Arc::new(SgxMutex::new(BufReader::with_capacity(stdio::STDIN_BUF_SIZE, stdin)))
-    }
+/// # Examples
+///
+/// ```no_run
+/// #![feature(stdio_locked)]
+/// use std::io::{self, BufRead};
+///
+/// fn main() -> io::Result<()> {
+///     let mut buffer = String::new();
+///     let mut handle = io::stdin_locked();
+///
+///     handle.read_line(&mut buffer)?;
+///     Ok(())
+/// }
+/// ```
+pub fn stdin_locked() -> StdinLock<'static> {
+    stdin().into_locked()
 }
 
 impl Stdin {
@@ -247,11 +341,22 @@
     /// returned guard also implements the [`Read`] and [`BufRead`] traits for
     /// accessing the underlying data.
     ///
-    /// [`Read`]: trait.Read.html
-    /// [`BufRead`]: trait.BufRead.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::io::{self, BufRead};
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut buffer = String::new();
+    ///     let stdin = io::stdin();
+    ///     let mut handle = stdin.lock();
+    ///
+    ///     handle.read_line(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn lock(&self) -> StdinLock<'_> {
-        StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
+        self.lock_any()
     }
 
     /// Locks this handle and reads a line of input, appending it to the specified buffer.
@@ -259,16 +364,112 @@
     /// For detailed semantics of this method, see the documentation on
     /// [`BufRead::read_line`].
     ///
-    /// [`BufRead::read_line`]: trait.BufRead.html#method.read_line
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::io;
+    ///
+    /// let mut input = String::new();
+    /// match io::stdin().read_line(&mut input) {
+    ///     Ok(n) => {
+    ///         println!("{} bytes read", n);
+    ///         println!("{}", input);
+    ///     }
+    ///     Err(error) => println!("error: {}", error),
+    /// }
+    /// ```
+    ///
+    /// You can run the example one of two ways:
+    ///
+    /// - Pipe some text to it, e.g., `printf foo | path/to/executable`
+    /// - Give it text interactively by running the executable directly,
+    ///   in which case it will wait for the Enter key to be pressed before
+    ///   continuing
     pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
         self.lock().read_line(buf)
     }
+
+    // Locks this handle with any lifetime. This depends on the
+    // implementation detail that the underlying `Mutex` is static.
+    fn lock_any<'a>(&self) -> StdinLock<'a> {
+        StdinLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
+    }
+
+    /// Consumes this handle to the standard input stream, locking the
+    /// shared global buffer associated with the stream and returning a
+    /// readable guard.
+    ///
+    /// The lock is released when the returned guard goes out of scope. The
+    /// returned guard also implements the [`Read`] and [`BufRead`] traits
+    /// for accessing the underlying data.
+    ///
+    /// It is often simpler to directly get a locked handle using the
+    /// [`stdin_locked`] function instead, unless nearby code also needs to
+    /// use an unlocked handle.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(stdio_locked)]
+    /// use std::io::{self, BufRead};
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut buffer = String::new();
+    ///     let mut handle = io::stdin().into_locked();
+    ///
+    ///     handle.read_line(&mut buffer)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn into_locked(self) -> StdinLock<'static> {
+        self.lock_any()
+    }
+
+    /// Consumes this handle and returns an iterator over input lines.
+    ///
+    /// For detailed semantics of this method, see the documentation on
+    /// [`BufRead::lines`].
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(stdin_forwarders)]
+    /// use std::io;
+    ///
+    /// let lines = io::stdin().lines();
+    /// for line in lines {
+    ///     println!("got a line: {}", line.unwrap());
+    /// }
+    /// ```
+    pub fn lines(self) -> Lines<StdinLock<'static>> {
+        self.into_locked().lines()
+    }
+
+    /// Consumes this handle and returns an iterator over input bytes,
+    /// split at the specified byte value.
+    ///
+    /// For detailed semantics of this method, see the documentation on
+    /// [`BufRead::split`].
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(stdin_forwarders)]
+    /// use std::io;
+    ///
+    /// let splits = io::stdin().split(b'-');
+    /// for split in splits {
+    ///     println!("got a chunk: {}", String::from_utf8_lossy(&split.unwrap()));
+    /// }
+    /// ```
+    pub fn split(self, byte: u8) -> Split<StdinLock<'static>> {
+        self.into_locked().split(byte)
+    }
 }
 
 impl fmt::Debug for Stdin {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Stdin { .. }")
+        f.debug_struct("Stdin").finish_non_exhaustive()
     }
 }
 
@@ -280,6 +481,10 @@
         self.lock().read_vectored(bufs)
     }
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.lock().is_read_vectored()
+    }
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::nop()
     }
@@ -294,6 +499,13 @@
     }
 }
 
+// only used by platform-dependent io::copy specializations, i.e. unused on some platforms
+impl StdinLock<'_> {
+    pub(crate) fn as_mut_buf(&mut self) -> &mut BufReader<impl Read> {
+        &mut self.inner
+    }
+}
+
 impl Read for StdinLock<'_> {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
         self.inner.read(buf)
@@ -304,23 +516,49 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.inner.is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::nop()
     }
+
+    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+        self.inner.read_to_end(buf)
+    }
+
+    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+        self.inner.read_to_string(buf)
+    }
+
+    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
+        self.inner.read_exact(buf)
+    }
 }
 
 impl BufRead for StdinLock<'_> {
     fn fill_buf(&mut self) -> io::Result<&[u8]> {
         self.inner.fill_buf()
     }
+
     fn consume(&mut self, n: usize) {
         self.inner.consume(n)
     }
+
+    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
+        self.inner.read_until(byte, buf)
+    }
+
+    fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
+        self.inner.read_line(buf)
+    }
 }
 
 impl fmt::Debug for StdinLock<'_> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("StdinLock { .. }")
+        f.debug_struct("StdinLock").finish_non_exhaustive()
     }
 }
 
@@ -337,57 +575,124 @@
 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
 /// an error.
 ///
-/// [`lock`]: #method.lock
-/// [`io::stdout`]: fn.stdout.html
+/// [`lock`]: Stdout::lock
+/// [`io::stdout`]: stdout
 pub struct Stdout {
     // FIXME: this should be LineWriter or BufWriter depending on the state of
     //        stdout (tty or not). Note that if this is not line buffered it
     //        should also flush-on-panic or some form of flush-on-abort.
-    inner: Arc<SgxReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>>,
+    inner: Pin<&'static ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>>,
 }
 
-/// A locked reference to the `Stdout` handle.
+/// A locked reference to the [`Stdout`] handle.
 ///
 /// This handle implements the [`Write`] trait, and is constructed via
-/// the [`Stdout::lock`] method.
+/// the [`Stdout::lock`] method. See its documentation for more.
 ///
 /// ### Note: Windows Portability Consideration
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
 /// an error.
-///
-/// [`Write`]: trait.Write.html
-/// [`Stdout::lock`]: struct.Stdout.html#method.lock
 pub struct StdoutLock<'a> {
-    inner: SgxReentrantMutexGuard<'a, RefCell<LineWriter<Maybe<StdoutRaw>>>>,
+    inner: ReentrantMutexGuard<'a, RefCell<LineWriter<StdoutRaw>>>,
 }
 
+static STDOUT: SyncOnceCell<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = SyncOnceCell::new();
+
 /// Constructs a new handle to the standard output of the current process.
 ///
 /// Each handle returned is a reference to a shared global buffer whose access
 /// is synchronized via a mutex. If you need more explicit control over
 /// locking, see the [`Stdout::lock`] method.
 ///
-/// [`Stdout::lock`]: struct.Stdout.html#method.lock
+/// ### Note: Windows Portability Consideration
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     io::stdout().write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     let stdout = io::stdout();
+///     let mut handle = stdout.lock();
+///
+///     handle.write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
+pub fn stdout() -> Stdout {
+    Stdout {
+        inner: Pin::static_ref(&STDOUT).get_or_init_pin(
+            || ReentrantMutex::new(RefCell::new(LineWriter::new(stdout_raw()))),
+            |_| (),
+        ),
+    }
+}
+
+/// Constructs a new locked handle to the standard output of the current
+/// process.
+///
+/// Each handle returned is a guard granting locked access to a shared
+/// global buffer whose access is synchronized via a mutex. If you need
+/// more explicit control over locking, for example, in a multi-threaded
+/// program, use the [`io::stdout`] function to obtain an unlocked handle,
+/// along with the [`Stdout::lock`] method.
+///
+/// The lock is released when the returned guard goes out of scope. The
+/// returned guard also implements the [`Write`] trait for writing data.
 ///
 /// ### Note: Windows Portability Consideration
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
 /// an error.
 ///
-pub fn stdout() -> Stdout {
-    static INSTANCE: LazyStatic<SgxReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> = LazyStatic::new();
-    return Stdout {
-        inner: unsafe { INSTANCE.get(stdout_init).expect("cannot access stdout during shutdown") },
-    };
+/// # Examples
+///
+/// ```no_run
+/// #![feature(stdio_locked)]
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     let mut handle = io::stdout_locked();
+///
+///     handle.write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
+pub fn stdout_locked() -> StdoutLock<'static> {
+    stdout().into_locked()
+}
 
-    fn stdout_init() -> Arc<SgxReentrantMutex<RefCell<LineWriter<Maybe<StdoutRaw>>>>> {
-        // This must not reentrantly access `INSTANCE`
-        let stdout = match stdout_raw() {
-            Ok(stdout) => Maybe::Real(stdout),
-            _ => Maybe::Fake,
-        };
-        Arc::new(SgxReentrantMutex::new(RefCell::new(LineWriter::new(stdout))))
+pub fn cleanup() {
+    if let Some(instance) = STDOUT.get() {
+        // Flush the data and disable buffering during shutdown
+        // by replacing the line writer by one with zero
+        // buffering capacity.
+        // We use try_lock() instead of lock(), because someone
+        // might have leaked a StdoutLock, which would
+        // otherwise cause a deadlock here.
+        if let Some(lock) = Pin::static_ref(instance).try_lock() {
+            *lock.borrow_mut() = LineWriter::with_capacity(0, stdout_raw());
+        }
     }
 }
 
@@ -398,30 +703,112 @@
     /// The lock is released when the returned lock goes out of scope. The
     /// returned guard also implements the `Write` trait for writing data.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io::{self, Write};
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let stdout = io::stdout();
+    ///     let mut handle = stdout.lock();
+    ///
+    ///     handle.write_all(b"hello world")?;
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn lock(&self) -> StdoutLock<'_> {
-        StdoutLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
+        self.lock_any()
+    }
+
+    // Locks this handle with any lifetime. This depends on the
+    // implementation detail that the underlying `ReentrantMutex` is
+    // static.
+    fn lock_any<'a>(&self) -> StdoutLock<'a> {
+        StdoutLock { inner: self.inner.lock() }
+    }
+
+    /// Consumes this handle to the standard output stream, locking the
+    /// shared global buffer associated with the stream and returning a
+    /// writable guard.
+    ///
+    /// The lock is released when the returned lock goes out of scope. The
+    /// returned guard also implements the [`Write`] trait for writing data.
+    ///
+    /// It is often simpler to directly get a locked handle using the
+    /// [`io::stdout_locked`] function instead, unless nearby code also
+    /// needs to use an unlocked handle.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(stdio_locked)]
+    /// use std::io::{self, Write};
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut handle = io::stdout().into_locked();
+    ///
+    ///     handle.write_all(b"hello world")?;
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn into_locked(self) -> StdoutLock<'static> {
+        self.lock_any()
     }
 }
 
 impl fmt::Debug for Stdout {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Stdout { .. }")
+        f.debug_struct("Stdout").finish_non_exhaustive()
     }
 }
 
 impl Write for Stdout {
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        (&*self).write(buf)
+    }
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        (&*self).write_vectored(bufs)
+    }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        io::Write::is_write_vectored(&&*self)
+    }
+    fn flush(&mut self) -> io::Result<()> {
+        (&*self).flush()
+    }
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        (&*self).write_all(buf)
+    }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        (&*self).write_all_vectored(bufs)
+    }
+    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+        (&*self).write_fmt(args)
+    }
+}
+
+impl Write for &Stdout {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
         self.lock().write(buf)
     }
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         self.lock().write_vectored(bufs)
     }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.lock().is_write_vectored()
+    }
     fn flush(&mut self) -> io::Result<()> {
         self.lock().flush()
     }
     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
         self.lock().write_all(buf)
     }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        self.lock().write_all_vectored(bufs)
+    }
     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
         self.lock().write_fmt(args)
     }
@@ -434,14 +821,24 @@
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         self.inner.borrow_mut().write_vectored(bufs)
     }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.inner.borrow_mut().is_write_vectored()
+    }
     fn flush(&mut self) -> io::Result<()> {
         self.inner.borrow_mut().flush()
     }
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        self.inner.borrow_mut().write_all(buf)
+    }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        self.inner.borrow_mut().write_all_vectored(bufs)
+    }
 }
 
 impl fmt::Debug for StdoutLock<'_> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("StdoutLock { .. }")
+        f.debug_struct("StdoutLock").finish_non_exhaustive()
     }
 }
 
@@ -449,24 +846,27 @@
 ///
 /// For more information, see the [`io::stderr`] method.
 ///
-/// [`io::stderr`]: fn.stderr.html
+/// [`io::stderr`]: stderr
 ///
 /// ### Note: Windows Portability Consideration
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
 /// an error.
 pub struct Stderr {
-    inner: Arc<SgxReentrantMutex<RefCell<Maybe<StderrRaw>>>>,
+    inner: Pin<&'static ReentrantMutex<RefCell<StderrRaw>>>,
 }
 
-/// A locked reference to the `Stderr` handle.
+/// A locked reference to the [`Stderr`] handle.
 ///
-/// This handle implements the `Write` trait and is constructed via
-/// the [`Stderr::lock`] method.
+/// This handle implements the [`Write`] trait and is constructed via
+/// the [`Stderr::lock`] method. See its documentation for more.
 ///
-/// [`Stderr::lock`]: struct.Stderr.html#method.lock
+/// ### Note: Windows Portability Consideration
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
 pub struct StderrLock<'a> {
-    inner: SgxReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
+    inner: ReentrantMutexGuard<'a, RefCell<StderrRaw>>,
 }
 
 /// Constructs a new handle to the standard error of the current process.
@@ -477,53 +877,187 @@
 /// When operating in a console, the Windows implementation of this stream does not support
 /// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
 /// an error.
+///
+/// # Examples
+///
+/// Using implicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     io::stderr().write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
+///
+/// Using explicit synchronization:
+///
+/// ```no_run
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     let stderr = io::stderr();
+///     let mut handle = stderr.lock();
+///
+///     handle.write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
 pub fn stderr() -> Stderr {
-    static INSTANCE: LazyStatic<SgxReentrantMutex<RefCell<Maybe<StderrRaw>>>> = LazyStatic::new();
-    return Stderr {
-        inner: unsafe { INSTANCE.get(stderr_init).expect("cannot access stderr during shutdown") },
-    };
+    // Note that unlike `stdout()` we don't use `at_exit` here to register a
+    // destructor. Stderr is not buffered , so there's no need to run a
+    // destructor for flushing the buffer
+    static INSTANCE: SyncOnceCell<ReentrantMutex<RefCell<StderrRaw>>> = SyncOnceCell::new();
 
-    fn stderr_init() -> Arc<SgxReentrantMutex<RefCell<Maybe<StderrRaw>>>> {
-        // This must not reentrantly access `INSTANCE`
-        let stderr = match stderr_raw() {
-            Ok(stderr) => Maybe::Real(stderr),
-            _ => Maybe::Fake,
-        };
-        Arc::new(SgxReentrantMutex::new(RefCell::new(stderr)))
+    Stderr {
+        inner: Pin::static_ref(&INSTANCE).get_or_init_pin(
+            || ReentrantMutex::new(RefCell::new(stderr_raw())),
+            |_| (),
+        ),
     }
 }
 
+/// Constructs a new locked handle to the standard error of the current
+/// process.
+///
+/// This handle is not buffered.
+///
+/// ### Note: Windows Portability Consideration
+/// When operating in a console, the Windows implementation of this stream does not support
+/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
+/// an error.
+///
+/// # Example
+///
+/// ```no_run
+/// #![feature(stdio_locked)]
+/// use std::io::{self, Write};
+///
+/// fn main() -> io::Result<()> {
+///     let mut handle = io::stderr_locked();
+///
+///     handle.write_all(b"hello world")?;
+///
+///     Ok(())
+/// }
+/// ```
+pub fn stderr_locked() -> StderrLock<'static> {
+    stderr().into_locked()
+}
+
 impl Stderr {
     /// Locks this handle to the standard error stream, returning a writable
     /// guard.
     ///
     /// The lock is released when the returned lock goes out of scope. The
-    /// returned guard also implements the `Write` trait for writing data.
+    /// returned guard also implements the [`Write`] trait for writing data.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::io::{self, Write};
+    ///
+    /// fn foo() -> io::Result<()> {
+    ///     let stderr = io::stderr();
+    ///     let mut handle = stderr.lock();
+    ///
+    ///     handle.write_all(b"hello world")?;
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn lock(&self) -> StderrLock<'_> {
-        StderrLock { inner: self.inner.lock().unwrap_or_else(|e| e.into_inner()) }
+        self.lock_any()
+    }
+
+    // Locks this handle with any lifetime. This depends on the
+    // implementation detail that the underlying `ReentrantMutex` is
+    // static.
+    fn lock_any<'a>(&self) -> StderrLock<'a> {
+        StderrLock { inner: self.inner.lock() }
+    }
+
+    /// Locks and consumes this handle to the standard error stream,
+    /// returning a writable guard.
+    ///
+    /// The lock is released when the returned guard goes out of scope. The
+    /// returned guard also implements the [`Write`] trait for writing
+    /// data.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(stdio_locked)]
+    /// use std::io::{self, Write};
+    ///
+    /// fn foo() -> io::Result<()> {
+    ///     let stderr = io::stderr();
+    ///     let mut handle = stderr.into_locked();
+    ///
+    ///     handle.write_all(b"hello world")?;
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn into_locked(self) -> StderrLock<'static> {
+        self.lock_any()
     }
 }
 
 impl fmt::Debug for Stderr {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Stderr { .. }")
+        f.debug_struct("Stderr").finish_non_exhaustive()
     }
 }
 
 impl Write for Stderr {
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        (&*self).write(buf)
+    }
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        (&*self).write_vectored(bufs)
+    }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        io::Write::is_write_vectored(&&*self)
+    }
+    fn flush(&mut self) -> io::Result<()> {
+        (&*self).flush()
+    }
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        (&*self).write_all(buf)
+    }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        (&*self).write_all_vectored(bufs)
+    }
+    fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
+        (&*self).write_fmt(args)
+    }
+}
+
+impl Write for &Stderr {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
         self.lock().write(buf)
     }
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         self.lock().write_vectored(bufs)
     }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.lock().is_write_vectored()
+    }
     fn flush(&mut self) -> io::Result<()> {
         self.lock().flush()
     }
     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
         self.lock().write_all(buf)
     }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        self.lock().write_all_vectored(bufs)
+    }
     fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> {
         self.lock().write_fmt(args)
     }
@@ -536,18 +1070,28 @@
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         self.inner.borrow_mut().write_vectored(bufs)
     }
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.inner.borrow_mut().is_write_vectored()
+    }
     fn flush(&mut self) -> io::Result<()> {
         self.inner.borrow_mut().flush()
     }
+    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
+        self.inner.borrow_mut().write_all(buf)
+    }
+    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
+        self.inner.borrow_mut().write_all_vectored(bufs)
+    }
 }
 
 impl fmt::Debug for StderrLock<'_> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("StderrLock { .. }")
+        f.debug_struct("StderrLock").finish_non_exhaustive()
     }
 }
 
-/// Write `args` to output stream `local_s` if possible, `global_s`
+/// Write `args` to the capture buffer if enabled and possible, or `global_s`
 /// otherwise. `label` identifies the stream in a panic message.
 ///
 /// This function is used to print error messages, so it takes extra
@@ -557,15 +1101,11 @@
 /// thread, it will just fall back to the global stream.
 ///
 /// However, if the actual I/O causes an error, this function does panic.
-fn print_to<T>(
-    args: fmt::Arguments<'_>,
-    global_s: fn() -> T,
-    label: &str,
-) where
+fn print_to<T>(args: fmt::Arguments<'_>, global_s: fn() -> T, label: &str)
+where
     T: Write,
 {
-    let result = global_s().write_fmt(args);
-    if let Err(e) = result {
+    if let Err(e) = global_s().write_fmt(args) {
         panic!("failed printing to {}: {}", label, e);
     }
 }
@@ -576,4 +1116,4 @@
 
 pub fn _eprint(args: fmt::Arguments<'_>) {
     print_to(args, stderr, "stderr");
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/io/util.rs b/sgx_tstd/src/io/util.rs
index ca5466b..56ccc4c 100644
--- a/sgx_tstd/src/io/util.rs
+++ b/sgx_tstd/src/io/util.rs
@@ -17,74 +17,37 @@
 
 #![allow(missing_copy_implementations)]
 
-use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write};
-use core::fmt;
-use core::mem::MaybeUninit;
-
-/// Copies the entire contents of a reader into a writer.
-///
-/// This function will continuously read data from `reader` and then
-/// write it into `writer` in a streaming fashion until `reader`
-/// returns EOF.
-///
-/// On success, the total number of bytes that were copied from
-/// `reader` to `writer` is returned.
-///
-/// If you’re wanting to copy the contents of one file to another and you’re
-/// working with filesystem paths, see the [`fs::copy`] function.
-///
-/// [`fs::copy`]: ../fs/fn.copy.html
-///
-/// # Errors
-///
-/// This function will return an error immediately if any call to `read` or
-/// `write` returns an error. All instances of `ErrorKind::Interrupted` are
-/// handled by this function and the underlying operation is retried.
-pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64>
-where
-    R: Read,
-    W: Write,
-{
-    let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
-    // FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized
-    // `MaybeUninit`. Revisit this once we decided whether that is valid or not.
-    // This is still technically undefined behavior due to creating a reference
-    // to uninitialized data, but within libstd we can rely on more guarantees
-    // than if this code were in an external lib.
-    unsafe {
-        reader.initializer().initialize(buf.assume_init_mut());
-    }
-
-    let mut written = 0;
-    loop {
-        let len = match reader.read(unsafe { buf.assume_init_mut() }) {
-            Ok(0) => return Ok(written),
-            Ok(len) => len,
-            Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
-            Err(e) => return Err(e),
-        };
-        writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
-        written += len as u64;
-    }
-}
+use crate::fmt;
+use crate::io::{
+    self, BufRead, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
+};
 
 /// A reader which is always at EOF.
 ///
-/// This struct is generally created by calling [`empty`]. Please see
-/// the documentation of [`empty()`][`empty`] for more details.
-///
-/// [`empty`]: fn.empty.html
-pub struct Empty {
-    _priv: (),
-}
+/// This struct is generally created by calling [`empty()`]. Please see
+/// the documentation of [`empty()`] for more details.
+#[non_exhaustive]
+#[derive(Copy, Clone, Default)]
+pub struct Empty;
 
 /// Constructs a new handle to an empty reader.
 ///
 /// All reads from the returned reader will return [`Ok`]`(0)`.
 ///
-/// [`Ok`]: ../result/enum.Result.html#variant.Ok
+/// # Examples
 ///
-pub fn empty() -> Empty { Empty { _priv: () } }
+/// A slightly sad example of not reading anything into a buffer:
+///
+/// ```
+/// use std::io::{self, Read};
+///
+/// let mut buffer = String::new();
+/// io::empty().read_to_string(&mut buffer).unwrap();
+/// assert!(buffer.is_empty());
+/// ```
+pub const fn empty() -> Empty {
+    Empty
+}
 
 impl Read for Empty {
     #[inline]
@@ -107,18 +70,37 @@
     fn consume(&mut self, _n: usize) {}
 }
 
+impl Seek for Empty {
+    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
+        Ok(0)
+    }
+
+    fn stream_len(&mut self) -> io::Result<u64> {
+        Ok(0)
+    }
+
+    fn stream_position(&mut self) -> io::Result<u64> {
+        Ok(0)
+    }
+}
+
 impl fmt::Debug for Empty {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Empty { .. }")
+        f.debug_struct("Empty").finish_non_exhaustive()
+    }
+}
+
+impl SizeHint for Empty {
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        Some(0)
     }
 }
 
 /// A reader which yields one byte over and over and over and over and over and...
 ///
-/// This struct is generally created by calling [`repeat`][repeat]. Please
-/// see the documentation of `repeat()` for more details.
-///
-/// [repeat]: fn.repeat.html
+/// This struct is generally created by calling [`repeat()`]. Please
+/// see the documentation of [`repeat()`] for more details.
 pub struct Repeat {
     byte: u8,
 }
@@ -127,7 +109,17 @@
 ///
 /// All reads from this reader will succeed by filling the specified buffer with
 /// the given byte.
-pub fn repeat(byte: u8) -> Repeat {
+///
+/// # Examples
+///
+/// ```
+/// use std::io::{self, Read};
+///
+/// let mut buffer = [0; 3];
+/// io::repeat(0b101).read_exact(&mut buffer).unwrap();
+/// assert_eq!(buffer, [0b101, 0b101, 0b101]);
+/// ```
+pub const fn repeat(byte: u8) -> Repeat {
     Repeat { byte }
 }
 
@@ -150,33 +142,60 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
         Initializer::nop()
     }
 }
 
+impl SizeHint for Repeat {
+    #[inline]
+    fn lower_bound(&self) -> usize {
+        usize::MAX
+    }
+
+    #[inline]
+    fn upper_bound(&self) -> Option<usize> {
+        None
+    }
+}
+
 impl fmt::Debug for Repeat {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Repeat { .. }")
+        f.debug_struct("Repeat").finish_non_exhaustive()
     }
 }
 
 /// A writer which will move data into the void.
 ///
-/// This struct is generally created by calling [`sink`][sink]. Please
-/// see the documentation of `sink()` for more details.
-///
-/// [sink]: fn.sink.html
-pub struct Sink {
-    _priv: (),
-}
+/// This struct is generally created by calling [`sink`]. Please
+/// see the documentation of [`sink()`] for more details.
+#[non_exhaustive]
+#[derive(Copy, Clone, Default)]
+pub struct Sink;
 
 /// Creates an instance of a writer which will successfully consume all data.
 ///
-/// All calls to `write` on the returned instance will return `Ok(buf.len())`
+/// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
 /// and the contents of the buffer will not be inspected.
-pub fn sink() -> Sink {
-    Sink { _priv: () }
+///
+/// [`write`]: Write::write
+///
+/// # Examples
+///
+/// ```rust
+/// use std::io::{self, Write};
+///
+/// let buffer = vec![1, 2, 3, 5, 8];
+/// let num_bytes = io::sink().write(&buffer).unwrap();
+/// assert_eq!(num_bytes, 5);
+/// ```
+pub const fn sink() -> Sink {
+    Sink
 }
 
 impl Write for Sink {
@@ -192,6 +211,34 @@
     }
 
     #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
+    fn flush(&mut self) -> io::Result<()> {
+        Ok(())
+    }
+}
+
+impl Write for &Sink {
+    #[inline]
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        Ok(buf.len())
+    }
+
+    #[inline]
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        let total_len = bufs.iter().map(|b| b.len()).sum();
+        Ok(total_len)
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
+    }
+
+    #[inline]
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -199,6 +246,6 @@
 
 impl fmt::Debug for Sink {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Sink { .. }")
+        f.debug_struct("Sink").finish_non_exhaustive()
     }
-}
+}
\ No newline at end of file
diff --git a/sgx_tstd/src/lazy.rs b/sgx_tstd/src/lazy.rs
index 36171de..4e14cf9 100644
--- a/sgx_tstd/src/lazy.rs
+++ b/sgx_tstd/src/lazy.rs
@@ -17,14 +17,16 @@
 
 //! Lazy values and one-time initialization of static data.
 
-use core::cell::{Cell, UnsafeCell};
-use core::fmt;
-use core::marker::PhantomData;
-use core::mem::MaybeUninit;
-use core::ops::{Deref, Drop};
-use core::pin::Pin;
-use crate::panic::{RefUnwindSafe, UnwindSafe};
-use crate::sync::Once;
+use crate::{
+    cell::{Cell, UnsafeCell},
+    fmt,
+    marker::PhantomData,
+    mem::MaybeUninit,
+    ops::{Deref, Drop},
+    panic::{RefUnwindSafe, UnwindSafe},
+    pin::Pin,
+    sync::Once,
+};
 
 #[doc(inline)]
 pub use core::lazy::*;
@@ -91,7 +93,19 @@
 impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for SyncOnceCell<T> {}
 impl<T: UnwindSafe> UnwindSafe for SyncOnceCell<T> {}
 
-impl<T> Default for SyncOnceCell<T> {
+impl<T> const Default for SyncOnceCell<T> {
+    /// Creates a new empty cell.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// #![feature(once_cell)]
+    ///
+    /// use std::lazy::SyncOnceCell;
+    ///
+    /// assert_eq!(SyncOnceCell::<()>::new(), SyncOnceCell::default());
+    ///
+    /// ```
     fn default() -> SyncOnceCell<T> {
         SyncOnceCell::new()
     }
@@ -120,6 +134,23 @@
 }
 
 impl<T> From<T> for SyncOnceCell<T> {
+    /// Create a new cell with its contents set to `value`.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// #![feature(once_cell)]
+    ///
+    /// use std::lazy::SyncOnceCell;
+    ///
+    /// # fn main() -> Result<(), i32> {
+    /// let a = SyncOnceCell::from(3);
+    /// let b = SyncOnceCell::new();
+    /// b.set(3)?;
+    /// assert_eq!(a, b);
+    /// Ok(())
+    /// # }
+    /// ```
     fn from(value: T) -> Self {
         let cell = Self::new();
         match cell.set(value) {
@@ -174,7 +205,10 @@
 
     /// Sets the contents of this cell to `value`.
     ///
-    /// Returns `Ok(())` if the cell's value was updated.
+    /// May block if another thread is currently attempting to initialize the cell. The cell is
+    /// guaranteed to contain a value when set returns, though not necessarily the one provided.
+    ///
+    /// Returns `Ok(())` if the cell's value was set by this call.
     ///
     /// # Examples
     ///
@@ -389,6 +423,7 @@
     /// assert_eq!(cell.take(), Some("hello".to_string()));
     /// assert_eq!(cell.get(), None);
     /// ```
+    #[allow(clippy::unnecessary_mut_passed)]
     pub fn take(&mut self) -> Option<T> {
         if self.is_initialized() {
             self.once = Once::new();
@@ -433,13 +468,17 @@
         res
     }
 
-    /// Safety: The value must be initialized
+    /// # Safety
+    ///
+    /// The value must be initialized
     unsafe fn get_unchecked(&self) -> &T {
         debug_assert!(self.is_initialized());
         (&*self.value.get()).assume_init_ref()
     }
 
-    /// Safety: The value must be initialized
+    /// # Safety
+    ///
+    /// The value must be initialized
     unsafe fn get_unchecked_mut(&mut self) -> &mut T {
         debug_assert!(self.is_initialized());
         (&mut *self.value.get()).assume_init_mut()
@@ -449,7 +488,7 @@
 unsafe impl<#[may_dangle] T> Drop for SyncOnceCell<T> {
     fn drop(&mut self) {
         if self.is_initialized() {
-            // Safety: The cell is initialized and being dropped, so it can't
+            // SAFETY: The cell is initialized and being dropped, so it can't
             // be accessed again. We also don't touch the `T` other than
             // dropping it, which validates our usage of #[may_dangle].
             unsafe { (&mut *self.value.get()).assume_init_drop() };
@@ -499,7 +538,7 @@
 
 impl<T: fmt::Debug, F> fmt::Debug for SyncLazy<T, F> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
+        f.debug_struct("Lazy").field("cell", &self.cell).finish_non_exhaustive()
     }
 }
 
diff --git a/sgx_tstd/src/lib.rs b/sgx_tstd/src/lib.rs
index aa45185..0f14bda 100644
--- a/sgx_tstd/src/lib.rs
+++ b/sgx_tstd/src/lib.rs
@@ -17,9 +17,8 @@
 
 //! # The Rust SGX SDK Standard Library
 //!
-//! The Rust SGX standard library (previously named as `sgx_tstdc`) is
-//! the foundation of portable Rust SGX SDK, a
-//! set of minimal and battle-tested shared abstractions for the Rust SGX
+//! The Rust SGX standard library is the foundation of portable Rust SGX SDK,
+//! a set of minimal and battle-tested shared abstractions for the Rust SGX
 //! ecosystem. Similar to Rust's libstd, it offers core types, like [`Vec<T>`] and
 //! [`Option<T>`], library-defined [operations on language
 //! primitives](#primitives), [standard macros](#macros), [I/O] and
@@ -31,7 +30,10 @@
 //! `std`, as in [`use std::env`], or in expressions through the absolute path
 //! `::std`, as in [`::std::env::args`].
 
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
 
 #![no_std]
 #![needs_panic_runtime]
@@ -39,73 +41,87 @@
 #![allow(unused_must_use)]
 #![allow(dead_code)]
 #![allow(deprecated)]
+#![allow(incomplete_features)]
 #![allow(unused_assignments)]
-#![allow(stable_features)]
+
+#![allow(clippy::declare_interior_mutable_const)]
+#![allow(clippy::len_without_is_empty)]
+#![allow(clippy::mem_replace_with_default)]
+#![allow(clippy::missing_safety_doc)]
+#![allow(clippy::new_without_default)]
+#![allow(clippy::transmute_ptr_to_ptr)]
+#![allow(clippy::wrong_self_convention)]
+
+#![feature(rustc_allow_const_fn_unstable)]
 
 #![feature(alloc_error_handler)]
 #![feature(allocator_api)]
-#![feature(allow_internal_unsafe)]
 #![feature(allocator_internals)]
+#![feature(allow_internal_unsafe)]
 #![feature(allow_internal_unstable)]
 #![feature(array_error_internals)]
 #![feature(asm)]
-#![cfg_attr(enable_auto_traits, feature(auto_traits))]
-#![cfg_attr(not(enable_auto_traits), feature(optin_builtin_traits))]
+#![feature(assert_matches)]
+#![feature(async_stream)]
+#![feature(bench_black_box)]
 #![feature(box_syntax)]
 #![feature(c_variadic)]
+#![feature(c_unwind)]
 #![feature(cfg_accessible)]
+#![feature(cfg_eval)]
 #![feature(cfg_target_has_atomic)]
 #![feature(char_error_internals)]
+#![feature(char_internals)]
 #![feature(concat_idents)]
-#![feature(const_fn)]
 #![feature(const_fn_fn_ptr_basics)]
+#![feature(const_format_args)]
+#![feature(const_raw_ptr_deref)]
+#![feature(const_trait_impl)]
 #![feature(core_intrinsics)]
 #![feature(custom_test_frameworks)]
+#![feature(decl_macro)]
 #![feature(dropck_eyepatch)]
+#![feature(duration_checked_float)]
+#![feature(edition_panic)]
 #![feature(extend_one)]
 #![feature(fn_traits)]
-#![feature(generator_trait)]
 #![feature(format_args_nl)]
 #![feature(gen_future)]
 #![feature(global_asm)]
 #![feature(hashmap_internals)]
-#![feature(int_error_internals)]
 #![feature(into_future)]
+#![feature(int_error_internals)]
+#![feature(iter_zip)]
 #![feature(lang_items)]
+#![feature(linked_list_remove)]
 #![feature(llvm_asm)]
 #![feature(log_syntax)]
+#![feature(map_try_insert)]
 #![feature(maybe_uninit_extra)]
-#![feature(maybe_uninit_ref)]
-#![feature(never_type)]
+#![feature(maybe_uninit_slice)]
 #![feature(needs_panic_runtime)]
+#![feature(negative_impls)]
+#![feature(never_type)]
+#![feature(new_uninit)]
 #![feature(once_cell)]
+#![feature(panic_info_message)]
+#![feature(panic_internals)]
 #![feature(panic_unwind)]
-#![cfg_attr(enable_prelude_version, feature(prelude_2021))]
+#![feature(pin_static_ref)]
 #![feature(prelude_import)]
 #![feature(ptr_internals)]
-#![feature(raw)]
-#![feature(shrink_to)]
 #![feature(rustc_attrs)]
-#![feature(slice_concat_ext)]
+#![feature(specialization)]
+#![feature(std_internals)]
 #![feature(str_internals)]
+#![feature(test)]
 #![feature(thread_local)]
 #![feature(toowned_clone_into)]
 #![feature(trace_macros)]
 #![feature(try_reserve)]
+#![feature(try_reserve_kind)]
 #![feature(unboxed_closures)]
-#![feature(untagged_unions)]
-#![feature(unwind_attributes)]
-#![feature(wake_trait)]
-#![feature(libc)]
-#![feature(panic_internals)]
-#![feature(std_internals)]
-#![feature(panic_info_message)]
-#![feature(unicode_internals)]
-#![feature(alloc_layout_extra)]
-#![feature(linked_list_remove)]
-#![feature(int_error_matching)]
-#![feature(drain_filter)]
-#![feature(negative_impls)]
+#![feature(vec_spare_capacity)]
 #![default_lib_allocator]
 
 // Explicitly import the prelude. The compiler uses this same unstable attribute
@@ -152,7 +168,7 @@
 // The Rust prelude
 pub mod prelude;
 
-// Public module declarations and reexports
+// Public module declarations and re-exports
 pub use alloc_crate::borrow;
 pub use alloc_crate::boxed;
 pub use alloc_crate::fmt;
@@ -170,6 +186,7 @@
 pub use core::cmp;
 pub use core::convert;
 pub use core::default;
+pub use core::future;
 pub use core::hash;
 pub use core::hint;
 pub use core::i128;
@@ -186,8 +203,8 @@
 pub use core::option;
 pub use core::pin;
 pub use core::ptr;
-pub use core::raw;
 pub use core::result;
+pub use core::stream;
 pub use core::u128;
 pub use core::u16;
 pub use core::u32;
@@ -223,6 +240,7 @@
 
 pub mod task {
     //! Types and Traits for working with asynchronous tasks.
+
     #[doc(inline)]
     pub use core::task::*;
 
@@ -230,8 +248,6 @@
     pub use alloc_crate::task::*;
 }
 
-pub mod future;
-
 // Platform-abstraction modules
 #[macro_use]
 mod sys_common;
@@ -240,9 +256,9 @@
 pub mod alloc;
 
 // Private support modules
-mod panicking;
 mod cpuid;
-mod memchr;
+mod panicking;
+
 #[cfg(not(feature = "untrusted_fs"))]
 mod fs;
 
@@ -259,53 +275,26 @@
     rsgx_thread_equal
 };
 
-#[cfg(debug_assertions)]
-pub mod debug;
-
 // Re-export macros defined in libcore.
 #[allow(deprecated, deprecated_in_future)]
 pub use core::{
-    // Stable
-    assert_eq,
-    assert_ne,
-    debug_assert,
-    debug_assert_eq,
-    debug_assert_ne,
-    // Unstable
-    matches,
-    r#try,
-    todo,
-    unimplemented,
-    unreachable,
-    write,
-    writeln,
+    assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, matches, r#try, todo,
+    unimplemented, unreachable, write, writeln,
 };
 
 // Re-export built-in macros defined through libcore.
+#[allow(deprecated)]
 pub use core::{
-    // Unstable
-    asm,
-    // Stable
-    assert,
-    cfg,
-    column,
-    compile_error,
-    concat,
-    concat_idents,
-    env,
-    file,
-    format_args,
-    format_args_nl,
-    global_asm,
-    include,
-    include_bytes,
-    include_str,
-    line,
-    log_syntax,
-    module_path,
-    option_env,
-    stringify,
-    trace_macros,
+    assert, assert_matches, cfg, column, compile_error, concat, concat_idents, const_format_args,
+    env, file, format_args, format_args_nl, include, include_bytes, include_str, line, llvm_asm,
+    log_syntax, module_path, option_env, stringify, trace_macros,
 };
 
 pub use core::primitive;
+
+mod sealed {
+    /// This trait being unreachable from outside the crate
+    /// prevents outside implementations of our extension traits.
+    /// This allows adding more trait methods in the future.
+    pub trait Sealed {}
+}
diff --git a/sgx_tstd/src/macros.rs b/sgx_tstd/src/macros.rs
index e45457c..e992568 100644
--- a/sgx_tstd/src/macros.rs
+++ b/sgx_tstd/src/macros.rs
@@ -17,48 +17,20 @@
 
 //! Standard library macros
 //!
-//! This modules contains a set of macros which are exported from the standard
+//! This module contains a set of macros which are exported from the standard
 //! library. Each macro is available for use when linking against the standard
 //! library.
 
-/// Panics the current thread.
-///
-/// This allows a program to terminate immediately and provide feedback
-/// to the caller of the program. `panic!` should be used when a program reaches
-/// an unrecoverable state.
-///
-/// This macro is the perfect way to assert conditions in example code and in
-/// tests. `panic!` is closely tied with the `unwrap` method of both [`Option`]
-/// and [`Result`][runwrap] enums. Both implementations call `panic!` when they are set
-/// to None or Err variants.
-///
-/// This macro is used to inject panic into a Rust thread, causing the thread to
-/// panic entirely. Each thread's panic can be reaped as the `Box<Any>` type,
-/// and the single-argument form of the `panic!` macro will be the value which
-/// is transmitted.
-///
-/// [`Result`] enum is often a better solution for recovering from errors than
-/// using the `panic!` macro. This macro should be used to avoid proceeding using
-/// incorrect values, such as from external sources. Detailed information about
-/// error handling is found in the [book].
-///
-/// The multi-argument form of this macro panics with a string and has the
-/// [`format!`] syntax for building a string.
-///
-/// # Current implementation
-///
-/// If the main thread panics it will terminate all your threads and end your
-/// program with code `101`.
-///
 #[macro_export]
-#[allow_internal_unstable(libstd_sys_internals)]
+#[rustc_builtin_macro(std_panic)]
+#[allow_internal_unstable(edition_panic)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_macro")]
 macro_rules! panic {
-    () => ({ $crate::panic!("explicit panic") });
-    ($msg:expr) => ({ $crate::rt::begin_panic($msg) });
-    ($msg:expr,) => ({ $crate::panic!($msg) });
-    ($fmt:expr, $($arg:tt)+) => ({
-        $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
-    });
+    // Expands to either `$crate::panic::panic_2015` or `$crate::panic::panic_2021`
+    // depending on the edition of the caller.
+    ($($arg:tt)*) => {
+        /* compiler built-in */
+    };
 }
 
 /// Prints to the standard output.
@@ -73,14 +45,31 @@
 /// Use `print!` only for the primary output of your program. Use
 /// [`eprint!`] instead to print error and progress messages.
 ///
-/// [`println!`]: ../std/macro.println.html
-/// [flush]: ../std/io/trait.Write.html#tymethod.flush
-/// [`eprint!`]: ../std/macro.eprint.html
+/// [flush]: crate::io::Write::flush
 ///
 /// # Panics
 ///
 /// Panics if writing to `io::stdout()` fails.
-#[cfg(feature = "stdio")]
+///
+/// # Examples
+///
+/// ```
+/// use std::io::{self, Write};
+///
+/// print!("this ");
+/// print!("will ");
+/// print!("be ");
+/// print!("on ");
+/// print!("the ");
+/// print!("same ");
+/// print!("line ");
+///
+/// io::stdout().flush().unwrap();
+///
+/// print!("this string has a newline, why not choose println! instead?\n");
+///
+/// io::stdout().flush().unwrap();
+/// ```
 #[macro_export]
 #[allow_internal_unstable(print_internals)]
 macro_rules! print {
@@ -98,13 +87,21 @@
 /// Use `println!` only for the primary output of your program. Use
 /// [`eprintln!`] instead to print error and progress messages.
 ///
-/// [`format!`]: ../std/macro.format.html
-/// [`std::fmt`]: ../std/fmt/index.html
-/// [`eprintln!`]: ../std/macro.eprintln.html
+/// [`std::fmt`]: crate::fmt
+///
 /// # Panics
 ///
-/// Panics if writing to `io::stdout` fails.
-#[cfg(feature = "stdio")]
+/// Panics if writing to [`io::stdout`] fails.
+///
+/// [`io::stdout`]: crate::io::stdout
+///
+/// # Examples
+///
+/// ```
+/// println!(); // prints just a newline
+/// println!("hello there!");
+/// println!("format {} arguments", "some");
+/// ```
 #[macro_export]
 #[allow_internal_unstable(print_internals, format_args_nl)]
 macro_rules! println {
@@ -125,19 +122,24 @@
 /// Prints to the standard error.
 ///
 /// Equivalent to the [`print!`] macro, except that output goes to
-/// [`io::stderr`] instead of `io::stdout`. See [`print!`] for
+/// [`io::stderr`] instead of [`io::stdout`]. See [`print!`] for
 /// example usage.
 ///
 /// Use `eprint!` only for error and progress messages. Use `print!`
 /// instead for the primary output of your program.
 ///
-/// [`io::stderr`]: ../std/io/struct.Stderr.html
-/// [`print!`]: ../std/macro.print.html
+/// [`io::stderr`]: crate::io::stderr
+/// [`io::stdout`]: crate::io::stdout
 ///
 /// # Panics
 ///
 /// Panics if writing to `io::stderr` fails.
-#[cfg(feature = "stdio")]
+///
+/// # Examples
+///
+/// ```
+/// eprint!("Error: Could not complete task");
+/// ```
 #[macro_export]
 #[allow_internal_unstable(print_internals)]
 macro_rules! eprint {
@@ -147,19 +149,24 @@
 /// Prints to the standard error, with a newline.
 ///
 /// Equivalent to the [`println!`] macro, except that output goes to
-/// [`io::stderr`] instead of `io::stdout`. See [`println!`] for
+/// [`io::stderr`] instead of [`io::stdout`]. See [`println!`] for
 /// example usage.
 ///
 /// Use `eprintln!` only for error and progress messages. Use `println!`
 /// instead for the primary output of your program.
 ///
-/// [`io::stderr`]: ../std/io/struct.Stderr.html
-/// [`println!`]: ../std/macro.println.html
+/// [`io::stderr`]: crate::io::stderr
+/// [`io::stdout`]: crate::io::stdout
 ///
 /// # Panics
 ///
 /// Panics if writing to `io::stderr` fails.
-#[cfg(feature = "stdio")]
+///
+/// # Examples
+///
+/// ```
+/// eprintln!("Error: Could not complete task");
+/// ```
 #[macro_export]
 #[allow_internal_unstable(print_internals, format_args_nl)]
 macro_rules! eprintln {
@@ -177,13 +184,140 @@
 #[macro_export]
 macro_rules! eprintln { ($($arg:tt)*) => ({}) }
 
-
+/// Prints and returns the value of a given expression for quick and dirty
+/// debugging.
+///
+/// An example:
+///
+/// ```rust
+/// let a = 2;
+/// let b = dbg!(a * 2) + 1;
+/// //      ^-- prints: [src/main.rs:2] a * 2 = 4
+/// assert_eq!(b, 5);
+/// ```
+///
+/// The macro works by using the `Debug` implementation of the type of
+/// the given expression to print the value to [stderr] along with the
+/// source location of the macro invocation as well as the source code
+/// of the expression.
+///
+/// Invoking the macro on an expression moves and takes ownership of it
+/// before returning the evaluated expression unchanged. If the type
+/// of the expression does not implement `Copy` and you don't want
+/// to give up ownership, you can instead borrow with `dbg!(&expr)`
+/// for some expression `expr`.
+///
+/// The `dbg!` macro works exactly the same in release builds.
+/// This is useful when debugging issues that only occur in release
+/// builds or when debugging in release mode is significantly faster.
+///
+/// Note that the macro is intended as a debugging tool and therefore you
+/// should avoid having uses of it in version control for long periods
+/// (other than in tests and similar).
+/// Debug output from production code is better done with other facilities
+/// such as the [`debug!`] macro from the [`log`] crate.
+///
+/// # Stability
+///
+/// The exact output printed by this macro should not be relied upon
+/// and is subject to future changes.
+///
+/// # Panics
+///
+/// Panics if writing to `io::stderr` fails.
+///
+/// # Further examples
+///
+/// With a method call:
+///
+/// ```rust
+/// fn foo(n: usize) {
+///     if let Some(_) = dbg!(n.checked_sub(4)) {
+///         // ...
+///     }
+/// }
+///
+/// foo(3)
+/// ```
+///
+/// This prints to [stderr]:
+///
+/// ```text,ignore
+/// [src/main.rs:4] n.checked_sub(4) = None
+/// ```
+///
+/// Naive factorial implementation:
+///
+/// ```rust
+/// fn factorial(n: u32) -> u32 {
+///     if dbg!(n <= 1) {
+///         dbg!(1)
+///     } else {
+///         dbg!(n * factorial(n - 1))
+///     }
+/// }
+///
+/// dbg!(factorial(4));
+/// ```
+///
+/// This prints to [stderr]:
+///
+/// ```text,ignore
+/// [src/main.rs:3] n <= 1 = false
+/// [src/main.rs:3] n <= 1 = false
+/// [src/main.rs:3] n <= 1 = false
+/// [src/main.rs:3] n <= 1 = true
+/// [src/main.rs:4] 1 = 1
+/// [src/main.rs:5] n * factorial(n - 1) = 2
+/// [src/main.rs:5] n * factorial(n - 1) = 6
+/// [src/main.rs:5] n * factorial(n - 1) = 24
+/// [src/main.rs:11] factorial(4) = 24
+/// ```
+///
+/// The `dbg!(..)` macro moves the input:
+///
+/// ```compile_fail
+/// /// A wrapper around `usize` which importantly is not Copyable.
+/// #[derive(Debug)]
+/// struct NoCopy(usize);
+///
+/// let a = NoCopy(42);
+/// let _ = dbg!(a); // <-- `a` is moved here.
+/// let _ = dbg!(a); // <-- `a` is moved again; error!
+/// ```
+///
+/// You can also use `dbg!()` without a value to just print the
+/// file and line whenever it's reached.
+///
+/// Finally, if you want to `dbg!(..)` multiple values, it will treat them as
+/// a tuple (and return it, too):
+///
+/// ```
+/// assert_eq!(dbg!(1usize, 2u32), (1, 2));
+/// ```
+///
+/// However, a single argument with a trailing comma will still not be treated
+/// as a tuple, following the convention of ignoring trailing commas in macro
+/// invocations. You can use a 1-tuple directly if you need one:
+///
+/// ```
+/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
+/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple
+/// ```
+///
+/// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
+/// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html
+/// [`log`]: https://crates.io/crates/log
 #[macro_export]
 macro_rules! dbg {
+    // NOTE: We cannot use `concat!` to make a static string as a format argument
+    // of `eprintln!` because `file!` could contain a `{` or
+    // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
+    // will be malformed.
     () => {
-        $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!());
+        $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!())
     };
-    ($val:expr) => {
+    ($val:expr $(,)?) => {
         // Use of `match` here is intentional because it affects the lifetimes
         // of temporaries - https://stackoverflow.com/a/48732525/1063961
         match $val {
@@ -194,8 +328,6 @@
             }
         }
     };
-    // Trailing comma with single argument is ignored
-    ($val:expr,) => { $crate::dbg!($val) };
     ($($val:expr),+ $(,)?) => {
         ($($crate::dbg!($val)),+,)
     };
diff --git a/sgx_tstd/src/memchr.rs b/sgx_tstd/src/memchr.rs
deleted file mode 100644
index 80bb483..0000000
--- a/sgx_tstd/src/memchr.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-//! Original implementation taken from rust-memchr
-
-/// A safe interface to `memchr`.
-///
-/// Returns the index corresponding to the first occurrence of `needle` in
-/// `haystack`, or `None` if one is not found.
-///
-/// memchr reduces to super-optimized machine code at around an order of
-/// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
-/// (See benchmarks.)
-///
-#[inline]
-pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    crate::sys::memchr::memchr(needle, haystack)
-}
-
-/// A safe interface to `memrchr`.
-///
-/// Returns the index corresponding to the last occurrence of `needle` in
-/// `haystack`, or `None` if one is not found.
-///
-#[inline]
-pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
-    crate::sys::memchr::memrchr(needle, haystack)
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/net/addr.rs b/sgx_tstd/src/net/addr.rs
index a4dbc4f..f955dc9 100644
--- a/sgx_tstd/src/net/addr.rs
+++ b/sgx_tstd/src/net/addr.rs
@@ -15,21 +15,23 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc as c;
-use core::fmt;
-use core::hash;
-use core::mem;
-use core::option;
-use core::iter;
+use crate::cmp::Ordering;
 #[cfg(feature = "net")]
-use core::convert::TryInto;
-use alloc_crate::vec;
-use alloc_crate::slice;
-use crate::io;
+use crate::convert::TryInto;
+use crate::fmt;
+use crate::hash;
+use crate::io::{self, Write};
+use crate::iter;
+use crate::mem;
 use crate::net::{htons, ntohs, IpAddr, Ipv4Addr, Ipv6Addr};
-use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::option;
+use crate::slice;
 #[cfg(feature = "net")]
 use crate::sys_common::net::LookupHost;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::vec;
+
+use sgx_libc as c;
 
 /// An internet socket address, either IPv4 or IPv6.
 ///
@@ -40,7 +42,20 @@
 /// The size of a `SocketAddr` instance may vary depending on the target operating
 /// system.
 ///
-#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
+/// [IP address]: IpAddr
+///
+/// # Examples
+///
+/// ```
+/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+///
+/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+///
+/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
+/// assert_eq!(socket.port(), 8080);
+/// assert_eq!(socket.is_ipv4(), true);
+/// ```
+#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
 pub enum SocketAddr {
     /// An IPv4 socket address.
     V4(SocketAddrV4),
@@ -50,35 +65,83 @@
 
 /// An IPv4 socket address.
 ///
-/// IPv4 socket addresses consist of an [IPv4 address] and a 16-bit port number, as
+/// IPv4 socket addresses consist of an [`IPv4` address] and a 16-bit port number, as
 /// stated in [IETF RFC 793].
 ///
 /// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
 ///
+/// The size of a `SocketAddrV4` struct may vary depending on the target operating
+/// system. Do not assume that this type has the same memory layout as the underlying
+/// system representation.
+///
+/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
+/// [`IPv4` address]: Ipv4Addr
+///
+/// # Examples
+///
+/// ```
+/// use std::net::{Ipv4Addr, SocketAddrV4};
+///
+/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+///
+/// assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
+/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
+/// assert_eq!(socket.port(), 8080);
+/// ```
 #[derive(Copy)]
 pub struct SocketAddrV4 {
+    // Do not assume that this struct is implemented as the underlying system representation.
+    // The memory layout is not part of the stable interface that std exposes.
     inner: c::sockaddr_in,
 }
 
 /// An IPv6 socket address.
 ///
-/// IPv6 socket addresses consist of an [Ipv6 address], a 16-bit port number, as well
+/// IPv6 socket addresses consist of an [`IPv6` address], a 16-bit port number, as well
 /// as fields containing the traffic class, the flow label, and a scope identifier
 /// (see [IETF RFC 2553, Section 3.3] for more details).
 ///
 /// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
 ///
 /// The size of a `SocketAddrV6` struct may vary depending on the target operating
-/// system.
+/// system. Do not assume that this type has the same memory layout as the underlying
+/// system representation.
 ///
+/// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
+/// [`IPv6` address]: Ipv6Addr
+///
+/// # Examples
+///
+/// ```
+/// use std::net::{Ipv6Addr, SocketAddrV6};
+///
+/// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+///
+/// assert_eq!("[2001:db8::1]:8080".parse(), Ok(socket));
+/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
+/// assert_eq!(socket.port(), 8080);
+/// ```
 #[derive(Copy)]
 pub struct SocketAddrV6 {
+    // Do not assume that this struct is implemented as the underlying system representation.
+    // The memory layout is not part of the stable interface that std exposes.
     inner: c::sockaddr_in6,
 }
 
 impl SocketAddr {
     /// Creates a new socket address from an [IP address] and a port number.
     ///
+    /// [IP address]: IpAddr
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
+    /// assert_eq!(socket.port(), 8080);
+    /// ```
     pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
         match ip {
             IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
@@ -88,7 +151,15 @@
 
     /// Returns the IP address associated with this socket address.
     ///
-    pub fn ip(&self) -> IpAddr {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
+    /// ```
+    pub const fn ip(&self) -> IpAddr {
         match *self {
             SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
             SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
@@ -97,6 +168,15 @@
 
     /// Changes the IP address associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
+    /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
+    /// ```
     pub fn set_ip(&mut self, new_ip: IpAddr) {
         // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
         match (self, new_ip) {
@@ -108,7 +188,15 @@
 
     /// Returns the port number associated with this socket address.
     ///
-    pub fn port(&self) -> u16 {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// assert_eq!(socket.port(), 8080);
+    /// ```
+    pub const fn port(&self) -> u16 {
         match *self {
             SocketAddr::V4(ref a) => a.port(),
             SocketAddr::V6(ref a) => a.port(),
@@ -117,6 +205,15 @@
 
     /// Changes the port number associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let mut socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// socket.set_port(1025);
+    /// assert_eq!(socket.port(), 1025);
+    /// ```
     pub fn set_port(&mut self, new_port: u16) {
         match *self {
             SocketAddr::V4(ref mut a) => a.set_port(new_port),
@@ -125,29 +222,62 @@
     }
 
     /// Returns [`true`] if the [IP address] in this `SocketAddr` is an
-    /// [IPv4 address], and [`false`] otherwise.
+    /// [`IPv4` address], and [`false`] otherwise.
     ///
-    pub fn is_ipv4(&self) -> bool {
+    /// [IP address]: IpAddr
+    /// [`IPv4` address]: IpAddr::V4
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
+    ///
+    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
+    /// assert_eq!(socket.is_ipv4(), true);
+    /// assert_eq!(socket.is_ipv6(), false);
+    /// ```
+    pub const fn is_ipv4(&self) -> bool {
         matches!(*self, SocketAddr::V4(_))
     }
 
     /// Returns [`true`] if the [IP address] in this `SocketAddr` is an
-    /// [IPv6 address], and [`false`] otherwise.
+    /// [`IPv6` address], and [`false`] otherwise.
     ///
-    pub fn is_ipv6(&self) -> bool {
+    /// [IP address]: IpAddr
+    /// [`IPv6` address]: IpAddr::V6
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv6Addr, SocketAddr};
+    ///
+    /// let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080);
+    /// assert_eq!(socket.is_ipv4(), false);
+    /// assert_eq!(socket.is_ipv6(), true);
+    /// ```
+    pub const fn is_ipv6(&self) -> bool {
         matches!(*self, SocketAddr::V6(_))
     }
 }
 
 impl SocketAddrV4 {
-    /// Creates a new socket address from an [IPv4 address] and a port number.
+    /// Creates a new socket address from an [`IPv4` address] and a port number.
     ///
+    /// [`IPv4` address]: Ipv4Addr
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV4, Ipv4Addr};
+    ///
+    /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+    /// ```
     pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
         SocketAddrV4 {
             inner: c::sockaddr_in {
                 sin_family: c::AF_INET as c::sa_family_t,
                 sin_port: htons(port),
-                sin_addr: *ip.as_inner(),
+                sin_addr: ip.into_inner(),
                 ..unsafe { mem::zeroed() }
             },
         }
@@ -155,39 +285,83 @@
 
     /// Returns the IP address associated with this socket address.
     ///
-    pub fn ip(&self) -> &Ipv4Addr {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV4, Ipv4Addr};
+    ///
+    /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+    /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
+    /// ```
+    pub const fn ip(&self) -> &Ipv4Addr {
+        // SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`.
+        // It is safe to cast from `&in_addr` to `&Ipv4Addr`.
         unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
     }
 
     /// Changes the IP address associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV4, Ipv4Addr};
+    ///
+    /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+    /// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1));
+    /// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1));
+    /// ```
     pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
-        self.inner.sin_addr = *new_ip.as_inner()
+        self.inner.sin_addr = new_ip.into_inner()
     }
 
     /// Returns the port number associated with this socket address.
     ///
-    pub fn port(&self) -> u16 {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV4, Ipv4Addr};
+    ///
+    /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+    /// assert_eq!(socket.port(), 8080);
+    /// ```
+    pub const fn port(&self) -> u16 {
         ntohs(self.inner.sin_port)
     }
 
     /// Changes the port number associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV4, Ipv4Addr};
+    ///
+    /// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
+    /// socket.set_port(4242);
+    /// assert_eq!(socket.port(), 4242);
+    /// ```
     pub fn set_port(&mut self, new_port: u16) {
         self.inner.sin_port = htons(new_port);
     }
 }
 
 impl SocketAddrV6 {
-    /// Creates a new socket address from an [IPv6 address], a 16-bit port number,
+    /// Creates a new socket address from an [`IPv6` address], a 16-bit port number,
     /// and the `flowinfo` and `scope_id` fields.
     ///
     /// For more information on the meaning and layout of the `flowinfo` and `scope_id`
     /// parameters, see [IETF RFC 2553, Section 3.3].
     ///
     /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
-    /// [IPv6 address]: ../../std/net/struct.Ipv6Addr.html
+    /// [`IPv6` address]: Ipv6Addr
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+    /// ```
+    #[allow(clippy::needless_update)]
     pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
         SocketAddrV6 {
             inner: c::sockaddr_in6 {
@@ -203,24 +377,58 @@
 
     /// Returns the IP address associated with this socket address.
     ///
-    pub fn ip(&self) -> &Ipv6Addr {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+    /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
+    /// ```
+    pub const fn ip(&self) -> &Ipv6Addr {
         unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
     }
 
     /// Changes the IP address associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+    /// socket.set_ip(Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
+    /// assert_eq!(socket.ip(), &Ipv6Addr::new(76, 45, 0, 0, 0, 0, 0, 0));
+    /// ```
     pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
         self.inner.sin6_addr = *new_ip.as_inner()
     }
 
     /// Returns the port number associated with this socket address.
     ///
-    pub fn port(&self) -> u16 {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+    /// assert_eq!(socket.port(), 8080);
+    /// ```
+    pub const fn port(&self) -> u16 {
         ntohs(self.inner.sin6_port)
     }
 
     /// Changes the port number associated with this socket address.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0);
+    /// socket.set_port(4242);
+    /// assert_eq!(socket.port(), 4242);
+    /// ```
     pub fn set_port(&mut self, new_port: u16) {
         self.inner.sin6_port = htons(new_port);
     }
@@ -237,16 +445,31 @@
     /// [Section 6]: https://tools.ietf.org/html/rfc2460#section-6
     /// [Section 7]: https://tools.ietf.org/html/rfc2460#section-7
     ///
-    pub fn flowinfo(&self) -> u32 {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
+    /// assert_eq!(socket.flowinfo(), 10);
+    /// ```
+    pub const fn flowinfo(&self) -> u32 {
         self.inner.sin6_flowinfo
     }
 
     /// Changes the flow information associated with this socket address.
     ///
-    /// See the [`flowinfo`] method's documentation for more details.
+    /// See [`SocketAddrV6::flowinfo`]'s documentation for more details.
     ///
-    /// [`flowinfo`]: #method.flowinfo
+    /// # Examples
     ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 10, 0);
+    /// socket.set_flowinfo(56);
+    /// assert_eq!(socket.flowinfo(), 56);
+    /// ```
     pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
         self.inner.sin6_flowinfo = new_flowinfo;
     }
@@ -256,16 +479,33 @@
     /// This information corresponds to the `sin6_scope_id` field in C's `netinet/in.h`,
     /// as specified in [IETF RFC 2553, Section 3.3].
     ///
-    pub fn scope_id(&self) -> u32 {
+    /// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
+    /// assert_eq!(socket.scope_id(), 78);
+    /// ```
+    pub const fn scope_id(&self) -> u32 {
         self.inner.sin6_scope_id
     }
 
     /// Changes the scope ID associated with this socket address.
     ///
-    /// See the [`scope_id`] method's documentation for more details.
+    /// See [`SocketAddrV6::scope_id`]'s documentation for more details.
     ///
-    /// [`scope_id`]: #method.scope_id
+    /// # Examples
     ///
+    /// ```
+    /// use std::net::{SocketAddrV6, Ipv6Addr};
+    ///
+    /// let mut socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 78);
+    /// socket.set_scope_id(42);
+    /// assert_eq!(socket.scope_id(), 42);
+    /// ```
     pub fn set_scope_id(&mut self, new_scope_id: u32) {
         self.inner.sin6_scope_id = new_scope_id;
     }
@@ -300,17 +540,10 @@
 impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
     /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`].
     ///
-    /// This conversion creates a [`SocketAddr::V4`] for a [`IpAddr::V4`]
-    /// and creates a [`SocketAddr::V6`] for a [`IpAddr::V6`].
+    /// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`]
+    /// and creates a [`SocketAddr::V6`] for an [`IpAddr::V6`].
     ///
     /// `u16` is treated as port of the newly created [`SocketAddr`].
-    ///
-    /// [`IpAddr`]: ../../std/net/enum.IpAddr.html
-    /// [`IpAddr::V4`]: ../../std/net/enum.IpAddr.html#variant.V4
-    /// [`IpAddr::V6`]: ../../std/net/enum.IpAddr.html#variant.V6
-    /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
-    /// [`SocketAddr::V4`]: ../../std/net/enum.SocketAddr.html#variant.V4
-    /// [`SocketAddr::V6`]: ../../std/net/enum.SocketAddr.html#variant.V6
     fn from(pieces: (I, u16)) -> SocketAddr {
         SocketAddr::new(pieces.0.into(), pieces.1)
     }
@@ -329,6 +562,18 @@
     }
 }
 
+impl IntoInner<c::sockaddr_in> for SocketAddrV4 {
+    fn into_inner(self) -> c::sockaddr_in {
+        self.inner
+    }
+}
+
+impl IntoInner<c::sockaddr_in6> for SocketAddrV6 {
+    fn into_inner(self) -> c::sockaddr_in6 {
+        self.inner
+    }
+}
+
 impl fmt::Display for SocketAddr {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match *self {
@@ -338,9 +583,34 @@
     }
 }
 
+impl fmt::Debug for SocketAddr {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, fmt)
+    }
+}
+
 impl fmt::Display for SocketAddrV4 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "{}:{}", self.ip(), self.port())
+        // Fast path: if there's no alignment stuff, write to the output buffer
+        // directly
+        if f.precision().is_none() && f.width().is_none() {
+            write!(f, "{}:{}", self.ip(), self.port())
+        } else {
+            const IPV4_SOCKET_BUF_LEN: usize = (3 * 4)  // the segments
+                + 3  // the separators
+                + 1 + 5; // the port
+            let mut buf = [0; IPV4_SOCKET_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
+
+            // Unwrap is fine because writing to a sufficiently-sized
+            // buffer is infallible
+            write!(buf_slice, "{}:{}", self.ip(), self.port()).unwrap();
+            let len = IPV4_SOCKET_BUF_LEN - buf_slice.len();
+
+            // This unsafe is OK because we know what is being written to the buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            f.pad(buf)
+        }
     }
 }
 
@@ -352,7 +622,36 @@
 
 impl fmt::Display for SocketAddrV6 {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(f, "[{}]:{}", self.ip(), self.port())
+        // Fast path: if there's no alignment stuff, write to the output
+        // buffer directly
+        if f.precision().is_none() && f.width().is_none() {
+            match self.scope_id() {
+                0 => write!(f, "[{}]:{}", self.ip(), self.port()),
+                scope_id => write!(f, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
+            }
+        } else {
+            const IPV6_SOCKET_BUF_LEN: usize = (4 * 8)  // The address
+            + 7  // The colon separators
+            + 2  // The brackets
+            + 1 + 10 // The scope id
+            + 1 + 5; // The port
+
+            let mut buf = [0; IPV6_SOCKET_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
+
+            match self.scope_id() {
+                0 => write!(buf_slice, "[{}]:{}", self.ip(), self.port()),
+                scope_id => write!(buf_slice, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
+            }
+            // Unwrap is fine because writing to a sufficiently-sized
+            // buffer is infallible
+            .unwrap();
+            let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();
+
+            // This unsafe is OK because we know what is being written to the buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            f.pad(buf)
+        }
     }
 }
 
@@ -394,6 +693,30 @@
 
 impl Eq for SocketAddrV6 {}
 
+impl PartialOrd for SocketAddrV4 {
+    fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl PartialOrd for SocketAddrV6 {
+    fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for SocketAddrV4 {
+    fn cmp(&self, other: &SocketAddrV4) -> Ordering {
+        self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
+    }
+}
+
+impl Ord for SocketAddrV6 {
+    fn cmp(&self, other: &SocketAddrV6) -> Ordering {
+        self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
+    }
+}
+
 impl hash::Hash for SocketAddrV4 {
     fn hash<H: hash::Hasher>(&self, s: &mut H) {
         (self.inner.sin_port, self.inner.sin_addr.s_addr).hash(s)
@@ -424,9 +747,9 @@
 ///    `(`[`Ipv4Addr`]`, `[`u16`]`)`, `(`[`Ipv6Addr`]`, `[`u16`]`)`:
 ///    [`to_socket_addrs`] constructs a [`SocketAddr`] trivially.
 ///
-///  * `(`[`&str`]`, `[`u16`]`)`: the string should be either a string representation
+///  * `(`[`&str`]`, `[`u16`]`)`: [`&str`] should be either a string representation
 ///    of an [`IpAddr`] address as expected by [`FromStr`] implementation or a host
-///    name.
+///    name. [`u16`] is the port number.
 ///
 ///  * [`&str`]: the string should be either a string representation of a
 ///    [`SocketAddr`] as expected by its [`FromStr`] implementation or a string like
@@ -444,19 +767,82 @@
 /// Addresses returned by the operating system that are not IP addresses are
 /// silently ignored.
 ///
-/// [`FromStr`]: ../../std/str/trait.FromStr.html
-/// [`IpAddr`]: ../../std/net/enum.IpAddr.html
-/// [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
-/// [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
-/// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
-/// [`SocketAddrV4`]: ../../std/net/struct.SocketAddrV4.html
-/// [`SocketAddrV6`]: ../../std/net/struct.SocketAddrV6.html
-/// [`&str`]: ../../std/primitive.str.html
-/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
-/// [`to_socket_addrs`]: #tymethod.to_socket_addrs
-/// [`UdpSocket`]: ../../std/net/struct.UdpSocket.html
-/// [`u16`]: ../../std/primitive.u16.html
+/// [`FromStr`]: crate::str::FromStr
+/// [`&str`]: str
+/// [`TcpStream`]: crate::net::TcpStream
+/// [`to_socket_addrs`]: ToSocketAddrs::to_socket_addrs
+/// [`UdpSocket`]: crate::net::UdpSocket
 ///
+/// # Examples
+///
+/// Creating a [`SocketAddr`] iterator that yields one item:
+///
+/// ```
+/// use std::net::{ToSocketAddrs, SocketAddr};
+///
+/// let addr = SocketAddr::from(([127, 0, 0, 1], 443));
+/// let mut addrs_iter = addr.to_socket_addrs().unwrap();
+///
+/// assert_eq!(Some(addr), addrs_iter.next());
+/// assert!(addrs_iter.next().is_none());
+/// ```
+///
+/// Creating a [`SocketAddr`] iterator from a hostname:
+///
+/// ```no_run
+/// use std::net::{SocketAddr, ToSocketAddrs};
+///
+/// // assuming 'localhost' resolves to 127.0.0.1
+/// let mut addrs_iter = "localhost:443".to_socket_addrs().unwrap();
+/// assert_eq!(addrs_iter.next(), Some(SocketAddr::from(([127, 0, 0, 1], 443))));
+/// assert!(addrs_iter.next().is_none());
+///
+/// // assuming 'foo' does not resolve
+/// assert!("foo:443".to_socket_addrs().is_err());
+/// ```
+///
+/// Creating a [`SocketAddr`] iterator that yields multiple items:
+///
+/// ```
+/// use std::net::{SocketAddr, ToSocketAddrs};
+///
+/// let addr1 = SocketAddr::from(([0, 0, 0, 0], 80));
+/// let addr2 = SocketAddr::from(([127, 0, 0, 1], 443));
+/// let addrs = vec![addr1, addr2];
+///
+/// let mut addrs_iter = (&addrs[..]).to_socket_addrs().unwrap();
+///
+/// assert_eq!(Some(addr1), addrs_iter.next());
+/// assert_eq!(Some(addr2), addrs_iter.next());
+/// assert!(addrs_iter.next().is_none());
+/// ```
+///
+/// Attempting to create a [`SocketAddr`] iterator from an improperly formatted
+/// socket address `&str` (missing the port):
+///
+/// ```
+/// use std::io;
+/// use std::net::ToSocketAddrs;
+///
+/// let err = "127.0.0.1".to_socket_addrs().unwrap_err();
+/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+/// ```
+///
+/// [`TcpStream::connect`] is an example of an function that utilizes
+/// `ToSocketAddrs` as a trait bound on its parameter in order to accept
+/// different types:
+///
+/// ```no_run
+/// use std::net::{TcpStream, Ipv4Addr};
+///
+/// let stream = TcpStream::connect(("127.0.0.1", 443));
+/// // or
+/// let stream = TcpStream::connect("127.0.0.1:443");
+/// // or
+/// let stream = TcpStream::connect((Ipv4Addr::new(127, 0, 0, 1), 443));
+/// ```
+///
+/// [`TcpStream::connect`]: crate::net::TcpStream::connect
 pub trait ToSocketAddrs {
     /// Returned iterator over socket addresses which this type may correspond
     /// to.
@@ -464,7 +850,7 @@
 
     /// Converts this object to an iterator of resolved `SocketAddr`s.
     ///
-    /// The returned iterator may not actually yield any values depending on the
+    /// The returned iterator might not actually yield any values depending on the
     /// outcome of any resolution performed.
     ///
     /// Note that this function may block the current thread while resolution is
@@ -521,6 +907,8 @@
 }
 
 #[cfg(feature = "net")]
+#[allow(clippy::unnecessary_wraps)]
+#[allow(clippy::needless_collect)]
 fn resolve_socket_addr(lh: LookupHost) -> io::Result<vec::IntoIter<SocketAddr>> {
     let p = lh.port();
     let v: Vec<_> = lh
@@ -548,13 +936,20 @@
         }
 
         #[cfg(not(feature = "net"))]
-        let r = Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid socket address"));
+        let r = Err(io::Error::new_const(io::ErrorKind::InvalidInput, &"invalid socket address"));
         #[cfg(feature = "net")]
         let r = resolve_socket_addr((host, port).try_into()?);
         r
     }
 }
 
+impl ToSocketAddrs for (String, u16) {
+    type Iter = vec::IntoIter<SocketAddr>;
+    fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
+        (&*self.0, self.1).to_socket_addrs()
+    }
+}
+
 // accepts strings like 'localhost:12345'
 impl ToSocketAddrs for str {
     type Iter = vec::IntoIter<SocketAddr>;
@@ -565,7 +960,7 @@
         }
 
         #[cfg(not(feature = "net"))]
-        let r = Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid socket address"));
+        let r = Err(io::Error::new_const(io::ErrorKind::InvalidInput, &"invalid socket address"));
         #[cfg(feature = "net")]
         let r = resolve_socket_addr(self.try_into()?);
         r
diff --git a/sgx_tstd/src/net/ip.rs b/sgx_tstd/src/net/ip.rs
index 550ecd8..69f350e 100644
--- a/sgx_tstd/src/net/ip.rs
+++ b/sgx_tstd/src/net/ip.rs
@@ -15,12 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc as c;
-use core::cmp::Ordering;
-use core::fmt;
-use core::hash;
-use crate::io::Write;
-use crate::sys_common::{AsInner, FromInner};
+#![allow(clippy::many_single_char_names)]
+
+use crate::cmp::Ordering;
+use crate::fmt::{self, Write as FmtWrite};
+use crate::hash;
+use crate::io::Write as IoWrite;
+use crate::mem::transmute;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+use sgx_libc as c;
 
 /// An IP address, either IPv4 or IPv6.
 ///
@@ -30,10 +33,21 @@
 /// The size of an `IpAddr` instance may vary depending on the target operating
 /// system.
 ///
-/// [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
-/// [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
+/// # Examples
 ///
-#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
+/// ```
+/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+///
+/// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
+/// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
+///
+/// assert_eq!("127.0.0.1".parse(), Ok(localhost_v4));
+/// assert_eq!("::1".parse(), Ok(localhost_v6));
+///
+/// assert_eq!(localhost_v4.is_ipv6(), false);
+/// assert_eq!(localhost_v4.is_ipv4(), true);
+/// ```
+#[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
 pub enum IpAddr {
     /// An IPv4 address.
     V4(Ipv4Addr),
@@ -52,15 +66,25 @@
 /// system.
 ///
 /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791
-/// [`IpAddr`]: ../../std/net/enum.IpAddr.html
 ///
 /// # Textual representation
 ///
 /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal
 /// notation, divided by `.` (this is called "dot-decimal notation").
+/// Notably, octal numbers and hexadecimal numbers are not allowed per [IETF RFC 6943].
 ///
-/// [`FromStr`]: ../../std/str/trait.FromStr.html
+/// [IETF RFC 6943]: https://tools.ietf.org/html/rfc6943#section-3.1.1
+/// [`FromStr`]: crate::str::FromStr
 ///
+/// # Examples
+///
+/// ```
+/// use std::net::Ipv4Addr;
+///
+/// let localhost = Ipv4Addr::new(127, 0, 0, 1);
+/// assert_eq!("127.0.0.1".parse(), Ok(localhost));
+/// assert_eq!(localhost.is_loopback(), true);
+/// ```
 #[derive(Copy)]
 pub struct Ipv4Addr {
     inner: c::in_addr,
@@ -71,13 +95,58 @@
 /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291].
 /// They are usually represented as eight 16-bit segments.
 ///
-/// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
-///
 /// The size of an `Ipv6Addr` struct may vary depending on the target operating
 /// system.
 ///
 /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
-/// [`IpAddr`]: ../../std/net/enum.IpAddr.html
+///
+/// # Embedding IPv4 Addresses
+///
+/// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses.
+///
+/// To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined:
+/// IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated.
+///
+/// Both types of addresses are not assigned any special meaning by this implementation,
+/// other than what the relevant standards prescribe. This means that an address like `::ffff:127.0.0.1`,
+/// while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only `::1` is.
+/// To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address.
+///
+/// ### IPv4-Compatible IPv6 Addresses
+///
+/// IPv4-compatible IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.1], and have been officially deprecated.
+/// The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows:
+///
+/// ```text
+/// |                80 bits               | 16 |      32 bits        |
+/// +--------------------------------------+--------------------------+
+/// |0000..............................0000|0000|    IPv4 address     |
+/// +--------------------------------------+----+---------------------+
+/// ```
+/// So `::a.b.c.d` would be an IPv4-compatible IPv6 address representing the IPv4 address `a.b.c.d`.
+///
+/// To convert from an IPv4 address to an IPv4-compatible IPv6 address, use [`Ipv4Addr::to_ipv6_compatible`].
+/// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-compatible IPv6 address to the canonical IPv4 address.
+///
+/// [IETF RFC 4291 Section 2.5.5.1]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.1
+///
+/// ### IPv4-Mapped IPv6 Addresses
+///
+/// IPv4-mapped IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.2].
+/// The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows:
+///
+/// ```text
+/// |                80 bits               | 16 |      32 bits        |
+/// +--------------------------------------+--------------------------+
+/// |0000..............................0000|FFFF|    IPv4 address     |
+/// +--------------------------------------+----+---------------------+
+/// ```
+/// So `::ffff:a.b.c.d` would be an IPv4-mapped IPv6 address representing the IPv4 address `a.b.c.d`.
+///
+/// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`].
+/// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address.
+///
+/// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2
 ///
 /// # Textual representation
 ///
@@ -86,37 +155,93 @@
 /// notation, and segments are separated by `:`. For more information, see
 /// [IETF RFC 5952].
 ///
-/// [`FromStr`]: ../../std/str/trait.FromStr.html
+/// [`FromStr`]: crate::str::FromStr
 /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952
 ///
+/// # Examples
+///
+/// ```
+/// use std::net::Ipv6Addr;
+///
+/// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
+/// assert_eq!("::1".parse(), Ok(localhost));
+/// assert_eq!(localhost.is_loopback(), true);
+/// ```
 #[derive(Copy)]
 pub struct Ipv6Addr {
     inner: c::in6_addr,
 }
 
-#[allow(missing_docs)]
+/// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2].
+///
+/// # Stability Guarantees
+///
+/// Not all possible values for a multicast scope have been assigned.
+/// Future RFCs may introduce new scopes, which will be added as variants to this enum;
+/// because of this the enum is marked as `#[non_exhaustive]`.
+///
+/// # Examples
+/// ```
+/// #![feature(ip)]
+///
+/// use std::net::Ipv6Addr;
+/// use std::net::Ipv6MulticastScope::*;
+///
+/// // An IPv6 multicast address with global scope (`ff0e::`).
+/// let address = Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0);
+///
+/// // Will print "Global scope".
+/// match address.multicast_scope() {
+///     Some(InterfaceLocal) => println!("Interface-Local scope"),
+///     Some(LinkLocal) => println!("Link-Local scope"),
+///     Some(RealmLocal) => println!("Realm-Local scope"),
+///     Some(AdminLocal) => println!("Admin-Local scope"),
+///     Some(SiteLocal) => println!("Site-Local scope"),
+///     Some(OrganizationLocal) => println!("Organization-Local scope"),
+///     Some(Global) => println!("Global scope"),
+///     Some(_) => println!("Unknown scope"),
+///     None => println!("Not a multicast address!")
+/// }
+///
+/// ```
+///
+/// [IPv6 multicast address]: Ipv6Addr
+/// [IETF RFC 7346 section 2]: https://tools.ietf.org/html/rfc7346#section-2
 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
+#[non_exhaustive]
 pub enum Ipv6MulticastScope {
+    /// Interface-Local scope.
     InterfaceLocal,
+    /// Link-Local scope.
     LinkLocal,
+    /// Realm-Local scope.
     RealmLocal,
+    /// Admin-Local scope.
     AdminLocal,
+    /// Site-Local scope.
     SiteLocal,
+    /// Organization-Local scope.
     OrganizationLocal,
+    /// Global scope.
     Global,
 }
 
 impl IpAddr {
     /// Returns [`true`] for the special 'unspecified' address.
     ///
-    /// See the documentation for [`Ipv4Addr::is_unspecified`][IPv4] and
-    /// [`Ipv6Addr::is_unspecified`][IPv6] for more details.
+    /// See the documentation for [`Ipv4Addr::is_unspecified()`] and
+    /// [`Ipv6Addr::is_unspecified()`] for more details.
     ///
-    /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
-    /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
-    /// [`true`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_unspecified(&self) -> bool {
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
+    /// ```
+    #[inline]
+    pub const fn is_unspecified(&self) -> bool {
         match self {
             IpAddr::V4(ip) => ip.is_unspecified(),
             IpAddr::V6(ip) => ip.is_unspecified(),
@@ -125,14 +250,19 @@
 
     /// Returns [`true`] if this is a loopback address.
     ///
-    /// See the documentation for [`Ipv4Addr::is_loopback`][IPv4] and
-    /// [`Ipv6Addr::is_loopback`][IPv6] for more details.
+    /// See the documentation for [`Ipv4Addr::is_loopback()`] and
+    /// [`Ipv6Addr::is_loopback()`] for more details.
     ///
-    /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
-    /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
-    /// [`true`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_loopback(&self) -> bool {
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
+    /// ```
+    #[inline]
+    pub const fn is_loopback(&self) -> bool {
         match self {
             IpAddr::V4(ip) => ip.is_loopback(),
             IpAddr::V6(ip) => ip.is_loopback(),
@@ -141,14 +271,21 @@
 
     /// Returns [`true`] if the address appears to be globally routable.
     ///
-    /// See the documentation for [`Ipv4Addr::is_global`][IPv4] and
-    /// [`Ipv6Addr::is_global`][IPv6] for more details.
+    /// See the documentation for [`Ipv4Addr::is_global()`] and
+    /// [`Ipv6Addr::is_global()`] for more details.
     ///
-    /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
-    /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
-    /// [`true`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_global(&self) -> bool {
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true);
+    /// ```
+    #[inline]
+    pub const fn is_global(&self) -> bool {
         match self {
             IpAddr::V4(ip) => ip.is_global(),
             IpAddr::V6(ip) => ip.is_global(),
@@ -157,14 +294,19 @@
 
     /// Returns [`true`] if this is a multicast address.
     ///
-    /// See the documentation for [`Ipv4Addr::is_multicast`][IPv4] and
-    /// [`Ipv6Addr::is_multicast`][IPv6] for more details.
+    /// See the documentation for [`Ipv4Addr::is_multicast()`] and
+    /// [`Ipv6Addr::is_multicast()`] for more details.
     ///
-    /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
-    /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
-    /// [`true`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_multicast(&self) -> bool {
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
+    /// ```
+    #[inline]
+    pub const fn is_multicast(&self) -> bool {
         match self {
             IpAddr::V4(ip) => ip.is_multicast(),
             IpAddr::V6(ip) => ip.is_multicast(),
@@ -173,39 +315,86 @@
 
     /// Returns [`true`] if this address is in a range designated for documentation.
     ///
-    /// See the documentation for [`Ipv4Addr::is_documentation`][IPv4] and
-    /// [`Ipv6Addr::is_documentation`][IPv6] for more details.
+    /// See the documentation for [`Ipv4Addr::is_documentation()`] and
+    /// [`Ipv6Addr::is_documentation()`] for more details.
     ///
-    /// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
-    /// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
-    /// [`true`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_documentation(&self) -> bool {
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
+    /// assert_eq!(
+    ///     IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(),
+    ///     true
+    /// );
+    /// ```
+    #[inline]
+    pub const fn is_documentation(&self) -> bool {
         match self {
             IpAddr::V4(ip) => ip.is_documentation(),
             IpAddr::V6(ip) => ip.is_documentation(),
         }
     }
 
-    /// Returns [`true`] if this address is an [IPv4 address], and [`false`] otherwise.
+    /// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
+    /// otherwise.
     ///
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [`false`]: ../../std/primitive.bool.html
-    /// [IPv4 address]: #variant.V4
+    /// [`IPv4` address]: IpAddr::V4
     ///
-    pub fn is_ipv4(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false);
+    /// ```
+    #[inline]
+    pub const fn is_ipv4(&self) -> bool {
         matches!(self, IpAddr::V4(_))
     }
 
-    /// Returns [`true`] if this address is an [IPv6 address], and [`false`] otherwise.
+    /// Returns [`true`] if this address is an [`IPv6` address], and [`false`]
+    /// otherwise.
     ///
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [`false`]: ../../std/primitive.bool.html
-    /// [IPv6 address]: #variant.V6
+    /// [`IPv6` address]: IpAddr::V6
     ///
-    pub fn is_ipv6(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true);
+    /// ```
+    #[inline]
+    pub const fn is_ipv6(&self) -> bool {
         matches!(self, IpAddr::V6(_))
     }
+
+    /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it
+    /// return `self` as-is.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
+    /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
+    /// ```
+    #[inline]
+    pub const fn to_canonical(&self) -> IpAddr {
+        match self {
+            &v4 @ IpAddr::V4(_) => v4,
+            IpAddr::V6(v6) => v6.to_canonical(),
+        }
+    }
 }
 
 impl Ipv4Addr {
@@ -213,57 +402,110 @@
     ///
     /// The result will represent the IP address `a`.`b`.`c`.`d`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::new(127, 0, 0, 1);
+    /// ```
+    #[inline]
     pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
-        // FIXME: should just be u32::from_be_bytes([a, b, c, d]),
-        // once that method is no longer rustc_const_unstable
-        Ipv4Addr {
-            inner: c::in_addr {
-                s_addr: u32::to_be(
-                    ((a as u32) << 24) | ((b as u32) << 16) | ((c as u32) << 8) | (d as u32),
-                ),
-            },
-        }
+        // `s_addr` is stored as BE on all machine and the array is in BE order.
+        // So the native endian conversion method is used so that it's never swapped.
+        Ipv4Addr { inner: c::in_addr { s_addr: u32::from_ne_bytes([a, b, c, d]) } }
     }
 
-    /// An IPv4 address with the address pointing to localhost: 127.0.0.1.
+    /// An IPv4 address with the address pointing to localhost: `127.0.0.1`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::LOCALHOST;
+    /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
+    /// ```
     pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
 
-    /// An IPv4 address representing an unspecified address: 0.0.0.0
+    /// An IPv4 address representing an unspecified address: `0.0.0.0`
     ///
+    /// This corresponds to the constant `INADDR_ANY` in other languages.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::UNSPECIFIED;
+    /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
+    /// ```
     pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
 
-    /// An IPv4 address representing the broadcast address: 255.255.255.255
+    /// An IPv4 address representing the broadcast address: `255.255.255.255`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::BROADCAST;
+    /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
+    /// ```
     pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
 
     /// Returns the four eight-bit integers that make up this address.
     ///
-    pub fn octets(&self) -> [u8; 4] {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::new(127, 0, 0, 1);
+    /// assert_eq!(addr.octets(), [127, 0, 0, 1]);
+    /// ```
+    #[inline]
+    pub const fn octets(&self) -> [u8; 4] {
         // This returns the order we want because s_addr is stored in big-endian.
         self.inner.s_addr.to_ne_bytes()
     }
 
-    /// Returns [`true`] for the special 'unspecified' address (0.0.0.0).
+    /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`).
     ///
     /// This property is defined in _UNIX Network Programming, Second Edition_,
     /// W. Richard Stevens, p. 891; see also [ip7].
     ///
-    /// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
-    /// [`true`]: ../../std/primitive.bool.html
+    /// [ip7]: https://man7.org/linux/man-pages/man7/ip.7.html
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
+    /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
+    /// ```
+    #[inline]
     pub const fn is_unspecified(&self) -> bool {
         self.inner.s_addr == 0
     }
 
-    /// Returns [`true`] if this is a loopback address (127.0.0.0/8).
+    /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`).
     ///
     /// This property is defined by [IETF RFC 1122].
     ///
     /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_loopback(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
+    /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
+    /// ```
+    #[inline]
+    pub const fn is_loopback(&self) -> bool {
         self.octets()[0] == 127
     }
 
@@ -271,14 +513,27 @@
     ///
     /// The private address ranges are defined in [IETF RFC 1918] and include:
     ///
-    ///  - 10.0.0.0/8
-    ///  - 172.16.0.0/12
-    ///  - 192.168.0.0/16
+    ///  - `10.0.0.0/8`
+    ///  - `172.16.0.0/12`
+    ///  - `192.168.0.0/16`
     ///
     /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_private(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
+    /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
+    /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
+    /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
+    /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
+    /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
+    /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
+    /// ```
+    #[inline]
+    pub const fn is_private(&self) -> bool {
         match self.octets() {
             [10, ..] => true,
             [172, b, ..] if b >= 16 && b <= 31 => true,
@@ -287,46 +542,101 @@
         }
     }
 
-    /// Returns [`true`] if the address is link-local (169.254.0.0/16).
+    /// Returns [`true`] if the address is link-local (`169.254.0.0/16`).
     ///
     /// This property is defined by [IETF RFC 3927].
     ///
     /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_link_local(&self) -> bool {
-        match self.octets() {
-            [169, 254, ..] => true,
-            _ => false,
-        }
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
+    /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
+    /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
+    /// ```
+    #[inline]
+    pub const fn is_link_local(&self) -> bool {
+        matches!(self.octets(), [169, 254, ..])
     }
 
     /// Returns [`true`] if the address appears to be globally routable.
     /// See [iana-ipv4-special-registry][ipv4-sr].
     ///
-    /// The following return false:
+    /// The following return [`false`]:
     ///
-    /// - private addresses (see [`is_private()`](#method.is_private))
-    /// - the loopback address (see [`is_loopback()`](#method.is_loopback))
-    /// - the link-local address (see [`is_link_local()`](#method.is_link_local))
-    /// - the broadcast address (see [`is_broadcast()`](#method.is_broadcast))
-    /// - addresses used for documentation (see [`is_documentation()`](#method.is_documentation))
-    /// - the unspecified address (see [`is_unspecified()`](#method.is_unspecified)), and the whole
-    ///   0.0.0.0/8 block
-    /// - addresses reserved for future protocols (see
-    /// [`is_ietf_protocol_assignment()`](#method.is_ietf_protocol_assignment), except
+    /// - private addresses (see [`Ipv4Addr::is_private()`])
+    /// - the loopback address (see [`Ipv4Addr::is_loopback()`])
+    /// - the link-local address (see [`Ipv4Addr::is_link_local()`])
+    /// - the broadcast address (see [`Ipv4Addr::is_broadcast()`])
+    /// - addresses used for documentation (see [`Ipv4Addr::is_documentation()`])
+    /// - the unspecified address (see [`Ipv4Addr::is_unspecified()`]), and the whole
+    ///   `0.0.0.0/8` block
+    /// - addresses reserved for future protocols, except
     /// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable
-    /// - addresses reserved for future use (see [`is_reserved()`](#method.is_reserved)
+    /// - addresses reserved for future use (see [`Ipv4Addr::is_reserved()`]
     /// - addresses reserved for networking devices benchmarking (see
-    /// [`is_benchmarking`](#method.is_benchmarking))
+    /// [`Ipv4Addr::is_benchmarking()`])
     ///
     /// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_global(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv4Addr;
+    ///
+    /// // private addresses are not global
+    /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false);
+    /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false);
+    /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false);
+    ///
+    /// // the 0.0.0.0/8 block is not global
+    /// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false);
+    /// // in particular, the unspecified address is not global
+    /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false);
+    ///
+    /// // the loopback address is not global
+    /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false);
+    ///
+    /// // link local addresses are not global
+    /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false);
+    ///
+    /// // the broadcast address is not global
+    /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false);
+    ///
+    /// // the address space designated for documentation is not global
+    /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false);
+    /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false);
+    /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false);
+    ///
+    /// // shared addresses are not global
+    /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false);
+    ///
+    /// // addresses reserved for protocol assignment are not global
+    /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false);
+    /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false);
+    ///
+    /// // addresses reserved for future use are not global
+    /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false);
+    ///
+    /// // addresses reserved for network devices benchmarking are not global
+    /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false);
+    ///
+    /// // All the other addresses are global
+    /// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true);
+    /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true);
+    /// ```
+    #[inline]
+    pub const fn is_global(&self) -> bool {
         // check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two
         // globally routable addresses in the 192.0.0.0/24 range.
-        if u32::from(*self) == 0xc0000009 || u32::from(*self) == 0xc000000a {
+        if u32::from_be_bytes(self.octets()) == 0xc0000009
+            || u32::from_be_bytes(self.octets()) == 0xc000000a
+        {
             return true;
         }
         !self.is_private()
@@ -335,7 +645,8 @@
             && !self.is_broadcast()
             && !self.is_documentation()
             && !self.is_shared()
-            && !self.is_ietf_protocol_assignment()
+            // addresses reserved for future protocols (`192.0.0.0/24`)
+            && !(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0)
             && !self.is_reserved()
             && !self.is_benchmarking()
             // Make sure the address is not in 0.0.0.0/8
@@ -346,40 +657,42 @@
     /// [IETF RFC 6598] (`100.64.0.0/10`).
     ///
     /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_shared(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
+    /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
+    /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
+    /// ```
+    #[inline]
+    pub const fn is_shared(&self) -> bool {
         self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
     }
 
-    /// Returns [`true`] if this address is part of `192.0.0.0/24`, which is reserved to
-    /// IANA for IETF protocol assignments, as documented in [IETF RFC 6890].
-    ///
-    /// Note that parts of this block are in use:
-    ///
-    /// - `192.0.0.8/32` is the "IPv4 dummy address" (see [IETF RFC 7600])
-    /// - `192.0.0.9/32` is the "Port Control Protocol Anycast" (see [IETF RFC 7723])
-    /// - `192.0.0.10/32` is used for NAT traversal (see [IETF RFC 8155])
-    ///
-    /// [IETF RFC 6890]: https://tools.ietf.org/html/rfc6890
-    /// [IETF RFC 7600]: https://tools.ietf.org/html/rfc7600
-    /// [IETF RFC 7723]: https://tools.ietf.org/html/rfc7723
-    /// [IETF RFC 8155]: https://tools.ietf.org/html/rfc8155
-    /// [`true`]: ../../std/primitive.bool.html
-    ///
-    pub fn is_ietf_protocol_assignment(&self) -> bool {
-        self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0
-    }
-
     /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for
     /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0`
     /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`.
     ///
     /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544
     /// [errata 423]: https://www.rfc-editor.org/errata/eid423
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_benchmarking(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false);
+    /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true);
+    /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true);
+    /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false);
+    /// ```
+    #[inline]
+    pub const fn is_benchmarking(&self) -> bool {
         self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18
     }
 
@@ -389,7 +702,6 @@
     /// it is obviously not reserved for future use.
     ///
     /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112
-    /// [`true`]: ../../std/primitive.bool.html
     ///
     /// # Warning
     ///
@@ -398,45 +710,87 @@
     /// treated as reserved in code that relies on an outdated version
     /// of this method.
     ///
-    pub fn is_reserved(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true);
+    /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true);
+    ///
+    /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false);
+    /// // The broadcast address is not considered as reserved for future use by this implementation
+    /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false);
+    /// ```
+    #[inline]
+    pub const fn is_reserved(&self) -> bool {
         self.octets()[0] & 240 == 240 && !self.is_broadcast()
     }
 
-    /// Returns [`true`] if this is a multicast address (224.0.0.0/4).
+    /// Returns [`true`] if this is a multicast address (`224.0.0.0/4`).
     ///
-    /// Multicast addresses have a most significant octet between 224 and 239,
+    /// Multicast addresses have a most significant octet between `224` and `239`,
     /// and is defined by [IETF RFC 5771].
     ///
     /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_multicast(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
+    /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
+    /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
+    /// ```
+    #[inline]
+    pub const fn is_multicast(&self) -> bool {
         self.octets()[0] >= 224 && self.octets()[0] <= 239
     }
 
-    /// Returns [`true`] if this is a broadcast address (255.255.255.255).
+    /// Returns [`true`] if this is a broadcast address (`255.255.255.255`).
     ///
-    /// A broadcast address has all octets set to 255 as defined in [IETF RFC 919].
+    /// A broadcast address has all octets set to `255` as defined in [IETF RFC 919].
     ///
     /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_broadcast(&self) -> bool {
-        self == &Self::BROADCAST
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
+    /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
+    /// ```
+    #[inline]
+    pub const fn is_broadcast(&self) -> bool {
+        u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets())
     }
 
     /// Returns [`true`] if this address is in a range designated for documentation.
     ///
     /// This is defined in [IETF RFC 5737]:
     ///
-    /// - 192.0.2.0/24 (TEST-NET-1)
-    /// - 198.51.100.0/24 (TEST-NET-2)
-    /// - 203.0.113.0/24 (TEST-NET-3)
+    /// - `192.0.2.0/24` (TEST-NET-1)
+    /// - `198.51.100.0/24` (TEST-NET-2)
+    /// - `203.0.113.0/24` (TEST-NET-3)
     ///
     /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_documentation(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
+    /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
+    /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
+    /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
+    /// ```
+    #[inline]
+    #[allow(clippy::match_like_matches_macro)]
+    pub const fn is_documentation(&self) -> bool {
         match self.octets() {
             [192, 0, 2, _] => true,
             [198, 51, 100, _] => true,
@@ -445,30 +799,55 @@
         }
     }
 
-    /// Converts this address to an IPv4-compatible [IPv6 address].
+    /// Converts this address to an [IPv4-compatible] [`IPv6` address].
     ///
-    /// a.b.c.d becomes ::a.b.c.d
+    /// `a.b.c.d` becomes `::a.b.c.d`
     ///
-    /// [IPv6 address]: ../../std/net/struct.Ipv6Addr.html
+    /// Note that IPv4-compatible addresses have been officially deprecated.
+    /// If you don't explicitly need an IPv4-compatible address for legacy reasons, consider using `to_ipv6_mapped` instead.
     ///
-    pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
-        let octets = self.octets();
-        Ipv6Addr::from([
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, octets[0], octets[1], octets[2], octets[3],
-        ])
+    /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
+    /// [`IPv6` address]: Ipv6Addr
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(
+    ///     Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(),
+    ///     Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff)
+    /// );
+    /// ```
+    #[inline]
+    pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
+        let [a, b, c, d] = self.octets();
+        Ipv6Addr {
+            inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] },
+        }
     }
 
-    /// Converts this address to an IPv4-mapped [IPv6 address].
+    /// Converts this address to an [IPv4-mapped] [`IPv6` address].
     ///
-    /// a.b.c.d becomes ::ffff:a.b.c.d
+    /// `a.b.c.d` becomes `::ffff:a.b.c.d`
     ///
-    /// [IPv6 address]: ../../std/net/struct.Ipv6Addr.html
+    /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
+    /// [`IPv6` address]: Ipv6Addr
     ///
-    pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
-        let octets = self.octets();
-        Ipv6Addr::from([
-            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, octets[0], octets[1], octets[2], octets[3],
-        ])
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(),
+    ///            Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff));
+    /// ```
+    #[inline]
+    pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
+        let [a, b, c, d] = self.octets();
+        Ipv6Addr {
+            inner: c::in6_addr { s6_addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] },
+        }
     }
 }
 
@@ -481,7 +860,28 @@
     }
 }
 
+impl fmt::Debug for IpAddr {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Display::fmt(self, fmt)
+    }
+}
+
 impl From<Ipv4Addr> for IpAddr {
+    /// Copies this address to a new `IpAddr::V4`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr};
+    ///
+    /// let addr = Ipv4Addr::new(127, 0, 0, 1);
+    ///
+    /// assert_eq!(
+    ///     IpAddr::V4(addr),
+    ///     IpAddr::from(addr)
+    /// )
+    /// ```
+    #[inline]
     fn from(ipv4: Ipv4Addr) -> IpAddr {
         IpAddr::V4(ipv4)
     }
@@ -490,6 +890,19 @@
 impl From<Ipv6Addr> for IpAddr {
     /// Copies this address to a new `IpAddr::V6`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv6Addr};
+    ///
+    /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
+    ///
+    /// assert_eq!(
+    ///     IpAddr::V6(addr),
+    ///     IpAddr::from(addr)
+    /// );
+    /// ```
+    #[inline]
     fn from(ipv6: Ipv6Addr) -> IpAddr {
         IpAddr::V6(ipv6)
     }
@@ -497,16 +910,23 @@
 
 impl fmt::Display for Ipv4Addr {
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
-        let mut buf = [0u8; IPV4_BUF_LEN];
-        let mut buf_slice = &mut buf[..];
         let octets = self.octets();
-        // Note: The call to write should never fail, hence the unwrap
-        write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
-        let len = IPV4_BUF_LEN - buf_slice.len();
-        // This unsafe is OK because we know what is being written to the buffer
-        let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
-        fmt.pad(buf)
+        // Fast Path: if there's no alignment stuff, write directly to the buffer
+        if fmt.precision().is_none() && fmt.width().is_none() {
+            write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
+        } else {
+            const IPV4_BUF_LEN: usize = 15; // Long enough for the longest possible IPv4 address
+            let mut buf = [0u8; IPV4_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
+
+            // Note: The call to write should never fail, hence the unwrap
+            write!(buf_slice, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();
+            let len = IPV4_BUF_LEN - buf_slice.len();
+
+            // This unsafe is OK because we know what is being written to the buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            fmt.pad(buf)
+        }
     }
 }
 
@@ -517,18 +937,21 @@
 }
 
 impl Clone for Ipv4Addr {
+    #[inline]
     fn clone(&self) -> Ipv4Addr {
         *self
     }
 }
 
 impl PartialEq for Ipv4Addr {
+    #[inline]
     fn eq(&self, other: &Ipv4Addr) -> bool {
         self.inner.s_addr == other.inner.s_addr
     }
 }
 
 impl PartialEq<Ipv4Addr> for IpAddr {
+    #[inline]
     fn eq(&self, other: &Ipv4Addr) -> bool {
         match self {
             IpAddr::V4(v4) => v4 == other,
@@ -538,6 +961,7 @@
 }
 
 impl PartialEq<IpAddr> for Ipv4Addr {
+    #[inline]
     fn eq(&self, other: &IpAddr) -> bool {
         match other {
             IpAddr::V4(v4) => self == v4,
@@ -549,19 +973,25 @@
 impl Eq for Ipv4Addr {}
 
 impl hash::Hash for Ipv4Addr {
+    #[inline]
     fn hash<H: hash::Hasher>(&self, s: &mut H) {
-        // `inner` is #[repr(packed)], so we need to copy `s_addr`.
+        // NOTE:
+        // * hash in big endian order
+        // * in netbsd, `in_addr` has `repr(packed)`, we need to
+        //   copy `s_addr` to avoid unsafe borrowing
         { self.inner.s_addr }.hash(s)
     }
 }
 
 impl PartialOrd for Ipv4Addr {
+    #[inline]
     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
         Some(self.cmp(other))
     }
 }
 
 impl PartialOrd<Ipv4Addr> for IpAddr {
+    #[inline]
     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
         match self {
             IpAddr::V4(v4) => v4.partial_cmp(other),
@@ -571,6 +1001,7 @@
 }
 
 impl PartialOrd<IpAddr> for Ipv4Addr {
+    #[inline]
     fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
         match other {
             IpAddr::V4(v4) => self.partial_cmp(v4),
@@ -580,25 +1011,32 @@
 }
 
 impl Ord for Ipv4Addr {
+    #[inline]
     fn cmp(&self, other: &Ipv4Addr) -> Ordering {
+        // Compare as native endian
         u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
     }
 }
 
-impl AsInner<c::in_addr> for Ipv4Addr {
-    fn as_inner(&self) -> &c::in_addr {
-        &self.inner
-    }
-}
-impl FromInner<c::in_addr> for Ipv4Addr {
-    fn from_inner(addr: c::in_addr) -> Ipv4Addr {
-        Ipv4Addr { inner: addr }
+impl IntoInner<c::in_addr> for Ipv4Addr {
+    #[inline]
+    fn into_inner(self) -> c::in_addr {
+        self.inner
     }
 }
 
 impl From<Ipv4Addr> for u32 {
     /// Converts an `Ipv4Addr` into a host byte order `u32`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::new(0xca, 0xfe, 0xba, 0xbe);
+    /// assert_eq!(0xcafebabe, u32::from(addr));
+    /// ```
+    #[inline]
     fn from(ip: Ipv4Addr) -> u32 {
         let ip = ip.octets();
         u32::from_be_bytes(ip)
@@ -608,6 +1046,15 @@
 impl From<u32> for Ipv4Addr {
     /// Converts a host byte order `u32` into an `Ipv4Addr`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::from(0xcafebabe);
+    /// assert_eq!(Ipv4Addr::new(0xca, 0xfe, 0xba, 0xbe), addr);
+    /// ```
+    #[inline]
     fn from(ip: u32) -> Ipv4Addr {
         Ipv4Addr::from(ip.to_be_bytes())
     }
@@ -616,6 +1063,15 @@
 impl From<[u8; 4]> for Ipv4Addr {
     /// Creates an `Ipv4Addr` from a four element byte array.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv4Addr;
+    ///
+    /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
+    /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
+    /// ```
+    #[inline]
     fn from(octets: [u8; 4]) -> Ipv4Addr {
         Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
     }
@@ -624,6 +1080,15 @@
 impl From<[u8; 4]> for IpAddr {
     /// Creates an `IpAddr::V4` from a four element byte array.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv4Addr};
+    ///
+    /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]);
+    /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr);
+    /// ```
+    #[inline]
     fn from(octets: [u8; 4]) -> IpAddr {
         IpAddr::V4(Ipv4Addr::from(octets))
     }
@@ -634,75 +1099,130 @@
     ///
     /// The result will represent the IP address `a:b:c:d:e:f:g:h`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff);
+    /// ```
+    #[allow(clippy::too_many_arguments)]
+    #[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_fn_transmute))]
+    #[inline]
     pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
+        let addr16 = [
+            a.to_be(),
+            b.to_be(),
+            c.to_be(),
+            d.to_be(),
+            e.to_be(),
+            f.to_be(),
+            g.to_be(),
+            h.to_be(),
+        ];
         Ipv6Addr {
             inner: c::in6_addr {
-                s6_addr: [
-                    (a >> 8) as u8,
-                    a as u8,
-                    (b >> 8) as u8,
-                    b as u8,
-                    (c >> 8) as u8,
-                    c as u8,
-                    (d >> 8) as u8,
-                    d as u8,
-                    (e >> 8) as u8,
-                    e as u8,
-                    (f >> 8) as u8,
-                    f as u8,
-                    (g >> 8) as u8,
-                    g as u8,
-                    (h >> 8) as u8,
-                    h as u8,
-                ],
+                // All elements in `addr16` are big endian.
+                // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`.
+                // rustc_allow_const_fn_unstable: the transmute could be written as stable const
+                // code, but that leads to worse code generation (#75085)
+                s6_addr: unsafe { transmute::<_, [u8; 16]>(addr16) },
             },
         }
     }
 
     /// An IPv6 address representing localhost: `::1`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::LOCALHOST;
+    /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
+    /// ```
     pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
 
     /// An IPv6 address representing the unspecified address: `::`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::UNSPECIFIED;
+    /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0));
+    /// ```
     pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
 
     /// Returns the eight 16-bit segments that make up this address.
     ///
-    pub fn segments(&self) -> [u16; 8] {
-        let arr = &self.inner.s6_addr;
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(),
+    ///            [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]);
+    /// ```
+    #[inline]
+    pub const fn segments(&self) -> [u16; 8] {
+        // All elements in `s6_addr` must be big endian.
+        // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`.
+        // rustc_allow_const_fn_unstable: the transmute could be written as stable const code, but
+        // that leads to worse code generation (#75085)
+        let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.inner.s6_addr) };
+        // We want native endian u16
         [
-            u16::from_be_bytes([arr[0], arr[1]]),
-            u16::from_be_bytes([arr[2], arr[3]]),
-            u16::from_be_bytes([arr[4], arr[5]]),
-            u16::from_be_bytes([arr[6], arr[7]]),
-            u16::from_be_bytes([arr[8], arr[9]]),
-            u16::from_be_bytes([arr[10], arr[11]]),
-            u16::from_be_bytes([arr[12], arr[13]]),
-            u16::from_be_bytes([arr[14], arr[15]]),
+            u16::from_be(a),
+            u16::from_be(b),
+            u16::from_be(c),
+            u16::from_be(d),
+            u16::from_be(e),
+            u16::from_be(f),
+            u16::from_be(g),
+            u16::from_be(h),
         ]
     }
 
-    /// Returns [`true`] for the special 'unspecified' address (::).
+    /// Returns [`true`] for the special 'unspecified' address (`::`).
     ///
     /// This property is defined in [IETF RFC 4291].
     ///
     /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_unspecified(&self) -> bool {
-        self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true);
+    /// ```
+    #[inline]
+    pub const fn is_unspecified(&self) -> bool {
+        u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
     }
 
-    /// Returns [`true`] if this is a loopback address (::1).
+    /// Returns [`true`] if this is the [loopback address] (`::1`),
+    /// as defined in [IETF RFC 4291 section 2.5.3].
     ///
-    /// This property is defined in [IETF RFC 4291].
+    /// Contrary to IPv4, in IPv6 there is only one loopback address.
     ///
-    /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
-    /// [`true`]: ../../std/primitive.bool.html
+    /// [loopback address]: Ipv6Addr::LOCALHOST
+    /// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
     ///
-    pub fn is_loopback(&self) -> bool {
-        self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true);
+    /// ```
+    #[inline]
+    pub const fn is_loopback(&self) -> bool {
+        u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
     }
 
     /// Returns [`true`] if the address appears to be globally routable.
@@ -713,10 +1233,19 @@
     /// - link-local and unique local unicast addresses
     /// - interface-, link-, realm-, admin- and site-local multicast addresses
     ///
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [`false`]: ../../std/primitive.bool.html
+    /// # Examples
     ///
-    pub fn is_global(&self) -> bool {
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true);
+    /// ```
+    #[inline]
+    pub const fn is_global(&self) -> bool {
         match self.multicast_scope() {
             Some(Ipv6MulticastScope::Global) => true,
             None => self.is_unicast_global(),
@@ -729,114 +1258,116 @@
     /// This property is defined in [IETF RFC 4193].
     ///
     /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_unique_local(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false);
+    /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true);
+    /// ```
+    #[inline]
+    pub const fn is_unique_local(&self) -> bool {
         (self.segments()[0] & 0xfe00) == 0xfc00
     }
 
-    /// Returns [`true`] if the address is a unicast link-local address (`fe80::/64`).
+    /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291].
+    /// Any address that is not a [multicast address] (`ff00::/8`) is unicast.
     ///
-    /// A common mis-conception is to think that "unicast link-local addresses start with
-    /// `fe80::`", but the [IETF RFC 4291] actually defines a stricter format for these addresses:
+    /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
+    /// [multicast address]: Ipv6Addr::is_multicast
     ///
-    /// ```no_rust
-    /// |   10     |
-    /// |  bits    |         54 bits         |          64 bits           |
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv6Addr;
+    ///
+    /// // The unspecified and loopback addresses are unicast.
+    /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true);
+    /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true);
+    ///
+    /// // Any address that is not a multicast address (`ff00::/8`) is unicast.
+    /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true);
+    /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false);
+    /// ```
+    #[inline]
+    pub const fn is_unicast(&self) -> bool {
+        !self.is_multicast()
+    }
+
+    /// Returns `true` if the address is a unicast address with link-local scope,
+    /// as defined in [RFC 4291].
+    ///
+    /// A unicast address has link-local scope if it has the prefix `fe80::/10`, as per [RFC 4291 section 2.4].
+    /// Note that this encompasses more addresses than those defined in [RFC 4291 section 2.5.6],
+    /// which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format:
+    ///
+    /// ```text
+    /// | 10 bits  |         54 bits         |          64 bits           |
     /// +----------+-------------------------+----------------------------+
     /// |1111111010|           0             |       interface ID         |
     /// +----------+-------------------------+----------------------------+
     /// ```
+    /// So while currently the only addresses with link-local scope an application will encounter are all in `fe80::/64`,
+    /// this might change in the future with the publication of new standards. More addresses in `fe80::/10` could be allocated,
+    /// and those addresses will have link-local scope.
     ///
-    /// This method validates the format defined in the RFC and won't recognize the following
-    /// addresses such as `fe80:0:0:1::` or `fe81::` as unicast link-local addresses for example.
-    /// If you need a less strict validation use [`is_unicast_link_local()`] instead.
+    /// Also note that while [RFC 4291 section 2.5.3] mentions about the [loopback address] (`::1`) that "it is treated as having Link-Local scope",
+    /// this does not mean that the loopback address actually has link-local scope and this method will return `false` on it.
     ///
-    /// # See also
+    /// [RFC 4291]: https://tools.ietf.org/html/rfc4291
+    /// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
+    /// [RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
+    /// [RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
+    /// [loopback address]: Ipv6Addr::LOCALHOST
     ///
-    /// - [IETF RFC 4291 section 2.5.6]
-    /// - [RFC 4291 errata 4406]
-    /// - [`is_unicast_link_local()`]
+    /// # Examples
     ///
-    /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
-    /// [IETF RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
-    /// [`is_unicast_link_local()`]: ../../std/net/struct.Ipv6Addr.html#method.is_unicast_link_local
-    ///
-    pub fn is_unicast_link_local_strict(&self) -> bool {
-        (self.segments()[0] & 0xffff) == 0xfe80
-            && (self.segments()[1] & 0xffff) == 0
-            && (self.segments()[2] & 0xffff) == 0
-            && (self.segments()[3] & 0xffff) == 0
-    }
-
-    /// Returns [`true`] if the address is a unicast link-local address (`fe80::/10`).
-    ///
-    /// This method returns [`true`] for addresses in the range reserved by [RFC 4291 section 2.4],
-    /// i.e. addresses with the following format:
-    ///
-    /// ```no_rust
-    /// |   10     |
-    /// |  bits    |         54 bits         |          64 bits           |
-    /// +----------+-------------------------+----------------------------+
-    /// |1111111010|    arbitratry value     |       interface ID         |
-    /// +----------+-------------------------+----------------------------+
     /// ```
+    /// #![feature(ip)]
     ///
-    /// As a result, this method consider addresses such as `fe80:0:0:1::` or `fe81::` to be
-    /// unicast link-local addresses, whereas [`is_unicast_link_local_strict()`] does not. If you
-    /// need a strict validation fully compliant with the RFC, use
-    /// [`is_unicast_link_local_strict()`].
+    /// use std::net::Ipv6Addr;
     ///
-    /// # See also
+    /// // The loopback address (`::1`) does not actually have link-local scope.
+    /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_link_local(), false);
     ///
-    /// - [IETF RFC 4291 section 2.4]
-    /// - [RFC 4291 errata 4406]
+    /// // Only addresses in `fe80::/10` have link-local scope.
+    /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), false);
+    /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
     ///
-    /// [IETF RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [RFC 4291 errata 4406]: https://www.rfc-editor.org/errata/eid4406
-    /// [`is_unicast_link_local_strict()`]: ../../std/net/struct.Ipv6Addr.html#method.is_unicast_link_local_strict
-    ///
-    pub fn is_unicast_link_local(&self) -> bool {
+    /// // Addresses outside the stricter `fe80::/64` also have link-local scope.
+    /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0).is_unicast_link_local(), true);
+    /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true);
+    /// ```
+    #[inline]
+    pub const fn is_unicast_link_local(&self) -> bool {
         (self.segments()[0] & 0xffc0) == 0xfe80
     }
 
-    /// Returns [`true`] if this is a deprecated unicast site-local address (fec0::/10). The
-    /// unicast site-local address format is defined in [RFC 4291 section 2.5.7] as:
-    ///
-    /// ```no_rust
-    /// |   10     |
-    /// |  bits    |         54 bits         |         64 bits            |
-    /// +----------+-------------------------+----------------------------+
-    /// |1111111011|        subnet ID        |       interface ID         |
-    /// +----------+-------------------------+----------------------------+
-    /// ```
-    ///
-    /// [`true`]: ../../std/primitive.bool.html
-    /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
-    ///
-    /// # Warning
-    ///
-    /// As per [RFC 3879], the whole `FEC0::/10` prefix is
-    /// deprecated. New software must not support site-local
-    /// addresses.
-    ///
-    /// [RFC 3879]: https://tools.ietf.org/html/rfc3879
-    pub fn is_unicast_site_local(&self) -> bool {
-        (self.segments()[0] & 0xffc0) == 0xfec0
-    }
-
     /// Returns [`true`] if this is an address reserved for documentation
-    /// (2001:db8::/32).
+    /// (`2001:db8::/32`).
     ///
     /// This property is defined in [IETF RFC 3849].
     ///
     /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_documentation(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false);
+    /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true);
+    /// ```
+    #[inline]
+    pub const fn is_documentation(&self) -> bool {
         (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8)
     }
 
@@ -858,11 +1389,21 @@
     /// Global Unicast).
     /// ```
     ///
-    /// [`true`]: ../../std/primitive.bool.html
     /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7
     ///
-    pub fn is_unicast_global(&self) -> bool {
-        !self.is_multicast()
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true);
+    /// ```
+    #[inline]
+    pub const fn is_unicast_global(&self) -> bool {
+        self.is_unicast()
             && !self.is_loopback()
             && !self.is_unicast_link_local()
             && !self.is_unique_local()
@@ -872,7 +1413,21 @@
 
     /// Returns the address's multicast scope if the address is multicast.
     ///
-    pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::{Ipv6Addr, Ipv6MulticastScope};
+    ///
+    /// assert_eq!(
+    ///     Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(),
+    ///     Some(Ipv6MulticastScope::Global)
+    /// );
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None);
+    /// ```
+    #[inline]
+    pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
         if self.is_multicast() {
             match self.segments()[0] & 0x000f {
                 1 => Some(Ipv6MulticastScope::InterfaceLocal),
@@ -889,136 +1444,220 @@
         }
     }
 
-    /// Returns [`true`] if this is a multicast address (ff00::/8).
+    /// Returns [`true`] if this is a multicast address (`ff00::/8`).
     ///
     /// This property is defined by [IETF RFC 4291].
     ///
     /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
-    /// [`true`]: ../../std/primitive.bool.html
     ///
-    pub fn is_multicast(&self) -> bool {
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false);
+    /// ```
+    #[inline]
+    pub const fn is_multicast(&self) -> bool {
         (self.segments()[0] & 0xff00) == 0xff00
     }
 
-    /// Converts this address to an [IPv4 address]. Returns [`None`] if this address is
-    /// neither IPv4-compatible or IPv4-mapped.
+    /// Converts this address to an [`IPv4` address] if it's an [IPv4-mapped] address,
+    /// as defined in [IETF RFC 4291 section 2.5.5.2], otherwise returns [`None`].
     ///
-    /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
+    /// `::ffff:a.b.c.d` becomes `a.b.c.d`.
+    /// All addresses *not* starting with `::ffff` will return `None`.
     ///
-    /// [IPv4 address]: ../../std/net/struct.Ipv4Addr.html
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// [`IPv4` address]: Ipv4Addr
+    /// [IPv4-mapped]: Ipv6Addr
+    /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
     ///
-    pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
-        match self.segments() {
-            [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
-                Some(Ipv4Addr::new((g >> 8) as u8, g as u8, (h >> 8) as u8, h as u8))
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    ///
+    /// use std::net::{Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(),
+    ///            Some(Ipv4Addr::new(192, 10, 2, 255)));
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
+    /// ```
+    #[inline]
+    pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
+        match self.octets() {
+            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
+                Some(Ipv4Addr::new(a, b, c, d))
             }
             _ => None,
         }
     }
 
+    /// Converts this address to an [`IPv4` address] if it is either
+    /// an [IPv4-compatible] address as defined in [IETF RFC 4291 section 2.5.5.1],
+    /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2],
+    /// otherwise returns [`None`].
+    ///
+    /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`
+    /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`.
+    ///
+    /// [IPv4 address]: Ipv4Addr
+    /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses
+    /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses
+    /// [IETF RFC 4291 section 2.5.5.1]: https://tools.ietf.org/html/rfc4291#section-2.5.5.1
+    /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{Ipv4Addr, Ipv6Addr};
+    ///
+    /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(),
+    ///            Some(Ipv4Addr::new(192, 10, 2, 255)));
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(),
+    ///            Some(Ipv4Addr::new(0, 0, 0, 1)));
+    /// ```
+    #[inline]
+    pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
+        if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() {
+            let [a, b] = ab.to_be_bytes();
+            let [c, d] = cd.to_be_bytes();
+            Some(Ipv4Addr::new(a, b, c, d))
+        } else {
+            None
+        }
+    }
+
+    /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it
+    /// returns self wrapped in an `IpAddr::V6`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(ip)]
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
+    /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
+    /// ```
+    #[inline]
+    pub const fn to_canonical(&self) -> IpAddr {
+        if let Some(mapped) = self.to_ipv4_mapped() {
+            return IpAddr::V4(mapped);
+        }
+        IpAddr::V6(*self)
+    }
+
     /// Returns the sixteen eight-bit integers the IPv6 address consists of.
     ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(),
+    ///            [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
+    /// ```
+    #[inline]
     pub const fn octets(&self) -> [u8; 16] {
         self.inner.s6_addr
     }
 }
 
+/// Write an Ipv6Addr, conforming to the canonical style described by
+/// [RFC 5952](https://tools.ietf.org/html/rfc5952).
 impl fmt::Display for Ipv6Addr {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        // Note: The calls to write should never fail, hence the unwraps in the function
-        // Long enough for the longest possible IPv6: 39
-        const IPV6_BUF_LEN: usize = 39;
-        let mut buf = [0u8; IPV6_BUF_LEN];
-        let mut buf_slice = &mut buf[..];
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        // If there are no alignment requirements, write out the IP address to
+        // f. Otherwise, write it to a local buffer, then use f.pad.
+        if f.precision().is_none() && f.width().is_none() {
+            let segments = self.segments();
 
-        match self.segments() {
-            // We need special cases for :: and ::1, otherwise they're formatted
-            // as ::0.0.0.[01]
-            [0, 0, 0, 0, 0, 0, 0, 0] => write!(buf_slice, "::").unwrap(),
-            [0, 0, 0, 0, 0, 0, 0, 1] => write!(buf_slice, "::1").unwrap(),
-            // Ipv4 Compatible address
-            [0, 0, 0, 0, 0, 0, g, h] => {
-                write!(
-                    buf_slice,
-                    "::{}.{}.{}.{}",
-                    (g >> 8) as u8,
-                    g as u8,
-                    (h >> 8) as u8,
-                    h as u8
-                )
-                .unwrap();
-            }
-            // Ipv4-Mapped address
-            [0, 0, 0, 0, 0, 0xffff, g, h] => {
-                write!(
-                    buf_slice,
-                    "::ffff:{}.{}.{}.{}",
-                    (g >> 8) as u8,
-                    g as u8,
-                    (h >> 8) as u8,
-                    h as u8
-                )
-                .unwrap();
-            }
-            _ => {
-                fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
-                    let mut longest_span_len = 0;
-                    let mut longest_span_at = 0;
-                    let mut cur_span_len = 0;
-                    let mut cur_span_at = 0;
+            // Special case for :: and ::1; otherwise they get written with the
+            // IPv4 formatter
+            if self.is_unspecified() {
+                f.write_str("::")
+            } else if self.is_loopback() {
+                f.write_str("::1")
+            } else if let Some(ipv4) = self.to_ipv4() {
+                match segments[5] {
+                    // IPv4 Compatible address
+                    0 => write!(f, "::{}", ipv4),
+                    // IPv4 Mapped address
+                    0xffff => write!(f, "::ffff:{}", ipv4),
+                    _ => unreachable!(),
+                }
+            } else {
+                #[derive(Copy, Clone, Default)]
+                struct Span {
+                    start: usize,
+                    len: usize,
+                }
 
-                    for i in 0..8 {
-                        if segments[i] == 0 {
-                            if cur_span_len == 0 {
-                                cur_span_at = i;
+                // Find the inner 0 span
+                let zeroes = {
+                    let mut longest = Span::default();
+                    let mut current = Span::default();
+
+                    for (i, &segment) in segments.iter().enumerate() {
+                        if segment == 0 {
+                            if current.len == 0 {
+                                current.start = i;
                             }
 
-                            cur_span_len += 1;
+                            current.len += 1;
 
-                            if cur_span_len > longest_span_len {
-                                longest_span_len = cur_span_len;
-                                longest_span_at = cur_span_at;
+                            if current.len > longest.len {
+                                longest = current;
                             }
                         } else {
-                            cur_span_len = 0;
-                            cur_span_at = 0;
+                            current = Span::default();
                         }
                     }
 
-                    (longest_span_at, longest_span_len)
+                    longest
+                };
+
+                /// Write a colon-separated part of the address
+                #[inline]
+                fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result {
+                    if let Some((first, tail)) = chunk.split_first() {
+                        write!(f, "{:x}", first)?;
+                        for segment in tail {
+                            f.write_char(':')?;
+                            write!(f, "{:x}", segment)?;
+                        }
+                    }
+                    Ok(())
                 }
 
-                let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
-
-                if zeros_len > 1 {
-                    fn fmt_subslice(segments: &[u16], buf: &mut &mut [u8]) {
-                        if !segments.is_empty() {
-                            write!(*buf, "{:x}", segments[0]).unwrap();
-                            for &seg in &segments[1..] {
-                                write!(*buf, ":{:x}", seg).unwrap();
-                            }
-                        }
-                    }
-
-                    fmt_subslice(&self.segments()[..zeros_at], &mut buf_slice);
-                    write!(buf_slice, "::").unwrap();
-                    fmt_subslice(&self.segments()[zeros_at + zeros_len..], &mut buf_slice);
+                if zeroes.len > 1 {
+                    fmt_subslice(f, &segments[..zeroes.start])?;
+                    f.write_str("::")?;
+                    fmt_subslice(f, &segments[zeroes.start + zeroes.len..])
                 } else {
-                    let &[a, b, c, d, e, f, g, h] = &self.segments();
-                    write!(
-                        buf_slice,
-                        "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
-                        a, b, c, d, e, f, g, h
-                    )
-                    .unwrap();
+                    fmt_subslice(f, &segments)
                 }
             }
+        } else {
+            // Slow path: write the address to a local buffer, the use f.pad.
+            // Defined recursively by using the fast path to write to the
+            // buffer.
+
+            // This is the largest possible size of an IPv6 address
+            const IPV6_BUF_LEN: usize = (4 * 8) + 7;
+            let mut buf = [0u8; IPV6_BUF_LEN];
+            let mut buf_slice = &mut buf[..];
+
+            // Note: This call to write should never fail, so unwrap is okay.
+            write!(buf_slice, "{}", self).unwrap();
+            let len = IPV6_BUF_LEN - buf_slice.len();
+
+            // This is safe because we know exactly what can be in this buffer
+            let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
+            f.pad(buf)
         }
-        let len = IPV6_BUF_LEN - buf_slice.len();
-        // This is safe because we know exactly what can be in this buffer
-        let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
-        fmt.pad(buf)
     }
 }
 
@@ -1029,18 +1668,21 @@
 }
 
 impl Clone for Ipv6Addr {
+    #[inline]
     fn clone(&self) -> Ipv6Addr {
         *self
     }
 }
 
 impl PartialEq for Ipv6Addr {
+    #[inline]
     fn eq(&self, other: &Ipv6Addr) -> bool {
         self.inner.s6_addr == other.inner.s6_addr
     }
 }
 
 impl PartialEq<IpAddr> for Ipv6Addr {
+    #[inline]
     fn eq(&self, other: &IpAddr) -> bool {
         match other {
             IpAddr::V4(_) => false,
@@ -1050,6 +1692,7 @@
 }
 
 impl PartialEq<Ipv6Addr> for IpAddr {
+    #[inline]
     fn eq(&self, other: &Ipv6Addr) -> bool {
         match self {
             IpAddr::V4(_) => false,
@@ -1061,18 +1704,21 @@
 impl Eq for Ipv6Addr {}
 
 impl hash::Hash for Ipv6Addr {
+    #[inline]
     fn hash<H: hash::Hasher>(&self, s: &mut H) {
         self.inner.s6_addr.hash(s)
     }
 }
 
 impl PartialOrd for Ipv6Addr {
+    #[inline]
     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
         Some(self.cmp(other))
     }
 }
 
 impl PartialOrd<Ipv6Addr> for IpAddr {
+    #[inline]
     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
         match self {
             IpAddr::V4(_) => Some(Ordering::Less),
@@ -1082,6 +1728,7 @@
 }
 
 impl PartialOrd<IpAddr> for Ipv6Addr {
+    #[inline]
     fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
         match other {
             IpAddr::V4(_) => Some(Ordering::Greater),
@@ -1091,17 +1738,20 @@
 }
 
 impl Ord for Ipv6Addr {
+    #[inline]
     fn cmp(&self, other: &Ipv6Addr) -> Ordering {
         self.segments().cmp(&other.segments())
     }
 }
 
 impl AsInner<c::in6_addr> for Ipv6Addr {
+    #[inline]
     fn as_inner(&self) -> &c::in6_addr {
         &self.inner
     }
 }
 impl FromInner<c::in6_addr> for Ipv6Addr {
+    #[inline]
     fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
         Ipv6Addr { inner: addr }
     }
@@ -1110,6 +1760,18 @@
 impl From<Ipv6Addr> for u128 {
     /// Convert an `Ipv6Addr` into a host byte order `u128`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::new(
+    ///     0x1020, 0x3040, 0x5060, 0x7080,
+    ///     0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
+    /// );
+    /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr));
+    /// ```
+    #[inline]
     fn from(ip: Ipv6Addr) -> u128 {
         let ip = ip.octets();
         u128::from_be_bytes(ip)
@@ -1119,12 +1781,48 @@
 impl From<u128> for Ipv6Addr {
     /// Convert a host byte order `u128` into an `Ipv6Addr`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128);
+    /// assert_eq!(
+    ///     Ipv6Addr::new(
+    ///         0x1020, 0x3040, 0x5060, 0x7080,
+    ///         0x90A0, 0xB0C0, 0xD0E0, 0xF00D,
+    ///     ),
+    ///     addr);
+    /// ```
+    #[inline]
     fn from(ip: u128) -> Ipv6Addr {
         Ipv6Addr::from(ip.to_be_bytes())
     }
 }
 
 impl From<[u8; 16]> for Ipv6Addr {
+    /// Creates an `Ipv6Addr` from a sixteen element byte array.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::from([
+    ///     25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
+    ///     17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
+    /// ]);
+    /// assert_eq!(
+    ///     Ipv6Addr::new(
+    ///         0x1918, 0x1716,
+    ///         0x1514, 0x1312,
+    ///         0x1110, 0x0f0e,
+    ///         0x0d0c, 0x0b0a
+    ///     ),
+    ///     addr
+    /// );
+    /// ```
+    #[inline]
     fn from(octets: [u8; 16]) -> Ipv6Addr {
         let inner = c::in6_addr { s6_addr: octets };
         Ipv6Addr::from_inner(inner)
@@ -1134,6 +1832,26 @@
 impl From<[u16; 8]> for Ipv6Addr {
     /// Creates an `Ipv6Addr` from an eight element 16-bit array.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::Ipv6Addr;
+    ///
+    /// let addr = Ipv6Addr::from([
+    ///     525u16, 524u16, 523u16, 522u16,
+    ///     521u16, 520u16, 519u16, 518u16,
+    /// ]);
+    /// assert_eq!(
+    ///     Ipv6Addr::new(
+    ///         0x20d, 0x20c,
+    ///         0x20b, 0x20a,
+    ///         0x209, 0x208,
+    ///         0x207, 0x206
+    ///     ),
+    ///     addr
+    /// );
+    /// ```
+    #[inline]
     fn from(segments: [u16; 8]) -> Ipv6Addr {
         let [a, b, c, d, e, f, g, h] = segments;
         Ipv6Addr::new(a, b, c, d, e, f, g, h)
@@ -1144,6 +1862,26 @@
 impl From<[u8; 16]> for IpAddr {
     /// Creates an `IpAddr::V6` from a sixteen element byte array.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv6Addr};
+    ///
+    /// let addr = IpAddr::from([
+    ///     25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8,
+    ///     17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8,
+    /// ]);
+    /// assert_eq!(
+    ///     IpAddr::V6(Ipv6Addr::new(
+    ///         0x1918, 0x1716,
+    ///         0x1514, 0x1312,
+    ///         0x1110, 0x0f0e,
+    ///         0x0d0c, 0x0b0a
+    ///     )),
+    ///     addr
+    /// );
+    /// ```
+    #[inline]
     fn from(octets: [u8; 16]) -> IpAddr {
         IpAddr::V6(Ipv6Addr::from(octets))
     }
@@ -1152,6 +1890,26 @@
 impl From<[u16; 8]> for IpAddr {
     /// Creates an `IpAddr::V6` from an eight element 16-bit array.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::net::{IpAddr, Ipv6Addr};
+    ///
+    /// let addr = IpAddr::from([
+    ///     525u16, 524u16, 523u16, 522u16,
+    ///     521u16, 520u16, 519u16, 518u16,
+    /// ]);
+    /// assert_eq!(
+    ///     IpAddr::V6(Ipv6Addr::new(
+    ///         0x20d, 0x20c,
+    ///         0x20b, 0x20a,
+    ///         0x209, 0x208,
+    ///         0x207, 0x206
+    ///     )),
+    ///     addr
+    /// );
+    /// ```
+    #[inline]
     fn from(segments: [u16; 8]) -> IpAddr {
         IpAddr::V6(Ipv6Addr::from(segments))
     }
diff --git a/sgx_tstd/src/net/mod.rs b/sgx_tstd/src/net/mod.rs
index 4c9b045..18b19f2 100644
--- a/sgx_tstd/src/net/mod.rs
+++ b/sgx_tstd/src/net/mod.rs
@@ -20,42 +20,54 @@
 //! This module provides networking functionality for the Transmission Control and User
 //! Datagram Protocols, as well as types for IP and socket addresses.
 //!
+//! # Organization
+//!
+//! * [`TcpListener`] and [`TcpStream`] provide functionality for communication over TCP
+//! * [`UdpSocket`] provides functionality for communication over UDP
+//! * [`IpAddr`] represents IP addresses of either IPv4 or IPv6; [`Ipv4Addr`] and
+//!   [`Ipv6Addr`] are respectively IPv4 and IPv6 addresses
+//! * [`SocketAddr`] represents socket addresses of either IPv4 or IPv6; [`SocketAddrV4`]
+//!   and [`SocketAddrV6`] are respectively IPv4 and IPv6 socket addresses
+//! * [`ToSocketAddrs`] is a trait that used for generic address resolution when interacting
+//!   with networking objects like [`TcpListener`], [`TcpStream`] or [`UdpSocket`]
+//! * Other types are return or parameter types for various methods in this module
 
 use crate::io::{self, Error, ErrorKind};
 
-pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
+pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
+pub use self::parser::AddrParseError;
 #[cfg(feature = "net")]
-pub use self::tcp::TcpStream;
-#[cfg(feature = "net")]
-pub use self::tcp::TcpListener;
+pub use self::tcp::{Incoming, TcpListener, TcpStream};
 #[cfg(feature = "net")]
 pub use self::udp::UdpSocket;
-pub use self::parser::AddrParseError;
 
-mod ip;
 mod addr;
+mod ip;
 mod parser;
 #[cfg(feature = "net")]
 mod tcp;
 #[cfg(feature = "net")]
 mod udp;
 
-/// Possible values which can be passed to the [`shutdown`] method of
+/// Possible values which can be passed to the [`TcpStream::shutdown`] method.
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub enum Shutdown {
     /// The reading portion of the [`TcpStream`] should be shut down.
     ///
-    /// All currently blocked and future [reads] will return [`Ok(0)`].
+    /// All currently blocked and future [reads] will return [`Ok`]`(0)`.
     ///
+    /// [reads]: crate::io::Read
     Read,
     /// The writing portion of the [`TcpStream`] should be shut down.
     ///
     /// All currently blocked and future [writes] will return an error.
     ///
+    /// [writes]: crate::io::Write
     Write,
     /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
     ///
+    /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
     Both,
 }
 
@@ -84,6 +96,6 @@
         }
     }
     Err(last_err.unwrap_or_else(|| {
-        Error::new(ErrorKind::InvalidInput, "could not resolve to any addresses")
+        Error::new_const(ErrorKind::InvalidInput, &"could not resolve to any addresses")
     }))
 }
\ No newline at end of file
diff --git a/sgx_tstd/src/net/parser.rs b/sgx_tstd/src/net/parser.rs
index b342fcd..3bc00f5 100644
--- a/sgx_tstd/src/net/parser.rs
+++ b/sgx_tstd/src/net/parser.rs
@@ -15,255 +15,267 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::fmt;
-use core::str::FromStr;
+//! A private parser implementation of IPv4, IPv6, and socket addresses.
+//!
+//! This module is "publicly exported" through the `FromStr` implementations
+//! below.
+
+use crate::convert::TryInto as _;
 use crate::error::Error;
+use crate::fmt;
 use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
+use crate::str::FromStr;
+
+trait ReadNumberHelper: crate::marker::Sized {
+    const ZERO: Self;
+    fn checked_mul(&self, other: u32) -> Option<Self>;
+    fn checked_add(&self, other: u32) -> Option<Self>;
+}
+
+macro_rules! impl_helper {
+    ($($t:ty)*) => ($(impl ReadNumberHelper for $t {
+        const ZERO: Self = 0;
+        #[inline]
+        fn checked_mul(&self, other: u32) -> Option<Self> {
+            Self::checked_mul(*self, other.try_into().ok()?)
+        }
+        #[inline]
+        fn checked_add(&self, other: u32) -> Option<Self> {
+            Self::checked_add(*self, other.try_into().ok()?)
+        }
+    })*)
+}
+
+impl_helper! { u8 u16 u32 }
 
 struct Parser<'a> {
-    // parsing as ASCII, so can use byte array
-    s: &'a [u8],
-    pos: usize,
+    // Parsing as ASCII, so can use byte array.
+    state: &'a [u8],
 }
 
 impl<'a> Parser<'a> {
-    fn new(s: &'a str) -> Parser<'a> {
-        Parser { s: s.as_bytes(), pos: 0 }
+    fn new(input: &'a str) -> Parser<'a> {
+        Parser { state: input.as_bytes() }
     }
 
-    fn is_eof(&self) -> bool {
-        self.pos == self.s.len()
-    }
-
-    // Commit only if parser returns Some
-    fn read_atomically<T, F>(&mut self, cb: F) -> Option<T>
+    /// Run a parser, and restore the pre-parse state if it fails.
+    fn read_atomically<T, F>(&mut self, inner: F) -> Option<T>
     where
         F: FnOnce(&mut Parser<'_>) -> Option<T>,
     {
-        let pos = self.pos;
-        let r = cb(self);
-        if r.is_none() {
-            self.pos = pos;
+        let state = self.state;
+        let result = inner(self);
+        if result.is_none() {
+            self.state = state;
         }
-        r
+        result
     }
 
-    // Commit only if parser read till EOF
-    fn read_till_eof<T, F>(&mut self, cb: F) -> Option<T>
+    /// Run a parser, but fail if the entire input wasn't consumed.
+    /// Doesn't run atomically.
+    fn parse_with<T, F>(&mut self, inner: F) -> Result<T, AddrParseError>
     where
         F: FnOnce(&mut Parser<'_>) -> Option<T>,
     {
-        self.read_atomically(move |p| cb(p).filter(|_| p.is_eof()))
+        let result = inner(self);
+        if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(()))
     }
 
-    // Apply 3 parsers sequentially
-    fn read_seq_3<A, B, C, PA, PB, PC>(&mut self, pa: PA, pb: PB, pc: PC) -> Option<(A, B, C)>
+    /// Peek the next character from the input
+    fn peek_char(&self) -> Option<char> {
+        self.state.first().map(|&b| char::from(b))
+    }
+
+    /// Read the next character from the input
+    fn read_char(&mut self) -> Option<char> {
+        self.state.split_first().map(|(&b, tail)| {
+            self.state = tail;
+            char::from(b)
+        })
+    }
+
+    #[must_use]
+    /// Read the next character from the input if it matches the target.
+    fn read_given_char(&mut self, target: char) -> Option<()> {
+        self.read_atomically(|p| {
+            p.read_char().and_then(|c| if c == target { Some(()) } else { None })
+        })
+    }
+
+    /// Helper for reading separators in an indexed loop. Reads the separator
+    /// character iff index > 0, then runs the parser. When used in a loop,
+    /// the separator character will only be read on index > 0 (see
+    /// read_ipv4_addr for an example)
+    fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T>
     where
-        PA: FnOnce(&mut Parser<'_>) -> Option<A>,
-        PB: FnOnce(&mut Parser<'_>) -> Option<B>,
-        PC: FnOnce(&mut Parser<'_>) -> Option<C>,
+        F: FnOnce(&mut Parser<'_>) -> Option<T>,
     {
         self.read_atomically(move |p| {
-            let a = pa(p);
-            let b = if a.is_some() { pb(p) } else { None };
-            let c = if b.is_some() { pc(p) } else { None };
-            match (a, b, c) {
-                (Some(a), Some(b), Some(c)) => Some((a, b, c)),
-                _ => None,
+            if index > 0 {
+                p.read_given_char(sep)?;
             }
+            inner(p)
         })
     }
 
-    // Read next char
-    fn read_char(&mut self) -> Option<char> {
-        if self.is_eof() {
-            None
-        } else {
-            let r = self.s[self.pos] as char;
-            self.pos += 1;
-            Some(r)
-        }
-    }
+    // Read a number off the front of the input in the given radix, stopping
+    // at the first non-digit character or eof. Fails if the number has more
+    // digits than max_digits or if there is no number.
+    fn read_number<T: ReadNumberHelper>(
+        &mut self,
+        radix: u32,
+        max_digits: Option<usize>,
+    ) -> Option<T> {
+        self.read_atomically(move |p| {
+            let mut result = T::ZERO;
+            let mut digit_count = 0;
 
-    // Return char and advance iff next char is equal to requested
-    fn read_given_char(&mut self, c: char) -> Option<char> {
-        self.read_atomically(|p| match p.read_char() {
-            Some(next) if next == c => Some(next),
-            _ => None,
+            while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
+                result = result.checked_mul(radix)?;
+                result = result.checked_add(digit)?;
+                digit_count += 1;
+                if let Some(max_digits) = max_digits {
+                    if digit_count > max_digits {
+                        return None;
+                    }
+                }
+            }
+
+            if digit_count == 0 { None } else { Some(result) }
         })
     }
 
-    // Read digit
-    fn read_digit(&mut self, radix: u8) -> Option<u8> {
-        fn parse_digit(c: char, radix: u8) -> Option<u8> {
-            let c = c as u8;
-            // assuming radix is either 10 or 16
-            if c >= b'0' && c <= b'9' {
-                Some(c - b'0')
-            } else if radix > 10 && c >= b'a' && c < b'a' + (radix - 10) {
-                Some(c - b'a' + 10)
-            } else if radix > 10 && c >= b'A' && c < b'A' + (radix - 10) {
-                Some(c - b'A' + 10)
-            } else {
-                None
-            }
-        }
-
-        self.read_atomically(|p| p.read_char().and_then(|c| parse_digit(c, radix)))
-    }
-
-    fn read_number_impl(&mut self, radix: u8, max_digits: u32, upto: u32) -> Option<u32> {
-        let mut r = 0;
-        let mut digit_count = 0;
-        loop {
-            match self.read_digit(radix) {
-                Some(d) => {
-                    r = r * (radix as u32) + (d as u32);
-                    digit_count += 1;
-                    if digit_count > max_digits || r >= upto {
-                        return None;
-                    }
-                }
-                None => {
-                    if digit_count == 0 {
-                        return None;
-                    } else {
-                        return Some(r);
-                    }
-                }
-            };
-        }
-    }
-
-    // Read number, failing if max_digits of number value exceeded
-    fn read_number(&mut self, radix: u8, max_digits: u32, upto: u32) -> Option<u32> {
-        self.read_atomically(|p| p.read_number_impl(radix, max_digits, upto))
-    }
-
-    fn read_ipv4_addr_impl(&mut self) -> Option<Ipv4Addr> {
-        let mut bs = [0; 4];
-        let mut i = 0;
-        while i < 4 {
-            if i != 0 && self.read_given_char('.').is_none() {
-                return None;
-            }
-
-            bs[i] = self.read_number(10, 3, 0x100).map(|n| n as u8)?;
-            i += 1;
-        }
-        Some(Ipv4Addr::new(bs[0], bs[1], bs[2], bs[3]))
-    }
-
-    // Read IPv4 address
+    /// Read an IPv4 address.
     fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {
-        self.read_atomically(|p| p.read_ipv4_addr_impl())
+        self.read_atomically(|p| {
+            let mut groups = [0; 4];
+
+            for (i, slot) in groups.iter_mut().enumerate() {
+                *slot = p.read_separator('.', i, |p| {
+                    // Disallow octal number in IP string.
+                    // https://tools.ietf.org/html/rfc6943#section-3.1.1
+                    match (p.peek_char(), p.read_number(10, None)) {
+                        (Some('0'), Some(number)) if number != 0 => None,
+                        (_, number) => number,
+                    }
+                })?;
+            }
+
+            Some(groups.into())
+        })
     }
 
-    fn read_ipv6_addr_impl(&mut self) -> Option<Ipv6Addr> {
-        fn ipv6_addr_from_head_tail(head: &[u16], tail: &[u16]) -> Ipv6Addr {
-            assert!(head.len() + tail.len() <= 8);
-            let mut gs = [0; 8];
-            gs[..head.len()].copy_from_slice(head);
-            gs[(8 - tail.len())..8].copy_from_slice(tail);
-            Ipv6Addr::new(gs[0], gs[1], gs[2], gs[3], gs[4], gs[5], gs[6], gs[7])
-        }
+    /// Read an IPv6 Address.
+    fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
+        /// Read a chunk of an IPv6 address into `groups`. Returns the number
+        /// of groups read, along with a bool indicating if an embedded
+        /// trailing IPv4 address was read. Specifically, read a series of
+        /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional
+        /// trailing embedded IPv4 address.
+        fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) {
+            let limit = groups.len();
 
-        fn read_groups(p: &mut Parser<'_>, groups: &mut [u16; 8], limit: usize) -> (usize, bool) {
-            let mut i = 0;
-            while i < limit {
+            for (i, slot) in groups.iter_mut().enumerate() {
+                // Try to read a trailing embedded IPv4 address. There must be
+                // at least two groups left.
                 if i < limit - 1 {
-                    let ipv4 = p.read_atomically(|p| {
-                        if i == 0 || p.read_given_char(':').is_some() {
-                            p.read_ipv4_addr()
-                        } else {
-                            None
-                        }
-                    });
+                    let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr());
+
                     if let Some(v4_addr) = ipv4 {
-                        let octets = v4_addr.octets();
-                        groups[i + 0] = ((octets[0] as u16) << 8) | (octets[1] as u16);
-                        groups[i + 1] = ((octets[2] as u16) << 8) | (octets[3] as u16);
+                        let [one, two, three, four] = v4_addr.octets();
+                        groups[i] = u16::from_be_bytes([one, two]);
+                        groups[i + 1] = u16::from_be_bytes([three, four]);
                         return (i + 2, true);
                     }
                 }
 
-                let group = p.read_atomically(|p| {
-                    if i == 0 || p.read_given_char(':').is_some() {
-                        p.read_number(16, 4, 0x10000).map(|n| n as u16)
-                    } else {
-                        None
-                    }
-                });
+                let group = p.read_separator(':', i, |p| p.read_number(16, Some(4)));
+
                 match group {
-                    Some(g) => groups[i] = g,
+                    Some(g) => *slot = g,
                     None => return (i, false),
                 }
-                i += 1;
             }
-            (i, false)
+            (groups.len(), false)
         }
 
-        let mut head = [0; 8];
-        let (head_size, head_ipv4) = read_groups(self, &mut head, 8);
+        self.read_atomically(|p| {
+            // Read the front part of the address; either the whole thing, or up
+            // to the first ::
+            let mut head = [0; 8];
+            let (head_size, head_ipv4) = read_groups(p, &mut head);
 
-        if head_size == 8 {
-            return Some(Ipv6Addr::new(
-                head[0], head[1], head[2], head[3], head[4], head[5], head[6], head[7],
-            ));
-        }
+            if head_size == 8 {
+                return Some(head.into());
+            }
 
-        // IPv4 part is not allowed before `::`
-        if head_ipv4 {
-            return None;
-        }
+            // IPv4 part is not allowed before `::`
+            if head_ipv4 {
+                return None;
+            }
 
-        // read `::` if previous code parsed less than 8 groups
-        if self.read_given_char(':').is_none() || self.read_given_char(':').is_none() {
-            return None;
-        }
+            // Read `::` if previous code parsed less than 8 groups.
+            // `::` indicates one or more groups of 16 bits of zeros.
+            p.read_given_char(':')?;
+            p.read_given_char(':')?;
 
-        let mut tail = [0; 8];
-        // `::` indicates one or more groups of 16 bits of zeros
-        let limit = 8 - (head_size + 1);
-        let (tail_size, _) = read_groups(self, &mut tail, limit);
-        Some(ipv6_addr_from_head_tail(&head[..head_size], &tail[..tail_size]))
+            // Read the back part of the address. The :: must contain at least one
+            // set of zeroes, so our max length is 7.
+            let mut tail = [0; 7];
+            let limit = 8 - (head_size + 1);
+            let (tail_size, _) = read_groups(p, &mut tail[..limit]);
+
+            // Concat the head and tail of the IP address
+            head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]);
+
+            Some(head.into())
+        })
     }
 
-    fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
-        self.read_atomically(|p| p.read_ipv6_addr_impl())
-    }
-
+    /// Read an IP Address, either IPv4 or IPv6.
     fn read_ip_addr(&mut self) -> Option<IpAddr> {
-        self.read_ipv4_addr().map(IpAddr::V4).or_else(|| self.read_ipv6_addr().map(IpAddr::V6))
+        self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6))
     }
 
+    /// Read a `:` followed by a port in base 10.
+    fn read_port(&mut self) -> Option<u16> {
+        self.read_atomically(|p| {
+            p.read_given_char(':')?;
+            p.read_number(10, None)
+        })
+    }
+
+    /// Read a `%` followed by a scope ID in base 10.
+    fn read_scope_id(&mut self) -> Option<u32> {
+        self.read_atomically(|p| {
+            p.read_given_char('%')?;
+            p.read_number(10, None)
+        })
+    }
+
+    /// Read an IPv4 address with a port.
     fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
-        let ip_addr = |p: &mut Parser<'_>| p.read_ipv4_addr();
-        let colon = |p: &mut Parser<'_>| p.read_given_char(':');
-        let port = |p: &mut Parser<'_>| p.read_number(10, 5, 0x10000).map(|n| n as u16);
-
-        self.read_seq_3(ip_addr, colon, port).map(|t| {
-            let (ip, _, port): (Ipv4Addr, char, u16) = t;
-            SocketAddrV4::new(ip, port)
+        self.read_atomically(|p| {
+            let ip = p.read_ipv4_addr()?;
+            let port = p.read_port()?;
+            Some(SocketAddrV4::new(ip, port))
         })
     }
 
+    /// Read an IPv6 address with a port.
     fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> {
-        let ip_addr = |p: &mut Parser<'_>| {
-            let open_br = |p: &mut Parser<'_>| p.read_given_char('[');
-            let ip_addr = |p: &mut Parser<'_>| p.read_ipv6_addr();
-            let clos_br = |p: &mut Parser<'_>| p.read_given_char(']');
-            p.read_seq_3(open_br, ip_addr, clos_br).map(|t| t.1)
-        };
-        let colon = |p: &mut Parser<'_>| p.read_given_char(':');
-        let port = |p: &mut Parser<'_>| p.read_number(10, 5, 0x10000).map(|n| n as u16);
+        self.read_atomically(|p| {
+            p.read_given_char('[')?;
+            let ip = p.read_ipv6_addr()?;
+            let scope_id = p.read_scope_id().unwrap_or(0);
+            p.read_given_char(']')?;
 
-        self.read_seq_3(ip_addr, colon, port).map(|t| {
-            let (ip, _, port): (Ipv6Addr, char, u16) = t;
-            SocketAddrV6::new(ip, port, 0, 0)
+            let port = p.read_port()?;
+            Some(SocketAddrV6::new(ip, port, 0, scope_id))
         })
     }
 
+    /// Read an IP address with a port
     fn read_socket_addr(&mut self) -> Option<SocketAddr> {
         self.read_socket_addr_v4()
             .map(SocketAddr::V4)
@@ -274,60 +286,42 @@
 impl FromStr for IpAddr {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_ip_addr()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_ip_addr())
     }
 }
 
 impl FromStr for Ipv4Addr {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_ipv4_addr()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_ipv4_addr())
     }
 }
 
 impl FromStr for Ipv6Addr {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_ipv6_addr()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_ipv6_addr())
     }
 }
 
 impl FromStr for SocketAddrV4 {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_socket_addr_v4()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_socket_addr_v4())
     }
 }
 
 impl FromStr for SocketAddrV6 {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_socket_addr_v6()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_socket_addr_v6())
     }
 }
 
 impl FromStr for SocketAddr {
     type Err = AddrParseError;
     fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
-        match Parser::new(s).read_till_eof(|p| p.read_socket_addr()) {
-            Some(s) => Ok(s),
-            None => Err(AddrParseError(())),
-        }
+        Parser::new(s).parse_with(|p| p.read_socket_addr())
     }
 }
 
@@ -355,24 +349,18 @@
 /// // No problem, the `panic!` message has disappeared.
 /// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic");
 /// ```
-///
-/// [`FromStr`]: ../../std/str/trait.FromStr.html
-/// [`IpAddr`]: ../../std/net/enum.IpAddr.html
-/// [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
-/// [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
-/// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
-/// [`SocketAddrV4`]: ../../std/net/struct.SocketAddrV4.html
-/// [`SocketAddrV6`]: ../../std/net/struct.SocketAddrV6.html
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct AddrParseError(());
 
 impl fmt::Display for AddrParseError {
+    #[allow(deprecated, deprecated_in_future)]
     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
         fmt.write_str(self.description())
     }
 }
 
 impl Error for AddrParseError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "invalid IP address syntax"
     }
diff --git a/sgx_tstd/src/net/tcp.rs b/sgx_tstd/src/net/tcp.rs
index 0aadd1e..0403d57 100644
--- a/sgx_tstd/src/net/tcp.rs
+++ b/sgx_tstd/src/net/tcp.rs
@@ -17,14 +17,15 @@
 
 use crate::io::prelude::*;
 
-use sgx_trts::libc::c_int;
-use core::fmt;
+use crate::fmt;
 use crate::io::{self, Initializer, IoSlice, IoSliceMut};
 use crate::net::{Shutdown, SocketAddr, ToSocketAddrs};
 use crate::sys_common::net as net_imp;
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::time::Duration;
 
+use sgx_libc::c_int;
+
 /// A TCP stream between a local and a remote socket.
 ///
 /// After creating a `TcpStream` by either [`connect`]ing to a remote host or
@@ -37,14 +38,27 @@
 ///
 /// The Transmission Control Protocol is specified in [IETF RFC 793].
 ///
-/// [`accept`]: ../../std/net/struct.TcpListener.html#method.accept
-/// [`connect`]: #method.connect
+/// [`accept`]: TcpListener::accept
+/// [`connect`]: TcpStream::connect
 /// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
-/// [reading]: ../../std/io/trait.Read.html
-/// [`shutdown`]: #method.shutdown
-/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
-/// [writing]: ../../std/io/trait.Write.html
+/// [reading]: Read
+/// [`shutdown`]: TcpStream::shutdown
+/// [writing]: Write
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::io::prelude::*;
+/// use std::net::TcpStream;
+///
+/// fn main() -> std::io::Result<()> {
+///     let mut stream = TcpStream::connect("127.0.0.1:34254")?;
+///
+///     stream.write(&[1])?;
+///     stream.read(&mut [0; 128])?;
+///     Ok(())
+/// } // the stream is closed here
+/// ```
 pub struct TcpStream(net_imp::TcpStream);
 
 /// A TCP socket server, listening for connections.
@@ -57,22 +71,37 @@
 ///
 /// The Transmission Control Protocol is specified in [IETF RFC 793].
 ///
-/// [`accept`]: #method.accept
-/// [`bind`]: #method.bind
+/// [`accept`]: TcpListener::accept
+/// [`bind`]: TcpListener::bind
 /// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
-/// [`Incoming`]: ../../std/net/struct.Incoming.html
-/// [`TcpListener::incoming`]: #method.incoming
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::net::{TcpListener, TcpStream};
+///
+/// fn handle_client(stream: TcpStream) {
+///     // ...
+/// }
+///
+/// fn main() -> std::io::Result<()> {
+///     let listener = TcpListener::bind("127.0.0.1:80")?;
+///
+///     // accept connections and process them serially
+///     for stream in listener.incoming() {
+///         handle_client(stream?);
+///     }
+///     Ok(())
+/// }
+/// ```
 pub struct TcpListener(net_imp::TcpListener);
 
 /// An iterator that infinitely [`accept`]s connections on a [`TcpListener`].
 ///
-/// This `struct` is created by the [`incoming`] method on [`TcpListener`].
+/// This `struct` is created by the [`TcpListener::incoming`] method.
 /// See its documentation for more.
 ///
-/// [`accept`]: ../../std/net/struct.TcpListener.html#method.accept
-/// [`incoming`]: ../../std/net/struct.TcpListener.html#method.incoming
-/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
+/// [`accept`]: TcpListener::accept
 #[derive(Debug)]
 pub struct Incoming<'a> {
     listener: &'a TcpListener,
@@ -91,14 +120,6 @@
         net_imp::TcpStream::new_v6().map(TcpStream)
     }
 
-    pub fn raw(&self) -> c_int {
-        self.0.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.0.into_raw()
-    }
-
     /// Opens a TCP connection to a remote host.
     ///
     /// `addr` is an address of the remote host. Anything which implements
@@ -110,6 +131,36 @@
     /// the addresses result in a successful connection, the error returned from
     /// the last connection attempt (the last address) is returned.
     ///
+    /// # Examples
+    ///
+    /// Open a TCP connection to `127.0.0.1:8080`:
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// if let Ok(stream) = TcpStream::connect("127.0.0.1:8080") {
+    ///     println!("Connected to the server!");
+    /// } else {
+    ///     println!("Couldn't connect to server...");
+    /// }
+    /// ```
+    ///
+    /// Open a TCP connection to `127.0.0.1:8080`. If the connection fails, open
+    /// a TCP connection to `127.0.0.1:8081`:
+    ///
+    /// ```no_run
+    /// use std::net::{SocketAddr, TcpStream};
+    ///
+    /// let addrs = [
+    ///     SocketAddr::from(([127, 0, 0, 1], 8080)),
+    ///     SocketAddr::from(([127, 0, 0, 1], 8081)),
+    /// ];
+    /// if let Ok(stream) = TcpStream::connect(&addrs[..]) {
+    ///     println!("Connected to the server!");
+    /// } else {
+    ///     println!("Couldn't connect to server...");
+    /// }
+    /// ```
     pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {
         super::each_addr(addr, net_imp::TcpStream::connect).map(TcpStream)
     }
@@ -140,8 +191,6 @@
     /// single system call. It instead calls `connect` in nonblocking mode and
     /// then uses an OS-specific mechanism to await the completion of the
     /// connection request.
-    ///
-    /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
     pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
         net_imp::TcpStream::connect_timeout(addr, timeout).map(TcpStream)
     }
@@ -164,12 +213,32 @@
 
     /// Returns the socket address of the remote peer of this TCP connection.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpStream};
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// assert_eq!(stream.peer_addr().unwrap(),
+    ///            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
+    /// ```
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
         self.0.peer_addr()
     }
 
     /// Returns the socket address of the local half of this TCP connection.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{IpAddr, Ipv4Addr, TcpStream};
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// assert_eq!(stream.local_addr().unwrap().ip(),
+    ///            IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
+    /// ```
     pub fn local_addr(&self) -> io::Result<SocketAddr> {
         self.0.socket_addr()
     }
@@ -180,8 +249,6 @@
     /// portions to return immediately with an appropriate value (see the
     /// documentation of [`Shutdown`]).
     ///
-    /// [`Shutdown`]: ../../std/net/enum.Shutdown.html
-    ///
     /// # Platform-specific behavior
     ///
     /// Calling this function multiple times may result in different behavior,
@@ -189,6 +256,15 @@
     /// return `Ok(())`, but on macOS, it will return `ErrorKind::NotConnected`.
     /// This may change in the future.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{Shutdown, TcpStream};
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.shutdown(Shutdown::Both).expect("shutdown call failed");
+    /// ```
     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
         self.0.shutdown(how)
     }
@@ -200,6 +276,15 @@
     /// data, and options set on one stream will be propagated to the other
     /// stream.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// let stream_clone = stream.try_clone().expect("clone failed...");
+    /// ```
     pub fn try_clone(&self) -> io::Result<TcpStream> {
         self.0.duplicate().map(TcpStream)
     }
@@ -216,13 +301,33 @@
     /// a result of setting this option. For example Unix typically returns an
     /// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
-    /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
-    /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
-    /// [`Duration`]: ../../std/time/struct.Duration.html
+    /// [`read`]: Read::read
+    /// [`WouldBlock`]: io::ErrorKind::WouldBlock
+    /// [`TimedOut`]: io::ErrorKind::TimedOut
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_read_timeout(None).expect("set_read_timeout call failed");
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::TcpStream;
+    /// use std::time::Duration;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
+    /// let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
+    /// let err = result.unwrap_err();
+    /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
+    /// ```
     pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
         self.0.set_read_timeout(dur)
     }
@@ -239,13 +344,33 @@
     /// as a result of setting this option. For example Unix typically returns
     /// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
-    /// [`Duration`]: ../../std/time/struct.Duration.html
-    /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
-    /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
+    /// [`write`]: Write::write
+    /// [`WouldBlock`]: io::ErrorKind::WouldBlock
+    /// [`TimedOut`]: io::ErrorKind::TimedOut
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_write_timeout(None).expect("set_write_timeout call failed");
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::TcpStream;
+    /// use std::time::Duration;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080").unwrap();
+    /// let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
+    /// let err = result.unwrap_err();
+    /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
+    /// ```
     pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
         self.0.set_write_timeout(dur)
     }
@@ -258,9 +383,18 @@
     ///
     /// Some platforms do not provide access to the current timeout.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
+    /// [`read`]: Read::read
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_read_timeout(None).expect("set_read_timeout call failed");
+    /// assert_eq!(stream.read_timeout().unwrap(), None);
+    /// ```
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
         self.0.read_timeout()
     }
@@ -273,9 +407,18 @@
     ///
     /// Some platforms do not provide access to the current timeout.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
+    /// [`write`]: Write::write
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_write_timeout(None).expect("set_write_timeout call failed");
+    /// assert_eq!(stream.write_timeout().unwrap(), None);
+    /// ```
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
         self.0.write_timeout()
     }
@@ -287,10 +430,65 @@
     /// Successive calls return the same data. This is accomplished by passing
     /// `MSG_PEEK` as a flag to the underlying `recv` system call.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8000")
+    ///                        .expect("couldn't bind to address");
+    /// let mut buf = [0; 10];
+    /// let len = stream.peek(&mut buf).expect("peek failed");
+    /// ```
     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
         self.0.peek(buf)
     }
 
+    /// Sets the value of the `SO_LINGER` option on this socket.
+    ///
+    /// This value controls how the socket is closed when data remains
+    /// to be sent. If `SO_LINGER` is set, the socket will remain open
+    /// for the specified duration as the system attempts to send pending data.
+    /// Otherwise, the system may close the socket immediately, or wait for a
+    /// default timeout.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(tcp_linger)]
+    ///
+    /// use std::net::TcpStream;
+    /// use std::time::Duration;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_linger(Some(Duration::from_secs(0))).expect("set_linger call failed");
+    /// ```
+    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
+        self.0.set_linger(linger)
+    }
+
+    /// Gets the value of the `SO_LINGER` option on this socket.
+    ///
+    /// For more information about this option, see [`TcpStream::set_linger`].
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(tcp_linger)]
+    ///
+    /// use std::net::TcpStream;
+    /// use std::time::Duration;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_linger(Some(Duration::from_secs(0))).expect("set_linger call failed");
+    /// assert_eq!(stream.linger().unwrap(), Some(Duration::from_secs(0)));
+    /// ```
+    pub fn linger(&self) -> io::Result<Option<Duration>> {
+        self.0.linger()
+    }
+
     /// Sets the value of the `TCP_NODELAY` option on this socket.
     ///
     /// If set, this option disables the Nagle algorithm. This means that
@@ -299,16 +497,33 @@
     /// sufficient amount to send out, thereby avoiding the frequent sending of
     /// small packets.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_nodelay(true).expect("set_nodelay call failed");
+    /// ```
     pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
         self.0.set_nodelay(nodelay)
     }
 
     /// Gets the value of the `TCP_NODELAY` option on this socket.
     ///
-    /// For more information about this option, see [`set_nodelay`][link].
+    /// For more information about this option, see [`TcpStream::set_nodelay`].
     ///
-    /// [link]: #method.set_nodelay
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_nodelay(true).expect("set_nodelay call failed");
+    /// assert_eq!(stream.nodelay().unwrap_or(false), true);
+    /// ```
     pub fn nodelay(&self) -> io::Result<bool> {
         self.0.nodelay()
     }
@@ -318,16 +533,33 @@
     /// This value sets the time-to-live field that is used in every packet sent
     /// from this socket.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_ttl(100).expect("set_ttl call failed");
+    /// ```
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
         self.0.set_ttl(ttl)
     }
 
     /// Gets the value of the `IP_TTL` option for this socket.
     ///
-    /// For more information about this option, see [`set_ttl`][link].
+    /// For more information about this option, see [`TcpStream::set_ttl`].
     ///
-    /// [link]: #method.set_ttl
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.set_ttl(100).expect("set_ttl call failed");
+    /// assert_eq!(stream.ttl().unwrap_or(0), 100);
+    /// ```
     pub fn ttl(&self) -> io::Result<u32> {
         self.0.ttl()
     }
@@ -338,6 +570,15 @@
     /// the field in the process. This can be useful for checking errors between
     /// calls.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpStream;
+    ///
+    /// let stream = TcpStream::connect("127.0.0.1:8080")
+    ///                        .expect("Couldn't connect to the server...");
+    /// stream.take_error().expect("No error was expected...");
+    /// ```
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         self.0.take_error()
     }
@@ -355,11 +596,44 @@
     /// `FIONBIO`. On Windows calling this method corresponds to calling
     /// `ioctlsocket` `FIONBIO`.
     ///
+    /// # Examples
+    ///
+    /// Reading bytes from a TCP stream in non-blocking mode:
+    ///
+    /// ```no_run
+    /// use std::io::{self, Read};
+    /// use std::net::TcpStream;
+    ///
+    /// let mut stream = TcpStream::connect("127.0.0.1:7878")
+    ///     .expect("Couldn't connect to the server...");
+    /// stream.set_nonblocking(true).expect("set_nonblocking call failed");
+    ///
+    /// # fn wait_for_fd() { unimplemented!() }
+    /// let mut buf = vec![];
+    /// loop {
+    ///     match stream.read_to_end(&mut buf) {
+    ///         Ok(_) => break,
+    ///         Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
+    ///             // wait until network socket is ready, typically implemented
+    ///             // via platform-specific APIs such as epoll or IOCP
+    ///             wait_for_fd();
+    ///         }
+    ///         Err(e) => panic!("encountered IO error: {}", e),
+    ///     };
+    /// };
+    /// println!("bytes: {:?}", buf);
+    /// ```
     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
         self.0.set_nonblocking(nonblocking)
     }
 }
 
+// In addition to the `impl`s here, `TcpStream` also has `impl`s for
+// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
+// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
+// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
+// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
+
 impl Read for TcpStream {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
         self.0.read(buf)
@@ -370,7 +644,13 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
+        // SAFETY: Read is guaranteed to work on uninitialized memory
         Initializer::nop()
     }
 }
@@ -384,6 +664,11 @@
         self.0.write_vectored(bufs)
     }
 
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -399,7 +684,13 @@
     }
 
     #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
+    #[inline]
     unsafe fn initializer(&self) -> Initializer {
+        // SAFETY: Read is guaranteed to work on uninitialized memory
         Initializer::nop()
     }
 }
@@ -413,6 +704,11 @@
         self.0.write_vectored(bufs)
     }
 
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
     fn flush(&mut self) -> io::Result<()> {
         Ok(())
     }
@@ -455,14 +751,6 @@
         net_imp::TcpListener::new_v6().map(TcpListener)
     }
 
-    pub fn raw(&self) -> c_int {
-        self.0.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.0.into_raw()
-    }
-
     /// Creates a new `TcpListener` which will be bound to the specified
     /// address.
     ///
@@ -470,7 +758,7 @@
     ///
     /// Binding with a port number of 0 will request that the OS assigns a port
     /// to this listener. The port allocated can be queried via the
-    /// [`local_addr`] method.
+    /// [`TcpListener::local_addr`] method.
     ///
     /// The address type can be any implementor of [`ToSocketAddrs`] trait. See
     /// its documentation for concrete examples.
@@ -480,6 +768,28 @@
     /// none of the addresses succeed in creating a listener, the error returned
     /// from the last attempt (the last address) is returned.
     ///
+    /// # Examples
+    ///
+    /// Creates a TCP listener bound to `127.0.0.1:80`:
+    ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
+    /// ```
+    ///
+    /// Creates a TCP listener bound to `127.0.0.1:80`. If that fails, create a
+    /// TCP listener bound to `127.0.0.1:443`:
+    ///
+    /// ```no_run
+    /// use std::net::{SocketAddr, TcpListener};
+    ///
+    /// let addrs = [
+    ///     SocketAddr::from(([127, 0, 0, 1], 80)),
+    ///     SocketAddr::from(([127, 0, 0, 1], 443)),
+    /// ];
+    /// let listener = TcpListener::bind(&addrs[..]).unwrap();
+    /// ```
     pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
         super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)
     }
@@ -506,6 +816,15 @@
 
     /// Returns the local socket address of this listener.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpListener};
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
+    /// assert_eq!(listener.local_addr().unwrap(),
+    ///            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080)));
+    /// ```
     pub fn local_addr(&self) -> io::Result<SocketAddr> {
         self.0.socket_addr()
     }
@@ -516,8 +835,14 @@
     /// object references. Both handles can be used to accept incoming
     /// connections and options set on one listener will affect the other.
     ///
-    /// [`TcpListener`]: ../../std/net/struct.TcpListener.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
+    /// let listener_clone = listener.try_clone().unwrap();
+    /// ```
     pub fn try_clone(&self) -> io::Result<TcpListener> {
         self.0.duplicate().map(TcpListener)
     }
@@ -528,8 +853,17 @@
     /// is established. When established, the corresponding [`TcpStream`] and the
     /// remote peer's address will be returned.
     ///
-    /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
+    /// match listener.accept() {
+    ///     Ok((_socket, addr)) => println!("new client: {:?}", addr),
+    ///     Err(e) => println!("couldn't get client: {:?}", e),
+    /// }
+    /// ```
     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
         self.0.accept().map(|(a, b)| (TcpStream(a), b))
     }
@@ -539,12 +873,31 @@
     ///
     /// The returned iterator will never return [`None`] and will also not yield
     /// the peer's [`SocketAddr`] structure. Iterating over it is equivalent to
-    /// calling [`accept`] in a loop.
+    /// calling [`TcpListener::accept`] in a loop.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
-    /// [`accept`]: #method.accept
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::{TcpListener, TcpStream};
+    ///
+    /// fn handle_connection(stream: TcpStream) {
+    ///    //...
+    /// }
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = TcpListener::bind("127.0.0.1:80").unwrap();
+    ///
+    ///     for stream in listener.incoming() {
+    ///         match stream {
+    ///             Ok(stream) => {
+    ///                 handle_connection(stream);
+    ///             }
+    ///             Err(e) => { /* connection failed */ }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
     pub fn incoming(&self) -> Incoming<'_> {
         Incoming { listener: self }
     }
@@ -554,26 +907,41 @@
     /// This value sets the time-to-live field that is used in every packet sent
     /// from this socket.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
+    /// listener.set_ttl(100).expect("could not set TTL");
+    /// ```
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
         self.0.set_ttl(ttl)
     }
 
     /// Gets the value of the `IP_TTL` option for this socket.
     ///
-    /// For more information about this option, see [`set_ttl`][link].
+    /// For more information about this option, see [`TcpListener::set_ttl`].
     ///
-    /// [link]: #method.set_ttl
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
+    /// listener.set_ttl(100).expect("could not set TTL");
+    /// assert_eq!(listener.ttl().unwrap_or(0), 100);
+    /// ```
     pub fn ttl(&self) -> io::Result<u32> {
         self.0.ttl()
     }
 
-    /// This option can only be set before the socket is bound
+    #[allow(missing_docs)]
     pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
         self.0.set_only_v6(only_v6)
     }
 
-    /// This option can only be set before the socket is bound
+    #[allow(missing_docs)]
     pub fn only_v6(&self) -> io::Result<bool> {
         self.0.only_v6()
     }
@@ -584,6 +952,14 @@
     /// the field in the process. This can be useful for checking errors between
     /// calls.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:80").unwrap();
+    /// listener.take_error().expect("No error was expected");
+    /// ```
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         self.0.take_error()
     }
@@ -600,11 +976,47 @@
     /// `FIONBIO`. On Windows calling this method corresponds to calling
     /// `ioctlsocket` `FIONBIO`.
     ///
+    /// # Examples
+    ///
+    /// Bind a TCP listener to an address, listen for connections, and read
+    /// bytes in nonblocking mode:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::TcpListener;
+    ///
+    /// let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
+    /// listener.set_nonblocking(true).expect("Cannot set non-blocking");
+    ///
+    /// # fn wait_for_fd() { unimplemented!() }
+    /// # fn handle_connection(stream: std::net::TcpStream) { unimplemented!() }
+    /// for stream in listener.incoming() {
+    ///     match stream {
+    ///         Ok(s) => {
+    ///             // do something with the TcpStream
+    ///             handle_connection(s);
+    ///         }
+    ///         Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
+    ///             // wait until network socket is ready, typically implemented
+    ///             // via platform-specific APIs such as epoll or IOCP
+    ///             wait_for_fd();
+    ///             continue;
+    ///         }
+    ///         Err(e) => panic!("encountered IO error: {}", e),
+    ///     }
+    /// }
+    /// ```
     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
         self.0.set_nonblocking(nonblocking)
     }
 }
 
+// In addition to the `impl`s here, `TcpListener` also has `impl`s for
+// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
+// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
+// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
+// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
+
 impl<'a> Iterator for Incoming<'a> {
     type Item = io::Result<TcpStream>;
     fn next(&mut self) -> Option<io::Result<TcpStream>> {
diff --git a/sgx_tstd/src/net/udp.rs b/sgx_tstd/src/net/udp.rs
index 417a0e7..2ba2f0b 100644
--- a/sgx_tstd/src/net/udp.rs
+++ b/sgx_tstd/src/net/udp.rs
@@ -15,14 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc::c_int;
-use core::fmt;
+use crate::fmt;
 use crate::io::{self, Error, ErrorKind};
 use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};
 use crate::sys_common::net as net_imp;
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::time::Duration;
 
+use sgx_libc::c_int;
+
 /// A UDP socket.
 ///
 /// After creating a `UdpSocket` by [`bind`]ing it to a socket address, data can be
@@ -37,16 +38,38 @@
 /// an unordered, unreliable protocol; refer to [`TcpListener`] and [`TcpStream`] for TCP
 /// primitives.
 ///
-/// [`bind`]: #method.bind
-/// [`connect`]: #method.connect
+/// [`bind`]: UdpSocket::bind
+/// [`connect`]: UdpSocket::connect
 /// [IETF RFC 768]: https://tools.ietf.org/html/rfc768
-/// [`recv`]: #method.recv
-/// [received from]: #method.recv_from
-/// [`send`]: #method.send
-/// [sent to]: #method.send_to
-/// [`TcpListener`]: ../../std/net/struct.TcpListener.html
-/// [`TcpStream`]: ../../std/net/struct.TcpStream.html
+/// [`recv`]: UdpSocket::recv
+/// [received from]: UdpSocket::recv_from
+/// [`send`]: UdpSocket::send
+/// [sent to]: UdpSocket::send_to
+/// [`TcpListener`]: crate::net::TcpListener
+/// [`TcpStream`]: crate::net::TcpStream
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::net::UdpSocket;
+///
+/// fn main() -> std::io::Result<()> {
+///     {
+///         let socket = UdpSocket::bind("127.0.0.1:34254")?;
+///
+///         // Receives a single datagram message on the socket. If `buf` is too small to hold
+///         // the message, it will be cut off.
+///         let mut buf = [0; 10];
+///         let (amt, src) = socket.recv_from(&mut buf)?;
+///
+///         // Redeclare `buf` as slice of the received data and send reverse data back to origin.
+///         let buf = &mut buf[..amt];
+///         buf.reverse();
+///         socket.send_to(buf, &src)?;
+///     } // the socket is closed here
+///     Ok(())
+/// }
+/// ```
 pub struct UdpSocket(net_imp::UdpSocket);
 
 impl UdpSocket {
@@ -62,14 +85,6 @@
         net_imp::UdpSocket::new_v6().map(UdpSocket)
     }
 
-    pub fn raw(&self) -> c_int {
-        self.0.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.0.into_raw()
-    }
-
     /// Creates a UDP socket from the given address.
     ///
     /// The address type can be any implementor of [`ToSocketAddrs`] trait. See
@@ -80,8 +95,28 @@
     /// of the addresses succeed in creating a socket, the error returned from
     /// the last attempt (the last address) is returned.
     ///
-    /// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
+    /// # Examples
     ///
+    /// Creates a UDP socket bound to `127.0.0.1:3400`:
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:3400").expect("couldn't bind to address");
+    /// ```
+    ///
+    /// Creates a UDP socket bound to `127.0.0.1:3400`. If the socket cannot be
+    /// bound to that address, create a UDP socket bound to `127.0.0.1:3401`:
+    ///
+    /// ```no_run
+    /// use std::net::{SocketAddr, UdpSocket};
+    ///
+    /// let addrs = [
+    ///     SocketAddr::from(([127, 0, 0, 1], 3400)),
+    ///     SocketAddr::from(([127, 0, 0, 1], 3401)),
+    /// ];
+    /// let socket = UdpSocket::bind(&addrs[..]).expect("couldn't bind to address");
+    /// ```
     pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
         super::each_addr(addr, net_imp::UdpSocket::bind).map(UdpSocket)
     }
@@ -98,6 +133,17 @@
     /// hold the message bytes. If a message is too long to fit in the supplied buffer,
     /// excess bytes may be discarded.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// let mut buf = [0; 10];
+    /// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf)
+    ///                                         .expect("Didn't receive data");
+    /// let filled_buf = &mut buf[..number_of_bytes];
+    /// ```
     pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
         self.0.recv_from(buf)
     }
@@ -115,6 +161,17 @@
     /// Do not use this function to implement busy waiting, instead use `libc::poll` to
     /// synchronize IO events on one or more sockets.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// let mut buf = [0; 10];
+    /// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf)
+    ///                                         .expect("Didn't receive data");
+    /// let filled_buf = &mut buf[..number_of_bytes];
+    /// ```
     pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
         self.0.peek_from(buf)
     }
@@ -131,25 +188,64 @@
     /// This will return an error when the IP version of the local socket
     /// does not match that returned from [`ToSocketAddrs`].
     ///
-    /// See issue #34202 for more details.
+    /// See [Issue #34202] for more details.
     ///
-    /// [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.send_to(&[0; 10], "127.0.0.1:4242").expect("couldn't send data");
+    /// ```
+    ///
+    /// [Issue #34202]: https://github.com/rust-lang/rust/issues/34202
     pub fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addr: A) -> io::Result<usize> {
         match addr.to_socket_addrs()?.next() {
             Some(addr) => self.0.send_to(buf, &addr),
-            None => Err(Error::new(ErrorKind::InvalidInput, "no addresses to send data to")),
+            None => Err(Error::new_const(ErrorKind::InvalidInput, &"no addresses to send data to")),
         }
     }
 
     /// Returns the socket address of the remote peer this socket was connected to.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
+    /// assert_eq!(socket.peer_addr().unwrap(),
+    ///            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
+    /// ```
+    ///
+    /// If the socket isn't connected, it will return a [`NotConnected`] error.
+    ///
+    /// [`NotConnected`]: io::ErrorKind::NotConnected
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// assert_eq!(socket.peer_addr().unwrap_err().kind(),
+    ///            std::io::ErrorKind::NotConnected);
+    /// ```
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
         self.0.peer_addr()
     }
 
     /// Returns the socket address that this socket was created from.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// assert_eq!(socket.local_addr().unwrap(),
+    ///            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 34254)));
+    /// ```
     pub fn local_addr(&self) -> io::Result<SocketAddr> {
         self.0.socket_addr()
     }
@@ -160,6 +256,14 @@
     /// object references. Both handles will read and write the same port, and
     /// options set on one socket will be propagated to the other.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// let socket_clone = socket.try_clone().expect("couldn't clone the socket");
+    /// ```
     pub fn try_clone(&self) -> io::Result<UdpSocket> {
         self.0.duplicate().map(UdpSocket)
     }
@@ -176,13 +280,32 @@
     /// a result of setting this option. For example Unix typically returns an
     /// error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
-    /// [`Duration`]: ../../std/time/struct.Duration.html
-    /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
-    /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
+    /// [`read`]: io::Read::read
+    /// [`WouldBlock`]: io::ErrorKind::WouldBlock
+    /// [`TimedOut`]: io::ErrorKind::TimedOut
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_read_timeout(None).expect("set_read_timeout call failed");
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::UdpSocket;
+    /// use std::time::Duration;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
+    /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
+    /// let err = result.unwrap_err();
+    /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
+    /// ```
     pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
         self.0.set_read_timeout(dur)
     }
@@ -199,13 +322,32 @@
     /// as a result of setting this option. For example Unix typically returns
     /// an error of the kind [`WouldBlock`], but Windows may return [`TimedOut`].
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
-    /// [`Duration`]: ../../std/time/struct.Duration.html
-    /// [`WouldBlock`]: ../../std/io/enum.ErrorKind.html#variant.WouldBlock
-    /// [`TimedOut`]: ../../std/io/enum.ErrorKind.html#variant.TimedOut
+    /// [`write`]: io::Write::write
+    /// [`WouldBlock`]: io::ErrorKind::WouldBlock
+    /// [`TimedOut`]: io::ErrorKind::TimedOut
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_write_timeout(None).expect("set_write_timeout call failed");
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::UdpSocket;
+    /// use std::time::Duration;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
+    /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
+    /// let err = result.unwrap_err();
+    /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
+    /// ```
     pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
         self.0.set_write_timeout(dur)
     }
@@ -214,9 +356,17 @@
     ///
     /// If the timeout is [`None`], then [`read`] calls will block indefinitely.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`read`]: ../../std/io/trait.Read.html#tymethod.read
+    /// [`read`]: io::Read::read
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_read_timeout(None).expect("set_read_timeout call failed");
+    /// assert_eq!(socket.read_timeout().unwrap(), None);
+    /// ```
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
         self.0.read_timeout()
     }
@@ -225,9 +375,17 @@
     ///
     /// If the timeout is [`None`], then [`write`] calls will block indefinitely.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`write`]: ../../std/io/trait.Write.html#tymethod.write
+    /// [`write`]: io::Write::write
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_write_timeout(None).expect("set_write_timeout call failed");
+    /// assert_eq!(socket.write_timeout().unwrap(), None);
+    /// ```
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
         self.0.write_timeout()
     }
@@ -237,17 +395,31 @@
     /// When enabled, this socket is allowed to send packets to a broadcast
     /// address.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_broadcast(false).expect("set_broadcast call failed");
+    /// ```
     pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
         self.0.set_broadcast(broadcast)
     }
 
     /// Gets the value of the `SO_BROADCAST` option for this socket.
     ///
-    /// For more information about this option, see
-    /// [`set_broadcast`][link].
+    /// For more information about this option, see [`UdpSocket::set_broadcast`].
     ///
-    /// [link]: #method.set_broadcast
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_broadcast(false).expect("set_broadcast call failed");
+    /// assert_eq!(socket.broadcast().unwrap(), false);
+    /// ```
     pub fn broadcast(&self) -> io::Result<bool> {
         self.0.broadcast()
     }
@@ -255,19 +427,33 @@
     /// Sets the value of the `IP_MULTICAST_LOOP` option for this socket.
     ///
     /// If enabled, multicast packets will be looped back to the local socket.
-    /// Note that this may not have any effect on IPv6 sockets.
+    /// Note that this might not have any effect on IPv6 sockets.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_loop_v4(false).expect("set_multicast_loop_v4 call failed");
+    /// ```
     pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
         self.0.set_multicast_loop_v4(multicast_loop_v4)
     }
 
     /// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
     ///
-    /// For more information about this option, see
-    /// [`set_multicast_loop_v4`][link].
+    /// For more information about this option, see [`UdpSocket::set_multicast_loop_v4`].
     ///
-    /// [link]: #method.set_multicast_loop_v4
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_loop_v4(false).expect("set_multicast_loop_v4 call failed");
+    /// assert_eq!(socket.multicast_loop_v4().unwrap(), false);
+    /// ```
     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
         self.0.multicast_loop_v4()
     }
@@ -278,19 +464,33 @@
     /// this socket. The default value is 1 which means that multicast packets
     /// don't leave the local network unless explicitly requested.
     ///
-    /// Note that this may not have any effect on IPv6 sockets.
+    /// Note that this might not have any effect on IPv6 sockets.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_ttl_v4(42).expect("set_multicast_ttl_v4 call failed");
+    /// ```
     pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
         self.0.set_multicast_ttl_v4(multicast_ttl_v4)
     }
 
     /// Gets the value of the `IP_MULTICAST_TTL` option for this socket.
     ///
-    /// For more information about this option, see
-    /// [`set_multicast_ttl_v4`][link].
+    /// For more information about this option, see [`UdpSocket::set_multicast_ttl_v4`].
     ///
-    /// [link]: #method.set_multicast_ttl_v4
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_ttl_v4(42).expect("set_multicast_ttl_v4 call failed");
+    /// assert_eq!(socket.multicast_ttl_v4().unwrap(), 42);
+    /// ```
     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
         self.0.multicast_ttl_v4()
     }
@@ -298,19 +498,33 @@
     /// Sets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
     ///
     /// Controls whether this socket sees the multicast packets it sends itself.
-    /// Note that this may not have any affect on IPv4 sockets.
+    /// Note that this might not have any affect on IPv4 sockets.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_loop_v6(false).expect("set_multicast_loop_v6 call failed");
+    /// ```
     pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
         self.0.set_multicast_loop_v6(multicast_loop_v6)
     }
 
     /// Gets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
     ///
-    /// For more information about this option, see
-    /// [`set_multicast_loop_v6`][link].
+    /// For more information about this option, see [`UdpSocket::set_multicast_loop_v6`].
     ///
-    /// [link]: #method.set_multicast_loop_v6
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_multicast_loop_v6(false).expect("set_multicast_loop_v6 call failed");
+    /// assert_eq!(socket.multicast_loop_v6().unwrap(), false);
+    /// ```
     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
         self.0.multicast_loop_v6()
     }
@@ -320,16 +534,31 @@
     /// This value sets the time-to-live field that is used in every packet sent
     /// from this socket.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_ttl(42).expect("set_ttl call failed");
+    /// ```
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
         self.0.set_ttl(ttl)
     }
 
     /// Gets the value of the `IP_TTL` option for this socket.
     ///
-    /// For more information about this option, see [`set_ttl`][link].
+    /// For more information about this option, see [`UdpSocket::set_ttl`].
     ///
-    /// [link]: #method.set_ttl
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.set_ttl(42).expect("set_ttl call failed");
+    /// assert_eq!(socket.ttl().unwrap(), 42);
+    /// ```
     pub fn ttl(&self) -> io::Result<u32> {
         self.0.ttl()
     }
@@ -356,20 +585,14 @@
 
     /// Executes an operation of the `IP_DROP_MEMBERSHIP` type.
     ///
-    /// For more information about this option, see
-    /// [`join_multicast_v4`][link].
-    ///
-    /// [link]: #method.join_multicast_v4
+    /// For more information about this option, see [`UdpSocket::join_multicast_v4`].
     pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
         self.0.leave_multicast_v4(multiaddr, interface)
     }
 
     /// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type.
     ///
-    /// For more information about this option, see
-    /// [`join_multicast_v6`][link].
-    ///
-    /// [link]: #method.join_multicast_v6
+    /// For more information about this option, see [`UdpSocket::join_multicast_v6`].
     pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
         self.0.leave_multicast_v6(multiaddr, interface)
     }
@@ -380,6 +603,18 @@
     /// the field in the process. This can be useful for checking errors between
     /// calls.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// match socket.take_error() {
+    ///     Ok(Some(error)) => println!("UdpSocket error: {:?}", error),
+    ///     Ok(None) => println!("No error"),
+    ///     Err(error) => println!("UdpSocket.take_error failed: {:?}", error),
+    /// }
+    /// ```
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         self.0.take_error()
     }
@@ -396,17 +631,40 @@
     /// error for each of the specified addresses, the error returned from the
     /// last connection attempt (the last address) is returned.
     ///
+    /// # Examples
+    ///
+    /// Creates a UDP socket bound to `127.0.0.1:3400` and connect the socket to
+    /// `127.0.0.1:8080`:
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:3400").expect("couldn't bind to address");
+    /// socket.connect("127.0.0.1:8080").expect("connect function failed");
+    /// ```
+    ///
+    /// Unlike in the TCP case, passing an array of addresses to the `connect`
+    /// function of a UDP socket is not a useful thing to do: The OS will be
+    /// unable to determine whether something is listening on the remote
+    /// address without the application sending data.
     pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> {
         super::each_addr(addr, |addr| self.0.connect(addr))
     }
 
     /// Sends data on the socket to the remote address to which it is connected.
     ///
-    /// The [`connect`] method will connect this socket to a remote address. This
+    /// [`UdpSocket::connect`] will connect this socket to a remote address. This
     /// method will fail if the socket is not connected.
     ///
-    /// [`connect`]: #method.connect
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.connect("127.0.0.1:8080").expect("connect function failed");
+    /// socket.send(&[0, 1, 2]).expect("couldn't send message");
+    /// ```
     pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
         self.0.send(buf)
     }
@@ -418,11 +676,22 @@
     /// hold the message bytes. If a message is too long to fit in the supplied buffer,
     /// excess bytes may be discarded.
     ///
-    /// The [`connect`] method will connect this socket to a remote address. This
+    /// [`UdpSocket::connect`] will connect this socket to a remote address. This
     /// method will fail if the socket is not connected.
     ///
-    /// [`connect`]: #method.connect
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.connect("127.0.0.1:8080").expect("connect function failed");
+    /// let mut buf = [0; 10];
+    /// match socket.recv(&mut buf) {
+    ///     Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]),
+    ///     Err(e) => println!("recv function failed: {:?}", e),
+    /// }
+    /// ```
     pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
         self.0.recv(buf)
     }
@@ -441,16 +710,27 @@
     /// Do not use this function to implement busy waiting, instead use `libc::poll` to
     /// synchronize IO events on one or more sockets.
     ///
-    /// The [`connect`] method will connect this socket to a remote address. This
+    /// [`UdpSocket::connect`] will connect this socket to a remote address. This
     /// method will fail if the socket is not connected.
     ///
-    /// [`connect`]: #method.connect
-    ///
     /// # Errors
     ///
     /// This method will fail if the socket is not connected. The `connect` method
     /// will connect this socket to a remote address.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
+    /// socket.connect("127.0.0.1:8080").expect("connect function failed");
+    /// let mut buf = [0; 10];
+    /// match socket.peek(&mut buf) {
+    ///     Ok(received) => println!("received {} bytes", received),
+    ///     Err(e) => println!("peek function failed: {:?}", e),
+    /// }
+    /// ```
     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
         self.0.peek(buf)
     }
@@ -468,11 +748,44 @@
     /// `FIONBIO`. On Windows calling this method corresponds to calling
     /// `ioctlsocket` `FIONBIO`.
     ///
+    /// # Examples
+    ///
+    /// Creates a UDP socket bound to `127.0.0.1:7878` and read bytes in
+    /// nonblocking mode:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::UdpSocket;
+    ///
+    /// let socket = UdpSocket::bind("127.0.0.1:7878").unwrap();
+    /// socket.set_nonblocking(true).unwrap();
+    ///
+    /// # fn wait_for_fd() { unimplemented!() }
+    /// let mut buf = [0; 10];
+    /// let (num_bytes_read, _) = loop {
+    ///     match socket.recv_from(&mut buf) {
+    ///         Ok(n) => break n,
+    ///         Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
+    ///             // wait until network socket is ready, typically implemented
+    ///             // via platform-specific APIs such as epoll or IOCP
+    ///             wait_for_fd();
+    ///         }
+    ///         Err(e) => panic!("encountered IO error: {}", e),
+    ///     }
+    /// };
+    /// println!("bytes: {:?}", &buf[..num_bytes_read]);
+    /// ```
     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
         self.0.set_nonblocking(nonblocking)
     }
 }
 
+// In addition to the `impl`s here, `UdpSocket` also has `impl`s for
+// `AsFd`/`From<OwnedFd>`/`Into<OwnedFd>` and
+// `AsRawFd`/`IntoRawFd`/`FromRawFd`, on Unix and WASI, and
+// `AsSocket`/`From<OwnedSocket>`/`Into<OwnedSocket>` and
+// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
+
 impl AsInner<net_imp::UdpSocket> for UdpSocket {
     fn as_inner(&self) -> &net_imp::UdpSocket {
         &self.0
diff --git a/sgx_tstd/src/num.rs b/sgx_tstd/src/num.rs
index dfb176a..4acb36f 100644
--- a/sgx_tstd/src/num.rs
+++ b/sgx_tstd/src/num.rs
@@ -26,3 +26,5 @@
 pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
 pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
 pub use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
+
+pub use core::num::IntErrorKind;
diff --git a/sgx_tstd/src/sys/ext/ffi.rs b/sgx_tstd/src/os/fd/mod.rs
similarity index 75%
copy from sgx_tstd/src/sys/ext/ffi.rs
copy to sgx_tstd/src/os/fd/mod.rs
index 52ac67a..37b7f24 100644
--- a/sgx_tstd/src/sys/ext/ffi.rs
+++ b/sgx_tstd/src/os/fd/mod.rs
@@ -15,6 +15,16 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific extension to the primitives in the `std::ffi` module
+//! Owned and borrowed Unix-like file descriptors.
 
-pub use crate::sys_common::os_str_bytes::*;
+#![deny(unsafe_op_in_unsafe_fn)]
+
+// `RawFd`, `AsRawFd`, etc.
+pub mod raw;
+
+// `OwnedFd`, `AsFd`, etc.
+pub mod owned;
+
+// Implementations for `AsRawFd` etc. for network types.
+#[cfg(feature = "net")]
+mod net;
diff --git a/sgx_tstd/src/os/fd/net.rs b/sgx_tstd/src/os/fd/net.rs
new file mode 100644
index 0000000..23b04d6
--- /dev/null
+++ b/sgx_tstd/src/os/fd/net.rs
@@ -0,0 +1,60 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::os::fd::owned::OwnedFd;
+use crate::os::fd::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
+use crate::sys_common::{self, AsInner, FromInner, IntoInner};
+use crate::{net, sys};
+
+macro_rules! impl_as_raw_fd {
+    ($($t:ident)*) => {$(
+        impl AsRawFd for net::$t {
+            #[inline]
+            fn as_raw_fd(&self) -> RawFd {
+                self.as_inner().socket().as_raw_fd()
+            }
+        }
+    )*};
+}
+impl_as_raw_fd! { TcpStream TcpListener UdpSocket }
+
+macro_rules! impl_from_raw_fd {
+    ($($t:ident)*) => {$(
+        impl FromRawFd for net::$t {
+            #[inline]
+            unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
+                unsafe {
+                    let socket = sys::net::Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)));
+                    net::$t::from_inner(sys_common::net::$t::from_inner(socket))
+                }
+            }
+        }
+    )*};
+}
+impl_from_raw_fd! { TcpStream TcpListener UdpSocket }
+
+macro_rules! impl_into_raw_fd {
+    ($($t:ident)*) => {$(
+        impl IntoRawFd for net::$t {
+            #[inline]
+            fn into_raw_fd(self) -> RawFd {
+                self.into_inner().into_socket().into_inner().into_inner().into_raw_fd()
+            }
+        }
+    )*};
+}
+impl_into_raw_fd! { TcpStream TcpListener UdpSocket }
diff --git a/sgx_tstd/src/os/fd/owned.rs b/sgx_tstd/src/os/fd/owned.rs
new file mode 100644
index 0000000..4f00737
--- /dev/null
+++ b/sgx_tstd/src/os/fd/owned.rs
@@ -0,0 +1,295 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Owned and borrowed Unix-like file descriptors.
+
+#![deny(unsafe_op_in_unsafe_fn)]
+
+use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
+use crate::fmt;
+use crate::marker::PhantomData;
+use crate::mem::forget;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::untrusted::fs;
+
+/// A borrowed file descriptor.
+///
+/// This has a lifetime parameter to tie it to the lifetime of something that
+/// owns the file descriptor.
+///
+/// This uses `repr(transparent)` and has the representation of a host file
+/// descriptor, so it can be used in FFI in places where a file descriptor is
+/// passed as an argument, it is not captured or consumed, and it never has the
+/// value `-1`.
+#[derive(Copy, Clone)]
+#[repr(transparent)]
+#[rustc_layout_scalar_valid_range_start(0)]
+// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
+// 32-bit c_int. Below is -2, in two's complement, but that only works out
+// because c_int is 32 bits.
+#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
+pub struct BorrowedFd<'fd> {
+    fd: RawFd,
+    _phantom: PhantomData<&'fd OwnedFd>,
+}
+
+/// An owned file descriptor.
+///
+/// This closes the file descriptor on drop.
+///
+/// This uses `repr(transparent)` and has the representation of a host file
+/// descriptor, so it can be used in FFI in places where a file descriptor is
+/// passed as a consumed argument or returned as an owned value, and it never
+/// has the value `-1`.
+#[repr(transparent)]
+#[rustc_layout_scalar_valid_range_start(0)]
+// libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
+// 32-bit c_int. Below is -2, in two's complement, but that only works out
+// because c_int is 32 bits.
+#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
+pub struct OwnedFd {
+    fd: RawFd,
+}
+
+impl BorrowedFd<'_> {
+    /// Return a `BorrowedFd` holding the given raw file descriptor.
+    ///
+    /// # Safety
+    ///
+    /// The resource pointed to by `fd` must remain open for the duration of
+    /// the returned `BorrowedFd`, and it must not have the value `-1`.
+    #[inline]
+    pub unsafe fn borrow_raw_fd(fd: RawFd) -> Self {
+        assert_ne!(fd, u32::MAX as RawFd);
+        // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
+        unsafe { Self { fd, _phantom: PhantomData } }
+    }
+}
+
+impl AsRawFd for BorrowedFd<'_> {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.fd
+    }
+}
+
+impl AsRawFd for OwnedFd {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.fd
+    }
+}
+
+impl IntoRawFd for OwnedFd {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        let fd = self.fd;
+        forget(self);
+        fd
+    }
+}
+
+impl FromRawFd for OwnedFd {
+    /// Constructs a new instance of `Self` from the given raw file descriptor.
+    ///
+    /// # Safety
+    ///
+    /// The resource pointed to by `fd` must be open and suitable for assuming
+    /// ownership. The resource must not require any cleanup other than `close`.
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> Self {
+        assert_ne!(fd, u32::MAX as RawFd);
+        // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
+        unsafe { Self { fd } }
+    }
+}
+
+impl Drop for OwnedFd {
+    #[inline]
+    fn drop(&mut self) {
+        unsafe {
+            // Note that errors are ignored when closing a file descriptor. The
+            // reason for this is that if an error occurs we don't actually know if
+            // the file descriptor was closed or not, and if we retried (for
+            // something like EINTR), we might close another valid file descriptor
+            // opened after we closed ours.
+            let _ = libc::close(self.fd);
+        }
+    }
+}
+
+impl fmt::Debug for BorrowedFd<'_> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
+    }
+}
+
+impl fmt::Debug for OwnedFd {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
+    }
+}
+
+/// A trait to borrow the file descriptor from an underlying object.
+///
+/// This is only available on unix platforms and must be imported in order to
+/// call the method. Windows platforms have a corresponding `AsHandle` and
+/// `AsSocket` set of traits.
+pub trait AsFd {
+    /// Borrows the file descriptor.
+    ///
+    /// # Example
+    ///
+    /// ```rust,no_run
+    /// # #![feature(io_safety)]
+    /// use std::fs::File;
+    /// # use std::io;
+    /// # #[cfg(target_os = "wasi")]
+    /// # use std::os::wasi::io::{AsFd, BorrowedFd};
+    /// # #[cfg(unix)]
+    /// # use std::os::unix::io::{AsFd, BorrowedFd};
+    ///
+    /// let mut f = File::open("foo.txt")?;
+    /// # #[cfg(any(unix, target_os = "wasi"))]
+    /// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
+    /// # Ok::<(), io::Error>(())
+    /// ```
+    fn as_fd(&self) -> BorrowedFd<'_>;
+}
+
+impl AsFd for BorrowedFd<'_> {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        *self
+    }
+}
+
+impl AsFd for OwnedFd {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        // Safety: `OwnedFd` and `BorrowedFd` have the same validity
+        // invariants, and the `BorrowdFd` is bounded by the lifetime
+        // of `&self`.
+        unsafe { BorrowedFd::borrow_raw_fd(self.as_raw_fd()) }
+    }
+}
+
+impl AsFd for fs::File {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.as_inner().as_fd()
+    }
+}
+
+impl From<fs::File> for OwnedFd {
+    #[inline]
+    fn from(file: fs::File) -> OwnedFd {
+        file.into_inner().into_inner().into_inner()
+    }
+}
+
+impl From<OwnedFd> for fs::File {
+    #[inline]
+    fn from(owned_fd: OwnedFd) -> Self {
+        Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
+    }
+}
+
+#[cfg(feature = "net")]
+impl AsFd for crate::net::TcpStream {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.as_inner().socket().as_fd()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<crate::net::TcpStream> for OwnedFd {
+    #[inline]
+    #[allow(clippy::useless_conversion)]
+    fn from(tcp_stream: crate::net::TcpStream) -> OwnedFd {
+        tcp_stream.into_inner().into_socket().into_inner().into_inner().into()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<OwnedFd> for crate::net::TcpStream {
+    #[inline]
+    fn from(owned_fd: OwnedFd) -> Self {
+        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
+            owned_fd,
+        ))))
+    }
+}
+
+#[cfg(feature = "net")]
+impl AsFd for crate::net::TcpListener {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.as_inner().socket().as_fd()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<crate::net::TcpListener> for OwnedFd {
+    #[inline]
+    #[allow(clippy::useless_conversion)]
+    fn from(tcp_listener: crate::net::TcpListener) -> OwnedFd {
+        tcp_listener.into_inner().into_socket().into_inner().into_inner().into()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<OwnedFd> for crate::net::TcpListener {
+    #[inline]
+    fn from(owned_fd: OwnedFd) -> Self {
+        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
+            owned_fd,
+        ))))
+    }
+}
+
+#[cfg(feature = "net")]
+impl AsFd for crate::net::UdpSocket {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.as_inner().socket().as_fd()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<crate::net::UdpSocket> for OwnedFd {
+    #[inline]
+    #[allow(clippy::useless_conversion)]
+    fn from(udp_socket: crate::net::UdpSocket) -> OwnedFd {
+        udp_socket.into_inner().into_socket().into_inner().into_inner().into()
+    }
+}
+
+#[cfg(feature = "net")]
+impl From<OwnedFd> for crate::net::UdpSocket {
+    #[inline]
+    fn from(owned_fd: OwnedFd) -> Self {
+        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
+            owned_fd,
+        ))))
+    }
+}
+
+mod libc {
+    pub use sgx_libc::ocall::close;
+}
diff --git a/sgx_tstd/src/os/fd/raw.rs b/sgx_tstd/src/os/fd/raw.rs
new file mode 100644
index 0000000..d73008d
--- /dev/null
+++ b/sgx_tstd/src/os/fd/raw.rs
@@ -0,0 +1,215 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Raw Unix-like file descriptors.
+
+use crate::io;
+use crate::os::raw;
+use crate::os::unix::io::OwnedFd;
+use crate::sys_common::{AsInner, IntoInner};
+use crate::untrusted::fs;
+
+use sgx_libc as libc;
+
+/// Raw file descriptors.
+pub type RawFd = raw::c_int;
+
+/// A trait to extract the raw file descriptor from an underlying object.
+///
+/// This is only available on unix and WASI platforms and must be imported in
+/// order to call the method. Windows platforms have a corresponding
+/// `AsRawHandle` and `AsRawSocket` set of traits.
+pub trait AsRawFd {
+    /// Extracts the raw file descriptor.
+    ///
+    /// This method does **not** pass ownership of the raw file descriptor
+    /// to the caller. The descriptor is only guaranteed to be valid while
+    /// the original object has not yet been destroyed.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// # use std::io;
+    /// #[cfg(unix)]
+    /// use std::os::unix::io::{AsRawFd, RawFd};
+    /// #[cfg(target_os = "wasi")]
+    /// use std::os::wasi::io::{AsRawFd, RawFd};
+    ///
+    /// let mut f = File::open("foo.txt")?;
+    /// // Note that `raw_fd` is only valid as long as `f` exists.
+    /// #[cfg(any(unix, target_os = "wasi"))]
+    /// let raw_fd: RawFd = f.as_raw_fd();
+    /// # Ok::<(), io::Error>(())
+    /// ```
+    fn as_raw_fd(&self) -> RawFd;
+}
+
+/// A trait to express the ability to construct an object from a raw file
+/// descriptor.
+pub trait FromRawFd {
+    /// Constructs a new instance of `Self` from the given raw file
+    /// descriptor.
+    ///
+    /// This function **consumes ownership** of the specified file
+    /// descriptor. The returned object will take responsibility for closing
+    /// it when the object goes out of scope.
+    ///
+    /// This function is also unsafe as the primitives currently returned
+    /// have the contract that they are the sole owner of the file
+    /// descriptor they are wrapping. Usage of this function could
+    /// accidentally allow violating this contract which can cause memory
+    /// unsafety in code that relies on it being true.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// # use std::io;
+    /// #[cfg(unix)]
+    /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
+    /// #[cfg(target_os = "wasi")]
+    /// use std::os::wasi::io::{FromRawFd, IntoRawFd, RawFd};
+    ///
+    /// let f = File::open("foo.txt")?;
+    /// # #[cfg(any(unix, target_os = "wasi"))]
+    /// let raw_fd: RawFd = f.into_raw_fd();
+    /// // SAFETY: no other functions should call `from_raw_fd`, so there
+    /// // is only one owner for the file descriptor.
+    /// # #[cfg(any(unix, target_os = "wasi"))]
+    /// let f = unsafe { File::from_raw_fd(raw_fd) };
+    /// # Ok::<(), io::Error>(())
+    /// ```
+    unsafe fn from_raw_fd(fd: RawFd) -> Self;
+}
+
+/// A trait to express the ability to consume an object and acquire ownership of
+/// its raw file descriptor.
+pub trait IntoRawFd {
+    /// Consumes this object, returning the raw underlying file descriptor.
+    ///
+    /// This function **transfers ownership** of the underlying file descriptor
+    /// to the caller. Callers are then the unique owners of the file descriptor
+    /// and must close the descriptor once it's no longer needed.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// # use std::io;
+    /// #[cfg(unix)]
+    /// use std::os::unix::io::{IntoRawFd, RawFd};
+    /// #[cfg(target_os = "wasi")]
+    /// use std::os::wasi::io::{IntoRawFd, RawFd};
+    ///
+    /// let f = File::open("foo.txt")?;
+    /// #[cfg(any(unix, target_os = "wasi"))]
+    /// let raw_fd: RawFd = f.into_raw_fd();
+    /// # Ok::<(), io::Error>(())
+    /// ```
+    fn into_raw_fd(self) -> RawFd;
+}
+
+impl AsRawFd for RawFd {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        *self
+    }
+}
+
+impl IntoRawFd for RawFd {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        self
+    }
+}
+
+impl FromRawFd for RawFd {
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
+        fd
+    }
+}
+
+impl AsRawFd for fs::File {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.as_inner().as_raw_fd()
+    }
+}
+
+impl FromRawFd for fs::File {
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
+        unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
+    }
+}
+
+impl IntoRawFd for fs::File {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        self.into_inner().into_inner().into_raw_fd()
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl AsRawFd for io::Stdin {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDIN_FILENO
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl AsRawFd for io::Stdout {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDOUT_FILENO
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl AsRawFd for io::Stderr {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDERR_FILENO
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl<'a> AsRawFd for io::StdinLock<'a> {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDIN_FILENO
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl<'a> AsRawFd for io::StdoutLock<'a> {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDOUT_FILENO
+    }
+}
+
+#[cfg(feature = "stdio")]
+impl<'a> AsRawFd for io::StderrLock<'a> {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        libc::STDERR_FILENO
+    }
+}
diff --git a/sgx_tstd/src/os/fs.rs b/sgx_tstd/src/os/fs.rs
index bb27842..09c9cf5 100644
--- a/sgx_tstd/src/os/fs.rs
+++ b/sgx_tstd/src/os/fs.rs
@@ -15,19 +15,22 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc;
+//! Linux-specific extensions to primitives in the `std::fs` module.
 
 #[cfg(feature = "untrusted_fs")]
 use crate::fs::Metadata;
+use crate::sys_common::AsInner;
 #[cfg(not(feature = "untrusted_fs"))]
 use crate::untrusted::fs::Metadata;
-use crate::sys_common::AsInner;
 
+#[allow(deprecated)]
 use crate::os::raw;
 
+use sgx_libc as libc;
+
 /// OS-specific extensions to [`fs::Metadata`].
 ///
-/// [`fs::Metadata`]: ../../../../std/fs/struct.Metadata.html
+/// [`fs::Metadata`]: crate::fs::Metadata
 pub trait MetadataExt {
     /// Gain a reference to the underlying `stat` structure which contains
     /// the raw information returned by the OS.
@@ -36,62 +39,292 @@
     /// Unix platforms. The `os::unix::fs::MetadataExt` trait contains the
     /// cross-Unix abstractions contained within the raw stat.
     ///
+    /// [`stat`]: struct@crate::os::linux::raw::stat
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let stat = meta.as_raw_stat();
+    ///     Ok(())
+    /// }
+    /// ```
     fn as_raw_stat(&self) -> &raw::stat;
 
     /// Returns the device ID on which this file resides.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_dev());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_dev(&self) -> u64;
     /// Returns the inode number.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_ino());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_ino(&self) -> u64;
     /// Returns the file type and mode.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_mode());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_mode(&self) -> u32;
     /// Returns the number of hard links to file.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_nlink());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_nlink(&self) -> u64;
     /// Returns the user ID of the file owner.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_uid());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_uid(&self) -> u32;
     /// Returns the group ID of the file owner.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_gid());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_gid(&self) -> u32;
     /// Returns the device ID that this file represents. Only relevant for special file.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_rdev());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_rdev(&self) -> u64;
     /// Returns the size of the file (if it is a regular file or a symbolic link) in bytes.
     ///
     /// The size of a symbolic link is the length of the pathname it contains,
     /// without a terminating null byte.
     ///
-    fn st_size(&self) -> u64;
-    /// Returns the last access time.
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_size());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn st_size(&self) -> u64;
+    /// Returns the last access time of the file, in seconds since Unix Epoch.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_atime());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_atime(&self) -> i64;
     /// Returns the last access time of the file, in nanoseconds since [`st_atime`].
     ///
+    /// [`st_atime`]: Self::st_atime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_atime_nsec());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_atime_nsec(&self) -> i64;
     /// Returns the last modification time of the file, in seconds since Unix Epoch.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_mtime());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_mtime(&self) -> i64;
     /// Returns the last modification time of the file, in nanoseconds since [`st_mtime`].
     ///
+    /// [`st_mtime`]: Self::st_mtime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_mtime_nsec());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_mtime_nsec(&self) -> i64;
     /// Returns the last status change time of the file, in seconds since Unix Epoch.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_ctime());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_ctime(&self) -> i64;
-    /// Returns the last status change time, nano seconds part.
+    /// Returns the last status change time of the file, in nanoseconds since [`st_ctime`].
     ///
+    /// [`st_ctime`]: Self::st_ctime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_ctime_nsec());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_ctime_nsec(&self) -> i64;
-    /// Returns the "preferred" blocksize for efficient filesystem I/O.
+    /// Returns the "preferred" block size for efficient filesystem I/O.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_blksize());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_blksize(&self) -> u64;
     /// Returns the number of blocks allocated to the file, 512-byte units.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::io;
+    /// use std::os::linux::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     println!("{}", meta.st_blocks());
+    ///     Ok(())
+    /// }
+    /// ```
     fn st_blocks(&self) -> u64;
 }
 
 impl MetadataExt for Metadata {
+    #[allow(deprecated)]
     fn as_raw_stat(&self) -> &raw::stat {
         unsafe { &*(self.as_inner().as_inner() as *const libc::stat64 as *const raw::stat) }
     }
diff --git a/sgx_tstd/src/os/mod.rs b/sgx_tstd/src/os/mod.rs
index effaeb4..657af0e 100644
--- a/sgx_tstd/src/os/mod.rs
+++ b/sgx_tstd/src/os/mod.rs
@@ -15,6 +15,10 @@
 // specific language governing permissions and limitations
 // under the License..
 
-pub use crate::sys::ext as unix;
+//! OS-specific functionality.
+
+pub mod fs;
 pub mod raw;
-pub mod fs;
\ No newline at end of file
+pub mod unix;
+
+mod fd;
diff --git a/sgx_tstd/src/os/raw.rs b/sgx_tstd/src/os/raw.rs
index 9fc5e7d..1defce6 100644
--- a/sgx_tstd/src/os/raw.rs
+++ b/sgx_tstd/src/os/raw.rs
@@ -15,6 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
+//! Linux-specific raw type definitions.
+
 pub type c_char = i8;
 pub type c_schar = i8;
 pub type c_uchar = u8;
diff --git a/sgx_tstd/src/os/unix/ffi/mod.rs b/sgx_tstd/src/os/unix/ffi/mod.rs
new file mode 100644
index 0000000..6247043
--- /dev/null
+++ b/sgx_tstd/src/os/unix/ffi/mod.rs
@@ -0,0 +1,54 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Unix-specific extension to the primitives in the `std::ffi` module.
+//!
+//! # Examples
+//!
+//! ```
+//! use std::ffi::OsString;
+//! use std::os::unix::ffi::OsStringExt;
+//!
+//! let bytes = b"foo".to_vec();
+//!
+//! // OsStringExt::from_vec
+//! let os_string = OsString::from_vec(bytes);
+//! assert_eq!(os_string.to_str(), Some("foo"));
+//!
+//! // OsStringExt::into_vec
+//! let bytes = os_string.into_vec();
+//! assert_eq!(bytes, b"foo");
+//! ```
+//!
+//! ```
+//! use std::ffi::OsStr;
+//! use std::os::unix::ffi::OsStrExt;
+//!
+//! let bytes = b"foo";
+//!
+//! // OsStrExt::from_bytes
+//! let os_str = OsStr::from_bytes(bytes);
+//! assert_eq!(os_str.to_str(), Some("foo"));
+//!
+//! // OsStrExt::as_bytes
+//! let bytes = os_str.as_bytes();
+//! assert_eq!(bytes, b"foo");
+//! ```
+
+mod os_str;
+
+pub use self::os_str::{OsStrExt, OsStringExt};
diff --git a/sgx_tstd/src/os/unix/ffi/os_str.rs b/sgx_tstd/src/os/unix/ffi/os_str.rs
new file mode 100644
index 0000000..c06e5ea
--- /dev/null
+++ b/sgx_tstd/src/os/unix/ffi/os_str.rs
@@ -0,0 +1,77 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::ffi::{OsStr, OsString};
+use crate::mem;
+use crate::sealed::Sealed;
+use crate::sys::os_str::Buf;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+
+// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication.
+// Keep this in mind when applying changes to this file that only apply to `unix`.
+
+/// Platform-specific extensions to [`OsString`].
+///
+/// This trait is sealed: it cannot be implemented outside the standard library.
+/// This is so that future additional methods are not breaking changes.
+pub trait OsStringExt: Sealed {
+    /// Creates an [`OsString`] from a byte vector.
+    ///
+    /// See the module documentation for an example.
+    fn from_vec(vec: Vec<u8>) -> Self;
+
+    /// Yields the underlying byte vector of this [`OsString`].
+    ///
+    /// See the module documentation for an example.
+    fn into_vec(self) -> Vec<u8>;
+}
+
+impl OsStringExt for OsString {
+    fn from_vec(vec: Vec<u8>) -> OsString {
+        FromInner::from_inner(Buf { inner: vec })
+    }
+    fn into_vec(self) -> Vec<u8> {
+        self.into_inner().inner
+    }
+}
+
+/// Platform-specific extensions to [`OsStr`].
+///
+/// This trait is sealed: it cannot be implemented outside the standard library.
+/// This is so that future additional methods are not breaking changes.
+pub trait OsStrExt: Sealed {
+    /// Creates an [`OsStr`] from a byte slice.
+    ///
+    /// See the module documentation for an example.
+    fn from_bytes(slice: &[u8]) -> &Self;
+
+    /// Gets the underlying byte view of the [`OsStr`] slice.
+    ///
+    /// See the module documentation for an example.
+    fn as_bytes(&self) -> &[u8];
+}
+
+impl OsStrExt for OsStr {
+    #[inline]
+    fn from_bytes(slice: &[u8]) -> &OsStr {
+        unsafe { mem::transmute(slice) }
+    }
+    #[inline]
+    fn as_bytes(&self) -> &[u8] {
+        &self.as_inner().inner
+    }
+}
diff --git a/sgx_tstd/src/os/unix/fs.rs b/sgx_tstd/src/os/unix/fs.rs
new file mode 100644
index 0000000..c5487a2
--- /dev/null
+++ b/sgx_tstd/src/os/unix/fs.rs
@@ -0,0 +1,909 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Unix-specific extensions to primitives in the `std::fs` module.
+
+use crate::io;
+use crate::os::fs::MetadataExt as _;
+use crate::path::Path;
+use crate::sys;
+use crate::sys_common::{AsInner, AsInnerMut, FromInner};
+use crate::untrusted::fs::{self, OpenOptions, Permissions};
+// Used for `File::read` on intra-doc links
+use crate::ffi::OsStr;
+use crate::sealed::Sealed;
+#[allow(unused_imports)]
+use io::{Read, Write};
+
+use sgx_libc as libc;
+
+/// Unix-specific extensions to [`fs::File`].
+pub trait FileExt {
+    /// Reads a number of bytes starting from a given offset.
+    ///
+    /// Returns the number of bytes read.
+    ///
+    /// The offset is relative to the start of the file and thus independent
+    /// from the current cursor.
+    ///
+    /// The current file cursor is not affected by this function.
+    ///
+    /// Note that similar to [`File::read`], it is not an error to return with a
+    /// short read.
+    ///
+    /// [`File::read`]: fs::File::read
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::fs::File;
+    /// use std::os::unix::prelude::FileExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut buf = [0u8; 8];
+    ///     let file = File::open("foo.txt")?;
+    ///
+    ///     // We now read 8 bytes from the offset 10.
+    ///     let num_bytes_read = file.read_at(&mut buf, 10)?;
+    ///     println!("read {} bytes: {:?}", num_bytes_read, buf);
+    ///     Ok(())
+    /// }
+    /// ```
+    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
+
+    /// Reads the exact number of byte required to fill `buf` from the given offset.
+    ///
+    /// The offset is relative to the start of the file and thus independent
+    /// from the current cursor.
+    ///
+    /// The current file cursor is not affected by this function.
+    ///
+    /// Similar to [`io::Read::read_exact`] but uses [`read_at`] instead of `read`.
+    ///
+    /// [`read_at`]: FileExt::read_at
+    ///
+    /// # Errors
+    ///
+    /// If this function encounters an error of the kind
+    /// [`io::ErrorKind::Interrupted`] then the error is ignored and the operation
+    /// will continue.
+    ///
+    /// If this function encounters an "end of file" before completely filling
+    /// the buffer, it returns an error of the kind [`io::ErrorKind::UnexpectedEof`].
+    /// The contents of `buf` are unspecified in this case.
+    ///
+    /// If any other read error is encountered then this function immediately
+    /// returns. The contents of `buf` are unspecified in this case.
+    ///
+    /// If this function returns an error, it is unspecified how many bytes it
+    /// has read, but it will never read more than would be necessary to
+    /// completely fill the buffer.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::fs::File;
+    /// use std::os::unix::prelude::FileExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut buf = [0u8; 8];
+    ///     let file = File::open("foo.txt")?;
+    ///
+    ///     // We now read exactly 8 bytes from the offset 10.
+    ///     file.read_exact_at(&mut buf, 10)?;
+    ///     println!("read {} bytes: {:?}", buf.len(), buf);
+    ///     Ok(())
+    /// }
+    /// ```
+    fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
+        while !buf.is_empty() {
+            match self.read_at(buf, offset) {
+                Ok(0) => break,
+                Ok(n) => {
+                    let tmp = buf;
+                    buf = &mut tmp[n..];
+                    offset += n as u64;
+                }
+                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
+                Err(e) => return Err(e),
+            }
+        }
+        if !buf.is_empty() {
+            Err(io::Error::new_const(io::ErrorKind::UnexpectedEof, &"failed to fill whole buffer"))
+        } else {
+            Ok(())
+        }
+    }
+
+    /// Writes a number of bytes starting from a given offset.
+    ///
+    /// Returns the number of bytes written.
+    ///
+    /// The offset is relative to the start of the file and thus independent
+    /// from the current cursor.
+    ///
+    /// The current file cursor is not affected by this function.
+    ///
+    /// When writing beyond the end of the file, the file is appropriately
+    /// extended and the intermediate bytes are initialized with the value 0.
+    ///
+    /// Note that similar to [`File::write`], it is not an error to return a
+    /// short write.
+    ///
+    /// [`File::write`]: fs::File::write
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io;
+    /// use std::os::unix::prelude::FileExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let file = File::open("foo.txt")?;
+    ///
+    ///     // We now write at the offset 10.
+    ///     file.write_at(b"sushi", 10)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
+
+    /// Attempts to write an entire buffer starting from a given offset.
+    ///
+    /// The offset is relative to the start of the file and thus independent
+    /// from the current cursor.
+    ///
+    /// The current file cursor is not affected by this function.
+    ///
+    /// This method will continuously call [`write_at`] until there is no more data
+    /// to be written or an error of non-[`io::ErrorKind::Interrupted`] kind is
+    /// returned. This method will not return until the entire buffer has been
+    /// successfully written or such an error occurs. The first error that is
+    /// not of [`io::ErrorKind::Interrupted`] kind generated from this method will be
+    /// returned.
+    ///
+    /// # Errors
+    ///
+    /// This function will return the first error of
+    /// non-[`io::ErrorKind::Interrupted`] kind that [`write_at`] returns.
+    ///
+    /// [`write_at`]: FileExt::write_at
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::io;
+    /// use std::os::unix::prelude::FileExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let file = File::open("foo.txt")?;
+    ///
+    ///     // We now write at the offset 10.
+    ///     file.write_all_at(b"sushi", 10)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
+        while !buf.is_empty() {
+            match self.write_at(buf, offset) {
+                Ok(0) => {
+                    return Err(io::Error::new_const(
+                        io::ErrorKind::WriteZero,
+                        &"failed to write whole buffer",
+                    ));
+                }
+                Ok(n) => {
+                    buf = &buf[n..];
+                    offset += n as u64
+                }
+                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
+                Err(e) => return Err(e),
+            }
+        }
+        Ok(())
+    }
+}
+
+impl FileExt for fs::File {
+    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
+        self.as_inner().read_at(buf, offset)
+    }
+    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
+        self.as_inner().write_at(buf, offset)
+    }
+}
+
+/// Unix-specific extensions to [`fs::Permissions`].
+pub trait PermissionsExt {
+    /// Returns the underlying raw `st_mode` bits that contain the standard
+    /// Unix permissions for this file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::os::unix::fs::PermissionsExt;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::create("foo.txt")?;
+    ///     let metadata = f.metadata()?;
+    ///     let permissions = metadata.permissions();
+    ///
+    ///     println!("permissions: {:o}", permissions.mode());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn mode(&self) -> u32;
+
+    /// Sets the underlying raw bits for this set of permissions.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::File;
+    /// use std::os::unix::fs::PermissionsExt;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let f = File::create("foo.txt")?;
+    ///     let metadata = f.metadata()?;
+    ///     let mut permissions = metadata.permissions();
+    ///
+    ///     permissions.set_mode(0o644); // Read/write for owner and read for others.
+    ///     assert_eq!(permissions.mode(), 0o644);
+    ///     Ok(())
+    /// }
+    /// ```
+    fn set_mode(&mut self, mode: u32);
+
+    /// Creates a new instance of `Permissions` from the given set of Unix
+    /// permission bits.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::fs::Permissions;
+    /// use std::os::unix::fs::PermissionsExt;
+    ///
+    /// // Read/write for owner and read for others.
+    /// let permissions = Permissions::from_mode(0o644);
+    /// assert_eq!(permissions.mode(), 0o644);
+    /// ```
+    fn from_mode(mode: u32) -> Self;
+}
+
+impl PermissionsExt for Permissions {
+    fn mode(&self) -> u32 {
+        self.as_inner().mode()
+    }
+
+    fn set_mode(&mut self, mode: u32) {
+        *self = Permissions::from_inner(FromInner::from_inner(mode));
+    }
+
+    fn from_mode(mode: u32) -> Permissions {
+        Permissions::from_inner(FromInner::from_inner(mode))
+    }
+}
+
+/// Unix-specific extensions to [`fs::OpenOptions`].
+pub trait OpenOptionsExt {
+    /// Sets the mode bits that a new file will be created with.
+    ///
+    /// If a new file is created as part of an `OpenOptions::open` call then this
+    /// specified `mode` will be used as the permission bits for the new file.
+    /// If no `mode` is set, the default of `0o666` will be used.
+    /// The operating system masks out bits with the system's `umask`, to produce
+    /// the final permissions.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::OpenOptions;
+    /// use std::os::unix::fs::OpenOptionsExt;
+    ///
+    /// # fn main() {
+    /// let mut options = OpenOptions::new();
+    /// options.mode(0o644); // Give read/write for owner and read for others.
+    /// let file = options.open("foo.txt");
+    /// # }
+    /// ```
+    fn mode(&mut self, mode: u32) -> &mut Self;
+
+    /// Pass custom flags to the `flags` argument of `open`.
+    ///
+    /// The bits that define the access mode are masked out with `O_ACCMODE`, to
+    /// ensure they do not interfere with the access mode set by Rusts options.
+    ///
+    /// Custom flags can only set flags, not remove flags set by Rusts options.
+    /// This options overwrites any previously set custom flags.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// # #![feature(rustc_private)]
+    /// extern crate libc;
+    /// use std::fs::OpenOptions;
+    /// use std::os::unix::fs::OpenOptionsExt;
+    ///
+    /// # fn main() {
+    /// let mut options = OpenOptions::new();
+    /// options.write(true);
+    /// if cfg!(unix) {
+    ///     options.custom_flags(libc::O_NOFOLLOW);
+    /// }
+    /// let file = options.open("foo.txt");
+    /// # }
+    /// ```
+    fn custom_flags(&mut self, flags: i32) -> &mut Self;
+}
+
+impl OpenOptionsExt for OpenOptions {
+    fn mode(&mut self, mode: u32) -> &mut OpenOptions {
+        self.as_inner_mut().mode(mode);
+        self
+    }
+
+    fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
+        self.as_inner_mut().custom_flags(flags);
+        self
+    }
+}
+
+/// Unix-specific extensions to [`fs::Metadata`].
+pub trait MetadataExt {
+    /// Returns the ID of the device containing the file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let dev_id = meta.dev();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn dev(&self) -> u64;
+    /// Returns the inode number.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let inode = meta.ino();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn ino(&self) -> u64;
+    /// Returns the rights applied to this file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let mode = meta.mode();
+    ///     let user_has_write_access      = mode & 0o200;
+    ///     let user_has_read_write_access = mode & 0o600;
+    ///     let group_has_read_access      = mode & 0o040;
+    ///     let others_have_exec_access    = mode & 0o001;
+    ///     Ok(())
+    /// }
+    /// ```
+    fn mode(&self) -> u32;
+    /// Returns the number of hard links pointing to this file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let nb_hard_links = meta.nlink();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn nlink(&self) -> u64;
+    /// Returns the user ID of the owner of this file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let user_id = meta.uid();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn uid(&self) -> u32;
+    /// Returns the group ID of the owner of this file.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let group_id = meta.gid();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn gid(&self) -> u32;
+    /// Returns the device ID of this file (if it is a special one).
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let device_id = meta.rdev();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn rdev(&self) -> u64;
+    /// Returns the total size of this file in bytes.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let file_size = meta.size();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn size(&self) -> u64;
+    /// Returns the last access time of the file, in seconds since Unix Epoch.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let last_access_time = meta.atime();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn atime(&self) -> i64;
+    /// Returns the last access time of the file, in nanoseconds since [`atime`].
+    ///
+    /// [`atime`]: MetadataExt::atime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let nano_last_access_time = meta.atime_nsec();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn atime_nsec(&self) -> i64;
+    /// Returns the last modification time of the file, in seconds since Unix Epoch.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let last_modification_time = meta.mtime();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn mtime(&self) -> i64;
+    /// Returns the last modification time of the file, in nanoseconds since [`mtime`].
+    ///
+    /// [`mtime`]: MetadataExt::mtime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let nano_last_modification_time = meta.mtime_nsec();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn mtime_nsec(&self) -> i64;
+    /// Returns the last status change time of the file, in seconds since Unix Epoch.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let last_status_change_time = meta.ctime();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn ctime(&self) -> i64;
+    /// Returns the last status change time of the file, in nanoseconds since [`ctime`].
+    ///
+    /// [`ctime`]: MetadataExt::ctime
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let nano_last_status_change_time = meta.ctime_nsec();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn ctime_nsec(&self) -> i64;
+    /// Returns the block size for filesystem I/O.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let block_size = meta.blksize();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn blksize(&self) -> u64;
+    /// Returns the number of blocks allocated to the file, in 512-byte units.
+    ///
+    /// Please note that this may be smaller than `st_size / 512` when the file has holes.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::MetadataExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("some_file")?;
+    ///     let blocks = meta.blocks();
+    ///     Ok(())
+    /// }
+    /// ```
+    fn blocks(&self) -> u64;
+}
+
+impl MetadataExt for fs::Metadata {
+    fn dev(&self) -> u64 {
+        self.st_dev()
+    }
+    fn ino(&self) -> u64 {
+        self.st_ino()
+    }
+    fn mode(&self) -> u32 {
+        self.st_mode()
+    }
+    fn nlink(&self) -> u64 {
+        self.st_nlink()
+    }
+    fn uid(&self) -> u32 {
+        self.st_uid()
+    }
+    fn gid(&self) -> u32 {
+        self.st_gid()
+    }
+    fn rdev(&self) -> u64 {
+        self.st_rdev()
+    }
+    fn size(&self) -> u64 {
+        self.st_size()
+    }
+    fn atime(&self) -> i64 {
+        self.st_atime()
+    }
+    fn atime_nsec(&self) -> i64 {
+        self.st_atime_nsec()
+    }
+    fn mtime(&self) -> i64 {
+        self.st_mtime()
+    }
+    fn mtime_nsec(&self) -> i64 {
+        self.st_mtime_nsec()
+    }
+    fn ctime(&self) -> i64 {
+        self.st_ctime()
+    }
+    fn ctime_nsec(&self) -> i64 {
+        self.st_ctime_nsec()
+    }
+    fn blksize(&self) -> u64 {
+        self.st_blksize()
+    }
+    fn blocks(&self) -> u64 {
+        self.st_blocks()
+    }
+}
+
+/// Unix-specific extensions for [`fs::FileType`].
+///
+/// Adds support for special Unix file types such as block/character devices,
+/// pipes, and sockets.
+pub trait FileTypeExt {
+    /// Returns `true` if this file type is a block device.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::FileTypeExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("block_device_file")?;
+    ///     let file_type = meta.file_type();
+    ///     assert!(file_type.is_block_device());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn is_block_device(&self) -> bool;
+    /// Returns `true` if this file type is a char device.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::FileTypeExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("char_device_file")?;
+    ///     let file_type = meta.file_type();
+    ///     assert!(file_type.is_char_device());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn is_char_device(&self) -> bool;
+    /// Returns `true` if this file type is a fifo.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::FileTypeExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("fifo_file")?;
+    ///     let file_type = meta.file_type();
+    ///     assert!(file_type.is_fifo());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn is_fifo(&self) -> bool;
+    /// Returns `true` if this file type is a socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs;
+    /// use std::os::unix::fs::FileTypeExt;
+    /// use std::io;
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let meta = fs::metadata("unix.socket")?;
+    ///     let file_type = meta.file_type();
+    ///     assert!(file_type.is_socket());
+    ///     Ok(())
+    /// }
+    /// ```
+    fn is_socket(&self) -> bool;
+}
+
+impl FileTypeExt for fs::FileType {
+    fn is_block_device(&self) -> bool {
+        self.as_inner().is(libc::S_IFBLK)
+    }
+    fn is_char_device(&self) -> bool {
+        self.as_inner().is(libc::S_IFCHR)
+    }
+    fn is_fifo(&self) -> bool {
+        self.as_inner().is(libc::S_IFIFO)
+    }
+    fn is_socket(&self) -> bool {
+        self.as_inner().is(libc::S_IFSOCK)
+    }
+}
+
+/// Unix-specific extension methods for [`fs::DirEntry`].
+pub trait DirEntryExt {
+    /// Returns the underlying `d_ino` field in the contained `dirent`
+    /// structure.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::fs;
+    /// use std::os::unix::fs::DirEntryExt;
+    ///
+    /// if let Ok(entries) = fs::read_dir(".") {
+    ///     for entry in entries {
+    ///         if let Ok(entry) = entry {
+    ///             // Here, `entry` is a `DirEntry`.
+    ///             println!("{:?}: {}", entry.file_name(), entry.ino());
+    ///         }
+    ///     }
+    /// }
+    /// ```
+    fn ino(&self) -> u64;
+}
+
+impl DirEntryExt for fs::DirEntry {
+    fn ino(&self) -> u64 {
+        self.as_inner().ino()
+    }
+}
+
+/// Sealed Unix-specific extension methods for [`fs::DirEntry`].
+pub trait DirEntryExt2: Sealed {
+    /// Returns a reference to the underlying `OsStr` of this entry's filename.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(dir_entry_ext2)]
+    /// use std::os::unix::fs::DirEntryExt2;
+    /// use std::{fs, io};
+    ///
+    /// fn main() -> io::Result<()> {
+    ///     let mut entries = fs::read_dir(".")?.collect::<Result<Vec<_>, io::Error>>()?;
+    ///     entries.sort_unstable_by(|a, b| a.file_name_ref().cmp(b.file_name_ref()));
+    ///
+    ///     for p in entries {
+    ///         println!("{:?}", p);
+    ///     }
+    ///
+    ///     Ok(())
+    /// }
+    /// ```
+    fn file_name_ref(&self) -> &OsStr;
+}
+
+/// Allows extension traits within `std`.
+impl Sealed for fs::DirEntry {}
+
+impl DirEntryExt2 for fs::DirEntry {
+    fn file_name_ref(&self) -> &OsStr {
+        self.as_inner().file_name_os_str()
+    }
+}
+
+/// Creates a new symbolic link on the filesystem.
+///
+/// The `link` path will be a symbolic link pointing to the `original` path.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::os::unix::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::symlink("a.txt", "b.txt")?;
+///     Ok(())
+/// }
+/// ```
+pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
+    sys::fs::symlink(original.as_ref(), link.as_ref())
+}
+
+/// Unix-specific extensions to [`fs::DirBuilder`].
+pub trait DirBuilderExt {
+    /// Sets the mode to create new directories with. This option defaults to
+    /// 0o777.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::fs::DirBuilder;
+    /// use std::os::unix::fs::DirBuilderExt;
+    ///
+    /// let mut builder = DirBuilder::new();
+    /// builder.mode(0o755);
+    /// ```
+    fn mode(&mut self, mode: u32) -> &mut Self;
+}
+
+impl DirBuilderExt for fs::DirBuilder {
+    fn mode(&mut self, mode: u32) -> &mut fs::DirBuilder {
+        self.as_inner_mut().set_mode(mode);
+        self
+    }
+}
+
+/// Change the root directory of the current process to the specified path.
+///
+/// This typically requires privileges, such as root or a specific capability.
+///
+/// This does not change the current working directory; you should call
+/// [`std::env::set_current_dir`][`crate::env::set_current_dir`] afterwards.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::os::unix::fs;
+///
+/// fn main() -> std::io::Result<()> {
+///     fs::chroot("/sandbox")?;
+///     std::env::set_current_dir("/")?;
+///     // continue working in sandbox
+///     Ok(())
+/// }
+/// ```
+pub fn chroot<P: AsRef<Path>>(dir: P) -> io::Result<()> {
+    sys::fs::chroot(dir.as_ref())
+}
diff --git a/sgx_tstd/src/sys/ext/ffi.rs b/sgx_tstd/src/os/unix/io/fd.rs
similarity index 86%
rename from sgx_tstd/src/sys/ext/ffi.rs
rename to sgx_tstd/src/os/unix/io/fd.rs
index 52ac67a..f855305 100644
--- a/sgx_tstd/src/sys/ext/ffi.rs
+++ b/sgx_tstd/src/os/unix/io/fd.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific extension to the primitives in the `std::ffi` module
 
-pub use crate::sys_common::os_str_bytes::*;
+//! Owned and borrowed file descriptors.
+
+pub use crate::os::fd::owned::*;
diff --git a/sgx_tstd/src/os/unix/io/mod.rs b/sgx_tstd/src/os/unix/io/mod.rs
new file mode 100644
index 0000000..2ccea5b
--- /dev/null
+++ b/sgx_tstd/src/os/unix/io/mod.rs
@@ -0,0 +1,53 @@
+//! Unix-specific extensions to general I/O primitives.
+//!
+//! Just like raw pointers, raw file descriptors point to resources with
+//! dynamic lifetimes, and they can dangle if they outlive their resources
+//! or be forged if they're created from invalid values.
+//!
+//! This module provides three types for representing file descriptors,
+//! with different ownership properties: raw, borrowed, and owned, which are
+//! analogous to types used for representing pointers:
+//!
+//! | Type               | Analogous to |
+//! | ------------------ | ------------ |
+//! | [`RawFd`]          | `*const _`   |
+//! | [`BorrowedFd<'a>`] | `&'a _`      |
+//! | [`OwnedFd`]        | `Box<_>`     |
+//!
+//! Like raw pointers, `RawFd` values are primitive values. And in new code,
+//! they should be considered unsafe to do I/O on (analogous to dereferencing
+//! them). Rust did not always provide this guidance, so existing code in the
+//! Rust ecosystem often doesn't mark `RawFd` usage as unsafe. Once the
+//! `io_safety` feature is stable, libraries will be encouraged to migrate,
+//! either by adding `unsafe` to APIs that dereference `RawFd` values, or by
+//! using to `BorrowedFd` or `OwnedFd` instead.
+//!
+//! Like references, `BorrowedFd` values are tied to a lifetime, to ensure
+//! that they don't outlive the resource they point to. These are safe to
+//! use. `BorrowedFd` values may be used in APIs which provide safe access to
+//! any system call except for:
+//!  - `close`, because that would end the dynamic lifetime of the resource
+//!    without ending the lifetime of the file descriptor.
+//!  - `dup2`/`dup3`, in the second argument, because this argument is
+//!    closed and assigned a new resource, which may break the assumptions
+//!    other code using that file descriptor.
+//! This list doesn't include `mmap`, since `mmap` does do a proper borrow of
+//! its file descriptor argument. That said, `mmap` is unsafe for other
+//! reasons: it operates on raw pointers, and it can have undefined behavior if
+//! the underlying storage is mutated. Mutations may come from other processes,
+//! or from the same process if the API provides `BorrowedFd` access, since as
+//! mentioned earlier, `BorrowedFd` values may be used in APIs which provide
+//! safe access to any system call. Consequently, code using `mmap` and
+//! presenting a safe API must take full responsibility for ensuring that safe
+//! Rust code cannot evoke undefined behavior through it.
+//!
+//! Like boxes, `OwnedFd` values conceptually own the resource they point to,
+//! and free (close) it when they are dropped.
+//!
+//! [`BorrowedFd<'a>`]: crate::os::unix::io::BorrowedFd
+
+mod fd;
+mod raw;
+
+pub use fd::*;
+pub use raw::*;
diff --git a/sgx_tstd/src/sys/ext/ffi.rs b/sgx_tstd/src/os/unix/io/raw.rs
similarity index 86%
copy from sgx_tstd/src/sys/ext/ffi.rs
copy to sgx_tstd/src/os/unix/io/raw.rs
index 52ac67a..39f947a 100644
--- a/sgx_tstd/src/sys/ext/ffi.rs
+++ b/sgx_tstd/src/os/unix/io/raw.rs
@@ -15,6 +15,6 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific extension to the primitives in the `std::ffi` module
+//! Unix-specific extensions to general I/O primitives.
 
-pub use crate::sys_common::os_str_bytes::*;
+pub use crate::os::fd::raw::*;
diff --git a/sgx_tstd/src/os/unix/mod.rs b/sgx_tstd/src/os/unix/mod.rs
new file mode 100644
index 0000000..1dbd958
--- /dev/null
+++ b/sgx_tstd/src/os/unix/mod.rs
@@ -0,0 +1,73 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Platform-specific extensions to `std` for Unix platforms.
+//!
+//! Provides access to platform-level information on Unix platforms, and
+//! exposes Unix-specific functions that would otherwise be inappropriate as
+//! part of the core `std` library.
+//!
+//! It exposes more ways to deal with platform-specific strings (`OsStr`,
+//! `OsString`), allows to set permissions more granularly, extract low-level
+//! file descriptors from files and sockets, and has platform-specific helpers
+//! for spawning processes.
+//!
+//! # Examples
+//!
+//! ```no_run
+//! use std::fs::File;
+//! use std::os::unix::prelude::*;
+//!
+//! fn main() -> std::io::Result<()> {
+//!     let f = File::create("foo.txt")?;
+//!     let fd = f.as_raw_fd();
+//!
+//!     // use fd with native unix bindings
+//!
+//!     Ok(())
+//! }
+//! ```
+
+pub mod ffi;
+pub mod fs;
+pub mod io;
+#[cfg(feature = "net")]
+pub mod net;
+pub mod raw;
+#[cfg(feature = "thread")]
+pub mod thread;
+#[cfg(feature = "net")]
+pub mod ucred;
+
+/// A prelude for conveniently writing platform-specific code.
+///
+/// Includes all extension traits, and some important type definitions.
+pub mod prelude {
+    #[doc(no_inline)]
+    pub use super::ffi::{OsStrExt, OsStringExt};
+    #[doc(no_inline)]
+    pub use super::fs::DirEntryExt;
+    #[doc(no_inline)]
+    pub use super::fs::FileExt;
+    #[doc(no_inline)]
+    pub use super::fs::{FileTypeExt, MetadataExt, OpenOptionsExt, PermissionsExt};
+    #[doc(no_inline)]
+    pub use super::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
+    #[doc(no_inline)]
+    #[cfg(feature = "thread")]
+    pub use super::thread::JoinHandleExt;
+}
diff --git a/sgx_tstd/src/os/unix/net/addr.rs b/sgx_tstd/src/os/unix/net/addr.rs
new file mode 100644
index 0000000..4f63569
--- /dev/null
+++ b/sgx_tstd/src/os/unix/net/addr.rs
@@ -0,0 +1,225 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::ffi::OsStr;
+use crate::os::unix::ffi::OsStrExt;
+use crate::path::Path;
+use crate::sys::cvt;
+use crate::{ascii, fmt, io, iter, mem};
+use sgx_libc as libc;
+
+fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
+    // Work with an actual instance of the type since using a null pointer is UB
+    let base = addr as *const _ as usize;
+    let path = &addr.sun_path as *const _ as usize;
+    path - base
+}
+
+pub(super) unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
+    let mut addr: libc::sockaddr_un = mem::zeroed();
+    addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
+
+    let bytes = path.as_os_str().as_bytes();
+
+    if bytes.contains(&0) {
+        return Err(io::Error::new_const(
+            io::ErrorKind::InvalidInput,
+            &"paths must not contain interior null bytes",
+        ));
+    }
+
+    if bytes.len() >= addr.sun_path.len() {
+        return Err(io::Error::new_const(
+            io::ErrorKind::InvalidInput,
+            &"path must be shorter than SUN_LEN",
+        ));
+    }
+    for (dst, src) in iter::zip(&mut addr.sun_path, bytes) {
+        *dst = *src as libc::c_char;
+    }
+    // null byte for pathname addresses is already there because we zeroed the
+    // struct
+
+    let mut len = sun_path_offset(&addr) + bytes.len();
+    match bytes.get(0) {
+        Some(&0) | None => {}
+        Some(_) => len += 1,
+    }
+    Ok((addr, len as libc::socklen_t))
+}
+
+enum AddressKind<'a> {
+    Unnamed,
+    Pathname(&'a Path),
+    Abstract(&'a [u8]),
+}
+
+struct AsciiEscaped<'a>(&'a [u8]);
+
+impl<'a> fmt::Display for AsciiEscaped<'a> {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(fmt, "\"")?;
+        for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
+            write!(fmt, "{}", byte as char)?;
+        }
+        write!(fmt, "\"")
+    }
+}
+
+/// An address associated with a Unix socket.
+///
+/// # Examples
+///
+/// ```
+/// use std::os::unix::net::UnixListener;
+///
+/// let socket = match UnixListener::bind("/tmp/sock") {
+///     Ok(sock) => sock,
+///     Err(e) => {
+///         println!("Couldn't bind: {:?}", e);
+///         return
+///     }
+/// };
+/// let addr = socket.local_addr().expect("Couldn't get local address");
+/// ```
+#[derive(Clone)]
+pub struct SocketAddr {
+    addr: libc::sockaddr_un,
+    len: libc::socklen_t,
+}
+
+impl SocketAddr {
+    pub(super) fn new<F>(f: F) -> io::Result<SocketAddr>
+    where
+        F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
+    {
+        unsafe {
+            let mut addr: libc::sockaddr_un = mem::zeroed();
+            let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
+            cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
+            SocketAddr::from_parts(addr, len)
+        }
+    }
+
+    pub(super) fn from_parts(
+        addr: libc::sockaddr_un,
+        mut len: libc::socklen_t,
+    ) -> io::Result<SocketAddr> {
+        if len == 0 {
+            // When there is a datagram from unnamed unix socket
+            // linux returns zero bytes of address
+            len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
+        } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
+            return Err(io::Error::new_const(
+                io::ErrorKind::InvalidInput,
+                &"file descriptor did not correspond to a Unix socket",
+            ));
+        }
+
+        Ok(SocketAddr { addr, len })
+    }
+
+    /// Returns `true` if the address is unnamed.
+    ///
+    /// # Examples
+    ///
+    /// A named address:
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixListener::bind("/tmp/sock")?;
+    ///     let addr = socket.local_addr().expect("Couldn't get local address");
+    ///     assert_eq!(addr.is_unnamed(), false);
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// An unnamed address:
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::unbound()?;
+    ///     let addr = socket.local_addr().expect("Couldn't get local address");
+    ///     assert_eq!(addr.is_unnamed(), true);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn is_unnamed(&self) -> bool {
+        matches!(self.address(), AddressKind::Unnamed)
+    }
+
+    /// Returns the contents of this address if it is a `pathname` address.
+    ///
+    /// # Examples
+    ///
+    /// With a pathname:
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    /// use std::path::Path;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixListener::bind("/tmp/sock")?;
+    ///     let addr = socket.local_addr().expect("Couldn't get local address");
+    ///     assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// Without a pathname:
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::unbound()?;
+    ///     let addr = socket.local_addr().expect("Couldn't get local address");
+    ///     assert_eq!(addr.as_pathname(), None);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn as_pathname(&self) -> Option<&Path> {
+        if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
+    }
+
+    fn address(&self) -> AddressKind<'_> {
+        let len = self.len as usize - sun_path_offset(&self.addr);
+        let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
+
+        if len == 0 {
+            AddressKind::Unnamed
+        } else if self.addr.sun_path[0] == 0 {
+            AddressKind::Abstract(&path[1..len])
+        } else {
+            AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
+        }
+    }
+}
+
+impl fmt::Debug for SocketAddr {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self.address() {
+            AddressKind::Unnamed => write!(fmt, "(unnamed)"),
+            AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
+            AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
+        }
+    }
+}
diff --git a/sgx_tstd/src/os/unix/net/ancillary.rs b/sgx_tstd/src/os/unix/net/ancillary.rs
new file mode 100644
index 0000000..77a7a58
--- /dev/null
+++ b/sgx_tstd/src/os/unix/net/ancillary.rs
@@ -0,0 +1,557 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use super::{sockaddr_un, SocketAddr};
+use crate::convert::TryFrom;
+use crate::io::{self, IoSlice, IoSliceMut};
+use crate::marker::PhantomData;
+use crate::mem::{size_of, zeroed};
+use crate::os::unix::io::RawFd;
+use crate::path::Path;
+use crate::ptr::{eq, read_unaligned};
+use crate::slice::from_raw_parts;
+use crate::sys::net::Socket;
+use sgx_libc as libc;
+
+pub(super) fn recv_vectored_with_ancillary_from(
+    socket: &Socket,
+    bufs: &mut [IoSliceMut<'_>],
+    ancillary: &mut SocketAncillary<'_>,
+) -> io::Result<(usize, bool, io::Result<SocketAddr>)> {
+    unsafe {
+        let mut msg_name: libc::sockaddr_un = zeroed();
+        let mut msg: libc::msghdr = zeroed();
+        msg.msg_name = &mut msg_name as *mut _ as *mut _;
+        msg.msg_namelen = size_of::<libc::sockaddr_un>() as libc::socklen_t;
+        msg.msg_iov = bufs.as_mut_ptr().cast();
+        msg.msg_iovlen = bufs.len() as _;
+        msg.msg_controllen = ancillary.buffer.len() as _;
+        // macos requires that the control pointer is null when the len is 0.
+        if msg.msg_controllen > 0 {
+            msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
+        }
+
+        let count = socket.recv_msg(&mut msg)?;
+
+        ancillary.length = msg.msg_controllen as usize;
+        ancillary.truncated = msg.msg_flags & libc::MSG_CTRUNC == libc::MSG_CTRUNC;
+
+        let truncated = msg.msg_flags & libc::MSG_TRUNC == libc::MSG_TRUNC;
+        let addr = SocketAddr::from_parts(msg_name, msg.msg_namelen);
+
+        Ok((count, truncated, addr))
+    }
+}
+
+pub(super) fn send_vectored_with_ancillary_to(
+    socket: &Socket,
+    path: Option<&Path>,
+    bufs: &[IoSlice<'_>],
+    ancillary: &mut SocketAncillary<'_>,
+) -> io::Result<usize> {
+    unsafe {
+        let (mut msg_name, msg_namelen) =
+            if let Some(path) = path { sockaddr_un(path)? } else { (zeroed(), 0) };
+
+        let mut msg: libc::msghdr = zeroed();
+        msg.msg_name = &mut msg_name as *mut _ as *mut _;
+        msg.msg_namelen = msg_namelen;
+        msg.msg_iov = bufs.as_ptr() as *mut _;
+        msg.msg_iovlen = bufs.len() as _;
+        msg.msg_controllen = ancillary.length as _;
+        // macos requires that the control pointer is null when the len is 0.
+        if msg.msg_controllen > 0 {
+            msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
+        }
+
+        ancillary.truncated = false;
+
+        socket.send_msg(&mut msg)
+    }
+}
+
+fn add_to_ancillary_data<T>(
+    buffer: &mut [u8],
+    length: &mut usize,
+    source: &[T],
+    cmsg_level: libc::c_int,
+    cmsg_type: libc::c_int,
+) -> bool {
+    let source_len = if let Some(source_len) = source.len().checked_mul(size_of::<T>()) {
+        if let Ok(source_len) = u32::try_from(source_len) {
+            source_len
+        } else {
+            return false;
+        }
+    } else {
+        return false;
+    };
+
+    unsafe {
+        let additional_space = libc::CMSG_SPACE(source_len) as usize;
+
+        let new_length = if let Some(new_length) = additional_space.checked_add(*length) {
+            new_length
+        } else {
+            return false;
+        };
+
+        if new_length > buffer.len() {
+            return false;
+        }
+
+        buffer[*length..new_length].fill(0);
+
+        *length = new_length;
+
+        let mut msg: libc::msghdr = zeroed();
+        msg.msg_control = buffer.as_mut_ptr().cast();
+        msg.msg_controllen = *length as _;
+
+        let mut cmsg = libc::CMSG_FIRSTHDR(&msg);
+        let mut previous_cmsg = cmsg;
+        while !cmsg.is_null() {
+            previous_cmsg = cmsg;
+            cmsg = libc::CMSG_NXTHDR(&msg, cmsg);
+
+            // Most operating systems, but not Linux or emscripten, return the previous pointer
+            // when its length is zero. Therefore, check if the previous pointer is the same as
+            // the current one.
+            if eq(cmsg, previous_cmsg) {
+                break;
+            }
+        }
+
+        if previous_cmsg.is_null() {
+            return false;
+        }
+
+        (*previous_cmsg).cmsg_level = cmsg_level;
+        (*previous_cmsg).cmsg_type = cmsg_type;
+        (*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as _;
+
+        let data = libc::CMSG_DATA(previous_cmsg).cast();
+
+        libc::memcpy(data, source.as_ptr().cast(), source_len as usize);
+    }
+    true
+}
+
+struct AncillaryDataIter<'a, T> {
+    data: &'a [u8],
+    phantom: PhantomData<T>,
+}
+
+impl<'a, T> AncillaryDataIter<'a, T> {
+    /// Create `AncillaryDataIter` struct to iterate through the data unit in the control message.
+    ///
+    /// # Safety
+    ///
+    /// `data` must contain a valid control message.
+    unsafe fn new(data: &'a [u8]) -> AncillaryDataIter<'a, T> {
+        AncillaryDataIter { data, phantom: PhantomData }
+    }
+}
+
+impl<'a, T> Iterator for AncillaryDataIter<'a, T> {
+    type Item = T;
+
+    fn next(&mut self) -> Option<T> {
+        if size_of::<T>() <= self.data.len() {
+            unsafe {
+                let unit = read_unaligned(self.data.as_ptr().cast());
+                self.data = &self.data[size_of::<T>()..];
+                Some(unit)
+            }
+        } else {
+            None
+        }
+    }
+}
+
+/// Unix credential.
+#[derive(Clone)]
+pub struct SocketCred(libc::ucred);
+
+impl SocketCred {
+    /// Create a Unix credential struct.
+    ///
+    /// PID, UID and GID is set to 0.
+    pub fn new() -> SocketCred {
+        SocketCred(libc::ucred { pid: 0, uid: 0, gid: 0 })
+    }
+
+    /// Set the PID.
+    pub fn set_pid(&mut self, pid: libc::pid_t) {
+        self.0.pid = pid;
+    }
+
+    /// Get the current PID.
+    pub fn get_pid(&self) -> libc::pid_t {
+        self.0.pid
+    }
+
+    /// Set the UID.
+    pub fn set_uid(&mut self, uid: libc::uid_t) {
+        self.0.uid = uid;
+    }
+
+    /// Get the current UID.
+    pub fn get_uid(&self) -> libc::uid_t {
+        self.0.uid
+    }
+
+    /// Set the GID.
+    pub fn set_gid(&mut self, gid: libc::gid_t) {
+        self.0.gid = gid;
+    }
+
+    /// Get the current GID.
+    pub fn get_gid(&self) -> libc::gid_t {
+        self.0.gid
+    }
+}
+
+/// This control message contains file descriptors.
+///
+/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_RIGHTS`.
+pub struct ScmRights<'a>(AncillaryDataIter<'a, RawFd>);
+
+impl<'a> Iterator for ScmRights<'a> {
+    type Item = RawFd;
+
+    fn next(&mut self) -> Option<RawFd> {
+        self.0.next()
+    }
+}
+
+/// This control message contains unix credentials.
+///
+/// The level is equal to `SOL_SOCKET` and the type is equal to `SCM_CREDENTIALS` or `SCM_CREDS`.
+pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
+
+impl<'a> Iterator for ScmCredentials<'a> {
+    type Item = SocketCred;
+
+    fn next(&mut self) -> Option<SocketCred> {
+        Some(SocketCred(self.0.next()?))
+    }
+}
+
+/// The error type which is returned from parsing the type a control message.
+#[non_exhaustive]
+#[derive(Debug)]
+pub enum AncillaryError {
+    Unknown { cmsg_level: i32, cmsg_type: i32 },
+}
+
+/// This enum represent one control message of variable type.
+pub enum AncillaryData<'a> {
+    ScmRights(ScmRights<'a>),
+    ScmCredentials(ScmCredentials<'a>),
+}
+
+impl<'a> AncillaryData<'a> {
+    /// Create an `AncillaryData::ScmRights` variant.
+    ///
+    /// # Safety
+    ///
+    /// `data` must contain a valid control message and the control message must be type of
+    /// `SOL_SOCKET` and level of `SCM_RIGHTS`.
+    unsafe fn as_rights(data: &'a [u8]) -> Self {
+        let ancillary_data_iter = AncillaryDataIter::new(data);
+        let scm_rights = ScmRights(ancillary_data_iter);
+        AncillaryData::ScmRights(scm_rights)
+    }
+
+    /// Create an `AncillaryData::ScmCredentials` variant.
+    ///
+    /// # Safety
+    ///
+    /// `data` must contain a valid control message and the control message must be type of
+    /// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDENTIALS`.
+    unsafe fn as_credentials(data: &'a [u8]) -> Self {
+        let ancillary_data_iter = AncillaryDataIter::new(data);
+        let scm_credentials = ScmCredentials(ancillary_data_iter);
+        AncillaryData::ScmCredentials(scm_credentials)
+    }
+
+    fn try_from_cmsghdr(cmsg: &'a libc::cmsghdr) -> Result<Self, AncillaryError> {
+        unsafe {
+            let cmsg_len_zero = libc::CMSG_LEN(0) as usize;
+            let data_len = (*cmsg).cmsg_len as usize - cmsg_len_zero;
+            let data = libc::CMSG_DATA(cmsg).cast();
+            let data = from_raw_parts(data, data_len);
+
+            match (*cmsg).cmsg_level {
+                libc::SOL_SOCKET => match (*cmsg).cmsg_type {
+                    libc::SCM_RIGHTS => Ok(AncillaryData::as_rights(data)),
+                    libc::SCM_CREDENTIALS => Ok(AncillaryData::as_credentials(data)),
+                    cmsg_type => {
+                        Err(AncillaryError::Unknown { cmsg_level: libc::SOL_SOCKET, cmsg_type })
+                    }
+                },
+                cmsg_level => {
+                    Err(AncillaryError::Unknown { cmsg_level, cmsg_type: (*cmsg).cmsg_type })
+                }
+            }
+        }
+    }
+}
+
+/// This struct is used to iterate through the control messages.
+pub struct Messages<'a> {
+    buffer: &'a [u8],
+    current: Option<&'a libc::cmsghdr>,
+}
+
+impl<'a> Iterator for Messages<'a> {
+    type Item = Result<AncillaryData<'a>, AncillaryError>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        unsafe {
+            let mut msg: libc::msghdr = zeroed();
+            msg.msg_control = self.buffer.as_ptr() as *mut _;
+            msg.msg_controllen = self.buffer.len() as _;
+
+            let cmsg = if let Some(current) = self.current {
+                libc::CMSG_NXTHDR(&msg, current)
+            } else {
+                libc::CMSG_FIRSTHDR(&msg)
+            };
+
+            let cmsg = cmsg.as_ref()?;
+
+            // Most operating systems, but not Linux or emscripten, return the previous pointer
+            // when its length is zero. Therefore, check if the previous pointer is the same as
+            // the current one.
+            if let Some(current) = self.current {
+                if eq(current, cmsg) {
+                    return None;
+                }
+            }
+
+            self.current = Some(cmsg);
+            let ancillary_result = AncillaryData::try_from_cmsghdr(cmsg);
+            Some(ancillary_result)
+        }
+    }
+}
+
+/// A Unix socket Ancillary data struct.
+///
+/// # Example
+/// ```no_run
+/// #![feature(unix_socket_ancillary_data)]
+/// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
+/// use std::io::IoSliceMut;
+///
+/// fn main() -> std::io::Result<()> {
+///     let sock = UnixStream::connect("/tmp/sock")?;
+///
+///     let mut fds = [0; 8];
+///     let mut ancillary_buffer = [0; 128];
+///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+///
+///     let mut buf = [1; 8];
+///     let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
+///     sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+///
+///     for ancillary_result in ancillary.messages() {
+///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+///             for fd in scm_rights {
+///                 println!("receive file descriptor: {}", fd);
+///             }
+///         }
+///     }
+///     Ok(())
+/// }
+/// ```
+#[derive(Debug)]
+pub struct SocketAncillary<'a> {
+    buffer: &'a mut [u8],
+    length: usize,
+    truncated: bool,
+}
+
+impl<'a> SocketAncillary<'a> {
+    /// Create an ancillary data with the given buffer.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// # #![allow(unused_mut)]
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::SocketAncillary;
+    /// let mut ancillary_buffer = [0; 128];
+    /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    /// ```
+    pub fn new(buffer: &'a mut [u8]) -> Self {
+        SocketAncillary { buffer, length: 0, truncated: false }
+    }
+
+    /// Returns the capacity of the buffer.
+    pub fn capacity(&self) -> usize {
+        self.buffer.len()
+    }
+
+    /// Returns `true` if the ancillary data is empty.
+    pub fn is_empty(&self) -> bool {
+        self.length == 0
+    }
+
+    /// Returns the number of used bytes.
+    pub fn len(&self) -> usize {
+        self.length
+    }
+
+    /// Returns the iterator of the control messages.
+    pub fn messages(&self) -> Messages<'_> {
+        Messages { buffer: &self.buffer[..self.length], current: None }
+    }
+
+    /// Is `true` if during a recv operation the ancillary was truncated.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixStream, SocketAncillary};
+    /// use std::io::IoSliceMut;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixStream::connect("/tmp/sock")?;
+    ///
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///
+    ///     let mut buf = [1; 8];
+    ///     let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
+    ///     sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///
+    ///     println!("Is truncated: {}", ancillary.truncated());
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn truncated(&self) -> bool {
+        self.truncated
+    }
+
+    /// Add file descriptors to the ancillary data.
+    ///
+    /// The function returns `true` if there was enough space in the buffer.
+    /// If there was not enough space then no file descriptors was appended.
+    /// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
+    /// and type `SCM_RIGHTS`.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixStream, SocketAncillary};
+    /// use std::os::unix::io::AsRawFd;
+    /// use std::io::IoSlice;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixStream::connect("/tmp/sock")?;
+    ///
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     ancillary.add_fds(&[sock.as_raw_fd()][..]);
+    ///
+    ///     let mut buf = [1; 8];
+    ///     let mut bufs = &mut [IoSlice::new(&mut buf[..])][..];
+    ///     sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn add_fds(&mut self, fds: &[RawFd]) -> bool {
+        self.truncated = false;
+        add_to_ancillary_data(
+            &mut self.buffer,
+            &mut self.length,
+            fds,
+            libc::SOL_SOCKET,
+            libc::SCM_RIGHTS,
+        )
+    }
+
+    /// Add credentials to the ancillary data.
+    ///
+    /// The function returns `true` if there was enough space in the buffer.
+    /// If there was not enough space then no credentials was appended.
+    /// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
+    /// and type `SCM_CREDENTIALS` or `SCM_CREDS`.
+    ///
+    pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool {
+        self.truncated = false;
+        add_to_ancillary_data(
+            &mut self.buffer,
+            &mut self.length,
+            creds,
+            libc::SOL_SOCKET,
+            libc::SCM_CREDENTIALS,
+        )
+    }
+
+    /// Clears the ancillary data, removing all values.
+    ///
+    /// # Example
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
+    /// use std::io::IoSliceMut;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixStream::connect("/tmp/sock")?;
+    ///
+    ///     let mut fds1 = [0; 8];
+    ///     let mut fds2 = [0; 8];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///
+    ///     let mut buf = [1; 8];
+    ///     let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
+    ///
+    ///     sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///     for ancillary_result in ancillary.messages() {
+    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+    ///             for fd in scm_rights {
+    ///                 println!("receive file descriptor: {}", fd);
+    ///             }
+    ///         }
+    ///     }
+    ///
+    ///     ancillary.clear();
+    ///
+    ///     sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///     for ancillary_result in ancillary.messages() {
+    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+    ///             for fd in scm_rights {
+    ///                 println!("receive file descriptor: {}", fd);
+    ///             }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn clear(&mut self) {
+        self.length = 0;
+        self.truncated = false;
+    }
+}
diff --git a/sgx_tstd/src/os/unix/net/datagram.rs b/sgx_tstd/src/os/unix/net/datagram.rs
new file mode 100644
index 0000000..70032d8
--- /dev/null
+++ b/sgx_tstd/src/os/unix/net/datagram.rs
@@ -0,0 +1,837 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
+use super::{sockaddr_un, SocketAddr};
+use crate::io::{IoSlice, IoSliceMut};
+use crate::net::Shutdown;
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
+use crate::path::Path;
+use crate::sys::cvt;
+use crate::sys::net::Socket;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::time::Duration;
+use crate::{fmt, io};
+
+/// A Unix datagram socket.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::os::unix::net::UnixDatagram;
+///
+/// fn main() -> std::io::Result<()> {
+///     let socket = UnixDatagram::bind("/path/to/my/socket")?;
+///     socket.send_to(b"hello world", "/path/to/other/socket")?;
+///     let mut buf = [0; 100];
+///     let (count, address) = socket.recv_from(&mut buf)?;
+///     println!("socket {:?} sent {:?}", address, &buf[..count]);
+///     Ok(())
+/// }
+/// ```
+pub struct UnixDatagram(Socket);
+
+impl fmt::Debug for UnixDatagram {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut builder = fmt.debug_struct("UnixDatagram");
+        builder.field("fd", self.0.as_inner());
+        if let Ok(addr) = self.local_addr() {
+            builder.field("local", &addr);
+        }
+        if let Ok(addr) = self.peer_addr() {
+            builder.field("peer", &addr);
+        }
+        builder.finish()
+    }
+}
+
+impl UnixDatagram {
+    /// Creates a Unix datagram socket bound to the given path.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
+    ///     Ok(sock) => sock,
+    ///     Err(e) => {
+    ///         println!("Couldn't bind: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
+        unsafe {
+            let socket = UnixDatagram::unbound()?;
+            let (addr, len) = sockaddr_un(path.as_ref())?;
+
+            cvt(libc::bind(socket.as_raw_fd(), &addr as *const _ as *const _, len as _))?;
+
+            Ok(socket)
+        }
+    }
+
+    /// Creates a Unix Datagram socket which is not bound to any address.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// let sock = match UnixDatagram::unbound() {
+    ///     Ok(sock) => sock,
+    ///     Err(e) => {
+    ///         println!("Couldn't unbound: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn unbound() -> io::Result<UnixDatagram> {
+        let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
+        Ok(UnixDatagram(inner))
+    }
+
+    /// Creates an unnamed pair of connected sockets.
+    ///
+    /// Returns two `UnixDatagrams`s which are connected to each other.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// let (sock1, sock2) = match UnixDatagram::pair() {
+    ///     Ok((sock1, sock2)) => (sock1, sock2),
+    ///     Err(e) => {
+    ///         println!("Couldn't unbound: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
+        let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
+        Ok((UnixDatagram(i1), UnixDatagram(i2)))
+    }
+
+    /// Connects the socket to the specified address.
+    ///
+    /// The [`send`] method may be used to send data to the specified address.
+    /// [`recv`] and [`recv_from`] will only receive data from that address.
+    ///
+    /// [`send`]: UnixDatagram::send
+    /// [`recv`]: UnixDatagram::recv
+    /// [`recv_from`]: UnixDatagram::recv_from
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     match sock.connect("/path/to/the/socket") {
+    ///         Ok(sock) => sock,
+    ///         Err(e) => {
+    ///             println!("Couldn't connect: {:?}", e);
+    ///             return Err(e)
+    ///         }
+    ///     };
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
+        unsafe {
+            let (addr, len) = sockaddr_un(path.as_ref())?;
+
+            cvt(libc::connect(self.as_raw_fd(), &addr as *const _ as *const _, len))?;
+        }
+        Ok(())
+    }
+
+    /// Creates a new independently owned handle to the underlying socket.
+    ///
+    /// The returned `UnixDatagram` is a reference to the same socket that this
+    /// object references. Both handles can be used to accept incoming
+    /// connections and options set on one side will affect the other.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
+    ///     let sock_copy = sock.try_clone().expect("try_clone failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn try_clone(&self) -> io::Result<UnixDatagram> {
+        self.0.duplicate().map(UnixDatagram)
+    }
+
+    /// Returns the address of this socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
+    ///     let addr = sock.local_addr().expect("Couldn't get local address");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn local_addr(&self) -> io::Result<SocketAddr> {
+        SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
+    }
+
+    /// Returns the address of this socket's peer.
+    ///
+    /// The [`connect`] method will connect the socket to a peer.
+    ///
+    /// [`connect`]: UnixDatagram::connect
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.connect("/path/to/the/socket")?;
+    ///
+    ///     let addr = sock.peer_addr().expect("Couldn't get peer address");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
+    }
+
+    #[allow(clippy::comparison_chain)]
+    fn recv_from_flags(
+        &self,
+        buf: &mut [u8],
+        flags: libc::c_int,
+    ) -> io::Result<(usize, SocketAddr)> {
+        let mut count = 0;
+        let addr = SocketAddr::new(|addr, len| unsafe {
+            count = libc::recvfrom(
+                self.as_raw_fd(),
+                buf.as_mut_ptr() as *mut _,
+                buf.len(),
+                flags,
+                addr,
+                len,
+            );
+            if count > 0 {
+                1
+            } else if count == 0 {
+                0
+            } else {
+                -1
+            }
+        })?;
+
+        Ok((count as usize, addr))
+    }
+
+    /// Receives data from the socket.
+    ///
+    /// On success, returns the number of bytes read and the address from
+    /// whence the data came.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     let mut buf = vec![0; 10];
+    ///     let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
+    ///     println!("received {} bytes from {:?}", size, sender);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
+        self.recv_from_flags(buf, 0)
+    }
+
+    /// Receives data from the socket.
+    ///
+    /// On success, returns the number of bytes read.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
+    ///     let mut buf = vec![0; 10];
+    ///     sock.recv(buf.as_mut_slice()).expect("recv function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
+        self.0.read(buf)
+    }
+
+    /// Receives data and ancillary data from socket.
+    ///
+    /// On success, returns the number of bytes read, if the data was truncated and the address from whence the msg came.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
+    /// use std::io::IoSliceMut;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     let mut buf1 = [1; 8];
+    ///     let mut buf2 = [2; 16];
+    ///     let mut buf3 = [3; 8];
+    ///     let mut bufs = &mut [
+    ///         IoSliceMut::new(&mut buf1),
+    ///         IoSliceMut::new(&mut buf2),
+    ///         IoSliceMut::new(&mut buf3),
+    ///     ][..];
+    ///     let mut fds = [0; 8];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     let (size, _truncated, sender) = sock.recv_vectored_with_ancillary_from(bufs, &mut ancillary)?;
+    ///     println!("received {}", size);
+    ///     for ancillary_result in ancillary.messages() {
+    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+    ///             for fd in scm_rights {
+    ///                 println!("receive file descriptor: {}", fd);
+    ///             }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn recv_vectored_with_ancillary_from(
+        &self,
+        bufs: &mut [IoSliceMut<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+    ) -> io::Result<(usize, bool, SocketAddr)> {
+        let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
+        let addr = addr?;
+
+        Ok((count, truncated, addr))
+    }
+
+    /// Receives data and ancillary data from socket.
+    ///
+    /// On success, returns the number of bytes read and if the data was truncated.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
+    /// use std::io::IoSliceMut;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     let mut buf1 = [1; 8];
+    ///     let mut buf2 = [2; 16];
+    ///     let mut buf3 = [3; 8];
+    ///     let mut bufs = &mut [
+    ///         IoSliceMut::new(&mut buf1),
+    ///         IoSliceMut::new(&mut buf2),
+    ///         IoSliceMut::new(&mut buf3),
+    ///     ][..];
+    ///     let mut fds = [0; 8];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     let (size, _truncated) = sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///     println!("received {}", size);
+    ///     for ancillary_result in ancillary.messages() {
+    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+    ///             for fd in scm_rights {
+    ///                 println!("receive file descriptor: {}", fd);
+    ///             }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn recv_vectored_with_ancillary(
+        &self,
+        bufs: &mut [IoSliceMut<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+    ) -> io::Result<(usize, bool)> {
+        let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
+        addr?;
+
+        Ok((count, truncated))
+    }
+
+    /// Sends data on the socket to the specified address.
+    ///
+    /// On success, returns the number of bytes written.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
+        unsafe {
+            let (addr, len) = sockaddr_un(path.as_ref())?;
+
+            let count = cvt(libc::sendto(
+                self.as_raw_fd(),
+                buf.as_ptr() as *const _,
+                buf.len(),
+                libc::MSG_NOSIGNAL,
+                &addr as *const _ as *const _,
+                len,
+            ))?;
+            Ok(count as usize)
+        }
+    }
+
+    /// Sends data on the socket to the socket's peer.
+    ///
+    /// The peer address may be set by the `connect` method, and this method
+    /// will return an error if the socket has not already been connected.
+    ///
+    /// On success, returns the number of bytes written.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.connect("/some/sock").expect("Couldn't connect");
+    ///     sock.send(b"omelette au fromage").expect("send_to function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
+        self.0.write(buf)
+    }
+
+    /// Sends data and ancillary data on the socket to the specified address.
+    ///
+    /// On success, returns the number of bytes written.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
+    /// use std::io::IoSlice;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     let buf1 = [1; 8];
+    ///     let buf2 = [2; 16];
+    ///     let buf3 = [3; 8];
+    ///     let bufs = &[
+    ///         IoSlice::new(&buf1),
+    ///         IoSlice::new(&buf2),
+    ///         IoSlice::new(&buf3),
+    ///     ][..];
+    ///     let fds = [0, 1, 2];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     ancillary.add_fds(&fds[..]);
+    ///     sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock")
+    ///         .expect("send_vectored_with_ancillary_to function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
+        &self,
+        bufs: &[IoSlice<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+        path: P,
+    ) -> io::Result<usize> {
+        send_vectored_with_ancillary_to(&self.0, Some(path.as_ref()), bufs, ancillary)
+    }
+
+    /// Sends data and ancillary data on the socket.
+    ///
+    /// On success, returns the number of bytes written.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
+    /// use std::io::IoSlice;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     let buf1 = [1; 8];
+    ///     let buf2 = [2; 16];
+    ///     let buf3 = [3; 8];
+    ///     let bufs = &[
+    ///         IoSlice::new(&buf1),
+    ///         IoSlice::new(&buf2),
+    ///         IoSlice::new(&buf3),
+    ///     ][..];
+    ///     let fds = [0, 1, 2];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     ancillary.add_fds(&fds[..]);
+    ///     sock.send_vectored_with_ancillary(bufs, &mut ancillary)
+    ///         .expect("send_vectored_with_ancillary function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn send_vectored_with_ancillary(
+        &self,
+        bufs: &[IoSlice<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+    ) -> io::Result<usize> {
+        send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
+    }
+
+    /// Sets the read timeout for the socket.
+    ///
+    /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
+    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
+    /// is passed to this method.
+    ///
+    /// [`recv`]: UnixDatagram::recv
+    /// [`recv_from`]: UnixDatagram::recv_from
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("set_read_timeout function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::unbound()?;
+    ///     let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
+    ///     let err = result.unwrap_err();
+    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
+        self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
+    }
+
+    /// Sets the write timeout for the socket.
+    ///
+    /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
+    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method.
+    ///
+    /// [`send`]: UnixDatagram::send
+    /// [`send_to`]: UnixDatagram::send_to
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("set_write_timeout function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::unbound()?;
+    ///     let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
+    ///     let err = result.unwrap_err();
+    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
+        self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
+    }
+
+    /// Returns the read timeout of this socket.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("set_read_timeout function failed");
+    ///     assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
+        self.0.timeout(libc::SO_RCVTIMEO)
+    }
+
+    /// Returns the write timeout of this socket.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("set_write_timeout function failed");
+    ///     assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
+        self.0.timeout(libc::SO_SNDTIMEO)
+    }
+
+    /// Moves the socket into or out of nonblocking mode.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_nonblocking(true).expect("set_nonblocking function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
+        self.0.set_nonblocking(nonblocking)
+    }
+
+    /// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
+    ///
+    /// Set the socket option `SO_PASSCRED`.
+    ///
+    /// # Examples
+    ///
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.set_passcred(true).expect("set_passcred function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
+        self.0.set_passcred(passcred)
+    }
+
+    /// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
+    /// This value can be change by [`set_passcred`].
+    ///
+    /// Get the socket option `SO_PASSCRED`.
+    ///
+    /// [`set_passcred`]: UnixDatagram::set_passcred
+    pub fn passcred(&self) -> io::Result<bool> {
+        self.0.passcred()
+    }
+
+    /// Returns the value of the `SO_ERROR` option.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     if let Ok(Some(err)) = sock.take_error() {
+    ///         println!("Got error: {:?}", err);
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
+        self.0.take_error()
+    }
+
+    /// Shut down the read, write, or both halves of this connection.
+    ///
+    /// This function will cause all pending and future I/O calls on the
+    /// specified portions to immediately return with an appropriate value
+    /// (see the documentation of [`Shutdown`]).
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixDatagram;
+    /// use std::net::Shutdown;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let sock = UnixDatagram::unbound()?;
+    ///     sock.shutdown(Shutdown::Both).expect("shutdown function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
+        self.0.shutdown(how)
+    }
+
+    /// Receives data on the socket from the remote address to which it is
+    /// connected, without removing that data from the queue. On success,
+    /// returns the number of bytes peeked.
+    ///
+    /// Successive calls return the same data. This is accomplished by passing
+    /// `MSG_PEEK` as a flag to the underlying `recv` system call.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_peek)]
+    ///
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::bind("/tmp/sock")?;
+    ///     let mut buf = [0; 10];
+    ///     let len = socket.peek(&mut buf).expect("peek failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
+        self.0.peek(buf)
+    }
+
+    /// Receives a single datagram message on the socket, without removing it from the
+    /// queue. On success, returns the number of bytes read and the origin.
+    ///
+    /// The function must be called with valid byte array `buf` of sufficient size to
+    /// hold the message bytes. If a message is too long to fit in the supplied buffer,
+    /// excess bytes may be discarded.
+    ///
+    /// Successive calls return the same data. This is accomplished by passing
+    /// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
+    ///
+    /// Do not use this function to implement busy waiting, instead use `libc::poll` to
+    /// synchronize IO events on one or more sockets.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_peek)]
+    ///
+    /// use std::os::unix::net::UnixDatagram;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixDatagram::bind("/tmp/sock")?;
+    ///     let mut buf = [0; 10];
+    ///     let (len, addr) = socket.peek_from(&mut buf).expect("peek failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
+        self.recv_from_flags(buf, libc::MSG_PEEK)
+    }
+}
+
+impl AsRawFd for UnixDatagram {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_inner().as_raw_fd()
+    }
+}
+
+impl FromRawFd for UnixDatagram {
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
+        UnixDatagram(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
+    }
+}
+
+impl IntoRawFd for UnixDatagram {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_inner().into_inner().into_raw_fd()
+    }
+}
+
+impl AsFd for UnixDatagram {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_inner().as_fd()
+    }
+}
+
+impl From<UnixDatagram> for OwnedFd {
+    #[inline]
+    fn from(unix_datagram: UnixDatagram) -> OwnedFd {
+        unsafe { OwnedFd::from_raw_fd(unix_datagram.into_raw_fd()) }
+    }
+}
+
+impl From<OwnedFd> for UnixDatagram {
+    #[inline]
+    fn from(owned: OwnedFd) -> Self {
+        unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
+    }
+}
+
+mod libc {
+    pub use sgx_libc::ocall::{
+        bind, connect, getpeername, getsockname, recvfrom, sendto,
+    };
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/os/unix/net/listener.rs b/sgx_tstd/src/os/unix/net/listener.rs
new file mode 100644
index 0000000..e9905c5
--- /dev/null
+++ b/sgx_tstd/src/os/unix/net/listener.rs
@@ -0,0 +1,350 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use super::{sockaddr_un, SocketAddr, UnixStream};
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
+use crate::path::Path;
+use crate::sys::cvt;
+use crate::sys::net::Socket;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::{fmt, io, mem};
+
+/// A structure representing a Unix domain socket server.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::thread;
+/// use std::os::unix::net::{UnixStream, UnixListener};
+///
+/// fn handle_client(stream: UnixStream) {
+///     // ...
+/// }
+///
+/// fn main() -> std::io::Result<()> {
+///     let listener = UnixListener::bind("/path/to/the/socket")?;
+///
+///     // accept connections and process them, spawning a new thread for each one
+///     for stream in listener.incoming() {
+///         match stream {
+///             Ok(stream) => {
+///                 /* connection succeeded */
+///                 thread::spawn(|| handle_client(stream));
+///             }
+///             Err(err) => {
+///                 /* connection failed */
+///                 break;
+///             }
+///         }
+///     }
+///     Ok(())
+/// }
+/// ```
+pub struct UnixListener(Socket);
+
+impl fmt::Debug for UnixListener {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut builder = fmt.debug_struct("UnixListener");
+        builder.field("fd", self.0.as_inner());
+        if let Ok(addr) = self.local_addr() {
+            builder.field("local", &addr);
+        }
+        builder.finish()
+    }
+}
+
+impl UnixListener {
+    /// Creates a new `UnixListener` bound to the specified socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// let listener = match UnixListener::bind("/path/to/the/socket") {
+    ///     Ok(sock) => sock,
+    ///     Err(e) => {
+    ///         println!("Couldn't connect: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
+        unsafe {
+            let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
+            let (addr, len) = sockaddr_un(path.as_ref())?;
+
+            cvt(libc::bind(inner.as_inner().as_raw_fd(), &addr as *const _ as *const _, len as _))?;
+            cvt(libc::listen(inner.as_inner().as_raw_fd(), 128))?;
+
+            Ok(UnixListener(inner))
+        }
+    }
+
+    /// Accepts a new incoming connection to this listener.
+    ///
+    /// This function will block the calling thread until a new Unix connection
+    /// is established. When established, the corresponding [`UnixStream`] and
+    /// the remote peer's address will be returned.
+    ///
+    /// [`UnixStream`]: crate::os::unix::net::UnixStream
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/path/to/the/socket")?;
+    ///
+    ///     match listener.accept() {
+    ///         Ok((socket, addr)) => println!("Got a client: {:?}", addr),
+    ///         Err(e) => println!("accept function failed: {:?}", e),
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
+        let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
+        let mut len = mem::size_of_val(&storage) as libc::socklen_t;
+        let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
+        let addr = SocketAddr::from_parts(storage, len)?;
+        Ok((UnixStream(sock), addr))
+    }
+
+    /// Creates a new independently owned handle to the underlying socket.
+    ///
+    /// The returned `UnixListener` is a reference to the same socket that this
+    /// object references. Both handles can be used to accept incoming
+    /// connections and options set on one listener will affect the other.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/path/to/the/socket")?;
+    ///     let listener_copy = listener.try_clone().expect("try_clone failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn try_clone(&self) -> io::Result<UnixListener> {
+        self.0.duplicate().map(UnixListener)
+    }
+
+    /// Returns the local socket address of this listener.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/path/to/the/socket")?;
+    ///     let addr = listener.local_addr().expect("Couldn't get local address");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn local_addr(&self) -> io::Result<SocketAddr> {
+        SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
+    }
+
+    /// Moves the socket into or out of nonblocking mode.
+    ///
+    /// This will result in the `accept` operation becoming nonblocking,
+    /// i.e., immediately returning from their calls. If the IO operation is
+    /// successful, `Ok` is returned and no further action is required. If the
+    /// IO operation could not be completed and needs to be retried, an error
+    /// with kind [`io::ErrorKind::WouldBlock`] is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/path/to/the/socket")?;
+    ///     listener.set_nonblocking(true).expect("Couldn't set non blocking");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
+        self.0.set_nonblocking(nonblocking)
+    }
+
+    /// Returns the value of the `SO_ERROR` option.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixListener;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/tmp/sock")?;
+    ///
+    ///     if let Ok(Some(err)) = listener.take_error() {
+    ///         println!("Got error: {:?}", err);
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// # Platform specific
+    /// On Redox this always returns `None`.
+    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
+        self.0.take_error()
+    }
+
+    /// Returns an iterator over incoming connections.
+    ///
+    /// The iterator will never return [`None`] and will also not yield the
+    /// peer's [`SocketAddr`] structure.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::thread;
+    /// use std::os::unix::net::{UnixStream, UnixListener};
+    ///
+    /// fn handle_client(stream: UnixStream) {
+    ///     // ...
+    /// }
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let listener = UnixListener::bind("/path/to/the/socket")?;
+    ///
+    ///     for stream in listener.incoming() {
+    ///         match stream {
+    ///             Ok(stream) => {
+    ///                 thread::spawn(|| handle_client(stream));
+    ///             }
+    ///             Err(err) => {
+    ///                 break;
+    ///             }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn incoming(&self) -> Incoming<'_> {
+        Incoming { listener: self }
+    }
+}
+
+impl AsRawFd for UnixListener {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_inner().as_raw_fd()
+    }
+}
+
+impl FromRawFd for UnixListener {
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
+        UnixListener(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
+    }
+}
+
+impl IntoRawFd for UnixListener {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_inner().into_inner().into_raw_fd()
+    }
+}
+
+impl AsFd for UnixListener {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_inner().as_fd()
+    }
+}
+
+impl From<OwnedFd> for UnixListener {
+    #[inline]
+    fn from(fd: OwnedFd) -> UnixListener {
+        UnixListener(Socket::from_inner(FromInner::from_inner(fd)))
+    }
+}
+
+impl From<UnixListener> for OwnedFd {
+    #[inline]
+    fn from(listener: UnixListener) -> OwnedFd {
+        listener.0.into_inner().into_inner()
+    }
+}
+
+impl<'a> IntoIterator for &'a UnixListener {
+    type Item = io::Result<UnixStream>;
+    type IntoIter = Incoming<'a>;
+
+    fn into_iter(self) -> Incoming<'a> {
+        self.incoming()
+    }
+}
+
+/// An iterator over incoming connections to a [`UnixListener`].
+///
+/// It will never return [`None`].
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::thread;
+/// use std::os::unix::net::{UnixStream, UnixListener};
+///
+/// fn handle_client(stream: UnixStream) {
+///     // ...
+/// }
+///
+/// fn main() -> std::io::Result<()> {
+///     let listener = UnixListener::bind("/path/to/the/socket")?;
+///
+///     for stream in listener.incoming() {
+///         match stream {
+///             Ok(stream) => {
+///                 thread::spawn(|| handle_client(stream));
+///             }
+///             Err(err) => {
+///                 break;
+///             }
+///         }
+///     }
+///     Ok(())
+/// }
+/// ```
+#[derive(Debug)]
+pub struct Incoming<'a> {
+    listener: &'a UnixListener,
+}
+
+impl<'a> Iterator for Incoming<'a> {
+    type Item = io::Result<UnixStream>;
+
+    fn next(&mut self) -> Option<io::Result<UnixStream>> {
+        Some(self.listener.accept().map(|s| s.0))
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (usize::MAX, None)
+    }
+}
+
+mod libc {
+    pub use sgx_libc::ocall::{bind, getsockname, listen};
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/sys/ext/ffi.rs b/sgx_tstd/src/os/unix/net/mod.rs
similarity index 76%
copy from sgx_tstd/src/sys/ext/ffi.rs
copy to sgx_tstd/src/os/unix/net/mod.rs
index 52ac67a..a258b3f 100644
--- a/sgx_tstd/src/sys/ext/ffi.rs
+++ b/sgx_tstd/src/os/unix/net/mod.rs
@@ -15,6 +15,16 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific extension to the primitives in the `std::ffi` module
+//! Unix-specific networking functionality
 
-pub use crate::sys_common::os_str_bytes::*;
+mod addr;
+mod ancillary;
+mod datagram;
+mod listener;
+mod stream;
+
+pub use self::addr::*;
+pub use self::ancillary::*;
+pub use self::datagram::*;
+pub use self::listener::*;
+pub use self::stream::*;
diff --git a/sgx_tstd/src/os/unix/net/stream.rs b/sgx_tstd/src/os/unix/net/stream.rs
new file mode 100644
index 0000000..9d51225
--- /dev/null
+++ b/sgx_tstd/src/os/unix/net/stream.rs
@@ -0,0 +1,633 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
+use super::{sockaddr_un, SocketAddr};
+use crate::fmt;
+use crate::io::{self, Initializer, IoSlice, IoSliceMut};
+use crate::net::Shutdown;
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
+use crate::os::unix::ucred;
+use crate::path::Path;
+use crate::sys::cvt;
+use crate::sys::net::Socket;
+use crate::sys_common::{AsInner, FromInner};
+use crate::time::Duration;
+
+pub use ucred::UCred;
+
+/// A Unix stream socket.
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::os::unix::net::UnixStream;
+/// use std::io::prelude::*;
+///
+/// fn main() -> std::io::Result<()> {
+///     let mut stream = UnixStream::connect("/path/to/my/socket")?;
+///     stream.write_all(b"hello world")?;
+///     let mut response = String::new();
+///     stream.read_to_string(&mut response)?;
+///     println!("{}", response);
+///     Ok(())
+/// }
+/// ```
+pub struct UnixStream(pub(super) Socket);
+
+impl fmt::Debug for UnixStream {
+    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut builder = fmt.debug_struct("UnixStream");
+        builder.field("fd", self.0.as_inner());
+        if let Ok(addr) = self.local_addr() {
+            builder.field("local", &addr);
+        }
+        if let Ok(addr) = self.peer_addr() {
+            builder.field("peer", &addr);
+        }
+        builder.finish()
+    }
+}
+
+impl UnixStream {
+    /// Connects to the socket named by `path`.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// let socket = match UnixStream::connect("/tmp/sock") {
+    ///     Ok(sock) => sock,
+    ///     Err(e) => {
+    ///         println!("Couldn't connect: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
+        unsafe {
+            let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
+            let (addr, len) = sockaddr_un(path.as_ref())?;
+
+            cvt(libc::connect(inner.as_raw_fd(), &addr as *const _ as *const _, len))?;
+            Ok(UnixStream(inner))
+        }
+    }
+
+    /// Creates an unnamed pair of connected sockets.
+    ///
+    /// Returns two `UnixStream`s which are connected to each other.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// let (sock1, sock2) = match UnixStream::pair() {
+    ///     Ok((sock1, sock2)) => (sock1, sock2),
+    ///     Err(e) => {
+    ///         println!("Couldn't create a pair of sockets: {:?}", e);
+    ///         return
+    ///     }
+    /// };
+    /// ```
+    pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
+        let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
+        Ok((UnixStream(i1), UnixStream(i2)))
+    }
+
+    /// Creates a new independently owned handle to the underlying socket.
+    ///
+    /// The returned `UnixStream` is a reference to the same stream that this
+    /// object references. Both handles will read and write the same stream of
+    /// data, and options set on one stream will be propagated to the other
+    /// stream.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let sock_copy = socket.try_clone().expect("Couldn't clone socket");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn try_clone(&self) -> io::Result<UnixStream> {
+        self.0.duplicate().map(UnixStream)
+    }
+
+    /// Returns the socket address of the local half of this connection.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let addr = socket.local_addr().expect("Couldn't get local address");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn local_addr(&self) -> io::Result<SocketAddr> {
+        SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
+    }
+
+    /// Returns the socket address of the remote half of this connection.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let addr = socket.peer_addr().expect("Couldn't get peer address");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
+        SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
+    }
+
+    /// Gets the peer credentials for this Unix domain socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(peer_credentials_unix_socket)]
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peer_cred(&self) -> io::Result<UCred> {
+        ucred::peer_cred(self)
+    }
+
+    /// Sets the read timeout for the socket.
+    ///
+    /// If the provided value is [`None`], then [`read`] calls will block
+    /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method.
+    ///
+    /// [`read`]: io::Read::read
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::os::unix::net::UnixStream;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
+    ///     let err = result.unwrap_err();
+    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
+        self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
+    }
+
+    /// Sets the write timeout for the socket.
+    ///
+    /// If the provided value is [`None`], then [`write`] calls will block
+    /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
+    /// passed to this method.
+    ///
+    /// [`read`]: io::Read::read
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_write_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("Couldn't set write timeout");
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
+    /// method:
+    ///
+    /// ```no_run
+    /// use std::io;
+    /// use std::net::UdpSocket;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UdpSocket::bind("127.0.0.1:34254")?;
+    ///     let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
+    ///     let err = result.unwrap_err();
+    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
+        self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
+    }
+
+    /// Returns the read timeout of this socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
+    ///     assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
+        self.0.timeout(libc::SO_RCVTIMEO)
+    }
+
+    /// Returns the write timeout of this socket.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    /// use std::time::Duration;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_write_timeout(Some(Duration::new(1, 0)))
+    ///         .expect("Couldn't set write timeout");
+    ///     assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
+        self.0.timeout(libc::SO_SNDTIMEO)
+    }
+
+    /// Moves the socket into or out of nonblocking mode.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_nonblocking(true).expect("Couldn't set nonblocking");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
+        self.0.set_nonblocking(nonblocking)
+    }
+
+    /// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
+    ///
+    /// Set the socket option `SO_PASSCRED`.
+    ///
+    /// # Examples
+    ///
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.set_passcred(true).expect("Couldn't set passcred");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
+        self.0.set_passcred(passcred)
+    }
+
+    /// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
+    /// This value can be change by [`set_passcred`].
+    ///
+    /// Get the socket option `SO_PASSCRED`.
+    ///
+    /// [`set_passcred`]: UnixStream::set_passcred
+    pub fn passcred(&self) -> io::Result<bool> {
+        self.0.passcred()
+    }
+
+    /// Returns the value of the `SO_ERROR` option.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     if let Ok(Some(err)) = socket.take_error() {
+    ///         println!("Got error: {:?}", err);
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    ///
+    /// # Platform specific
+    /// On Redox this always returns `None`.
+    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
+        self.0.take_error()
+    }
+
+    /// Shuts down the read, write, or both halves of this connection.
+    ///
+    /// This function will cause all pending and future I/O calls on the
+    /// specified portions to immediately return with an appropriate value
+    /// (see the documentation of [`Shutdown`]).
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::os::unix::net::UnixStream;
+    /// use std::net::Shutdown;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     socket.shutdown(Shutdown::Both).expect("shutdown function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
+        self.0.shutdown(how)
+    }
+
+    /// Receives data on the socket from the remote address to which it is
+    /// connected, without removing that data from the queue. On success,
+    /// returns the number of bytes peeked.
+    ///
+    /// Successive calls return the same data. This is accomplished by passing
+    /// `MSG_PEEK` as a flag to the underlying `recv` system call.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_peek)]
+    ///
+    /// use std::os::unix::net::UnixStream;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let mut buf = [0; 10];
+    ///     let len = socket.peek(&mut buf).expect("peek failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
+        self.0.peek(buf)
+    }
+
+    /// Receives data and ancillary data from socket.
+    ///
+    /// On success, returns the number of bytes read.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
+    /// use std::io::IoSliceMut;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let mut buf1 = [1; 8];
+    ///     let mut buf2 = [2; 16];
+    ///     let mut buf3 = [3; 8];
+    ///     let mut bufs = &mut [
+    ///         IoSliceMut::new(&mut buf1),
+    ///         IoSliceMut::new(&mut buf2),
+    ///         IoSliceMut::new(&mut buf3),
+    ///     ][..];
+    ///     let mut fds = [0; 8];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     let size = socket.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
+    ///     println!("received {}", size);
+    ///     for ancillary_result in ancillary.messages() {
+    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
+    ///             for fd in scm_rights {
+    ///                 println!("receive file descriptor: {}", fd);
+    ///             }
+    ///         }
+    ///     }
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn recv_vectored_with_ancillary(
+        &self,
+        bufs: &mut [IoSliceMut<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+    ) -> io::Result<usize> {
+        let (count, _, _) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
+
+        Ok(count)
+    }
+
+    /// Sends data and ancillary data on the socket.
+    ///
+    /// On success, returns the number of bytes written.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(unix_socket_ancillary_data)]
+    /// use std::os::unix::net::{UnixStream, SocketAncillary};
+    /// use std::io::IoSlice;
+    ///
+    /// fn main() -> std::io::Result<()> {
+    ///     let socket = UnixStream::connect("/tmp/sock")?;
+    ///     let buf1 = [1; 8];
+    ///     let buf2 = [2; 16];
+    ///     let buf3 = [3; 8];
+    ///     let bufs = &[
+    ///         IoSlice::new(&buf1),
+    ///         IoSlice::new(&buf2),
+    ///         IoSlice::new(&buf3),
+    ///     ][..];
+    ///     let fds = [0, 1, 2];
+    ///     let mut ancillary_buffer = [0; 128];
+    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
+    ///     ancillary.add_fds(&fds[..]);
+    ///     socket.send_vectored_with_ancillary(bufs, &mut ancillary)
+    ///         .expect("send_vectored_with_ancillary function failed");
+    ///     Ok(())
+    /// }
+    /// ```
+    pub fn send_vectored_with_ancillary(
+        &self,
+        bufs: &[IoSlice<'_>],
+        ancillary: &mut SocketAncillary<'_>,
+    ) -> io::Result<usize> {
+        send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
+    }
+}
+
+impl io::Read for UnixStream {
+    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+        io::Read::read(&mut &*self, buf)
+    }
+
+    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+        io::Read::read_vectored(&mut &*self, bufs)
+    }
+
+    #[inline]
+    fn is_read_vectored(&self) -> bool {
+        io::Read::is_read_vectored(&&*self)
+    }
+
+    #[inline]
+    unsafe fn initializer(&self) -> Initializer {
+        Initializer::nop()
+    }
+}
+
+impl<'a> io::Read for &'a UnixStream {
+    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+        self.0.read(buf)
+    }
+
+    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+        self.0.read_vectored(bufs)
+    }
+
+    #[inline]
+    fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
+    #[inline]
+    unsafe fn initializer(&self) -> Initializer {
+        Initializer::nop()
+    }
+}
+
+impl io::Write for UnixStream {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        io::Write::write(&mut &*self, buf)
+    }
+
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        io::Write::write_vectored(&mut &*self, bufs)
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        io::Write::is_write_vectored(&&*self)
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        io::Write::flush(&mut &*self)
+    }
+}
+
+impl<'a> io::Write for &'a UnixStream {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        self.0.write(buf)
+    }
+
+    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+        self.0.write_vectored(bufs)
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        Ok(())
+    }
+}
+
+impl AsRawFd for UnixStream {
+    #[inline]
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_raw_fd()
+    }
+}
+
+impl FromRawFd for UnixStream {
+    #[inline]
+    unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
+        UnixStream(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
+    }
+}
+
+impl IntoRawFd for UnixStream {
+    #[inline]
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_raw_fd()
+    }
+}
+
+impl AsFd for UnixStream {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_fd()
+    }
+}
+
+impl From<UnixStream> for OwnedFd {
+    #[inline]
+    fn from(unix_stream: UnixStream) -> OwnedFd {
+        unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) }
+    }
+}
+
+impl From<OwnedFd> for UnixStream {
+    #[inline]
+    fn from(owned: OwnedFd) -> Self {
+        unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
+    }
+}
+
+mod libc {
+    pub use sgx_libc::ocall::{connect, getpeername, getsockname};
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/sys/ext/raw.rs b/sgx_tstd/src/os/unix/raw.rs
similarity index 83%
rename from sgx_tstd/src/sys/ext/raw.rs
rename to sgx_tstd/src/os/unix/raw.rs
index b5a4746..8b094ba 100644
--- a/sgx_tstd/src/sys/ext/raw.rs
+++ b/sgx_tstd/src/os/unix/raw.rs
@@ -15,12 +15,18 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific primitives available on all unix platforms
+//! Unix-specific primitives available on all unix platforms.
 
+#[allow(non_camel_case_types)]
 pub type uid_t = u32;
+#[allow(non_camel_case_types)]
 pub type gid_t = u32;
+#[allow(non_camel_case_types)]
 pub type pid_t = i32;
 
+#[doc(inline)]
 pub use crate::os::raw::pthread_t;
+#[doc(inline)]
 pub use crate::os::raw::{blkcnt_t, time_t};
+#[doc(inline)]
 pub use crate::os::raw::{blksize_t, dev_t, ino_t, mode_t, nlink_t, off_t};
diff --git a/sgx_tstd/src/sys/ext/thread.rs b/sgx_tstd/src/os/unix/thread.rs
similarity index 90%
rename from sgx_tstd/src/sys/ext/thread.rs
rename to sgx_tstd/src/os/unix/thread.rs
index 3d55aa4..08127d4 100644
--- a/sgx_tstd/src/sys/ext/thread.rs
+++ b/sgx_tstd/src/os/unix/thread.rs
@@ -17,15 +17,15 @@
 
 //! Unix-specific extensions to primitives in the `std::thread` module.
 
-use crate::os::raw::pthread_t;
+#[allow(deprecated)]
+use crate::os::unix::raw::pthread_t;
 use crate::sys_common::{AsInner, IntoInner};
 use crate::thread::JoinHandle;
 
+#[allow(deprecated)]
 pub type RawPthread = pthread_t;
 
-/// Unix-specific extensions to [`thread::JoinHandle`].
-///
-/// [`thread::JoinHandle`]: ../../../../std/thread/struct.JoinHandle.html
+/// Unix-specific extensions to [`JoinHandle`].
 pub trait JoinHandleExt {
     /// Extracts the raw pthread_t without taking ownership
     fn as_pthread_t(&self) -> RawPthread;
diff --git a/sgx_tstd/src/os/unix/ucred.rs b/sgx_tstd/src/os/unix/ucred.rs
new file mode 100644
index 0000000..dad14b6
--- /dev/null
+++ b/sgx_tstd/src/os/unix/ucred.rs
@@ -0,0 +1,81 @@
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Unix peer credentials.
+
+// NOTE: Code in this file is heavily based on work done in PR 13 from the tokio-uds repository on
+//       GitHub.
+//
+//       For reference, the link is here: https://github.com/tokio-rs/tokio-uds/pull/13
+//       Credit to Martin Habovštiak (GitHub username Kixunil) and contributors for this work.
+
+use sgx_libc::{gid_t, pid_t, uid_t};
+
+/// Credentials for a UNIX process for credentials passing.
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
+pub struct UCred {
+    /// The UID part of the peer credential. This is the effective UID of the process at the domain
+    /// socket's endpoint.
+    pub uid: uid_t,
+    /// The GID part of the peer credential. This is the effective GID of the process at the domain
+    /// socket's endpoint.
+    pub gid: gid_t,
+    /// The PID part of the peer credential. This field is optional because the PID part of the
+    /// peer credentials is not supported on every platform. On platforms where the mechanism to
+    /// discover the PID exists, this field will be populated to the PID of the process at the
+    /// domain socket's endpoint. Otherwise, it will be set to None.
+    pub pid: Option<pid_t>,
+}
+
+pub use self::impl_linux::peer_cred;
+
+pub mod impl_linux {
+    use super::UCred;
+    use crate::os::unix::io::AsRawFd;
+    use crate::os::unix::net::UnixStream;
+    use crate::{io, mem};
+    use sgx_libc::{c_void, socklen_t, ucred, SOL_SOCKET, SO_PEERCRED};
+    use sgx_libc::ocall::getsockopt;
+
+    pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
+        let ucred_size = mem::size_of::<ucred>();
+
+        // Trivial sanity checks.
+        assert!(mem::size_of::<u32>() <= mem::size_of::<usize>());
+        assert!(ucred_size <= u32::MAX as usize);
+
+        let mut ucred_size = ucred_size as socklen_t;
+        let mut ucred: ucred = ucred { pid: 1, uid: 1, gid: 1 };
+
+        unsafe {
+            let ret = getsockopt(
+                socket.as_raw_fd(),
+                SOL_SOCKET,
+                SO_PEERCRED,
+                &mut ucred as *mut ucred as *mut c_void,
+                &mut ucred_size,
+            );
+
+            if ret == 0 && ucred_size as usize == mem::size_of::<ucred>() {
+                Ok(UCred { uid: ucred.uid, gid: ucred.gid, pid: Some(ucred.pid) })
+            } else {
+                Err(io::Error::last_os_error())
+            }
+        }
+    }
+}
diff --git a/sgx_tstd/src/panic.rs b/sgx_tstd/src/panic.rs
index ec6f74b..5455a6f 100644
--- a/sgx_tstd/src/panic.rs
+++ b/sgx_tstd/src/panic.rs
@@ -17,203 +17,58 @@
 
 //! Panic support in the standard library
 
+use crate::any::Any;
 use crate::collections;
 use crate::panicking;
+use crate::sync::{SgxMutex, SgxRwLock};
 use crate::thread::Result;
-use crate::sync::{SgxMutex, SgxReentrantMutex, SgxRwLock};
-use core::any::Any;
-use core::cell::UnsafeCell;
-use core::fmt;
-use core::future::Future;
-use core::ops::{Deref, DerefMut};
-use core::pin::Pin;
-use core::ptr::{NonNull, Unique};
-use core::sync::atomic;
-use core::task::{Context, Poll};
-use alloc_crate::boxed::Box;
-use alloc_crate::rc::Rc;
-use alloc_crate::sync::Arc;
 
-pub use crate::panicking::{set_panic_handler, take_panic_handler};
+#[doc(hidden)]
+#[allow_internal_unstable(libstd_sys_internals, const_format_args)]
+#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
+#[rustc_macro_transparency = "semitransparent"]
+pub macro panic_2015 {
+    () => ({
+        $crate::rt::begin_panic("explicit panic")
+    }),
+    ($msg:expr $(,)?) => ({
+        $crate::rt::begin_panic($msg)
+    }),
+    ($fmt:expr, $($arg:tt)+) => ({
+        $crate::rt::begin_panic_fmt(&$crate::const_format_args!($fmt, $($arg)+))
+    }),
+}
+
+#[doc(hidden)]
+pub use core::panic::panic_2021;
+
+pub use crate::panicking::{set_hook, take_hook};
+
 pub use core::panic::{Location, PanicInfo};
 
-/// A marker trait which represents "panic safe" types in Rust.
-///
-/// This trait is implemented by default for many types and behaves similarly in
-/// terms of inference of implementation to the [`Send`] and [`Sync`] traits. The
-/// purpose of this trait is to encode what types are safe to cross a [`catch_unwind`]
-/// boundary with no fear of unwind safety.
-///
-/// [`Send`]: ../marker/trait.Send.html
-/// [`Sync`]: ../marker/trait.Sync.html
-/// [`catch_unwind`]: ./fn.catch_unwind.html
-///
-/// ## What is unwind safety?
-///
-/// In Rust a function can "return" early if it either panics or calls a
-/// function which transitively panics. This sort of control flow is not always
-/// anticipated, and has the possibility of causing subtle bugs through a
-/// combination of two critical components:
-///
-/// 1. A data structure is in a temporarily invalid state when the thread
-///    panics.
-/// 2. This broken invariant is then later observed.
-///
-/// Typically in Rust, it is difficult to perform step (2) because catching a
-/// panic involves either spawning a thread (which in turns makes it difficult
-/// to later witness broken invariants) or using the `catch_unwind` function in this
-/// module. Additionally, even if an invariant is witnessed, it typically isn't a
-/// problem in Rust because there are no uninitialized values (like in C or C++).
-///
-/// It is possible, however, for **logical** invariants to be broken in Rust,
-/// which can end up causing behavioral bugs. Another key aspect of unwind safety
-/// in Rust is that, in the absence of `unsafe` code, a panic cannot lead to
-/// memory unsafety.
-///
-/// That was a bit of a whirlwind tour of unwind safety, but for more information
-/// about unwind safety and how it applies to Rust, see an [associated RFC][rfc].
-///
-/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
-///
-/// ## What is `UnwindSafe`?
-///
-/// Now that we've got an idea of what unwind safety is in Rust, it's also
-/// important to understand what this trait represents. As mentioned above, one
-/// way to witness broken invariants is through the `catch_unwind` function in this
-/// module as it allows catching a panic and then re-using the environment of
-/// the closure.
-///
-/// Simply put, a type `T` implements `UnwindSafe` if it cannot easily allow
-/// witnessing a broken invariant through the use of `catch_unwind` (catching a
-/// panic). This trait is an auto trait, so it is automatically implemented for
-/// many types, and it is also structurally composed (e.g., a struct is unwind
-/// safe if all of its components are unwind safe).
-///
-/// Note, however, that this is not an unsafe trait, so there is not a succinct
-/// contract that this trait is providing. Instead it is intended as more of a
-/// "speed bump" to alert users of `catch_unwind` that broken invariants may be
-/// witnessed and may need to be accounted for.
-///
-/// ## Who implements `UnwindSafe`?
-///
-/// Types such as `&mut T` and `&RefCell<T>` are examples which are **not**
-/// unwind safe. The general idea is that any mutable state which can be shared
-/// across `catch_unwind` is not unwind safe by default. This is because it is very
-/// easy to witness a broken invariant outside of `catch_unwind` as the data is
-/// simply accessed as usual.
-///
-/// Types like `&Mutex<T>`, however, are unwind safe because they implement
-/// poisoning by default. They still allow witnessing a broken invariant, but
-/// they already provide their own "speed bumps" to do so.
-///
-/// ## When should `UnwindSafe` be used?
-///
-/// It is not intended that most types or functions need to worry about this trait.
-/// It is only used as a bound on the `catch_unwind` function and as mentioned
-/// above, the lack of `unsafe` means it is mostly an advisory. The
-/// [`AssertUnwindSafe`] wrapper struct can be used to force this trait to be
-/// implemented for any closed over variables passed to `catch_unwind`.
-///
-/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
-pub auto trait UnwindSafe {}
+pub use core::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
 
-/// A marker trait representing types where a shared reference is considered
-/// unwind safe.
+/// Panic the current thread with the given message as the panic payload.
 ///
-/// This trait is namely not implemented by [`UnsafeCell`], the root of all
-/// interior mutability.
+/// The message can be of any (`Any + Send`) type, not just strings.
 ///
-/// This is a "helper marker trait" used to provide impl blocks for the
-/// [`UnwindSafe`] trait, for more information see that documentation.
+/// The message is wrapped in a `Box<'static + Any + Send>`, which can be
+/// accessed later using [`PanicInfo::payload`].
 ///
-/// [`UnsafeCell`]: ../cell/struct.UnsafeCell.html
-/// [`UnwindSafe`]: ./trait.UnwindSafe.html
-pub auto trait RefUnwindSafe {}
+/// See the [`panic!`] macro for more information about panicking.
+#[inline]
+#[track_caller]
+pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
+    crate::panicking::begin_panic(msg);
+}
 
-/// A simple wrapper around a type to assert that it is unwind safe.
-///
-/// When using [`catch_unwind`] it may be the case that some of the closed over
-/// variables are not unwind safe. For example if `&mut T` is captured the
-/// compiler will generate a warning indicating that it is not unwind safe. It
-/// may not be the case, however, that this is actually a problem due to the
-/// specific usage of [`catch_unwind`] if unwind safety is specifically taken into
-/// account. This wrapper struct is useful for a quick and lightweight
-/// annotation that a variable is indeed unwind safe.
-///
-/// [`catch_unwind`]: ./fn.catch_unwind.html
-pub struct AssertUnwindSafe<T>(
-    pub T
-);
-
-// Implementations of the `UnwindSafe` trait:
-//
-// * By default everything is unwind safe
-// * pointers T contains mutability of some form are not unwind safe
-// * Unique, an owning pointer, lifts an implementation
-// * Types like Mutex/RwLock which are explicitly poisoned are unwind safe
-// * Our custom AssertUnwindSafe wrapper is indeed unwind safe
-
-impl<T: ?Sized> !UnwindSafe for &mut T {}
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for &T {}
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *const T {}
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *mut T {}
-impl<T: UnwindSafe + ?Sized> UnwindSafe for Unique<T> {}
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for NonNull<T> {}
 impl<T: ?Sized> UnwindSafe for SgxMutex<T> {}
-impl<T> UnwindSafe for SgxReentrantMutex<T> {}
 impl<T: ?Sized> UnwindSafe for SgxRwLock<T> {}
-impl<T> UnwindSafe for AssertUnwindSafe<T> {}
-
-// not covered via the Shared impl above b/c the inner contents use
-// Cell/AtomicUsize, but the usage here is unwind safe so we can lift the
-// impl up one level to Arc/Rc itself
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Rc<T> {}
-impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Arc<T> {}
-
-// Pretty simple implementations for the `RefUnwindSafe` marker trait,
-// basically just saying that `UnsafeCell` is the
-// only thing which doesn't implement it (which then transitively applies to
-// everything else).
-
-impl<T: ?Sized> !RefUnwindSafe for UnsafeCell<T> {}
-impl<T> RefUnwindSafe for AssertUnwindSafe<T> {}
 
 impl<T: ?Sized> RefUnwindSafe for SgxMutex<T> {}
-impl<T> RefUnwindSafe for SgxReentrantMutex<T> {}
 impl<T: ?Sized> RefUnwindSafe for SgxRwLock<T> {}
 
-#[cfg(target_has_atomic_load_store = "ptr")]
-impl RefUnwindSafe for atomic::AtomicIsize {}
-#[cfg(target_has_atomic_load_store = "8")]
-impl RefUnwindSafe for atomic::AtomicI8 {}
-#[cfg(target_has_atomic_load_store = "16")]
-impl RefUnwindSafe for atomic::AtomicI16 {}
-#[cfg(target_has_atomic_load_store = "32")]
-impl RefUnwindSafe for atomic::AtomicI32 {}
-#[cfg(target_has_atomic_load_store = "64")]
-impl RefUnwindSafe for atomic::AtomicI64 {}
-#[cfg(target_has_atomic_load_store = "128")]
-impl RefUnwindSafe for atomic::AtomicI128 {}
-
-#[cfg(target_has_atomic_load_store = "ptr")]
-impl RefUnwindSafe for atomic::AtomicUsize {}
-#[cfg(target_has_atomic_load_store = "8")]
-impl RefUnwindSafe for atomic::AtomicU8 {}
-#[cfg(target_has_atomic_load_store = "16")]
-impl RefUnwindSafe for atomic::AtomicU16 {}
-#[cfg(target_has_atomic_load_store = "32")]
-impl RefUnwindSafe for atomic::AtomicU32 {}
-#[cfg(target_has_atomic_load_store = "64")]
-impl RefUnwindSafe for atomic::AtomicU64 {}
-#[cfg(target_has_atomic_load_store = "128")]
-impl RefUnwindSafe for atomic::AtomicU128 {}
-
-#[cfg(target_has_atomic_load_store = "8")]
-impl RefUnwindSafe for atomic::AtomicBool {}
-
-#[cfg(target_has_atomic_load_store = "ptr")]
-impl<T> RefUnwindSafe for atomic::AtomicPtr<T> {}
-
+// https://github.com/rust-lang/rust/issues/62301
 impl<K, V, S> UnwindSafe for collections::HashMap<K, V, S>
 where
     K: UnwindSafe,
@@ -222,43 +77,6 @@
 {
 }
 
-impl<T> Deref for AssertUnwindSafe<T> {
-    type Target = T;
-
-    fn deref(&self) -> &T {
-        &self.0
-    }
-}
-
-impl<T> DerefMut for AssertUnwindSafe<T> {
-    fn deref_mut(&mut self) -> &mut T {
-        &mut self.0
-    }
-}
-
-impl<R, F: FnOnce() -> R> FnOnce<()> for AssertUnwindSafe<F> {
-    type Output = R;
-
-    extern "rust-call" fn call_once(self, _args: ()) -> R {
-        (self.0)()
-    }
-}
-
-impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_tuple("AssertUnwindSafe").field(&self.0).finish()
-    }
-}
-
-impl<F: Future> Future for AssertUnwindSafe<F> {
-    type Output = F::Output;
-
-    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
-        let pinned_field = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) };
-        F::poll(pinned_field, cx)
-    }
-}
-
 /// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
 ///
 /// This function will return `Ok` with the closure's result if the closure
@@ -275,8 +93,6 @@
 /// can fail on a regular basis. Additionally, this function is not guaranteed
 /// to catch all panics, see the "Notes" section below.
 ///
-/// [`Result`]: ../result/enum.Result.html
-///
 /// The closure provided is required to adhere to the [`UnwindSafe`] trait to ensure
 /// that all captured variables are safe to cross this boundary. The purpose of
 /// this bound is to encode the concept of [exception safety][rfc] in the type
@@ -285,18 +101,33 @@
 /// becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to quickly
 /// assert that the usage here is indeed unwind safe.
 ///
-/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
-/// [`UnwindSafe`]: ./trait.UnwindSafe.html
-///
 /// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
 ///
 /// # Notes
 ///
-/// Note that this function **may not catch all panics** in Rust. A panic in
+/// Note that this function **might not catch all panics** in Rust. A panic in
 /// Rust is not always implemented via unwinding, but can be implemented by
 /// aborting the process as well. This function *only* catches unwinding panics,
 /// not those that abort the process.
 ///
+/// Also note that unwinding into Rust code with a foreign exception (e.g.
+/// an exception thrown from C++ code) is undefined behavior.
+///
+/// # Examples
+///
+/// ```
+/// use std::panic;
+///
+/// let result = panic::catch_unwind(|| {
+///     println!("hello!");
+/// });
+/// assert!(result.is_ok());
+///
+/// let result = panic::catch_unwind(|| {
+///     panic!("oh no!");
+/// });
+/// assert!(result.is_err());
+/// ```
 pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
     unsafe { panicking::r#try(f) }
 }
@@ -306,8 +137,6 @@
 /// This is designed to be used in conjunction with [`catch_unwind`] to, for
 /// example, carry a panic across a layer of C code.
 ///
-/// [`catch_unwind`]: ./fn.catch_unwind.html
-///
 /// # Notes
 ///
 /// Note that panics in Rust are not always implemented via unwinding, but they
@@ -315,6 +144,54 @@
 /// panics are implemented this way then this function will abort the process,
 /// not trigger an unwind.
 ///
+/// # Examples
+///
+/// ```should_panic
+/// use std::panic;
+///
+/// let result = panic::catch_unwind(|| {
+///     panic!("oh no!");
+/// });
+///
+/// if let Err(err) = result {
+///     panic::resume_unwind(err);
+/// }
+/// ```
 pub fn resume_unwind(payload: Box<dyn Any + Send>) -> ! {
     panicking::rust_panic_without_hook(payload)
 }
+
+/// Make all future panics abort directly without running the panic hook or unwinding.
+///
+/// There is no way to undo this; the effect lasts until the process exits or
+/// execs (or the equivalent).
+///
+/// # Use after fork
+///
+/// This function is particularly useful for calling after `libc::fork`.  After `fork`, in a
+/// multithreaded program it is (on many platforms) not safe to call the allocator.  It is also
+/// generally highly undesirable for an unwind to unwind past the `fork`, because that results in
+/// the unwind propagating to code that was only ever expecting to run in the parent.
+///
+/// `panic::always_abort()` helps avoid both of these.  It directly avoids any further unwinding,
+/// and if there is a panic, the abort will occur without allocating provided that the arguments to
+/// panic can be formatted without allocating.
+///
+/// Examples
+///
+/// ```no_run
+/// #![feature(panic_always_abort)]
+/// use std::panic;
+///
+/// panic::always_abort();
+///
+/// let _ = panic::catch_unwind(|| {
+///     panic!("inside the catch");
+/// });
+///
+/// // We will have aborted already, due to the panic.
+/// unreachable!();
+/// ```
+pub fn always_abort() {
+    crate::panicking::panic_count::set_always_abort();
+}
diff --git a/sgx_tstd/src/panicking.rs b/sgx_tstd/src/panicking.rs
index c219162..1582e1b 100644
--- a/sgx_tstd/src/panicking.rs
+++ b/sgx_tstd/src/panicking.rs
@@ -24,20 +24,20 @@
 //! * Executing a panic up to doing the actual implementation
 //! * Shims around "try"
 
-use sgx_trts::trts::rsgx_abort;
-use core::any::Any;
-use core::fmt;
-use core::intrinsics;
-use core::mem::{self, ManuallyDrop};
 use core::panic::{BoxMeUp, Location, PanicInfo};
-use core::ptr;
-use core::sync::atomic::{AtomicPtr, Ordering};
-use alloc_crate::boxed::Box;
-use alloc_crate::string::String;
+
+use crate::any::Any;
+use crate::fmt;
+use crate::intrinsics;
+use crate::mem::{self, ManuallyDrop};
+use crate::ptr;
+use crate::sync::atomic::{AtomicPtr, Ordering};
 use crate::sys::stdio::panic_output;
-use crate::sys_common::{thread_info, util};
+use crate::sys_common::thread_info;
 use crate::thread;
 
+use sgx_trts::trts::rsgx_abort;
+
 // Binary interface to the panic runtime that the standard library depends on.
 //
 // The standard library is tagged with `#![needs_panic_runtime]` (introduced in
@@ -51,12 +51,14 @@
 #[allow(improper_ctypes)]
 extern "C" {
     fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
+}
 
-    /// `payload` is actually a `*mut &mut dyn BoxMeUp` but that would cause FFI warnings.
-    /// It cannot be `Box<dyn BoxMeUp>` because the other end of this call does not depend
-    /// on liballoc, and thus cannot use `Box`.
-    #[unwind(allowed)]
-    fn __rust_start_panic(payload: usize) -> u32;
+#[allow(improper_ctypes)]
+extern "C-unwind" {
+    /// `payload` is passed through another layer of raw pointers as `&mut dyn Trait` is not
+    /// FFI-safe. `BoxMeUp` lazily performs allocation only when needed (this avoids allocations
+    /// when using the "abort" panic runtime).
+    fn __rust_start_panic(payload: *mut &mut dyn BoxMeUp) -> u32;
 }
 
 /// This function is called by the panic runtime if FFI code catches a Rust
@@ -67,6 +69,8 @@
     rtabort!("Rust panics must be rethrown");
 }
 
+/// This function is called by the panic runtime if it catches an exception
+/// object which does not correspond to a Rust panic.
 #[rustc_std_internal_symbol]
 extern "C" fn __rust_foreign_exception() -> ! {
     rtabort!("Rust cannot catch foreign exceptions");
@@ -74,28 +78,68 @@
 
 static PANIC_HANDLER: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
 
-#[cfg(not(feature = "stdio"))]
-#[allow(unused_variables)]
-fn default_panic_handler(info: &PanicInfo<'_>) {
-
+/// Registers a custom panic hook, replacing any that was previously registered.
+///
+/// The panic hook is invoked when a thread panics, but before the panic runtime
+/// is invoked. As such, the hook will run with both the aborting and unwinding
+/// runtimes. The default hook prints a message to standard error and generates
+/// a backtrace if requested, but this behavior can be customized with the
+/// `set_hook` and [`take_hook`] functions.
+///
+/// [`take_hook`]: ./fn.take_hook.html
+///
+/// The hook is provided with a `PanicInfo` struct which contains information
+/// about the origin of the panic, including the payload passed to `panic!` and
+/// the source code location from which the panic originated.
+///
+/// The panic hook is a global resource.
+///
+/// # Panics
+///
+/// Panics if called from a panicking thread.
+///
+pub fn set_hook(hook: fn(&PanicInfo<'_>)) {
+    if thread::panicking() {
+        panic!("cannot modify the panic hook from a panicking thread");
+    }
+    PANIC_HANDLER.store(hook as *mut (), Ordering::SeqCst);
 }
 
+
+/// Unregisters the current panic hook, returning it.
+///
+/// *See also the function [`set_hook`].*
+///
+/// [`set_hook`]: ./fn.set_hook.html
+///
+/// If no custom hook is registered, the default hook will be returned.
+///
+/// # Panics
+///
+/// Panics if called from a panicking thread.
+/// 
+pub fn take_hook() -> fn(&PanicInfo<'_>) {
+    let hook = PANIC_HANDLER.swap(ptr::null_mut(), Ordering::SeqCst);
+    if hook.is_null() { default_hook } else { unsafe { mem::transmute(hook) } }
+}
+
+#[cfg(not(feature = "stdio"))]
+#[allow(unused_variables)]
+fn default_hook(info: &PanicInfo<'_>) {}
+
 #[cfg(feature = "stdio")]
-fn default_panic_handler(info: &PanicInfo<'_>) {
+fn default_hook(info: &PanicInfo<'_>) {
     #[cfg(feature = "backtrace")]
     use crate::sys_common::backtrace::{self, RustBacktrace};
 
-    #[allow(unused_variables)]
-    let panics = update_panic_count(0);
-
+    // If this is a double panic, make sure that we print a backtrace
+    // for this panic. Otherwise only print it if logging is enabled.
     #[cfg(feature = "backtrace")]
-    let backtrace_env = {
+    let backtrace_env = if panic_count::get_count() >= 2 {
         use crate::sys::backtrace::PrintFmt;
-        if panics >= 2 {
-            RustBacktrace::Print(PrintFmt::Full)
-        } else {
-            backtrace::rust_backtrace_env()
-        }
+        RustBacktrace::Print(PrintFmt::Full)
+    } else {
+        backtrace::rust_backtrace_env()
     };
 
     // The current implementation always returns `Some`.
@@ -105,7 +149,7 @@
         Some(s) => *s,
         None => match info.payload().downcast_ref::<String>() {
             Some(s) => &s[..],
-            None => "Box<Any>",
+            None => "Box<dyn Any>",
         },
     };
     let thread = thread_info::current_thread();
@@ -116,7 +160,7 @@
 
         #[cfg(feature = "backtrace")]
         {
-            use core::sync::atomic::AtomicBool;
+            use crate::sync::atomic::AtomicBool;
             static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
 
             match backtrace_env {
@@ -139,35 +183,97 @@
     }
 }
 
-/// Registers a custom panic handler, replacing any that was previously registered.
-pub fn set_panic_handler(handler: fn(&PanicInfo<'_>)) {
-    if thread::panicking() {
-        panic!("cannot modify the panic hook from a panicking thread");
-    }
-    PANIC_HANDLER.store(handler as *mut (), Ordering::SeqCst);
-}
-
-pub fn take_panic_handler() -> fn(&PanicInfo<'_>) {
-    let hook = PANIC_HANDLER.swap(ptr::null_mut(), Ordering::SeqCst);
-    if hook.is_null() { default_panic_handler } else { unsafe { mem::transmute(hook) } }
-}
-
 fn panic_handler(info: &PanicInfo<'_>) {
     let hook = PANIC_HANDLER.load(Ordering::SeqCst);
     let handler: fn(&PanicInfo<'_>) =
-        if hook.is_null() { default_panic_handler } else { unsafe { mem::transmute(hook) } };
+        if hook.is_null() { default_hook } else { unsafe { mem::transmute(hook) } };
     handler(info);
 }
 
-pub fn update_panic_count(amt: isize) -> usize {
-    use core::cell::Cell;
-    thread_local! { static PANIC_COUNT: Cell<usize> = Cell::new(0) }
+#[doc(hidden)]
+pub mod panic_count {
+    use crate::cell::Cell;
+    use crate::sync::atomic::{AtomicUsize, Ordering};
 
-    PANIC_COUNT.with(|c| {
-        let next = (c.get() as isize + amt) as usize;
-        c.set(next);
-        next
-    })
+    pub const ALWAYS_ABORT_FLAG: usize = 1 << (usize::BITS - 1);
+
+    // Panic count for the current thread.
+    thread_local! { static LOCAL_PANIC_COUNT: Cell<usize> = Cell::new(0) }
+
+    // Sum of panic counts from all threads. The purpose of this is to have
+    // a fast path in `is_zero` (which is used by `panicking`). In any particular
+    // thread, if that thread currently views `GLOBAL_PANIC_COUNT` as being zero,
+    // then `LOCAL_PANIC_COUNT` in that thread is zero. This invariant holds before
+    // and after increase and decrease, but not necessarily during their execution.
+    //
+    // Additionally, the top bit of GLOBAL_PANIC_COUNT (GLOBAL_ALWAYS_ABORT_FLAG)
+    // records whether panic::always_abort() has been called.  This can only be
+    // set, never cleared.
+    //
+    // This could be viewed as a struct containing a single bit and an n-1-bit
+    // value, but if we wrote it like that it would be more than a single word,
+    // and even a newtype around usize would be clumsy because we need atomics.
+    // But we use such a tuple for the return type of increase().
+    //
+    // Stealing a bit is fine because it just amounts to assuming that each
+    // panicking thread consumes at least 2 bytes of address space.
+    static GLOBAL_PANIC_COUNT: AtomicUsize = AtomicUsize::new(0);
+
+    pub fn increase() -> (bool, usize) {
+        (
+            GLOBAL_PANIC_COUNT.fetch_add(1, Ordering::Relaxed) & ALWAYS_ABORT_FLAG != 0,
+            LOCAL_PANIC_COUNT.with(|c| {
+                let next = c.get() + 1;
+                c.set(next);
+                next
+            }),
+        )
+    }
+
+    pub fn decrease() {
+        GLOBAL_PANIC_COUNT.fetch_sub(1, Ordering::Relaxed);
+        LOCAL_PANIC_COUNT.with(|c| {
+            let next = c.get() - 1;
+            c.set(next);
+            next
+        });
+    }
+
+    pub fn set_always_abort() {
+        GLOBAL_PANIC_COUNT.fetch_or(ALWAYS_ABORT_FLAG, Ordering::Relaxed);
+    }
+
+    // Disregards ALWAYS_ABORT_FLAG
+    pub fn get_count() -> usize {
+        LOCAL_PANIC_COUNT.with(|c| c.get())
+    }
+
+    // Disregards ALWAYS_ABORT_FLAG
+    #[inline]
+    pub fn count_is_zero() -> bool {
+        if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) & !ALWAYS_ABORT_FLAG == 0 {
+            // Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
+            // (including the current one) will have `LOCAL_PANIC_COUNT`
+            // equal to zero, so TLS access can be avoided.
+            //
+            // In terms of performance, a relaxed atomic load is similar to a normal
+            // aligned memory read (e.g., a mov instruction in x86), but with some
+            // compiler optimization restrictions. On the other hand, a TLS access
+            // might require calling a non-inlinable function (such as `__tls_get_addr`
+            // when using the GD TLS model).
+            true
+        } else {
+            is_zero_slow_path()
+        }
+    }
+
+    // Slow path is in a separate function to reduce the amount of code
+    // inlined from `is_zero`.
+    #[inline(never)]
+    #[cold]
+    fn is_zero_slow_path() -> bool {
+        LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
+    }
 }
 
 /// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
@@ -205,49 +311,46 @@
     let mut data = Data { f: ManuallyDrop::new(f) };
 
     let data_ptr = &mut data as *mut _ as *mut u8;
-    return if do_try(do_call::<F, R>, data_ptr, do_catch::<F, R>) == 0 {
+    // SAFETY:
+    //
+    // Access to the union's fields: this is `std` and we know that the `r#try`
+    // intrinsic fills in the `r` or `p` union field based on its return value.
+    //
+    // The call to `intrinsics::r#try` is made safe by:
+    // - `do_call`, the first argument, can be called with the initial `data_ptr`.
+    // - `do_catch`, the second argument, can be called with the `data_ptr` as well.
+    // See their safety preconditions for more informations
+    return if intrinsics::r#try(do_call::<F, R>, data_ptr, do_catch::<F, R>) == 0 {
         Ok(ManuallyDrop::into_inner(data.r))
     } else {
         Err(ManuallyDrop::into_inner(data.p))
     };
 
-    // Compatibility wrapper around the try intrinsic for bootstrap.
-    //
-    // We also need to mark it #[inline(never)] to work around a bug on MinGW
-    // targets: the unwinding implementation was relying on UB, but this only
-    // becomes a problem in practice if inlining is involved.
-    #[cfg(not(bootstrap))]
-    use intrinsics::r#try as do_try;
-    #[cfg(bootstrap)]
-    #[inline(never)]
-    unsafe fn do_try(try_fn: fn(*mut u8), data: *mut u8, catch_fn: fn(*mut u8, *mut u8)) -> i32 {
-        use core::mem::MaybeUninit;
-        type TryPayload = *mut u8;
-
-        let mut payload: MaybeUninit<TryPayload> = MaybeUninit::uninit();
-        let payload_ptr = payload.as_mut_ptr() as *mut u8;
-        let r = intrinsics::r#try(try_fn, data, payload_ptr);
-        if r != 0 {
-            catch_fn(data, payload.assume_init())
-        }
-        r
-    }
-
     // We consider unwinding to be rare, so mark this function as cold. However,
     // do not mark it no-inline -- that decision is best to leave to the
     // optimizer (in most cases this function is not inlined even as a normal,
     // non-cold function, though, as of the writing of this comment).
     #[cold]
     unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send + 'static> {
+        // SAFETY: The whole unsafe block hinges on a correct implementation of
+        // the panic handler `__rust_panic_cleanup`. As such we can only
+        // assume it returns the correct thing for `Box::from_raw` to work
+        // without undefined behavior.
         let obj = Box::from_raw(__rust_panic_cleanup(payload));
-        update_panic_count(-1);
+        panic_count::decrease();
         obj
     }
 
-    // See comment on do_try above for why #[inline(never)] is needed on bootstrap.
-    #[cfg_attr(bootstrap, inline(never))]
-    #[cfg_attr(not(bootstrap), inline)]
+    // SAFETY:
+    // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
+    // Its must contains a valid `f` (type: F) value that can be use to fill
+    // `data.r`.
+    //
+    // This function cannot be marked as `unsafe` because `intrinsics::r#try`
+    // expects normal function pointers.
+    #[inline]
     fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
+        // SAFETY: this is the responsibilty of the caller, see above.
         unsafe {
             let data = data as *mut Data<F, R>;
             let data = &mut (*data);
@@ -259,8 +362,21 @@
     // We *do* want this part of the catch to be inlined: this allows the
     // compiler to properly track accesses to the Data union and optimize it
     // away most of the time.
+    //
+    // SAFETY:
+    // data must be non-NUL, correctly aligned, and a pointer to a `Data<F, R>`
+    // Since this uses `cleanup` it also hinges on a correct implementation of
+    // `__rustc_panic_cleanup`.
+    //
+    // This function cannot be marked as `unsafe` because `intrinsics::r#try`
+    // expects normal function pointers.
     #[inline]
     fn do_catch<F: FnOnce() -> R, R>(data: *mut u8, payload: *mut u8) {
+        // SAFETY: this is the responsibilty of the caller, see above.
+        //
+        // When `__rustc_panic_cleaner` is correctly implemented we can rely
+        // on `obj` being the correct thing to pass to `data.p` (after wrapping
+        // in `ManuallyDrop`).
         unsafe {
             let data = data as *mut Data<F, R>;
             let data = &mut (*data);
@@ -271,8 +387,9 @@
 }
 
 /// Determines whether the current thread is unwinding because of panic.
+#[inline]
 pub fn panicking() -> bool {
-    update_panic_count(0) != 0
+    !panic_count::count_is_zero()
 }
 
 /// The entry point for panicking with a formatted message.
@@ -286,6 +403,7 @@
 // otherwise avoid inlining because of it is cold path.
 #[track_caller]
 #[inline(never)]
+#[lang = "begin_panic_fmt"]
 pub fn begin_panic_fmt(msg: &fmt::Arguments<'_>) -> ! {
     let info = PanicInfo::internal_constructor(Some(msg), Location::caller());
     begin_panic_handler(&info)
@@ -293,7 +411,6 @@
 
 /// Entry point of panics from the libcore crate (`panic_impl` lang item).
 #[panic_handler]
-#[unwind(allowed)]
 pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
     struct PanicPayload<'a> {
         inner: &'a fmt::Arguments<'a>,
@@ -305,8 +422,9 @@
             PanicPayload { inner, string: None }
         }
 
+        #[allow(clippy::drop_copy)]
         fn fill(&mut self) -> &mut String {
-            use core::fmt::Write;
+            use crate::fmt::Write;
 
             let inner = self.inner;
             // Lazily, the first time this gets called, run the actual string formatting.
@@ -332,9 +450,38 @@
         }
     }
 
+    struct StrPanicPayload(&'static str);
+
+    unsafe impl BoxMeUp for StrPanicPayload {
+        fn take_box(&mut self) -> *mut (dyn Any + Send) {
+            Box::into_raw(Box::new(self.0))
+        }
+
+        fn get(&mut self) -> &(dyn Any + Send) {
+            &self.0
+        }
+    }
+
     let loc = info.location().unwrap(); // The current implementation always returns Some
     let msg = info.message().unwrap(); // The current implementation always returns Some
-    rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
+    #[cfg(feature = "backtrace")]
+    {
+        crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
+            if let Some(msg) = msg.as_str() {
+                rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc);
+            } else {
+                rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
+            }
+        })
+    }
+    #[cfg(not(feature = "backtrace"))]
+    {
+        if let Some(msg) = msg.as_str() {
+            rust_panic_with_hook(&mut StrPanicPayload(msg), info.message(), loc);
+        } else {
+            rust_panic_with_hook(&mut PanicPayload::new(msg), info.message(), loc);
+        }
+    }
 }
 
 /// This is the entry point of panicking for the non-format-string variants of
@@ -344,11 +491,10 @@
 // lang item for CTFE panic support
 // never inline unless panic_immediate_abort to avoid code
 // bloat at the call sites as much as possible
+#[inline(never)]
 #[cold]
 #[track_caller]
 pub fn begin_panic<M: Any + Send>(msg: M) -> ! {
-    rust_panic_with_hook(&mut PanicPayload::new(msg), None, Location::caller());
-
     struct PanicPayload<A> {
         inner: Option<A>,
     }
@@ -380,6 +526,18 @@
             }
         }
     }
+
+    let loc = Location::caller();
+    #[cfg(feature = "backtrace")]
+    {
+        crate::sys_common::backtrace::__rust_end_short_backtrace(move || {
+            rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc)
+        })
+    }
+    #[cfg(not(feature = "backtrace"))]
+    {
+        rust_panic_with_hook(&mut PanicPayload::new(msg), None, loc)
+    }
 }
 
 /// Central point for dispatching panics.
@@ -392,18 +550,24 @@
     message: Option<&fmt::Arguments<'_>>,
     location: &Location<'_>,
 ) -> ! {
-    let panics = update_panic_count(1);
+    let (must_abort, panics) = panic_count::increase();
 
     // If this is the third nested call (e.g., panics == 2, this is 0-indexed),
     // the panic hook probably triggered the last panic, otherwise the
     // double-panic check would have aborted the process. In this case abort the
     // process real quickly as we don't want to try calling it again as it'll
     // probably just panic again.
-    if panics > 2 {
-        util::dumb_print(format_args!(
-            "thread panicked while processing \
-                                       panic. aborting.\n"
-        ));
+    if must_abort || panics > 2 {
+        if panics > 2 {
+            // Don't try to print the message in this case
+            // - perhaps that is causing the recursive panics.
+            rtprintpanic!("thread panicked while processing panic. aborting.\n");
+        } else {
+            // Unfortunately, this does not print a backtrace, because creating
+            // a `Backtrace` will allocate, which we must to avoid here.
+            let panicinfo = PanicInfo::internal_constructor(message, location);
+            rtprintpanic!("{}\npanicked after panic::always_abort(), aborting.\n", panicinfo);
+        }
         rsgx_abort()
     }
 
@@ -416,10 +580,7 @@
         // have limited options. Currently our preference is to
         // just abort. In the future we may consider resuming
         // unwinding or otherwise exiting the thread cleanly.
-        util::dumb_print(format_args!(
-            "thread panicked while panicking. \
-                                       aborting.\n"
-        ));
+        rtprintpanic!("thread panicked while panicking. aborting.\n");
         rsgx_abort()
     }
 
@@ -429,7 +590,7 @@
 /// This is the entry point for `resume_unwind`.
 /// It just forwards the payload to the panic runtime.
 pub fn rust_panic_without_hook(payload: Box<dyn Any + Send>) -> ! {
-    update_panic_count(1);
+    panic_count::increase();
 
     struct RewrapBox(Box<dyn Any + Send>);
 
@@ -449,11 +610,11 @@
 /// An unmangled function (through `rustc_std_internal_symbol`) on which to slap
 /// yer breakpoints.
 #[inline(never)]
-#[rustc_std_internal_symbol]
+#[cfg_attr(not(test), rustc_std_internal_symbol)]
 fn rust_panic(mut msg: &mut dyn BoxMeUp) -> ! {
     let code = unsafe {
         let obj = &mut msg as *mut &mut dyn BoxMeUp;
-        __rust_start_panic(obj as usize)
+        __rust_start_panic(obj)
     };
     rtabort!("failed to initiate panic, error {}", code)
 }
diff --git a/sgx_tstd/src/path.rs b/sgx_tstd/src/path.rs
index fcdedbf..3303a50 100644
--- a/sgx_tstd/src/path.rs
+++ b/sgx_tstd/src/path.rs
@@ -29,24 +29,71 @@
 //! [`PathBuf`]; note that the paths may differ syntactically by the
 //! normalization described in the documentation for the [`components`] method.
 //!
+//! ## Simple usage
+//!
+//! Path manipulation includes both parsing components from slices and building
+//! new owned paths.
+//!
+//! To parse a path, you can create a [`Path`] slice from a [`str`]
+//! slice and start asking questions:
+//!
+//! ```
+//! use std::path::Path;
+//! use std::ffi::OsStr;
+//!
+//! let path = Path::new("/tmp/foo/bar.txt");
+//!
+//! let parent = path.parent();
+//! assert_eq!(parent, Some(Path::new("/tmp/foo")));
+//!
+//! let file_stem = path.file_stem();
+//! assert_eq!(file_stem, Some(OsStr::new("bar")));
+//!
+//! let extension = path.extension();
+//! assert_eq!(extension, Some(OsStr::new("txt")));
+//! ```
+//!
+//! To build or modify paths, use [`PathBuf`]:
+//!
+//! ```
+//! use std::path::PathBuf;
+//!
+//! // This way works...
+//! let mut path = PathBuf::from("c:\\");
+//!
+//! path.push("windows");
+//! path.push("system32");
+//!
+//! path.set_extension("dll");
+//!
+//! // ... but push is best used if you don't know everything up
+//! // front. If you do, this way is better:
+//! let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
+//! ```
+//!
+//! [`components`]: Path::components
+//! [`push`]: PathBuf::push
 
+#![allow(clippy::upper_case_acronyms)]
+
+use crate::borrow::{Borrow, Cow};
+use crate::cmp;
 use crate::error::Error;
+use crate::fmt;
 #[cfg(feature = "untrusted_fs")]
 use crate::fs;
+use crate::hash::{Hash, Hasher};
 #[cfg(feature = "untrusted_fs")]
 use crate::io;
-use crate::ffi::{OsStr, OsString};
-use crate::sys::path::{is_sep_byte, is_verbatim_sep, parse_prefix, MAIN_SEP_STR};
-use alloc_crate::borrow::{Borrow, Cow};
-use alloc_crate::rc::Rc;
-use alloc_crate::sync::Arc;
-use alloc_crate::str::FromStr;
-use core::cmp;
-use core::fmt;
-use core::hash::{Hash, Hasher};
-use core::iter::{self, FusedIterator};
-use core::ops::{self, Deref};
+use crate::iter::{self, FusedIterator};
+use crate::ops::{self, Deref};
+use crate::rc::Rc;
+use crate::str::FromStr;
+use crate::sync::Arc;
 
+use crate::ffi::{OsStr, OsString};
+
+use crate::sys::path::{is_sep_byte, is_verbatim_sep, parse_prefix, MAIN_SEP_STR};
 
 ////////////////////////////////////////////////////////////////////////////////
 // GENERAL NOTES
@@ -70,28 +117,44 @@
 /// `\\?\`), in which case `/` is *not* treated as a separator and essentially
 /// no normalization is performed.
 ///
+/// # Examples
+///
+/// ```
+/// use std::path::{Component, Path, Prefix};
+/// use std::path::Prefix::*;
+/// use std::ffi::OsStr;
+///
+/// fn get_path_prefix(s: &str) -> Prefix {
+///     let path = Path::new(s);
+///     match path.components().next().unwrap() {
+///         Component::Prefix(prefix_component) => prefix_component.kind(),
+///         _ => panic!(),
+///     }
+/// }
+///
+/// ```
 #[derive(Copy, Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
 pub enum Prefix<'a> {
-    /// Verbatim prefix, e.g. `\\?\cat_pics`.
+    /// Verbatim prefix, e.g., `\\?\cat_pics`.
     ///
     /// Verbatim prefixes consist of `\\?\` immediately followed by the given
     /// component.
     Verbatim(&'a OsStr),
 
     /// Verbatim prefix using Windows' _**U**niform **N**aming **C**onvention_,
-    /// e.g. `\\?\UNC\server\share`.
+    /// e.g., `\\?\UNC\server\share`.
     ///
     /// Verbatim UNC prefixes consist of `\\?\UNC\` immediately followed by the
     /// server's hostname and a share name.
     VerbatimUNC(&'a OsStr, &'a OsStr),
 
-    /// Verbatim disk prefix, e.g. `\\?\C:\`.
+    /// Verbatim disk prefix, e.g., `\\?\C:`.
     ///
     /// Verbatim disk prefixes consist of `\\?\` immediately followed by the
-    /// drive letter and `:\`.
+    /// drive letter and `:`.
     VerbatimDisk(u8),
 
-    /// Device namespace prefix, e.g. `\\.\COM42`.
+    /// Device namespace prefix, e.g., `\\.\COM42`.
     ///
     /// Device namespace prefixes consist of `\\.\` immediately followed by the
     /// device name.
@@ -128,6 +191,19 @@
 
     /// Determines if the prefix is verbatim, i.e., begins with `\\?\`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Prefix::*;
+    /// use std::ffi::OsStr;
+    ///
+    /// assert!(Verbatim(OsStr::new("pictures")).is_verbatim());
+    /// assert!(VerbatimUNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
+    /// assert!(VerbatimDisk(b'C').is_verbatim());
+    /// assert!(!DeviceNS(OsStr::new("BrainInterface")).is_verbatim());
+    /// assert!(!UNC(OsStr::new("server"), OsStr::new("share")).is_verbatim());
+    /// assert!(!Disk(b'C').is_verbatim());
+    /// ```
     #[inline]
     pub fn is_verbatim(&self) -> bool {
         use self::Prefix::*;
@@ -152,6 +228,14 @@
 /// Determines whether the character is one of the permitted path
 /// separators for the current platform.
 ///
+/// # Examples
+///
+/// ```
+/// use std::path;
+///
+/// assert!(path::is_separator('/')); // '/' works for both Unix and Windows
+/// assert!(!path::is_separator('❤'));
+/// ```
 pub fn is_separator(c: char) -> bool {
     c.is_ascii() && is_sep_byte(c as u8)
 }
@@ -198,6 +282,7 @@
     unsafe { &*(s as *const OsStr as *const [u8]) }
 }
 unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
+    // SAFETY: see the comment of `os_str_as_u8_slice`
     &*(s as *const [u8] as *const OsStr)
 }
 
@@ -212,26 +297,42 @@
 }
 
 // basic workhorse for splitting stem and extension
-fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
-    unsafe {
-        if os_str_as_u8_slice(file) == b".." {
-            return (Some(file), None);
-        }
-
-        // The unsafety here stems from converting between &OsStr and &[u8]
-        // and back. This is safe to do because (1) we only look at ASCII
-        // contents of the encoding and (2) new &OsStr values are produced
-        // only from ASCII-bounded slices of existing &OsStr values.
-
-        let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
-        let after = iter.next();
-        let before = iter.next();
-        if before == Some(b"") {
-            (Some(file), None)
-        } else {
-            (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s)))
-        }
+fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
+    if os_str_as_u8_slice(file) == b".." {
+        return (Some(file), None);
     }
+
+    // The unsafety here stems from converting between &OsStr and &[u8]
+    // and back. This is safe to do because (1) we only look at ASCII
+    // contents of the encoding and (2) new &OsStr values are produced
+    // only from ASCII-bounded slices of existing &OsStr values.
+    let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
+    let after = iter.next();
+    let before = iter.next();
+    if before == Some(b"") {
+        (Some(file), None)
+    } else {
+        unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
+    }
+}
+
+fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
+    let slice = os_str_as_u8_slice(file);
+    if slice == b".." {
+        return (file, None);
+    }
+
+    // The unsafety here stems from converting between &OsStr and &[u8]
+    // and back. This is safe to do because (1) we only look at ASCII
+    // contents of the encoding and (2) new &OsStr values are produced
+    // only from ASCII-bounded slices of existing &OsStr values.
+    let i = match slice[1..].iter().position(|b| *b == b'.') {
+        Some(i) => i + 1,
+        None => return (file, None),
+    };
+    let before = &slice[..i];
+    let after = &slice[i + 1..];
+    unsafe { (u8_slice_as_os_str(before), Some(u8_slice_as_os_str(after))) }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -264,6 +365,27 @@
 ///
 /// Does not occur on Unix.
 ///
+/// # Examples
+///
+/// ```
+/// # if cfg!(windows) {
+/// use std::path::{Component, Path, Prefix};
+/// use std::ffi::OsStr;
+///
+/// let path = Path::new(r"c:\you\later\");
+/// match path.components().next().unwrap() {
+///     Component::Prefix(prefix_component) => {
+///         assert_eq!(Prefix::Disk(b'C'), prefix_component.kind());
+///         assert_eq!(OsStr::new("c:"), prefix_component.as_os_str());
+///     }
+///     _ => unreachable!(),
+/// }
+/// # }
+/// ```
+///
+/// [`as_os_str`]: PrefixComponent::as_os_str
+/// [`kind`]: PrefixComponent::kind
+/// [`Prefix` variant]: Component::Prefix
 #[derive(Copy, Clone, Eq, Debug)]
 pub struct PrefixComponent<'a> {
     /// The prefix as an unparsed `OsStr` slice.
@@ -278,33 +400,34 @@
     ///
     /// See [`Prefix`]'s documentation for more information on the different
     /// kinds of prefixes.
-    ///
-    /// [`Prefix`]: enum.Prefix.html
+    #[inline]
     pub fn kind(&self) -> Prefix<'a> {
         self.parsed
     }
 
     /// Returns the raw [`OsStr`] slice for this prefix.
-    ///
-    /// [`OsStr`]: ../../std/ffi/struct.OsStr.html
+    #[inline]
     pub fn as_os_str(&self) -> &'a OsStr {
         self.raw
     }
 }
 
 impl<'a> cmp::PartialEq for PrefixComponent<'a> {
+    #[inline]
     fn eq(&self, other: &PrefixComponent<'a>) -> bool {
         cmp::PartialEq::eq(&self.parsed, &other.parsed)
     }
 }
 
 impl<'a> cmp::PartialOrd for PrefixComponent<'a> {
+    #[inline]
     fn partial_cmp(&self, other: &PrefixComponent<'a>) -> Option<cmp::Ordering> {
         cmp::PartialOrd::partial_cmp(&self.parsed, &other.parsed)
     }
 }
 
 impl cmp::Ord for PrefixComponent<'_> {
+    #[inline]
     fn cmp(&self, other: &Self) -> cmp::Ordering {
         cmp::Ord::cmp(&self.parsed, &other.parsed)
     }
@@ -322,8 +445,22 @@
 /// (`/` or `\`).
 ///
 /// This `enum` is created by iterating over [`Components`], which in turn is
-/// created by the [`components`][`Path::components`] method on [`Path`].
+/// created by the [`components`](Path::components) method on [`Path`].
 ///
+/// # Examples
+///
+/// ```rust
+/// use std::path::{Component, Path};
+///
+/// let path = Path::new("/tmp/foo/bar.txt");
+/// let components = path.components().collect::<Vec<_>>();
+/// assert_eq!(&components, &[
+///     Component::RootDir,
+///     Component::Normal("tmp".as_ref()),
+///     Component::Normal("foo".as_ref()),
+///     Component::Normal("bar.txt".as_ref()),
+/// ]);
+/// ```
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
 
 pub enum Component<'a> {
@@ -333,8 +470,6 @@
     /// for more.
     ///
     /// Does not occur on Unix.
-    ///
-    /// [`Prefix`]: enum.Prefix.html
     Prefix(PrefixComponent<'a>),
 
     /// The root directory component, appears after any prefix and before anything else.
@@ -342,13 +477,13 @@
     /// It represents a separator that designates that a path starts from root.
     RootDir,
 
-    /// A reference to the current directory, i.e. `.`.
+    /// A reference to the current directory, i.e., `.`.
     CurDir,
 
-    /// A reference to the parent directory, i.e. `..`.
+    /// A reference to the parent directory, i.e., `..`.
     ParentDir,
 
-    /// A normal component, e.g. `a` and `b` in `a/b`.
+    /// A normal component, e.g., `a` and `b` in `a/b`.
     ///
     /// This variant is the most common one, it represents references to files
     /// or directories.
@@ -358,6 +493,15 @@
 impl<'a> Component<'a> {
     /// Extracts the underlying [`OsStr`] slice.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("./tmp/foo/bar.txt");
+    /// let components: Vec<_> = path.components().map(|comp| comp.as_os_str()).collect();
+    /// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
+    /// ```
     pub fn as_os_str(self) -> &'a OsStr {
         match self {
             Component::Prefix(p) => p.as_os_str(),
@@ -370,12 +514,14 @@
 }
 
 impl AsRef<OsStr> for Component<'_> {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         self.as_os_str()
     }
 }
 
 impl AsRef<Path> for Component<'_> {
+    #[inline]
     fn as_ref(&self) -> &Path {
         self.as_os_str().as_ref()
     }
@@ -386,6 +532,19 @@
 /// This `struct` is created by the [`components`] method on [`Path`].
 /// See its documentation for more.
 ///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+///
+/// let path = Path::new("/tmp/foo/bar.txt");
+///
+/// for component in path.components() {
+///     println!("{:?}", component);
+/// }
+/// ```
+///
+/// [`components`]: Path::components
 #[derive(Clone)]
 pub struct Components<'a> {
     // The path left to parse components from
@@ -395,7 +554,7 @@
     prefix: Option<Prefix<'a>>,
 
     // true if path *physically* has a root separator; for most Windows
-    // prefixes, it may have a "logical" rootseparator for the purposes of
+    // prefixes, it may have a "logical" root separator for the purposes of
     // normalization, e.g.,  \\server\share == \\server\share\.
     has_physical_root: bool,
 
@@ -410,10 +569,7 @@
 /// This `struct` is created by the [`iter`] method on [`Path`].
 /// See its documentation for more.
 ///
-/// [`Component`]: enum.Component.html
-/// [`iter`]: struct.Path.html#method.iter
-/// [`OsStr`]: ../../std/ffi/struct.OsStr.html
-/// [`Path`]: struct.Path.html
+/// [`iter`]: Path::iter
 #[derive(Clone)]
 pub struct Iter<'a> {
     inner: Components<'a>,
@@ -472,6 +628,17 @@
 
     /// Extracts a slice corresponding to the portion of the path remaining for iteration.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let mut components = Path::new("/tmp/foo/bar.txt").components();
+    /// components.next();
+    /// components.next();
+    ///
+    /// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
+    /// ```
     pub fn as_path(&self) -> &'a Path {
         let mut comps = self.clone();
         if comps.front == State::Body {
@@ -571,12 +738,14 @@
 }
 
 impl AsRef<Path> for Components<'_> {
+    #[inline]
     fn as_ref(&self) -> &Path {
         self.as_path()
     }
 }
 
 impl AsRef<OsStr> for Components<'_> {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         self.as_path().as_os_str()
     }
@@ -599,18 +768,32 @@
 impl<'a> Iter<'a> {
     /// Extracts a slice corresponding to the portion of the path remaining for iteration.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let mut iter = Path::new("/tmp/foo/bar.txt").iter();
+    /// iter.next();
+    /// iter.next();
+    ///
+    /// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
+    /// ```
+    #[inline]
     pub fn as_path(&self) -> &'a Path {
         self.inner.as_path()
     }
 }
 
 impl AsRef<Path> for Iter<'_> {
+    #[inline]
     fn as_ref(&self) -> &Path {
         self.as_path()
     }
 }
 
 impl AsRef<OsStr> for Iter<'_> {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         self.as_path().as_os_str()
     }
@@ -619,12 +802,14 @@
 impl<'a> Iterator for Iter<'a> {
     type Item = &'a OsStr;
 
+    #[inline]
     fn next(&mut self) -> Option<&'a OsStr> {
         self.inner.next().map(Component::as_os_str)
     }
 }
 
 impl<'a> DoubleEndedIterator for Iter<'a> {
+    #[inline]
     fn next_back(&mut self) -> Option<&'a OsStr> {
         self.inner.next_back().map(Component::as_os_str)
     }
@@ -733,30 +918,79 @@
 impl FusedIterator for Components<'_> {}
 
 impl<'a> cmp::PartialEq for Components<'a> {
+    #[inline]
     fn eq(&self, other: &Components<'a>) -> bool {
-        Iterator::eq(self.clone(), other.clone())
+        Iterator::eq(self.clone().rev(), other.clone().rev())
     }
 }
 
 impl cmp::Eq for Components<'_> {}
 
 impl<'a> cmp::PartialOrd for Components<'a> {
+    #[inline]
     fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
-        Iterator::partial_cmp(self.clone(), other.clone())
+        Some(compare_components(self.clone(), other.clone()))
     }
 }
 
 impl cmp::Ord for Components<'_> {
+    #[inline]
     fn cmp(&self, other: &Self) -> cmp::Ordering {
-        Iterator::cmp(self.clone(), other.clone())
+        compare_components(self.clone(), other.clone())
     }
 }
 
+fn compare_components(mut left: Components<'_>, mut right: Components<'_>) -> cmp::Ordering {
+    // Fast path for long shared prefixes
+    //
+    // - compare raw bytes to find first mismatch
+    // - backtrack to find separator before mismatch to avoid ambiguous parsings of '.' or '..' characters
+    // - if found update state to only do a component-wise comparison on the remainder,
+    //   otherwise do it on the full path
+    //
+    // The fast path isn't taken for paths with a PrefixComponent to avoid backtracking into
+    // the middle of one
+    if left.prefix.is_none() && right.prefix.is_none() && left.front == right.front {
+        // this might benefit from a [u8]::first_mismatch simd implementation, if it existed
+        let first_difference =
+            match left.path.iter().zip(right.path.iter()).position(|(&a, &b)| a != b) {
+                None if left.path.len() == right.path.len() => return cmp::Ordering::Equal,
+                None => left.path.len().min(right.path.len()),
+                Some(diff) => diff,
+            };
+
+        if let Some(previous_sep) =
+            left.path[..first_difference].iter().rposition(|&b| left.is_sep_byte(b))
+        {
+            let mismatched_component_start = previous_sep + 1;
+            left.path = &left.path[mismatched_component_start..];
+            left.front = State::Body;
+            right.path = &right.path[mismatched_component_start..];
+            right.front = State::Body;
+        }
+    }
+
+    Iterator::cmp(left, right)
+}
+
 /// An iterator over [`Path`] and its ancestors.
 ///
 /// This `struct` is created by the [`ancestors`] method on [`Path`].
 /// See its documentation for more.
 ///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+///
+/// let path = Path::new("/foo/bar");
+///
+/// for ancestor in path.ancestors() {
+///     println!("{}", ancestor.display());
+/// }
+/// ```
+///
+/// [`ancestors`]: Path::ancestors
 #[derive(Copy, Clone, Debug)]
 pub struct Ancestors<'a> {
     next: Option<&'a Path>,
@@ -765,6 +999,7 @@
 impl<'a> Iterator for Ancestors<'a> {
     type Item = &'a Path;
 
+    #[inline]
     fn next(&mut self) -> Option<Self::Item> {
         let next = self.next;
         self.next = next.and_then(Path::parent);
@@ -784,27 +1019,75 @@
 /// the path in place. It also implements [`Deref`] to [`Path`], meaning that
 /// all methods on [`Path`] slices are available on `PathBuf` values as well.
 ///
-/// [`String`]: ../string/struct.String.html
-/// [`Path`]: struct.Path.html
-/// [`push`]: struct.PathBuf.html#method.push
-/// [`set_extension`]: struct.PathBuf.html#method.set_extension
-/// [`Deref`]: ../ops/trait.Deref.html
+/// [`push`]: PathBuf::push
+/// [`set_extension`]: PathBuf::set_extension
 ///
 /// More details about the overall approach can be found in
-/// the [module documentation](index.html).
+/// the [module documentation](self).
 ///
-#[derive(Clone)]
+/// # Examples
+///
+/// You can use [`push`] to build up a `PathBuf` from
+/// components:
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let mut path = PathBuf::new();
+///
+/// path.push(r"C:\");
+/// path.push("windows");
+/// path.push("system32");
+///
+/// path.set_extension("dll");
+/// ```
+///
+/// However, [`push`] is best used for dynamic situations. This is a better way
+/// to do this when you know all of the components ahead of time:
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();
+/// ```
+///
+/// We can still do better than this! Since these are all strings, we can use
+/// `From::from`:
+///
+/// ```
+/// use std::path::PathBuf;
+///
+/// let path = PathBuf::from(r"C:\windows\system32.dll");
+/// ```
+///
+/// Which method works best depends on what kind of situation you're in.
+#[cfg_attr(not(test), rustc_diagnostic_item = "PathBuf")]
+// FIXME:
+// `PathBuf::as_mut_vec` current implementation relies
+// on `PathBuf` being layout-compatible with `Vec<u8>`.
+// When attribute privacy is implemented, `PathBuf` should be annotated as `#[repr(transparent)]`.
+// Anyway, `PathBuf` representation and layout are considered implementation detail, are
+// not documented and must not be relied upon.
 pub struct PathBuf {
     inner: OsString,
 }
 
 impl PathBuf {
+    #[inline]
     fn as_mut_vec(&mut self) -> &mut Vec<u8> {
         unsafe { &mut *(self as *mut PathBuf as *mut Vec<u8>) }
     }
 
     /// Allocates an empty `PathBuf`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let path = PathBuf::new();
+    /// ```
+    #[inline]
     pub fn new() -> PathBuf {
         PathBuf { inner: OsString::new() }
     }
@@ -812,14 +1095,38 @@
     /// Creates a new `PathBuf` with a given capacity used to create the
     /// internal [`OsString`]. See [`with_capacity`] defined on [`OsString`].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let mut path = PathBuf::with_capacity(10);
+    /// let capacity = path.capacity();
+    ///
+    /// // This push is done without reallocating
+    /// path.push(r"C:\");
+    ///
+    /// assert_eq!(capacity, path.capacity());
+    /// ```
+    ///
+    /// [`with_capacity`]: OsString::with_capacity
+    #[inline]
     pub fn with_capacity(capacity: usize) -> PathBuf {
         PathBuf { inner: OsString::with_capacity(capacity) }
     }
 
     /// Coerces to a [`Path`] slice.
     ///
-    /// [`Path`]: struct.Path.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let p = PathBuf::from("/test");
+    /// assert_eq!(Path::new("/test"), p.as_path());
+    /// ```
+    #[inline]
+    #[allow(clippy::ptr_arg)]
     pub fn as_path(&self) -> &Path {
         self
     }
@@ -834,6 +1141,27 @@
     ///   replaces everything except for the prefix (if any) of `self`.
     /// * if `path` has a prefix but no root, it replaces `self`.
     ///
+    /// # Examples
+    ///
+    /// Pushing a relative path extends the existing path:
+    ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let mut path = PathBuf::from("/tmp");
+    /// path.push("file.bk");
+    /// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
+    /// ```
+    ///
+    /// Pushing an absolute path replaces the existing path:
+    ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let mut path = PathBuf::from("/tmp");
+    /// path.push("/etc");
+    /// assert_eq!(path, PathBuf::from("/etc"));
+    /// ```
     pub fn push<P: AsRef<Path>>(&mut self, path: P) {
         self._push(path.as_ref())
     }
@@ -875,10 +1203,20 @@
     /// Returns `false` and does nothing if [`self.parent`] is [`None`].
     /// Otherwise, returns `true`.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`self.parent`]: struct.PathBuf.html#method.parent
+    /// [`self.parent`]: Path::parent
     ///
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let mut p = PathBuf::from("/spirited/away.rs");
+    ///
+    /// p.pop();
+    /// assert_eq!(Path::new("/spirited"), p);
+    /// p.pop();
+    /// assert_eq!(Path::new("/"), p);
+    /// ```
     pub fn pop(&mut self) -> bool {
         match self.parent().map(|p| p.as_u8_slice().len()) {
             Some(len) => {
@@ -898,10 +1236,22 @@
     /// `file_name`. The new path will be a sibling of the original path.
     /// (That is, it will have the same parent.)
     ///
-    /// [`self.file_name`]: struct.PathBuf.html#method.file_name
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
-    /// [`pop`]: struct.PathBuf.html#method.pop
+    /// [`self.file_name`]: Path::file_name
+    /// [`pop`]: PathBuf::pop
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let mut buf = PathBuf::from("/");
+    /// assert!(buf.file_name() == None);
+    /// buf.set_file_name("bar");
+    /// assert!(buf == PathBuf::from("/bar"));
+    /// assert!(buf.file_name().is_some());
+    /// buf.set_file_name("baz.txt");
+    /// assert!(buf == PathBuf::from("/baz.txt"));
+    /// ```
     pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
         self._set_file_name(file_name.as_ref())
     }
@@ -922,10 +1272,22 @@
     /// If [`self.extension`] is [`None`], the extension is added; otherwise
     /// it is replaced.
     ///
-    /// [`self.file_name`]: struct.PathBuf.html#method.file_name
-    /// [`self.extension`]: struct.PathBuf.html#method.extension
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// [`self.file_name`]: Path::file_name
+    /// [`self.extension`]: Path::extension
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let mut p = PathBuf::from("/feel/the");
+    ///
+    /// p.set_extension("force");
+    /// assert_eq!(Path::new("/feel/the.force"), p.as_path());
+    ///
+    /// p.set_extension("dark_side");
+    /// assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());
+    /// ```
     pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
         self._set_extension(extension.as_ref())
     }
@@ -955,16 +1317,21 @@
 
     /// Consumes the `PathBuf`, yielding its internal [`OsString`] storage.
     ///
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::PathBuf;
+    ///
+    /// let p = PathBuf::from("/the/head");
+    /// let os_str = p.into_os_string();
+    /// ```
+    #[inline]
     pub fn into_os_string(self) -> OsString {
         self.inner
     }
 
-    /// Converts this `PathBuf` into a [boxed][`Box`] [`Path`].
-    ///
-    /// [`Box`]: ../../std/boxed/struct.Box.html
-    /// [`Path`]: struct.Path.html
+    /// Converts this `PathBuf` into a [boxed](Box) [`Path`].
+    #[inline]
     pub fn into_boxed_path(self) -> Box<Path> {
         let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
         unsafe { Box::from_raw(rw) }
@@ -972,54 +1339,70 @@
 
     /// Invokes [`capacity`] on the underlying instance of [`OsString`].
     ///
-    /// [`capacity`]: ../ffi/struct.OsString.html#method.capacity
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`capacity`]: OsString::capacity
+    #[inline]
+    #[allow(clippy::ptr_arg)]
     pub fn capacity(&self) -> usize {
         self.inner.capacity()
     }
 
     /// Invokes [`clear`] on the underlying instance of [`OsString`].
     ///
-    /// [`clear`]: ../ffi/struct.OsString.html#method.clear
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`clear`]: OsString::clear
+    #[inline]
     pub fn clear(&mut self) {
         self.inner.clear()
     }
 
     /// Invokes [`reserve`] on the underlying instance of [`OsString`].
     ///
-    /// [`reserve`]: ../ffi/struct.OsString.html#method.reserve
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`reserve`]: OsString::reserve
+    #[inline]
     pub fn reserve(&mut self, additional: usize) {
         self.inner.reserve(additional)
     }
 
     /// Invokes [`reserve_exact`] on the underlying instance of [`OsString`].
     ///
-    /// [`reserve_exact`]: ../ffi/struct.OsString.html#method.reserve_exact
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`reserve_exact`]: OsString::reserve_exact
+    #[inline]
     pub fn reserve_exact(&mut self, additional: usize) {
         self.inner.reserve_exact(additional)
     }
 
     /// Invokes [`shrink_to_fit`] on the underlying instance of [`OsString`].
     ///
-    /// [`shrink_to_fit`]: ../ffi/struct.OsString.html#method.shrink_to_fit
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`shrink_to_fit`]: OsString::shrink_to_fit
+    #[inline]
     pub fn shrink_to_fit(&mut self) {
         self.inner.shrink_to_fit()
     }
 
     /// Invokes [`shrink_to`] on the underlying instance of [`OsString`].
     ///
-    /// [`shrink_to`]: ../ffi/struct.OsString.html#method.shrink_to
-    /// [`OsString`]: ../ffi/struct.OsString.html
+    /// [`shrink_to`]: OsString::shrink_to
+    #[inline]
     pub fn shrink_to(&mut self, min_capacity: usize) {
         self.inner.shrink_to(min_capacity)
     }
 }
 
+impl Clone for PathBuf {
+    #[inline]
+    fn clone(&self) -> Self {
+        PathBuf { inner: self.inner.clone() }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, source: &Self) {
+        self.inner.clone_from(&source.inner)
+    }
+}
+
 impl From<&Path> for Box<Path> {
+    /// Creates a boxed [`Path`] from a reference.
+    ///
+    /// This will allocate and clone `path` to it.
     fn from(path: &Path) -> Box<Path> {
         let boxed: Box<OsStr> = path.inner.into();
         let rw = Box::into_raw(boxed) as *mut Path;
@@ -1027,10 +1410,24 @@
     }
 }
 
+impl From<Cow<'_, Path>> for Box<Path> {
+    /// Creates a boxed [`Path`] from a clone-on-write pointer.
+    ///
+    /// Converting from a `Cow::Owned` does not clone or allocate.
+    #[inline]
+    fn from(cow: Cow<'_, Path>) -> Box<Path> {
+        match cow {
+            Cow::Borrowed(path) => Box::from(path),
+            Cow::Owned(path) => Box::from(path),
+        }
+    }
+}
+
 impl From<Box<Path>> for PathBuf {
     /// Converts a `Box<Path>` into a `PathBuf`
     ///
     /// This conversion does not allocate or copy memory.
+    #[inline]
     fn from(boxed: Box<Path>) -> PathBuf {
         boxed.into_path_buf()
     }
@@ -1041,6 +1438,7 @@
     ///
     /// This conversion currently should not allocate memory,
     /// but this behavior is not guaranteed on all platforms or in all future versions.
+    #[inline]
     fn from(p: PathBuf) -> Box<Path> {
         p.into_boxed_path()
     }
@@ -1054,13 +1452,17 @@
 }
 
 impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
+    /// Converts a borrowed `OsStr` to a `PathBuf`.
+    ///
+    /// Allocates a [`PathBuf`] and copies the data into it.
+    #[inline]
     fn from(s: &T) -> PathBuf {
         PathBuf::from(s.as_ref().to_os_string())
     }
 }
 
 impl From<OsString> for PathBuf {
-    /// Converts a `OsString` into a `PathBuf`
+    /// Converts an [`OsString`] into a [`PathBuf`]
     ///
     /// This conversion does not allocate or copy memory.
     #[inline]
@@ -1070,18 +1472,20 @@
 }
 
 impl From<PathBuf> for OsString {
-    /// Converts a `PathBuf` into a `OsString`
+    /// Converts a [`PathBuf`] into an [`OsString`]
     ///
     /// This conversion does not allocate or copy memory.
+    #[inline]
     fn from(path_buf: PathBuf) -> OsString {
         path_buf.inner
     }
 }
 
 impl From<String> for PathBuf {
-    /// Converts a `String` into a `PathBuf`
+    /// Converts a [`String`] into a [`PathBuf`]
     ///
     /// This conversion does not allocate or copy memory.
+    #[inline]
     fn from(s: String) -> PathBuf {
         PathBuf::from(OsString::from(s))
     }
@@ -1090,6 +1494,7 @@
 impl FromStr for PathBuf {
     type Err = core::convert::Infallible;
 
+    #[inline]
     fn from_str(s: &str) -> Result<Self, Self::Err> {
         Ok(PathBuf::from(s))
     }
@@ -1107,6 +1512,11 @@
     fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
         iter.into_iter().for_each(move |p| self.push(p.as_ref()));
     }
+
+    #[inline]
+    fn extend_one(&mut self, p: P) {
+        self.push(p.as_ref());
+    }
 }
 
 impl fmt::Debug for PathBuf {
@@ -1124,18 +1534,24 @@
 }
 
 impl Borrow<Path> for PathBuf {
+    #[inline]
     fn borrow(&self) -> &Path {
         self.deref()
     }
 }
 
 impl Default for PathBuf {
+    #[inline]
     fn default() -> Self {
         PathBuf::new()
     }
 }
 
 impl<'a> From<&'a Path> for Cow<'a, Path> {
+    /// Creates a clone-on-write pointer from a reference to
+    /// [`Path`].
+    ///
+    /// This conversion does not clone or allocate.
     #[inline]
     fn from(s: &'a Path) -> Cow<'a, Path> {
         Cow::Borrowed(s)
@@ -1143,6 +1559,10 @@
 }
 
 impl<'a> From<PathBuf> for Cow<'a, Path> {
+    /// Creates a clone-on-write pointer from an owned
+    /// instance of [`PathBuf`].
+    ///
+    /// This conversion does not clone or allocate.
     #[inline]
     fn from(s: PathBuf) -> Cow<'a, Path> {
         Cow::Owned(s)
@@ -1150,6 +1570,10 @@
 }
 
 impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
+    /// Creates a clone-on-write pointer from a reference to
+    /// [`PathBuf`].
+    ///
+    /// This conversion does not clone or allocate.
     #[inline]
     fn from(p: &'a PathBuf) -> Cow<'a, Path> {
         Cow::Borrowed(p.as_path())
@@ -1157,6 +1581,9 @@
 }
 
 impl<'a> From<Cow<'a, Path>> for PathBuf {
+    /// Converts a clone-on-write pointer to an owned path.
+    ///
+    /// Converting from a `Cow::Owned` does not clone or allocate.
     #[inline]
     fn from(p: Cow<'a, Path>) -> Self {
         p.into_owned()
@@ -1164,7 +1591,7 @@
 }
 
 impl From<PathBuf> for Arc<Path> {
-    /// Converts a `PathBuf` into an `Arc` by moving the `PathBuf` data into a new `Arc` buffer.
+    /// Converts a [`PathBuf`] into an [`Arc`] by moving the [`PathBuf`] data into a new [`Arc`] buffer.
     #[inline]
     fn from(s: PathBuf) -> Arc<Path> {
         let arc: Arc<OsStr> = Arc::from(s.into_os_string());
@@ -1173,7 +1600,7 @@
 }
 
 impl From<&Path> for Arc<Path> {
-    /// Converts a `Path` into an `Arc` by copying the `Path` data into a new `Arc` buffer.
+    /// Converts a [`Path`] into an [`Arc`] by copying the [`Path`] data into a new [`Arc`] buffer.
     #[inline]
     fn from(s: &Path) -> Arc<Path> {
         let arc: Arc<OsStr> = Arc::from(s.as_os_str());
@@ -1182,7 +1609,7 @@
 }
 
 impl From<PathBuf> for Rc<Path> {
-    /// Converts a `PathBuf` into an `Rc` by moving the `PathBuf` data into a new `Rc` buffer.
+    /// Converts a [`PathBuf`] into an [`Rc`] by moving the [`PathBuf`] data into a new `Rc` buffer.
     #[inline]
     fn from(s: PathBuf) -> Rc<Path> {
         let rc: Rc<OsStr> = Rc::from(s.into_os_string());
@@ -1191,7 +1618,7 @@
 }
 
 impl From<&Path> for Rc<Path> {
-    /// Converts a `Path` into an `Rc` by copying the `Path` data into a new `Rc` buffer.
+    /// Converts a [`Path`] into an [`Rc`] by copying the [`Path`] data into a new `Rc` buffer.
     #[inline]
     fn from(s: &Path) -> Rc<Path> {
         let rc: Rc<OsStr> = Rc::from(s.as_os_str());
@@ -1201,15 +1628,18 @@
 
 impl ToOwned for Path {
     type Owned = PathBuf;
+    #[inline]
     fn to_owned(&self) -> PathBuf {
         self.to_path_buf()
     }
+    #[inline]
     fn clone_into(&self, target: &mut PathBuf) {
         self.inner.clone_into(&mut target.inner);
     }
 }
 
 impl cmp::PartialEq for PathBuf {
+    #[inline]
     fn eq(&self, other: &PathBuf) -> bool {
         self.components() == other.components()
     }
@@ -1224,18 +1654,21 @@
 impl cmp::Eq for PathBuf {}
 
 impl cmp::PartialOrd for PathBuf {
+    #[inline]
     fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
-        self.components().partial_cmp(other.components())
+        Some(compare_components(self.components(), other.components()))
     }
 }
 
 impl cmp::Ord for PathBuf {
+    #[inline]
     fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
-        self.components().cmp(other.components())
+        compare_components(self.components(), other.components())
     }
 }
 
 impl AsRef<OsStr> for PathBuf {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         &self.inner[..]
     }
@@ -1252,25 +1685,44 @@
 /// pointer like `&` or [`Box`]. For an owned version of this type,
 /// see [`PathBuf`].
 ///
-/// [`str`]: ../primitive.str.html
-/// [`Box`]: ../boxed/struct.Box.html
-/// [`PathBuf`]: struct.PathBuf.html
-///
 /// More details about the overall approach can be found in
-/// the [module documentation](index.html).
+/// the [module documentation](self).
 ///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+/// use std::ffi::OsStr;
+///
+/// // Note: this example does work on Windows
+/// let path = Path::new("./foo/bar.txt");
+///
+/// let parent = path.parent();
+/// assert_eq!(parent, Some(Path::new("./foo")));
+///
+/// let file_stem = path.file_stem();
+/// assert_eq!(file_stem, Some(OsStr::new("bar")));
+///
+/// let extension = path.extension();
+/// assert_eq!(extension, Some(OsStr::new("txt")));
+/// ```
+#[cfg_attr(not(test), rustc_diagnostic_item = "Path")]
+// FIXME:
+// `Path::new` current implementation relies
+// on `Path` being layout-compatible with `OsStr`.
+// When attribute privacy is implemented, `Path` should be annotated as `#[repr(transparent)]`.
+// Anyway, `Path` representation and layout are considered implementation detail, are
+// not documented and must not be relied upon.
 pub struct Path {
     inner: OsStr,
 }
 
-/// An error returned from [`Path::strip_prefix`][`strip_prefix`] if the prefix
-/// was not found.
+/// An error returned from [`Path::strip_prefix`] if the prefix was not found.
 ///
 /// This `struct` is created by the [`strip_prefix`] method on [`Path`].
 /// See its documentation for more.
 ///
-/// [`strip_prefix`]: struct.Path.html#method.strip_prefix
-/// [`Path`]: struct.Path.html
+/// [`strip_prefix`]: Path::strip_prefix
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct StripPrefixError(());
 
@@ -1289,14 +1741,39 @@
     ///
     /// This is a cost-free conversion.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// Path::new("foo.txt");
+    /// ```
+    ///
+    /// You can create `Path`s from `String`s, or even other `Path`s:
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let string = String::from("foo.txt");
+    /// let from_string = Path::new(&string);
+    /// let from_path = Path::new(&from_string);
+    /// assert_eq!(from_string, from_path);
+    /// ```
     pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
         unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
     }
 
     /// Yields the underlying [`OsStr`] slice.
     ///
-    /// [`OsStr`]: ../ffi/struct.OsStr.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let os_str = Path::new("foo.txt").as_os_str();
+    /// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt"));
+    /// ```
+    #[inline]
     pub fn as_os_str(&self) -> &OsStr {
         &self.inner
     }
@@ -1307,8 +1784,17 @@
     /// Note that validation is performed because non-UTF-8 strings are
     /// perfectly valid for some OS.
     ///
-    /// [`&str`]: ../primitive.str.html
+    /// [`&str`]: str
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("foo.txt");
+    /// assert_eq!(path.to_str(), Some("foo.txt"));
+    /// ```
+    #[inline]
     pub fn to_str(&self) -> Option<&str> {
         self.inner.to_str()
     }
@@ -1318,17 +1804,36 @@
     /// Any non-Unicode sequences are replaced with
     /// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
     ///
-    /// [`Cow<str>`]: ../borrow/enum.Cow.html
-    /// [U+FFFD]: ../char/constant.REPLACEMENT_CHARACTER.html
+    /// [U+FFFD]: super::char::REPLACEMENT_CHARACTER
     ///
+    /// # Examples
+    ///
+    /// Calling `to_string_lossy` on a `Path` with valid unicode:
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("foo.txt");
+    /// assert_eq!(path.to_string_lossy(), "foo.txt");
+    /// ```
+    ///
+    /// Had `path` contained invalid unicode, the `to_string_lossy` call might
+    /// have returned `"fo�.txt"`.
+    #[inline]
     pub fn to_string_lossy(&self) -> Cow<'_, str> {
         self.inner.to_string_lossy()
     }
 
     /// Converts a `Path` to an owned [`PathBuf`].
     ///
-    /// [`PathBuf`]: struct.PathBuf.html
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path_buf = Path::new("foo.txt").to_path_buf();
+    /// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt"));
+    /// ```
     #[rustc_conversion_suggestion]
     pub fn to_path_buf(&self) -> PathBuf {
         PathBuf::from(self.inner.to_os_string())
@@ -1343,6 +1848,15 @@
     /// * On Windows, a path is absolute if it has a prefix and starts with the
     /// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// assert!(!Path::new("foo.txt").is_absolute());
+    /// ```
+    ///
+    /// [`has_root`]: Path::has_root
     #[allow(deprecated)]
     pub fn is_absolute(&self) -> bool {
         self.has_root() && (cfg!(unix) || self.prefix().is_some())
@@ -1352,6 +1866,16 @@
     ///
     /// See [`is_absolute`]'s documentation for more details.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// assert!(Path::new("foo.txt").is_relative());
+    /// ```
+    ///
+    /// [`is_absolute`]: Path::is_absolute
+    #[inline]
     pub fn is_relative(&self) -> bool {
         !self.is_absolute()
     }
@@ -1369,6 +1893,14 @@
     ///     * has a prefix followed by a separator, e.g., `c:\windows` but not `c:windows`
     ///     * has any non-disk prefix, e.g., `\\server\share`
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// assert!(Path::new("/etc/passwd").has_root());
+    /// ```
+    #[inline]
     pub fn has_root(&self) -> bool {
         self.components().has_root()
     }
@@ -1377,8 +1909,19 @@
     ///
     /// Returns [`None`] if the path terminates in a root or prefix.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/foo/bar");
+    /// let parent = path.parent().unwrap();
+    /// assert_eq!(parent, Path::new("/foo"));
+    ///
+    /// let grand_parent = parent.parent().unwrap();
+    /// assert_eq!(grand_parent, Path::new("/"));
+    /// assert_eq!(grand_parent.parent(), None);
+    /// ```
     pub fn parent(&self) -> Option<&Path> {
         let mut comps = self.components();
         let comp = comps.next_back();
@@ -1398,8 +1941,29 @@
     /// [`None`], the iterator will do likewise. The iterator will always yield at least one value,
     /// namely `&self`.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let mut ancestors = Path::new("/foo/bar").ancestors();
+    /// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
+    /// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
+    /// assert_eq!(ancestors.next(), Some(Path::new("/")));
+    /// assert_eq!(ancestors.next(), None);
+    ///
+    /// let mut ancestors = Path::new("../foo/bar").ancestors();
+    /// assert_eq!(ancestors.next(), Some(Path::new("../foo/bar")));
+    /// assert_eq!(ancestors.next(), Some(Path::new("../foo")));
+    /// assert_eq!(ancestors.next(), Some(Path::new("..")));
+    /// assert_eq!(ancestors.next(), Some(Path::new("")));
+    /// assert_eq!(ancestors.next(), None);
+    /// ```
+    ///
+    /// [`parent`]: Path::parent
+    #[inline]
     pub fn ancestors(&self) -> Ancestors<'_> {
-        Ancestors { next: Some(&self) }
+        Ancestors { next: Some(self) }
     }
 
     /// Returns the final component of the `Path`, if there is one.
@@ -1409,8 +1973,19 @@
     ///
     /// Returns [`None`] if the path terminates in `..`.
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::Path;
+    /// use std::ffi::OsStr;
+    ///
+    /// assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
+    /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
+    /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
+    /// assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
+    /// assert_eq!(None, Path::new("foo.txt/..").file_name());
+    /// assert_eq!(None, Path::new("/").file_name());
+    /// ```
     pub fn file_name(&self) -> Option<&OsStr> {
         self.components().next_back().and_then(|p| match p {
             Component::Normal(p) => Some(p),
@@ -1425,9 +2000,27 @@
     /// If `base` is not a prefix of `self` (i.e., [`starts_with`]
     /// returns `false`), returns [`Err`].
     ///
-    /// [`starts_with`]: #method.starts_with
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
+    /// [`starts_with`]: Path::starts_with
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let path = Path::new("/test/haha/foo.txt");
+    ///
+    /// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
+    /// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
+    /// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
+    /// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
+    /// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
+    ///
+    /// assert!(path.strip_prefix("test").is_err());
+    /// assert!(path.strip_prefix("/haha").is_err());
+    ///
+    /// let prefix = PathBuf::from("/test/");
+    /// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
+    /// ```
     pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
     where
         P: AsRef<Path>,
@@ -1445,6 +2038,24 @@
     ///
     /// Only considers whole path components to match.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/etc/passwd");
+    ///
+    /// assert!(path.starts_with("/etc"));
+    /// assert!(path.starts_with("/etc/"));
+    /// assert!(path.starts_with("/etc/passwd"));
+    /// assert!(path.starts_with("/etc/passwd/")); // extra slash is okay
+    /// assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
+    ///
+    /// assert!(!path.starts_with("/e"));
+    /// assert!(!path.starts_with("/etc/passwd.txt"));
+    ///
+    /// assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
+    /// ```
     pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
         self._starts_with(base.as_ref())
     }
@@ -1457,6 +2068,20 @@
     ///
     /// Only considers whole path components to match.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/etc/resolv.conf");
+    ///
+    /// assert!(path.ends_with("resolv.conf"));
+    /// assert!(path.ends_with("etc/resolv.conf"));
+    /// assert!(path.ends_with("/etc/resolv.conf"));
+    ///
+    /// assert!(!path.ends_with("/resolv.conf"));
+    /// assert!(!path.ends_with("conf")); // use .extension() instead
+    /// ```
     pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
         self._ends_with(child.as_ref())
     }
@@ -1467,7 +2092,7 @@
 
     /// Extracts the stem (non-extension) portion of [`self.file_name`].
     ///
-    /// [`self.file_name`]: struct.Path.html#method.file_name
+    /// [`self.file_name`]: Path::file_name
     ///
     /// The stem is:
     ///
@@ -1476,10 +2101,55 @@
     /// * The entire file name if the file name begins with `.` and has no other `.`s within;
     /// * Otherwise, the portion of the file name before the final `.`
     ///
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
+    /// assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
+    /// ```
+    ///
+    /// # See Also
+    /// This method is similar to [`Path::file_prefix`], which extracts the portion of the file name
+    /// before the *first* `.`
+    ///
+    /// [`Path::file_prefix`]: Path::file_prefix
     ///
     pub fn file_stem(&self) -> Option<&OsStr> {
-        self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.or(after))
+        self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.or(after))
+    }
+
+    /// Extracts the prefix of [`self.file_name`].
+    ///
+    /// The prefix is:
+    ///
+    /// * [`None`], if there is no file name;
+    /// * The entire file name if there is no embedded `.`;
+    /// * The portion of the file name before the first non-beginning `.`;
+    /// * The entire file name if the file name begins with `.` and has no other `.`s within;
+    /// * The portion of the file name before the second `.` if the file name begins with `.`
+    ///
+    /// [`self.file_name`]: Path::file_name
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # #![feature(path_file_prefix)]
+    /// use std::path::Path;
+    ///
+    /// assert_eq!("foo", Path::new("foo.rs").file_prefix().unwrap());
+    /// assert_eq!("foo", Path::new("foo.tar.gz").file_prefix().unwrap());
+    /// ```
+    ///
+    /// # See Also
+    /// This method is similar to [`Path::file_stem`], which extracts the portion of the file name
+    /// before the *last* `.`
+    ///
+    /// [`Path::file_stem`]: Path::file_stem
+    ///
+    pub fn file_prefix(&self) -> Option<&OsStr> {
+        self.file_name().map(split_file_at_dot).map(|(before, _after)| before)
     }
 
     /// Extracts the extension of [`self.file_name`], if possible.
@@ -1491,20 +2161,32 @@
     /// * [`None`], if the file name begins with `.` and has no other `.`s within;
     /// * Otherwise, the portion of the file name after the final `.`
     ///
-    /// [`self.file_name`]: struct.Path.html#method.file_name
-    /// [`None`]: ../../std/option/enum.Option.html#variant.None
+    /// [`self.file_name`]: Path::file_name
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// assert_eq!("rs", Path::new("foo.rs").extension().unwrap());
+    /// assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
+    /// ```
     pub fn extension(&self) -> Option<&OsStr> {
-        self.file_name().map(split_file_at_dot).and_then(|(before, after)| before.and(after))
+        self.file_name().map(rsplit_file_at_dot).and_then(|(before, after)| before.and(after))
     }
 
     /// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
     ///
     /// See [`PathBuf::push`] for more details on what it means to adjoin a path.
     ///
-    /// [`PathBuf`]: struct.PathBuf.html
-    /// [`PathBuf::push`]: struct.PathBuf.html#method.push
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
+    /// ```
+    #[must_use]
     pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
         self._join(path.as_ref())
     }
@@ -1519,9 +2201,17 @@
     ///
     /// See [`PathBuf::set_file_name`] for more details.
     ///
-    /// [`PathBuf`]: struct.PathBuf.html
-    /// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let path = Path::new("/tmp/foo.txt");
+    /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
+    ///
+    /// let path = Path::new("/tmp");
+    /// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
+    /// ```
     pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> PathBuf {
         self._with_file_name(file_name.as_ref())
     }
@@ -1536,9 +2226,19 @@
     ///
     /// See [`PathBuf::set_extension`] for more details.
     ///
-    /// [`PathBuf`]: struct.PathBuf.html
-    /// [`PathBuf::set_extension`]: struct.PathBuf.html#method.set_extension
+    /// # Examples
     ///
+    /// ```
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let path = Path::new("foo.rs");
+    /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
+    ///
+    /// let path = Path::new("foo.tar.gz");
+    /// assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
+    /// assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
+    /// assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
+    /// ```
     pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> PathBuf {
         self._with_extension(extension.as_ref())
     }
@@ -1567,6 +2267,21 @@
     /// and `a/b/../c` are distinct, to account for the possibility that `b`
     /// is a symbolic link (so its parent isn't `a`).
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::{Path, Component};
+    /// use std::ffi::OsStr;
+    ///
+    /// let mut components = Path::new("/tmp/foo.txt").components();
+    ///
+    /// assert_eq!(components.next(), Some(Component::RootDir));
+    /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
+    /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
+    /// assert_eq!(components.next(), None)
+    /// ```
+    ///
+    /// [`CurDir`]: Component::CurDir
     pub fn components(&self) -> Components<'_> {
         let prefix = parse_prefix(self.as_os_str());
         Components {
@@ -1584,18 +2299,42 @@
     /// For more information about the particulars of how the path is separated
     /// into components, see [`components`].
     ///
-    /// [`components`]: #method.components
-    /// [`OsStr`]: ../ffi/struct.OsStr.html
+    /// [`components`]: Path::components
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::{self, Path};
+    /// use std::ffi::OsStr;
+    ///
+    /// let mut it = Path::new("/tmp/foo.txt").iter();
+    /// assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
+    /// assert_eq!(it.next(), Some(OsStr::new("tmp")));
+    /// assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
+    /// assert_eq!(it.next(), None)
+    /// ```
+    #[inline]
     pub fn iter(&self) -> Iter<'_> {
         Iter { inner: self.components() }
     }
 
     /// Returns an object that implements [`Display`] for safely printing paths
-    /// that may contain non-Unicode data.
+    /// that may contain non-Unicode data. This may perform lossy conversion,
+    /// depending on the platform.  If you would like an implementation which
+    /// escapes the path please use [`Debug`] instead.
     ///
-    /// [`Display`]: ../fmt/trait.Display.html
+    /// [`Display`]: fmt::Display
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/tmp/foo.rs");
+    ///
+    /// println!("{}", path.display());
+    /// ```
+    #[inline]
     pub fn display(&self) -> Display<'_> {
         Display { path: self }
     }
@@ -1607,9 +2346,17 @@
     ///
     /// This is an alias to [`fs::metadata`].
     ///
-    /// [`fs::metadata`]: ../fs/fn.metadata.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/Minas/tirith");
+    /// let metadata = path.metadata().expect("metadata call failed");
+    /// println!("{:?}", metadata.file_type());
+    /// ```
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn metadata(&self) -> io::Result<fs::Metadata> {
         fs::metadata(self)
     }
@@ -1618,9 +2365,17 @@
     ///
     /// This is an alias to [`fs::symlink_metadata`].
     ///
-    /// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/Minas/tirith");
+    /// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
+    /// println!("{:?}", metadata.file_type());
+    /// ```
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
         fs::symlink_metadata(self)
     }
@@ -1630,9 +2385,16 @@
     ///
     /// This is an alias to [`fs::canonicalize`].
     ///
-    /// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::{Path, PathBuf};
+    ///
+    /// let path = Path::new("/foo/test/../test/bar.rs");
+    /// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
+    /// ```
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn canonicalize(&self) -> io::Result<PathBuf> {
         fs::canonicalize(self)
     }
@@ -1641,24 +2403,27 @@
     ///
     /// This is an alias to [`fs::read_link`].
     ///
-    /// [`fs::read_link`]: ../fs/fn.read_link.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    ///
+    /// let path = Path::new("/laputa/sky_castle.rs");
+    /// let path_link = path.read_link().expect("read_link call failed");
+    /// ```
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn read_link(&self) -> io::Result<PathBuf> {
         fs::read_link(self)
     }
 
     /// Returns an iterator over the entries within a directory.
     ///
-    /// The iterator will yield instances of [`io::Result`]`<`[`DirEntry`]`>`. New
+    /// The iterator will yield instances of [`io::Result`]`<`[`fs::DirEntry`]`>`. New
     /// errors may be encountered after an iterator is initially constructed.
     ///
     /// This is an alias to [`fs::read_dir`].
     ///
-    /// [`io::Result`]: ../io/type.Result.html
-    /// [`DirEntry`]: ../fs/struct.DirEntry.html
-    /// [`fs::read_dir`]: ../fs/fn.read_dir.html
-    ///
     /// # Examples
     ///
     /// ```no_run
@@ -1672,6 +2437,7 @@
     /// }
     /// ```
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn read_dir(&self) -> io::Result<fs::ReadDir> {
         fs::read_dir(self)
     }
@@ -1679,24 +2445,81 @@
     /// Returns `true` if the path points at an existing entity.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file.
     ///
-    /// If you cannot access the directory containing the file, e.g., because of a
-    /// permission error, this will return `false`.
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// assert!(!Path::new("does_not_exist.txt").exists());
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`].
     #[cfg(feature = "untrusted_fs")]
+    #[inline]
     pub fn exists(&self) -> bool {
         fs::metadata(self).is_ok()
     }
 
+    /// Returns `Ok(true)` if the path points at an existing entity.
+    ///
+    /// This function will traverse symbolic links to query information about the
+    /// destination file. In case of broken symbolic links this will return `Ok(false)`.
+    ///
+    /// As opposed to the `exists()` method, this one doesn't silently ignore errors
+    /// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
+    /// denied on some of the parent directories.)
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(path_try_exists)]
+    ///
+    /// use std::path::Path;
+    /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
+    /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
+    /// ```
+    // FIXME: stabilization should modify documentation of `exists()` to recommend this method
+    // instead.
+    #[cfg(feature = "untrusted_fs")]
+    #[inline]
+    pub fn try_exists(&self) -> io::Result<bool> {
+        fs::try_exists(self)
+    }
+
     /// Returns `true` if the path exists on disk and is pointing at a regular file.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file.
     ///
-    /// If you cannot access the directory containing the file, e.g., because of a
-    /// permission error, this will return `false`.
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
+    /// assert_eq!(Path::new("a_file.txt").is_file(), true);
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
+    /// [`fs::Metadata::is_file`] if it was [`Ok`].
+    ///
+    /// When the goal is simply to read from (or write to) the source, the most
+    /// reliable way to test the source can be read (or written to) is to open
+    /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
+    /// a Unix-like system for example. See [`fs::File::open`] or
+    /// [`fs::OpenOptions::open`] for more information.
     #[cfg(feature = "untrusted_fs")]
     pub fn is_file(&self) -> bool {
         fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
@@ -1705,21 +2528,57 @@
     /// Returns `true` if the path exists on disk and is pointing at a directory.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file.
     ///
-    /// If you cannot access the directory containing the file, e.g., because of a
-    /// permission error, this will return `false`.
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
+    /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
+    /// [`fs::Metadata::is_dir`] if it was [`Ok`].
     #[cfg(feature = "untrusted_fs")]
     pub fn is_dir(&self) -> bool {
         fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
     }
 
-    /// Converts a [`Box<Path>`][`Box`] into a [`PathBuf`] without copying or
-    /// allocating.
+    /// Returns true if the path exists on disk and is pointing at a symbolic link.
     ///
-    /// [`Box`]: ../../std/boxed/struct.Box.html
-    /// [`PathBuf`]: struct.PathBuf.html
+    /// This function will not traverse symbolic links.
+    /// In case of a broken symbolic link this will also return true.
+    ///
+    /// If you cannot access the directory containing the file, e.g., because of a
+    /// permission error, this will return false.
+    ///
+    /// # Examples
+    ///
+    #[cfg_attr(unix, doc = "```no_run")]
+    #[cfg_attr(not(unix), doc = "```ignore")]
+    /// #![feature(is_symlink)]
+    /// use std::path::Path;
+    /// use std::os::unix::fs::symlink;
+    ///
+    /// let link_path = Path::new("link");
+    /// symlink("/origin_does_not_exists/", link_path).unwrap();
+    /// assert_eq!(link_path.is_symlink(), true);
+    /// assert_eq!(link_path.exists(), false);
+    /// ```
+    #[cfg(feature = "untrusted_fs")]
+    pub fn is_symlink(&self) -> bool {
+        fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
+    }
+
+    /// Converts a [`Box<Path>`](Box) into a [`PathBuf`] without copying or
+    /// allocating.
     pub fn into_path_buf(self: Box<Path>) -> PathBuf {
         let rw = Box::into_raw(self) as *mut OsStr;
         let inner = unsafe { Box::from_raw(rw) };
@@ -1728,6 +2587,7 @@
 }
 
 impl AsRef<OsStr> for Path {
+    #[inline]
     fn as_ref(&self) -> &OsStr {
         &self.inner
     }
@@ -1743,8 +2603,22 @@
 ///
 /// A [`Path`] might contain non-Unicode data. This `struct` implements the
 /// [`Display`] trait in a way that mitigates that. It is created by the
-/// [`display`][`Path::display`] method on [`Path`].
+/// [`display`](Path::display) method on [`Path`]. This may perform lossy
+/// conversion, depending on the platform. If you would like an implementation
+/// which escapes the path please use [`Debug`] instead.
 ///
+/// # Examples
+///
+/// ```
+/// use std::path::Path;
+///
+/// let path = Path::new("/tmp/foo.rs");
+///
+/// println!("{}", path.display());
+/// ```
+///
+/// [`Display`]: fmt::Display
+/// [`format!`]: crate::format
 pub struct Display<'a> {
     path: &'a Path,
 }
@@ -1762,8 +2636,9 @@
 }
 
 impl cmp::PartialEq for Path {
+    #[inline]
     fn eq(&self, other: &Path) -> bool {
-        self.components().eq(other.components())
+        self.components() == other.components()
     }
 }
 
@@ -1778,36 +2653,42 @@
 impl cmp::Eq for Path {}
 
 impl cmp::PartialOrd for Path {
+    #[inline]
     fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
-        self.components().partial_cmp(other.components())
+        Some(compare_components(self.components(), other.components()))
     }
 }
 
 impl cmp::Ord for Path {
+    #[inline]
     fn cmp(&self, other: &Path) -> cmp::Ordering {
-        self.components().cmp(other.components())
+        compare_components(self.components(), other.components())
     }
 }
 
 impl AsRef<Path> for Path {
+    #[inline]
     fn as_ref(&self) -> &Path {
         self
     }
 }
 
 impl AsRef<Path> for OsStr {
+    #[inline]
     fn as_ref(&self) -> &Path {
         Path::new(self)
     }
 }
 
 impl AsRef<Path> for Cow<'_, OsStr> {
+    #[inline]
     fn as_ref(&self) -> &Path {
         Path::new(self)
     }
 }
 
 impl AsRef<Path> for OsString {
+    #[inline]
     fn as_ref(&self) -> &Path {
         Path::new(self)
     }
@@ -1821,6 +2702,7 @@
 }
 
 impl AsRef<Path> for String {
+    #[inline]
     fn as_ref(&self) -> &Path {
         Path::new(self)
     }
@@ -1836,6 +2718,7 @@
 impl<'a> IntoIterator for &'a PathBuf {
     type Item = &'a OsStr;
     type IntoIter = Iter<'a>;
+    #[inline]
     fn into_iter(self) -> Iter<'a> {
         self.iter()
     }
@@ -1844,6 +2727,7 @@
 impl<'a> IntoIterator for &'a Path {
     type Item = &'a OsStr;
     type IntoIter = Iter<'a>;
+    #[inline]
     fn into_iter(self) -> Iter<'a> {
         self.iter()
     }
@@ -1935,12 +2819,14 @@
 impl_cmp_os_str!(Cow<'a, Path>, OsString);
 
 impl fmt::Display for StripPrefixError {
+    #[allow(deprecated, deprecated_in_future)]
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         self.description().fmt(f)
     }
 }
 
 impl Error for StripPrefixError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "prefix not found"
     }
diff --git a/sgx_tstd/src/prelude/mod.rs b/sgx_tstd/src/prelude/mod.rs
index cb8353f..465dc89 100644
--- a/sgx_tstd/src/prelude/mod.rs
+++ b/sgx_tstd/src/prelude/mod.rs
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! The Rust Prelude.
+//! # The Rust Prelude
 //!
 //! Rust comes with a variety of things in its standard library. However, if
 //! you had to manually import every single thing that you used, it would be
@@ -27,14 +27,96 @@
 //! things, particularly traits, which are used in almost every single Rust
 //! program.
 //!
-//! On a technical level, Rust inserts
+//! # Other preludes
 //!
+//! Preludes can be seen as a pattern to make using multiple types more
+//! convenient. As such, you'll find other preludes in the standard library,
+//! such as [`std::io::prelude`]. Various libraries in the Rust ecosystem may
+//! also define their own preludes.
+//!
+//! [`std::io::prelude`]: crate::io::prelude
+//!
+//! The difference between 'the prelude' and these other preludes is that they
+//! are not automatically `use`'d, and must be imported manually. This is still
+//! easier than importing all of their constituent components.
+//!
+//! # Prelude contents
+//!
+//! The first version of the prelude is used in Rust 2015 and Rust 2018,
+//! and lives in [`std::prelude::v1`].
+//! [`std::prelude::rust_2015`] and [`std::prelude::rust_2018`] re-export this prelude.
+//! It re-exports the following:
+//!
+//! * <code>[std::marker]::{[Copy], [Send], [Sized], [Sync], [Unpin]}</code>,
+//!   marker traits that indicate fundamental properties of types.
+//! * <code>[std::ops]::{[Drop], [Fn], [FnMut], [FnOnce]}</code>, various
+//!   operations for both destructors and overloading `()`.
+//! * <code>[std::mem]::[drop][mem::drop]</code>, a convenience function for explicitly
+//!   dropping a value.
+//! * <code>[std::boxed]::[Box]</code>, a way to allocate values on the heap.
+//! * <code>[std::borrow]::[ToOwned]</code>, the conversion trait that defines
+//!   [`to_owned`], the generic method for creating an owned type from a
+//!   borrowed type.
+//! * <code>[std::clone]::[Clone]</code>, the ubiquitous trait that defines
+//!   [`clone`][Clone::clone], the method for producing a copy of a value.
+//! * <code>[std::cmp]::{[PartialEq], [PartialOrd], [Eq], [Ord]}</code>, the
+//!   comparison traits, which implement the comparison operators and are often
+//!   seen in trait bounds.
+//! * <code>[std::convert]::{[AsRef], [AsMut], [Into], [From]}</code>, generic
+//!   conversions, used by savvy API authors to create overloaded methods.
+//! * <code>[std::default]::[Default]</code>, types that have default values.
+//! * <code>[std::iter]::{[Iterator], [Extend], [IntoIterator], [DoubleEndedIterator], [ExactSizeIterator]}</code>,
+//!   iterators of various
+//!   kinds.
+//! * <code>[std::option]::[Option]::{[self][Option], [Some], [None]}</code>, a
+//!   type which expresses the presence or absence of a value. This type is so
+//!   commonly used, its variants are also exported.
+//! * <code>[std::result]::[Result]::{[self][Result], [Ok], [Err]}</code>, a type
+//!   for functions that may succeed or fail. Like [`Option`], its variants are
+//!   exported as well.
+//! * <code>[std::string]::{[String], [ToString]}</code>, heap-allocated strings.
+//! * <code>[std::vec]::[Vec]</code>, a growable, heap-allocated vector.
+//!
+//! The prelude used in Rust 2021, [`std::prelude::rust_2021`], includes all of the above,
+//! and in addition re-exports:
+//!
+//! * <code>[std::convert]::{[TryFrom], [TryInto]}</code>,
+//! * <code>[std::iter]::[FromIterator]</code>.
+//!
+//! [mem::drop]: crate::mem::drop
+//! [std::borrow]: crate::borrow
+//! [std::boxed]: crate::boxed
+//! [std::clone]: crate::clone
+//! [std::cmp]: crate::cmp
+//! [std::convert]: crate::convert
+//! [std::default]: crate::default
+//! [std::iter]: crate::iter
+//! [std::marker]: crate::marker
+//! [std::mem]: crate::mem
+//! [std::ops]: crate::ops
+//! [std::option]: crate::option
+//! [`std::prelude::v1`]: v1
+//! [`std::prelude::rust_2015`]: rust_2015
+//! [`std::prelude::rust_2018`]: rust_2018
+//! [`std::prelude::rust_2021`]: rust_2021
+//! [std::result]: crate::result
+//! [std::slice]: crate::slice
+//! [std::string]: crate::string
+//! [std::vec]: mod@crate::vec
+//! [TryFrom]: crate::convert::TryFrom
+//! [TryInto]: crate::convert::TryInto
+//! [FromIterator]: crate::iter::FromIterator
+//! [`to_owned`]: crate::borrow::ToOwned::to_owned
+//! [book-closures]: ../../book/ch13-01-closures.html
+//! [book-dtor]: ../../book/ch15-03-drop.html
+//! [book-enums]: ../../book/ch06-01-defining-an-enum.html
+//! [book-iter]: ../../book/ch13-02-iterators.html
+
 pub mod v1;
 
 /// The 2015 version of the prelude of The Rust Standard Library.
 ///
 /// See the [module-level documentation](self) for more.
-#[cfg(enable_prelude_version)]
 pub mod rust_2015 {
     #[doc(no_inline)]
     pub use super::v1::*;
@@ -43,7 +125,6 @@
 /// The 2018 version of the prelude of The Rust Standard Library.
 ///
 /// See the [module-level documentation](self) for more.
-#[cfg(enable_prelude_version)]
 pub mod rust_2018 {
     #[doc(no_inline)]
     pub use super::v1::*;
@@ -52,7 +133,6 @@
 /// The 2021 version of the prelude of The Rust Standard Library.
 ///
 /// See the [module-level documentation](self) for more.
-#[cfg(enable_prelude_version)]
 pub mod rust_2021 {
     #[doc(no_inline)]
     pub use super::v1::*;
diff --git a/sgx_tstd/src/prelude/v1.rs b/sgx_tstd/src/prelude/v1.rs
index c6b5d7b..715598a 100644
--- a/sgx_tstd/src/prelude/v1.rs
+++ b/sgx_tstd/src/prelude/v1.rs
@@ -17,8 +17,9 @@
 
 //! The first version of the prelude of The Rust Standard Library.
 //!
-//! See the [module-level documentation](../index.html) for more.
+//! See the [module-level documentation](super) for more.
 
+// Re-exported core operators
 #[doc(no_inline)]
 pub use crate::marker::{Send, Sized, Sync, Unpin};
 #[doc(no_inline)]
@@ -41,28 +42,37 @@
 pub use crate::result::Result::{self, Err, Ok};
 
 // Re-exported built-in macros
+#[allow(deprecated)]
 #[doc(no_inline)]
 pub use core::prelude::v1::{
-    asm, assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
-    format_args_nl, global_asm, include, include_bytes, include_str, line, llvm_asm, log_syntax,
-    module_path, option_env, stringify, trace_macros,
+    assert, cfg, column, compile_error, concat, concat_idents, env, file, format_args,
+    format_args_nl, include, include_bytes, include_str, line, llvm_asm, log_syntax, module_path,
+    option_env, stringify, trace_macros, Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq,
+    PartialOrd,
 };
 
-// FIXME: Attribute and derive macros are not documented because for them rustdoc generates
+#[doc(no_inline)]
+pub use core::prelude::v1::asm;
+
+#[doc(no_inline)]
+pub use core::prelude::v1::global_asm;
+
+// FIXME: Attribute and internal derive macros are not documented because for them rustdoc generates
 // dead links which fail link checker testing.
-#[allow(deprecated)]
+#[allow(deprecated, deprecated_in_future)]
 #[doc(hidden)]
 pub use core::prelude::v1::{
-    global_allocator, test, test_case, Clone, Copy, Debug, Default, Eq, Hash, Ord,
-    PartialEq, PartialOrd, RustcDecodable, RustcEncodable,
+    global_allocator, test, test_case, RustcDecodable, RustcEncodable,
 };
 
-#[cfg(derive_macros)]
 #[doc(hidden)]
 pub use core::prelude::v1::derive;
 #[doc(hidden)]
 pub use core::prelude::v1::cfg_accessible;
 
+#[doc(hidden)]
+pub use core::prelude::v1::cfg_eval;
+
 // The file so far is equivalent to src/libcore/prelude/v1.rs,
 // and below to src/liballoc/prelude.rs.
 // Those files are duplicated rather than using glob imports
diff --git a/sgx_tstd/src/rand/reader.rs b/sgx_tstd/src/rand/reader.rs
deleted file mode 100644
index 868f88a..0000000
--- a/sgx_tstd/src/rand/reader.rs
+++ /dev/null
@@ -1,69 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-//! A wrapper around any Read to treat it as an RNG.
-
-#![allow(dead_code)]
-
-use crate::io::prelude::*;
-use rand::Rng;
-
-/// An RNG that reads random bytes straight from a `Read`. This will
-/// work best with an infinite reader, but this is not required.
-///
-/// # Panics
-///
-/// It will panic if it there is insufficient data to fulfill a request.
-pub struct ReaderRng<R> {
-    reader: R
-}
-
-impl<R: Read> ReaderRng<R> {
-    /// Create a new `ReaderRng` from a `Read`.
-    pub fn new(r: R) -> ReaderRng<R> {
-        ReaderRng {
-            reader: r
-        }
-    }
-}
-
-impl<R: Read> Rng for ReaderRng<R> {
-    fn next_u32(&mut self) -> u32 {
-        // This is designed for speed: reading a LE integer on a LE
-        // platform just involves blitting the bytes into the memory
-        // of the u32, similarly for BE on BE; avoiding byteswapping.
-        let mut bytes = [0; 4];
-        self.fill_bytes(&mut bytes);
-        unsafe { *(bytes.as_ptr() as *const u32) }
-    }
-    fn next_u64(&mut self) -> u64 {
-        // see above for explanation.
-        let mut bytes = [0; 8];
-        self.fill_bytes(&mut bytes);
-        unsafe { *(bytes.as_ptr() as *const u64) }
-    }
-    fn fill_bytes(&mut self, mut v: &mut [u8]) {
-        while !v.is_empty() {
-            let t = v;
-            match self.reader.read(t) {
-                Ok(0) => panic!("ReaderRng.fill_bytes: EOF reached"),
-                Ok(n) => v = t.split_at_mut(n).1,
-                Err(e) => panic!("ReaderRng.fill_bytes: {}", e),
-            }
-        }
-    }
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/rt.rs b/sgx_tstd/src/rt.rs
index ab97c0e..e090e4c 100644
--- a/sgx_tstd/src/rt.rs
+++ b/sgx_tstd/src/rt.rs
@@ -17,19 +17,19 @@
 
 //! Runtime services
 
-use alloc_crate::slice;
-use core::str;
 use crate::enclave;
+use crate::slice;
+use crate::str;
 use crate::sync::SgxSpinlock;
 use crate::thread;
 use sgx_trts::enclave::rsgx_is_supported_EDMM;
 use sgx_types::{sgx_enclave_id_t, sgx_thread_t, SGX_THREAD_T_NULL};
 
 // Reexport some of our utilities which are expected by other crates.
-pub use crate::panicking::{begin_panic, begin_panic_fmt, update_panic_count};
-pub use crate::sys_common::at_exit;
-use crate::sys_common::cleanup;
+pub use crate::panicking::{begin_panic, begin_panic_fmt, panic_count};
 use crate::sync::Once;
+pub use crate::sys_common::at_exit;
+use crate::sys_common::rt::cleanup;
 
 static INIT: Once = Once::new();
 static EXIT: Once = Once::new();
@@ -43,13 +43,9 @@
     }
 
     GLOBAL_INIT_LOCK.lock();
-    EXIT.call_once(|| {
-        unsafe {
-            if INIT_TCS == thread::rsgx_thread_self() {
-                if !rsgx_is_supported_EDMM() {
-                    uninit_global_object();
-                }
-            }
+    EXIT.call_once(|| unsafe {
+        if INIT_TCS == thread::rsgx_thread_self() && !rsgx_is_supported_EDMM() {
+            uninit_global_object();
         }
     });
 }
@@ -76,4 +72,3 @@
 global_dtors_object! {
     GLOBAL_DTORS, global_exit = { cleanup(); }
 }
-
diff --git a/sgx_tstd/src/sgxfs.rs b/sgx_tstd/src/sgxfs.rs
index 91b42d7..ffacfad 100644
--- a/sgx_tstd/src/sgxfs.rs
+++ b/sgx_tstd/src/sgxfs.rs
@@ -17,11 +17,11 @@
 
 //! Filesystem manipulation operations.
 
-use sgx_types::{sgx_key_128bit_t, sgx_align_key_128bit_t};
 use crate::io::{self, SeekFrom, Seek, Read, Initializer, Write};
 use crate::path::Path;
 use crate::sys::sgxfs as fs_imp;
 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
+use sgx_types::{sgx_key_128bit_t, sgx_align_key_128bit_t};
 
 /// A reference to an open file on the filesystem.
 ///
@@ -275,12 +275,12 @@
 
     fn _open(&self, path: &Path) -> io::Result<SgxFile> {
         let inner = fs_imp::SgxFile::open(path, &self.0)?;
-        Ok(SgxFile { inner: inner })
+        Ok(SgxFile { inner })
     }
 
     fn _open_ex(&self, path: &Path, key: &sgx_key_128bit_t) -> io::Result<SgxFile> {
         let inner = fs_imp::SgxFile::open_ex(path, &self.0, key)?;
-        Ok(SgxFile { inner: inner })
+        Ok(SgxFile { inner })
     }
 }
 
diff --git a/sgx_tstd/src/sync/barrier.rs b/sgx_tstd/src/sync/barrier.rs
index 1f96aff..b5c7d93 100644
--- a/sgx_tstd/src/sync/barrier.rs
+++ b/sgx_tstd/src/sync/barrier.rs
@@ -15,15 +15,38 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::fmt;
-use crate::sync::{SgxCondvar, SgxMutex};
+use crate::fmt;
+use crate::sync::{SgxCondvar as Condvar, SgxMutex as Mutex};
 
 /// A barrier enables multiple threads to synchronize the beginning
 /// of some computation.
 ///
+/// # Examples
+///
+/// ```
+/// use std::sync::{Arc, Barrier};
+/// use std::thread;
+///
+/// let mut handles = Vec::with_capacity(10);
+/// let barrier = Arc::new(Barrier::new(10));
+/// for _ in 0..10 {
+///     let c = Arc::clone(&barrier);
+///     // The same messages will be printed together.
+///     // You will NOT see any interleaving.
+///     handles.push(thread::spawn(move|| {
+///         println!("before wait");
+///         c.wait();
+///         println!("after wait");
+///     }));
+/// }
+/// // Wait for other threads to finish.
+/// for handle in handles {
+///     handle.join().unwrap();
+/// }
+/// ```
 pub struct Barrier {
-    lock: SgxMutex<BarrierState>,
-    cvar: SgxCondvar,
+    lock: Mutex<BarrierState>,
+    cvar: Condvar,
     num_threads: usize,
 }
 
@@ -33,32 +56,44 @@
     generation_id: usize,
 }
 
-/// A `BarrierWaitResult` is returned by [`wait`] when all threads in the [`Barrier`]
-/// have rendezvoused.
+/// A `BarrierWaitResult` is returned by [`Barrier::wait()`] when all threads
+/// in the [`Barrier`] have rendezvoused.
 ///
-/// [`wait`]: struct.Barrier.html#method.wait
-/// [`Barrier`]: struct.Barrier.html
+/// # Examples
 ///
+/// ```
+/// use std::sync::Barrier;
+///
+/// let barrier = Barrier::new(1);
+/// let barrier_wait_result = barrier.wait();
+/// ```
 pub struct BarrierWaitResult(bool);
 
 impl fmt::Debug for Barrier {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Barrier { .. }")
+        f.debug_struct("Barrier").finish_non_exhaustive()
     }
 }
 
 impl Barrier {
     /// Creates a new barrier that can block a given number of threads.
     ///
-    /// A barrier will block `n`-1 threads which call [`wait`] and then wake up
-    /// all threads at once when the `n`th thread calls [`wait`].
+    /// A barrier will block `n`-1 threads which call [`wait()`] and then wake
+    /// up all threads at once when the `n`th thread calls [`wait()`].
     ///
-    /// [`wait`]: #method.wait
+    /// [`wait()`]: Barrier::wait
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::Barrier;
+    ///
+    /// let barrier = Barrier::new(10);
+    /// ```
     pub fn new(n: usize) -> Barrier {
         Barrier {
-            lock: SgxMutex::new(BarrierState { count: 0, generation_id: 0 }),
-            cvar: SgxCondvar::new(),
+            lock: Mutex::new(BarrierState { count: 0, generation_id: 0 }),
+            cvar: Condvar::new(),
             num_threads: n,
         }
     }
@@ -69,20 +104,40 @@
     /// be used continuously.
     ///
     /// A single (arbitrary) thread will receive a [`BarrierWaitResult`] that
-    /// returns `true` from [`is_leader`] when returning from this function, and
-    /// all other threads will receive a result that will return `false` from
-    /// [`is_leader`].
+    /// returns `true` from [`BarrierWaitResult::is_leader()`] when returning
+    /// from this function, and all other threads will receive a result that
+    /// will return `false` from [`BarrierWaitResult::is_leader()`].
     ///
-    /// [`BarrierWaitResult`]: struct.BarrierWaitResult.html
-    /// [`is_leader`]: struct.BarrierWaitResult.html#method.is_leader
+    /// # Examples
     ///
+    /// ```
+    /// use std::sync::{Arc, Barrier};
+    /// use std::thread;
+    ///
+    /// let mut handles = Vec::with_capacity(10);
+    /// let barrier = Arc::new(Barrier::new(10));
+    /// for _ in 0..10 {
+    ///     let c = Arc::clone(&barrier);
+    ///     // The same messages will be printed together.
+    ///     // You will NOT see any interleaving.
+    ///     handles.push(thread::spawn(move|| {
+    ///         println!("before wait");
+    ///         c.wait();
+    ///         println!("after wait");
+    ///     }));
+    /// }
+    /// // Wait for other threads to finish.
+    /// for handle in handles {
+    ///     handle.join().unwrap();
+    /// }
+    /// ```
     pub fn wait(&self) -> BarrierWaitResult {
         let mut lock = self.lock.lock().unwrap();
         let local_gen = lock.generation_id;
         lock.count += 1;
         if lock.count < self.num_threads {
             // We need a while loop to guard against spurious wakeups.
-            // http://en.wikipedia.org/wiki/Spurious_wakeup
+            // https://en.wikipedia.org/wiki/Spurious_wakeup
             while local_gen == lock.generation_id && lock.count < self.num_threads {
                 lock = self.cvar.wait(lock).unwrap();
             }
@@ -103,13 +158,21 @@
 }
 
 impl BarrierWaitResult {
-    /// Returns `true` if this thread from [`wait`] is the "leader thread".
+    /// Returns `true` if this thread is the "leader thread" for the call to
+    /// [`Barrier::wait()`].
     ///
     /// Only one thread will have `true` returned from their result, all other
     /// threads will have `false` returned.
     ///
-    /// [`wait`]: struct.Barrier.html#method.wait
+    /// # Examples
     ///
+    /// ```
+    /// use std::sync::Barrier;
+    ///
+    /// let barrier = Barrier::new(1);
+    /// let barrier_wait_result = barrier.wait();
+    /// println!("{:?}", barrier_wait_result.is_leader());
+    /// ```
     pub fn is_leader(&self) -> bool {
         self.0
     }
diff --git a/sgx_tstd/src/sync/condvar.rs b/sgx_tstd/src/sync/condvar.rs
index 48aefbe..faeb08e 100644
--- a/sgx_tstd/src/sync/condvar.rs
+++ b/sgx_tstd/src/sync/condvar.rs
@@ -27,84 +27,78 @@
 //! Synchronization library supports, as well as the OCALLs that each API function needs.
 //!
 
-use sgx_types::SysError;
-use sgx_trts::oom;
-use sgx_trts::libc;
-use core::sync::atomic::{AtomicUsize, Ordering};
-use core::fmt;
-use core::alloc::AllocError;
-use alloc_crate::boxed::Box;
-use crate::sync::{mutex, SgxThreadMutex, SgxMutexGuard};
-use crate::sys_common::poison::{self, LockResult, PoisonError};
-use crate::time::Duration;
-use crate::time::Instant;
-use crate::sys::condvar as imp;
+use crate::alloc::AllocError;
+use crate::fmt;
+use crate::sync::{mutex, poison, LockResult, SgxMutexGuard, PoisonError};
+use crate::sys_common::condvar as sys;
+use crate::time::{Duration, Instant};
 #[cfg(not(feature = "untrusted_time"))]
 use crate::untrusted::time::InstantEx;
 
+use sgx_libc as libc;
+use sgx_trts::oom;
+
+pub use crate::sys_common::condvar::SgxThreadCondvar;
+
 /// A type indicating whether a timed wait on a condition variable returned
 /// due to a time out or not.
 ///
 /// It is returned by the [`wait_timeout`] method.
 ///
-/// [`wait_timeout`]: struct.Condvar.html#method.wait_timeout
+/// [`wait_timeout`]: Condvar::wait_timeout
 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
 pub struct WaitTimeoutResult(bool);
 
 impl WaitTimeoutResult {
     /// Returns `true` if the wait was known to have timed out.
     ///
+    /// # Examples
+    ///
+    /// This example spawns a thread which will update the boolean value and
+    /// then wait 100 milliseconds before notifying the condvar.
+    ///
+    /// The main thread will wait with a timeout on the condvar and then leave
+    /// once the boolean has been updated and notified.
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxCondvar as Condvar, SgxMutex as Mutex};
+    /// use std::thread;
+    /// use std::time::Duration;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move || {
+    ///     let (lock, cvar) = &*pair2;
+    ///
+    ///     // Let's wait 20 milliseconds before notifying the condvar.
+    ///     thread::sleep(Duration::from_millis(20));
+    ///
+    ///     let mut started = lock.lock().unwrap();
+    ///     // We update the boolean value.
+    ///     *started = true;
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// loop {
+    ///     // Let's put a timeout on the condvar's wait.
+    ///     let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap();
+    ///     // 10 milliseconds have passed, or maybe the value changed!
+    ///     started = result.0;
+    ///     if *started == true {
+    ///         // We received the notification and the value has been updated, we can leave.
+    ///         break
+    ///     }
+    /// }
+    /// ```
     pub fn timed_out(&self) -> bool {
         self.0
     }
 }
 
-pub struct SgxThreadCondvar(imp::SgxThreadCondvar);
-
-unsafe impl Send for SgxThreadCondvar {}
-unsafe impl Sync for SgxThreadCondvar {}
-
-impl SgxThreadCondvar {
-    pub const fn new() -> SgxThreadCondvar {
-        SgxThreadCondvar(imp::SgxThreadCondvar::new())
-    }
-
-    #[inline]
-    pub unsafe fn wait(&self, mutex: &SgxThreadMutex) -> SysError {
-        self.0.wait(mutex::raw(mutex))
-    }
-
-    #[inline]
-    pub unsafe fn wait_timeout(&self, mutex: &SgxThreadMutex, dur: Duration) -> SysError {
-        self.0.wait_timeout(mutex::raw(mutex), dur)
-    }
-
-    #[inline]
-    pub unsafe fn signal(&self) -> SysError {
-        self.0.signal()
-    }
-
-    #[inline]
-    pub unsafe fn broadcast(&self) -> SysError {
-        self.0.broadcast()
-    }
-
-    #[inline]
-    pub unsafe fn notify_one(&self) -> SysError {
-        self.signal()
-    }
-
-    #[inline]
-    pub unsafe fn notify_all(&self) -> SysError {
-        self.broadcast()
-    }
-
-    #[inline]
-    pub unsafe fn destroy(&self) -> SysError {
-        self.0.destroy()
-    }
-}
-
 /// A Condition Variable
 ///
 /// Condition variables represent the ability to block a thread such that it
@@ -113,28 +107,52 @@
 /// and a mutex. The predicate is always verified inside of the mutex before
 /// determining that a thread must block.
 ///
-/// Functions in this module will block the current **thread** of execution and
-/// are bindings to system-provided condition variables where possible. Note
-/// that this module places one additional restriction over the system condition
-/// variables: each condvar can be used with precisely one mutex at runtime. Any
-/// attempt to use multiple mutexes on the same condition variable will result
-/// in a runtime panic. If this is not desired, then the unsafe primitives in
-/// `sys` do not have this restriction but may result in undefined behavior.
+/// Functions in this module will block the current **thread** of execution.
+/// Note that any attempt to use multiple mutexes on the same condition
+/// variable may result in a runtime panic.
 ///
+/// # Examples
+///
+/// ```
+/// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+/// use std::thread;
+///
+/// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+/// let pair2 = Arc::clone(&pair);
+///
+/// // Inside of our lock, spawn a new thread, and then wait for it to start.
+/// thread::spawn(move|| {
+///     let (lock, cvar) = &*pair2;
+///     let mut started = lock.lock().unwrap();
+///     *started = true;
+///     // We notify the condvar that the value has changed.
+///     cvar.notify_one();
+/// });
+///
+/// // Wait for the thread to start up.
+/// let (lock, cvar) = &*pair;
+/// let mut started = lock.lock().unwrap();
+/// while !*started {
+///     started = cvar.wait(started).unwrap();
+/// }
+/// ```
 pub struct SgxCondvar {
-    inner: Box<SgxThreadCondvar>,
-    mutex: AtomicUsize,
+    inner: sys::SgxMovableThreadCondvar,
 }
 
 impl SgxCondvar {
+    /// Creates a new condition variable which is ready to be waited on and
+    /// notified.
     ///
-    /// Creates a new condition variable which is ready to be waited on and notified.
+    /// # Examples
     ///
+    /// ```
+    /// use std::sync::SgxCondvar as Condvar;
+    ///
+    /// let condvar = Condvar::new();
+    /// ```
     pub fn new() -> SgxCondvar {
-        SgxCondvar {
-            inner: Box::new(SgxThreadCondvar::new()),
-            mutex: AtomicUsize::new(0),
-        }
+        SgxCondvar { inner: sys::SgxMovableThreadCondvar::new() }
     }
 
     /// Blocks the current thread until this condition variable receives a
@@ -159,14 +177,42 @@
     ///
     /// # Panics
     ///
-    /// This function will [`panic!`] if it is used with more than one mutex
-    /// over time. Each condition variable is dynamically bound to exactly one
-    /// mutex to ensure defined behavior across platforms. If this functionality
-    /// is not desired, then unsafe primitives in `sys` are provided.
+    /// This function may [`panic!`] if it is used with more than one mutex
+    /// over time.
+    ///
+    /// [`notify_one`]: Self::notify_one
+    /// [`notify_all`]: Self::notify_all
+    /// [poisoning]: super::Mutex#poisoning
+    /// [`Mutex`]: super::Mutex
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut started = lock.lock().unwrap();
+    ///     *started = true;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
+    /// while !*started {
+    ///     started = cvar.wait(started).unwrap();
+    /// }
+    /// ```
     pub fn wait<'a, T>(&self, guard: SgxMutexGuard<'a, T>) -> LockResult<SgxMutexGuard<'a, T>> {
         let poisoned = unsafe {
             let lock = mutex::guard_lock(&guard);
-            self.verify(lock);
             self.inner.wait(lock);
             mutex::guard_poison(&guard).get()
         };
@@ -174,37 +220,6 @@
     }
 
     /// Blocks the current thread until this condition variable receives a
-    /// notification and the required condition is met. Spurious wakeups are
-    /// ignored and this function will only return once the condition has been
-    /// met.
-    ///
-    /// This function will atomically unlock the mutex specified (represented by
-    /// `guard`) and block the current thread. This means that any calls
-    /// to [`notify_one`] or [`notify_all`] which happen logically after the
-    /// mutex is unlocked are candidates to wake this thread up. When this
-    /// function call returns, the lock specified will have been re-acquired.
-    ///
-    /// # Errors
-    ///
-    /// This function will return an error if the mutex being waited on is
-    /// poisoned when this thread re-acquires the lock. For more information,
-    /// see information about [poisoning] on the [`Mutex`] type.
-    ///
-    pub fn wait_until<'a, T, F>(
-        &self,
-        mut guard: SgxMutexGuard<'a, T>,
-        mut condition: F
-    ) -> LockResult<SgxMutexGuard<'a, T>>
-    where
-        F: FnMut(&mut T) -> bool,
-    {
-        while !condition(&mut *guard) {
-            guard = self.wait(guard)?;
-        }
-        Ok(guard)
-    }
-
-    /// Blocks the current thread until this condition variable receives a
     /// notification and the provided condition is false.
     ///
     /// This function will atomically unlock the mutex specified (represented by
@@ -219,11 +234,33 @@
     /// poisoned when this thread re-acquires the lock. For more information,
     /// see information about [poisoning] on the [`Mutex`] type.
     ///
-    /// [`notify_one`]: #method.notify_one
-    /// [`notify_all`]: #method.notify_all
-    /// [poisoning]: ../sync/struct.Mutex.html#poisoning
-    /// [`Mutex`]: ../sync/struct.Mutex.html
+    /// [`notify_one`]: Self::notify_one
+    /// [`notify_all`]: Self::notify_all
+    /// [poisoning]: super::Mutex#poisoning
+    /// [`Mutex`]: super::Mutex
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    ///
+    /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut pending = lock.lock().unwrap();
+    ///     *pending = false;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// // As long as the value inside the `Mutex<bool>` is `true`, we wait.
+    /// let _guard = cvar.wait_while(lock.lock().unwrap(), |pending| { *pending }).unwrap();
+    /// ```
     pub fn wait_while<'a, T, F>(
         &self,
         mut guard: SgxMutexGuard<'a, T>,
@@ -245,7 +282,7 @@
     /// except that the thread will be blocked for roughly no longer
     /// than `ms` milliseconds. This method should not be used for
     /// precise timing due to anomalies such as preemption or platform
-    /// differences that may not cause the maximum amount of time
+    /// differences that might not cause the maximum amount of time
     /// waited to be precisely `ms`.
     ///
     /// Note that the best effort is made to ensure that the time waited is
@@ -258,8 +295,39 @@
     /// Like [`wait`], the lock specified will be re-acquired when this function
     /// returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait`]: #method.wait
+    /// [`wait`]: Self::wait
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut started = lock.lock().unwrap();
+    ///     *started = true;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
+    /// loop {
+    ///     let result = cvar.wait_timeout_ms(started, 10).unwrap();
+    ///     // 10 milliseconds have passed, or maybe the value changed!
+    ///     started = result.0;
+    ///     if *started == true {
+    ///         // We received the notification and the value has been updated, we can leave.
+    ///         break
+    ///     }
+    /// }
+    /// ```
     pub fn wait_timeout_ms<'a, T>(
         &self,
         guard: SgxMutexGuard<'a, T>,
@@ -275,7 +343,7 @@
     /// The semantics of this function are equivalent to [`wait`] except that
     /// the thread will be blocked for roughly no longer than `dur`. This
     /// method should not be used for precise timing due to anomalies such as
-    /// preemption or platform differences that may not cause the maximum
+    /// preemption or platform differences that might not cause the maximum
     /// amount of time waited to be precisely `dur`.
     ///
     /// Note that the best effort is made to ensure that the time waited is
@@ -295,10 +363,41 @@
     /// Like [`wait`], the lock specified will be re-acquired when this function
     /// returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait`]: #method.wait
-    /// [`wait_timeout_while`]: #method.wait_timeout_while
-    /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
+    /// [`wait`]: Self::wait
+    /// [`wait_timeout_while`]: Self::wait_timeout_while
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    /// use std::time::Duration;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut started = lock.lock().unwrap();
+    ///     *started = true;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // wait for the thread to start up
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// // as long as the value inside the `Mutex<bool>` is `false`, we wait
+    /// loop {
+    ///     let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap();
+    ///     // 10 milliseconds have passed, or maybe the value changed!
+    ///     started = result.0;
+    ///     if *started == true {
+    ///         // We received the notification and the value has been updated, we can leave.
+    ///         break
+    ///     }
+    /// }
+    /// ```
     pub fn wait_timeout<'a, T>(
         &self,
         guard: SgxMutexGuard<'a, T>,
@@ -306,7 +405,6 @@
     ) -> LockResult<(SgxMutexGuard<'a, T>, WaitTimeoutResult)> {
         let (poisoned, result) = unsafe {
             let lock = mutex::guard_lock(&guard);
-            self.verify(lock);
             let result = self.inner.wait_timeout(lock, dur);
             (mutex::guard_poison(&guard).get(), WaitTimeoutResult(result.err() == Some(libc::ETIMEDOUT)))
         };
@@ -314,57 +412,12 @@
     }
 
     /// Waits on this condition variable for a notification, timing out after a
-    /// specified duration.  Spurious wakes will not cause this function to
-    /// return.
-    ///
-    /// The semantics of this function are equivalent to [`wait_until`] except
-    /// that the thread will be blocked for roughly no longer than `dur`. This
-    /// method should not be used for precise timing due to anomalies such as
-    /// preemption or platform differences that may not cause the maximum
-    /// amount of time waited to be precisely `dur`.
-    ///
-    /// Note that the best effort is made to ensure that the time waited is
-    /// measured with a monotonic clock, and not affected by the changes made to
-    /// the system time.
-    ///
-    /// The returned [`WaitTimeoutResult`] value indicates if the timeout is
-    /// known to have elapsed without the condition being met.
-    ///
-    /// Like [`wait_until`], the lock specified will be re-acquired when this
-    /// function returns, regardless of whether the timeout elapsed or not.
-    ///
-    /// [`wait_until`]: #method.wait_until
-    /// [`wait_timeout`]: #method.wait_timeout
-    /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
-    ///
-    pub fn wait_timeout_until<'a, T, F>(
-    	&self,
-    	mut guard: SgxMutexGuard<'a, T>,
-        dur: Duration, mut condition: F,
-	) -> LockResult<(SgxMutexGuard<'a, T>, WaitTimeoutResult)>
-	where
-	    F: FnMut(&mut T) -> bool,
-	{
-        let start = Instant::now();
-        loop {
-            if condition(&mut *guard) {
-                return Ok((guard, WaitTimeoutResult(false)));
-            }
-            let timeout = match dur.checked_sub(start.elapsed()) {
-                Some(timeout) => timeout,
-                None => return Ok((guard, WaitTimeoutResult(true))),
-            };
-            guard = self.wait_timeout(guard, timeout)?.0;
-        }
-    }
-
-    /// Waits on this condition variable for a notification, timing out after a
     /// specified duration.
     ///
     /// The semantics of this function are equivalent to [`wait_while`] except
     /// that the thread will be blocked for roughly no longer than `dur`. This
     /// method should not be used for precise timing due to anomalies such as
-    /// preemption or platform differences that may not cause the maximum
+    /// preemption or platform differences that might not cause the maximum
     /// amount of time waited to be precisely `dur`.
     ///
     /// Note that the best effort is made to ensure that the time waited is
@@ -377,10 +430,39 @@
     /// Like [`wait_while`], the lock specified will be re-acquired when this
     /// function returns, regardless of whether the timeout elapsed or not.
     ///
-    /// [`wait_while`]: #method.wait_while
-    /// [`wait_timeout`]: #method.wait_timeout
-    /// [`WaitTimeoutResult`]: struct.WaitTimeoutResult.html
+    /// [`wait_while`]: Self::wait_while
+    /// [`wait_timeout`]: Self::wait_timeout
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    /// use std::time::Duration;
+    ///
+    /// let pair = Arc::new((Mutex::new(true), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut pending = lock.lock().unwrap();
+    ///     *pending = false;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // wait for the thread to start up
+    /// let (lock, cvar) = &*pair;
+    /// let result = cvar.wait_timeout_while(
+    ///     lock.lock().unwrap(),
+    ///     Duration::from_millis(100),
+    ///     |&mut pending| pending,
+    /// ).unwrap();
+    /// if result.1.timed_out() {
+    ///     // timed-out without the condition ever evaluating to false.
+    /// }
+    /// // access the locked mutex via result.0
+    /// ```
     pub fn wait_timeout_while<'a, T, F>(
         &self,
         mut guard: SgxMutexGuard<'a, T>,
@@ -407,7 +489,7 @@
     ///
     /// If there is a blocked thread on this condition variable, then it will
     /// be woken up from its call to [`wait`] or [`wait_timeout`]. Calls to
-    /// `notify_one` are not buffered in any way.
+    /// `signal` are not buffered in any way.
     ///
     /// To wake up all threads, see [`broadcast`].
     pub fn signal(&self) {
@@ -433,41 +515,92 @@
         }
     }
 
+    /// Wakes up one blocked thread on this condvar.
+    ///
+    /// If there is a blocked thread on this condition variable, then it will
+    /// be woken up from its call to [`wait`] or [`wait_timeout`]. Calls to
+    /// `notify_one` are not buffered in any way.
+    ///
+    /// To wake up all threads, see [`notify_all`].
+    ///
+    /// [`wait`]: Self::wait
+    /// [`wait_timeout`]: Self::wait_timeout
+    /// [`notify_all`]: Self::notify_all
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut started = lock.lock().unwrap();
+    ///     *started = true;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_one();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
+    /// while !*started {
+    ///     started = cvar.wait(started).unwrap();
+    /// }
+    /// ```
     #[inline]
     pub fn notify_one(&self) {
         self.signal()
     }
 
+    /// Wakes up all blocked threads on this condvar.
+    ///
+    /// This method will ensure that any current waiters on the condition
+    /// variable are awoken. Calls to `notify_all()` are not buffered in any
+    /// way.
+    ///
+    /// To wake up only one thread, see [`notify_one`].
+    ///
+    /// [`notify_one`]: Self::notify_one
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex, SgxCondvar as Condvar};
+    /// use std::thread;
+    ///
+    /// let pair = Arc::new((Mutex::new(false), Condvar::new()));
+    /// let pair2 = Arc::clone(&pair);
+    ///
+    /// thread::spawn(move|| {
+    ///     let (lock, cvar) = &*pair2;
+    ///     let mut started = lock.lock().unwrap();
+    ///     *started = true;
+    ///     // We notify the condvar that the value has changed.
+    ///     cvar.notify_all();
+    /// });
+    ///
+    /// // Wait for the thread to start up.
+    /// let (lock, cvar) = &*pair;
+    /// let mut started = lock.lock().unwrap();
+    /// // As long as the value inside the `Mutex<bool>` is `false`, we wait.
+    /// while !*started {
+    ///     started = cvar.wait(started).unwrap();
+    /// }
+    /// ```
     #[inline]
     pub fn notify_all(&self) {
         self.broadcast()
     }
-
-    fn verify(&self, mutex: &SgxThreadMutex) {
-        let addr = mutex as *const _ as usize;
-        match self.mutex.compare_and_swap(0, addr, Ordering::SeqCst) {
-            // If we got out 0, then we have successfully bound the mutex to
-            // this cvar.
-            0 => {}
-
-            // If we get out a value that's the same as `addr`, then someone
-            // already beat us to the punch.
-            n if n == addr => {}
-
-            // Anything else and we're using more than one mutex on this cvar,
-            // which is currently disallowed.
-            _ => panic!(
-                "attempted to use a condition variable with two \
-                         mutexes"
-            ),
-        }
-    }
 }
 
-
 impl fmt::Debug for SgxCondvar {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Condvar { .. }")
+        f.debug_struct("SgxCondvar").finish_non_exhaustive()
     }
 }
 
@@ -477,10 +610,3 @@
         SgxCondvar::new()
     }
 }
-
-impl Drop for SgxCondvar {
-    fn drop(&mut self) {
-        let result = unsafe { self.inner.destroy() };
-        debug_assert_eq!(result, Ok(()), "Error when destroy an SgxCondvar: {}", result.unwrap_err());
-    }
-}
diff --git a/sgx_tstd/src/sync/mod.rs b/sgx_tstd/src/sync/mod.rs
index 099c297..1d52d16 100644
--- a/sgx_tstd/src/sync/mod.rs
+++ b/sgx_tstd/src/sync/mod.rs
@@ -33,17 +33,18 @@
 pub use self::barrier::{Barrier, BarrierWaitResult};
 pub use self::condvar::{SgxCondvar, SgxThreadCondvar, WaitTimeoutResult};
 pub use self::mutex::{SgxMutex, SgxMutexGuard, SgxThreadMutex};
-pub use self::remutex::{SgxReentrantMutex, SgxReentrantMutexGuard, SgxReentrantThreadMutex};
 pub use self::once::{Once, OnceState, ONCE_INIT};
+pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
 pub use self::rwlock::{SgxRwLock, SgxRwLockReadGuard, SgxRwLockWriteGuard, SgxThreadRwLock};
 pub use self::spinlock::{SgxSpinlock, SgxSpinlockGuard, SgxThreadSpinlock};
-pub use crate::sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult};
+
 #[cfg(feature = "thread")]
 pub mod mpsc;
+
 mod barrier;
 mod condvar;
 mod mutex;
-mod remutex;
 mod once;
+mod poison;
 mod rwlock;
 mod spinlock;
diff --git a/sgx_tstd/src/sync/mpsc/blocking.rs b/sgx_tstd/src/sync/mpsc/blocking.rs
index 603a285..3ab6730 100644
--- a/sgx_tstd/src/sync/mpsc/blocking.rs
+++ b/sgx_tstd/src/sync/mpsc/blocking.rs
@@ -17,16 +17,16 @@
 
 //! Generic support for building blocking abstractions.
 
-use core::sync::atomic::{AtomicBool, Ordering};
-use core::mem;
-use alloc_crate::sync::Arc;
-use crate::thread::{self, SgxThread};
+use crate::mem;
+use crate::sync::atomic::{AtomicBool, Ordering};
+use crate::sync::Arc;
+use crate::thread::{self, SgxThread as Thread};
 use crate::time::Instant;
 #[cfg(not(feature = "untrusted_time"))]
 use crate::untrusted::time::InstantEx;
 
 struct Inner {
-    thread: SgxThread,
+    thread: Thread,
     woken: AtomicBool,
 }
 
@@ -55,7 +55,11 @@
 
 impl SignalToken {
     pub fn signal(&self) -> bool {
-        let wake = !self.inner.woken.compare_and_swap(false, true, Ordering::SeqCst);
+        let wake = self
+            .inner
+            .woken
+            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
+            .is_ok();
         if wake {
             self.inner.thread.unpark();
         }
diff --git a/sgx_tstd/src/sync/mpsc/cache_aligned.rs b/sgx_tstd/src/sync/mpsc/cache_aligned.rs
index c888a1f..090732b 100644
--- a/sgx_tstd/src/sync/mpsc/cache_aligned.rs
+++ b/sgx_tstd/src/sync/mpsc/cache_aligned.rs
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::ops::{Deref, DerefMut};
+use crate::ops::{Deref, DerefMut};
 
 #[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(align(64))]
diff --git a/sgx_tstd/src/sync/mpsc/mod.rs b/sgx_tstd/src/sync/mpsc/mod.rs
index 37e779d..0523e58 100644
--- a/sgx_tstd/src/sync/mpsc/mod.rs
+++ b/sgx_tstd/src/sync/mpsc/mod.rs
@@ -15,8 +15,6 @@
 // specific language governing permissions and limitations
 // under the License..
 
-// ignore-tidy-filelength
-
 //! Multi-producer, single-consumer FIFO queue communication primitives.
 //!
 //! This module provides message-based communication over channels, concretely
@@ -44,12 +42,7 @@
 //!    that a bound of 0 is allowed, causing the channel to become a "rendezvous"
 //!    channel where each sender atomically hands off a message to a receiver.
 //!
-//! [`Sender`]: ../../../std/sync/mpsc/struct.Sender.html
-//! [`SyncSender`]: ../../../std/sync/mpsc/struct.SyncSender.html
-//! [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
-//! [`send`]: ../../../std/sync/mpsc/struct.Sender.html#method.send
-//! [`channel`]: ../../../std/sync/mpsc/fn.channel.html
-//! [`sync_channel`]: ../../../std/sync/mpsc/fn.sync_channel.html
+//! [`send`]: Sender::send
 //!
 //! ## Disconnection
 //!
@@ -63,9 +56,7 @@
 //! will continue to [`unwrap`] the results returned from this module,
 //! instigating a propagation of failure among threads if one unexpectedly dies.
 //!
-//! [`Result`]: ../../../std/result/enum.Result.html
-//! [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-//! [`unwrap`]: ../../../std/result/enum.Result.html#method.unwrap
+//! [`unwrap`]: Result::unwrap
 //!
 //! # Examples
 //!
@@ -131,6 +122,35 @@
 //! });
 //! rx.recv().unwrap();
 //! ```
+//!
+//! Unbounded receive loop:
+//!
+//! ```
+//! use std::sync::mpsc::sync_channel;
+//! use std::thread;
+//!
+//! let (tx, rx) = sync_channel(3);
+//!
+//! for _ in 0..3 {
+//!     // It would be the same without thread and clone here
+//!     // since there will still be one `tx` left.
+//!     let tx = tx.clone();
+//!     // cloned tx dropped within thread
+//!     thread::spawn(move || tx.send("ok").unwrap());
+//! }
+//!
+//! // Drop the last sender to stop `rx` waiting for message.
+//! // The program will not complete if we comment this out.
+//! // **All** `tx` needs to be dropped for `rx` to have `Err`.
+//! drop(tx);
+//!
+//! // Unbounded receiver waiting for all senders to complete.
+//! while let Ok(msg) = rx.recv() {
+//!     println!("{}", msg);
+//! }
+//!
+//! println!("completed");
+//! ```
 
 
 // A description of how Rust's channel implementation works
@@ -285,21 +305,22 @@
 // And now that you've seen all the races that I found and attempted to fix,
 // here's the code for you to find some more!
 
-use alloc_crate::sync::Arc;
-use core::fmt;
-use core::mem;
-use core::cell::UnsafeCell;
+use crate::cell::UnsafeCell;
 use crate::error;
+use crate::fmt;
+use crate::mem;
+use crate::sync::Arc;
 use crate::time::{Duration, Instant};
 #[cfg(not(feature = "untrusted_time"))]
 use crate::untrusted::time::InstantEx;
+
 mod blocking;
+mod mpsc_queue;
 mod oneshot;
 mod shared;
+mod spsc_queue;
 mod stream;
 mod sync;
-mod mpsc_queue;
-mod spsc_queue;
 
 mod cache_aligned;
 
@@ -308,9 +329,7 @@
 ///
 /// Messages sent to the channel can be retrieved using [`recv`].
 ///
-/// [`channel`]: fn.channel.html
-/// [`sync_channel`]: fn.sync_channel.html
-/// [`recv`]: struct.Receiver.html#method.recv
+/// [`recv`]: Receiver::recv
 ///
 /// # Examples
 ///
@@ -347,10 +366,8 @@
 /// waiting for a new message, and [`None`] will be returned
 /// when the corresponding channel has hung up.
 ///
-/// [`iter`]: struct.Receiver.html#method.iter
-/// [`Receiver`]: struct.Receiver.html
-/// [`next`]: ../../../std/iter/trait.Iterator.html#tymethod.next
-/// [`None`]: ../../../std/option/enum.Option.html#variant.None
+/// [`iter`]: Receiver::iter
+/// [`next`]: Iterator::next
 ///
 /// # Examples
 ///
@@ -384,9 +401,7 @@
 /// This iterator will never block the caller in order to wait for data to
 /// become available. Instead, it will return [`None`].
 ///
-/// [`Receiver`]: struct.Receiver.html
-/// [`try_iter`]: struct.Receiver.html#method.try_iter
-/// [`None`]: ../../../std/option/enum.Option.html#variant.None
+/// [`try_iter`]: Receiver::try_iter
 ///
 /// # Examples
 ///
@@ -426,9 +441,7 @@
 /// is called, waiting for a new message, and [`None`] will be
 /// returned if the corresponding channel has hung up.
 ///
-/// [`Receiver`]: struct.Receiver.html
-/// [`next`]: ../../../std/iter/trait.Iterator.html#tymethod.next
-/// [`None`]: ../../../std/option/enum.Option.html#variant.None
+/// [`next`]: Iterator::next
 ///
 /// # Examples
 ///
@@ -458,8 +471,10 @@
 ///
 /// Messages can be sent through this channel with [`send`].
 ///
-/// [`channel`]: fn.channel.html
-/// [`send`]: struct.Sender.html#method.send
+/// Note: all senders (the original and the clones) need to be dropped for the receiver
+/// to stop blocking to receive messages with [`Receiver::recv`].
+///
+/// [`send`]: Sender::send
 ///
 /// # Examples
 ///
@@ -501,9 +516,8 @@
 ///
 /// [`send`] will block if there is no space in the internal buffer.
 ///
-/// [`sync_channel`]: fn.sync_channel.html
-/// [`send`]: struct.SyncSender.html#method.send
-/// [`try_send`]: struct.SyncSender.html#method.try_send
+/// [`send`]: SyncSender::send
+/// [`try_send`]: SyncSender::try_send
 ///
 /// # Examples
 ///
@@ -554,9 +568,6 @@
 /// A **send** operation can only fail if the receiving end of a channel is
 /// disconnected, implying that the data could never be received. The error
 /// contains the data being sent as a payload so it can be recovered.
-///
-/// [`Sender::send`]: struct.Sender.html#method.send
-/// [`SyncSender::send`]: struct.SyncSender.html#method.send
 #[derive(PartialEq, Eq, Clone, Copy)]
 pub struct SendError<T>(pub T);
 
@@ -566,10 +577,7 @@
 /// [`channel`] (or [`sync_channel`]) is disconnected, implying that no further
 /// messages will ever be received.
 ///
-/// [`recv`]: struct.Receiver.html#method.recv
-/// [`Receiver`]: struct.Receiver.html
-/// [`channel`]: fn.channel.html
-/// [`sync_channel`]: fn.sync_channel.html
+/// [`recv`]: Receiver::recv
 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
 pub struct RecvError;
 
@@ -577,9 +585,7 @@
 /// not return data when called. This can occur with both a [`channel`] and
 /// a [`sync_channel`].
 ///
-/// [`try_recv`]: struct.Receiver.html#method.try_recv
-/// [`channel`]: fn.channel.html
-/// [`sync_channel`]: fn.sync_channel.html
+/// [`try_recv`]: Receiver::try_recv
 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
 pub enum TryRecvError {
     /// This **channel** is currently empty, but the **Sender**(s) have not yet
@@ -595,9 +601,7 @@
 /// unable to return data when called. This can occur with both a [`channel`] and
 /// a [`sync_channel`].
 ///
-/// [`recv_timeout`]: struct.Receiver.html#method.recv_timeout
-/// [`channel`]: fn.channel.html
-/// [`sync_channel`]: fn.sync_channel.html
+/// [`recv_timeout`]: Receiver::recv_timeout
 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
 pub enum RecvTimeoutError {
     /// This **channel** is currently empty, but the **Sender**(s) have not yet
@@ -611,7 +615,7 @@
 /// This enumeration is the list of the possible error outcomes for the
 /// [`try_send`] method.
 ///
-/// [`try_send`]: struct.SyncSender.html#method.try_send
+/// [`try_send`]: SyncSender::try_send
 #[derive(PartialEq, Eq, Clone, Copy)]
 pub enum TrySendError<T> {
     /// The data could not be sent on the [`sync_channel`] because it would require that
@@ -620,15 +624,10 @@
     /// If this is a buffered channel, then the buffer is full at this time. If
     /// this is not a buffered channel, then there is no [`Receiver`] available to
     /// acquire the data.
-    ///
-    /// [`sync_channel`]: fn.sync_channel.html
-    /// [`Receiver`]: struct.Receiver.html
     Full(T),
 
     /// This [`sync_channel`]'s receiving half has disconnected, so the data could not be
     /// sent. The data is returned back to the callee in this case.
-    ///
-    /// [`sync_channel`]: fn.sync_channel.html
     Disconnected(T),
 }
 
@@ -639,6 +638,8 @@
     Sync(Arc<sync::Packet<T>>),
 }
 
+#[doc(hidden)]
+#[allow(clippy::mut_from_ref)]
 trait UnsafeFlavor<T> {
     fn inner_unsafe(&self) -> &UnsafeCell<Flavor<T>>;
     unsafe fn inner_mut(&self) -> &mut Flavor<T> {
@@ -664,7 +665,7 @@
 /// the same order as it was sent, and no [`send`] will block the calling thread
 /// (this channel has an "infinite buffer", unlike [`sync_channel`], which will
 /// block after its buffer limit is reached). [`recv`] will block until a message
-/// is available.
+/// is available while there is at least one [`Sender`] alive (including clones).
 ///
 /// The [`Sender`] can be cloned to [`send`] to the same channel multiple times, but
 /// only one [`Receiver`] is supported.
@@ -674,13 +675,8 @@
 /// [`Sender`] is disconnected while trying to [`recv`], the [`recv`] method will
 /// return a [`RecvError`].
 ///
-/// [`send`]: struct.Sender.html#method.send
-/// [`recv`]: struct.Receiver.html#method.recv
-/// [`Sender`]: struct.Sender.html
-/// [`Receiver`]: struct.Receiver.html
-/// [`sync_channel`]: fn.sync_channel.html
-/// [`SendError`]: struct.SendError.html
-/// [`RecvError`]: struct.RecvError.html
+/// [`send`]: Sender::send
+/// [`recv`]: Receiver::recv
 ///
 /// # Examples
 ///
@@ -726,13 +722,8 @@
 /// [`SendError`]. Similarly, If the [`SyncSender`] is disconnected while trying
 /// to [`recv`], the [`recv`] method will return a [`RecvError`].
 ///
-/// [`channel`]: fn.channel.html
-/// [`send`]: struct.SyncSender.html#method.send
-/// [`recv`]: struct.Receiver.html#method.recv
-/// [`SyncSender`]: struct.SyncSender.html
-/// [`Receiver`]: struct.Receiver.html
-/// [`SendError`]: struct.SendError.html
-/// [`RecvError`]: struct.RecvError.html
+/// [`send`]: SyncSender::send
+/// [`recv`]: Receiver::recv
 ///
 /// # Examples
 ///
@@ -778,9 +769,6 @@
     /// will be received. It is possible for the corresponding receiver to
     /// hang up immediately after this function returns [`Ok`].
     ///
-    /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-    /// [`Ok`]: ../../../std/result/enum.Result.html#variant.Ok
-    ///
     /// This method will never block the current thread.
     ///
     /// # Examples
@@ -836,6 +824,11 @@
 }
 
 impl<T> Clone for Sender<T> {
+    /// Clone a sender to send to other threads.
+    ///
+    /// Note, be aware of the lifetime of the sender because all senders
+    /// (including the original) need to be dropped in order for
+    /// [`Receiver::recv`] to stop blocking.
     fn clone(&self) -> Sender<T> {
         let packet = match *unsafe { self.inner() } {
             Flavor::Oneshot(ref p) => {
@@ -892,7 +885,7 @@
 
 impl<T> fmt::Debug for Sender<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("Sender").finish()
+        f.debug_struct("Sender").finish_non_exhaustive()
     }
 }
 
@@ -921,9 +914,6 @@
     /// [`Receiver`] has disconnected and is no longer able to receive
     /// information.
     ///
-    /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-    /// [`Receiver`]: ../../../std/sync/mpsc/struct.Receiver.html
-    ///
     /// # Examples
     ///
     /// ```rust
@@ -958,7 +948,7 @@
     /// See [`send`] for notes about guarantees of whether the
     /// receiver has received the data or not if this function is successful.
     ///
-    /// [`send`]: ../../../std/sync/mpsc/struct.SyncSender.html#method.send
+    /// [`send`]: Self::send
     ///
     /// # Examples
     ///
@@ -1017,7 +1007,7 @@
 
 impl<T> fmt::Debug for SyncSender<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("SyncSender").finish()
+        f.debug_struct("SyncSender").finish_non_exhaustive()
     }
 }
 
@@ -1042,7 +1032,7 @@
     /// Compared with [`recv`], this function has two failure cases instead of one
     /// (one for disconnection, one for an empty buffer).
     ///
-    /// [`recv`]: struct.Receiver.html#method.recv
+    /// [`recv`]: Self::recv
     ///
     /// # Examples
     ///
@@ -1089,9 +1079,10 @@
     /// corresponding channel has hung up.
     ///
     /// This function will always block the current thread if there is no data
-    /// available and it's possible for more data to be sent. Once a message is
-    /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this
-    /// receiver will wake up and return that message.
+    /// available and it's possible for more data to be sent (at least one sender
+    /// still exists). Once a message is sent to the corresponding [`Sender`]
+    /// (or [`SyncSender`]), this receiver will wake up and return that
+    /// message.
     ///
     /// If the corresponding [`Sender`] has disconnected, or it disconnects while
     /// this call is blocking, this call will wake up and return [`Err`] to
@@ -1099,10 +1090,6 @@
     /// However, since channels are buffered, messages sent before the disconnect
     /// will still be properly received.
     ///
-    /// [`Sender`]: struct.Sender.html
-    /// [`SyncSender`]: struct.SyncSender.html
-    /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-    ///
     /// # Examples
     ///
     /// ```
@@ -1174,9 +1161,10 @@
     /// corresponding channel has hung up, or if it waits more than `timeout`.
     ///
     /// This function will always block the current thread if there is no data
-    /// available and it's possible for more data to be sent. Once a message is
-    /// sent to the corresponding [`Sender`] (or [`SyncSender`]), then this
-    /// receiver will wake up and return that message.
+    /// available and it's possible for more data to be sent (at least one sender
+    /// still exists). Once a message is sent to the corresponding [`Sender`]
+    /// (or [`SyncSender`]), this receiver will wake up and return that
+    /// message.
     ///
     /// If the corresponding [`Sender`] has disconnected, or it disconnects while
     /// this call is blocking, this call will wake up and return [`Err`] to
@@ -1184,10 +1172,6 @@
     /// However, since channels are buffered, messages sent before the disconnect
     /// will still be properly received.
     ///
-    /// [`Sender`]: struct.Sender.html
-    /// [`SyncSender`]: struct.SyncSender.html
-    /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-    ///
     /// # Known Issues
     ///
     /// There is currently a known issue (see [`#39364`]) that causes `recv_timeout`
@@ -1284,10 +1268,6 @@
     /// However, since channels are buffered, messages sent before the disconnect
     /// will still be properly received.
     ///
-    /// [`Sender`]: struct.Sender.html
-    /// [`SyncSender`]: struct.SyncSender.html
-    /// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
-    ///
     /// # Examples
     ///
     /// Successfully receiving value before reaching deadline:
@@ -1376,9 +1356,6 @@
     /// Returns an iterator that will block waiting for messages, but never
     /// [`panic!`]. It will return [`None`] when the channel has hung up.
     ///
-    /// [`panic!`]: ../../../std/macro.panic.html
-    /// [`None`]: ../../../std/option/enum.Option.html#variant.None
-    ///
     /// # Examples
     ///
     /// ```rust
@@ -1408,8 +1385,6 @@
     /// channel has hung up. The iterator will never [`panic!`] or block the
     /// user by waiting for values.
     ///
-    /// [`panic!`]: ../../../std/macro.panic.html
-    ///
     /// # Examples
     ///
     /// ```no_run
@@ -1500,13 +1475,13 @@
 
 impl<T> fmt::Debug for Receiver<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("Receiver").finish()
+        f.debug_struct("Receiver").finish_non_exhaustive()
     }
 }
 
 impl<T> fmt::Debug for SendError<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        "SendError(..)".fmt(f)
+        f.debug_struct("SendError").finish_non_exhaustive()
     }
 }
 
@@ -1517,6 +1492,7 @@
 }
 
 impl<T: Send> error::Error for SendError<T> {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "sending on a closed channel"
     }
@@ -1541,20 +1517,21 @@
 }
 
 impl<T: Send> error::Error for TrySendError<T> {
-
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         match *self {
             TrySendError::Full(..) => "sending on a full channel",
             TrySendError::Disconnected(..) => "sending on a closed channel",
         }
     }
-
-    fn cause(&self) -> Option<&dyn error::Error> {
-        None
-    }
 }
 
 impl<T> From<SendError<T>> for TrySendError<T> {
+    /// Converts a `SendError<T>` into a `TrySendError<T>`.
+    ///
+    /// This conversion always returns a `TrySendError::Disconnected` containing the data in the `SendError<T>`.
+    ///
+    /// No data is allocated on the heap.
     fn from(err: SendError<T>) -> TrySendError<T> {
         match err {
             SendError(t) => TrySendError::Disconnected(t),
@@ -1569,7 +1546,7 @@
 }
 
 impl error::Error for RecvError {
-
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "receiving on a closed channel"
     }
@@ -1585,7 +1562,7 @@
 }
 
 impl error::Error for TryRecvError {
-
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         match *self {
             TryRecvError::Empty => "receiving on an empty channel",
@@ -1595,6 +1572,11 @@
 }
 
 impl From<RecvError> for TryRecvError {
+    /// Converts a `RecvError` into a `TryRecvError`.
+    ///
+    /// This conversion always returns `TryRecvError::Disconnected`.
+    ///
+    /// No data is allocated on the heap.
     fn from(err: RecvError) -> TryRecvError {
         match err {
             RecvError => TryRecvError::Disconnected,
@@ -1612,6 +1594,7 @@
 }
 
 impl error::Error for RecvTimeoutError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         match *self {
             RecvTimeoutError::Timeout => "timed out waiting on channel",
@@ -1621,11 +1604,14 @@
 }
 
 impl From<RecvError> for RecvTimeoutError {
+    /// Converts a `RecvError` into a `RecvTimeoutError`.
+    ///
+    /// This conversion always returns `RecvTimeoutError::Disconnected`.
+    ///
+    /// No data is allocated on the heap.
     fn from(err: RecvError) -> RecvTimeoutError {
         match err {
             RecvError => RecvTimeoutError::Disconnected,
         }
     }
 }
-
-
diff --git a/sgx_tstd/src/sync/mpsc/mpsc_queue.rs b/sgx_tstd/src/sync/mpsc/mpsc_queue.rs
index 19b5a18..1550412 100644
--- a/sgx_tstd/src/sync/mpsc/mpsc_queue.rs
+++ b/sgx_tstd/src/sync/mpsc/mpsc_queue.rs
@@ -23,17 +23,19 @@
 //!
 //! Note that the current implementation of this queue has a caveat of the `pop`
 //! method, and see the method for more information about it. Due to this
-//! caveat, this queue may not be appropriate for all use-cases.
+//! caveat, this queue might not be appropriate for all use-cases.
 
-// http://www.1024cores.net/home/lock-free-algorithms
-//                         /queues/non-intrusive-mpsc-node-based-queue
+// https://www.1024cores.net/home/lock-free-algorithms
+//                          /queues/non-intrusive-mpsc-node-based-queue
+
 
 pub use self::PopResult::*;
 
-use core::ptr;
 use core::cell::UnsafeCell;
-use core::sync::atomic::{AtomicPtr, Ordering};
-use alloc_crate::boxed::Box;
+use core::ptr;
+
+use crate::boxed::Box;
+use crate::sync::atomic::{AtomicPtr, Ordering};
 
 /// A result of the `pop` function.
 pub enum PopResult<T> {
diff --git a/sgx_tstd/src/sync/mpsc/oneshot.rs b/sgx_tstd/src/sync/mpsc/oneshot.rs
index 1ffb8f6..6dd483f 100644
--- a/sgx_tstd/src/sync/mpsc/oneshot.rs
+++ b/sgx_tstd/src/sync/mpsc/oneshot.rs
@@ -38,22 +38,21 @@
 /// consuming the port). This upgrade is then also stored in the shared packet.
 /// The one caveat to consider is that when a port sees a disconnected channel
 /// it must check for data because there is no "data plus upgrade" state.
-
 pub use self::Failure::*;
 use self::MyUpgrade::*;
 pub use self::UpgradeResult::*;
 
-use core::cell::UnsafeCell;
-use core::ptr;
-use core::sync::atomic::{AtomicUsize, Ordering};
+use crate::cell::UnsafeCell;
+use crate::ptr;
+use crate::sync::atomic::{AtomicUsize, Ordering};
 use crate::sync::mpsc::blocking::{self, SignalToken};
 use crate::sync::mpsc::Receiver;
 use crate::time::Instant;
 
 // Various states you can find a port in.
-const EMPTY: usize = 0;          // initial state: no data, no blocked receiver
-const DATA: usize = 1;           // data ready for receiver to take
-const DISCONNECTED: usize = 2;   // channel is disconnected OR upgraded
+const EMPTY: usize = 0; // initial state: no data, no blocked receiver
+const DATA: usize = 1; // data ready for receiver to take
+const DISCONNECTED: usize = 2; // channel is disconnected OR upgraded
 // Any other value represents a pointer to a SignalToken value. The
 // protocol ensures that when the state moves *to* a pointer,
 // ownership of the token is given to the packet, and when the state
@@ -76,6 +75,7 @@
     Upgraded(Receiver<T>),
 }
 
+#[allow(clippy::enum_variant_names)]
 pub enum UpgradeResult {
     UpSuccess,
     UpDisconnected,
@@ -147,7 +147,7 @@
             let ptr = unsafe { signal_token.cast_to_usize() };
 
             // race with senders to enter the blocking state
-            if self.state.compare_and_swap(EMPTY, ptr, Ordering::SeqCst) == EMPTY {
+            if self.state.compare_exchange(EMPTY, ptr, Ordering::SeqCst, Ordering::SeqCst).is_ok() {
                 if let Some(deadline) = deadline {
                     let timed_out = !wait_token.wait_max_until(deadline);
                     // Try to reset the state
@@ -179,7 +179,12 @@
                 // the state changes under our feet we'd rather just see that state
                 // change.
                 DATA => {
-                    self.state.compare_and_swap(DATA, EMPTY, Ordering::SeqCst);
+                    let _ = self.state.compare_exchange(
+                        DATA,
+                        EMPTY,
+                        Ordering::SeqCst,
+                        Ordering::SeqCst,
+                    );
                     match (&mut *self.data.get()).take() {
                         Some(data) => Ok(data),
                         None => unreachable!(),
@@ -278,11 +283,14 @@
         let state = match self.state.load(Ordering::SeqCst) {
             // Each of these states means that no further activity will happen
             // with regard to abortion selection
-            s @ EMPTY | s @ DATA | s @ DISCONNECTED => s,
+            s @ (EMPTY | DATA | DISCONNECTED) => s,
 
             // If we've got a blocked thread, then use an atomic to gain ownership
             // of it (may fail)
-            ptr => self.state.compare_and_swap(ptr, EMPTY, Ordering::SeqCst),
+            ptr => self
+                .state
+                .compare_exchange(ptr, EMPTY, Ordering::SeqCst, Ordering::SeqCst)
+                .unwrap_or_else(|x| x),
         };
 
         // Now that we've got ownership of our state, figure out what to do
diff --git a/sgx_tstd/src/sync/mpsc/shared.rs b/sgx_tstd/src/sync/mpsc/shared.rs
index ee18611..30e8cbf 100644
--- a/sgx_tstd/src/sync/mpsc/shared.rs
+++ b/sgx_tstd/src/sync/mpsc/shared.rs
@@ -24,25 +24,25 @@
 /// High level implementation details can be found in the comment of the parent
 /// module. You'll also note that the implementation of the shared and stream
 /// channels are quite similar, and this is no coincidence!
-
 pub use self::Failure::*;
 use self::StartResult::*;
 
 use core::cmp;
-use core::intrinsics::abort;
-use core::cell::UnsafeCell;
-use core::ptr;
-use core::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
+
+use crate::cell::UnsafeCell;
+use crate::ptr;
+use crate::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
 use crate::sync::mpsc::blocking::{self, SignalToken};
 use crate::sync::mpsc::mpsc_queue as mpsc;
-use crate::sync::{SgxMutex, SgxMutexGuard};
+use crate::sync::{SgxMutex as Mutex, SgxMutexGuard as MutexGuard};
 use crate::thread;
 use crate::time::Instant;
 
+use sgx_trts::trts;
+
 const DISCONNECTED: isize = isize::MIN;
 const FUDGE: isize = 1024;
 const MAX_REFCOUNT: usize = (isize::MAX) as usize;
-
 const MAX_STEALS: isize = 1 << 20;
 
 pub struct Packet<T> {
@@ -61,7 +61,7 @@
 
     // this lock protects various portions of this implementation during
     // select()
-    select_lock: SgxMutex<()>,
+    select_lock: Mutex<()>,
 }
 
 pub enum Failure {
@@ -87,7 +87,7 @@
             channels: AtomicUsize::new(2),
             port_dropped: AtomicBool::new(false),
             sender_drain: AtomicIsize::new(0),
-            select_lock: SgxMutex::new(()),
+            select_lock: Mutex::new(()),
         }
     }
 
@@ -96,7 +96,7 @@
     // In other case mutex data will be duplicated while cloning
     // and that could cause problems on platforms where it is
     // represented by opaque data structure
-    pub fn postinit_lock(&self) -> SgxMutexGuard<'_, ()> {
+    pub fn postinit_lock(&self) -> MutexGuard<'_, ()> {
         self.select_lock.lock().unwrap()
     }
 
@@ -105,8 +105,8 @@
     // threads in select().
     //
     // This can only be called at channel-creation time
-    pub fn inherit_blocker(&self, token: Option<SignalToken>, guard: SgxMutexGuard<'_, ()>) {
-        token.map(|token| {
+    pub fn inherit_blocker(&self, token: Option<SignalToken>, guard: MutexGuard<'_, ()>) {
+        if let Some(token) = token {
             assert_eq!(self.cnt.load(Ordering::SeqCst), 0);
             assert_eq!(self.to_wake.load(Ordering::SeqCst), 0);
             self.to_wake.store(unsafe { token.cast_to_usize() }, Ordering::SeqCst);
@@ -133,7 +133,7 @@
             unsafe {
                 *self.steals.get() = -1;
             }
-        });
+        }
 
         // When the shared packet is constructed, we grabbed this lock. The
         // purpose of this lock is to ensure that abort_selection() doesn't
@@ -369,7 +369,7 @@
 
         // See comments on Arc::clone() on why we do this (for `mem::forget`).
         if old_count > MAX_REFCOUNT {
-            abort();
+            trts::rsgx_abort();
         }
     }
 
@@ -396,12 +396,18 @@
 
     // See the long discussion inside of stream.rs for why the queue is drained,
     // and why it is done in this fashion.
+    #[allow(clippy::while_let_loop)]
     pub fn drop_port(&self) {
         self.port_dropped.store(true, Ordering::SeqCst);
         let mut steals = unsafe { *self.steals.get() };
-        while {
-            let cnt = self.cnt.compare_and_swap(steals, DISCONNECTED, Ordering::SeqCst);
-            cnt != DISCONNECTED && cnt != steals
+        while match self.cnt.compare_exchange(
+            steals,
+            DISCONNECTED,
+            Ordering::SeqCst,
+            Ordering::SeqCst,
+        ) {
+            Ok(_) => false,
+            Err(old) => old != DISCONNECTED,
         } {
             // See the discussion in 'try_recv' for why we yield
             // control of this thread.
diff --git a/sgx_tstd/src/sync/mpsc/spsc_queue.rs b/sgx_tstd/src/sync/mpsc/spsc_queue.rs
index 94abb90..2766c51 100644
--- a/sgx_tstd/src/sync/mpsc/spsc_queue.rs
+++ b/sgx_tstd/src/sync/mpsc/spsc_queue.rs
@@ -21,12 +21,13 @@
 //! concurrently between two threads. This data structure is safe to use and
 //! enforces the semantics that there is one pusher and one popper.
 
-// http://www.1024cores.net/home/lock-free-algorithms/queues/unbounded-spsc-queue
+// https://www.1024cores.net/home/lock-free-algorithms/queues/unbounded-spsc-queue
 
-use core::ptr;
 use core::cell::UnsafeCell;
-use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
-use alloc_crate::boxed::Box;
+use core::ptr;
+
+use crate::boxed::Box;
+use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
 
 use super::cache_aligned::CacheAligned;
 
@@ -247,4 +248,3 @@
         }
     }
 }
-
diff --git a/sgx_tstd/src/sync/mpsc/stream.rs b/sgx_tstd/src/sync/mpsc/stream.rs
index c7afda1..2666072 100644
--- a/sgx_tstd/src/sync/mpsc/stream.rs
+++ b/sgx_tstd/src/sync/mpsc/stream.rs
@@ -23,18 +23,18 @@
 ///
 /// High level implementation details can be found in the comment of the parent
 /// module.
-
 pub use self::Failure::*;
 use self::Message::*;
 pub use self::UpgradeResult::*;
 
 use core::cmp;
-use core::cell::UnsafeCell;
-use core::ptr;
-use core::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
 
+use crate::cell::UnsafeCell;
+use crate::ptr;
 use crate::thread;
 use crate::time::Instant;
+
+use crate::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
 use crate::sync::mpsc::blocking::{self, SignalToken};
 use crate::sync::mpsc::spsc_queue as spsc;
 use crate::sync::mpsc::Receiver;
@@ -64,6 +64,7 @@
     Upgraded(Receiver<T>),
 }
 
+#[allow(clippy::enum_variant_names)]
 pub enum UpgradeResult {
     UpSuccess,
     UpDisconnected,
@@ -219,7 +220,7 @@
             // Messages which actually popped from the queue shouldn't count as
             // a steal, so offset the decrement here (we already have our
             // "steal" factored into the channel count above).
-            data @ Ok(..) | data @ Err(Upgraded(..)) => unsafe {
+            data @ (Ok(..) | Err(Upgraded(..))) => unsafe {
                 *self.queue.consumer_addition().steals.get() -= 1;
                 data
             },
@@ -335,22 +336,23 @@
         // data, but eventually we're guaranteed to break out of this loop
         // (because there is a bounded number of senders).
         let mut steals = unsafe { *self.queue.consumer_addition().steals.get() };
-        while {
-            let cnt = self.queue.producer_addition().cnt.compare_and_swap(
-                steals,
-                DISCONNECTED,
-                Ordering::SeqCst,
-            );
-            cnt != DISCONNECTED && cnt != steals
+        while match self.queue.producer_addition().cnt.compare_exchange(
+            steals,
+            DISCONNECTED,
+            Ordering::SeqCst,
+            Ordering::SeqCst,
+        ) {
+                Ok(_) => false,
+                Err(old) => old != DISCONNECTED,
         } {
-            while let Some(_) = self.queue.pop() {
+            while self.queue.pop().is_some() {
                 steals += 1;
             }
         }
 
         // At this point in time, we have gated all future senders from sending,
         // and we have flagged the channel as being disconnected. The senders
-        // still have some responsibility, however, because some sends may not
+        // still have some responsibility, however, because some sends might not
         // complete until after we flag the disconnection. There are more
         // details in the sending methods that see DISCONNECTED
     }
@@ -381,7 +383,7 @@
         // at all.
         //
         // Hence, because of these invariants, we immediately return `Ok(true)`.
-        // Note that the data may not actually be sent on the channel just yet.
+        // Note that the data might not actually be sent on the channel just yet.
         // The other end could have flagged the upgrade but not sent data to
         // this end. This is fine because we know it's a small bounded windows
         // of time until the data is actually sent.
diff --git a/sgx_tstd/src/sync/mpsc/sync.rs b/sgx_tstd/src/sync/mpsc/sync.rs
index 31fd4da..88ccfca 100644
--- a/sgx_tstd/src/sync/mpsc/sync.rs
+++ b/sgx_tstd/src/sync/mpsc/sync.rs
@@ -39,19 +39,19 @@
 /// implementation shares almost all code for the buffered and unbuffered cases
 /// of a synchronous channel. There are a few branches for the unbuffered case,
 /// but they're mostly just relevant to blocking senders.
-
 pub use self::Failure::*;
 use self::Blocker::*;
 
-use core::intrinsics::abort;
 use core::mem;
 use core::ptr;
-use core::sync::atomic::{AtomicUsize, Ordering};
 
+use crate::sync::atomic::{AtomicUsize, Ordering};
 use crate::sync::mpsc::blocking::{self, SignalToken, WaitToken};
-use crate::sync::{SgxMutex, SgxMutexGuard};
+use crate::sync::{SgxMutex as Mutex, SgxMutexGuard as MutexGuard};
 use crate::time::Instant;
 
+use sgx_trts::trts;
+
 const MAX_REFCOUNT: usize = (isize::MAX) as usize;
 
 pub struct Packet<T> {
@@ -59,7 +59,7 @@
     /// the other shared channel already had the code implemented
     channels: AtomicUsize,
 
-    lock: SgxMutex<State<T>>,
+    lock: Mutex<State<T>>,
 }
 
 unsafe impl<T: Send> Send for Packet<T> {}
@@ -121,10 +121,10 @@
 /// Atomically blocks the current thread, placing it into `slot`, unlocking `lock`
 /// in the meantime. This re-locks the mutex upon returning.
 fn wait<'a, 'b, T>(
-    lock: &'a SgxMutex<State<T>>,
-    mut guard: SgxMutexGuard<'b, State<T>>,
+    lock: &'a Mutex<State<T>>,
+    mut guard: MutexGuard<'b, State<T>>,
     f: fn(SignalToken) -> Blocker,
-) -> SgxMutexGuard<'a, State<T>> {
+) -> MutexGuard<'a, State<T>> {
     let (wait_token, signal_token) = blocking::tokens();
     match mem::replace(&mut guard.blocker, f(signal_token)) {
         NoneBlocked => {}
@@ -137,11 +137,11 @@
 
 /// Same as wait, but waiting at most until `deadline`.
 fn wait_timeout_receiver<'a, 'b, T>(
-    lock: &'a SgxMutex<State<T>>,
+    lock: &'a Mutex<State<T>>,
     deadline: Instant,
-    mut guard: SgxMutexGuard<'b, State<T>>,
+    mut guard: MutexGuard<'b, State<T>>,
     success: &mut bool,
-) -> SgxMutexGuard<'a, State<T>> {
+) -> MutexGuard<'a, State<T>> {
     let (wait_token, signal_token) = blocking::tokens();
     match mem::replace(&mut guard.blocker, BlockedReceiver(signal_token)) {
         NoneBlocked => {}
@@ -156,7 +156,7 @@
     new_guard
 }
 
-fn abort_selection<T>(guard: &mut SgxMutexGuard<'_, State<T>>) -> bool {
+fn abort_selection<T>(guard: &mut MutexGuard<'_, State<T>>) -> bool {
     match mem::replace(&mut guard.blocker, NoneBlocked) {
         NoneBlocked => true,
         BlockedSender(token) => {
@@ -171,7 +171,7 @@
 }
 
 /// Wakes up a thread, dropping the lock at the correct time
-fn wakeup<T>(token: SignalToken, guard: SgxMutexGuard<'_, State<T>>) {
+fn wakeup<T>(token: SignalToken, guard: MutexGuard<'_, State<T>>) {
     // We need to be careful to wake up the waiting thread *outside* of the mutex
     // in case it incurs a context switch.
     drop(guard);
@@ -182,7 +182,7 @@
     pub fn new(capacity: usize) -> Packet<T> {
         Packet {
             channels: AtomicUsize::new(1),
-            lock: SgxMutex::new(State {
+            lock: Mutex::new(State {
                 disconnected: false,
                 blocker: NoneBlocked,
                 cap: capacity,
@@ -199,7 +199,7 @@
 
     // wait until a send slot is available, returning locked access to
     // the channel state.
-    fn acquire_send_slot(&self) -> SgxMutexGuard<'_, State<T>> {
+    fn acquire_send_slot(&self) -> MutexGuard<'_, State<T>> {
         let mut node = Node { token: None, next: ptr::null_mut() };
         loop {
             let mut guard = self.lock.lock().unwrap();
@@ -340,7 +340,7 @@
     // * `waited` - flag if the receiver blocked to receive some data, or if it
     //              just picked up some data on the way out
     // * `guard` - the lock guard that is held over this channel's lock
-    fn wakeup_senders(&self, waited: bool, mut guard: SgxMutexGuard<'_, State<T>>) {
+    fn wakeup_senders(&self, waited: bool, mut guard: MutexGuard<'_, State<T>>) {
         let pending_sender1: Option<SignalToken> = guard.queue.dequeue();
 
         // If this is a no-buffer channel (cap == 0), then if we didn't wait we
@@ -361,8 +361,12 @@
         mem::drop(guard);
 
         // only outside of the lock do we wake up the pending threads
-        pending_sender1.map(|t| t.signal());
-        pending_sender2.map(|t| t.signal());
+        if let Some(token) = pending_sender1 {
+            token.signal();
+        }
+        if let Some(token) = pending_sender2 {
+            token.signal();
+        }
     }
 
     // Prepares this shared packet for a channel clone, essentially just bumping
@@ -372,7 +376,7 @@
 
         // See comments on Arc::clone() on why we do this (for `mem::forget`).
         if old_count > MAX_REFCOUNT {
-            abort();
+            trts::rsgx_abort();
         }
     }
 
@@ -426,7 +430,9 @@
         while let Some(token) = queue.dequeue() {
             token.signal();
         }
-        waiter.map(|t| t.signal());
+        if let Some(token) = waiter {
+            token.signal();
+        }
     }
 }
 
diff --git a/sgx_tstd/src/sync/mutex.rs b/sgx_tstd/src/sync/mutex.rs
index dec2139..ff2257b 100644
--- a/sgx_tstd/src/sync/mutex.rs
+++ b/sgx_tstd/src/sync/mutex.rs
@@ -26,194 +26,14 @@
 //! The table below illustrates the primitives that the Intel(R) SGX Thread
 //! Synchronization library supports, as well as the OCALLs that each API function needs.
 //!
-use sgx_types::SysError;
-use core::cell::UnsafeCell;
-use core::mem;
-use core::ptr;
-use core::fmt;
-use core::ops::{Deref, DerefMut};
-use alloc_crate::boxed::Box;
-use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
-use crate::sys::mutex as imp;
 
-/// The structure of sgx mutex.
-pub struct SgxThreadMutex(imp::SgxThreadMutex);
+use crate::cell::UnsafeCell;
+use crate::fmt;
+use crate::ops::{Deref, DerefMut};
+use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
+use crate::sys_common::mutex as sys;
 
-unsafe impl Send for SgxThreadMutex {}
-unsafe impl Sync for SgxThreadMutex {}
-
-impl SgxThreadMutex {
-    ///
-    /// The function initializes a trusted mutex object within the enclave.
-    ///
-    /// # Description
-    ///
-    /// When a thread creates a mutex within an enclave, sgx_thread_mutex_
-    /// init simply initializes the various fields of the mutex object to indicate that
-    /// the mutex is available. rsgx_thread_mutex_init creates a non-recursive
-    /// mutex. The results of using a mutex in a lock or unlock operation before it has
-    /// been fully initialized (for example, the function call to rsgx_thread_mutex_
-    /// init returns) are undefined. To avoid race conditions in the initialization of a
-    /// trusted mutex, it is recommended statically initializing the mutex with the
-    /// macro SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_NON_RECURSIVE_MUTEX_INITIALIZER ,
-    /// of, or SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER instead.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Return value
-    ///
-    /// The trusted mutex object to be initialized.
-    ///
-    pub const fn new() -> SgxThreadMutex {
-        SgxThreadMutex(imp::SgxThreadMutex::new(imp::SgxThreadMutexControl::SGX_THREAD_MUTEX_NONRECURSIVE))
-    }
-
-    ///
-    /// The function locks a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// To acquire a mutex, a thread first needs to acquire the corresponding spin
-    /// lock. After the spin lock is acquired, the thread checks whether the mutex is
-    /// available. If the queue is empty or the thread is at the head of the queue the
-    /// thread will now become the owner of the mutex. To confirm its ownership, the
-    /// thread updates the refcount and owner fields. If the mutex is not available, the
-    /// thread searches the queue. If the thread is already in the queue, but not at the
-    /// head, it means that the thread has previously tried to lock the mutex, but it
-    /// did not succeed and had to wait outside the enclave and it has been
-    /// awakened unexpectedly. When this happens, the thread makes an OCALL and
-    /// simply goes back to sleep. If the thread is trying to lock the mutex for the first
-    /// time, it will update the waiting queue and make an OCALL to get suspended.
-    /// Note that threads release the spin lock after acquiring the mutex or before
-    /// leaving the enclave.
-    ///
-    /// **Note**
-    ///
-    /// A thread should not exit an enclave returning from a root ECALL after acquiring
-    /// the ownership of a mutex. Do not split the critical section protected by a
-    /// mutex across root ECALLs.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    #[inline]
-    pub unsafe fn lock(&self) -> SysError {
-        self.0.lock()
-    }
-
-    ///
-    /// The function tries to lock a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// A thread may check the status of the mutex, which implies acquiring the spin
-    /// lock and verifying that the mutex is available and that the queue is empty or
-    /// the thread is at the head of the queue. When this happens, the thread
-    /// acquires the mutex, releases the spin lock and returns 0. Otherwise, the
-    /// thread releases the spin lock and returns EINVAL/EBUSY. The thread is not suspended
-    /// in this case.
-    ///
-    /// **Note**
-    ///
-    /// A thread should not exit an enclave returning from a root ECALL after acquiring
-    /// the ownership of a mutex. Do not split the critical section protected by a
-    /// mutex across root ECALLs.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    /// **EBUSY**
-    ///
-    /// The mutex is locked by another thread or has pending threads to acquire the mutex
-    ///
-    #[inline]
-    pub unsafe fn try_lock(&self) -> SysError {
-        self.0.try_lock()
-    }
-
-    ///
-    /// The function unlocks a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// Before a thread releases a mutex, it has to verify it is the owner of the mutex. If
-    /// that is the case, the thread decreases the refcount by 1 and then may either
-    /// continue normal execution or wakeup the first thread in the queue. Note that
-    /// to ensure the state of the mutex remains consistent, the thread that is
-    /// awakened by the thread releasing the mutex will then try to acquire the
-    /// mutex almost as in the initial call to the rsgx_thread_mutex_lock routine.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid or it is not locked by any thread.
-    ///
-    /// **EPERM**
-    ///
-    /// The mutex is locked by another thread.
-    ///
-    #[inline]
-    pub unsafe fn unlock(&self) -> SysError {
-        self.0.unlock()
-    }
-
-    ///
-    /// The function destroys a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// rsgx_thread_mutex_destroy resets the mutex, which brings it to its initial
-    /// status. In this process, certain fields are checked to prevent releasing a mutex
-    /// that is still owned by a thread or on which threads are still waiting.
-    ///
-    /// **Note**
-    ///
-    /// Locking or unlocking a mutex after it has been destroyed results in undefined
-    /// behavior. After a mutex is destroyed, it must be re-created before it can be
-    /// used again.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    /// **EBUSY**
-    ///
-    /// The mutex is locked by another thread or has pending threads to acquire the mutex.
-    ///
-    #[inline]
-    pub unsafe fn destroy(&self) -> SysError {
-        self.0.destroy()
-    }
-}
-
-pub fn raw(mutex: &SgxThreadMutex) -> &imp::SgxThreadMutex { &mutex.0 }
+pub use crate::sys_common::mutex::SgxThreadMutex;
 
 /// A mutual exclusion primitive useful for protecting shared data
 ///
@@ -242,15 +62,136 @@
 /// the guard that would have otherwise been returned on a successful lock. This
 /// allows access to the data, despite the lock being poisoned.
 ///
-/// [`new`]: #method.new
-/// [`lock`]: #method.lock
-/// [`try_lock`]: #method.try_lock
-/// [`Result`]: ../../std/result/enum.Result.html
-/// [`unwrap()`]: ../../std/result/enum.Result.html#method.unwrap
-/// [`PoisonError`]: ../../std/sync/struct.PoisonError.html
-/// [`into_inner`]: ../../std/sync/struct.PoisonError.html#method.into_inner
+/// [`new`]: Self::new
+/// [`lock`]: Self::lock
+/// [`try_lock`]: Self::try_lock
+/// [`unwrap()`]: Result::unwrap
+/// [`PoisonError`]: super::PoisonError
+/// [`into_inner`]: super::PoisonError::into_inner
+///
+/// # Examples
+///
+/// ```
+/// use std::sync::{Arc, SgxMutex as Mutex};
+/// use std::thread;
+/// use std::sync::mpsc::channel;
+///
+/// const N: usize = 10;
+///
+/// // Spawn a few threads to increment a shared variable (non-atomically), and
+/// // let the main thread know once all increments are done.
+/// //
+/// // Here we're using an Arc to share memory among threads, and the data inside
+/// // the Arc is protected with a mutex.
+/// let data = Arc::new(Mutex::new(0));
+///
+/// let (tx, rx) = channel();
+/// for _ in 0..N {
+///     let (data, tx) = (Arc::clone(&data), tx.clone());
+///     thread::spawn(move || {
+///         // The shared state can only be accessed once the lock is held.
+///         // Our non-atomic increment is safe because we're the only thread
+///         // which can access the shared state when the lock is held.
+///         //
+///         // We unwrap() the return value to assert that we are not expecting
+///         // threads to ever fail while holding the lock.
+///         let mut data = data.lock().unwrap();
+///         *data += 1;
+///         if *data == N {
+///             tx.send(()).unwrap();
+///         }
+///         // the lock is unlocked here when `data` goes out of scope.
+///     });
+/// }
+///
+/// rx.recv().unwrap();
+/// ```
+///
+/// To recover from a poisoned mutex:
+///
+/// ```
+/// use std::sync::{Arc, Mutex};
+/// use std::thread;
+///
+/// let lock = Arc::new(Mutex::new(0_u32));
+/// let lock2 = Arc::clone(&lock);
+///
+/// let _ = thread::spawn(move || -> () {
+///     // This thread will acquire the mutex first, unwrapping the result of
+///     // `lock` because the lock has not been poisoned.
+///     let _guard = lock2.lock().unwrap();
+///
+///     // This panic while holding the lock (`_guard` is in scope) will poison
+///     // the mutex.
+///     panic!();
+/// }).join();
+///
+/// // The lock is poisoned by this point, but the returned result can be
+/// // pattern matched on to return the underlying guard on both branches.
+/// let mut guard = match lock.lock() {
+///     Ok(guard) => guard,
+///     Err(poisoned) => poisoned.into_inner(),
+/// };
+///
+/// *guard += 1;
+/// ```
+///
+/// It is sometimes necessary to manually drop the mutex guard to unlock it
+/// sooner than the end of the enclosing scope.
+///
+/// ```
+/// use std::sync::{Arc, Mutex};
+/// use std::thread;
+///
+/// const N: usize = 3;
+///
+/// let data_mutex = Arc::new(Mutex::new(vec![1, 2, 3, 4]));
+/// let res_mutex = Arc::new(Mutex::new(0));
+///
+/// let mut threads = Vec::with_capacity(N);
+/// (0..N).for_each(|_| {
+///     let data_mutex_clone = Arc::clone(&data_mutex);
+///     let res_mutex_clone = Arc::clone(&res_mutex);
+///
+///     threads.push(thread::spawn(move || {
+///         let mut data = data_mutex_clone.lock().unwrap();
+///         // This is the result of some important and long-ish work.
+///         let result = data.iter().fold(0, |acc, x| acc + x * 2);
+///         data.push(result);
+///         drop(data);
+///         *res_mutex_clone.lock().unwrap() += result;
+///     }));
+/// });
+///
+/// let mut data = data_mutex.lock().unwrap();
+/// // This is the result of some important and long-ish work.
+/// let result = data.iter().fold(0, |acc, x| acc + x * 2);
+/// data.push(result);
+/// // We drop the `data` explicitly because it's not necessary anymore and the
+/// // thread still has work to do. This allow other threads to start working on
+/// // the data immediately, without waiting for the rest of the unrelated work
+/// // to be done here.
+/// //
+/// // It's even more important here than in the threads because we `.join` the
+/// // threads after that. If we had not dropped the mutex guard, a thread could
+/// // be waiting forever for it, causing a deadlock.
+/// drop(data);
+/// // Here the mutex guard is not assigned to a variable and so, even if the
+/// // scope does not end after this line, the mutex is still released: there is
+/// // no deadlock.
+/// *res_mutex.lock().unwrap() += result;
+///
+/// threads.into_iter().for_each(|thread| {
+///     thread
+///         .join()
+///         .expect("The thread creating or execution failed !")
+/// });
+///
+/// assert_eq!(*res_mutex.lock().unwrap(), 800);
+/// ```
+#[cfg_attr(not(test), rustc_diagnostic_item = "mutex_type")]
 pub struct SgxMutex<T: ?Sized> {
-    inner: Box<SgxThreadMutex>,
+    inner: sys::SgxMovableThreadMutex,
     poison: poison::Flag,
     data: UnsafeCell<T>,
 }
@@ -260,7 +201,6 @@
 unsafe impl<T: ?Sized + Send> Send for SgxMutex<T> {}
 unsafe impl<T: ?Sized + Send> Sync for SgxMutex<T> {}
 
-///
 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
 /// dropped (falls out of scope), the lock will be unlocked.
 ///
@@ -270,11 +210,9 @@
 /// This structure is created by the [`lock`] and [`try_lock`] methods on
 /// [`Mutex`].
 ///
-/// [`Deref`]: ../../std/ops/trait.Deref.html
-/// [`DerefMut`]: ../../std/ops/trait.DerefMut.html
-/// [`lock`]: struct.Mutex.html#method.lock
-/// [`try_lock`]: struct.Mutex.html#method.try_lock
-/// [`Mutex`]: struct.Mutex.html
+/// [`lock`]: Mutex::lock
+/// [`try_lock`]: Mutex::try_lock
+#[must_use = "if unused the Mutex will immediately unlock"]
 pub struct SgxMutexGuard<'a, T: ?Sized + 'a> {
     lock: &'a SgxMutex<T>,
     poison: poison::Guard,
@@ -284,12 +222,18 @@
 unsafe impl<T: ?Sized + Sync> Sync for SgxMutexGuard<'_, T> {}
 
 impl<T> SgxMutex<T> {
-    ///
     /// Creates a new mutex in an unlocked state ready for use.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxMutex;
+    ///
+    /// let mutex = SgxMutex::new(0);
+    /// ```
     pub fn new(t: T) -> SgxMutex<T> {
-        SgxMutex{
-            inner: Box::new(SgxThreadMutex::new()),
+        SgxMutex {
+            inner: sys::SgxMovableThreadMutex::new(),
             poison: poison::Flag::new(),
             data: UnsafeCell::new(t),
         }
@@ -320,9 +264,24 @@
     ///
     /// This function might panic when called if the lock is already held by
     /// the current thread.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex};
+    /// use std::thread;
+    ///
+    /// let mutex = Arc::new(Mutex::new(0));
+    /// let c_mutex = Arc::clone(&mutex);
+    ///
+    /// thread::spawn(move || {
+    ///     *c_mutex.lock().unwrap() = 10;
+    /// }).join().expect("thread::spawn failed");
+    /// assert_eq!(*mutex.lock().unwrap(), 10);
+    /// ```
     pub fn lock(&self) -> LockResult<SgxMutexGuard<'_, T>> {
         unsafe {
-            self.inner.lock();
+            self.inner.raw_lock();
             SgxMutexGuard::new(self)
         }
     }
@@ -341,10 +300,34 @@
     /// # Errors
     ///
     /// If another user of this mutex panicked while holding the mutex, then
-    /// this call will return failure if the mutex would otherwise be
-    /// acquired.
+    /// this call will return the [`Poisoned`] error if the mutex would
+    /// otherwise be acquired.
     ///
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
+    /// If the mutex could not be acquired because it is already locked, then
+    /// this call will return the [`WouldBlock`] error.
+    ///
+    /// [`Poisoned`]: TryLockError::Poisoned
+    /// [`WouldBlock`]: TryLockError::WouldBlock
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex};
+    /// use std::thread;
+    ///
+    /// let mutex = Arc::new(Mutex::new(0));
+    /// let c_mutex = Arc::clone(&mutex);
+    ///
+    /// thread::spawn(move || {
+    ///     let mut lock = c_mutex.try_lock();
+    ///     if let Ok(ref mut mutex) = lock {
+    ///         **mutex = 10;
+    ///     } else {
+    ///         println!("try_lock failed");
+    ///     }
+    /// }).join().expect("thread::spawn failed");
+    /// assert_eq!(*mutex.lock().unwrap(), 10);
+    /// ```
     pub fn try_lock(&self) -> TryLockResult<SgxMutexGuard<'_, T>> {
         unsafe {
             match self.inner.try_lock() {
@@ -354,11 +337,46 @@
         }
     }
 
+    /// Immediately drops the guard, and consequently unlocks the mutex.
+    ///
+    /// This function is equivalent to calling [`drop`] on the guard but is more self-documenting.
+    /// Alternately, the guard will be automatically dropped when it goes out of scope.
+    ///
+    /// ```
+    /// #![feature(mutex_unlock)]
+    ///
+    /// use std::sync::SgxMutex as Mutex;
+    /// let mutex = Mutex::new(0);
+    ///
+    /// let mut guard = mutex.lock().unwrap();
+    /// *guard += 20;
+    /// Mutex::unlock(guard);
+    /// ```
+    pub fn unlock(guard: SgxMutexGuard<'_, T>) {
+        drop(guard);
+    }
+
     /// Determines whether the mutex is poisoned.
     ///
     /// If another thread is active, the mutex can still become poisoned at any
     /// time. You should not trust a `false` value for program correctness
     /// without additional synchronization.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxMutex as Mutex};
+    /// use std::thread;
+    ///
+    /// let mutex = Arc::new(Mutex::new(0));
+    /// let c_mutex = Arc::clone(&mutex);
+    ///
+    /// let _ = thread::spawn(move || {
+    ///     let _lock = c_mutex.lock().unwrap();
+    ///     panic!(); // the mutex gets poisoned
+    /// }).join();
+    /// assert_eq!(mutex.is_poisoned(), true);
+    /// ```
     #[inline]
     pub fn is_poisoned(&self) -> bool {
         self.poison.get()
@@ -370,21 +388,21 @@
     ///
     /// If another user of this mutex panicked while holding the mutex, then
     /// this call will return an error instead.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxMutex;
+    ///
+    /// let mutex = SgxMutex::new(0);
+    /// assert_eq!(mutex.into_inner().unwrap(), 0);
+    /// ```
     pub fn into_inner(self) -> LockResult<T>
     where
         T: Sized,
     {
-        unsafe {
-            let (inner, poison, data) = {
-                let SgxMutex {ref inner, ref poison, ref data } = self;
-                (ptr::read(inner), ptr::read(poison), ptr::read(data))
-            };
-            mem::forget(self);
-            inner.destroy();
-            drop(inner);
-
-            poison::map_result(poison.borrow(), |_| data.into_inner())
-        }
+        let data = self.data.into_inner();
+        poison::map_result(self.poison.borrow(), |_| data)
     }
 
     /// Returns a mutable reference to the underlying data.
@@ -397,25 +415,24 @@
     /// If another user of this mutex panicked while holding the mutex, then
     /// this call will return an error instead.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxMutex;
+    ///
+    /// let mut mutex = SgxMutex::new(0);
+    /// *mutex.get_mut().unwrap() = 10;
+    /// assert_eq!(*mutex.lock().unwrap(), 10);
+    /// ```
     pub fn get_mut(&mut self) -> LockResult<&mut T> {
-        let data = unsafe { &mut *self.data.get() };
+        let data = self.data.get_mut();
         poison::map_result(self.poison.borrow(), |_| data)
     }
 }
 
-unsafe impl<#[may_dangle] T: ?Sized> Drop for SgxMutex<T> {
-    fn drop(&mut self) {
-        // IMPORTANT: This code must be kept in sync with `SgxMutex::into_inner`.
-        let result = unsafe { self.inner.destroy() };
-        debug_assert_eq!(result, Ok(()), "Error when destroy an SgxMutex: {}", result.unwrap_err());
-    }
-}
-
 impl<T> From<T> for SgxMutex<T> {
     /// Creates a new mutex in an unlocked state ready for use.
     /// This is equivalent to [`Mutex::new`].
-    ///
-    /// [`Mutex::new`]: ../../std/sync/struct.Mutex.html#method.new
     fn from(t: T) -> Self {
         SgxMutex::new(t)
     }
@@ -430,11 +447,14 @@
 
 impl<T: ?Sized + fmt::Debug> fmt::Debug for SgxMutex<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut d = f.debug_struct("SgxMutex");
         match self.try_lock() {
-            Ok(guard) => f.debug_struct("SgxMutex").field("data", &&*guard).finish(),
+            Ok(guard) => {
+                d.field("data", &&*guard);
+            }
             Err(TryLockError::Poisoned(err)) => {
-                f.debug_struct("SgxMutex").field("data", &&**err.get_ref()).finish()
-            },
+                d.field("data", &&**err.get_ref());
+            }
             Err(TryLockError::WouldBlock) => {
                 struct LockedPlaceholder;
                 impl fmt::Debug for LockedPlaceholder {
@@ -442,10 +462,11 @@
                         f.write_str("<locked>")
                     }
                 }
-
-                f.debug_struct("SgxMutex").field("data", &LockedPlaceholder).finish()
+                d.field("data", &LockedPlaceholder);
             }
         }
+        d.field("poisoned", &self.poison.get());
+        d.finish_non_exhaustive()
     }
 }
 
@@ -474,7 +495,7 @@
     fn drop(&mut self) {
         let result = unsafe {
             self.lock.poison.done(&self.poison);
-            self.lock.inner.unlock()
+            self.lock.inner.raw_unlock()
         };
         debug_assert_eq!(result, Ok(()), "Error when unlocking an SgxMutex: {}", result.unwrap_err());
     }
@@ -492,7 +513,7 @@
     }
 }
 
-pub fn guard_lock<'a, T: ?Sized>(guard: &SgxMutexGuard<'a, T>) -> &'a SgxThreadMutex {
+pub fn guard_lock<'a, T: ?Sized>(guard: &SgxMutexGuard<'a, T>) -> &'a sys::SgxMovableThreadMutex {
     &guard.lock.inner
 }
 
diff --git a/sgx_tstd/src/sync/once.rs b/sgx_tstd/src/sync/once.rs
index 21201de..6d5209d 100644
--- a/sgx_tstd/src/sync/once.rs
+++ b/sgx_tstd/src/sync/once.rs
@@ -68,22 +68,62 @@
 //
 // You'll find a few more details in the implementation, but that's the gist of
 // it!
+//
+// Atomic orderings:
+// When running `Once` we deal with multiple atomics:
+// `Once.state_and_queue` and an unknown number of `Waiter.signaled`.
+// * `state_and_queue` is used (1) as a state flag, (2) for synchronizing the
+//   result of the `Once`, and (3) for synchronizing `Waiter` nodes.
+//     - At the end of the `call_inner` function we have to make sure the result
+//       of the `Once` is acquired. So every load which can be the only one to
+//       load COMPLETED must have at least Acquire ordering, which means all
+//       three of them.
+//     - `WaiterQueue::Drop` is the only place that may store COMPLETED, and
+//       must do so with Release ordering to make the result available.
+//     - `wait` inserts `Waiter` nodes as a pointer in `state_and_queue`, and
+//       needs to make the nodes available with Release ordering. The load in
+//       its `compare_exchange` can be Relaxed because it only has to compare
+//       the atomic, not to read other data.
+//     - `WaiterQueue::Drop` must see the `Waiter` nodes, so it must load
+//       `state_and_queue` with Acquire ordering.
+//     - There is just one store where `state_and_queue` is used only as a
+//       state flag, without having to synchronize data: switching the state
+//       from INCOMPLETE to RUNNING in `call_inner`. This store can be Relaxed,
+//       but the read has to be Acquire because of the requirements mentioned
+//       above.
+// * `Waiter.signaled` is both used as a flag, and to protect a field with
+//   interior mutability in `Waiter`. `Waiter.thread` is changed in
+//   `WaiterQueue::Drop` which then sets `signaled` with Release ordering.
+//   After `wait` loads `signaled` with Acquire and sees it is true, it needs to
+//   see the changes to drop the `Waiter` struct correctly.
+// * There is one place where the two atomics `Once.state_and_queue` and
+//   `Waiter.signaled` come together, and might be reordered by the compiler or
+//   processor. Because both use Acquire ordering such a reordering is not
+//   allowed, so no need for SeqCst.
 
-use core::cell::Cell;
-use core::fmt;
-use core::marker;
+use crate::cell::Cell;
+use crate::fmt;
+use crate::marker;
 use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
-use crate::thread::{self, SgxThread};
+use crate::thread::{self, SgxThread as Thread};
 
 /// A synchronization primitive which can be used to run a one-time global
 /// initialization. Useful for one-time initialization for FFI or related
-/// functionality. This type can only be constructed with the [`Once::new`]
-/// constructor.
+/// functionality. This type can only be constructed with [`Once::new()`].
 ///
-/// [`Once::new`]: struct.Once.html#method.new
+/// # Examples
 ///
+/// ```
+/// use std::sync::Once;
+///
+/// static START: Once = Once::new();
+///
+/// START.call_once(|| {
+///     // run initialization here
+/// });
+/// ```
 pub struct Once {
-    // `state_and_queue` is actually an a pointer to a `Waiter` with extra state
+    // `state_and_queue` is actually a pointer to a `Waiter` with extra state
     // bits, so we add the `PhantomData` appropriately.
     state_and_queue: AtomicUsize,
     _marker: marker::PhantomData<*const Waiter>,
@@ -94,11 +134,8 @@
 unsafe impl Sync for Once {}
 unsafe impl Send for Once {}
 
-/// State yielded to [`call_once_force`]’s closure parameter. The state can be
-/// used to query the poison status of the [`Once`].
-///
-/// [`call_once_force`]: struct.Once.html#method.call_once_force
-/// [`Once`]: struct.Once.html
+/// State yielded to [`Once::call_once_force()`]’s closure parameter. The state
+/// can be used to query the poison status of the [`Once`].
 #[derive(Debug)]
 pub struct OnceState {
     poisoned: bool,
@@ -107,8 +144,13 @@
 
 /// Initialization value for static [`Once`] values.
 ///
-/// [`Once`]: struct.Once.html
+/// # Examples
 ///
+/// ```
+/// use std::sync::{Once, ONCE_INIT};
+///
+/// static START: Once = ONCE_INIT;
+/// ```
 pub const ONCE_INIT: Once = Once::new();
 
 // Four states that a Once can be in, encoded into the lower bits of
@@ -130,7 +172,7 @@
 // use interior mutability.
 #[repr(align(4))] // Ensure the two lower bits are free to use as state bits.
 struct Waiter {
-    thread: Cell<Option<SgxThread>>,
+    thread: Cell<Option<Thread>>,
     signaled: AtomicBool,
     next: *const Waiter,
 }
@@ -145,6 +187,7 @@
 
 impl Once {
     /// Creates a new `Once` value.
+    #[inline]
     pub const fn new() -> Once {
         Once { state_and_queue: AtomicUsize::new(INCOMPLETE), _marker: marker::PhantomData }
     }
@@ -157,21 +200,50 @@
     /// routine is currently running.
     ///
     /// When this function returns, it is guaranteed that some initialization
-    /// has run and completed (it may not be the closure specified). It is also
+    /// has run and completed (it might not be the closure specified). It is also
     /// guaranteed that any memory writes performed by the executed closure can
     /// be reliably observed by other threads at this point (there is a
     /// happens-before relation between the closure and code executing after the
     /// return).
     ///
-    /// If the given closure recursively invokes `call_once` on the same `Once`
+    /// If the given closure recursively invokes `call_once` on the same [`Once`]
     /// instance the exact behavior is not specified, allowed outcomes are
     /// a panic or a deadlock.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::Once;
+    ///
+    /// static mut VAL: usize = 0;
+    /// static INIT: Once = Once::new();
+    ///
+    /// // Accessing a `static mut` is unsafe much of the time, but if we do so
+    /// // in a synchronized fashion (e.g., write once or read all) then we're
+    /// // good to go!
+    /// //
+    /// // This function will only call `expensive_computation` once, and will
+    /// // otherwise always return the value returned from the first invocation.
+    /// fn get_cached_val() -> usize {
+    ///     unsafe {
+    ///         INIT.call_once(|| {
+    ///             VAL = expensive_computation();
+    ///         });
+    ///         VAL
+    ///     }
+    /// }
+    ///
+    /// fn expensive_computation() -> usize {
+    ///     // ...
+    /// # 2
+    /// }
+    /// ```
+    ///
     /// # Panics
     ///
     /// The closure `f` will only be executed once if this is called
     /// concurrently amongst many threads. If that closure panics, however, then
-    /// it will *poison* this `Once` instance, causing all future invocations of
+    /// it will *poison* this [`Once`] instance, causing all future invocations of
     /// `call_once` to also panic.
     ///
     /// This is similar to [poisoning with mutexes][poison].
@@ -190,22 +262,50 @@
         self.call_inner(false, &mut |_| f.take().unwrap()());
     }
 
-    /// Performs the same function as [`call_once`] except ignores poisoning.
+    /// Performs the same function as [`call_once()`] except ignores poisoning.
     ///
-    /// Unlike [`call_once`], if this `Once` has been poisoned (i.e., a previous
-    /// call to `call_once` or `call_once_force` caused a panic), calling
-    /// `call_once_force` will still invoke the closure `f` and will _not_
-    /// result in an immediate panic. If `f` panics, the `Once` will remain
-    /// in a poison state. If `f` does _not_ panic, the `Once` will no
-    /// longer be in a poison state and all future calls to `call_once` or
-    /// `call_one_force` will be no-ops.
+    /// Unlike [`call_once()`], if this [`Once`] has been poisoned (i.e., a previous
+    /// call to [`call_once()`] or [`call_once_force()`] caused a panic), calling
+    /// [`call_once_force()`] will still invoke the closure `f` and will _not_
+    /// result in an immediate panic. If `f` panics, the [`Once`] will remain
+    /// in a poison state. If `f` does _not_ panic, the [`Once`] will no
+    /// longer be in a poison state and all future calls to [`call_once()`] or
+    /// [`call_once_force()`] will be no-ops.
     ///
     /// The closure `f` is yielded a [`OnceState`] structure which can be used
-    /// to query the poison status of the `Once`.
+    /// to query the poison status of the [`Once`].
     ///
-    /// [`call_once`]: struct.Once.html#method.call_once
-    /// [`OnceState`]: struct.OnceState.html
+    /// [`call_once()`]: Once::call_once
+    /// [`call_once_force()`]: Once::call_once_force
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::Once;
+    /// use std::thread;
+    ///
+    /// static INIT: Once = Once::new();
+    ///
+    /// // poison the once
+    /// let handle = thread::spawn(|| {
+    ///     INIT.call_once(|| panic!());
+    /// });
+    /// assert!(handle.join().is_err());
+    ///
+    /// // poisoning propagates
+    /// let handle = thread::spawn(|| {
+    ///     INIT.call_once(|| {});
+    /// });
+    /// assert!(handle.join().is_err());
+    ///
+    /// // call_once_force will still run and reset the poisoned state
+    /// INIT.call_once_force(|state| {
+    ///     assert!(state.is_poisoned());
+    /// });
+    ///
+    /// // once any success happens, we stop propagating the poison
+    /// INIT.call_once(|| {});
+    /// ```
     pub fn call_once_force<F>(&self, f: F)
     where
         F: FnOnce(&OnceState),
@@ -219,19 +319,47 @@
         self.call_inner(true, &mut |p| f.take().unwrap()(p));
     }
 
-    /// Returns `true` if some `call_once` call has completed
+    /// Returns `true` if some [`call_once()`] call has completed
     /// successfully. Specifically, `is_completed` will return false in
     /// the following situations:
-    ///   * `call_once` was not called at all,
-    ///   * `call_once` was called, but has not yet completed,
-    ///   * the `Once` instance is poisoned
+    ///   * [`call_once()`] was not called at all,
+    ///   * [`call_once()`] was called, but has not yet completed,
+    ///   * the [`Once`] instance is poisoned
     ///
-    /// This function returning `false` does not mean that `Once` has not been
+    /// This function returning `false` does not mean that [`Once`] has not been
     /// executed. For example, it may have been executed in the time between
     /// when `is_completed` starts executing and when it returns, in which case
     /// the `false` return value would be stale (but still permissible).
-    /// `call_once`.
     ///
+    /// [`call_once()`]: Once::call_once
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::Once;
+    ///
+    /// static INIT: Once = Once::new();
+    ///
+    /// assert_eq!(INIT.is_completed(), false);
+    /// INIT.call_once(|| {
+    ///     assert_eq!(INIT.is_completed(), false);
+    /// });
+    /// assert_eq!(INIT.is_completed(), true);
+    /// ```
+    ///
+    /// ```
+    /// use std::sync::Once;
+    /// use std::thread;
+    ///
+    /// static INIT: Once = Once::new();
+    ///
+    /// assert_eq!(INIT.is_completed(), false);
+    /// let handle = thread::spawn(|| {
+    ///     INIT.call_once(|| panic!());
+    /// });
+    /// assert!(handle.join().is_err());
+    /// assert_eq!(INIT.is_completed(), false);
+    /// ```
     #[inline]
     pub fn is_completed(&self) -> bool {
         // An `Acquire` load is enough because that makes all the initialization
@@ -342,7 +470,7 @@
             // If the managing thread happens to signal and unpark us before we
             // can park ourselves, the result could be this thread never gets
             // unparked. Luckily `park` comes with the guarantee that if it got
-            // an `unpark` just before on an unparked thread is does not park.
+            // an `unpark` just before on an unparked thread it does not park.
             thread::park();
         }
         break;
@@ -351,7 +479,7 @@
 
 impl fmt::Debug for Once {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("Once { .. }")
+        f.debug_struct("Once").finish_non_exhaustive()
     }
 }
 
@@ -387,12 +515,40 @@
 
 impl OnceState {
     /// Returns `true` if the associated [`Once`] was poisoned prior to the
-    /// invocation of the closure passed to [`call_once_force`].
+    /// invocation of the closure passed to [`Once::call_once_force()`].
     ///
-    /// [`call_once_force`]: struct.Once.html#method.call_once_force
-    /// [`Once`]: struct.Once.html
+    /// # Examples
     ///
-    pub fn poisoned(&self) -> bool {
+    /// A poisoned [`Once`]:
+    ///
+    /// ```
+    /// use std::sync::Once;
+    /// use std::thread;
+    ///
+    /// static INIT: Once = Once::new();
+    ///
+    /// // poison the once
+    /// let handle = thread::spawn(|| {
+    ///     INIT.call_once(|| panic!());
+    /// });
+    /// assert!(handle.join().is_err());
+    ///
+    /// INIT.call_once_force(|state| {
+    ///     assert!(state.is_poisoned());
+    /// });
+    /// ```
+    ///
+    /// An unpoisoned [`Once`]:
+    ///
+    /// ```
+    /// use std::sync::Once;
+    ///
+    /// static INIT: Once = Once::new();
+    ///
+    /// INIT.call_once_force(|state| {
+    ///     assert!(!state.is_poisoned());
+    /// });
+    pub fn is_poisoned(&self) -> bool {
         self.poisoned
     }
 
@@ -401,4 +557,4 @@
     pub(crate) fn poison(&self) {
         self.set_state_on_drop_to.set(POISONED);
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/sys_common/poison.rs b/sgx_tstd/src/sync/poison.rs
similarity index 76%
rename from sgx_tstd/src/sys_common/poison.rs
rename to sgx_tstd/src/sync/poison.rs
index 1e76ec3..a50fea1 100644
--- a/sgx_tstd/src/sys_common/poison.rs
+++ b/sgx_tstd/src/sync/poison.rs
@@ -15,10 +15,10 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::thread;
 use crate::error::Error;
-use core::fmt;
-use core::sync::atomic::{AtomicBool, Ordering};
+use crate::fmt;
+use crate::sync::atomic::{AtomicBool, Ordering};
+use crate::thread;
 
 pub struct Flag {
     failed: AtomicBool,
@@ -69,14 +69,46 @@
 /// is held. The precise semantics for when a lock is poisoned is documented on
 /// each lock, but once a lock is poisoned then all future acquisitions will
 /// return this error.
+///
+/// # Examples
+///
+/// ```
+/// use std::sync::{Arc, SgxMutex as Mutex};
+/// use std::thread;
+///
+/// let mutex = Arc::new(Mutex::new(1));
+///
+/// // poison the mutex
+/// let c_mutex = Arc::clone(&mutex);
+/// let _ = thread::spawn(move || {
+///     let mut data = c_mutex.lock().unwrap();
+///     *data = 2;
+///     panic!();
+/// }).join();
+///
+/// match mutex.lock() {
+///     Ok(_) => unreachable!(),
+///     Err(p_err) => {
+///         let data = p_err.get_ref();
+///         println!("recovered: {}", data);
+///     }
+/// };
+/// ```
+/// [`SgxMutex`]: crate::sync::SgxMutex
+/// [`SgxRwLock`]: crate::sync::SgxRwLock
 pub struct PoisonError<T> {
     guard: T,
 }
 
 /// An enumeration of possible errors associated with a [`TryLockResult`] which
 /// can occur while trying to acquire a lock, from the [`try_lock`] method on a
-/// [`Mutex`] or the [`try_read`] and [`try_write`] methods on an [`RwLock`].
+/// [`SgxMutex`] or the [`try_read`] and [`try_write`] methods on an [`SgxRwLock`].
 ///
+/// [`try_lock`]: crate::sync::SgxMutex::try_lock
+/// [`try_read`]: crate::sync::SgxRwLock::try_read
+/// [`try_write`]: crate::sync::SgxRwLock::try_write
+/// [`SgxMutex`]: crate::sync::SgxMutex
+/// [`SgxRwLock`]: crate::sync::SgxRwLock
 pub enum TryLockError<T> {
     /// The lock could not be acquired because another thread failed while holding
     /// the lock.
@@ -94,18 +126,19 @@
 /// the associated guard, and it can be acquired through the [`into_inner`]
 /// method.
 ///
+/// [`into_inner`]: PoisonError::into_inner
 pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
 
 /// A type alias for the result of a nonblocking locking method.
 ///
 /// For more information, see [`LockResult`]. A `TryLockResult` doesn't
-/// necessarily hold the associated guard in the [`Err`] type as the lock may not
+/// necessarily hold the associated guard in the [`Err`] type as the lock might not
 /// have been acquired for other reasons.
 pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
 
 impl<T> fmt::Debug for PoisonError<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        "PoisonError { inner: .. }".fmt(f)
+        f.debug_struct("PoisonError").finish_non_exhaustive()
     }
 }
 
@@ -116,6 +149,7 @@
 }
 
 impl<T> Error for PoisonError<T> {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "poisoned lock: another task failed inside"
     }
@@ -124,8 +158,8 @@
 impl<T> PoisonError<T> {
     /// Creates a `PoisonError`.
     ///
-    /// This is generally created by methods like [`SgxMutex::lock`] or [`SgxRwLock::read`].
-    ///
+    /// This is generally created by methods like [`Mutex::lock`](crate::sync::Mutex::lock)
+    /// or [`RwLock::read`](crate::sync::RwLock::read).
     pub fn new(guard: T) -> PoisonError<T> {
         PoisonError { guard }
     }
@@ -133,20 +167,39 @@
     /// Consumes this error indicating that a lock is poisoned, returning the
     /// underlying guard to allow access regardless.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::HashSet;
+    /// use std::sync::{Arc, SgxMutex as Mutex};
+    /// use std::thread;
+    ///
+    /// let mutex = Arc::new(Mutex::new(HashSet::new()));
+    ///
+    /// // poison the mutex
+    /// let c_mutex = Arc::clone(&mutex);
+    /// let _ = thread::spawn(move || {
+    ///     let mut data = c_mutex.lock().unwrap();
+    ///     data.insert(10);
+    ///     panic!();
+    /// }).join();
+    ///
+    /// let p_err = mutex.lock().unwrap_err();
+    /// let data = p_err.into_inner();
+    /// println!("recovered {} items", data.len());
+    /// ```
     pub fn into_inner(self) -> T {
         self.guard
     }
 
     /// Reaches into this error indicating that a lock is poisoned, returning a
     /// reference to the underlying guard to allow access regardless.
-    ///
     pub fn get_ref(&self) -> &T {
         &self.guard
     }
 
     /// Reaches into this error indicating that a lock is poisoned, returning a
     /// mutable reference to the underlying guard to allow access regardless.
-    ///
     pub fn get_mut(&mut self) -> &mut T {
         &mut self.guard
     }
@@ -178,6 +231,7 @@
 }
 
 impl<T> Error for TryLockError<T> {
+    #[allow(deprecated, deprecated_in_future)]
     fn description(&self) -> &str {
         match *self {
             TryLockError::Poisoned(ref p) => p.description(),
@@ -185,6 +239,7 @@
         }
     }
 
+    #[allow(deprecated)]
     fn cause(&self) -> Option<&dyn Error> {
         match *self {
             TryLockError::Poisoned(ref p) => Some(p),
diff --git a/sgx_tstd/src/sync/remutex.bak.rs b/sgx_tstd/src/sync/remutex.bak.rs
new file mode 100644
index 0000000..6e2aafb
--- /dev/null
+++ b/sgx_tstd/src/sync/remutex.bak.rs
@@ -0,0 +1,186 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+
+use alloc_crate::boxed::Box;
+use core::fmt;
+use core::ops::Deref;
+use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
+use crate::sys_common::remutex as sys;
+
+pub use crate::sys_common::remutex::SgxReentrantThreadMutex;
+
+
+/// A re-entrant mutual exclusion
+///
+/// This mutex will block *other* threads waiting for the lock to become
+/// available. The thread which has already locked the mutex can lock it
+/// multiple times without blocking, preventing a common source of deadlocks.
+pub struct SgxReentrantMutex<T> {
+    inner: Box<sys::SgxReentrantThreadMutex>,
+    poison: poison::Flag,
+    data: T,
+}
+
+unsafe impl<T: Send> Send for SgxReentrantMutex<T> {}
+unsafe impl<T: Send> Sync for SgxReentrantMutex<T> {}
+
+impl<T> SgxReentrantMutex<T> {
+    /// Creates a new reentrant mutex in an unlocked state.
+    pub fn new(t: T) -> SgxReentrantMutex<T> {
+        SgxReentrantMutex{
+            inner: Box::new(sys::SgxReentrantThreadMutex::new()),
+            poison: poison::Flag::new(),
+            data: t,
+        }
+    }
+
+    /// Acquires a mutex, blocking the current thread until it is able to do so.
+    ///
+    /// This function will block the caller until it is available to acquire the mutex.
+    /// Upon returning, the thread is the only thread with the mutex held. When the thread
+    /// calling this method already holds the lock, the call shall succeed without
+    /// blocking.
+    ///
+    /// # Errors
+    ///
+    /// If another user of this mutex panicked while holding the mutex, then
+    /// this call will return failure if the mutex would otherwise be
+    /// acquired.
+    pub fn lock(&self) -> LockResult<SgxReentrantMutexGuard<'_, T>> {
+        unsafe {
+            self.inner.lock();
+            SgxReentrantMutexGuard::new(self)
+        }
+    }
+
+    /// Attempts to acquire this lock.
+    ///
+    /// If the lock could not be acquired at this time, then `Err` is returned.
+    /// Otherwise, an RAII guard is returned.
+    ///
+    /// This function does not block.
+    ///
+    /// # Errors
+    ///
+    /// If another user of this mutex panicked while holding the mutex, then
+    /// this call will return failure if the mutex would otherwise be
+    /// acquired.
+    pub fn try_lock(&self) -> TryLockResult<SgxReentrantMutexGuard<'_, T>> {
+        unsafe {
+            match self.inner.try_lock() {
+                Ok(_) => Ok(SgxReentrantMutexGuard::new(self)?),
+                Err(_) => Err(TryLockError::WouldBlock),
+            }
+        }
+    }
+}
+
+impl<T> Drop for SgxReentrantMutex<T> {
+    fn drop(&mut self) {
+        // This is actually safe b/c we know that there is no further usage of
+        // this mutex (it's up to the user to arrange for a mutex to get
+        // dropped, that's not our job)
+        let result = unsafe { self.inner.destroy() };
+        debug_assert_eq!(result, Ok(()), "Error when destroy an SgxReentrantMutex: {}", result.unwrap_err());
+    }
+}
+
+impl<T: fmt::Debug + 'static> fmt::Debug for SgxReentrantMutex<T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self.try_lock() {
+            Ok(guard) => f.debug_struct("SgxReentrantMutex").field("data", &*guard).finish(),
+            Err(TryLockError::Poisoned(err)) => {
+                f.debug_struct("SgxReentrantMutex").field("data", &**err.get_ref()).finish()
+            },
+            Err(TryLockError::WouldBlock) => {
+                struct LockedPlaceholder;
+                impl fmt::Debug for LockedPlaceholder {
+                    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+                        f.write_str("<locked>")
+                    }
+                }
+
+                f.debug_struct("SgxReentrantMutex").field("data", &LockedPlaceholder).finish()
+            }
+        }
+    }
+}
+
+/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
+/// dropped (falls out of scope), the lock will be unlocked.
+///
+/// The data protected by the mutex can be accessed through this guard via its
+/// Deref implementation.
+///
+/// # Mutability
+///
+/// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
+/// because implementation of the trait would violate Rust’s reference aliasing
+/// rules. Use interior mutability (usually `RefCell`) in order to mutate the
+/// guarded data.
+#[must_use]
+pub struct SgxReentrantMutexGuard<'a, T: 'a> {
+    // funny underscores due to how Deref currently works (it disregards field
+    // privacy).
+    lock: &'a SgxReentrantMutex<T>,
+    poison: poison::Guard,
+}
+
+impl<T> !Send for SgxReentrantMutexGuard<'_, T> {}
+
+impl<'mutex, T> SgxReentrantMutexGuard<'mutex, T> {
+    fn new(lock: &'mutex SgxReentrantMutex<T>) -> LockResult<SgxReentrantMutexGuard<'mutex, T>> {
+        poison::map_result(lock.poison.borrow(), |guard| {
+            SgxReentrantMutexGuard {
+                lock: lock,
+                poison: guard,
+            }
+        })
+    }
+}
+
+impl<T> Deref for SgxReentrantMutexGuard<'_, T> {
+    type Target = T;
+
+    fn deref(&self) -> &T {
+        &self.lock.data
+    }
+}
+
+impl<T> Drop for SgxReentrantMutexGuard<'_, T> {
+    #[inline]
+    fn drop(&mut self) {
+        let result = unsafe {
+            self.lock.poison.done(&self.poison);
+            self.lock.inner.unlock()
+        };
+        debug_assert_eq!(result, Ok(()), "Error when unlocking an SgxReentrantMutex: {}", result.unwrap_err());
+    }
+}
+
+impl<T: fmt::Debug> fmt::Debug for SgxReentrantMutexGuard<'_, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        fmt::Debug::fmt(&**self, f)
+    }
+}
+
+impl<T: fmt::Display> fmt::Display for SgxReentrantMutexGuard<'_, T> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        (**self).fmt(f)
+    }
+}
diff --git a/sgx_tstd/src/sync/remutex.rs b/sgx_tstd/src/sync/remutex.rs
deleted file mode 100644
index 65bf6c0..0000000
--- a/sgx_tstd/src/sync/remutex.rs
+++ /dev/null
@@ -1,356 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use sgx_types::SysError;
-use core::fmt;
-use core::ops::Deref;
-use alloc_crate::boxed::Box;
-use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
-use crate::sys::mutex as sys;
-
-/// The structure of sgx mutex.
-pub struct SgxReentrantThreadMutex(sys::SgxThreadMutex);
-
-unsafe impl Send for SgxReentrantThreadMutex {}
-unsafe impl Sync for SgxReentrantThreadMutex {}
-
-impl SgxReentrantThreadMutex {
-    ///
-    /// The function initializes a trusted mutex object within the enclave.
-    ///
-    /// # Description
-    ///
-    /// When a thread creates a mutex within an enclave, sgx_thread_mutex_
-    /// init simply initializes the various fields of the mutex object to indicate that
-    /// the mutex is available. rsgx_thread_mutex_init creates a non-recursive
-    /// mutex. The results of using a mutex in a lock or unlock operation before it has
-    /// been fully initialized (for example, the function call to rsgx_thread_mutex_
-    /// init returns) are undefined. To avoid race conditions in the initialization of a
-    /// trusted mutex, it is recommended statically initializing the mutex with the
-    /// macro SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_NON_RECURSIVE_MUTEX_INITIALIZER ,
-    /// of, or SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER instead.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Return value
-    ///
-    /// The trusted mutex object to be initialized.
-    ///
-    pub const fn new() -> SgxReentrantThreadMutex {
-        SgxReentrantThreadMutex(sys::SgxThreadMutex::new(sys::SgxThreadMutexControl::SGX_THREAD_MUTEX_RECURSIVE))
-    }
-
-    ///
-    /// The function locks a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// To acquire a mutex, a thread first needs to acquire the corresponding spin
-    /// lock. After the spin lock is acquired, the thread checks whether the mutex is
-    /// available. If the queue is empty or the thread is at the head of the queue the
-    /// thread will now become the owner of the mutex. To confirm its ownership, the
-    /// thread updates the refcount and owner fields. If the mutex is not available, the
-    /// thread searches the queue. If the thread is already in the queue, but not at the
-    /// head, it means that the thread has previously tried to lock the mutex, but it
-    /// did not succeed and had to wait outside the enclave and it has been
-    /// awakened unexpectedly. When this happens, the thread makes an OCALL and
-    /// simply goes back to sleep. If the thread is trying to lock the mutex for the first
-    /// time, it will update the waiting queue and make an OCALL to get suspended.
-    /// Note that threads release the spin lock after acquiring the mutex or before
-    /// leaving the enclave.
-    ///
-    /// **Note**
-    ///
-    /// A thread should not exit an enclave returning from a root ECALL after acquiring
-    /// the ownership of a mutex. Do not split the critical section protected by a
-    /// mutex across root ECALLs.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    #[inline]
-    pub unsafe fn lock(&self) -> SysError {
-        self.0.lock()
-    }
-
-    ///
-    /// The function tries to lock a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// A thread may check the status of the mutex, which implies acquiring the spin
-    /// lock and verifying that the mutex is available and that the queue is empty or
-    /// the thread is at the head of the queue. When this happens, the thread
-    /// acquires the mutex, releases the spin lock and returns 0. Otherwise, the
-    /// thread releases the spin lock and returns EINVAL/EBUSY. The thread is not suspended
-    /// in this case.
-    ///
-    /// **Note**
-    ///
-    /// A thread should not exit an enclave returning from a root ECALL after acquiring
-    /// the ownership of a mutex. Do not split the critical section protected by a
-    /// mutex across root ECALLs.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    /// **EBUSY**
-    ///
-    /// The mutex is locked by another thread or has pending threads to acquire the mutex
-    ///
-    #[inline]
-    pub unsafe fn try_lock(&self) -> SysError {
-        self.0.try_lock()
-    }
-
-    ///
-    /// The function unlocks a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// Before a thread releases a mutex, it has to verify it is the owner of the mutex. If
-    /// that is the case, the thread decreases the refcount by 1 and then may either
-    /// continue normal execution or wakeup the first thread in the queue. Note that
-    /// to ensure the state of the mutex remains consistent, the thread that is
-    /// awakened by the thread releasing the mutex will then try to acquire the
-    /// mutex almost as in the initial call to the rsgx_thread_mutex_lock routine.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid or it is not locked by any thread.
-    ///
-    /// **EPERM**
-    ///
-    /// The mutex is locked by another thread.
-    ///
-    #[inline]
-    pub unsafe fn unlock(&self) -> SysError {
-        self.0.unlock()
-    }
-
-    ///
-    /// The function destroys a trusted mutex object within an enclave.
-    ///
-    /// # Description
-    ///
-    /// rsgx_thread_mutex_destroy resets the mutex, which brings it to its initial
-    /// status. In this process, certain fields are checked to prevent releasing a mutex
-    /// that is still owned by a thread or on which threads are still waiting.
-    ///
-    /// **Note**
-    ///
-    /// Locking or unlocking a mutex after it has been destroyed results in undefined
-    /// behavior. After a mutex is destroyed, it must be re-created before it can be
-    /// used again.
-    ///
-    /// # Requirements
-    ///
-    /// Library: libsgx_tstdc.a
-    ///
-    /// # Errors
-    ///
-    /// **EINVAL**
-    ///
-    /// The trusted mutex object is invalid.
-    ///
-    /// **EBUSY**
-    ///
-    /// The mutex is locked by another thread or has pending threads to acquire the mutex.
-    ///
-    #[inline]
-    pub unsafe fn destroy(&self) -> SysError {
-        self.0.destroy()
-    }
-}
-
-/// A re-entrant mutual exclusion
-///
-/// This mutex will block *other* threads waiting for the lock to become
-/// available. The thread which has already locked the mutex can lock it
-/// multiple times without blocking, preventing a common source of deadlocks.
-pub struct SgxReentrantMutex<T> {
-    inner: Box<SgxReentrantThreadMutex>,
-    poison: poison::Flag,
-    data: T,
-}
-
-unsafe impl<T: Send> Send for SgxReentrantMutex<T> {}
-unsafe impl<T: Send> Sync for SgxReentrantMutex<T> {}
-
-impl<T> SgxReentrantMutex<T> {
-    /// Creates a new reentrant mutex in an unlocked state.
-    pub fn new(t: T) -> SgxReentrantMutex<T> {
-        SgxReentrantMutex{
-            inner: Box::new(SgxReentrantThreadMutex::new()),
-            poison: poison::Flag::new(),
-            data: t,
-        }
-    }
-
-    /// Acquires a mutex, blocking the current thread until it is able to do so.
-    ///
-    /// This function will block the caller until it is available to acquire the mutex.
-    /// Upon returning, the thread is the only thread with the mutex held. When the thread
-    /// calling this method already holds the lock, the call shall succeed without
-    /// blocking.
-    ///
-    /// # Errors
-    ///
-    /// If another user of this mutex panicked while holding the mutex, then
-    /// this call will return failure if the mutex would otherwise be
-    /// acquired.
-    pub fn lock(&self) -> LockResult<SgxReentrantMutexGuard<'_, T>> {
-        unsafe { self.inner.lock(); }
-        SgxReentrantMutexGuard::new(&self)
-    }
-
-    /// Attempts to acquire this lock.
-    ///
-    /// If the lock could not be acquired at this time, then `Err` is returned.
-    /// Otherwise, an RAII guard is returned.
-    ///
-    /// This function does not block.
-    ///
-    /// # Errors
-    ///
-    /// If another user of this mutex panicked while holding the mutex, then
-    /// this call will return failure if the mutex would otherwise be
-    /// acquired.
-    pub fn try_lock(&self) -> TryLockResult<SgxReentrantMutexGuard<'_, T>> {
-        match unsafe { self.inner.try_lock() } {
-            Ok(_) => Ok(SgxReentrantMutexGuard::new(&self)?),
-            Err(_) => Err(TryLockError::WouldBlock),
-        }
-    }
-}
-
-impl<T> Drop for SgxReentrantMutex<T> {
-    fn drop(&mut self) {
-        // This is actually safe b/c we know that there is no further usage of
-        // this mutex (it's up to the user to arrange for a mutex to get
-        // dropped, that's not our job)
-        let result = unsafe { self.inner.destroy() };
-        debug_assert_eq!(result, Ok(()), "Error when destroy an SgxReentrantMutex: {}", result.unwrap_err());
-    }
-}
-
-impl<T: fmt::Debug + 'static> fmt::Debug for SgxReentrantMutex<T> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match self.try_lock() {
-            Ok(guard) => f.debug_struct("SgxReentrantMutex").field("data", &*guard).finish(),
-            Err(TryLockError::Poisoned(err)) => {
-                f.debug_struct("SgxReentrantMutex").field("data", &**err.get_ref()).finish()
-            },
-            Err(TryLockError::WouldBlock) => {
-                struct LockedPlaceholder;
-                impl fmt::Debug for LockedPlaceholder {
-                    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-                        f.write_str("<locked>")
-                    }
-                }
-
-                f.debug_struct("SgxReentrantMutex").field("data", &LockedPlaceholder).finish()
-            }
-        }
-    }
-}
-
-/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
-/// dropped (falls out of scope), the lock will be unlocked.
-///
-/// The data protected by the mutex can be accessed through this guard via its
-/// Deref implementation.
-///
-/// # Mutability
-///
-/// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
-/// because implementation of the trait would violate Rust’s reference aliasing
-/// rules. Use interior mutability (usually `RefCell`) in order to mutate the
-/// guarded data.
-#[must_use]
-pub struct SgxReentrantMutexGuard<'a, T: 'a> {
-    // funny underscores due to how Deref currently works (it disregards field
-    // privacy).
-    lock: &'a SgxReentrantMutex<T>,
-    poison: poison::Guard,
-}
-
-impl<T> !Send for SgxReentrantMutexGuard<'_, T> {}
-
-impl<'mutex, T> SgxReentrantMutexGuard<'mutex, T> {
-    fn new(lock: &'mutex SgxReentrantMutex<T>) -> LockResult<SgxReentrantMutexGuard<'mutex, T>> {
-        poison::map_result(lock.poison.borrow(), |guard| {
-            SgxReentrantMutexGuard {
-                lock: lock,
-                poison: guard,
-            }
-        })
-    }
-}
-
-impl<T> Deref for SgxReentrantMutexGuard<'_, T> {
-    type Target = T;
-
-    fn deref(&self) -> &T {
-        &self.lock.data
-    }
-}
-
-impl<T> Drop for SgxReentrantMutexGuard<'_, T> {
-    #[inline]
-    fn drop(&mut self) {
-        let result = unsafe {
-            self.lock.poison.done(&self.poison);
-            self.lock.inner.unlock()
-        };
-        debug_assert_eq!(result, Ok(()), "Error when unlocking an SgxReentrantMutex: {}", result.unwrap_err());
-    }
-}
-
-impl<T: fmt::Debug> fmt::Debug for SgxReentrantMutexGuard<'_, T> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Debug::fmt(&**self, f)
-    }
-}
-
-impl<T: fmt::Display> fmt::Display for SgxReentrantMutexGuard<'_, T> {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        (**self).fmt(f)
-    }
-}
diff --git a/sgx_tstd/src/sync/rwlock.rs b/sgx_tstd/src/sync/rwlock.rs
index c12e77b..1a1c2d4 100644
--- a/sgx_tstd/src/sync/rwlock.rs
+++ b/sgx_tstd/src/sync/rwlock.rs
@@ -15,84 +15,16 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::SysError;
-use sgx_trts::libc;
-use core::cell::UnsafeCell;
-use core::mem;
-use core::ptr;
-use core::fmt;
-use core::ops::{Deref, DerefMut};
-use alloc_crate::boxed::Box;
-use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult};
-use crate::sys::rwlock as imp;
+use crate::cell::UnsafeCell;
+use crate::fmt;
+use crate::ops::{Deref, DerefMut};
+use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
+use crate::sys_common::rwlock as sys;
 
-/// An OS-based reader-writer lock.
-///
-/// This structure is entirely unsafe and serves as the lowest layer of a
-/// cross-platform binding of system rwlocks. It is recommended to use the
-/// safer types at the top level of this crate instead of this type.
-pub struct SgxThreadRwLock(imp::SgxThreadRwLock);
+use sgx_libc as libc;
 
-unsafe impl Send for SgxThreadRwLock {}
-unsafe impl Sync for SgxThreadRwLock {}
+pub use crate::sys_common::rwlock::SgxThreadRwLock;
 
-impl SgxThreadRwLock {
-
-    /// Creates a new reader-writer lock for use.
-    pub const fn new() -> Self {
-        SgxThreadRwLock(imp::SgxThreadRwLock::new())
-    }
-
-    /// Acquires shared access to the underlying lock, blocking the current
-    /// thread to do so.
-    #[inline]
-    pub unsafe fn read(&self) -> SysError {
-        self.0.read()
-    }
-
-    /// Attempts to acquire shared access to this lock, returning whether it
-    /// succeeded or not.
-    ///
-    /// This function does not block the current thread.
-    #[inline]
-    pub unsafe fn try_read(&self) -> SysError {
-        self.0.try_read()
-    }
-
-    /// Acquires write access to the underlying lock, blocking the current thread
-    /// to do so.
-    #[inline]
-    pub unsafe fn write(&self) -> SysError {
-        self.0.write()
-    }
-
-    /// Attempts to acquire exclusive access to this lock, returning whether it
-    /// succeeded or not.
-    ///
-    /// This function does not block the current thread.
-    #[inline]
-    pub unsafe fn try_write(&self) -> SysError {
-        self.0.try_write()
-    }
-
-    /// Unlocks previously acquired shared access to this lock.
-    #[inline]
-    pub unsafe fn read_unlock(&self) -> SysError {
-        self.0.read_unlock()
-    }
-
-    /// Unlocks previously acquired exclusive access to this lock.
-    #[inline]
-    pub unsafe fn write_unlock(&self) -> SysError {
-        self.0.write_unlock()
-    }
-
-    /// Destroys OS-related resources with this RWLock.
-    #[inline]
-    pub unsafe fn destroy(&self) -> SysError {
-        self.0.destroy()
-    }
-}
 
 /// A reader-writer lock
 ///
@@ -108,7 +40,21 @@
 ///
 /// The priority policy of the lock is dependent on the underlying operating
 /// system's implementation, and this type does not guarantee that any
-/// particular policy will be used.
+/// particular policy will be used. In particular, a writer which is waiting to
+/// acquire the lock in `write` might or might not block concurrent calls to
+/// `read`, e.g.:
+///
+/// <details><summary>Potential deadlock example</summary>
+///
+/// ```text
+/// // Thread 1             |  // Thread 2
+/// let _rg = lock.read();  |
+///                         |  // will block
+///                         |  let _wg = lock.write();
+/// // may deadlock         |
+/// let _rg = lock.read();  |
+/// ```
+/// </details>
 ///
 /// The type parameter `T` represents the data that this lock protects. It is
 /// required that `T` satisfies [`Send`] to be shared across threads and
@@ -118,13 +64,37 @@
 ///
 /// # Poisoning
 ///
-/// An `RwLock`, like [`Mutex`], will become poisoned on a panic. Note, however,
+/// An `RwLock`, like [`SgxMutex`], will become poisoned on a panic. Note, however,
 /// that an `RwLock` may only be poisoned if a panic occurs while it is locked
 /// exclusively (write mode). If a panic occurs in any reader, then the lock
 /// will not be poisoned.
 ///
+/// # Examples
+///
+/// ```
+/// use std::sync::SgxRwLock as RwLock;
+///
+/// let lock = RwLock::new(5);
+///
+/// // many reader locks can be held at once
+/// {
+///     let r1 = lock.read().unwrap();
+///     let r2 = lock.read().unwrap();
+///     assert_eq!(*r1, 5);
+///     assert_eq!(*r2, 5);
+/// } // read locks are dropped at this point
+///
+/// // only one write lock may be held, however
+/// {
+///     let mut w = lock.write().unwrap();
+///     *w += 1;
+///     assert_eq!(*w, 6);
+/// } // write lock is dropped here
+/// ```
+///
+/// [`SgxMutex`]: super::SgxMutex
 pub struct SgxRwLock<T: ?Sized> {
-    inner: Box<SgxThreadRwLock>,
+    inner: sys::SgxMovableThreadRwLock,
     poison: poison::Flag,
     data: UnsafeCell<T>,
 }
@@ -137,9 +107,11 @@
 ///
 /// This structure is created by the [`read`] and [`try_read`] methods on
 /// [`RwLock`].
+///
+/// [`read`]: RwLock::read
+/// [`try_read`]: RwLock::try_read
 pub struct SgxRwLockReadGuard<'a, T: ?Sized + 'a> {
     lock: &'a SgxRwLock<T>,
-    poison: poison::Guard,
 }
 
 impl<T: ?Sized> !Send for SgxRwLockReadGuard<'_, T> {}
@@ -150,6 +122,9 @@
 ///
 /// This structure is created by the [`write`] and [`try_write`] methods
 /// on [`RwLock`].
+///
+/// [`write`]: RwLock::write
+/// [`try_write`]: RwLock::try_write
 pub struct SgxRwLockWriteGuard<'a, T: ?Sized + 'a> {
     lock: &'a SgxRwLock<T>,
     poison: poison::Guard,
@@ -160,9 +135,17 @@
 
 impl<T> SgxRwLock<T> {
     /// Creates a new instance of an `RwLock<T>` which is unlocked.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let lock = RwLock::new(5);
+    /// ```
     pub fn new(t: T) -> SgxRwLock<T> {
         SgxRwLock {
-            inner: Box::new(SgxThreadRwLock::new()),
+            inner: sys::SgxMovableThreadRwLock::new(),
             poison: poison::Flag::new(),
             data: UnsafeCell::new(t),
         }
@@ -191,6 +174,25 @@
     /// # Panics
     ///
     /// This function might panic when called if the lock is already held by the current thread.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxRwLock as RwLock};
+    /// use std::thread;
+    ///
+    /// let lock = Arc::new(RwLock::new(1));
+    /// let c_lock = Arc::clone(&lock);
+    ///
+    /// let n = lock.read().unwrap();
+    /// assert_eq!(*n, 1);
+    ///
+    /// thread::spawn(move || {
+    ///     let r = c_lock.read();
+    ///     assert!(r.is_ok());
+    /// }).join().unwrap();
+    /// ```
+    #[inline]
     pub fn read(&self) -> LockResult<SgxRwLockReadGuard<'_, T>> {
         unsafe {
             let ret = self.inner.read();
@@ -215,14 +217,33 @@
     ///
     /// # Errors
     ///
-    /// This function will return an error if the RwLock is poisoned. An RwLock
-    /// is poisoned whenever a writer panics while holding an exclusive lock. An
-    /// error will only be returned if the lock would have otherwise been
+    /// This function will return the [`Poisoned`] error if the RwLock is poisoned.
+    /// An RwLock is poisoned whenever a writer panics while holding an exclusive
+    /// lock. `Poisoned` will only be returned if the lock would have otherwise been
     /// acquired.
+    ///
+    /// This function will return the [`WouldBlock`] error if the RwLock could not
+    /// be acquired because it was already locked exclusively.
+    ///
+    /// [`Poisoned`]: TryLockError::Poisoned
+    /// [`WouldBlock`]: TryLockError::WouldBlock
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let lock = RwLock::new(1);
+    ///
+    /// match lock.try_read() {
+    ///     Ok(n) => assert_eq!(*n, 1),
+    ///     Err(_) => unreachable!(),
+    /// };
+    /// ```
+    #[inline]
     pub fn try_read(&self) -> TryLockResult<SgxRwLockReadGuard<'_, T>> {
         unsafe {
-            let ret = self.inner.try_read();
-            match ret {
+            match self.inner.try_read() {
                 Ok(_) => Ok(SgxRwLockReadGuard::new(self)?),
                 Err(_) => Err(TryLockError::WouldBlock),
             }
@@ -247,10 +268,23 @@
     /// # Panics
     ///
     /// This function might panic when called if the lock is already held by the current thread.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let lock = RwLock::new(1);
+    ///
+    /// let mut n = lock.write().unwrap();
+    /// *n = 2;
+    ///
+    /// assert!(lock.try_read().is_err());
+    /// ```
+    #[inline]
     pub fn write(&self) -> LockResult<SgxRwLockWriteGuard<'_, T>> {
         unsafe {
-            let ret = self.inner.write();
-            match ret {
+            match self.inner.write() {
                 Err(libc::EAGAIN) => panic!("rwlock maximum writer count exceeded"),
                 Err(libc::EDEADLK) => panic!("rwlock write lock would result in deadlock"),
                 _ => SgxRwLockWriteGuard::new(self),
@@ -271,14 +305,34 @@
     ///
     /// # Errors
     ///
-    /// This function will return an error if the RwLock is poisoned. An RwLock
-    /// is poisoned whenever a writer panics while holding an exclusive lock. An
-    /// error will only be returned if the lock would have otherwise been
-    /// acquired.
+    /// This function will return the [`Poisoned`] error if the RwLock is
+    /// poisoned. An RwLock is poisoned whenever a writer panics while holding
+    /// an exclusive lock. `Poisoned` will only be returned if the lock would have
+    /// otherwise been acquired.
+    ///
+    /// This function will return the [`WouldBlock`] error if the RwLock could not
+    /// be acquired because it was already locked exclusively.
+    ///
+    /// [`Poisoned`]: TryLockError::Poisoned
+    /// [`WouldBlock`]: TryLockError::WouldBlock
+    ///
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let lock = RwLock::new(1);
+    ///
+    /// let n = lock.read().unwrap();
+    /// assert_eq!(*n, 1);
+    ///
+    /// assert!(lock.try_write().is_err());
+    /// ```
+    #[inline]
     pub fn try_write(&self) -> TryLockResult<SgxRwLockWriteGuard<'_, T>> {
         unsafe {
-            let ret = self.inner.try_write();
-            match ret {
+            match self.inner.try_write() {
                 Ok(_) => Ok(SgxRwLockWriteGuard::new(self)?),
                 Err(_) => Err(TryLockError::WouldBlock),
             }
@@ -291,6 +345,21 @@
     /// time. You should not trust a `false` value for program correctness
     /// without additional synchronization.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::{Arc, SgxRwLock as RwLock};
+    /// use std::thread;
+    ///
+    /// let lock = Arc::new(RwLock::new(0));
+    /// let c_lock = Arc::clone(&lock);
+    ///
+    /// let _ = thread::spawn(move || {
+    ///     let _lock = c_lock.write().unwrap();
+    ///     panic!(); // the lock gets poisoned
+    /// }).join();
+    /// assert_eq!(lock.is_poisoned(), true);
+    /// ```
     #[inline]
     pub fn is_poisoned(&self) -> bool {
         self.poison.get()
@@ -304,27 +373,25 @@
     /// is poisoned whenever a writer panics while holding an exclusive lock. An
     /// error will only be returned if the lock would have otherwise been
     /// acquired.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let lock = RwLock::new(String::new());
+    /// {
+    ///     let mut s = lock.write().unwrap();
+    ///     *s = "modified".to_owned();
+    /// }
+    /// assert_eq!(lock.into_inner().unwrap(), "modified");
+    /// ```
     pub fn into_inner(self) -> LockResult<T>
     where
         T: Sized,
     {
-        // We know statically that there are no outstanding references to
-        // `self` so there's no need to lock the inner lock.
-        //
-        // To get the inner value, we'd like to call `data.into_inner()`,
-        // but because `RwLock` impl-s `Drop`, we can't move out of it, so
-        // we'll have to destructure it manually instead.
-        unsafe {
-            let (inner, poison, data) = {
-                let SgxRwLock { ref inner, ref poison, ref data } = self;
-                (ptr::read(inner), ptr::read(poison), ptr::read(data))
-            };
-            mem::forget(self);
-            let _ = inner.destroy();
-            drop(inner);
-
-            poison::map_result(poison.borrow(), |_| data.into_inner())
-        }
+        let data = self.data.into_inner();
+        poison::map_result(self.poison.borrow(), |_| data)
     }
 
     /// Returns a mutable reference to the underlying data.
@@ -339,28 +406,30 @@
     /// error will only be returned if the lock would have otherwise been
     /// acquired.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::sync::SgxRwLock as RwLock;
+    ///
+    /// let mut lock = RwLock::new(0);
+    /// *lock.get_mut().unwrap() = 10;
+    /// assert_eq!(*lock.read().unwrap(), 10);
+    /// ```
     pub fn get_mut(&mut self) -> LockResult<&mut T> {
-        // We know statically that there are no other references to `self`, so
-        // there's no need to lock the inner lock.
-        let data = unsafe { &mut *self.data.get() };
+        let data = self.data.get_mut();
         poison::map_result(self.poison.borrow(), |_| data)
     }
 }
 
-unsafe impl<#[may_dangle] T: ?Sized> Drop for SgxRwLock<T> {
-    fn drop(&mut self) {
-        // IMPORTANT: This code needs to be kept in sync with `SgxRwLock::into_inner`.
-        let result = unsafe { self.inner.destroy() };
-        debug_assert_eq!(result, Ok(()), "Error when destroy an SgxMutex: {}", result.unwrap_err());
-    }
-}
-
 impl<T: ?Sized + fmt::Debug> fmt::Debug for SgxRwLock<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        let mut d = f.debug_struct("SgxRwLock");
         match self.try_read() {
-            Ok(guard) => f.debug_struct("SgxRwLock").field("data", &&*guard).finish(),
+            Ok(guard) => {
+                d.field("data", &&*guard);
+            }
             Err(TryLockError::Poisoned(err)) => {
-                f.debug_struct("SgxRwLock").field("data", &&**err.get_ref()).finish()
+                d.field("data", &&**err.get_ref());
             }
             Err(TryLockError::WouldBlock) => {
                 struct LockedPlaceholder;
@@ -369,10 +438,11 @@
                         f.write_str("<locked>")
                     }
                 }
-
-                f.debug_struct("SgxRwLock").field("data", &LockedPlaceholder).finish()
+                d.field("data", &LockedPlaceholder);
             }
         }
+        d.field("poisoned", &self.poison.get());
+        d.finish_non_exhaustive()
     }
 }
 
@@ -386,8 +456,6 @@
 impl<T> From<T> for SgxRwLock<T> {
     /// Creates a new instance of an `SgxRwLock<T>` which is unlocked.
     /// This is equivalent to [`SgxRwLock::new`].
-    ///
-    /// [`SgxRwLock::new`]: #method.new
     fn from(t: T) -> Self {
         SgxRwLock::new(t)
     }
@@ -396,7 +464,7 @@
 impl<'rwlock, T: ?Sized> SgxRwLockReadGuard<'rwlock, T> {
 
     unsafe fn new(lock: &'rwlock SgxRwLock<T>) -> LockResult<SgxRwLockReadGuard<'rwlock, T>> {
-            poison::map_result(lock.poison.borrow(), |guard| SgxRwLockReadGuard { lock, poison: guard })
+        poison::map_result(lock.poison.borrow(), |_| SgxRwLockReadGuard { lock })
     }
 }
 
@@ -408,7 +476,7 @@
 
 impl<T: fmt::Debug> fmt::Debug for SgxRwLockReadGuard<'_, T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("SgxRwLockReadGuard").field("lock", &self.lock).finish()
+        (**self).fmt(f)
     }
 }
 
@@ -420,7 +488,7 @@
 
 impl<T: fmt::Debug> fmt::Debug for SgxRwLockWriteGuard<'_, T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("SgxRwLockWriteGuard").field("lock", &self.lock).finish()
+        (**self).fmt(f)
     }
 }
 
@@ -455,7 +523,6 @@
 impl<T: ?Sized> Drop for SgxRwLockReadGuard<'_, T> {
     fn drop(&mut self) {
         let result = unsafe {
-            self.lock.poison.done(&self.poison);
             self.lock.inner.read_unlock()
         };
         debug_assert_eq!(result, Ok(()), "Error when unlocking an SgxRwLock: {}", result.unwrap_err());
@@ -464,8 +531,8 @@
 
 impl<T: ?Sized> Drop for SgxRwLockWriteGuard<'_, T> {
     fn drop(&mut self) {
+        self.lock.poison.done(&self.poison);
         let result = unsafe {
-            self.lock.poison.done(&self.poison);
             self.lock.inner.write_unlock()
         };
         debug_assert_eq!(result, Ok(()), "Error when unlocking an SgxRwLock: {}", result.unwrap_err());
diff --git a/sgx_tstd/src/sync/spinlock.rs b/sgx_tstd/src/sync/spinlock.rs
index 869d6c9..b3fe9be 100644
--- a/sgx_tstd/src/sync/spinlock.rs
+++ b/sgx_tstd/src/sync/spinlock.rs
@@ -15,9 +15,9 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::{self, sgx_spinlock_t};
 use core::marker;
 use core::cell::UnsafeCell;
+use sgx_types::{self, sgx_spinlock_t};
 
 unsafe fn raw_lock(lock: &mut sgx_spinlock_t) -> *mut sgx_spinlock_t {
     lock as *mut _
@@ -77,7 +77,6 @@
 unsafe impl Sync for SgxThreadSpinlock {}
 
 impl SgxThreadSpinlock {
-
     pub const fn new() -> SgxThreadSpinlock {
         SgxThreadSpinlock{ lock: UnsafeCell::new(sgx_types::SGX_SPINLOCK_INITIALIZER) }
     }
@@ -126,7 +125,7 @@
 
 impl<'spinlock> SgxSpinlockGuard<'spinlock> {
     unsafe fn new(lock: &'spinlock SgxSpinlock) -> Self {
-        SgxSpinlockGuard{lock: lock}
+        SgxSpinlockGuard{ lock }
     }
 }
 
@@ -137,4 +136,3 @@
         }
     }
 }
-
diff --git a/sgx_tstd/src/sys/backtrace/mod.rs b/sgx_tstd/src/sys/backtrace/mod.rs
index 815c3b8..eb4a642 100644
--- a/sgx_tstd/src/sys/backtrace/mod.rs
+++ b/sgx_tstd/src/sys/backtrace/mod.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
+pub use self::printing::{BacktraceFmt, BacktraceFrameFmt, PrintFmt};
 /// Backtrace support built on libgcc with some extra OS-specific support
 ///
 /// Some methods of getting a backtrace:
@@ -89,9 +90,7 @@
 /// pointers, and we use dladdr() or libbacktrace to translate these addresses
 /// to symbols. This is a bit of a hokey implementation as-is, but it works for
 /// all unix platforms we support right now, so it at least gets the job done.
-
 pub use self::tracing::{trace_unsynchronized, Frame};
-pub use self::printing::{BacktraceFmt, BacktraceFrameFmt, PrintFmt};
 
 // tracing impls:
 mod tracing;
@@ -132,22 +131,21 @@
 }
 
 pub mod gnu {
-    use crate::ffi::CString;
     use crate::enclave;
+    use crate::ffi::CString;
     use crate::io::{self, Error, ErrorKind};
+    use crate::mem;
     use crate::os::unix::ffi::OsStrExt;
-    use core::mem;
-    use sgx_trts::libc::c_char;
 
-    pub fn get_enclave_filename() -> io::Result<Vec<c_char>> {
+    pub fn get_enclave_filename() -> io::Result<Vec<u8>> {
         let p = enclave::get_enclave_path();
         let result = match p {
-            None => { Err(Error::new(ErrorKind::Other, "Not implemented")) },
+            None => Err(Error::new(ErrorKind::Other, "Not implemented")),
             Some(path) => {
-                let c_str = CString::new(path.as_os_str().as_bytes())?;
-                let c_vec = unsafe{ mem::transmute(c_str.into_bytes_with_nul()) };
-                Ok(c_vec)
-            },
+                let cstr = CString::new(path.as_os_str().as_bytes())?;
+                let v = unsafe { mem::transmute(cstr.into_bytes_with_nul()) };
+                Ok(v)
+            }
         };
         result
     }
diff --git a/sgx_tstd/src/sys/backtrace/printing/mod.rs b/sgx_tstd/src/sys/backtrace/printing/mod.rs
index b7c705f..2674060 100644
--- a/sgx_tstd/src/sys/backtrace/printing/mod.rs
+++ b/sgx_tstd/src/sys/backtrace/printing/mod.rs
@@ -15,10 +15,10 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::sys::backtrace::{Frame, BytesOrWideString};
+use crate::ffi::c_void;
+use crate::fmt;
+use crate::sys::backtrace::{BytesOrWideString, Frame};
 use crate::sys_common::backtrace::{Symbol, SymbolName};
-use core::ffi::c_void;
-use core::fmt;
 
 const HEX_WIDTH: usize = 2 + 2 * core::mem::size_of::<usize>();
 
@@ -31,16 +31,20 @@
     fmt: &'a mut fmt::Formatter<'b>,
     frame_index: usize,
     format: PrintFmt,
-    print_path: &'a mut (dyn FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b),
+    print_path:
+        &'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result + 'b),
 }
 
 /// The styles of printing that we can print
+#[allow(clippy::manual_non_exhaustive)]
+#[non_exhaustive]
 #[derive(Copy, Clone, Eq, PartialEq)]
 pub enum PrintFmt {
     /// Prints a terser backtrace which ideally only contains relevant information
     Short,
     /// Prints a backtrace that contains all possible information
     Full,
+
     #[doc(hidden)]
     __Nonexhaustive,
 }
@@ -56,7 +60,8 @@
     pub fn new(
         fmt: &'a mut fmt::Formatter<'b>,
         format: PrintFmt,
-        print_path: &'a mut (dyn FnMut(&mut fmt::Formatter, BytesOrWideString) -> fmt::Result + 'b),
+        print_path: &'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result
+                     + 'b),
     ) -> Self {
         BacktraceFmt {
             fmt,
@@ -69,7 +74,7 @@
     /// Prints a preamble for the backtrace about to be printed.
     ///
     /// This is required on some platforms for backtraces to be fully
-    /// sumbolicated later, and otherwise this should just be the first method
+    /// symbolicated later, and otherwise this should just be the first method
     /// you call after creating a `BacktraceFmt`.
     pub fn add_context(&mut self) -> fmt::Result {
         Ok(())
@@ -109,11 +114,12 @@
     /// Prints a raw traced `Frame` and `Symbol`, typically from within the raw
     /// callbacks of this crate.
     pub fn symbol(&mut self, frame: &Frame, symbol: &Symbol) -> fmt::Result {
-        self.print_raw(
+        self.print_raw_with_column(
             frame.ip(),
             symbol.name(),
             symbol.filename_raw(),
             symbol.lineno(),
+            symbol.colno(),
         )?;
         Ok(())
     }
@@ -126,11 +132,27 @@
     pub fn print_raw(
         &mut self,
         frame_ip: *mut c_void,
-        symbol_name: Option<SymbolName>,
-        filename: Option<BytesOrWideString>,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
         lineno: Option<u32>,
     ) -> fmt::Result {
-        self.print_raw_generic(frame_ip, symbol_name, filename, lineno)?;
+        self.print_raw_with_column(frame_ip, symbol_name, filename, lineno, None)
+    }
+
+    /// Adds a raw frame to the backtrace output, including column information.
+    ///
+    /// This method, like the previous, takes the raw arguments in case
+    /// they're being source from different locations. Note that this may be
+    /// called multiple times for one frame.
+    pub fn print_raw_with_column(
+        &mut self,
+        frame_ip: *mut c_void,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
+        lineno: Option<u32>,
+        colno: Option<u32>,
+    ) -> fmt::Result {
+        self.print_raw_generic(frame_ip, symbol_name, filename, lineno, colno)?;
         self.symbol_index += 1;
         Ok(())
     }
@@ -139,9 +161,10 @@
     fn print_raw_generic(
         &mut self,
         mut frame_ip: *mut c_void,
-        symbol_name: Option<SymbolName>,
-        filename: Option<BytesOrWideString>,
+        symbol_name: Option<SymbolName<'_>>,
+        filename: Option<BytesOrWideString<'_>>,
         lineno: Option<u32>,
+        colno: Option<u32>,
     ) -> fmt::Result {
         // No need to print "null" frames, it basically just means that the
         // system backtrace was a bit eager to trace back super far.
@@ -174,18 +197,22 @@
             (Some(name), PrintFmt::Full) => write!(self.fmt.fmt, "{}", name)?,
             (None, _) | (_, PrintFmt::__Nonexhaustive) => write!(self.fmt.fmt, "<unknown>")?,
         }
-
         self.fmt.fmt.write_str("\n")?;
 
         // And last up, print out the filename/line number if they're available.
         if let (Some(file), Some(line)) = (filename, lineno) {
-            self.print_fileline(file, line)?;
+            self.print_fileline(file, line, colno)?;
         }
 
         Ok(())
     }
 
-    fn print_fileline(&mut self, file: BytesOrWideString, line: u32) -> fmt::Result {
+    fn print_fileline(
+        &mut self,
+        file: BytesOrWideString<'_>,
+        line: u32,
+        colno: Option<u32>,
+    ) -> fmt::Result {
         // Filename/line are printed on lines under the symbol name, so print
         // some appropriate whitespace to sort of right-align ourselves.
         if let PrintFmt::Full = self.fmt.format {
@@ -196,7 +223,14 @@
         // Delegate to our internal callback to print the filename and then
         // print out the line number.
         (self.fmt.print_path)(self.fmt.fmt, file)?;
-        write!(self.fmt.fmt, ":{}\n", line)?;
+        write!(self.fmt.fmt, ":{}", line)?;
+
+        // Add column number, if available.
+        if let Some(colno) = colno {
+            write!(self.fmt.fmt, ":{}", colno)?;
+        }
+
+        writeln!(self.fmt.fmt)?;
         Ok(())
     }
 }
diff --git a/sgx_tstd/src/sys/backtrace/tracing/gcc_s.rs b/sgx_tstd/src/sys/backtrace/tracing/gcc_s.rs
index 0707778..1b58e53 100644
--- a/sgx_tstd/src/sys/backtrace/tracing/gcc_s.rs
+++ b/sgx_tstd/src/sys/backtrace/tracing/gcc_s.rs
@@ -15,14 +15,16 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_unwind as uw;
-use core::ffi::c_void;
+use crate::ffi::c_void;
 use crate::sys::backtrace::Bomb;
 
+use sgx_unwind as uw;
+
 pub enum Frame {
     Raw(*mut uw::_Unwind_Context),
     Cloned {
         ip: *mut c_void,
+        sp: *mut c_void,
         symbol_address: *mut c_void,
     },
 }
@@ -43,6 +45,13 @@
         unsafe { uw::_Unwind_GetIP(ctx) as *mut c_void }
     }
 
+    pub fn sp(&self) -> *mut c_void {
+        match *self {
+            Frame::Raw(ctx) => unsafe { uw::_Unwind_GetCFA(ctx) as *mut c_void },
+            Frame::Cloned { sp, .. } => sp,
+        }
+    }
+
     pub fn symbol_address(&self) -> *mut c_void {
         if let Frame::Cloned { symbol_address, .. } = *self {
             return symbol_address;
@@ -62,12 +71,17 @@
             unsafe { uw::_Unwind_FindEnclosingFunction(self.ip()) }
         }
     }
+
+    pub fn module_base_address(&self) -> Option<*mut c_void> {
+        None
+    }
 }
 
 impl Clone for Frame {
     fn clone(&self) -> Frame {
         Frame::Cloned {
             ip: self.ip(),
+            sp: self.sp(),
             symbol_address: self.symbol_address(),
         }
     }
diff --git a/sgx_tstd/src/sys/backtrace/tracing/mod.rs b/sgx_tstd/src/sys/backtrace/tracing/mod.rs
index e96c42a..41d9677 100644
--- a/sgx_tstd/src/sys/backtrace/tracing/mod.rs
+++ b/sgx_tstd/src/sys/backtrace/tracing/mod.rs
@@ -15,8 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::ffi::c_void;
-use core::fmt;
+use crate::ffi::c_void;
+use crate::fmt;
 
 /// Same as `trace`, only unsafe as it's unsynchronized.
 ///
@@ -55,6 +55,14 @@
         self.inner.ip()
     }
 
+    /// Returns the current stack pointer of this frame.
+    ///
+    /// In the case that a backend cannot recover the stack pointer for this
+    /// frame, a null pointer is returned.
+    pub fn sp(&self) -> *mut c_void {
+        self.inner.sp()
+    }
+
     /// Returns the starting symbol address of the frame of this function.
     ///
     /// This will attempt to rewind the instruction pointer returned by `ip` to
@@ -66,10 +74,15 @@
     pub fn symbol_address(&self) -> *mut c_void {
         self.inner.symbol_address()
     }
+
+    /// Returns the base address of the module to which the frame belongs.
+    pub fn module_base_address(&self) -> Option<*mut c_void> {
+        self.inner.module_base_address()
+    }
 }
 
 impl fmt::Debug for Frame {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         f.debug_struct("Frame")
             .field("ip", &self.ip())
             .field("symbol_address", &self.symbol_address())
diff --git a/sgx_tstd/src/sys/cmath.rs b/sgx_tstd/src/sys/cmath.rs
index c5df605..7e1db43 100644
--- a/sgx_tstd/src/sys/cmath.rs
+++ b/sgx_tstd/src/sys/cmath.rs
@@ -15,34 +15,32 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_libc::{c_float, c_double};
-
 #[link(name = "sgx_tstdc")]
 extern "C" {
-    pub fn acos(n: c_double) -> c_double;
-    pub fn acosf(n: c_float) -> c_float;
-    pub fn asin(n: c_double) -> c_double;
-    pub fn asinf(n: c_float) -> c_float;
-    pub fn atan(n: c_double) -> c_double;
-    pub fn atan2(a: c_double, b: c_double) -> c_double;
-    pub fn atan2f(a: c_float, b: c_float) -> c_float;
-    pub fn atanf(n: c_float) -> c_float;
-    pub fn cbrt(n: c_double) -> c_double;
-    pub fn cbrtf(n: c_float) -> c_float;
-    pub fn cosh(n: c_double) -> c_double;
-    pub fn coshf(n: c_float) -> c_float;
-    pub fn expm1(n: c_double) -> c_double;
-    pub fn expm1f(n: c_float) -> c_float;
-    pub fn fdim(a: c_double, b: c_double) -> c_double;
-    pub fn fdimf(a: c_float, b: c_float) -> c_float;
-    pub fn hypot(x: c_double, y: c_double) -> c_double;
-    pub fn hypotf(x: c_float, y: c_float) -> c_float;
-    pub fn log1p(n: c_double) -> c_double;
-    pub fn log1pf(n: c_float) -> c_float;
-    pub fn sinh(n: c_double) -> c_double;
-    pub fn sinhf(n: c_float) -> c_float;
-    pub fn tan(n: c_double) -> c_double;
-    pub fn tanf(n: c_float) -> c_float;
-    pub fn tanh(n: c_double) -> c_double;
-    pub fn tanhf(n: c_float) -> c_float;
-}
+    pub fn acos(n: f64) -> f64;
+    pub fn acosf(n: f32) -> f32;
+    pub fn asin(n: f64) -> f64;
+    pub fn asinf(n: f32) -> f32;
+    pub fn atan(n: f64) -> f64;
+    pub fn atan2(a: f64, b: f64) -> f64;
+    pub fn atan2f(a: f32, b: f32) -> f32;
+    pub fn atanf(n: f32) -> f32;
+    pub fn cbrt(n: f64) -> f64;
+    pub fn cbrtf(n: f32) -> f32;
+    pub fn cosh(n: f64) -> f64;
+    pub fn coshf(n: f32) -> f32;
+    pub fn expm1(n: f64) -> f64;
+    pub fn expm1f(n: f32) -> f32;
+    pub fn fdim(a: f64, b: f64) -> f64;
+    pub fn fdimf(a: f32, b: f32) -> f32;
+    pub fn hypot(x: f64, y: f64) -> f64;
+    pub fn hypotf(x: f32, y: f32) -> f32;
+    pub fn log1p(n: f64) -> f64;
+    pub fn log1pf(n: f32) -> f32;
+    pub fn sinh(n: f64) -> f64;
+    pub fn sinhf(n: f32) -> f32;
+    pub fn tan(n: f64) -> f64;
+    pub fn tanf(n: f32) -> f32;
+    pub fn tanh(n: f64) -> f64;
+    pub fn tanhf(n: f32) -> f32;
+}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/condvar.rs b/sgx_tstd/src/sys/condvar.rs
index 4a749c1..7864bcc 100644
--- a/sgx_tstd/src/sys/condvar.rs
+++ b/sgx_tstd/src/sys/condvar.rs
@@ -15,17 +15,19 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use alloc_crate::collections::LinkedList;
-use core::cell::UnsafeCell;
+use crate::boxed::Box;
+use crate::cell::UnsafeCell;
+use crate::collections::LinkedList;
 use crate::io::{self, Error};
-use crate::sys::mutex::{self, SgxThreadMutex};
 use crate::sync::SgxThreadSpinlock;
-use crate::time::Duration;
+use crate::sys::mutex::{self, SgxThreadMutex};
 use crate::thread::rsgx_thread_self;
+use crate::time::Duration;
 use crate::u64;
+
+use sgx_libc as libc;
 use sgx_trts::enclave::SgxThreadData;
-use sgx_trts::libc;
-use sgx_types::{SysError, sgx_thread_t, SGX_THREAD_T_NULL};
+use sgx_types::{sgx_thread_t, SysError, SGX_THREAD_T_NULL};
 
 struct SgxThreadCondvarInner {
     lock: SgxThreadSpinlock,
@@ -54,7 +56,10 @@
         loop {
             self.lock.unlock();
             if waiter == SGX_THREAD_T_NULL {
-                mutex::thread_wait_event(SgxThreadData::current().get_tcs(), Duration::new(u64::MAX, 1_000_000_000 - 1));
+                mutex::thread_wait_event(
+                    SgxThreadData::current().get_tcs(),
+                    Duration::new(u64::MAX, 1_000_000_000 - 1),
+                );
             } else {
                 mutex::thread_setwait_events(
                     SgxThreadData::from_raw(waiter).get_tcs(),
@@ -143,7 +148,7 @@
 
         let mut tcs_vec: Vec<usize> = Vec::new();
         while let Some(waiter) = self.queue.pop_back() {
-           tcs_vec.push(SgxThreadData::from_raw(waiter).get_tcs())
+            tcs_vec.push(SgxThreadData::from_raw(waiter).get_tcs())
         }
         self.lock.unlock();
         mutex::thread_set_multiple_events(tcs_vec.as_slice());
@@ -170,24 +175,26 @@
     }
 }
 
+pub type SgxMovableThreadCondvar = Box<SgxThreadCondvar>;
+
 unsafe impl Send for SgxThreadCondvar {}
 unsafe impl Sync for SgxThreadCondvar {}
 
-
 pub struct SgxThreadCondvar {
     inner: UnsafeCell<SgxThreadCondvarInner>,
 }
 
 impl SgxThreadCondvar {
     pub const fn new() -> Self {
-        SgxThreadCondvar { inner: UnsafeCell::new(SgxThreadCondvarInner::new()) }
+        SgxThreadCondvar {
+            inner: UnsafeCell::new(SgxThreadCondvarInner::new()),
+        }
     }
 
     #[inline]
     pub unsafe fn wait(&self, mutex: &SgxThreadMutex) -> SysError {
         let condvar: &mut SgxThreadCondvarInner = &mut *self.inner.get();
         condvar.wait(mutex)
-        
     }
 
     #[inline]
diff --git a/sgx_tstd/src/sys/ext/fs.rs b/sgx_tstd/src/sys/ext/fs.rs
deleted file mode 100644
index a919b68..0000000
--- a/sgx_tstd/src/sys/ext/fs.rs
+++ /dev/null
@@ -1,428 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use sgx_trts::libc;
-#[cfg(feature = "untrusted_fs")]
-use crate::fs::{self, OpenOptions, Permissions};
-#[cfg(not(feature = "untrusted_fs"))]
-use crate::untrusted::fs::{self, OpenOptions, Permissions};
-use crate::io;
-use crate::path::Path;
-use crate::sys;
-use crate::sys_common::{AsInner, AsInnerMut, FromInner};
-use crate::os::fs::MetadataExt as UnixMetadataExt;
-
-/// Unix-specific extensions to `File`
-pub trait FileExt {
-    /// Reads a number of bytes starting from a given offset.
-    ///
-    /// Returns the number of bytes read.
-    ///
-    /// The offset is relative to the start of the file and thus independent
-    /// from the current cursor.
-    ///
-    /// The current file cursor is not affected by this function.
-    ///
-    /// Note that similar to [`File::read`], it is not an error to return with a
-    /// short read.
-    ///
-    /// [`File::read`]: ../../../../std/fs/struct.File.html#method.read
-    ///
-    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;
-
-    /// Reads the exact number of byte required to fill `buf` from the given offset.
-    ///
-    /// The offset is relative to the start of the file and thus independent
-    /// from the current cursor.
-    ///
-    /// The current file cursor is not affected by this function.
-    ///
-    /// Similar to [`Read::read_exact`] but uses [`read_at`] instead of `read`.
-    ///
-    /// [`Read::read_exact`]: ../../../../std/io/trait.Read.html#method.read_exact
-    /// [`read_at`]: #tymethod.read_at
-    ///
-    /// # Errors
-    ///
-    /// If this function encounters an error of the kind
-    /// [`ErrorKind::Interrupted`] then the error is ignored and the operation
-    /// will continue.
-    ///
-    /// If this function encounters an "end of file" before completely filling
-    /// the buffer, it returns an error of the kind [`ErrorKind::UnexpectedEof`].
-    /// The contents of `buf` are unspecified in this case.
-    ///
-    /// If any other read error is encountered then this function immediately
-    /// returns. The contents of `buf` are unspecified in this case.
-    ///
-    /// If this function returns an error, it is unspecified how many bytes it
-    /// has read, but it will never read more than would be necessary to
-    /// completely fill the buffer.
-    ///
-    /// [`ErrorKind::Interrupted`]: ../../../../std/io/enum.ErrorKind.html#variant.Interrupted
-    /// [`ErrorKind::UnexpectedEof`]: ../../../../std/io/enum.ErrorKind.html#variant.UnexpectedEof
-    ///
-    fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64) -> io::Result<()> {
-        while !buf.is_empty() {
-            match self.read_at(buf, offset) {
-                Ok(0) => break,
-                Ok(n) => {
-                    let tmp = buf;
-                    buf = &mut tmp[n..];
-                    offset += n as u64;
-                }
-                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
-                Err(e) => return Err(e),
-            }
-        }
-        if !buf.is_empty() {
-            Err(io::Error::new(io::ErrorKind::UnexpectedEof, "failed to fill whole buffer"))
-        } else {
-            Ok(())
-        }
-    }
-
-    /// Writes a number of bytes starting from a given offset.
-    ///
-    /// Returns the number of bytes written.
-    ///
-    /// The offset is relative to the start of the file and thus independent
-    /// from the current cursor.
-    ///
-    /// The current file cursor is not affected by this function.
-    ///
-    /// When writing beyond the end of the file, the file is appropriately
-    /// extended and the intermediate bytes are initialized with the value 0.
-    ///
-    /// Note that similar to [`File::write`], it is not an error to return a
-    /// short write.
-    ///
-    /// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
-    ///
-    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
-
-    /// Attempts to write an entire buffer starting from a given offset.
-    ///
-    /// The offset is relative to the start of the file and thus independent
-    /// from the current cursor.
-    ///
-    /// The current file cursor is not affected by this function.
-    ///
-    /// This method will continuously call [`write_at`] until there is no more data
-    /// to be written or an error of non-[`ErrorKind::Interrupted`] kind is
-    /// returned. This method will not return until the entire buffer has been
-    /// successfully written or such an error occurs. The first error that is
-    /// not of [`ErrorKind::Interrupted`] kind generated from this method will be
-    /// returned.
-    ///
-    /// # Errors
-    ///
-    /// This function will return the first error of
-    /// non-[`ErrorKind::Interrupted`] kind that [`write_at`] returns.
-    ///
-    /// [`ErrorKind::Interrupted`]: ../../../../std/io/enum.ErrorKind.html#variant.Interrupted
-    /// [`write_at`]: #tymethod.write_at
-    ///
-    fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
-        while !buf.is_empty() {
-            match self.write_at(buf, offset) {
-                Ok(0) => {
-                    return Err(io::Error::new(
-                        io::ErrorKind::WriteZero,
-                        "failed to write whole buffer",
-                    ));
-                }
-                Ok(n) => {
-                    buf = &buf[n..];
-                    offset += n as u64
-                }
-                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
-                Err(e) => return Err(e),
-            }
-        }
-        Ok(())
-    }
-}
-
-impl FileExt for fs::File {
-    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
-        self.as_inner().read_at(buf, offset)
-    }
-    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
-        self.as_inner().write_at(buf, offset)
-    }
-}
-
-/// Unix-specific extensions to [`fs::Permissions`].
-///
-/// [`fs::Permissions`]: ../../../../std/fs/struct.Permissions.html
-pub trait PermissionsExt {
-    /// Returns the underlying raw `st_mode` bits that contain the standard
-    /// Unix permissions for this file.
-    ///
-    fn mode(&self) -> u32;
-
-    /// Sets the underlying raw bits for this set of permissions.
-    ///
-    fn set_mode(&mut self, mode: u32);
-
-    /// Creates a new instance of `Permissions` from the given set of Unix
-    /// permission bits.
-    ///
-    fn from_mode(mode: u32) -> Self;
-}
-
-impl PermissionsExt for Permissions {
-    fn mode(&self) -> u32 {
-        self.as_inner().mode()
-    }
-
-    fn set_mode(&mut self, mode: u32) {
-        *self = Permissions::from_inner(FromInner::from_inner(mode));
-    }
-
-    fn from_mode(mode: u32) -> Permissions {
-        Permissions::from_inner(FromInner::from_inner(mode))
-    }
-}
-
-/// Unix-specific extensions to [`fs::OpenOptions`].
-///
-/// [`fs::OpenOptions`]: ../../../../std/fs/struct.OpenOptions.html
-pub trait OpenOptionsExt {
-    /// Sets the mode bits that a new file will be created with.
-    ///
-    /// If a new file is created as part of an `OpenOptions::open` call then this
-    /// specified `mode` will be used as the permission bits for the new file.
-    /// If no `mode` is set, the default of `0o666` will be used.
-    /// The operating system masks out bits with the system's `umask`, to produce
-    /// the final permissions.
-    ///
-    fn mode(&mut self, mode: u32) -> &mut Self;
-
-    /// Pass custom flags to the `flags` argument of `open`.
-    ///
-    /// The bits that define the access mode are masked out with `O_ACCMODE`, to
-    /// ensure they do not interfere with the access mode set by Rusts options.
-    ///
-    /// Custom flags can only set flags, not remove flags set by Rusts options.
-    /// This options overwrites any previously set custom flags.
-    ///
-    fn custom_flags(&mut self, flags: i32) -> &mut Self;
-}
-
-impl OpenOptionsExt for OpenOptions {
-    fn mode(&mut self, mode: u32) -> &mut OpenOptions {
-        self.as_inner_mut().mode(mode);
-        self
-    }
-
-    fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
-        self.as_inner_mut().custom_flags(flags);
-        self
-    }
-}
-
-/// Unix-specific extensions to [`fs::Metadata`].
-///
-/// [`fs::Metadata`]: ../../../../std/fs/struct.Metadata.html
-pub trait MetadataExt {
-    /// Returns the ID of the device containing the file.
-    ///
-    fn dev(&self) -> u64;
-    /// Returns the inode number.
-    ///
-    fn ino(&self) -> u64;
-    /// Returns the rights applied to this file.
-    ///
-    fn mode(&self) -> u32;
-    /// Returns the number of hard links pointing to this file.
-    ///
-    fn nlink(&self) -> u64;
-    /// Returns the user ID of the owner of this file.
-    ///
-    fn uid(&self) -> u32;
-    /// Returns the group ID of the owner of this file.
-    ///
-    fn gid(&self) -> u32;
-    /// Returns the device ID of this file (if it is a special one).
-    ///
-    fn rdev(&self) -> u64;
-    /// Returns the total size of this file in bytes.
-    ///
-    fn size(&self) -> u64;
-    /// Returns the last access time of the file, in seconds since Unix Epoch.
-    ///
-    fn atime(&self) -> i64;
-    /// Returns the last access time of the file, in nanoseconds since [`atime`].
-    ///
-    /// [`atime`]: #tymethod.atime
-    ///
-    fn atime_nsec(&self) -> i64;
-    /// Returns the last modification time of the file, in seconds since Unix Epoch.
-    ///
-    fn mtime(&self) -> i64;
-    /// Returns the last modification time of the file, in nanoseconds since [`mtime`].
-    ///
-    /// [`mtime`]: #tymethod.mtime
-    ///
-    fn mtime_nsec(&self) -> i64;
-    /// Returns the last status change time of the file, in seconds since Unix Epoch.
-    ///
-    fn ctime(&self) -> i64;
-    /// Returns the last status change time of the file, in nanoseconds since [`ctime`].
-    ///
-    /// [`ctime`]: #tymethod.ctime
-    ///
-    fn ctime_nsec(&self) -> i64;
-    /// Returns the blocksize for filesystem I/O.
-    ///
-    fn blksize(&self) -> u64;
-    /// Returns the number of blocks allocated to the file, in 512-byte units.
-    ///
-    /// Please note that this may be smaller than `st_size / 512` when the file has holes.
-    ///
-    fn blocks(&self) -> u64;
-}
-
-impl MetadataExt for fs::Metadata {
-    fn dev(&self) -> u64 {
-        self.st_dev()
-    }
-    fn ino(&self) -> u64 {
-        self.st_ino()
-    }
-    fn mode(&self) -> u32 {
-        self.st_mode()
-    }
-    fn nlink(&self) -> u64 {
-        self.st_nlink()
-    }
-    fn uid(&self) -> u32 {
-        self.st_uid()
-    }
-    fn gid(&self) -> u32 {
-        self.st_gid()
-    }
-    fn rdev(&self) -> u64 {
-        self.st_rdev()
-    }
-    fn size(&self) -> u64 {
-        self.st_size()
-    }
-    fn atime(&self) -> i64 {
-        self.st_atime()
-    }
-    fn atime_nsec(&self) -> i64 {
-        self.st_atime_nsec()
-    }
-    fn mtime(&self) -> i64 {
-        self.st_mtime()
-    }
-    fn mtime_nsec(&self) -> i64 {
-        self.st_mtime_nsec()
-    }
-    fn ctime(&self) -> i64 {
-        self.st_ctime()
-    }
-    fn ctime_nsec(&self) -> i64 {
-        self.st_ctime_nsec()
-    }
-    fn blksize(&self) -> u64 {
-        self.st_blksize()
-    }
-    fn blocks(&self) -> u64 {
-        self.st_blocks()
-    }
-}
-
-/// Unix-specific extensions for [`FileType`].
-///
-/// Adds support for special Unix file types such as block/character devices,
-/// pipes, and sockets.
-///
-/// [`FileType`]: ../../../../std/fs/struct.FileType.html
-pub trait FileTypeExt {
-    /// Returns `true` if this file type is a block device.
-    ///
-    fn is_block_device(&self) -> bool;
-    /// Returns `true` if this file type is a char device.
-    ///
-    fn is_char_device(&self) -> bool;
-    /// Returns `true` if this file type is a fifo.
-    ///
-    fn is_fifo(&self) -> bool;
-    /// Returns `true` if this file type is a socket.
-    ///
-    fn is_socket(&self) -> bool;
-}
-
-impl FileTypeExt for fs::FileType {
-    fn is_block_device(&self) -> bool {
-        self.as_inner().is(libc::S_IFBLK)
-    }
-    fn is_char_device(&self) -> bool {
-        self.as_inner().is(libc::S_IFCHR)
-    }
-    fn is_fifo(&self) -> bool {
-        self.as_inner().is(libc::S_IFIFO)
-    }
-    fn is_socket(&self) -> bool {
-        self.as_inner().is(libc::S_IFSOCK)
-    }
-}
-/// Unix-specific extension methods for [`fs::DirEntry`].
-///
-pub trait DirEntryExt {
-    /// Returns the underlying `d_ino` field in the contained `dirent`
-    /// structure.
-    fn ino(&self) -> u64;
-}
-
-impl DirEntryExt for fs::DirEntry {
-    fn ino(&self) -> u64 {
-        self.as_inner().ino()
-    }
-}
-
-/// Creates a new symbolic link on the filesystem.
-///
-/// The `dst` path will be a symbolic link pointing to the `src` path.
-///
-/// # Note
-///
-/// On Windows, you must specify whether a symbolic link points to a file
-/// or directory. Use `os::windows::fs::symlink_file` to create a
-/// symbolic link to a file, or `os::windows::fs::symlink_dir` to create a
-/// symbolic link to a directory. Additionally, the process must have
-/// `SeCreateSymbolicLinkPrivilege` in order to be able to create a
-/// symbolic link.
-///
-pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
-    sys::fs::symlink(src.as_ref(), dst.as_ref())
-}
-
-pub trait DirBuilderExt {
-    fn mode(&mut self, mode: u32) -> &mut Self;
-}
-
-impl DirBuilderExt for fs::DirBuilder {
-    fn mode(&mut self, mode: u32) -> &mut fs::DirBuilder {
-        self.as_inner_mut().set_mode(mode);
-        self
-    }
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/ext/io.rs b/sgx_tstd/src/sys/ext/io.rs
deleted file mode 100644
index 51ce9f5..0000000
--- a/sgx_tstd/src/sys/ext/io.rs
+++ /dev/null
@@ -1,135 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-//! Unix-specific extensions to general I/O primitives
-
-use sgx_trts::libc;
-#[cfg(feature = "untrusted_fs")]
-use crate::fs;
-#[cfg(not(feature = "untrusted_fs"))]
-use crate::untrusted::fs;
-use crate::os::raw;
-use crate::sys;
-use crate::io;
-use crate::sys_common::{AsInner, FromInner, IntoInner};
-
-/// Raw file descriptors.
-pub type RawFd = raw::c_int;
-
-/// A trait to extract the raw unix file descriptor from an underlying
-/// object.
-///
-/// This is only available on unix platforms and must be imported in order
-/// to call the method. Windows platforms have a corresponding `AsRawHandle`
-/// and `AsRawSocket` set of traits.
-pub trait AsRawFd {
-    /// Extracts the raw file descriptor.
-    ///
-    /// This method does **not** pass ownership of the raw file descriptor
-    /// to the caller. The descriptor is only guaranteed to be valid while
-    /// the original object has not yet been destroyed.
-    fn as_raw_fd(&self) -> RawFd;
-}
-
-/// A trait to express the ability to construct an object from a raw file
-/// descriptor.
-pub trait FromRawFd {
-    /// Constructs a new instance of `Self` from the given raw file
-    /// descriptor.
-    ///
-    /// This function **consumes ownership** of the specified file
-    /// descriptor. The returned object will take responsibility for closing
-    /// it when the object goes out of scope.
-    ///
-    /// This function is also unsafe as the primitives currently returned
-    /// have the contract that they are the sole owner of the file
-    /// descriptor they are wrapping. Usage of this function could
-    /// accidentally allow violating this contract which can cause memory
-    /// unsafety in code that relies on it being true.
-    unsafe fn from_raw_fd(fd: RawFd) -> Self;
-}
-
-/// A trait to express the ability to consume an object and acquire ownership of
-/// its raw file descriptor.
-pub trait IntoRawFd {
-    /// Consumes this object, returning the raw underlying file descriptor.
-    ///
-    /// This function **transfers ownership** of the underlying file descriptor
-    /// to the caller. Callers are then the unique owners of the file descriptor
-    /// and must close the descriptor once it's no longer needed.
-    fn into_raw_fd(self) -> RawFd;
-}
-
-impl AsRawFd for fs::File {
-    fn as_raw_fd(&self) -> RawFd {
-        self.as_inner().fd().raw()
-    }
-}
-
-impl FromRawFd for fs::File {
-    unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
-        fs::File::from_inner(sys::fs::File::from_inner(fd))
-    }
-}
-
-impl IntoRawFd for fs::File {
-    fn into_raw_fd(self) -> RawFd {
-        self.into_inner().into_fd().into_raw()
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl AsRawFd for io::Stdin {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDIN_FILENO
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl AsRawFd for io::Stdout {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDOUT_FILENO
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl AsRawFd for io::Stderr {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDERR_FILENO
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl<'a> AsRawFd for io::StdinLock<'a> {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDIN_FILENO
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl<'a> AsRawFd for io::StdoutLock<'a> {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDOUT_FILENO
-    }
-}
-
-#[cfg(feature = "stdio")]
-impl<'a> AsRawFd for io::StderrLock<'a> {
-    fn as_raw_fd(&self) -> RawFd {
-        libc::STDERR_FILENO
-    }
-}
diff --git a/sgx_tstd/src/sys/ext/mod.rs b/sgx_tstd/src/sys/ext/mod.rs
deleted file mode 100644
index 2a2f9e0..0000000
--- a/sgx_tstd/src/sys/ext/mod.rs
+++ /dev/null
@@ -1,38 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-pub mod ffi;
-pub mod fs;
-pub mod io;
-#[cfg(feature = "net")]
-pub mod net;
-pub mod raw;
-#[cfg(feature = "thread")]
-pub mod thread;
-
-/// A prelude for conveniently writing platform-specific code.
-///
-/// Includes all extension traits, and some important type definitions.
-pub mod prelude {
-    pub use super::ffi::{OsStrExt, OsStringExt};
-    pub use super::fs::DirEntryExt;
-    pub use super::fs::FileExt;
-    pub use super::fs::{FileTypeExt, MetadataExt, OpenOptionsExt, PermissionsExt};
-    pub use super::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
-    #[cfg(feature = "thread")]
-    pub use super::thread::JoinHandleExt;
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/ext/net.rs b/sgx_tstd/src/sys/ext/net.rs
deleted file mode 100644
index 65c3249..0000000
--- a/sgx_tstd/src/sys/ext/net.rs
+++ /dev/null
@@ -1,825 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use core::mem;
-use core::fmt;
-use crate::ascii;
-use crate::ffi::OsStr;
-use crate::io::{self, Initializer, IoSlice, IoSliceMut};
-use crate::net::{self, Shutdown};
-use crate::os::unix::ffi::OsStrExt;
-use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
-use crate::path::Path;
-use crate::time::Duration;
-use crate::sys::{self, cvt};
-use crate::sys::net::Socket;
-use crate::sys_common::{self, AsInner, FromInner, IntoInner};
-
-fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
-    // Work with an actual instance of the type since using a null pointer is UB
-    let base = addr as *const _ as usize;
-    let path = &addr.sun_path as *const _ as usize;
-    path - base
-}
-
-unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
-    let mut addr: libc::sockaddr_un = mem::zeroed();
-    addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
-
-    let bytes = path.as_os_str().as_bytes();
-
-    if bytes.contains(&0) {
-        return Err(io::Error::new(
-            io::ErrorKind::InvalidInput,
-            "paths may not contain interior null bytes",
-        ));
-    }
-
-    if bytes.len() >= addr.sun_path.len() {
-        return Err(io::Error::new(
-            io::ErrorKind::InvalidInput,
-            "path must be shorter than SUN_LEN",
-        ));
-    }
-    for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
-        *dst = *src as libc::c_char;
-    }
-    // null byte for pathname addresses is already there because we zeroed the
-    // struct
-
-    let mut len = sun_path_offset(&addr) + bytes.len();
-    match bytes.get(0) {
-        Some(&0) | None => {}
-        Some(_) => len += 1,
-    }
-    Ok((addr, len as libc::socklen_t))
-}
-
-enum AddressKind<'a> {
-    Unnamed,
-    Pathname(&'a Path),
-    Abstract(&'a [u8]),
-}
-
-/// An address associated with a Unix socket.
-///
-#[derive(Clone)]
-pub struct SocketAddr {
-    addr: libc::sockaddr_un,
-    len: libc::socklen_t,
-}
-
-impl SocketAddr {
-    fn new<F>(f: F) -> io::Result<SocketAddr>
-    where
-        F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
-    {
-        unsafe {
-            let mut addr: libc::sockaddr_un = mem::zeroed();
-            let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
-            cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
-            SocketAddr::from_parts(addr, len)
-        }
-    }
-
-    fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
-        if len == 0 {
-            // When there is a datagram from unnamed unix socket
-            // linux returns zero bytes of address
-            len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
-        } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
-            return Err(io::Error::new(
-                io::ErrorKind::InvalidInput,
-                "file descriptor did not correspond to a Unix socket",
-            ));
-        }
-
-        Ok(SocketAddr { addr, len })
-    }
-
-    /// Returns `true` if the address is unnamed.
-    ///
-    pub fn is_unnamed(&self) -> bool {
-        if let AddressKind::Unnamed = self.address() { true } else { false }
-    }
-
-    /// Returns the contents of this address if it is a `pathname` address.
-    ///
-    pub fn as_pathname(&self) -> Option<&Path> {
-        if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
-    }
-
-    fn address(&self) -> AddressKind<'_> {
-        let len = self.len as usize - sun_path_offset(&self.addr);
-        let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
-
-        // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
-        if len == 0
-        {
-            AddressKind::Unnamed
-        } else if self.addr.sun_path[0] == 0 {
-            AddressKind::Abstract(&path[1..len])
-        } else {
-            AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
-        }
-    }
-}
-
-impl fmt::Debug for SocketAddr {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match self.address() {
-            AddressKind::Unnamed => write!(fmt, "(unnamed)"),
-            AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
-            AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
-        }
-    }
-}
-
-struct AsciiEscaped<'a>(&'a [u8]);
-
-impl<'a> fmt::Display for AsciiEscaped<'a> {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        write!(fmt, "\"")?;
-        for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
-            write!(fmt, "{}", byte as char)?;
-        }
-        write!(fmt, "\"")
-    }
-}
-
-/// A Unix stream socket.
-///
-pub struct UnixStream(Socket);
-
-impl fmt::Debug for UnixStream {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let mut builder = fmt.debug_struct("UnixStream");
-        builder.field("fd", self.0.as_inner());
-        if let Ok(addr) = self.local_addr() {
-            builder.field("local", &addr);
-        }
-        if let Ok(addr) = self.peer_addr() {
-            builder.field("peer", &addr);
-        }
-        builder.finish()
-    }
-}
-
-impl UnixStream {
-    /// Connects to the socket named by `path`.
-    ///
-    pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
-        fn inner(path: &Path) -> io::Result<UnixStream> {
-            unsafe {
-                let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
-                let (addr, len) = sockaddr_un(path)?;
-
-                cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
-                Ok(UnixStream(inner))
-            }
-        }
-        inner(path.as_ref())
-    }
-
-    /// Creates an unnamed pair of connected sockets.
-    ///
-    /// Returns two `UnixStream`s which are connected to each other.
-    ///
-    pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
-        let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
-        Ok((UnixStream(i1), UnixStream(i2)))
-    }
-
-    /// Creates a new independently owned handle to the underlying socket.
-    ///
-    /// The returned `UnixStream` is a reference to the same stream that this
-    /// object references. Both handles will read and write the same stream of
-    /// data, and options set on one stream will be propagated to the other
-    /// stream.
-    ///
-    pub fn try_clone(&self) -> io::Result<UnixStream> {
-        self.0.duplicate().map(UnixStream)
-    }
-
-    /// Returns the socket address of the local half of this connection.
-    ///
-    pub fn local_addr(&self) -> io::Result<SocketAddr> {
-        SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
-    }
-
-    /// Returns the socket address of the remote half of this connection.
-    ///
-    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
-        SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
-    }
-
-    /// Sets the read timeout for the socket.
-    ///
-    /// If the provided value is [`None`], then [`read`] calls will block
-    /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
-    /// method.
-    ///
-    /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
-    /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
-    /// [`Duration`]: ../../../../std/time/struct.Duration.html
-    ///
-    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
-        self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
-    }
-
-    /// Sets the write timeout for the socket.
-    ///
-    /// If the provided value is [`None`], then [`write`] calls will block
-    /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
-    /// passed to this method.
-    ///
-    /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
-    /// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
-    /// [`Duration`]: ../../../../std/time/struct.Duration.html
-    ///
-    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
-        self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
-    }
-
-    /// Returns the read timeout of this socket.
-    ///
-    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        self.0.timeout(libc::SO_RCVTIMEO)
-    }
-
-    /// Returns the write timeout of this socket.
-    ///
-    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        self.0.timeout(libc::SO_SNDTIMEO)
-    }
-
-    /// Moves the socket into or out of nonblocking mode.
-    ///
-    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
-        self.0.set_nonblocking(nonblocking)
-    }
-
-    /// Returns the value of the `SO_ERROR` option.
-    ///
-    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        self.0.take_error()
-    }
-
-    /// Shuts down the read, write, or both halves of this connection.
-    ///
-    /// This function will cause all pending and future I/O calls on the
-    /// specified portions to immediately return with an appropriate value
-    /// (see the documentation of [`Shutdown`]).
-    ///
-    /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
-    ///
-    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
-        self.0.shutdown(how)
-    }
-}
-
-impl io::Read for UnixStream {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        io::Read::read(&mut &*self, buf)
-    }
-
-    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        io::Read::read_vectored(&mut &*self, bufs)
-    }
-
-    #[inline]
-    unsafe fn initializer(&self) -> Initializer {
-        Initializer::nop()
-    }
-}
-
-impl<'a> io::Read for &'a UnixStream {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        self.0.read(buf)
-    }
-
-    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        self.0.read_vectored(bufs)
-    }
-
-    #[inline]
-    unsafe fn initializer(&self) -> Initializer {
-        Initializer::nop()
-    }
-}
-
-impl io::Write for UnixStream {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        io::Write::write(&mut &*self, buf)
-    }
-
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        io::Write::write_vectored(&mut &*self, bufs)
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        io::Write::flush(&mut &*self)
-    }
-}
-
-impl<'a> io::Write for &'a UnixStream {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        self.0.write(buf)
-    }
-
-    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        self.0.write_vectored(bufs)
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        Ok(())
-    }
-}
-
-impl AsRawFd for UnixStream {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.0.as_inner()
-    }
-}
-
-impl FromRawFd for UnixStream {
-    unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
-        UnixStream(Socket::from_inner(fd))
-    }
-}
-
-impl IntoRawFd for UnixStream {
-    fn into_raw_fd(self) -> RawFd {
-        self.0.into_inner()
-    }
-}
-
-impl AsRawFd for net::TcpStream {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.as_inner().socket().as_inner()
-    }
-}
-
-impl AsRawFd for net::TcpListener {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.as_inner().socket().as_inner()
-    }
-}
-
-impl AsRawFd for net::UdpSocket {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.as_inner().socket().as_inner()
-    }
-}
-
-impl FromRawFd for net::TcpStream {
-    unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
-        let socket = sys::net::Socket::from_inner(fd);
-        net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
-    }
-}
-
-impl FromRawFd for net::TcpListener {
-    unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
-        let socket = sys::net::Socket::from_inner(fd);
-        net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
-    }
-}
-
-impl FromRawFd for net::UdpSocket {
-    unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
-        let socket = sys::net::Socket::from_inner(fd);
-        net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
-    }
-}
-
-impl IntoRawFd for net::TcpStream {
-    fn into_raw_fd(self) -> RawFd {
-        self.into_inner().into_socket().into_inner()
-    }
-}
-
-impl IntoRawFd for net::TcpListener {
-    fn into_raw_fd(self) -> RawFd {
-        self.into_inner().into_socket().into_inner()
-    }
-}
-
-impl IntoRawFd for net::UdpSocket {
-    fn into_raw_fd(self) -> RawFd {
-        self.into_inner().into_socket().into_inner()
-    }
-}
-
-/// A structure representing a Unix domain socket server.
-///
-pub struct UnixListener(Socket);
-
-impl fmt::Debug for UnixListener {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let mut builder = fmt.debug_struct("UnixListener");
-        builder.field("fd", self.0.as_inner());
-        if let Ok(addr) = self.local_addr() {
-            builder.field("local", &addr);
-        }
-        builder.finish()
-    }
-}
-
-impl UnixListener {
-    /// Creates a new `UnixListener` bound to the specified socket.
-    ///
-    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
-        fn inner(path: &Path) -> io::Result<UnixListener> {
-            unsafe {
-                let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
-                let (addr, len) = sockaddr_un(path)?;
-
-                cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
-                cvt(libc::listen(*inner.as_inner(), 128))?;
-
-                Ok(UnixListener(inner))
-            }
-        }
-        inner(path.as_ref())
-    }
-
-    /// Accepts a new incoming connection to this listener.
-    ///
-    /// This function will block the calling thread until a new Unix connection
-    /// is established. When established, the corresponding [`UnixStream`] and
-    /// the remote peer's address will be returned.
-    ///
-    pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
-        let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
-        let mut len = mem::size_of_val(&storage) as libc::socklen_t;
-        let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
-        let addr = SocketAddr::from_parts(storage, len)?;
-        Ok((UnixStream(sock), addr))
-    }
-
-    /// Creates a new independently owned handle to the underlying socket.
-    ///
-    /// The returned `UnixListener` is a reference to the same socket that this
-    /// object references. Both handles can be used to accept incoming
-    /// connections and options set on one listener will affect the other.
-    ///
-    pub fn try_clone(&self) -> io::Result<UnixListener> {
-        self.0.duplicate().map(UnixListener)
-    }
-
-    /// Returns the local socket address of this listener.
-    ///
-    pub fn local_addr(&self) -> io::Result<SocketAddr> {
-        SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
-    }
-
-    /// Moves the socket into or out of nonblocking mode.
-    ///
-    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
-        self.0.set_nonblocking(nonblocking)
-    }
-
-    /// Returns the value of the `SO_ERROR` option.
-    ///
-    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        self.0.take_error()
-    }
-
-    /// Returns an iterator over incoming connections.
-    ///
-    /// The iterator will never return [`None`] and will also not yield the
-    /// peer's [`SocketAddr`] structure.
-    ///
-    /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-    /// [`SocketAddr`]: struct.SocketAddr.html
-    ///
-    pub fn incoming(&self) -> Incoming<'_> {
-        Incoming { listener: self }
-    }
-}
-
-impl AsRawFd for UnixListener {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.0.as_inner()
-    }
-}
-
-impl FromRawFd for UnixListener {
-    unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
-        UnixListener(Socket::from_inner(fd))
-    }
-}
-
-impl IntoRawFd for UnixListener {
-    fn into_raw_fd(self) -> RawFd {
-        self.0.into_inner()
-    }
-}
-
-impl<'a> IntoIterator for &'a UnixListener {
-    type Item = io::Result<UnixStream>;
-    type IntoIter = Incoming<'a>;
-
-    fn into_iter(self) -> Incoming<'a> {
-        self.incoming()
-    }
-}
-
-/// An iterator over incoming connections to a [`UnixListener`].
-///
-/// It will never return [`None`].
-///
-/// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-/// [`UnixListener`]: struct.UnixListener.html
-///
-#[derive(Debug)]
-pub struct Incoming<'a> {
-    listener: &'a UnixListener,
-}
-
-impl<'a> Iterator for Incoming<'a> {
-    type Item = io::Result<UnixStream>;
-
-    fn next(&mut self) -> Option<io::Result<UnixStream>> {
-        Some(self.listener.accept().map(|s| s.0))
-    }
-
-    fn size_hint(&self) -> (usize, Option<usize>) {
-        (usize::max_value(), None)
-    }
-}
-
-/// A Unix datagram socket.
-///
-pub struct UnixDatagram(Socket);
-
-impl fmt::Debug for UnixDatagram {
-    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
-        let mut builder = fmt.debug_struct("UnixDatagram");
-        builder.field("fd", self.0.as_inner());
-        if let Ok(addr) = self.local_addr() {
-            builder.field("local", &addr);
-        }
-        if let Ok(addr) = self.peer_addr() {
-            builder.field("peer", &addr);
-        }
-        builder.finish()
-    }
-}
-
-impl UnixDatagram {
-    /// Creates a Unix datagram socket bound to the given path.
-    ///
-    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
-        fn inner(path: &Path) -> io::Result<UnixDatagram> {
-            unsafe {
-                let socket = UnixDatagram::unbound()?;
-                let (addr, len) = sockaddr_un(path)?;
-
-                cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
-
-                Ok(socket)
-            }
-        }
-        inner(path.as_ref())
-    }
-
-    /// Creates a Unix Datagram socket which is not bound to any address.
-    ///
-    pub fn unbound() -> io::Result<UnixDatagram> {
-        let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
-        Ok(UnixDatagram(inner))
-    }
-
-    /// Creates an unnamed pair of connected sockets.
-    ///
-    /// Returns two `UnixDatagrams`s which are connected to each other.
-    ///
-    pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
-        let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
-        Ok((UnixDatagram(i1), UnixDatagram(i2)))
-    }
-
-    /// Connects the socket to the specified address.
-    ///
-    /// The [`send`] method may be used to send data to the specified address.
-    /// [`recv`] and [`recv_from`] will only receive data from that address.
-    ///
-    /// [`send`]: #method.send
-    /// [`recv`]: #method.recv
-    /// [`recv_from`]: #method.recv_from
-    ///
-    pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
-        fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
-            unsafe {
-                let (addr, len) = sockaddr_un(path)?;
-
-                cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
-
-                Ok(())
-            }
-        }
-        inner(self, path.as_ref())
-    }
-
-    /// Creates a new independently owned handle to the underlying socket.
-    ///
-    /// The returned `UnixDatagram` is a reference to the same socket that this
-    /// object references. Both handles can be used to accept incoming
-    /// connections and options set on one side will affect the other.
-    ///
-    pub fn try_clone(&self) -> io::Result<UnixDatagram> {
-        self.0.duplicate().map(UnixDatagram)
-    }
-
-    /// Returns the address of this socket.
-    ///
-    pub fn local_addr(&self) -> io::Result<SocketAddr> {
-        SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
-    }
-
-    /// Returns the address of this socket's peer.
-    ///
-    /// The [`connect`] method will connect the socket to a peer.
-    ///
-    /// [`connect`]: #method.connect
-    ///
-    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
-        SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
-    }
-
-    /// Receives data from the socket.
-    ///
-    /// On success, returns the number of bytes read and the address from
-    /// whence the data came.
-    ///
-    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
-        let mut count = 0;
-        let addr = SocketAddr::new(|addr, len| unsafe {
-            count = libc::recvfrom(
-                *self.0.as_inner(),
-                buf.as_mut_ptr() as *mut _,
-                buf.len(),
-                0,
-                addr,
-                len,
-            );
-            if count > 0 {
-                1
-            } else if count == 0 {
-                0
-            } else {
-                -1
-            }
-        })?;
-
-        Ok((count as usize, addr))
-    }
-
-    /// Receives data from the socket.
-    ///
-    /// On success, returns the number of bytes read.
-    ///
-    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
-        self.0.read(buf)
-    }
-
-    /// Sends data on the socket to the specified address.
-    ///
-    /// On success, returns the number of bytes written.
-    ///
-    pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
-        fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
-            unsafe {
-                let (addr, len) = sockaddr_un(path)?;
-
-                let count = cvt(libc::sendto(
-                    *d.0.as_inner(),
-                    buf.as_ptr() as *const _,
-                    buf.len(),
-                    libc::MSG_NOSIGNAL,
-                    &addr as *const _ as *const _,
-                    len,
-                ))?;
-                Ok(count as usize)
-            }
-        }
-        inner(self, buf, path.as_ref())
-    }
-
-    /// Sends data on the socket to the socket's peer.
-    ///
-    /// The peer address may be set by the `connect` method, and this method
-    /// will return an error if the socket has not already been connected.
-    ///
-    /// On success, returns the number of bytes written.
-    ///
-    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
-        self.0.write(buf)
-    }
-
-    /// Sets the read timeout for the socket.
-    ///
-    /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
-    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
-    /// is passed to this method.
-    ///
-    /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-    /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
-    /// [`recv`]: #method.recv
-    /// [`recv_from`]: #method.recv_from
-    /// [`Duration`]: ../../../../std/time/struct.Duration.html
-    ///
-    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
-        self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
-    }
-
-    /// Sets the write timeout for the socket.
-    ///
-    /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
-    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
-    /// method.
-    ///
-    /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
-    /// [`send`]: #method.send
-    /// [`send_to`]: #method.send_to
-    /// [`Duration`]: ../../../../std/time/struct.Duration.html
-    ///
-    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
-        self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
-    }
-
-    /// Returns the read timeout of this socket.
-    ///
-    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        self.0.timeout(libc::SO_RCVTIMEO)
-    }
-
-    /// Returns the write timeout of this socket.
-    ///
-    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        self.0.timeout(libc::SO_SNDTIMEO)
-    }
-
-    /// Moves the socket into or out of nonblocking mode.
-    ///
-    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
-        self.0.set_nonblocking(nonblocking)
-    }
-
-    /// Returns the value of the `SO_ERROR` option.
-    ///
-    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
-        self.0.take_error()
-    }
-
-    /// Shut down the read, write, or both halves of this connection.
-    ///
-    /// This function will cause all pending and future I/O calls on the
-    /// specified portions to immediately return with an appropriate value
-    /// (see the documentation of [`Shutdown`]).
-    ///
-    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
-        self.0.shutdown(how)
-    }
-}
-
-impl AsRawFd for UnixDatagram {
-    fn as_raw_fd(&self) -> RawFd {
-        *self.0.as_inner()
-    }
-}
-
-impl FromRawFd for UnixDatagram {
-    unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
-        UnixDatagram(Socket::from_inner(fd))
-    }
-}
-
-impl IntoRawFd for UnixDatagram {
-    fn into_raw_fd(self) -> RawFd {
-        self.0.into_inner()
-    }
-}
-
-mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{connect, listen, bind, sendto, recvfrom, getsockname, getpeername};
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/fd.rs b/sgx_tstd/src/sys/fd.rs
index 430e86f..cac6135 100644
--- a/sgx_tstd/src/sys/fd.rs
+++ b/sgx_tstd/src/sys/fd.rs
@@ -15,54 +15,31 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc::{c_int, c_void, ssize_t};
-use core::cmp;
-use core::mem;
-use core::sync::atomic::{AtomicBool, Ordering};
+use crate::cmp;
 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
 use crate::sys::cvt;
-use crate::sys_common::AsInner;
+use crate::sys_common::{AsInner, FromInner, IntoInner};
+
+use sgx_libc::{c_int, c_void};
 
 #[derive(Debug)]
-pub struct FileDesc {
-    fd: c_int,
-}
+pub struct FileDesc(OwnedFd);
 
-fn max_len() -> usize {
-    // The maximum read limit on most posix-like systems is `SSIZE_MAX`,
-    // with the man page quoting that if the count of bytes to read is
-    // greater than `SSIZE_MAX` the result is "unspecified".
-    //
-    // On macOS, however, apparently the 64-bit libc is either buggy or
-    // intentionally showing odd behavior by rejecting any read with a size
-    // larger than or equal to INT_MAX. To handle both of these the read
-    // size is capped on both platforms.
-    if cfg!(target_os = "macos") {
-        <c_int>::max_value() as usize - 1
-    } else {
-        <ssize_t>::max_value() as usize
-    }
+const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
+
+const fn max_iov() -> usize {
+    sgx_libc::UIO_MAXIOV as usize
 }
 
 impl FileDesc {
-    pub fn new(fd: c_int) -> FileDesc {
-        FileDesc { fd }
-    }
-
-    pub fn raw(&self) -> c_int {
-        self.fd
-    }
-
-    /// Extracts the actual file descriptor without closing it.
-    pub fn into_raw(self) -> c_int {
-        let fd = self.fd;
-        mem::forget(self);
-        fd
-    }
-
     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
         let ret = cvt(unsafe {
-            libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
+            libc::read(
+                self.as_raw_fd(),
+                buf.as_mut_ptr() as *mut c_void,
+                cmp::min(buf.len(), READ_LIMIT),
+            )
         })?;
         Ok(ret as usize)
     }
@@ -70,14 +47,19 @@
     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
         let ret = cvt(unsafe {
             libc::readv(
-                self.fd,
+                self.as_raw_fd(),
                 bufs.as_ptr() as *const libc::iovec,
-                cmp::min(bufs.len(), c_int::max_value() as usize) as c_int,
+                cmp::min(bufs.len(), max_iov()) as c_int,
             )
         })?;
         Ok(ret as usize)
     }
 
+    #[inline]
+    pub fn is_read_vectored(&self) -> bool {
+        true
+    }
+
     pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
         let mut me = self;
         (&mut me).read_to_end(buf)
@@ -96,9 +78,9 @@
 
         unsafe {
             cvt_pread64(
-                self.fd,
+                self.as_raw_fd(),
                 buf.as_mut_ptr() as *mut c_void,
-                cmp::min(buf.len(), max_len()),
+                cmp::min(buf.len(), READ_LIMIT),
                 offset as i64,
             )
             .map(|n| n as usize)
@@ -107,7 +89,11 @@
 
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
         let ret = cvt(unsafe {
-            libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
+            libc::write(
+                self.as_raw_fd(),
+                buf.as_ptr() as *const c_void,
+                cmp::min(buf.len(), READ_LIMIT),
+            )
         })?;
         Ok(ret as usize)
     }
@@ -115,14 +101,19 @@
     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
         let ret = cvt(unsafe {
             libc::writev(
-                self.fd,
+                self.as_raw_fd(),
                 bufs.as_ptr() as *const libc::iovec,
-                cmp::min(bufs.len(), c_int::max_value() as usize) as c_int,
+                cmp::min(bufs.len(), max_iov()) as c_int,
             )
         })?;
         Ok(ret as usize)
     }
 
+    #[inline]
+    pub fn is_write_vectored(&self) -> bool {
+        true
+    }
+
     pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
         unsafe fn cvt_pwrite64(
             fd: c_int,
@@ -136,9 +127,9 @@
 
         unsafe {
             cvt_pwrite64(
-                self.fd,
+                self.as_raw_fd(),
                 buf.as_ptr() as *const c_void,
-                cmp::min(buf.len(), max_len()),
+                cmp::min(buf.len(), READ_LIMIT),
                 offset as i64,
             )
             .map(|n| n as usize)
@@ -146,15 +137,15 @@
     }
 
     pub fn get_cloexec(&self) -> io::Result<bool> {
-        unsafe { Ok((cvt(libc::fcntl_arg0(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
+        unsafe { Ok((cvt(libc::fcntl_arg0(self.as_raw_fd(), libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
     }
 
     pub fn set_cloexec(&self) -> io::Result<()> {
         unsafe {
-            let previous = cvt(libc::fcntl_arg0(self.fd, libc::F_GETFD))?;
+            let previous = cvt(libc::fcntl_arg0(self.as_raw_fd(), libc::F_GETFD))?;
             let new = previous | libc::FD_CLOEXEC;
             if new != previous {
-                cvt(libc::fcntl_arg1(self.fd, libc::F_SETFD, new))?;
+                cvt(libc::fcntl_arg1(self.as_raw_fd(), libc::F_SETFD, new))?;
             }
             Ok(())
         }
@@ -163,7 +154,7 @@
     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
         unsafe {
             let mut v = nonblocking as c_int;
-            cvt(libc::ioctl_arg1(self.fd, libc::FIONBIO, &mut v as *mut c_int))?;
+            cvt(libc::ioctl_arg1(self.as_raw_fd(), libc::FIONBIO, &mut v as *mut c_int))?;
             Ok(())
         }
     }
@@ -171,47 +162,9 @@
     pub fn duplicate(&self) -> io::Result<FileDesc> {
         // We want to atomically duplicate this file descriptor and set the
         // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
-        // flag, however, isn't supported on older Linux kernels (earlier than
-        // 2.6.24).
-        //
-        // To detect this and ensure that CLOEXEC is still set, we
-        // follow a strategy similar to musl [1] where if passing
-        // F_DUPFD_CLOEXEC causes `fcntl` to return EINVAL it means it's not
-        // supported (the third parameter, 0, is always valid), so we stop
-        // trying that.
-        //
-        // Also note that Android doesn't have F_DUPFD_CLOEXEC, but get it to
-        // resolve so we at least compile this.
-        //
-        // [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
-        use libc::F_DUPFD_CLOEXEC;
-
-        let make_filedesc = |fd| {
-            let fd = FileDesc::new(fd);
-            fd.set_cloexec()?;
-            Ok(fd)
-        };
-        static TRY_CLOEXEC: AtomicBool = AtomicBool::new(!cfg!(target_os = "android"));
-        let fd = self.raw();
-        if TRY_CLOEXEC.load(Ordering::Relaxed) {
-            match cvt(unsafe { libc::fcntl_arg1(fd, F_DUPFD_CLOEXEC, 0) }) {
-                // We *still* call the `set_cloexec` method as apparently some
-                // linux kernel at some point stopped setting CLOEXEC even
-                // though it reported doing so on F_DUPFD_CLOEXEC.
-                Ok(fd) => {
-                    return Ok(if cfg!(target_os = "linux") {
-                        make_filedesc(fd)?
-                    } else {
-                        FileDesc::new(fd)
-                    });
-                }
-                Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
-                    TRY_CLOEXEC.store(false, Ordering::Relaxed);
-                }
-                Err(e) => return Err(e),
-            }
-        }
-        cvt(unsafe { libc::fcntl_arg1(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
+        // is a POSIX flag that was added to Linux in 2.6.24.
+        let fd = cvt(unsafe { libc::fcntl_arg1(self.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 0) })?;
+        Ok(unsafe { FileDesc::from_raw_fd(fd) })
     }
 }
 
@@ -226,26 +179,52 @@
     }
 }
 
-impl AsInner<c_int> for FileDesc {
-    fn as_inner(&self) -> &c_int {
-        &self.fd
+impl AsInner<OwnedFd> for FileDesc {
+    fn as_inner(&self) -> &OwnedFd {
+        &self.0
     }
 }
 
-impl Drop for FileDesc {
-    fn drop(&mut self) {
-        // Note that errors are ignored when closing a file descriptor. The
-        // reason for this is that if an error occurs we don't actually know if
-        // the file descriptor was closed or not, and if we retried (for
-        // something like EINTR), we might close another valid file descriptor
-        // opened after we closed ours.
-        let _ = unsafe { libc::close(self.fd) };
+impl IntoInner<OwnedFd> for FileDesc {
+    fn into_inner(self) -> OwnedFd {
+        self.0
+    }
+}
+
+impl FromInner<OwnedFd> for FileDesc {
+    fn from_inner(owned_fd: OwnedFd) -> Self {
+        Self(owned_fd)
+    }
+}
+
+impl AsFd for FileDesc {
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_fd()
+    }
+}
+
+impl AsRawFd for FileDesc {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_raw_fd()
+    }
+}
+
+impl IntoRawFd for FileDesc {
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_raw_fd()
+    }
+}
+
+impl FromRawFd for FileDesc {
+    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
+        Self(FromRawFd::from_raw_fd(raw_fd))
     }
 }
 
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{read, pread64, write, pwrite64, readv, writev,
-                                    fcntl_arg0, fcntl_arg1, ioctl_arg0, ioctl_arg1,
-                                    close};
+    pub use sgx_libc::ocall::{
+        close, fcntl_arg0, fcntl_arg1, ioctl_arg0, ioctl_arg1, pread64, pwrite64, read, readv,
+        write, writev,
+    };
+    pub use sgx_libc::*;
 }
diff --git a/sgx_tstd/src/sys/fs.rs b/sgx_tstd/src/sys/fs.rs
index d701810..3e8daf3 100644
--- a/sgx_tstd/src/sys/fs.rs
+++ b/sgx_tstd/src/sys/fs.rs
@@ -15,19 +15,25 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc::{c_int, mode_t, time_t, stat64, off64_t, DIR, dirent64};
 use crate::os::unix::prelude::*;
+
 use crate::ffi::{CStr, CString, OsStr, OsString};
-use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
+use crate::fmt;
+use crate::io::{self, Error, IoSlice, IoSliceMut, SeekFrom};
+use crate::mem;
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
 use crate::path::{Path, PathBuf};
+use crate::ptr;
+use crate::sync::Arc;
 use crate::sys::fd::FileDesc;
 use crate::sys::time::SystemTime;
 use crate::sys::{cvt, cvt_r};
-use crate::sys_common::{AsInner, FromInner};
-use core::{fmt, mem, ptr};
-use alloc_crate::sync::Arc;
+use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
+use crate::untrusted::fs;
 
-pub use crate::sys_common::fs::remove_dir_all;
+use sgx_libc::{c_int, dirent64, mode_t, off64_t, stat64};
+
+pub use crate::sys_common::fs::{remove_dir_all, try_exists};
 
 pub struct File(FileDesc);
 
@@ -36,31 +42,25 @@
     stat: stat64,
 }
 
-#[derive(Debug)]
-pub struct DirBuilder { 
-    mode: mode_t,
- }
-
 // all DirEntry's will have a reference to this struct
 struct InnerReadDir {
     dirp: Dir,
     root: PathBuf,
 }
 
-#[derive(Clone)]
 pub struct ReadDir {
     inner: Arc<InnerReadDir>,
     end_of_stream: bool,
 }
 
-struct Dir(*mut DIR);
+struct Dir(*mut libc::DIR);
 
 unsafe impl Send for Dir {}
 unsafe impl Sync for Dir {}
 
 pub struct DirEntry {
     entry: dirent64,
-    dir: ReadDir,
+    dir: Arc<InnerReadDir>,
 }
 
 #[derive(Clone, Debug)]
@@ -87,6 +87,11 @@
     mode: mode_t,
 }
 
+#[derive(Debug)]
+pub struct DirBuilder {
+    mode: mode_t,
+}
+
 impl FileAttr {
     fn from_stat64(stat: stat64) -> Self {
         Self { stat }
@@ -97,6 +102,7 @@
     pub fn size(&self) -> u64 {
         self.stat.st_size as u64
     }
+
     pub fn perm(&self) -> FilePermissions {
         FilePermissions { mode: (self.stat.st_mode as mode_t) }
     }
@@ -109,22 +115,22 @@
 impl FileAttr {
     pub fn modified(&self) -> io::Result<SystemTime> {
         Ok(SystemTime::from(libc::timespec {
-            tv_sec: self.stat.st_mtime as time_t,
+            tv_sec: self.stat.st_mtime as libc::time_t,
             tv_nsec: self.stat.st_mtime_nsec as _,
         }))
     }
 
     pub fn accessed(&self) -> io::Result<SystemTime> {
         Ok(SystemTime::from(libc::timespec {
-            tv_sec: self.stat.st_atime as time_t,
+            tv_sec: self.stat.st_atime as libc::time_t,
             tv_nsec: self.stat.st_atime_nsec as _,
         }))
     }
 
     pub fn created(&self) -> io::Result<SystemTime> {
-        Err(io::Error::new(
-            io::ErrorKind::Other,
-            "creation time is not available on this platform \
+        Err(io::Error::new_const(
+            io::ErrorKind::Unsupported,
+            &"creation time is not available on this platform \
                             currently",
         ))
     }
@@ -195,10 +201,11 @@
         }
 
         unsafe {
-            let mut ret = DirEntry { entry: mem::zeroed(), dir: self.clone() };
+            let mut ret = DirEntry { entry: mem::zeroed(), dir: Arc::clone(&self.inner) };
             let mut entry_ptr = ptr::null_mut();
             loop {
-                if libc::readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
+                let err = libc::readdir64_r(self.inner.dirp.0, &mut ret.entry, &mut entry_ptr);
+                if err != 0 {
                     if entry_ptr.is_null() {
                         // We encountered an error (which will be returned in this iteration), but
                         // we also reached the end of the directory stream. The `end_of_stream`
@@ -206,7 +213,7 @@
                         // (instead of looping forever)
                         self.end_of_stream = true;
                     }
-                    return Some(Err(Error::last_os_error()));
+                    return Some(Err(Error::from_raw_os_error(err)));
                 }
                 if entry_ptr.is_null() {
                     return None;
@@ -228,7 +235,7 @@
 
 impl DirEntry {
     pub fn path(&self) -> PathBuf {
-        self.dir.inner.root.join(OsStr::from_bytes(self.name_bytes()))
+        self.dir.root.join(OsStr::from_bytes(self.name_bytes()))
     }
 
     pub fn file_name(&self) -> OsString {
@@ -236,12 +243,11 @@
     }
 
     pub fn metadata(&self) -> io::Result<FileAttr> {
-        let fd = cvt(unsafe {libc::dirfd(self.dir.inner.dirp.0)})?;
+        let fd = cvt(unsafe { libc::dirfd(self.dir.dirp.0) })?;
+        let name = self.entry.d_name.as_ptr();
         let mut stat: stat64 = unsafe { mem::zeroed() };
-        cvt(unsafe {
-            libc::fstatat64(fd, self.entry.d_name.as_ptr(), &mut stat, libc::AT_SYMLINK_NOFOLLOW)
-        })?;
-        Ok(FileAttr { stat })
+        cvt(unsafe { libc::fstatat64(fd, name, &mut stat, libc::AT_SYMLINK_NOFOLLOW) })?;
+        Ok(FileAttr::from_stat64(stat))
     }
 
     pub fn file_type(&self) -> io::Result<FileType> {
@@ -264,6 +270,10 @@
     fn name_bytes(&self) -> &[u8] {
         unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes() }
     }
+
+    pub fn file_name_os_str(&self) -> &OsStr {
+        OsStr::from_bytes(self.name_bytes())
+    }
 }
 
 impl OpenOptions {
@@ -310,11 +320,11 @@
 
     fn get_access_mode(&self) -> io::Result<c_int> {
         match (self.read, self.write, self.append) {
-            (true,  false, false) => Ok(libc::O_RDONLY),
-            (false, true,  false) => Ok(libc::O_WRONLY),
-            (true,  true,  false) => Ok(libc::O_RDWR),
-            (false, _,     true)  => Ok(libc::O_WRONLY | libc::O_APPEND),
-            (true,  _,     true)  => Ok(libc::O_RDWR | libc::O_APPEND),
+            (true, false, false) => Ok(libc::O_RDONLY),
+            (false, true, false) => Ok(libc::O_WRONLY),
+            (true, true, false) => Ok(libc::O_RDWR),
+            (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND),
+            (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND),
             (false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)),
         }
     }
@@ -335,12 +345,12 @@
         }
 
         Ok(match (self.create, self.truncate, self.create_new) {
-                (false, false, false) => 0,
-                (true,  false, false) => libc::O_CREAT,
-                (false, true,  false) => libc::O_TRUNC,
-                (true,  true,  false) => libc::O_CREAT | libc::O_TRUNC,
-                (_,      _,    true)  => libc::O_CREAT | libc::O_EXCL,
-           })
+            (false, false, false) => 0,
+            (true, false, false) => libc::O_CREAT,
+            (false, true, false) => libc::O_TRUNC,
+            (true, true, false) => libc::O_CREAT | libc::O_TRUNC,
+            (_, _, true) => libc::O_CREAT | libc::O_EXCL,
+        })
     }
 }
 
@@ -355,66 +365,32 @@
             | opts.get_access_mode()?
             | opts.get_creation_mode()?
             | (opts.custom_flags as c_int & !libc::O_ACCMODE);
+        // The third argument of `open64` is documented to have type `mode_t`. On
+        // some platforms (like macOS, where `open64` is actually `open`), `mode_t` is `u16`.
+        // However, since this is a variadic function, C integer promotion rules mean that on
+        // the ABI level, this still gets passed as `c_int` (aka `u32` on Unix platforms).
         let fd = cvt_r(|| unsafe { libc::open64(path.as_ptr(), flags, opts.mode as c_int) })?;
-        let fd = FileDesc::new(fd);
-
-        // Currently the standard library supports Linux 2.6.18 which did not
-        // have the O_CLOEXEC flag (passed above). If we're running on an older
-        // Linux kernel then the flag is just ignored by the OS. After we open
-        // the first file, we check whether it has CLOEXEC set. If it doesn't,
-        // we will explicitly ask for a CLOEXEC fd for every further file we
-        // open, if it does, we will skip that step.
-        //
-        // The CLOEXEC flag, however, is supported on versions of macOS/BSD/etc
-        // that we support, so we only do this on Linux currently.
-        fn ensure_cloexec(fd: &FileDesc) -> io::Result<()> {
-            use crate::sync::atomic::{AtomicUsize, Ordering};
-
-            const OPEN_CLOEXEC_UNKNOWN: usize = 0;
-            const OPEN_CLOEXEC_SUPPORTED: usize = 1;
-            const OPEN_CLOEXEC_NOTSUPPORTED: usize = 2;
-            static OPEN_CLOEXEC: AtomicUsize = AtomicUsize::new(OPEN_CLOEXEC_UNKNOWN);
-
-            let need_to_set;
-            match OPEN_CLOEXEC.load(Ordering::Relaxed) {
-                OPEN_CLOEXEC_UNKNOWN => {
-                    need_to_set = !fd.get_cloexec()?;
-                    OPEN_CLOEXEC.store(
-                        if need_to_set {
-                            OPEN_CLOEXEC_NOTSUPPORTED
-                        } else {
-                            OPEN_CLOEXEC_SUPPORTED
-                        },
-                        Ordering::Relaxed,
-                    );
-                }
-                OPEN_CLOEXEC_SUPPORTED => need_to_set = false,
-                OPEN_CLOEXEC_NOTSUPPORTED => need_to_set = true,
-                _ => unreachable!(),
-            }
-            if need_to_set {
-                fd.set_cloexec()?;
-            }
-            Ok(())
-        }
-
-        ensure_cloexec(&fd)?;
-        Ok(File(fd))
+        Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
     }
 
     pub fn file_attr(&self) -> io::Result<FileAttr> {
+        let fd = self.as_raw_fd();
         let mut stat: stat64 = unsafe { mem::zeroed() };
-        cvt(unsafe { libc::fstat64(self.0.raw(), &mut stat) })?;
+        cvt(unsafe { libc::fstat64(fd, &mut stat) })?;
         Ok(FileAttr::from_stat64(stat))
     }
 
     pub fn fsync(&self) -> io::Result<()> {
-        cvt_r(|| unsafe { libc::fsync(self.0.raw()) })?;
-        Ok(())
+        cvt_r(|| unsafe { os_fsync(self.as_raw_fd()) })?;
+        return Ok(());
+
+        unsafe fn os_fsync(fd: c_int) -> c_int {
+            libc::fsync(fd)
+        }
     }
 
     pub fn datasync(&self) -> io::Result<()> {
-        cvt_r(|| unsafe { os_datasync(self.0.raw()) })?;
+        cvt_r(|| unsafe { os_datasync(self.as_raw_fd()) })?;
         return Ok(());
 
         unsafe fn os_datasync(fd: c_int) -> c_int {
@@ -424,9 +400,9 @@
 
     pub fn truncate(&self, size: u64) -> io::Result<()> {
         use crate::convert::TryInto;
-            let size: off64_t =
+        let size: off64_t =
                 size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
-            cvt_r(|| unsafe { libc::ftruncate64(self.0.raw(), size) }).map(drop)
+        cvt_r(|| unsafe { libc::ftruncate64(self.as_raw_fd(), size) }).map(drop)
     }
 
     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
@@ -437,6 +413,11 @@
         self.0.read_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
     pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
         self.0.read_at(buf, offset)
     }
@@ -449,6 +430,11 @@
         self.0.write_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
     pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
         self.0.write_at(buf, offset)
     }
@@ -466,7 +452,7 @@
             SeekFrom::Current(off) => (libc::SEEK_CUR, off),
         };
 
-        let n = cvt(unsafe { libc::lseek64(self.0.raw(), pos, whence) })?;
+        let n = cvt(unsafe { libc::lseek64(self.as_raw_fd(), pos, whence) })?;
         Ok(n as u64)
     }
 
@@ -474,16 +460,8 @@
         self.0.duplicate().map(File)
     }
 
-    pub fn fd(&self) -> &FileDesc {
-        &self.0
-    }
-
-    pub fn into_fd(self) -> FileDesc {
-        self.0
-    }
-
     pub fn set_permissions(&self, perm: FilePermissions) -> io::Result<()> {
-        cvt_r(|| unsafe { libc::fchmod(self.0.raw(), perm.mode) })?;
+        cvt_r(|| unsafe { libc::fchmod(self.as_raw_fd(), perm.mode) })?;
         Ok(())
     }
 }
@@ -508,9 +486,51 @@
     Ok(CString::new(path.as_os_str().as_bytes())?)
 }
 
-impl FromInner<c_int> for File {
-    fn from_inner(fd: c_int) -> File {
-        File(FileDesc::new(fd))
+impl AsInner<FileDesc> for File {
+    fn as_inner(&self) -> &FileDesc {
+        &self.0
+    }
+}
+
+impl AsInnerMut<FileDesc> for File {
+    fn as_inner_mut(&mut self) -> &mut FileDesc {
+        &mut self.0
+    }
+}
+
+impl IntoInner<FileDesc> for File {
+    fn into_inner(self) -> FileDesc {
+        self.0
+    }
+}
+
+impl FromInner<FileDesc> for File {
+    fn from_inner(file_desc: FileDesc) -> Self {
+        Self(file_desc)
+    }
+}
+
+impl AsFd for File {
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_fd()
+    }
+}
+
+impl AsRawFd for File {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_raw_fd()
+    }
+}
+
+impl IntoRawFd for File {
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_raw_fd()
+    }
+}
+
+impl FromRawFd for File {
+    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
+        Self(FromRawFd::from_raw_fd(raw_fd))
     }
 }
 
@@ -535,7 +555,7 @@
             }
         }
 
-        let fd = self.0.raw();
+        let fd = self.as_raw_fd();
         let mut b = f.debug_struct("File");
         b.field("fd", &fd);
         if let Some(path) = get_path(fd) {
@@ -614,17 +634,17 @@
     }
 }
 
-pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
-    let src = cstr(src)?;
-    let dst = cstr(dst)?;
-    cvt(unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) })?;
+pub fn symlink(original: &Path, link: &Path) -> io::Result<()> {
+    let original = cstr(original)?;
+    let link = cstr(link)?;
+    cvt(unsafe { libc::symlink(original.as_ptr(), link.as_ptr()) })?;
     Ok(())
 }
 
-pub fn link(src: &Path, dst: &Path) -> io::Result<()> {
-    let src = cstr(src)?;
-    let dst = cstr(dst)?;
-    cvt(unsafe { libc::link(src.as_ptr(), dst.as_ptr()) })?;
+pub fn link(original: &Path, link: &Path) -> io::Result<()> {
+    let original = cstr(original)?;
+    let link = cstr(link)?;
+    cvt(unsafe { libc::linkat(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?;
     Ok(())
 }
 
@@ -656,35 +676,56 @@
     Ok(PathBuf::from(OsString::from_vec(buf)))
 }
 
+fn open_from(from: &Path) -> io::Result<(fs::File, fs::Metadata)> {
+    use crate::untrusted::fs::File;
+    use crate::sys_common::fs::NOT_FILE_ERROR;
+
+    let reader = File::open(from)?;
+    let metadata = reader.metadata()?;
+    if !metadata.is_file() {
+        return Err(NOT_FILE_ERROR);
+    }
+    Ok((reader, metadata))
+}
+
+fn open_to_and_set_permissions(
+    to: &Path,
+    reader_metadata: fs::Metadata,
+) -> io::Result<(fs::File, fs::Metadata)> {
+    let perm = reader_metadata.permissions();
+    let writer = fs::OpenOptions::new()
+        // create the file with the correct mode right away
+        .mode(perm.mode())
+        .write(true)
+        .create(true)
+        .truncate(true)
+        .open(to)?;
+    let writer_metadata = writer.metadata()?;
+    if writer_metadata.is_file() {
+        // Set the correct file permissions, in case the file already existed.
+        // Don't set the permissions on already existing non-files like
+        // pipes/FIFOs or device nodes.
+        writer.set_permissions(perm)?;
+    }
+    Ok((writer, writer_metadata))
+}
+
 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
-    cfg_if! {
-        if #[cfg(feature = "untrusted_fs")] {
-            use crate::fs::File;
-        } else {
-            use crate::untrusted::fs::File;
-            use crate::untrusted::path::PathEx;
-        }
-    }
+    let (mut reader, reader_metadata) = open_from(from)?;
+    let (mut writer, _) = open_to_and_set_permissions(to, reader_metadata)?;
 
-    if !from.is_file() {
-        return Err(Error::new(
-            ErrorKind::InvalidInput,
-            "the source path is not an existing regular file",
-        ));
-    }
+    io::copy::copy(&mut reader, &mut writer)
+}
 
-    let mut reader = File::open(from)?;
-    let mut writer = File::create(to)?;
-    let perm = reader.metadata()?.permissions();
-
-    let ret = io::copy(&mut reader, &mut writer)?;
-    writer.set_permissions(perm)?;
-    Ok(ret)
+pub fn chroot(_dir: &Path) -> io::Result<()> {
+    super::unsupported::unsupported()
 }
 
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{open64, fstat64, fsync, fdatasync, ftruncate64, lseek64, fchmod,
-                                    unlink, link, rename, chmod, readlink, symlink, stat64, lstat64,
-                                    fcntl_arg0, realpath, free, readdir64_r, closedir, dirfd, mkdir, rmdir, opendir, fstatat64};
-}
\ No newline at end of file
+    pub use sgx_libc::ocall::{
+        chmod, closedir, dirfd, fchmod, fcntl_arg0, fdatasync, free, fstat64, fstatat64, fsync,
+        ftruncate64, linkat, lseek64, lstat64, mkdir, open64, opendir, readdir64_r, readlink,
+        realpath, rename, rmdir, stat64, symlink, unlink,
+    };
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/sys/io.rs b/sgx_tstd/src/sys/io.rs
index 3baf6f7..cced5d2 100644
--- a/sgx_tstd/src/sys/io.rs
+++ b/sgx_tstd/src/sys/io.rs
@@ -15,9 +15,10 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc::{iovec, c_void};
-use core::marker::PhantomData;
-use alloc_crate::slice;
+use crate::marker::PhantomData;
+use crate::slice;
+
+use sgx_libc::{c_void, iovec};
 
 #[derive(Copy, Clone)]
 #[repr(transparent)]
diff --git a/sgx_tstd/src/sys/memchr.rs b/sgx_tstd/src/sys/memchr.rs
index aa3ebac..06cd237 100644
--- a/sgx_tstd/src/sys/memchr.rs
+++ b/sgx_tstd/src/sys/memchr.rs
@@ -15,4 +15,4 @@
 // specific language governing permissions and limitations
 // under the License..
 
-pub use sgx_trts::memchr::*;
\ No newline at end of file
+pub use sgx_trts::memchr::*;
diff --git a/sgx_tstd/src/sys/mod.rs b/sgx_tstd/src/sys/mod.rs
index 15c6441..589e18c 100644
--- a/sgx_tstd/src/sys/mod.rs
+++ b/sgx_tstd/src/sys/mod.rs
@@ -15,69 +15,92 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc;
-use sgx_trts::trts;
 use crate::io::ErrorKind;
+use sgx_libc as libc;
+use sgx_trts::trts;
 
 pub use self::rand::hashmap_random_keys;
 
-pub mod mutex;
+#[cfg(feature = "backtrace")]
+pub mod backtrace;
+pub mod cmath;
 pub mod condvar;
-pub mod rwlock;
+pub mod env;
 pub mod fd;
 pub mod fs;
-pub mod sgxfs;
 pub mod io;
+pub mod memchr;
+pub mod mutex;
+#[cfg(feature = "net")]
+pub mod net;
+pub mod os;
+pub mod os_str;
+pub mod path;
+#[cfg(feature = "pipe")]
+pub mod pipe;
+pub mod rand;
+pub mod rwlock;
+pub mod sgxfs;
+#[cfg(feature = "stdio")]
+pub mod stdio;
 #[cfg(feature = "thread")]
 pub mod thread;
 #[cfg(feature = "thread")]
-pub mod fast_thread_local;
+pub mod thread_local_dtor;
 #[cfg(feature = "thread")]
-pub mod thread_local;
-#[cfg(feature = "net")]
-pub mod net;
-pub mod path;
-pub mod ext;
-pub mod rand;
-pub mod os;
-#[cfg(feature = "stdio")]
-pub mod stdio;
-#[cfg(feature = "backtrace")]
-pub mod backtrace;
+pub mod thread_local_key;
 pub mod time;
-pub mod memchr;
-pub mod cmath;
-pub mod env;
-#[cfg(feature = "pipe")]
-pub mod pipe;
-
-pub use crate::sys_common::os_str_bytes as os_str;
 
 pub fn decode_error_kind(errno: i32) -> ErrorKind {
+    use ErrorKind::*;
     match errno as libc::c_int {
-        libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
-        libc::ECONNRESET => ErrorKind::ConnectionReset,
-        libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
-        libc::EPIPE => ErrorKind::BrokenPipe,
-        libc::ENOTCONN => ErrorKind::NotConnected,
-        libc::ECONNABORTED => ErrorKind::ConnectionAborted,
-        libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
-        libc::EADDRINUSE => ErrorKind::AddrInUse,
-        libc::ENOENT => ErrorKind::NotFound,
-        libc::EINTR => ErrorKind::Interrupted,
-        libc::EINVAL => ErrorKind::InvalidInput,
-        libc::ETIMEDOUT => ErrorKind::TimedOut,
-        libc::EEXIST => ErrorKind::AlreadyExists,
+        libc::E2BIG => ArgumentListTooLong,
+        libc::EADDRINUSE => AddrInUse,
+        libc::EADDRNOTAVAIL => AddrNotAvailable,
+        libc::EBUSY => ResourceBusy,
+        libc::ECONNABORTED => ConnectionAborted,
+        libc::ECONNREFUSED => ConnectionRefused,
+        libc::ECONNRESET => ConnectionReset,
+        libc::EDEADLK => Deadlock,
+        libc::EDQUOT => FilesystemQuotaExceeded,
+        libc::EEXIST => AlreadyExists,
+        libc::EFBIG => FileTooLarge,
+        libc::EHOSTUNREACH => HostUnreachable,
+        libc::EINTR => Interrupted,
+        libc::EINVAL => InvalidInput,
+        libc::EISDIR => IsADirectory,
+        libc::ELOOP => FilesystemLoop,
+        libc::ENOENT => NotFound,
+        libc::ENOMEM => OutOfMemory,
+        libc::ENOSPC => StorageFull,
+        libc::ENOSYS => Unsupported,
+        libc::EMLINK => TooManyLinks,
+        libc::ENAMETOOLONG => FilenameTooLong,
+        libc::ENETDOWN => NetworkDown,
+        libc::ENETUNREACH => NetworkUnreachable,
+        libc::ENOTCONN => NotConnected,
+        libc::ENOTDIR => NotADirectory,
+        libc::ENOTEMPTY => DirectoryNotEmpty,
+        libc::EPIPE => BrokenPipe,
+        libc::EROFS => ReadOnlyFilesystem,
+        libc::ESPIPE => NotSeekable,
+        libc::ESTALE => StaleNetworkFileHandle,
+        libc::ETIMEDOUT => TimedOut,
+        libc::ETXTBSY => ExecutableFileBusy,
+        libc::EXDEV => CrossesDevices,
+
+        libc::EACCES | libc::EPERM => PermissionDenied,
 
         // These two constants can have the same value on some systems,
         // but different values on others, so we can't use a match
         // clause
-        x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => ErrorKind::WouldBlock,
+        x if x == libc::EAGAIN || x == libc::EWOULDBLOCK => WouldBlock,
 
-        _ => ErrorKind::Other,
+        _ => Uncategorized,
     }
 }
 
+#[doc(hidden)]
 pub trait IsMinusOne {
     fn is_minus_one(&self) -> bool;
 }
@@ -109,6 +132,25 @@
     }
 }
 
-pub unsafe fn abort_internal() -> ! {
+pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> {
+    if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) }
+}
+
+pub fn abort_internal() -> ! {
     trts::rsgx_abort()
 }
+
+pub mod unsupported {
+    use crate::io;
+
+    pub fn unsupported<T>() -> io::Result<T> {
+        Err(unsupported_err())
+    }
+
+    pub fn unsupported_err() -> io::Error {
+        io::Error::new_const(
+            io::ErrorKind::Unsupported,
+            &"operation not supported on this platform",
+        )
+    }
+}
diff --git a/sgx_tstd/src/sys/mutex.rs b/sgx_tstd/src/sys/mutex.rs
index 6fcb7f4..b88b100 100644
--- a/sgx_tstd/src/sys/mutex.rs
+++ b/sgx_tstd/src/sys/mutex.rs
@@ -15,18 +15,20 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use alloc_crate::collections::LinkedList;
-use core::cell::UnsafeCell;
-use core::cmp;
-use core::ptr;
+use crate::boxed::Box;
+use crate::cell::UnsafeCell;
+use crate::cmp;
+use crate::collections::LinkedList;
+use crate::ptr;
 use crate::sync::SgxThreadSpinlock;
 use crate::thread::rsgx_thread_self;
 use crate::time::Duration;
 use crate::u64;
+
+use sgx_libc as libc;
 use sgx_libc::{c_int, c_long, c_void, time_t, timespec};
 use sgx_trts::enclave::SgxThreadData;
 use sgx_trts::error::set_errno;
-use sgx_trts::libc;
 use sgx_types::{self, sgx_status_t, sgx_thread_t, SysError, SGX_THREAD_T_NULL};
 
 extern "C" {
@@ -182,7 +184,7 @@
     const fn new(control: SgxThreadMutexControl) -> Self {
         SgxThreadMutexInner {
             refcount: 0,
-            control: control,
+            control,
             lock: SgxThreadSpinlock::new(),
             owner: SGX_THREAD_T_NULL,
             queue: LinkedList::new(),
@@ -201,8 +203,7 @@
             }
 
             if self.owner == SGX_THREAD_T_NULL
-                && (self.queue.front() == Some(&rsgx_thread_self())
-                    || self.queue.front() == None)
+                && (self.queue.front() == Some(&rsgx_thread_self()) || self.queue.front() == None)
             {
                 if self.queue.front() == Some(&rsgx_thread_self()) {
                     self.queue.pop_front();
@@ -237,8 +238,7 @@
         }
 
         if self.owner == SGX_THREAD_T_NULL
-            && (self.queue.front() == Some(&rsgx_thread_self())
-                || self.queue.front() == None)
+            && (self.queue.front() == Some(&rsgx_thread_self()) || self.queue.front() == None)
         {
             if self.queue.front() == Some(&rsgx_thread_self()) {
                 self.queue.pop_front();
@@ -311,6 +311,8 @@
     }
 }
 
+pub type SgxMovableThreadMutex = Box<SgxThreadMutex>;
+
 unsafe impl Send for SgxThreadMutex {}
 unsafe impl Sync for SgxThreadMutex {}
 
@@ -319,9 +321,58 @@
 }
 
 impl SgxThreadMutex {
-    pub const fn new(control: SgxThreadMutexControl) -> Self {
+    pub const fn new() -> Self {
         SgxThreadMutex {
-            lock: UnsafeCell::new(SgxThreadMutexInner::new(control)),
+            lock: UnsafeCell::new(SgxThreadMutexInner::new(
+                SgxThreadMutexControl::SGX_THREAD_MUTEX_NONRECURSIVE,
+            )),
+        }
+    }
+
+    #[inline]
+    pub unsafe fn lock(&self) -> SysError {
+        let mutex: &mut SgxThreadMutexInner = &mut *self.lock.get();
+        mutex.lock()
+    }
+
+    #[inline]
+    pub unsafe fn try_lock(&self) -> SysError {
+        let mutex: &mut SgxThreadMutexInner = &mut *self.lock.get();
+        mutex.try_lock()
+    }
+
+    #[inline]
+    pub unsafe fn unlock(&self) -> SysError {
+        let mutex: &mut SgxThreadMutexInner = &mut *self.lock.get();
+        mutex.unlock()
+    }
+
+    #[inline]
+    pub unsafe fn unlock_lazy(&self, waiter: &mut sgx_thread_t) -> SysError {
+        let mutex: &mut SgxThreadMutexInner = &mut *self.lock.get();
+        mutex.unlock_lazy(waiter)
+    }
+
+    #[inline]
+    pub unsafe fn destroy(&self) -> SysError {
+        let mutex: &mut SgxThreadMutexInner = &mut *self.lock.get();
+        mutex.destroy()
+    }
+}
+
+pub struct SgxReentrantThreadMutex {
+    lock: UnsafeCell<SgxThreadMutexInner>,
+}
+
+unsafe impl Send for SgxReentrantThreadMutex {}
+unsafe impl Sync for SgxReentrantThreadMutex {}
+
+impl SgxReentrantThreadMutex {
+    pub const fn new() -> Self {
+        SgxReentrantThreadMutex {
+            lock: UnsafeCell::new(SgxThreadMutexInner::new(
+                SgxThreadMutexControl::SGX_THREAD_MUTEX_RECURSIVE,
+            )),
         }
     }
 
diff --git a/sgx_tstd/src/sys/net.rs b/sgx_tstd/src/sys/net.rs
index b6aa6c7..0683999 100644
--- a/sgx_tstd/src/sys/net.rs
+++ b/sgx_tstd/src/sys/net.rs
@@ -15,20 +15,22 @@
 // specific language governing permissions and limitations
 // under the License..
 
-#![allow(dead_code)]
-use sgx_trts::libc::{c_int, size_t, c_void};
-use core::mem;
-use core::cmp;
-use core::str;
+use crate::cmp;
 use crate::ffi::CStr;
 use crate::io::{self, IoSlice, IoSliceMut};
+use crate::mem;
 use crate::net::{Shutdown, SocketAddr};
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
+use crate::str;
 use crate::sys::fd::FileDesc;
-use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
+use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::time::{Duration, Instant};
 #[cfg(not(feature = "untrusted_time"))]
 use crate::untrusted::time::InstantEx;
+
+use sgx_libc::{c_int, c_void, size_t, sockaddr, socklen_t, MSG_PEEK};
+
 pub use crate::sys::{cvt, cvt_r};
 
 pub type wrlen_t = size_t;
@@ -57,14 +59,14 @@
         str::from_utf8(CStr::from_ptr(strerr).to_bytes()).unwrap().to_owned()
     };
     Err(io::Error::new(
-        io::ErrorKind::Other,
+        io::ErrorKind::Uncategorized,
         &format!("failed to lookup address information: {}", detail)[..],
     ))
 }
 
 impl Socket {
     pub fn new(sockfd: c_int) -> io::Result<Socket> {
-        let fd = FileDesc::new(sockfd);
+        let fd = unsafe { FileDesc::from_raw_fd(sockfd) };
         fd.set_cloexec()?;
         Ok(Socket(fd))
     }
@@ -79,23 +81,11 @@
 
     pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
         unsafe {
-            // On linux we first attempt to pass the SOCK_CLOEXEC flag to
-            // atomically create the socket and set it as CLOEXEC. Support for
-            // this option, however, was added in 2.6.27, and we still support
-            // 2.6.18 as a kernel, so if the returned error is EINVAL we
-            // fallthrough to the fallback.
-            match cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0)) {
-                Ok(fd) => return Ok(Socket(FileDesc::new(fd))),
-                Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {}
-                Err(e) => return Err(e),
-            }
-
-            let fd = cvt(libc::socket(fam, ty, 0))?;
-            let fd = FileDesc::new(fd);
-            fd.set_cloexec()?;
-            let socket = Socket(fd);
-
-            Ok(socket)
+            // On platforms that support it we pass the SOCK_CLOEXEC
+            // flag to atomically create the socket and set it as
+            // CLOEXEC. On Linux this was added in 2.6.27.
+            let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
+            Ok(Socket(FileDesc::from_raw_fd(fd)))
         }
     }
 
@@ -103,21 +93,9 @@
         unsafe {
             let mut fds = [0, 0];
 
-            // Like above, see if we can set cloexec atomically
-            match cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr())) {
-                Ok(_) => {
-                    return Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))));
-                }
-                Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {}
-                Err(e) => return Err(e),
-            }
-
-            cvt(libc::socketpair(fam, ty, 0, fds.as_mut_ptr()))?;
-            let a = FileDesc::new(fds[0]);
-            let b = FileDesc::new(fds[1]);
-            a.set_cloexec()?;
-            b.set_cloexec()?;
-            Ok((Socket(a), Socket(b)))
+            // Like above, set cloexec atomically
+            cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
+            Ok((Socket(FileDesc::from_raw_fd(fds[0])), Socket(FileDesc::from_raw_fd(fds[1]))))
         }
     }
 
@@ -125,7 +103,7 @@
         self.set_nonblocking(true)?;
         let r = unsafe {
             let (addrp, len) = addr.into_inner();
-            cvt(libc::connect(self.0.raw(), addrp, len))
+            cvt(libc::connect(self.as_raw_fd(), addrp, len))
         };
         self.set_nonblocking(false)?;
 
@@ -136,12 +114,12 @@
             Err(e) => return Err(e),
         }
 
-        let mut pollfd = libc::pollfd { fd: self.0.raw(), events: libc::POLLOUT, revents: 0 };
+        let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
 
         if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
-            return Err(io::Error::new(
+            return Err(io::Error::new_const(
                 io::ErrorKind::InvalidInput,
-                "cannot set a 0 duration timeout",
+                &"cannot set a 0 duration timeout",
             ));
         }
 
@@ -150,7 +128,7 @@
         loop {
             let elapsed = start.elapsed();
             if elapsed >= timeout {
-                return Err(io::Error::new(io::ErrorKind::TimedOut, "connection timed out"));
+                return Err(io::Error::new_const(io::ErrorKind::TimedOut, &"connection timed out"));
             }
 
             let timeout = timeout - elapsed;
@@ -162,7 +140,7 @@
                 timeout = 1;
             }
 
-            let timeout = cmp::min(timeout, c_int::max_value() as u64) as c_int;
+            let timeout = cmp::min(timeout, c_int::MAX as u64) as c_int;
 
             match unsafe { libc::poll(&mut pollfd, 1, timeout) } {
                 -1 => {
@@ -177,7 +155,10 @@
                     // for POLLHUP rather than read readiness
                     if pollfd.revents & libc::POLLHUP != 0 {
                         let e = self.take_error()?.unwrap_or_else(|| {
-                            io::Error::new(io::ErrorKind::Other, "no error set after POLLHUP")
+                            io::Error::new_const(
+                                io::ErrorKind::Uncategorized,
+                                &"no error set after POLLHUP",
+                            )
                         });
                         return Err(e);
                     }
@@ -198,31 +179,24 @@
     // This function is guarded by feature `net` and should only
     // be used on demand.
     // We don't support linux kernel < 2.6.28. So we only use accept4.
-    pub fn accept(&self, storage: *mut libc::sockaddr, len: *mut libc::socklen_t) -> io::Result<Socket> {
-        let res = cvt_r(|| unsafe {
-            libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC)
-        });
-        match res {
-            Ok(fd) => Ok(Socket(FileDesc::new(fd))),
-            Err(e) => Err(e),
+    pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
+        // Unfortunately the only known way right now to accept a socket and
+        // atomically set the CLOEXEC flag is to use the `accept4` syscall on
+        // platforms that support it. On Linux, this was added in 2.6.28,
+        // glibc 2.10 and musl 0.9.5.
+        unsafe {
+            let fd = cvt_r(|| libc::accept4(self.as_raw_fd(), storage, len, libc::SOCK_CLOEXEC))?;
+            Ok(Socket(FileDesc::from_raw_fd(fd)))
         }
     }
 
-    pub fn raw(&self) -> c_int {
-        self.0.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.0.into_raw()
-    }
-
     pub fn duplicate(&self) -> io::Result<Socket> {
         self.0.duplicate().map(Socket)
     }
 
     fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
         let ret = cvt(unsafe {
-            libc::recv(self.0.raw(), buf.as_mut_ptr() as *mut c_void, buf.len(), flags)
+            libc::recv(self.as_raw_fd(), buf.as_mut_ptr() as *mut c_void, buf.len(), flags)
         })?;
         Ok(ret as usize)
     }
@@ -232,13 +206,18 @@
     }
 
     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
-        self.recv_with_flags(buf, libc::MSG_PEEK)
+        self.recv_with_flags(buf, MSG_PEEK)
     }
 
     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
         self.0.read_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
     fn recv_from_with_flags(
         &self,
         buf: &mut [u8],
@@ -249,7 +228,7 @@
 
         let n = cvt(unsafe {
             libc::recvfrom(
-                self.0.raw(),
+                self.as_raw_fd(),
                 buf.as_mut_ptr() as *mut c_void,
                 buf.len(),
                 flags,
@@ -264,8 +243,13 @@
         self.recv_from_with_flags(buf, 0)
     }
 
+    pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
+        let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
+        Ok(n as usize)
+    }
+
     pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
-        self.recv_from_with_flags(buf, libc::MSG_PEEK)
+        self.recv_from_with_flags(buf, MSG_PEEK)
     }
 
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
@@ -276,18 +260,28 @@
         self.0.write_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
+    }
+
+    pub fn send_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
+        let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
+        Ok(n as usize)
+    }
+
     pub fn set_timeout(&self, dur: Option<Duration>, kind: c_int) -> io::Result<()> {
         let timeout = match dur {
             Some(dur) => {
                 if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
-                    return Err(io::Error::new(
+                    return Err(io::Error::new_const(
                         io::ErrorKind::InvalidInput,
-                        "cannot set a 0 duration timeout",
+                        &"cannot set a 0 duration timeout",
                     ));
                 }
 
-                let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
-                    libc::time_t::max_value()
+                let secs = if dur.as_secs() > libc::time_t::MAX as u64 {
+                    libc::time_t::MAX
                 } else {
                     dur.as_secs() as libc::time_t
                 };
@@ -322,10 +316,25 @@
             Shutdown::Read => libc::SHUT_RD,
             Shutdown::Both => libc::SHUT_RDWR,
         };
-        cvt(unsafe { libc::shutdown(self.0.raw(), how) })?;
+        cvt(unsafe { libc::shutdown(self.as_raw_fd(), how) })?;
         Ok(())
     }
 
+    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
+        let linger = libc::linger {
+            l_onoff: linger.is_some() as c_int,
+            l_linger: linger.unwrap_or_default().as_secs() as c_int,
+        };
+
+        setsockopt(self, libc::SOL_SOCKET, libc::SO_LINGER, linger)
+    }
+
+    pub fn linger(&self) -> io::Result<Option<Duration>> {
+        let val: libc::linger = getsockopt(self, libc::SOL_SOCKET, libc::SO_LINGER)?;
+
+        Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
+    }
+
     pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
         setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int)
     }
@@ -335,37 +344,77 @@
         Ok(raw != 0)
     }
 
+    pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
+        setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
+    }
+
+    pub fn passcred(&self) -> io::Result<bool> {
+        let passcred: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)?;
+        Ok(passcred != 0)
+    }
+
     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
         let mut nonblocking = nonblocking as c_int;
-        cvt(unsafe { libc::ioctl_arg1(*self.as_inner(), libc::FIONBIO, &mut nonblocking) }).map(drop)
+        cvt(unsafe { libc::ioctl_arg1(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
     }
 
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
         if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
     }
-}
 
-impl AsInner<c_int> for Socket {
-    fn as_inner(&self) -> &c_int {
-        self.0.as_inner()
+    // This is used by sys_common code to abstract over Windows and Unix.
+    pub fn as_raw(&self) -> RawFd {
+        self.as_raw_fd()
     }
 }
 
-impl FromInner<c_int> for Socket {
-    fn from_inner(fd: c_int) -> Socket {
-        Socket(FileDesc::new(fd))
+impl AsInner<FileDesc> for Socket {
+    fn as_inner(&self) -> &FileDesc {
+        &self.0
     }
 }
 
-impl IntoInner<c_int> for Socket {
-    fn into_inner(self) -> c_int {
-        self.0.into_raw()
+impl IntoInner<FileDesc> for Socket {
+    fn into_inner(self) -> FileDesc {
+        self.0
+    }
+}
+
+impl FromInner<FileDesc> for Socket {
+    fn from_inner(file_desc: FileDesc) -> Self {
+        Self(file_desc)
+    }
+}
+
+impl AsFd for Socket {
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_fd()
+    }
+}
+
+impl AsRawFd for Socket {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_raw_fd()
+    }
+}
+
+impl IntoRawFd for Socket {
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_raw_fd()
+    }
+}
+
+impl FromRawFd for Socket {
+    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
+        Self(FromRawFd::from_raw_fd(raw_fd))
     }
 }
 
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{socket, socketpair, connect, accept4, recv, recvfrom, shutdown,
-                                    ioctl_arg1, poll, gai_strerror};
-}
\ No newline at end of file
+    pub use sgx_libc::ocall::{
+        accept4, connect, gai_strerror, ioctl_arg1, poll, recv, recvfrom, recvmsg, sendmsg, shutdown, socket,
+        socketpair,
+    };
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/sys/os.rs b/sgx_tstd/src/sys/os.rs
index 9627cfe..b8bfe37 100644
--- a/sgx_tstd/src/sys/os.rs
+++ b/sgx_tstd/src/sys/os.rs
@@ -15,28 +15,29 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::error as trts_error;
-use sgx_types::metadata;
 use crate::os::unix::prelude::*;
+
 use crate::error::Error as StdError;
 use crate::ffi::{CStr, CString, OsStr, OsString};
-use crate::path::{self, PathBuf};
-use crate::sync::SgxThreadMutex;
-use crate::sys::cvt;
-use crate::memchr;
+use crate::fmt;
 use crate::io;
-use core::marker::PhantomData;
-use core::fmt;
-use core::iter;
-use core::ptr;
-use core::mem;
-use alloc_crate::slice;
-use alloc_crate::string::String;
-use alloc_crate::str;
-use alloc_crate::vec::{self, Vec};
+use crate::iter;
+use crate::mem;
+use crate::path::{self, PathBuf};
+use crate::ptr;
+use crate::slice;
+use crate::str;
+use crate::sync::SgxThreadRwLock;
+use crate::sys::cvt;
+use crate::sys::memchr;
+use crate::vec;
+
+use sgx_trts::error as trts_error;
+use sgx_types::metadata::SE_PAGE_SIZE;
 
 const TMPBUF_SZ: usize = 128;
-static ENV_LOCK: SgxThreadMutex = SgxThreadMutex::new();
+const PATH_SEPARATOR: u8 = b':';
+static ENV_LOCK: SgxThreadRwLock = SgxThreadRwLock::new();
 
 pub fn errno() -> i32 {
     trts_error::errno()
@@ -58,177 +59,6 @@
     }
 }
 
-pub struct SplitPaths<'a> {
-    iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>,
-                    fn(&'a [u8]) -> PathBuf>,
-}
-
-pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
-    fn bytes_to_path(b: &[u8]) -> PathBuf {
-        PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
-    }
-    fn is_colon(b: &u8) -> bool { *b == b':' }
-    let unparsed = unparsed.as_bytes();
-    SplitPaths {
-        iter: unparsed.split(is_colon as fn(&u8) -> bool)
-                      .map(bytes_to_path as fn(&[u8]) -> PathBuf)
-    }
-}
-
-impl<'a> Iterator for SplitPaths<'a> {
-    type Item = PathBuf;
-    fn next(&mut self) -> Option<PathBuf> { self.iter.next() }
-    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
-}
-
-#[derive(Debug)]
-pub struct JoinPathsError;
-
-pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
-    where I: Iterator<Item=T>, T: AsRef<OsStr>
-{
-    let mut joined = Vec::new();
-    let sep = b':';
-
-    for (i, path) in paths.enumerate() {
-        let path = path.as_ref().as_bytes();
-        if i > 0 { joined.push(sep) }
-        if path.contains(&sep) {
-            return Err(JoinPathsError)
-        }
-        joined.extend_from_slice(path);
-    }
-    Ok(OsStringExt::from_vec(joined))
-}
-
-impl fmt::Display for JoinPathsError {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        "path segment contains separator `:`".fmt(f)
-    }
-}
-
-impl StdError for JoinPathsError {
-    fn description(&self) -> &str { "failed to join paths" }
-}
-
-pub fn current_exe() -> io::Result<PathBuf> {
-    match crate::fs::read_link("/proc/self/exe") {
-        Err(ref e) if e.kind() == io::ErrorKind::NotFound => {
-            Err(io::Error::new(
-                io::ErrorKind::Other,
-                "no /proc/self/exe available. Is /proc mounted?"
-            ))
-        },
-        other => other,
-    }
-}
-
-pub struct Env {
-    iter: vec::IntoIter<(OsString, OsString)>,
-    _dont_send_or_sync_me: PhantomData<*mut ()>,
-}
-
-impl Iterator for Env {
-    type Item = (OsString, OsString);
-    fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() }
-    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
-}
-
-pub unsafe fn environ() -> *const *const libc::c_char {
-    libc::environ()
-}
-
-/// Returns a vector of (variable, value) byte-vector pairs for all the
-/// environment variables of the current process.
-pub fn env() -> Env {
-    unsafe {
-        ENV_LOCK.lock();
-        let mut environ = environ();
-        let mut result = Vec::new();
-        if !environ.is_null() {
-            while !(*environ).is_null() {
-                if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
-                    result.push(key_value);
-                }
-                environ = environ.add(1);
-            }
-        }
-        let ret = Env {
-            iter: result.into_iter(),
-            _dont_send_or_sync_me: PhantomData,
-        };
-        ENV_LOCK.unlock();
-        return ret;
-    }
-
-    fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
-        // Strategy (copied from glibc): Variable name and value are separated
-        // by an ASCII equals sign '='. Since a variable name must not be
-        // empty, allow variable names starting with an equals sign. Skip all
-        // malformed lines.
-        if input.is_empty() {
-            return None;
-        }
-        let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
-        pos.map(|p| {
-            (
-                OsStringExt::from_vec(input[..p].to_vec()),
-                OsStringExt::from_vec(input[p + 1..].to_vec()),
-            )
-        })
-    }
-}
-
-pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
-    // environment variables with a nul byte can't be set, so their value is
-    // always None as well
-    let k = CString::new(k.as_bytes())?;
-    unsafe {
-        ENV_LOCK.lock();
-        let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
-        let ret = if s.is_null() {
-            None
-        } else {
-            Some(OsStringExt::from_vec(CStr::from_ptr(s).to_bytes().to_vec()))
-        };
-        ENV_LOCK.unlock();
-        Ok(ret)
-    }
-}
-
-pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
-    let k = CString::new(k.as_bytes())?;
-    let v = CString::new(v.as_bytes())?;
-
-    unsafe {
-        ENV_LOCK.lock();
-        let ret = cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop);
-        ENV_LOCK.unlock();
-        ret
-    }
-}
-
-pub fn unsetenv(n: &OsStr) -> io::Result<()> {
-    let nbuf = CString::new(n.as_bytes())?;
-
-    unsafe {
-        ENV_LOCK.lock();
-        let ret = cvt(libc::unsetenv(nbuf.as_ptr())).map(drop);
-        ENV_LOCK.unlock();
-        ret
-    }
-}
-
-pub fn page_size() -> usize {
-    metadata::SE_PAGE_SIZE
-}
-
-pub fn temp_dir() -> PathBuf {
-    crate::env::var_os("TMPDIR").map(PathBuf::from).unwrap_or_else(|| {
-        PathBuf::from("/tmp")
-    })
-}
-
 pub fn getcwd() -> io::Result<PathBuf> {
     let mut buf = Vec::with_capacity(512);
     loop {
@@ -258,21 +88,203 @@
 pub fn chdir(p: &path::Path) -> io::Result<()> {
     let p: &OsStr = p.as_ref();
     let p = CString::new(p.as_bytes())?;
-    unsafe {
-        match libc::chdir(p.as_ptr()) == (0 as libc::c_int) {
-            true => Ok(()),
-            false => Err(io::Error::last_os_error()),
-        }
+    if unsafe { libc::chdir(p.as_ptr()) } != 0 {
+        return Err(io::Error::last_os_error());
+    }
+    Ok(())
+}
+
+#[allow(clippy::type_complexity)]
+pub struct SplitPaths<'a> {
+    iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, fn(&'a [u8]) -> PathBuf>,
+}
+
+pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
+    fn bytes_to_path(b: &[u8]) -> PathBuf {
+        PathBuf::from(<OsStr as OsStrExt>::from_bytes(b))
+    }
+    fn is_separator(b: &u8) -> bool {
+        *b == PATH_SEPARATOR
+    }
+    let unparsed = unparsed.as_bytes();
+    SplitPaths {
+        iter: unparsed
+            .split(is_separator as fn(&u8) -> bool)
+            .map(bytes_to_path as fn(&[u8]) -> PathBuf),
     }
 }
 
+impl<'a> Iterator for SplitPaths<'a> {
+    type Item = PathBuf;
+    fn next(&mut self) -> Option<PathBuf> {
+        self.iter.next()
+    }
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        self.iter.size_hint()
+    }
+}
+
+#[derive(Debug)]
+pub struct JoinPathsError;
+
+pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
+where
+    I: Iterator<Item = T>,
+    T: AsRef<OsStr>,
+{
+    let mut joined = Vec::new();
+
+    for (i, path) in paths.enumerate() {
+        let path = path.as_ref().as_bytes();
+        if i > 0 {
+            joined.push(PATH_SEPARATOR)
+        }
+        if path.contains(&PATH_SEPARATOR) {
+            return Err(JoinPathsError);
+        }
+        joined.extend_from_slice(path);
+    }
+    Ok(OsStringExt::from_vec(joined))
+}
+
+impl fmt::Display for JoinPathsError {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "path segment contains separator `{}`", char::from(PATH_SEPARATOR))
+    }
+}
+
+impl StdError for JoinPathsError {
+    #[allow(deprecated)]
+    fn description(&self) -> &str {
+        "failed to join paths"
+    }
+}
+
+pub fn current_exe() -> io::Result<PathBuf> {
+    match crate::fs::read_link("/proc/self/exe") {
+        Err(ref e) if e.kind() == io::ErrorKind::NotFound => Err(io::Error::new_const(
+            io::ErrorKind::Uncategorized,
+            &"no /proc/self/exe available. Is /proc mounted?",
+        )),
+        other => other,
+    }
+}
+
+pub struct Env {
+    iter: vec::IntoIter<(OsString, OsString)>,
+}
+
+impl !Send for Env {}
+impl !Sync for Env {}
+
+impl Iterator for Env {
+    type Item = (OsString, OsString);
+    fn next(&mut self) -> Option<(OsString, OsString)> {
+        self.iter.next()
+    }
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        self.iter.size_hint()
+    }
+}
+
+pub unsafe fn environ() -> *const *const libc::c_char {
+    libc::environ()
+}
+
+/// Returns a vector of (variable, value) byte-vector pairs for all the
+/// environment variables of the current process.
+pub fn env() -> Env {
+    unsafe {
+        ENV_LOCK.read();
+        let mut environ = environ();
+        let mut result = Vec::new();
+        if !environ.is_null() {
+            while !(*environ).is_null() {
+                if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
+                    result.push(key_value);
+                }
+                environ = environ.add(1);
+            }
+        }
+        let ret = Env { iter: result.into_iter() };
+        ENV_LOCK.read_unlock();
+        return ret;
+    }
+
+    fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
+        // Strategy (copied from glibc): Variable name and value are separated
+        // by an ASCII equals sign '='. Since a variable name must not be
+        // empty, allow variable names starting with an equals sign. Skip all
+        // malformed lines.
+        if input.is_empty() {
+            return None;
+        }
+        let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
+        pos.map(|p| {
+            (
+                OsStringExt::from_vec(input[..p].to_vec()),
+                OsStringExt::from_vec(input[p + 1..].to_vec()),
+            )
+        })
+    }
+}
+
+pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
+    // environment variables with a nul byte can't be set, so their value is
+    // always None as well
+    let k = CString::new(k.as_bytes())?;
+    unsafe {
+        ENV_LOCK.read();
+        let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
+        let ret = if s.is_null() {
+            None
+        } else {
+            Some(OsStringExt::from_vec(CStr::from_ptr(s).to_bytes().to_vec()))
+        };
+        ENV_LOCK.read_unlock();
+        Ok(ret)
+    }
+}
+
+pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
+    let k = CString::new(k.as_bytes())?;
+    let v = CString::new(v.as_bytes())?;
+
+    unsafe {
+        ENV_LOCK.write();
+        let ret = cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop);
+        ENV_LOCK.write_unlock();
+        ret
+    }
+}
+
+pub fn unsetenv(n: &OsStr) -> io::Result<()> {
+    let nbuf = CString::new(n.as_bytes())?;
+
+    unsafe {
+        ENV_LOCK.write();
+        let ret = cvt(libc::unsetenv(nbuf.as_ptr())).map(drop);
+        ENV_LOCK.write_unlock();
+        ret
+    }
+}
+
+pub fn page_size() -> usize {
+    SE_PAGE_SIZE
+}
+
+pub fn temp_dir() -> PathBuf {
+    crate::env::var_os("TMPDIR").map(PathBuf::from).unwrap_or_else(|| {
+        PathBuf::from("/tmp")
+    })
+}
+
 pub fn home_dir() -> Option<PathBuf> {
-    return crate::env::var_os("HOME").or_else(|| unsafe {
-        fallback()
-    }).map(PathBuf::from);
+    return crate::env::var_os("HOME").or_else(|| unsafe { fallback() }).map(PathBuf::from);
+
     unsafe fn fallback() -> Option<OsString> {
         let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
-            n if n < 0 => 512 as usize,
+            n if n < 0 => 512_usize,
             n => n as usize,
         };
         let mut buf = Vec::with_capacity(amt);
@@ -283,7 +295,7 @@
             &mut passwd,
             buf.as_mut_ptr(),
             buf.capacity(),
-            &mut result
+            &mut result,
         ) {
             0 if !result.is_null() => {
                 let ptr = passwd.pw_dir as *const _;
@@ -296,6 +308,6 @@
 }
 
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{environ, getenv, setenv, unsetenv, getcwd, chdir, sysconf, getuid, getpwuid_r};
+    pub use sgx_libc::ocall::{environ, getenv, setenv, unsetenv, getcwd, chdir, sysconf, getuid, getpwuid_r};
+    pub use sgx_libc::*;
 }
\ No newline at end of file
diff --git a/sgx_tstd/src/sys_common/os_str_bytes.rs b/sgx_tstd/src/sys/os_str.rs
similarity index 69%
rename from sgx_tstd/src/sys_common/os_str_bytes.rs
rename to sgx_tstd/src/sys/os_str.rs
index ad7c1c3..dec62b4 100644
--- a/sgx_tstd/src/sys_common/os_str_bytes.rs
+++ b/sgx_tstd/src/sys/os_str.rs
@@ -18,38 +18,43 @@
 //! The underlying OsString/OsStr implementation on Unix and many other
 //! systems: just a `Vec<u8>`/`[u8]`.
 
-use core::fmt;
-use core::mem;
-use core::str::lossy::Utf8Lossy;
-use alloc_crate::borrow::Cow;
-use alloc_crate::str;
-use alloc_crate::string::String;
-use alloc_crate::vec::Vec;
-use alloc_crate::boxed::Box;
-use alloc_crate::rc::Rc;
-use alloc_crate::sync::Arc;
-use crate::ffi::{OsStr, OsString};
-use crate::sys_common::{AsInner, FromInner, IntoInner};
-use crate::sys_common::bytestring::debug_fmt_bytestring;
+use crate::borrow::Cow;
+use crate::fmt;
+use crate::fmt::Write;
+use crate::mem;
+use crate::rc::Rc;
+use crate::str;
+use crate::sync::Arc;
+use crate::sys_common::{AsInner, IntoInner};
 
-#[derive(Clone, Hash)]
-pub(crate) struct Buf {
+use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
+
+#[derive(Hash)]
+#[repr(transparent)]
+pub struct Buf {
     pub inner: Vec<u8>,
 }
 
-// FIXME:
-// `Buf::as_slice` current implementation relies
-// on `Slice` being layout-compatible with `[u8]`.
-// When attribute privacy is implemented, `Slice` should be annotated as `#[repr(transparent)]`.
-// Anyway, `Slice` representation and layout are considered implementation detail, are
-// not documented and must not be relied upon.
-pub(crate) struct Slice {
+#[repr(transparent)]
+pub struct Slice {
     pub inner: [u8],
 }
 
 impl fmt::Debug for Slice {
     fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
-        debug_fmt_bytestring(&self.inner, formatter)
+        // Writes out a valid unicode string with the correct escape sequences
+
+        formatter.write_str("\"")?;
+        for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(&self.inner).chunks() {
+            for c in valid.chars().flat_map(|c| c.escape_debug()) {
+                formatter.write_char(c)?
+            }
+
+            for b in broken {
+                write!(formatter, "\\x{:02X}", b)?;
+            }
+        }
+        formatter.write_str("\"")
     }
 }
 
@@ -71,6 +76,18 @@
     }
 }
 
+impl Clone for Buf {
+    #[inline]
+    fn clone(&self) -> Self {
+        Buf { inner: self.inner.clone() }
+    }
+
+    #[inline]
+    fn clone_from(&mut self, source: &Self) {
+        self.inner.clone_from(&source.inner)
+    }
+}
+
 impl IntoInner<Vec<u8>> for Buf {
     fn into_inner(self) -> Vec<u8> {
         self.inner
@@ -125,7 +142,7 @@
 
     #[inline]
     pub fn as_slice(&self) -> &Slice {
-        // Safety: Slice just wraps [u8],
+        // SAFETY: Slice just wraps [u8],
         // and &*self.inner is &[u8], therefore
         // transmuting &[u8] to &Slice is safe.
         unsafe { mem::transmute(&*self.inner) }
@@ -133,7 +150,7 @@
 
     #[inline]
     pub fn as_mut_slice(&mut self) -> &mut Slice {
-        // Safety: Slice just wraps [u8],
+        // SAFETY: Slice just wraps [u8],
         // and &mut *self.inner is &mut [u8], therefore
         // transmuting &mut [u8] to &mut Slice is safe.
         unsafe { mem::transmute(&mut *self.inner) }
@@ -249,61 +266,3 @@
         self.inner.eq_ignore_ascii_case(&other.inner)
     }
 }
-
-/// Platform-specific extensions to [`OsString`].
-///
-/// [`OsString`]: ../../../../std/ffi/struct.OsString.html
-pub trait OsStringExt {
-    /// Creates an [`OsString`] from a byte vector.
-    ///
-    /// See the module docmentation for an example.
-    ///
-    /// [`OsString`]: ../../../ffi/struct.OsString.html
-    fn from_vec(vec: Vec<u8>) -> Self;
-
-    /// Yields the underlying byte vector of this [`OsString`].
-    ///
-    /// See the module docmentation for an example.
-    ///
-    /// [`OsString`]: ../../../ffi/struct.OsString.html
-    fn into_vec(self) -> Vec<u8>;
-}
-
-impl OsStringExt for OsString {
-    fn from_vec(vec: Vec<u8>) -> OsString {
-        FromInner::from_inner(Buf { inner: vec })
-    }
-    fn into_vec(self) -> Vec<u8> {
-        self.into_inner().inner
-    }
-}
-
-/// Platform-specific extensions to [`OsStr`].
-///
-/// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
-pub trait OsStrExt {
-    /// Creates an [`OsStr`] from a byte slice.
-    ///
-    /// See the module documentation for an example.
-    ///
-    /// [`OsStr`]: ../../../ffi/struct.OsStr.html
-    fn from_bytes(slice: &[u8]) -> &Self;
-
-    /// Gets the underlying byte view of the [`OsStr`] slice.
-    ///
-    /// See the module documentation for an example.
-    ///
-    /// [`OsStr`]: ../../../ffi/struct.OsStr.html
-    fn as_bytes(&self) -> &[u8];
-}
-
-impl OsStrExt for OsStr {
-    #[inline]
-    fn from_bytes(slice: &[u8]) -> &OsStr {
-        unsafe { mem::transmute(slice) }
-    }
-    #[inline]
-    fn as_bytes(&self) -> &[u8] {
-        &self.as_inner().inner
-    }
-}
diff --git a/sgx_tstd/src/sys/pipe.rs b/sgx_tstd/src/sys/pipe.rs
index 3a5e249..dcf46c4 100644
--- a/sgx_tstd/src/sys/pipe.rs
+++ b/sgx_tstd/src/sys/pipe.rs
@@ -16,11 +16,11 @@
 // under the License..
 
 use crate::io::{self, IoSlice, IoSliceMut};
+use crate::mem;
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
 use crate::sys::fd::FileDesc;
 use crate::sys::{cvt, cvt_r};
-use core::mem;
-use alloc_crate::vec::Vec;
-
+use crate::sys_common::IntoInner;
 
 ////////////////////////////////////////////////////////////////////////////////
 // Anonymous pipes
@@ -30,11 +30,10 @@
 
 pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
     let mut fds = [0; 2];
-    cvt(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
-
-    let fd0 = FileDesc::new(fds[0]);
-    let fd1 = FileDesc::new(fds[1]);
-    Ok((AnonPipe(fd0), AnonPipe(fd1)))
+    unsafe {
+        cvt(libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC))?;
+        Ok((AnonPipe(FileDesc::from_raw_fd(fds[0])), AnonPipe(FileDesc::from_raw_fd(fds[1]))))
+    }
 }
 
 impl AnonPipe {
@@ -46,6 +45,11 @@
         self.0.read_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_read_vectored(&self) -> bool {
+        self.0.is_read_vectored()
+    }
+
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
         self.0.write(buf)
     }
@@ -54,10 +58,14 @@
         self.0.write_vectored(bufs)
     }
 
-    pub fn fd(&self) -> &FileDesc {
-        &self.0
+    #[inline]
+    pub fn is_write_vectored(&self) -> bool {
+        self.0.is_write_vectored()
     }
-    pub fn into_fd(self) -> FileDesc {
+}
+
+impl IntoInner<FileDesc> for AnonPipe {
+    fn into_inner(self) -> FileDesc {
         self.0
     }
 }
@@ -65,15 +73,15 @@
 pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
     // Set both pipes into nonblocking mode as we're gonna be reading from both
     // in the `select` loop below, and we wouldn't want one to block the other!
-    let p1 = p1.into_fd();
-    let p2 = p2.into_fd();
+    let p1 = p1.into_inner();
+    let p2 = p2.into_inner();
     p1.set_nonblocking(true)?;
     p2.set_nonblocking(true)?;
 
     let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
-    fds[0].fd = p1.raw();
+    fds[0].fd = p1.as_raw_fd();
     fds[0].events = libc::POLLIN;
-    fds[1].fd = p2.raw();
+    fds[1].fd = p2.as_raw_fd();
     fds[1].events = libc::POLLIN;
     loop {
         // wait for either pipe to become readable using `poll`
@@ -110,7 +118,32 @@
     }
 }
 
+impl AsRawFd for AnonPipe {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0.as_raw_fd()
+    }
+}
+
+impl AsFd for AnonPipe {
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        self.0.as_fd()
+    }
+}
+
+impl IntoRawFd for AnonPipe {
+    fn into_raw_fd(self) -> RawFd {
+        self.0.into_raw_fd()
+    }
+}
+
+impl FromRawFd for AnonPipe {
+    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
+        Self(FromRawFd::from_raw_fd(raw_fd))
+    }
+}
+
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{pipe2, poll};
+    pub use sgx_libc::ocall::{pipe2, poll};
+    pub use sgx_libc::*;
+    
 }
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/rand.rs b/sgx_tstd/src/sys/rand.rs
index d1f791a..858c2d3 100644
--- a/sgx_tstd/src/sys/rand.rs
+++ b/sgx_tstd/src/sys/rand.rs
@@ -15,8 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::mem;
 use alloc_crate::slice;
+use core::mem;
 
 pub fn hashmap_random_keys() -> (u64, u64) {
     let mut v = (0, 0);
@@ -39,11 +39,7 @@
         getrandom(v).expect("unexpected getrandom error");
     }
 
-    #[allow(dead_code)]
-    fn is_getrandom_available() -> bool { true }
-
     pub fn fill_bytes(v: &mut [u8]) {
         getrandom_fill_bytes(v)
     }
 }
-
diff --git a/sgx_tstd/src/sys/rwlock.bak.rs b/sgx_tstd/src/sys/rwlock.bak.rs
new file mode 100644
index 0000000..b9339a5
--- /dev/null
+++ b/sgx_tstd/src/sys/rwlock.bak.rs
@@ -0,0 +1,308 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use core::cell::UnsafeCell;
+use crate::sync::{SgxThreadCondvar, SgxThreadMutex, SgxThreadSpinlock};
+use crate::thread;
+use sgx_trts::libc;
+use sgx_types::{sgx_thread_t, SysError, SGX_THREAD_T_NULL};
+
+struct SgxThreadRwLockInner {
+    readers_num: u32,
+    writers_num: u32,
+    busy: u32,
+    writer_thread: sgx_thread_t,
+    condvar: SgxThreadCondvar,
+    mutex: SgxThreadMutex,
+    spinlock: SgxThreadSpinlock,
+}
+
+impl SgxThreadRwLockInner {
+    const fn new() -> Self {
+        SgxThreadRwLockInner {
+            readers_num: 0,
+            writers_num: 0,
+            busy: 0,
+            writer_thread: SGX_THREAD_T_NULL,
+            condvar: SgxThreadCondvar::new(),
+            mutex: SgxThreadMutex::new(),
+            spinlock: SgxThreadSpinlock::new(),
+        }
+    }
+
+    unsafe fn ref_busy(&mut self) -> SysError {
+        let ret: SysError;
+        self.spinlock.lock();
+        {
+            if self.busy == u32::MAX {
+                ret = Err(libc::EAGAIN);
+            } else {
+                self.busy += 1;
+                ret = Ok(());
+            }
+        }
+        self.spinlock.unlock();
+        ret
+    }
+
+    unsafe fn deref_busy(&mut self) -> SysError {
+        let ret: SysError;
+        self.spinlock.lock();
+        {
+            if self.busy == 0 {
+                ret = Err(libc::EAGAIN);
+            } else {
+                self.busy -= 1;
+                ret = Ok(());
+            }
+        }
+        self.spinlock.unlock();
+        ret
+    }
+
+    unsafe fn read(&mut self) -> SysError {
+        self.ref_busy()?;
+        self.mutex.lock();
+        {
+            if self.writer_thread == thread::rsgx_thread_self() {
+                self.mutex.unlock();
+                self.deref_busy();
+                return Err(libc::EDEADLK);
+            }
+            if self.readers_num == u32::MAX {
+                self.mutex.unlock();
+                self.deref_busy();
+                return Err(libc::EAGAIN);
+            }
+            while self.writers_num > 0 {
+                self.condvar.wait(&self.mutex);
+            }
+            self.readers_num += 1;
+        }
+        self.mutex.unlock();
+        self.deref_busy();
+        Ok(())
+    }
+
+    unsafe fn try_read(&mut self) -> SysError {
+        self.ref_busy()?;
+        self.mutex.lock();
+        {
+            let mut ret = Ok(());
+            if self.writer_thread == thread::rsgx_thread_self() {
+                ret = Err(libc::EDEADLK);
+            } else if self.readers_num == u32::MAX {
+                ret = Err(libc::EAGAIN);
+            } else if self.writers_num > 0 {
+                ret = Err(libc::EBUSY);
+            }
+            match ret {
+                Ok(_) => {}
+                Err(e) => {
+                    self.mutex.unlock();
+                    self.deref_busy();
+                    return Err(e);
+                }
+            }
+            self.readers_num += 1;
+        }
+        self.mutex.unlock();
+        self.deref_busy();
+        Ok(())
+    }
+
+    unsafe fn write(&mut self) -> SysError {
+        self.ref_busy()?;
+        self.mutex.lock();
+        {
+            if self.writer_thread == thread::rsgx_thread_self() {
+                self.mutex.unlock();
+                self.deref_busy();
+                return Err(libc::EDEADLK);
+            }
+
+            if self.writers_num == u32::MAX {
+                self.mutex.unlock();
+                self.deref_busy();
+                return Err(libc::EAGAIN);
+            }
+
+            self.writers_num += 1;
+            while self.readers_num > 0 {
+                self.condvar.wait(&self.mutex);
+            }
+            while self.writer_thread != SGX_THREAD_T_NULL {
+                self.condvar.wait(&self.mutex);
+            }
+            self.writer_thread = thread::rsgx_thread_self();
+        }
+        self.mutex.unlock();
+        self.deref_busy();
+        Ok(())
+    }
+
+    pub unsafe fn try_write(&mut self) -> SysError {
+        self.ref_busy()?;
+        self.mutex.lock();
+        {
+            let mut ret = Ok(());
+            if self.writer_thread == thread::rsgx_thread_self() {
+                ret = Err(libc::EDEADLK);
+            } else if self.writers_num == u32::MAX {
+                ret = Err(libc::EAGAIN);
+            } else if self.readers_num > 0 || self.writer_thread != SGX_THREAD_T_NULL {
+                ret = Err(libc::EBUSY);
+            }
+
+            match ret {
+                Ok(_) => {}
+                Err(e) => {
+                    self.mutex.unlock();
+                    self.deref_busy();
+                    return Err(e);
+                }
+            }
+            self.writers_num += 1;
+            self.writer_thread = thread::rsgx_thread_self();
+        }
+        self.mutex.unlock();
+        self.deref_busy();
+        Ok(())
+    }
+
+    unsafe fn read_unlock(&mut self) -> SysError {
+        self.raw_unlock()
+    }
+
+    unsafe fn write_unlock(&mut self) -> SysError {
+        self.raw_unlock()
+    }
+
+    unsafe fn raw_unlock(&mut self) -> SysError {
+        self.mutex.lock();
+        {
+            if self.readers_num > 0 {
+                self.readers_num -= 1;
+                if self.readers_num == 0 && self.writers_num > 0 {
+                    self.condvar.broadcast();
+                }
+            } else {
+                if self.writer_thread != thread::rsgx_thread_self() {
+                    self.mutex.unlock();
+                    return Err(libc::EPERM);
+                }
+                self.writers_num -= 1;
+                self.writer_thread = SGX_THREAD_T_NULL;
+                if self.busy > 0 {
+                    self.condvar.broadcast();
+                }
+            }
+        }
+        self.mutex.unlock();
+        Ok(())
+    }
+
+    unsafe fn destroy(&mut self) -> SysError {
+        self.mutex.lock();
+        {
+            if self.readers_num > 0 || self.writers_num > 0 || self.busy > 0 {
+                self.spinlock.unlock();
+                return Err(libc::EBUSY);
+            }
+
+            self.condvar.destroy();
+            self.mutex.destroy();
+        }
+        self.spinlock.unlock();
+        Ok(())
+    }
+}
+
+/// An OS-based reader-writer lock.
+///
+/// This structure is entirely unsafe and serves as the lowest layer of a
+/// cross-platform binding of system rwlocks. It is recommended to use the
+/// safer types at the top level of this crate instead of this type.
+pub struct SgxThreadRwLock {
+    lock: UnsafeCell<SgxThreadRwLockInner>,
+}
+
+impl SgxThreadRwLock {
+    /// Creates a new reader-writer lock for use.
+    pub const fn new() -> Self {
+        SgxThreadRwLock {
+            lock: UnsafeCell::new(SgxThreadRwLockInner::new()),
+        }
+    }
+
+    /// Acquires shared access to the underlying lock, blocking the current
+    /// thread to do so.
+    #[inline]
+    pub unsafe fn read(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.read()
+    }
+
+    /// Attempts to acquire shared access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    #[inline]
+    pub unsafe fn try_read(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.try_read()
+    }
+
+    /// Acquires write access to the underlying lock, blocking the current thread
+    /// to do so.
+    #[inline]
+    pub unsafe fn write(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.write()
+    }
+
+    /// Attempts to acquire exclusive access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    #[inline]
+    pub unsafe fn try_write(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.try_write()
+    }
+
+    /// Unlocks previously acquired shared access to this lock.
+    #[inline]
+    pub unsafe fn read_unlock(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.read_unlock()
+    }
+
+    /// Unlocks previously acquired exclusive access to this lock.
+    #[inline]
+    pub unsafe fn write_unlock(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.write_unlock()
+    }
+
+    /// Destroys OS-related resources with this RWLock.
+    #[inline]
+    pub unsafe fn destroy(&self) -> SysError {
+        let rwlock: &mut SgxThreadRwLockInner = &mut *self.lock.get();
+        rwlock.destroy()
+    }
+}
diff --git a/sgx_tstd/src/sys/rwlock.rs b/sgx_tstd/src/sys/rwlock.rs
index 702d685..c5056ca 100644
--- a/sgx_tstd/src/sys/rwlock.rs
+++ b/sgx_tstd/src/sys/rwlock.rs
@@ -15,14 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use alloc_crate::collections::LinkedList;
-use core::cell::UnsafeCell;
+use crate::cell::UnsafeCell;
+use crate::collections::LinkedList;
 use crate::sync::SgxThreadSpinlock;
-use crate::time::Duration;
-use crate::thread::rsgx_thread_self;
 use crate::sys::mutex;
+use crate::thread::rsgx_thread_self;
+use crate::time::Duration;
+
+use sgx_libc as libc;
 use sgx_trts::enclave::SgxThreadData;
-use sgx_trts::libc;
 use sgx_types::{sgx_thread_t, SysError, SGX_THREAD_T_NULL};
 
 struct SgxThreadRwLockInner {
@@ -162,8 +163,8 @@
         if self.reader_count == 0 {
             let waiter = self.reader_queue.front();
             self.lock.unlock();
-            if waiter.is_some() {
-                mutex::thread_set_event(SgxThreadData::from_raw(*waiter.unwrap()).get_tcs());
+            if let Some(td) = waiter {
+                mutex::thread_set_event(SgxThreadData::from_raw(*td).get_tcs());
             }
         } else {
             self.lock.unlock();
@@ -192,8 +193,8 @@
         } else {
             let waiter = self.writer_queue.front();
             self.lock.unlock();
-            if waiter.is_some() {
-                mutex::thread_set_event(SgxThreadData::from_raw(*waiter.unwrap()).get_tcs());
+            if let Some(td) = waiter {
+                mutex::thread_set_event(SgxThreadData::from_raw(*td).get_tcs());
             }
         }
         Ok(())
@@ -224,6 +225,8 @@
     }
 }
 
+pub type SgxMovableThreadRwLock = Box<SgxThreadRwLock>;
+
 unsafe impl Send for SgxThreadRwLock {}
 unsafe impl Sync for SgxThreadRwLock {}
 
diff --git a/sgx_tstd/src/sys/sgxfs.rs b/sgx_tstd/src/sys/sgxfs.rs
index 7d6f24f..54a245c 100644
--- a/sgx_tstd/src/sys/sgxfs.rs
+++ b/sgx_tstd/src/sys/sgxfs.rs
@@ -15,14 +15,14 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::{sgx_status_t, sgx_key_128bit_t, sgx_align_key_128bit_t};
-use sgx_trts::libc;
-use sgx_tprotected_fs::{self, SgxFileStream};
 use crate::os::unix::prelude::*;
 use crate::ffi::{CString, CStr};
-use crate::io::{self, Error, ErrorKind, SeekFrom};
+use crate::io::{self, Error, SeekFrom};
 use crate::path::Path;
 use crate::sys_common::FromInner;
+use sgx_libc as libc;
+use sgx_tprotected_fs::{self, SgxFileStream};
+use sgx_types::{sgx_status_t, sgx_key_128bit_t, sgx_align_key_128bit_t};
 
 pub struct SgxFile(SgxFileStream);
 
@@ -46,24 +46,33 @@
         }
     }
 
-    pub fn read(&mut self, read: bool) { self.read = read; }
-    pub fn write(&mut self, write: bool) { self.write = write; }
-    pub fn append(&mut self, append: bool) { self.append = append; }
-    pub fn update(&mut self, update: bool) { self.update = update; }
-    pub fn binary(&mut self, binary: bool) { self.binary = binary; }
+    pub fn read(&mut self, read: bool) {
+        self.read = read;
+    }
+    pub fn write(&mut self, write: bool) {
+        self.write = write;
+    }
+    pub fn append(&mut self, append: bool) {
+        self.append = append;
+    }
+    pub fn update(&mut self, update: bool) {
+        self.update = update;
+    }
+    pub fn binary(&mut self, binary: bool) {
+        self.binary = binary;
+    }
 
     fn get_access_mode(&self) -> io::Result<String> {
-
         let mut mode = match (self.read, self.write, self.append) {
             (true,  false, false) => "r".to_string(),
             (false, true,  false) => "w".to_string(),
             (false, false, true)  => "a".to_string(),
             _ => {return Err(Error::from_raw_os_error(libc::EINVAL))},
         };
-        if self.update == true {
+        if self.update {
             mode += "+";
         }
-        if self.binary == true {
+        if self.binary {
             mode += "b";
         }
         Ok(mode)
@@ -86,13 +95,13 @@
     }
 
     pub fn open_c(path: &CStr, opts: &CStr, key: &sgx_key_128bit_t, auto: bool) -> io::Result<SgxFile> {
-        let file = if auto == true {
+        let file = if auto {
             SgxFileStream::open_auto_key(path, opts)
         } else {
             SgxFileStream::open(path, opts, key)
         };
 
-        file.map(|stream| SgxFile(stream))
+        file.map(SgxFile)
             .map_err(|err| {
                 match err {
                     1 => Error::from_sgx_error(sgx_status_t::SGX_ERROR_UNEXPECTED),
@@ -294,8 +303,9 @@
 }
 
 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
-
     use crate::sgxfs::SgxFile;
+    use crate::sys_common::fs::NOT_FILE_ERROR;
+
     cfg_if! {
         if #[cfg(feature = "untrusted_fs")] {
             use crate::fs;
@@ -307,17 +317,14 @@
 
     let metadata = from.metadata()?;
     if !metadata.is_file() {
-        return Err(Error::new(
-            ErrorKind::InvalidInput,
-            "the source path is not an existing regular file",
-        ));
+        return Err(NOT_FILE_ERROR);
     }
 
     let mut reader = SgxFile::open(from)?;
     let mut writer = SgxFile::create(to)?;
     let perm = metadata.permissions();
 
-    let ret = io::copy(&mut reader, &mut writer)?;
+    let ret = io::copy::copy(&mut reader, &mut writer)?;
     fs::set_permissions(to, perm)?;
     Ok(ret)
 }
diff --git a/sgx_tstd/src/sys/stdio.rs b/sgx_tstd/src/sys/stdio.rs
index 5049c99..d36bbe7 100644
--- a/sgx_tstd/src/sys/stdio.rs
+++ b/sgx_tstd/src/sys/stdio.rs
@@ -14,44 +14,58 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License..
+
 use crate::io::{self, IoSlice, IoSliceMut};
-use sgx_trts::libc;
-use crate::sys::fd::FileDesc;
 use crate::mem::ManuallyDrop;
+use crate::os::unix::io::{AsFd, BorrowedFd, FromRawFd};
+use crate::sys::fd::FileDesc;
+use sgx_libc as libc;
 
 pub struct Stdin(());
 pub struct Stdout(());
 pub struct Stderr(());
 
 impl Stdin {
-    pub fn new() -> io::Result<Stdin> {
-        Ok(Stdin(()))
+    pub const fn new() -> Stdin {
+        Stdin(())
     }
 }
 
 impl io::Read for Stdin {
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDIN_FILENO)).read(buf)
+        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read(buf) }
     }
 
     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDIN_FILENO)).read_vectored(bufs)
+        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDIN_FILENO)).read_vectored(bufs) }
+    }
+
+    #[inline]
+    fn is_read_vectored(&self) -> bool {
+        true
     }
 }
 
 impl Stdout {
-    pub fn new() -> io::Result<Stdout> {
-        Ok(Stdout(()))
+    pub const fn new() -> Stdout {
+        Stdout(())
     }
 }
 
 impl io::Write for Stdout {
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDOUT_FILENO)).write(buf)
+        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write(buf) }
     }
 
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDOUT_FILENO)).write_vectored(bufs)
+        unsafe {
+            ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDOUT_FILENO)).write_vectored(bufs)
+        }
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
     }
 
     fn flush(&mut self) -> io::Result<()> {
@@ -60,18 +74,25 @@
 }
 
 impl Stderr {
-    pub fn new() -> io::Result<Stderr> {
-        Ok(Stderr(()))
+    pub const fn new() -> Stderr {
+        Stderr(())
     }
 }
 
 impl io::Write for Stderr {
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDERR_FILENO)).write(buf)
+        unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write(buf) }
     }
 
     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
-        ManuallyDrop::new(FileDesc::new(libc::STDERR_FILENO)).write_vectored(bufs)
+        unsafe {
+            ManuallyDrop::new(FileDesc::from_raw_fd(libc::STDERR_FILENO)).write_vectored(bufs)
+        }
+    }
+
+    #[inline]
+    fn is_write_vectored(&self) -> bool {
+        true
     }
 
     fn flush(&mut self) -> io::Result<()> {
@@ -86,5 +107,47 @@
 pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
 
 pub fn panic_output() -> Option<impl io::Write> {
-    Stderr::new().ok()
-}
\ No newline at end of file
+    Some(Stderr::new())
+}
+
+impl AsFd for io::Stdin {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
+    }
+}
+
+impl<'a> AsFd for io::StdinLock<'a> {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDIN_FILENO) }
+    }
+}
+
+impl AsFd for io::Stdout {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
+    }
+}
+
+impl<'a> AsFd for io::StdoutLock<'a> {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDOUT_FILENO) }
+    }
+}
+
+impl AsFd for io::Stderr {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
+    }
+}
+
+impl<'a> AsFd for io::StderrLock<'a> {
+    #[inline]
+    fn as_fd(&self) -> BorrowedFd<'_> {
+        unsafe { BorrowedFd::borrow_raw_fd(libc::STDERR_FILENO) }
+    }
+}
diff --git a/sgx_tstd/src/sys/thread.rs b/sgx_tstd/src/sys/thread.rs
index 15f6a72..d4b0eb8 100644
--- a/sgx_tstd/src/sys/thread.rs
+++ b/sgx_tstd/src/sys/thread.rs
@@ -15,15 +15,18 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_types::{sgx_ocalloc, sgx_ocfree, sgx_status_t};
-use core::cmp;
-use core::mem;
-use core::ptr;
+use crate::cmp;
 use crate::ffi::CStr;
 use crate::io;
+use crate::mem;
+use crate::num::NonZeroUsize;
+use crate::ptr;
 use crate::sys::os;
 use crate::time::Duration;
 
+use sgx_trts::enclave;
+use sgx_types::{sgx_ocalloc, sgx_ocfree, sgx_status_t};
+
 pub struct Thread {
     id: libc::pthread_t,
 }
@@ -62,6 +65,11 @@
         }
     }
 
+    pub fn yield_now() {
+        let ret = unsafe { libc::sched_yield() };
+        debug_assert_eq!(ret, 0);
+    }
+
     pub fn set_name(name: &CStr) {
         const PR_SET_NAME: libc::c_int = 15;
         // pthread wrapper only appeared in glibc 2.12, so we use syscall
@@ -77,11 +85,6 @@
         }
     }
 
-    pub fn yield_now() {
-        let ret = unsafe { libc::sched_yield() };
-        debug_assert_eq!(ret, 0);
-    }
-
     pub fn sleep(dur: Duration) {
         let mut secs = dur.as_secs();
         let mut nsecs = dur.subsec_nanos() as _;
@@ -91,11 +94,12 @@
         unsafe {
             while secs > 0 || nsecs > 0 {
                 let mut ts = libc::timespec {
-                    tv_sec: cmp::min(libc::time_t::max_value() as u64, secs) as libc::time_t,
+                    tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
                     tv_nsec: nsecs,
                 };
                 secs -= ts.tv_sec as u64;
-                if libc::nanosleep(&ts, &mut ts) == -1 {
+                let ts_ptr = &mut ts as *mut _;
+                if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
                     assert_eq!(os::errno(), libc::EINTR);
                     secs += ts.tv_sec as u64;
                     nsecs = ts.tv_nsec;
@@ -132,7 +136,16 @@
     }
 }
 
+pub fn available_concurrency() -> io::Result<NonZeroUsize> {
+    let cpus = enclave::rsgx_get_cpu_core_num();
+    NonZeroUsize::new(cpus as usize).ok_or_else(|| io::Error::new_const(
+        io::ErrorKind::NotFound,
+        &"The number of hardware threads is not known for the target platform",
+    ))
+}
+
 mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{sched_yield, nanosleep, prctl};
-}
\ No newline at end of file
+    pub use sgx_libc::ocall::{sched_yield, nanosleep, prctl};
+    pub use sgx_libc::*;
+    
+}
diff --git a/sgx_tstd/src/sys/fast_thread_local.rs b/sgx_tstd/src/sys/thread_local_dtor.rs
similarity index 90%
rename from sgx_tstd/src/sys/fast_thread_local.rs
rename to sgx_tstd/src/sys/thread_local_dtor.rs
index df6cdf4..3a48c42 100644
--- a/sgx_tstd/src/sys/fast_thread_local.rs
+++ b/sgx_tstd/src/sys/thread_local_dtor.rs
@@ -22,9 +22,7 @@
 // Note, however, that we run on lots older linuxes, as well as cross
 // compiling from a newer linux to an older linux, so we also have a
 // fallback implementation to use as well.
-//
-// Due to rust-lang/rust#18804, make sure this is not generic!
 pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
-    use crate::sys_common::thread_local::register_dtor_fallback;
+    use crate::sys_common::thread_local_dtor::register_dtor_fallback;
     register_dtor_fallback(t, dtor);
 }
diff --git a/sgx_tstd/src/sys/thread_local.rs b/sgx_tstd/src/sys/thread_local_key.rs
similarity index 95%
rename from sgx_tstd/src/sys/thread_local.rs
rename to sgx_tstd/src/sys/thread_local_key.rs
index 0aa9d01..9467982 100644
--- a/sgx_tstd/src/sys/thread_local.rs
+++ b/sgx_tstd/src/sys/thread_local_key.rs
@@ -17,7 +17,8 @@
 
 #![allow(dead_code)] // not used on all platforms
 
-use core::mem;
+use crate::mem;
+use sgx_libc as libc;
 
 pub type Key = libc::pthread_key_t;
 
@@ -49,7 +50,3 @@
 pub fn requires_synchronized_create() -> bool {
     false
 }
-
-mod libc {
-    pub use sgx_trts::libc::*;
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys/time.rs b/sgx_tstd/src/sys/time.rs
index 89940c4..9f28de2 100644
--- a/sgx_tstd/src/sys/time.rs
+++ b/sgx_tstd/src/sys/time.rs
@@ -15,12 +15,15 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use sgx_trts::libc;
-use core::cmp::Ordering;
-use core::hash::{Hash, Hasher};
-use core::convert::TryInto;
+use crate::cmp::Ordering;
 use crate::time::Duration;
+
+use core::hash::{Hash, Hasher};
+
 pub use self::inner::{Instant, SystemTime, UNIX_EPOCH};
+use crate::convert::TryInto;
+
+use sgx_libc as libc;
 
 const NSEC_PER_SEC: u64 = 1_000_000_000;
 
@@ -36,17 +39,29 @@
 
     fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
         if self >= other {
-            Ok(if self.t.tv_nsec >= other.t.tv_nsec {
-                Duration::new(
-                    (self.t.tv_sec - other.t.tv_sec) as u64,
-                    (self.t.tv_nsec - other.t.tv_nsec) as u32,
-                )
+            // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
+            // to optimize it into a branchless form (see also #75545):
+            //
+            // 1. `self.t.tv_sec - other.t.tv_sec` shows up as a common expression
+            //    in both branches, i.e. the `else` must have its `- 1`
+            //    subtraction after the common one, not interleaved with it
+            //    (it used to be `self.t.tv_sec - 1 - other.t.tv_sec`)
+            //
+            // 2. the `Duration::new` call (or any other additional complexity)
+            //    is outside of the `if`-`else`, not duplicated in both branches
+            //
+            // Ideally this code could be rearranged such that it more
+            // directly expresses the lower-cost behavior we want from it.
+            let (secs, nsec) = if self.t.tv_nsec >= other.t.tv_nsec {
+                ((self.t.tv_sec - other.t.tv_sec) as u64, (self.t.tv_nsec - other.t.tv_nsec) as u32)
             } else {
-                Duration::new(
-                    (self.t.tv_sec - 1 - other.t.tv_sec) as u64,
+                (
+                    (self.t.tv_sec - other.t.tv_sec - 1) as u64,
                     self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) - other.t.tv_nsec as u32,
                 )
-            })
+            };
+
+            Ok(Duration::new(secs, nsec))
         } else {
             match other.sub_timespec(self) {
                 Ok(d) => Err(d),
@@ -119,7 +134,7 @@
 }
 
 mod inner {
-    use core::fmt;
+    use crate::fmt;
     use crate::sys::cvt;
     use crate::time::Duration;
 
@@ -135,9 +150,7 @@
         t: Timespec,
     }
 
-    pub const UNIX_EPOCH: SystemTime = SystemTime {
-        t: Timespec::zero(),
-    };
+    pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
 
     impl Instant {
         pub fn now() -> Instant {
@@ -145,15 +158,12 @@
         }
 
         pub const fn zero() -> Instant {
-            Instant {
-                t: Timespec::zero(),
-            }
+            Instant { t: Timespec::zero() }
         }
 
         pub fn actually_monotonic() -> bool {
-            (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) ||
-            (cfg!(target_os = "linux") && cfg!(target_arch = "x86")) ||
-            false // last clause, used so `||` is always trailing above
+            (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64"))
+                || (cfg!(target_os = "linux") && cfg!(target_arch = "x86"))
         }
 
         pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -226,7 +236,7 @@
     }
 
     mod libc {
-        pub use sgx_trts::libc::*;
-        pub use sgx_trts::libc::ocall::clock_gettime;
+        pub use sgx_libc::ocall::clock_gettime;
+        pub use sgx_libc::*;
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/sys_common/at_exit_imp.rs b/sgx_tstd/src/sys_common/at_exit_imp.rs
index cfcbf9f..462a6f4 100644
--- a/sgx_tstd/src/sys_common/at_exit_imp.rs
+++ b/sgx_tstd/src/sys_common/at_exit_imp.rs
@@ -15,11 +15,13 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//use sync::SgxThreadMutex;
-use crate::sync::SgxThreadSpinlock;
+//! Implementation of running at_exit routines
+//!
+//! Documentation can be found on the `rt::at_exit` function.
 
-use core::ptr;
-use core::mem;
+use crate::mem;
+use crate::ptr;
+use crate::sync::SgxThreadSpinlock as ThreadSpinlock;
 
 type Queue = Vec<Box<dyn FnOnce()>>;
 
@@ -27,7 +29,7 @@
 // on poisoning and this module needs to operate at a lower level than requiring
 // the thread infrastructure to be in place (useful on the borders of
 // initialization/destruction).
-static LOCK: SgxThreadSpinlock = SgxThreadSpinlock::new();
+static LOCK: ThreadSpinlock = ThreadSpinlock::new();
 static mut QUEUE: *mut Queue = ptr::null_mut();
 const DONE: *mut Queue = 1_usize as *mut _;
 
diff --git a/sgx_tstd/src/sys_common/backtrace.rs b/sgx_tstd/src/sys_common/backtrace.rs
index ec49b4c..ab06fbb 100644
--- a/sgx_tstd/src/sys_common/backtrace.rs
+++ b/sgx_tstd/src/sys_common/backtrace.rs
@@ -17,24 +17,23 @@
 
 /// Common code for printing the backtrace in the same way across the different
 /// supported platforms.
-///
 pub use crate::sys_common::gnu::*;
 
-use crate::sys::backtrace::{self, BacktraceFmt, BytesOrWideString, PrintFmt};
+use crate::borrow::Cow;
+use crate::fmt;
 use crate::io;
 use crate::io::prelude::*;
 use crate::path::{Path, PathBuf};
-use crate::sync::SgxThreadMutex;
-use core::fmt;
-use core::sync::atomic::{self, Ordering};
-use alloc_crate::borrow::Cow;
+use crate::sync::atomic::{self, Ordering};
+use crate::sync::SgxThreadMutex as ThreadMutex;
+use crate::sys::backtrace::{self, BacktraceFmt, BytesOrWideString, PrintFmt};
 
 /// Max number of frames to print.
 const MAX_NB_FRAMES: usize = 100;
 
-pub fn lock() -> impl Drop {
+pub unsafe fn lock() -> impl Drop {
     struct Guard;
-    static LOCK: SgxThreadMutex = SgxThreadMutex::new();
+    static LOCK: ThreadMutex = ThreadMutex::new();
 
     impl Drop for Guard {
         fn drop(&mut self) {
@@ -44,10 +43,8 @@
         }
     }
 
-    unsafe {
-        LOCK.lock();
-        Guard
-    }
+    LOCK.lock();
+    Guard
 }
 
 /// Prints the current backtrace.
@@ -83,6 +80,8 @@
     bt_fmt.add_context()?;
     let mut idx = 0;
     let mut res = Ok(());
+    // Start immediately if we're not using a short backtrace.
+    let mut start = print_fmt != PrintFmt::Short;
     backtrace::trace_unsynchronized(|frame| {
         if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
             return false;
@@ -94,25 +93,30 @@
             hit = true;
             if print_fmt == PrintFmt::Short {
                 if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
-                    if sym.contains("__rust_begin_short_backtrace") {
+                    if start && sym.contains("__rust_begin_short_backtrace") {
                         stop = true;
                         return;
                     }
+                    if sym.contains("__rust_end_short_backtrace") {
+                        start = true;
+                        return;
+                    }
                 }
             }
 
-            res = bt_fmt.frame().symbol(frame, symbol);
+            if start {
+                res = bt_fmt.frame().symbol(frame, symbol);
+            }
         });
         if stop {
             return false;
         }
-        if !hit {
+        if !hit && start {
             res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
         }
 
         idx += 1;
-        //res.is_ok()
-        true
+        res.is_ok()
     });
     res?;
     bt_fmt.finish()?;
@@ -133,10 +137,29 @@
 pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T
 where
     F: FnOnce() -> T,
-    F: Send,
-    T: Send,
 {
-    f()
+    let result = f();
+
+    // prevent this frame from being tail-call optimised away
+    crate::hint::black_box(());
+
+    result
+}
+
+/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. Note that
+/// this is only inline(never) when backtraces in libstd are enabled, otherwise
+/// it's fine to optimize away.
+#[cfg_attr(feature = "backtrace", inline(never))]
+pub fn __rust_end_short_backtrace<F, T>(f: F) -> T
+where
+    F: FnOnce() -> T,
+{
+    let result = f();
+
+    // prevent this frame from being tail-call optimised away
+    crate::hint::black_box(());
+
+    result
 }
 
 pub enum RustBacktrace {
diff --git a/sgx_tstd/src/sys_common/bytestring.rs b/sgx_tstd/src/sys_common/bytestring.rs
deleted file mode 100644
index 251a800..0000000
--- a/sgx_tstd/src/sys_common/bytestring.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-#![allow(dead_code)]
-
-use core::fmt::{Formatter, Result, Write};
-use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
-
-pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
-    // Writes out a valid unicode string with the correct escape sequences
-    fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
-        for c in s.chars().flat_map(|c| c.escape_debug()) {
-            f.write_char(c)?
-        }
-        Ok(())
-    }
-
-    f.write_str("\"")?;
-    for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {
-        write_str_escaped(f, valid)?;
-        for b in broken {
-            write!(f, "\\x{:02X}", b)?;
-        }
-    }
-    f.write_str("\"")
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys_common/condvar.rs b/sgx_tstd/src/sys_common/condvar.rs
new file mode 100644
index 0000000..7d045e2
--- /dev/null
+++ b/sgx_tstd/src/sys_common/condvar.rs
@@ -0,0 +1,139 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use core::time::Duration;
+use crate::sys::condvar as imp;
+use crate::sys::mutex as mutex_imp;
+use crate::sys_common::mutex::{SgxMovableThreadMutex, SgxThreadMutex};
+
+use sgx_types::SysError;
+
+mod check;
+
+pub struct SgxThreadCondvar(imp::SgxThreadCondvar);
+
+unsafe impl Send for SgxThreadCondvar {}
+unsafe impl Sync for SgxThreadCondvar {}
+
+impl SgxThreadCondvar {
+    pub const fn new() -> SgxThreadCondvar {
+        SgxThreadCondvar(imp::SgxThreadCondvar::new())
+    }
+
+    #[inline]
+    pub unsafe fn wait(&self, mutex: &SgxThreadMutex) -> SysError {
+        self.0.wait(mutex.raw())
+    }
+
+    #[inline]
+    pub unsafe fn wait_timeout(&self, mutex: &SgxThreadMutex, dur: Duration) -> SysError {
+        self.0.wait_timeout(mutex.raw(), dur)
+    }
+
+    #[inline]
+    pub unsafe fn signal(&self) -> SysError {
+        self.0.signal()
+    }
+
+    #[inline]
+    pub unsafe fn broadcast(&self) -> SysError {
+        self.0.broadcast()
+    }
+
+    #[inline]
+    pub unsafe fn notify_one(&self) -> SysError {
+        self.signal()
+    }
+
+    #[inline]
+    pub unsafe fn notify_all(&self) -> SysError {
+        self.broadcast()
+    }
+
+    #[inline]
+    pub unsafe fn destroy(&self) -> SysError {
+        self.0.destroy()
+    }
+}
+
+type CondvarCheck = <mutex_imp::SgxMovableThreadMutex as check::CondvarCheck>::Check;
+
+/// An OS-based condition variable.
+pub struct SgxMovableThreadCondvar {
+    inner: imp::SgxMovableThreadCondvar,
+    check: CondvarCheck,
+}
+
+impl SgxMovableThreadCondvar {
+    /// Creates a new condition variable for use.
+    pub fn new() -> SgxMovableThreadCondvar {
+        let c = imp::SgxMovableThreadCondvar::from(imp::SgxThreadCondvar::new());
+        SgxMovableThreadCondvar { inner: c, check: CondvarCheck::new() }
+    }
+
+    #[inline]
+    pub unsafe fn signal(&self) -> SysError {
+        self.inner.signal()
+    }
+
+    #[inline]
+    pub unsafe fn broadcast(&self) -> SysError {
+        self.inner.broadcast()
+    }
+
+    /// Signals one waiter on this condition variable to wake up.
+    #[inline]
+    pub unsafe fn notify_one(&self) -> SysError {
+        self.signal()
+    }
+
+    /// Awakens all current waiters on this condition variable.
+    #[inline]
+    pub unsafe fn notify_all(&self) -> SysError {
+        self.broadcast()
+    }
+
+    /// Waits for a signal on the specified mutex.
+    ///
+    /// Behavior is undefined if the mutex is not locked by the current thread.
+    ///
+    /// May panic if used with more than one mutex.
+    #[inline]
+    pub unsafe fn wait(&self, mutex: &SgxMovableThreadMutex) -> SysError {
+        self.check.verify(mutex);
+        self.inner.wait(mutex.raw())
+    }
+
+    /// Waits for a signal on the specified mutex with a timeout duration
+    /// specified by `dur` (a relative time into the future).
+    ///
+    /// Behavior is undefined if the mutex is not locked by the current thread.
+    ///
+    /// May panic if used with more than one mutex.
+    #[inline]
+    pub unsafe fn wait_timeout(&self, mutex: &SgxMovableThreadMutex, dur: Duration) -> SysError {
+        self.check.verify(mutex);
+        self.inner.wait_timeout(mutex.raw(), dur)
+    }
+}
+
+impl Drop for SgxMovableThreadCondvar {
+    fn drop(&mut self) {
+        let r = unsafe { self.inner.destroy() };
+        debug_assert_eq!(r, Ok(()));
+    }
+}
diff --git a/sgx_tstd/src/sys_common/condvar/check.rs b/sgx_tstd/src/sys_common/condvar/check.rs
new file mode 100644
index 0000000..450eedf
--- /dev/null
+++ b/sgx_tstd/src/sys_common/condvar/check.rs
@@ -0,0 +1,65 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::sync::atomic::{AtomicUsize, Ordering};
+use crate::sys::mutex as mutex_imp;
+use crate::sys_common::mutex::SgxMovableThreadMutex;
+
+pub trait CondvarCheck {
+    type Check;
+}
+
+/// For boxed mutexes, a `Condvar` will check it's only ever used with the same
+/// mutex, based on its (stable) address.
+impl CondvarCheck for Box<mutex_imp::SgxThreadMutex> {
+    type Check = SameMutexCheck;
+}
+
+pub struct SameMutexCheck {
+    addr: AtomicUsize,
+}
+
+#[allow(dead_code)]
+impl SameMutexCheck {
+    pub const fn new() -> Self {
+        Self { addr: AtomicUsize::new(0) }
+    }
+    pub fn verify(&self, mutex: &SgxMovableThreadMutex) {
+        let addr = mutex.raw() as *const mutex_imp::SgxThreadMutex as usize;
+        match self.addr.compare_exchange(0, addr, Ordering::SeqCst, Ordering::SeqCst) {
+            Ok(_) => {}               // Stored the address
+            Err(n) if n == addr => {} // Lost a race to store the same address
+            _ => panic!("attempted to use a condition variable with two mutexes"),
+        }
+    }
+}
+
+/// Unboxed mutexes may move, so `Condvar` can not require its address to stay
+/// constant.
+impl CondvarCheck for mutex_imp::SgxThreadMutex {
+    type Check = NoCheck;
+}
+
+pub struct NoCheck;
+
+#[allow(dead_code)]
+impl NoCheck {
+    pub const fn new() -> Self {
+        Self
+    }
+    pub fn verify(&self, _: &SgxMovableThreadMutex) {}
+}
diff --git a/sgx_tstd/src/sys_common/fs.rs b/sgx_tstd/src/sys_common/fs.rs
index b13b886..0b6ea93 100644
--- a/sgx_tstd/src/sys_common/fs.rs
+++ b/sgx_tstd/src/sys_common/fs.rs
@@ -14,13 +14,30 @@
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License..
-#![allow(dead_code)]
-#[cfg(feature = "untrusted_fs")]
-use crate::fs;
-#[cfg(not(feature = "untrusted_fs"))]
-use crate::untrusted::fs;
-use crate::io;
+use crate::io::{self, Error, ErrorKind};
 use crate::path::Path;
+use crate::untrusted::fs;
+
+pub(crate) const NOT_FILE_ERROR: Error = Error::new_const(
+    ErrorKind::InvalidInput,
+    &"the source path is neither a regular file nor a symlink to a regular file",
+);
+
+pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
+    let mut reader = fs::File::open(from)?;
+    let metadata = reader.metadata()?;
+
+    if !metadata.is_file() {
+        return Err(NOT_FILE_ERROR);
+    }
+
+    let mut writer = fs::File::create(to)?;
+    let perm = metadata.permissions();
+
+    let ret = io::copy(&mut reader, &mut writer)?;
+    writer.set_permissions(perm)?;
+    Ok(ret)
+}
 
 pub fn remove_dir_all(path: &Path) -> io::Result<()> {
     let filetype = fs::symlink_metadata(path)?.file_type();
@@ -37,4 +54,12 @@
         }
     }
     fs::remove_dir(path)
-}
\ No newline at end of file
+}
+
+pub fn try_exists(path: &Path) -> io::Result<bool> {
+    match fs::metadata(path) {
+        Ok(_) => Ok(true),
+        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
+        Err(error) => Err(error),
+    }
+}
diff --git a/sgx_tstd/src/sys_common/gnu/libbacktrace.rs b/sgx_tstd/src/sys_common/gnu/libbacktrace.rs
index 8e9c227..00b611c 100644
--- a/sgx_tstd/src/sys_common/gnu/libbacktrace.rs
+++ b/sgx_tstd/src/sys_common/gnu/libbacktrace.rs
@@ -15,16 +15,21 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::{ptr, mem, slice};
-use crate::sys::backtrace::{self, BytesOrWideString, Bomb};
+use crate::marker;
+use crate::mem;
+use crate::ptr;
+use crate::slice;
+use crate::sys::backtrace::{self, Bomb, BytesOrWideString};
 use crate::sys_common::backtrace::{ResolveWhat, SymbolName};
+
 use sgx_backtrace_sys as bt;
 use sgx_libc::{self, c_char, c_int, c_void, uintptr_t};
 
-pub enum Symbol {
+pub enum Symbol<'a> {
     Syminfo {
         pc: uintptr_t,
         symname: *const c_char,
+        _marker: marker::PhantomData<&'a ()>,
     },
     Pcinfo {
         pc: uintptr_t,
@@ -35,8 +40,8 @@
     },
 }
 
-impl Symbol {
-    pub fn name(&self) -> Option<SymbolName> {
+impl Symbol<'_> {
+    pub fn name(&self) -> Option<SymbolName<'_>> {
         let symbol = |ptr: *const c_char| unsafe {
             if ptr.is_null() {
                 None
@@ -76,10 +81,7 @@
                 None
             } else {
                 let len = sgx_libc::strlen(ptr);
-                Some(slice::from_raw_parts(
-                    ptr as *const u8,
-                    len,
-                ))
+                Some(slice::from_raw_parts(ptr as *const u8, len))
             }
         };
         match *self {
@@ -116,7 +118,7 @@
         }
     }
 
-    pub fn filename_bytes(&self) -> Option<&[u8]> {
+    fn filename_bytes(&self) -> Option<&[u8]> {
         match *self {
             Symbol::Syminfo { .. } => None,
             Symbol::Pcinfo { filename, .. } => {
@@ -132,7 +134,7 @@
         }
     }
 
-    pub fn filename_raw(&self) -> Option<BytesOrWideString> {
+    pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
         self.filename_bytes().map(BytesOrWideString::Bytes)
     }
 
@@ -142,6 +144,10 @@
             Symbol::Pcinfo { lineno, .. } => Some(lineno as u32),
         }
     }
+
+    pub fn colno(&self) -> Option<u32> {
+        None
+    }
 }
 
 extern "C" fn error_cb(_data: *mut c_void, _msg: *const c_char, _errnum: c_int) {
@@ -176,7 +182,7 @@
     // not debug info, so if that happens we're sure to call the callback with
     // at least one symbol from the `syminfo_cb`.
     unsafe {
-        let syminfo_state = &mut *(data as *mut SyminfoState);
+        let syminfo_state = &mut *(data as *mut SyminfoState<'_>);
         let mut pcinfo_state = PcinfoState {
             symname,
             called: false,
@@ -191,14 +197,16 @@
         );
         if !pcinfo_state.called {
             let inner = Symbol::Syminfo {
-                pc: pc,
-                symname: symname,
+                pc,
+                symname,
+                _marker: marker::PhantomData,
             };
             (pcinfo_state.cb)(&super::Symbol {
                 name: inner.name_bytes().map(|m| m.to_vec()),
                 addr: inner.addr(),
                 filename: inner.filename_bytes().map(|m| m.to_vec()),
                 lineno: inner.lineno(),
+                colno: inner.colno(),
             });
         }
     }
@@ -229,9 +237,9 @@
         let state = &mut *(data as *mut PcinfoState);
         state.called = true;
         let inner = Symbol::Pcinfo {
-            pc: pc,
-            filename: filename,
-            lineno: lineno,
+            pc,
+            filename,
+            lineno,
             symname: state.symname,
             function,
         };
@@ -240,11 +248,12 @@
             addr: inner.addr(),
             filename: inner.filename_bytes().map(|m| m.to_vec()),
             lineno: inner.lineno(),
+            colno: inner.colno(),
         });
     }
 
     bomb.set(false);
-    return 0;
+    0
 }
 
 // The libbacktrace API supports creating a state, but it does not
@@ -279,7 +288,7 @@
     };
 
     STATE = bt::backtrace_create_state(
-        filename,
+        filename.cast(),
         // Don't exercise threadsafe capabilities of libbacktrace since
         // we're always calling it in a synchronized fashion.
         0,
@@ -290,7 +299,7 @@
     STATE
 }
 
-pub unsafe fn resolve(what: ResolveWhat, cb: &mut dyn FnMut(&super::Symbol)) {
+pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
     let symaddr = what.address_or_ip() as usize;
 
     // backtrace errors are currently swept under the rug
@@ -299,27 +308,20 @@
         return;
     }
 
-    // Call the `backtrace_syminfo` API first. This is (from reading the code)
-    // guaranteed to call `syminfo_cb` exactly once (or fail with an error
+    // Call the `backtrace_syminfo` API which (from reading the code)
+    // should call `syminfo_cb` exactly once (or fail with an error
     // presumably). We then handle more within the `syminfo_cb`.
     //
     // Note that we do this since `syminfo` will consult the symbol table,
     // finding symbol names even if there's no debug information in the binary.
-    let mut called = false;
-    {
-        let mut syminfo_state = SyminfoState {
-            pc: symaddr,
-            cb: &mut |sym| {
-                called = true;
-                cb(sym);
-            },
-        };
-        bt::backtrace_syminfo(
-            state,
-            symaddr as uintptr_t,
-            syminfo_cb,
-            error_cb,
-            &mut syminfo_state as *mut _ as *mut _,
-        );
-    }
+    let mut syminfo_state = SyminfoState { pc: symaddr, cb };
+    bt::backtrace_syminfo(
+        state,
+        symaddr as uintptr_t,
+        syminfo_cb,
+        error_cb,
+        &mut syminfo_state as *mut _ as *mut _,
+    );
 }
+
+pub unsafe fn clear_symbol_cache() {}
diff --git a/sgx_tstd/src/sys_common/gnu/mod.rs b/sgx_tstd/src/sys_common/gnu/mod.rs
index 6ff7ca7..67985f4 100644
--- a/sgx_tstd/src/sys_common/gnu/mod.rs
+++ b/sgx_tstd/src/sys_common/gnu/mod.rs
@@ -15,11 +15,12 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use core::{fmt, str};
-use core::ffi::c_void;
-use alloc_crate::vec::Vec;
-use crate::sys::backtrace::Frame;
+use crate::ffi::c_void;
+use crate::fmt;
+use crate::str;
 use crate::sys::backtrace::BytesOrWideString;
+use crate::sys::backtrace::Frame;
+
 use sgx_demangle::{try_demangle, Demangle};
 
 pub enum ResolveWhat<'a> {
@@ -112,6 +113,7 @@
     addr: Option<*mut c_void>,
     filename: Option<Vec<u8>>,
     lineno: Option<u32>,
+    colno: Option<u32>,
 }
 
 impl Symbol {
@@ -124,19 +126,29 @@
     /// * The raw `str` value of the symbol can be accessed (if it's valid
     ///   utf-8).
     /// * The raw bytes for the symbol name can be accessed.
-    pub fn name(&self) -> Option<SymbolName> {
+    pub fn name(&self) -> Option<SymbolName<'_>> {
         self.name.as_ref().map(|m| SymbolName::new(m.as_slice()))
     }
 
     /// Returns the starting address of this function.
     pub fn addr(&self) -> Option<*mut c_void> {
-       self.addr
+        self.addr
     }
 
     /// Returns the raw filename as a slice. This is mainly useful for `no_std`
     /// environments.
-    pub fn filename_raw(&self) -> Option<BytesOrWideString> {
-        self.filename.as_ref().map(|f| BytesOrWideString::Bytes(f.as_slice()))
+    pub fn filename_raw(&self) -> Option<BytesOrWideString<'_>> {
+        self.filename
+            .as_ref()
+            .map(|f| BytesOrWideString::Bytes(f.as_slice()))
+    }
+
+    /// Returns the column number for where this symbol is currently executing.
+    ///
+    /// Only gimli currently provides a value here and even then only if `filename`
+    /// returns `Some`, and so it is then consequently subject to similar caveats.
+    pub fn colno(&self) -> Option<u32> {
+        self.colno
     }
 
     /// Returns the line number for where this symbol is currently executing.
@@ -149,7 +161,7 @@
 }
 
 impl fmt::Debug for Symbol {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         let mut d = f.debug_struct("Symbol");
         if let Some(name) = self.name() {
             d.field("name", &name);
@@ -168,6 +180,9 @@
         if let Some(lineno) = self.lineno() {
             d.field("lineno", &lineno);
         }
+        if let Some(colno) = self.colno() {
+            d.field("colno", &colno);
+        }
         d.finish()
     }
 }
@@ -188,8 +203,8 @@
         let demangled = str_bytes.and_then(|s| try_demangle(s).ok());
 
         SymbolName {
-            bytes: bytes,
-            demangled: demangled,
+            bytes,
+            demangled,
         }
     }
 
@@ -210,11 +225,11 @@
 }
 
 fn format_symbol_name(
-    fmt: fn(&str, &mut fmt::Formatter) -> fmt::Result,
+    fmt: fn(&str, &mut fmt::Formatter<'_>) -> fmt::Result,
     mut bytes: &[u8],
-    f: &mut fmt::Formatter,
+    f: &mut fmt::Formatter<'_>,
 ) -> fmt::Result {
-    while bytes.len() > 0 {
+    while !bytes.is_empty() {
         match str::from_utf8(bytes) {
             Ok(name) => {
                 fmt(name, f)?;
@@ -234,7 +249,7 @@
 }
 
 impl<'a> fmt::Display for SymbolName<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if let Some(ref s) = self.demangled {
             s.fmt(f)
         } else {
@@ -244,7 +259,7 @@
 }
 
 impl<'a> fmt::Debug for SymbolName<'a> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if let Some(ref s) = self.demangled {
             s.fmt(f)
         } else {
@@ -255,4 +270,3 @@
 
 mod libbacktrace;
 use self::libbacktrace::resolve as resolve_imp;
-
diff --git a/sgx_tstd/src/sys_common/io.rs b/sgx_tstd/src/sys_common/io.rs
index c42faee..0e7a369 100644
--- a/sgx_tstd/src/sys_common/io.rs
+++ b/sgx_tstd/src/sys_common/io.rs
@@ -15,48 +15,4 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use crate::io;
-use crate::io::ErrorKind;
-use crate::io::Read;
-use alloc_crate::slice::from_raw_parts_mut;
-use alloc_crate::vec::Vec;
-
 pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
-
-// Provides read_to_end functionality over an uninitialized buffer.
-// This function is unsafe because it calls the underlying
-// read function with a slice into uninitialized memory. The default
-// implementation of read_to_end for readers will zero out new memory in
-// the buf before passing it to read, but avoiding this zero can often
-// lead to a fairly significant performance win.
-//
-// Implementations using this method have to adhere to two guarantees:
-//  *  The implementation of read never reads the buffer provided.
-//  *  The implementation of read correctly reports how many bytes were written.
-#[allow(dead_code)]
-pub unsafe fn read_to_end_uninitialized(r: &mut dyn Read, buf: &mut Vec<u8>) -> io::Result<usize> {
-    let start_len = buf.len();
-    buf.reserve(16);
-
-    // Always try to read into the empty space of the vector (from the length to the capacity).
-    // If the vector ever fills up then we reserve an extra byte which should trigger the normal
-    // reallocation routines for the vector, which will likely double the size.
-    //
-    // This function is similar to the read_to_end function in std::io, but the logic about
-    // reservations and slicing is different enough that this is duplicated here.
-    loop {
-        if buf.len() == buf.capacity() {
-            buf.reserve(1);
-        }
-
-        let buf_slice = from_raw_parts_mut(buf.as_mut_ptr().offset(buf.len() as isize),
-                                           buf.capacity() - buf.len());
-
-        match r.read(buf_slice) {
-            Ok(0) => { return Ok(buf.len() - start_len); }
-            Ok(n) => { let len = buf.len() + n; buf.set_len(len); },
-            Err(ref e) if e.kind() == ErrorKind::Interrupted => { }
-            Err(e) => { return Err(e); }
-        }
-    }
-}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys_common/memchr.rs b/sgx_tstd/src/sys_common/memchr.rs
index 47f15e4..de5e2e2 100644
--- a/sgx_tstd/src/sys_common/memchr.rs
+++ b/sgx_tstd/src/sys_common/memchr.rs
@@ -15,135 +15,48 @@
 // specific language governing permissions and limitations
 // under the License..
 
-#[allow(dead_code)]
-pub mod fallback {
-    use core::cmp;
-    use core::mem;
+use crate::sys::memchr as sys;
 
-    const LO_U64: u64 = 0x0101010101010101;
-    const HI_U64: u64 = 0x8080808080808080;
+/// A safe interface to `memchr`.
+///
+/// Returns the index corresponding to the first occurrence of `needle` in
+/// `haystack`, or `None` if one is not found.
+///
+/// memchr reduces to super-optimized machine code at around an order of
+/// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
+/// (See benchmarks.)
+///
+/// # Examples
+///
+/// This shows how to find the first position of a byte in a byte string.
+///
+/// ```ignore (cannot-doctest-private-modules)
+/// use memchr::memchr;
+///
+/// let haystack = b"the quick brown fox";
+/// assert_eq!(memchr(b'k', haystack), Some(8));
+/// ```
+#[inline]
+pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+    sys::memchr(needle, haystack)
+}
 
-    // use truncation
-    const LO_USIZE: usize = LO_U64 as usize;
-    const HI_USIZE: usize = HI_U64 as usize;
-
-    /// Return `true` if `x` contains any zero byte.
-    ///
-    /// From *Matters Computational*, J. Arndt
-    ///
-    /// "The idea is to subtract one from each of the bytes and then look for
-    /// bytes where the borrow propagated all the way to the most significant
-    /// bit."
-    #[inline]
-    fn contains_zero_byte(x: usize) -> bool {
-        x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
-    }
-
-    #[cfg(target_pointer_width = "32")]
-    #[inline]
-    fn repeat_byte(b: u8) -> usize {
-        let mut rep = (b as usize) << 8 | b as usize;
-        rep = rep << 16 | rep;
-        rep
-    }
-
-    #[cfg(target_pointer_width = "64")]
-    #[inline]
-    fn repeat_byte(b: u8) -> usize {
-        let mut rep = (b as usize) << 8 | b as usize;
-        rep = rep << 16 | rep;
-        rep = rep << 32 | rep;
-        rep
-    }
-
-    /// Return the first index matching the byte `a` in `text`.
-    pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
-        // Scan for a single byte value by reading two `usize` words at a time.
-        //
-        // Split `text` in three parts
-        // - unaligned initial part, before the first word aligned address in text
-        // - body, scan by 2 words at a time
-        // - the last remaining part, < 2 word size
-        let len = text.len();
-        let ptr = text.as_ptr();
-        let usize_bytes = mem::size_of::<usize>();
-
-        // search up to an aligned boundary
-        let mut offset = ptr.align_offset(usize_bytes);
-        if offset > 0 {
-            offset = cmp::min(offset, len);
-            if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
-                return Some(index);
-            }
-        }
-
-        // search the body of the text
-        let repeated_x = repeat_byte(x);
-
-        if len >= 2 * usize_bytes {
-            while offset <= len - 2 * usize_bytes {
-                unsafe {
-                    let u = *(ptr.offset(offset as isize) as *const usize);
-                    let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize);
-
-                    // break if there is a matching byte
-                    let zu = contains_zero_byte(u ^ repeated_x);
-                    let zv = contains_zero_byte(v ^ repeated_x);
-                    if zu || zv {
-                        break;
-                    }
-                }
-                offset += usize_bytes * 2;
-            }
-        }
-
-        // find the byte after the point the body loop stopped
-        text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i)
-    }
-
-    /// Return the last index matching the byte `a` in `text`.
-    pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
-        // Scan for a single byte value by reading two `usize` words at a time.
-        //
-        // Split `text` in three parts
-        // - unaligned tail, after the last word aligned address in text
-        // - body, scan by 2 words at a time
-        // - the first remaining bytes, < 2 word size
-        let len = text.len();
-        let ptr = text.as_ptr();
-        let usize_bytes = mem::size_of::<usize>();
-
-        // search to an aligned boundary
-        let end_align = (ptr as usize + len) & (usize_bytes - 1);
-        let mut offset;
-        if end_align > 0 {
-            offset = if end_align >= len { 0 } else { len - end_align };
-            if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
-                return Some(offset + index);
-            }
-        } else {
-            offset = len;
-        }
-
-        // search the body of the text
-        let repeated_x = repeat_byte(x);
-
-        while offset >= 2 * usize_bytes {
-            unsafe {
-                let u = *(ptr.offset(offset as isize - 2 * usize_bytes as isize) as *const usize);
-                let v = *(ptr.offset(offset as isize - usize_bytes as isize) as *const usize);
-
-                // break if there is a matching byte
-                let zu = contains_zero_byte(u ^ repeated_x);
-                let zv = contains_zero_byte(v ^ repeated_x);
-                if zu || zv {
-                    break;
-                }
-            }
-            offset -= 2 * usize_bytes;
-        }
-
-        // find the byte before the point the body loop stopped
-        text[..offset].iter().rposition(|elt| *elt == x)
-    }
+/// A safe interface to `memrchr`.
+///
+/// Returns the index corresponding to the last occurrence of `needle` in
+/// `haystack`, or `None` if one is not found.
+///
+/// # Examples
+///
+/// This shows how to find the last position of a byte in a byte string.
+///
+/// ```ignore (cannot-doctest-private-modules)
+/// use memchr::memrchr;
+///
+/// let haystack = b"the quick brown fox";
+/// assert_eq!(memrchr(b'o', haystack), Some(17));
+/// ```
+#[inline]
+pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
+    sys::memrchr(needle, haystack)
 }
diff --git a/sgx_tstd/src/sys_common/mod.rs b/sgx_tstd/src/sys_common/mod.rs
index 655be88..12e78ac 100644
--- a/sgx_tstd/src/sys_common/mod.rs
+++ b/sgx_tstd/src/sys_common/mod.rs
@@ -15,52 +15,50 @@
 // specific language governing permissions and limitations
 // under the License..
 
-macro_rules! rtabort {
-    ($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*)))
-}
+//! Platform-independent platform abstraction
+//!
+//! This is the platform-independent portion of the standard library's
+//! platform abstraction layer, whereas `std::sys` is the
+//! platform-specific portion.
+//!
+//! The relationship between `std::sys_common`, `std::sys` and the
+//! rest of `std` is complex, with dependencies going in all
+//! directions: `std` depending on `sys_common`, `sys_common`
+//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
+//! This is because `sys_common` not only contains platform-independent code,
+//! but also code that is shared between the different platforms in `sys`.
+//! Ideally all that shared code should be moved to `sys::common`,
+//! and the dependencies between `std`, `sys_common` and `sys` all would form a dag.
+//! Progress on this is tracked in #84187.
 
-#[allow(unused_macros)]
-macro_rules! rtassert {
-    ($e:expr) => {
-        if !$e {
-            rtabort!(concat!("assertion failed: ", stringify!($e)));
-        }
-    };
-}
-
-#[allow(unused_macros)] // not used on all platforms
-macro_rules! rtunwrap {
-    ($ok:ident, $e:expr) => {
-        match $e {
-            $ok(v) => v,
-            ref err => {
-                let err = err.as_ref().map(drop); // map Ok/Some which might not be Debug
-                rtabort!(concat!("unwrap failed: ", stringify!($e), " = {:?}"), err)
-            }
-        }
-    };
-}
+#![allow(missing_docs)]
+#![allow(missing_debug_implementations)]
 
 pub mod at_exit_imp;
-pub mod os_str_bytes;
 #[cfg(feature = "backtrace")]
 pub mod backtrace;
+pub mod condvar;
+pub mod fs;
 #[cfg(feature = "backtrace")]
 pub mod gnu;
 pub mod io;
 pub mod memchr;
-pub mod poison;
-pub mod thread_info;
-#[cfg(feature = "thread")]
-pub mod thread;
-#[cfg(feature = "thread")]
-pub mod thread_local;
-pub mod util;
-pub mod wtf8;
+pub mod mutex;
 #[cfg(feature = "net")]
 pub mod net;
-pub mod bytestring;
-pub mod fs;
+pub mod remutex;
+#[macro_use]
+pub mod rt;
+pub mod rwlock;
+#[cfg(feature = "thread")]
+pub mod thread;
+pub mod thread_info;
+#[cfg(feature = "thread")]
+pub mod thread_local_dtor;
+#[cfg(feature = "thread")]
+pub mod thread_local_key;
+pub mod thread_parker;
+pub mod wtf8;
 
 /// A trait for viewing representations from std types
 #[doc(hidden)]
@@ -96,39 +94,11 @@
 /// closure will be run once the main thread exits. Returns `Err` to indicate
 /// that the closure could not be registered, meaning that it is not scheduled
 /// to be run.
+#[allow(clippy::result_unit_err)]
 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
     if at_exit_imp::push(Box::new(f)) { Ok(()) } else { Err(()) }
 }
 
-/// One-time runtime cleanup.
-//#[allow(dead_code)]
-//pub fn cleanup() {
-//
-//    use crate::sync::Once;
-//
-//    static CLEANUP: Once = Once::new();
-//    CLEANUP.call_once(||
-//        at_exit_imp::cleanup()
-//    );
-//}
-
-/// One-time runtime cleanup.
-pub fn cleanup() {
-    use crate::sync::SgxThreadSpinlock;
-
-    static SPIN_LOCK: SgxThreadSpinlock = SgxThreadSpinlock::new();
-    static mut IS_CLEAUP: bool = false;
-
-    unsafe {
-        SPIN_LOCK.lock();
-        if IS_CLEAUP == false {
-            at_exit_imp::cleanup();
-            IS_CLEAUP = true;
-        }
-        SPIN_LOCK.unlock();
-    }
-}
-
 // Computes (value*numer)/denom without overflow, as long as both
 // (numer*denom) and the overall result fit into i64 (which is the case
 // for our time conversions).
@@ -140,4 +110,4 @@
     // substitute into (value*numer)/denom and simplify.
     // r < denom, so (denom*numer) is the upper bound of (r*numer)
     q * numer + r * numer / denom
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/sys_common/mutex.rs b/sgx_tstd/src/sys_common/mutex.rs
new file mode 100644
index 0000000..b06b043
--- /dev/null
+++ b/sgx_tstd/src/sys_common/mutex.rs
@@ -0,0 +1,258 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//!
+//! The Intel(R) Software Guard Extensions SDK already supports mutex and conditional
+//! variable synchronization mechanisms by means of the following API and data types
+//! defined in the Types and Enumerations section. Some functions included in the
+//! trusted Thread Synchronization library may make calls outside the enclave (OCALLs).
+//! If you use any of the APIs below, you must first import the needed OCALL functions
+//! from sgx_tstd.edl. Otherwise, you will get a linker error when the enclave is
+//! being built; see Calling Functions outside the Enclave for additional details.
+//! The table below illustrates the primitives that the Intel(R) SGX Thread
+//! Synchronization library supports, as well as the OCALLs that each API function needs.
+//!
+
+use crate::sys::mutex as imp;
+
+use sgx_types::SysError;
+
+/// The structure of sgx mutex.
+pub struct SgxThreadMutex(imp::SgxThreadMutex);
+
+unsafe impl Send for SgxThreadMutex {}
+unsafe impl Sync for SgxThreadMutex {}
+
+impl SgxThreadMutex {
+    ///
+    /// The function initializes a trusted mutex object within the enclave.
+    ///
+    /// # Description
+    ///
+    /// When a thread creates a mutex within an enclave, sgx_thread_mutex_
+    /// init simply initializes the various fields of the mutex object to indicate that
+    /// the mutex is available. rsgx_thread_mutex_init creates a non-recursive
+    /// mutex. The results of using a mutex in a lock or unlock operation before it has
+    /// been fully initialized (for example, the function call to rsgx_thread_mutex_
+    /// init returns) are undefined. To avoid race conditions in the initialization of a
+    /// trusted mutex, it is recommended statically initializing the mutex with the
+    /// macro SGX_THREAD_MUTEX_INITIALIZER, SGX_THREAD_NON_RECURSIVE_MUTEX_INITIALIZER ,
+    /// of, or SGX_THREAD_RECURSIVE_MUTEX_INITIALIZER instead.
+    ///
+    /// # Requirements
+    ///
+    /// Library: libsgx_tstdc.a
+    ///
+    /// # Return value
+    ///
+    /// The trusted mutex object to be initialized.
+    ///
+    pub const fn new() -> SgxThreadMutex {
+        SgxThreadMutex(imp::SgxThreadMutex::new())
+    }
+
+    ///
+    /// The function locks a trusted mutex object within an enclave.
+    ///
+    /// # Description
+    ///
+    /// To acquire a mutex, a thread first needs to acquire the corresponding spin
+    /// lock. After the spin lock is acquired, the thread checks whether the mutex is
+    /// available. If the queue is empty or the thread is at the head of the queue the
+    /// thread will now become the owner of the mutex. To confirm its ownership, the
+    /// thread updates the refcount and owner fields. If the mutex is not available, the
+    /// thread searches the queue. If the thread is already in the queue, but not at the
+    /// head, it means that the thread has previously tried to lock the mutex, but it
+    /// did not succeed and had to wait outside the enclave and it has been
+    /// awakened unexpectedly. When this happens, the thread makes an OCALL and
+    /// simply goes back to sleep. If the thread is trying to lock the mutex for the first
+    /// time, it will update the waiting queue and make an OCALL to get suspended.
+    /// Note that threads release the spin lock after acquiring the mutex or before
+    /// leaving the enclave.
+    ///
+    /// **Note**
+    ///
+    /// A thread should not exit an enclave returning from a root ECALL after acquiring
+    /// the ownership of a mutex. Do not split the critical section protected by a
+    /// mutex across root ECALLs.
+    ///
+    /// # Requirements
+    ///
+    /// Library: libsgx_tstdc.a
+    ///
+    /// # Errors
+    ///
+    /// **EINVAL**
+    ///
+    /// The trusted mutex object is invalid.
+    ///
+    #[inline]
+    pub unsafe fn lock(&self) -> SysError {
+        self.0.lock()
+    }
+
+    ///
+    /// The function tries to lock a trusted mutex object within an enclave.
+    ///
+    /// # Description
+    ///
+    /// A thread may check the status of the mutex, which implies acquiring the spin
+    /// lock and verifying that the mutex is available and that the queue is empty or
+    /// the thread is at the head of the queue. When this happens, the thread
+    /// acquires the mutex, releases the spin lock and returns 0. Otherwise, the
+    /// thread releases the spin lock and returns EINVAL/EBUSY. The thread is not suspended
+    /// in this case.
+    ///
+    /// **Note**
+    ///
+    /// A thread should not exit an enclave returning from a root ECALL after acquiring
+    /// the ownership of a mutex. Do not split the critical section protected by a
+    /// mutex across root ECALLs.
+    ///
+    /// # Requirements
+    ///
+    /// Library: libsgx_tstdc.a
+    ///
+    /// # Errors
+    ///
+    /// **EINVAL**
+    ///
+    /// The trusted mutex object is invalid.
+    ///
+    /// **EBUSY**
+    ///
+    /// The mutex is locked by another thread or has pending threads to acquire the mutex
+    ///
+    #[inline]
+    pub unsafe fn try_lock(&self) -> SysError {
+        self.0.try_lock()
+    }
+
+    ///
+    /// The function unlocks a trusted mutex object within an enclave.
+    ///
+    /// # Description
+    ///
+    /// Before a thread releases a mutex, it has to verify it is the owner of the mutex. If
+    /// that is the case, the thread decreases the refcount by 1 and then may either
+    /// continue normal execution or wakeup the first thread in the queue. Note that
+    /// to ensure the state of the mutex remains consistent, the thread that is
+    /// awakened by the thread releasing the mutex will then try to acquire the
+    /// mutex almost as in the initial call to the rsgx_thread_mutex_lock routine.
+    ///
+    /// # Requirements
+    ///
+    /// Library: libsgx_tstdc.a
+    ///
+    /// # Errors
+    ///
+    /// **EINVAL**
+    ///
+    /// The trusted mutex object is invalid or it is not locked by any thread.
+    ///
+    /// **EPERM**
+    ///
+    /// The mutex is locked by another thread.
+    ///
+    #[inline]
+    pub unsafe fn unlock(&self) -> SysError {
+        self.0.unlock()
+    }
+
+    ///
+    /// The function destroys a trusted mutex object within an enclave.
+    ///
+    /// # Description
+    ///
+    /// rsgx_thread_mutex_destroy resets the mutex, which brings it to its initial
+    /// status. In this process, certain fields are checked to prevent releasing a mutex
+    /// that is still owned by a thread or on which threads are still waiting.
+    ///
+    /// **Note**
+    ///
+    /// Locking or unlocking a mutex after it has been destroyed results in undefined
+    /// behavior. After a mutex is destroyed, it must be re-created before it can be
+    /// used again.
+    ///
+    /// # Requirements
+    ///
+    /// Library: libsgx_tstdc.a
+    ///
+    /// # Errors
+    ///
+    /// **EINVAL**
+    ///
+    /// The trusted mutex object is invalid.
+    ///
+    /// **EBUSY**
+    ///
+    /// The mutex is locked by another thread or has pending threads to acquire the mutex.
+    ///
+    #[inline]
+    pub unsafe fn destroy(&self) -> SysError {
+        self.0.destroy()
+    }
+
+    pub(super) fn raw(&self) -> &imp::SgxThreadMutex {
+        &self.0
+    }
+}
+
+pub struct SgxMovableThreadMutex(imp::SgxMovableThreadMutex);
+
+unsafe impl Sync for SgxMovableThreadMutex {}
+
+impl SgxMovableThreadMutex {
+    /// Creates a new mutex.
+    pub fn new() -> SgxMovableThreadMutex {
+        let mutex = imp::SgxMovableThreadMutex::from(imp::SgxThreadMutex::new());
+        SgxMovableThreadMutex(mutex)
+    }
+
+    pub(super) fn raw(&self) -> &imp::SgxThreadMutex {
+        &self.0
+    }
+
+    /// Locks the mutex blocking the current thread until it is available.
+    #[inline]
+    pub fn raw_lock(&self) -> SysError {
+        unsafe { self.0.lock() }
+    }
+
+    /// Attempts to lock the mutex without blocking, returning whether it was
+    /// successfully acquired or not.
+    #[inline]
+    pub fn try_lock(&self) -> SysError {
+        unsafe { self.0.try_lock() }
+    }
+
+    /// Unlocks the mutex.
+    ///
+    /// Behavior is undefined if the current thread does not actually hold the
+    /// mutex.
+    #[inline]
+    pub unsafe fn raw_unlock(&self) -> SysError {
+        self.0.unlock()
+    }
+}
+
+impl Drop for SgxMovableThreadMutex {
+    fn drop(&mut self) {
+        let r = unsafe { self.0.destroy() };
+        debug_assert_eq!(r, Ok(()));
+    }
+}
diff --git a/sgx_tstd/src/sys_common/net.rs b/sgx_tstd/src/sys_common/net.rs
index 6e7f400..f7964ef 100644
--- a/sgx_tstd/src/sys_common/net.rs
+++ b/sgx_tstd/src/sys_common/net.rs
@@ -17,19 +17,23 @@
 
 #![allow(dead_code)]
 
-use sgx_trts::libc::{c_int, c_uint, c_void};
-use core::cmp;
-use core::fmt;
-use core::mem;
-use core::ptr;
-use core::convert::{TryFrom, TryInto};
+use crate::cmp;
+use crate::convert::{TryFrom, TryInto};
 use crate::ffi::CString;
+use crate::fmt;
 use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut};
+use crate::mem;
 use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
+use crate::ptr;
 use crate::sys::net::{cvt, cvt_gai, cvt_r, init, wrlen_t, Socket};
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::time::Duration;
 
+
+use sgx_libc::{c_int, c_uint, c_void};
+
+type IpV4MultiCastType = c_int;
+
 ////////////////////////////////////////////////////////////////////////////////
 // sockaddr and misc bindings
 ////////////////////////////////////////////////////////////////////////////////
@@ -37,13 +41,7 @@
 pub fn setsockopt<T>(sock: &Socket, opt: c_int, val: c_int, payload: T) -> io::Result<()> {
     unsafe {
         let payload = &payload as *const T as *const c_void;
-        cvt(libc::setsockopt(
-            *sock.as_inner(),
-            opt,
-            val,
-            payload,
-            mem::size_of::<T>() as libc::socklen_t,
-        ))?;
+        cvt(c::setsockopt(sock.as_raw(), opt, val, payload, mem::size_of::<T>() as c::socklen_t))?;
         Ok(())
     }
 }
@@ -51,14 +49,8 @@
 pub fn getsockopt<T: Copy>(sock: &Socket, opt: c_int, val: c_int) -> io::Result<T> {
     unsafe {
         let mut slot: T = mem::zeroed();
-        let mut len = mem::size_of::<T>() as libc::socklen_t;
-        cvt(libc::getsockopt(
-            *sock.as_inner(),
-            opt,
-            val,
-            &mut slot as *mut _ as *mut _,
-            &mut len,
-        ))?;
+        let mut len = mem::size_of::<T>() as c::socklen_t;
+        cvt(c::getsockopt(sock.as_raw(), opt, val, &mut slot as *mut _ as *mut _, &mut len))?;
         assert_eq!(len as usize, mem::size_of::<T>());
         Ok(slot)
     }
@@ -66,31 +58,31 @@
 
 fn sockname<F>(f: F) -> io::Result<SocketAddr>
 where
-    F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> c_int,
+    F: FnOnce(*mut c::sockaddr, *mut c::socklen_t) -> c_int,
 {
     unsafe {
-        let mut storage: libc::sockaddr_storage = mem::zeroed();
-        let mut len = mem::size_of_val(&storage) as libc::socklen_t;
+        let mut storage: c::sockaddr_storage = mem::zeroed();
+        let mut len = mem::size_of_val(&storage) as c::socklen_t;
         cvt(f(&mut storage as *mut _ as *mut _, &mut len))?;
         sockaddr_to_addr(&storage, len as usize)
     }
 }
 
-pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage, len: usize) -> io::Result<SocketAddr> {
+pub fn sockaddr_to_addr(storage: &c::sockaddr_storage, len: usize) -> io::Result<SocketAddr> {
     match storage.ss_family as c_int {
-        libc::AF_INET => {
-            assert!(len as usize >= mem::size_of::<libc::sockaddr_in>());
+        c::AF_INET => {
+            assert!(len as usize >= mem::size_of::<c::sockaddr_in>());
             Ok(SocketAddr::V4(FromInner::from_inner(unsafe {
-                *(storage as *const _ as *const libc::sockaddr_in)
+                *(storage as *const _ as *const c::sockaddr_in)
             })))
         }
-        libc::AF_INET6 => {
-            assert!(len as usize >= mem::size_of::<libc::sockaddr_in6>());
+        c::AF_INET6 => {
+            assert!(len as usize >= mem::size_of::<c::sockaddr_in6>());
             Ok(SocketAddr::V6(FromInner::from_inner(unsafe {
-                *(storage as *const _ as *const libc::sockaddr_in6)
+                *(storage as *const _ as *const c::sockaddr_in6)
             })))
         }
-        _ => Err(Error::new(ErrorKind::InvalidInput, "invalid argument")),
+        _ => Err(Error::new_const(ErrorKind::InvalidInput, &"invalid argument")),
     }
 }
 
@@ -103,8 +95,8 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 pub struct LookupHost {
-    original: *mut libc::addrinfo,
-    cur: *mut libc::addrinfo,
+    original: *mut c::addrinfo,
+    cur: *mut c::addrinfo,
     port: u16,
 }
 
@@ -121,7 +113,7 @@
             unsafe {
                 let cur = self.cur.as_ref()?;
                 self.cur = cur.ai_next;
-                match sockaddr_to_addr(mem::transmute(cur.ai_addr), cur.ai_addrlen as usize) {
+                match sockaddr_to_addr(&*(cur.ai_addr as *const c::sockaddr_storage), cur.ai_addrlen as usize) {
                     Ok(addr) => return Some(addr),
                     Err(_) => continue,
                 }
@@ -135,7 +127,7 @@
 
 impl Drop for LookupHost {
     fn drop(&mut self) {
-        unsafe { libc::freeaddrinfo(self.original) }
+        unsafe { c::freeaddrinfo(self.original) }
     }
 }
 
@@ -147,17 +139,14 @@
             ($e:expr, $msg:expr) => {
                 match $e {
                     Some(r) => r,
-                    None => return Err(io::Error::new(io::ErrorKind::InvalidInput, $msg)),
+                    None => return Err(io::Error::new_const(io::ErrorKind::InvalidInput, &$msg)),
                 }
             };
         }
 
         // split the string by ':' and convert the second part to u16
-        let mut parts_iter = s.rsplitn(2, ':');
-        let port_str = try_opt!(parts_iter.next(), "invalid socket address");
-        let host = try_opt!(parts_iter.next(), "invalid socket address");
+        let (host, port_str) = try_opt!(s.rsplit_once(':'), "invalid socket address");
         let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
-
         (host, port).try_into()
     }
 }
@@ -169,11 +158,11 @@
         init();
 
         let c_host = CString::new(host)?;
-        let mut hints: libc::addrinfo = unsafe { mem::zeroed() };
-        hints.ai_socktype = libc::SOCK_STREAM;
+        let mut hints: c::addrinfo = unsafe { mem::zeroed() };
+        hints.ai_socktype = c::SOCK_STREAM;
         let mut res = ptr::null_mut();
         unsafe {
-            cvt_gai(libc::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res))
+            cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res))
                 .map(|_| LookupHost { original: res, cur: res, port })
         }
     }
@@ -194,31 +183,23 @@
     }
 
     pub fn new_v4() -> io::Result<TcpStream> {
-        let sock = Socket::new_raw(libc::AF_INET, libc::SOCK_STREAM)?;
+        let sock = Socket::new_raw(c::AF_INET, c::SOCK_STREAM)?;
         Ok(TcpStream { inner: sock })
     }
 
     pub fn new_v6() -> io::Result<TcpStream> {
-        let sock = Socket::new_raw(libc::AF_INET6, libc::SOCK_STREAM)?;
+        let sock = Socket::new_raw(c::AF_INET6, c::SOCK_STREAM)?;
         Ok(TcpStream { inner: sock })
     }
 
-    pub fn raw(&self) -> c_int {
-        self.inner.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.inner.into_raw()
-    }
-
     pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
         let addr = addr?;
 
         init();
 
-        let sock = Socket::new_socket_addr_type(addr, libc::SOCK_STREAM)?;
+        let sock = Socket::new_socket_addr_type(addr, c::SOCK_STREAM)?;
         let (addrp, len) = addr.into_inner();
-        cvt_r(|| unsafe { libc::connect(*sock.as_inner(), addrp, len) })?;
+        cvt_r(|| unsafe { c::connect(sock.as_raw(), addrp, len) })?;
         Ok(TcpStream { inner: sock })
     }
 
@@ -228,13 +209,13 @@
         init();
 
         let (addrp, len) = addr.into_inner();
-        cvt_r(|| unsafe { libc::connect(*self.inner.as_inner(), addrp, len) }).map(drop)
+        cvt_r(|| unsafe { c::connect(self.inner.as_raw(), addrp, len) }).map(drop)
     }
 
     pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
         init();
 
-        let sock = Socket::new_socket_addr_type(addr, libc::SOCK_STREAM)?;
+        let sock = Socket::new_socket_addr_type(addr, c::SOCK_STREAM)?;
         sock.connect_timeout(addr, timeout)?;
         Ok(TcpStream { inner: sock })
     }
@@ -252,19 +233,19 @@
     }
 
     pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
-        self.inner.set_timeout(dur, libc::SO_RCVTIMEO)
+        self.inner.set_timeout(dur, c::SO_RCVTIMEO)
     }
 
     pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
-        self.inner.set_timeout(dur, libc::SO_SNDTIMEO)
+        self.inner.set_timeout(dur, c::SO_SNDTIMEO)
     }
 
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        self.inner.timeout(libc::SO_RCVTIMEO)
+        self.inner.timeout(c::SO_RCVTIMEO)
     }
 
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        self.inner.timeout(libc::SO_SNDTIMEO)
+        self.inner.timeout(c::SO_SNDTIMEO)
     }
 
     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
@@ -279,15 +260,15 @@
         self.inner.read_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_read_vectored(&self) -> bool {
+        self.inner.is_read_vectored()
+    }
+
     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
-        let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
+        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
         let ret = cvt(unsafe {
-            libc::send(
-                *self.inner.as_inner(),
-                buf.as_ptr() as *const c_void,
-                len,
-                libc::MSG_NOSIGNAL,
-            )
+            c::send(self.inner.as_raw(), buf.as_ptr() as *const c_void, len, c::MSG_NOSIGNAL)
         })?;
         Ok(ret as usize)
     }
@@ -296,16 +277,17 @@
         self.inner.write_vectored(bufs)
     }
 
+    #[inline]
+    pub fn is_write_vectored(&self) -> bool {
+        self.inner.is_write_vectored()
+    }
+
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
-        sockname(|buf, len| unsafe {
-            libc::getpeername(*self.inner.as_inner(), buf, len)
-        })
+        sockname(|buf, len| unsafe { c::getpeername(self.inner.as_raw(), buf, len) })
     }
 
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
-        sockname(|buf, len| unsafe {
-            libc::getsockname(*self.inner.as_inner(), buf, len)
-        })
+        sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
     }
 
     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
@@ -316,6 +298,14 @@
         self.inner.duplicate().map(|s| TcpStream { inner: s })
     }
 
+    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
+        self.inner.set_linger(linger)
+    }
+
+    pub fn linger(&self) -> io::Result<Option<Duration>> {
+        self.inner.linger()
+    }
+
     pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
         self.inner.set_nodelay(nodelay)
     }
@@ -325,23 +315,14 @@
     }
 
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL, ttl as c_int)
+        setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL)?;
+        let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
         Ok(raw as u32)
     }
 
-    pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY, only_v6 as c_int)
-    }
-
-    pub fn only_v6(&self) -> io::Result<bool> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY)?;
-        Ok(raw != 0)
-    }
-
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         self.inner.take_error()
     }
@@ -370,7 +351,7 @@
         }
 
         let name = "fd";
-        res.field(name, &self.inner.as_inner()).finish()
+        res.field(name, &self.inner.as_raw()).finish()
     }
 }
 
@@ -389,41 +370,33 @@
     }
 
     pub fn new_v4() -> io::Result<TcpListener> {
-        let sock = Socket::new_raw(libc::AF_INET, libc::SOCK_STREAM)?;
+        let sock = Socket::new_raw(c::AF_INET, c::SOCK_STREAM)?;
         Ok(TcpListener { inner: sock })
     }
 
     pub fn new_v6() -> io::Result<TcpListener> {
-        let sock = Socket::new_raw(libc::AF_INET6, libc::SOCK_STREAM)?;
+        let sock = Socket::new_raw(c::AF_INET6, c::SOCK_STREAM)?;
         Ok(TcpListener { inner: sock })
     }
 
-    pub fn raw(&self) -> c_int {
-        self.inner.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.inner.into_raw()
-    }
-
     pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
         let addr = addr?;
 
         init();
 
-        let sock = Socket::new_socket_addr_type(addr, libc::SOCK_STREAM)?;
+        let sock = Socket::new_socket_addr_type(addr, c::SOCK_STREAM)?;
 
-        // On platforms with Berkeley-derived sockets, this allows
-        // to quickly rebind a socket, without needing to wait for
-        // the OS to clean up the previous one.
-        setsockopt(&sock, libc::SOL_SOCKET, libc::SO_REUSEADDR, 1 as c_int)?;
+        // On platforms with Berkeley-derived sockets, this allows to quickly
+        // rebind a socket, without needing to wait for the OS to clean up the
+        // previous one.
+        setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1_i32)?;
 
         // Bind our new socket
         let (addrp, len) = addr.into_inner();
-        cvt(unsafe { libc::bind(*sock.as_inner(), addrp, len as _) })?;
+        cvt(unsafe { c::bind(sock.as_raw(), addrp, len as _) })?;
 
         // Start listening
-        cvt(unsafe { libc::listen(*sock.as_inner(), 128) })?;
+        cvt(unsafe { c::listen(sock.as_raw(), 128) })?;
         Ok(TcpListener { inner: sock })
     }
 
@@ -432,10 +405,10 @@
 
         init();
 
-        setsockopt(&self.inner, libc::SOL_SOCKET, libc::SO_REUSEADDR, 1 as c_int)?;
+        setsockopt(&self.inner, c::SOL_SOCKET, c::SO_REUSEADDR, 1_i32)?;
         let (addrp, len) = addr.into_inner();
-        cvt(unsafe { libc::bind(*self.inner.as_inner(), addrp, len as _) })?;
-        cvt(unsafe { libc::listen(*self.inner.as_inner(), 128) }).map(drop)
+        cvt(unsafe { c::bind(self.inner.as_raw(), addrp, len as _) })?;
+        cvt(unsafe { c::listen(self.inner.as_raw(), 128) }).map(drop)
     }
 
     pub fn socket(&self) -> &Socket {
@@ -447,14 +420,12 @@
     }
 
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
-        sockname(|buf, len| unsafe {
-            libc::getsockname(*self.inner.as_inner(), buf, len)
-        })
+        sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
     }
 
     pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
-        let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
-        let mut len = mem::size_of_val(&storage) as libc::socklen_t;
+        let mut storage: c::sockaddr_storage = unsafe { mem::zeroed() };
+        let mut len = mem::size_of_val(&storage) as c::socklen_t;
         let sock = self.inner.accept(&mut storage as *mut _ as *mut _, &mut len)?;
         let addr = sockaddr_to_addr(&storage, len as usize)?;
         Ok((TcpStream { inner: sock }, addr))
@@ -465,20 +436,20 @@
     }
 
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL, ttl as c_int)
+        setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL)?;
+        let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
         Ok(raw as u32)
     }
 
     pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY, only_v6 as c_int)
+        setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int)
     }
 
     pub fn only_v6(&self) -> io::Result<bool> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_V6ONLY)?;
+        let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)?;
         Ok(raw != 0)
     }
 
@@ -506,7 +477,7 @@
         }
 
         let name = "fd";
-        res.field(name, &self.inner.as_inner()).finish()
+        res.field(name, &self.inner.as_raw()).finish()
     }
 }
 
@@ -519,38 +490,29 @@
 }
 
 impl UdpSocket {
-
     pub fn new(sockfd: c_int) -> io::Result<UdpSocket> {
         let sock = Socket::new(sockfd)?;
         Ok(UdpSocket { inner: sock })
     }
 
     pub fn new_v4() -> io::Result<UdpSocket> {
-        let sock = Socket::new_raw(libc::AF_INET, libc::SOCK_DGRAM)?;
+        let sock = Socket::new_raw(c::AF_INET, c::SOCK_DGRAM)?;
         Ok(UdpSocket { inner: sock })
     }
 
     pub fn new_v6() -> io::Result<UdpSocket> {
-        let sock = Socket::new_raw(libc::AF_INET6, libc::SOCK_DGRAM)?;
+        let sock = Socket::new_raw(c::AF_INET6, c::SOCK_DGRAM)?;
         Ok(UdpSocket { inner: sock })
     }
 
-    pub fn raw(&self) -> c_int {
-        self.inner.raw()
-    }
-
-    pub fn into_raw(self) -> c_int {
-        self.inner.into_raw()
-    }
-
     pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
         let addr = addr?;
 
         init();
 
-        let sock = Socket::new_socket_addr_type(addr, libc::SOCK_DGRAM)?;
+        let sock = Socket::new_socket_addr_type(addr, c::SOCK_DGRAM)?;
         let (addrp, len) = addr.into_inner();
-        cvt(unsafe { libc::bind(*sock.as_inner(), addrp, len as _) })?;
+        cvt(unsafe { c::bind(sock.as_raw(), addrp, len as _) })?;
         Ok(UdpSocket { inner: sock })
     }
 
@@ -560,7 +522,7 @@
         init();
 
         let (addrp, len) = addr.into_inner();
-        cvt(unsafe { libc::bind(*self.inner.as_inner(), addrp, len as _) }).map(drop)
+        cvt(unsafe { c::bind(self.inner.as_raw(), addrp, len as _) }).map(drop)
     }
 
     pub fn socket(&self) -> &Socket {
@@ -572,15 +534,11 @@
     }
 
     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
-        sockname(|buf, len| unsafe {
-            libc::getpeername(*self.inner.as_inner(), buf, len)
-        })
+        sockname(|buf, len| unsafe { c::getpeername(self.inner.as_raw(), buf, len) })
     }
 
     pub fn socket_addr(&self) -> io::Result<SocketAddr> {
-        sockname(|buf, len| unsafe { 
-            libc::getsockname(*self.inner.as_inner(), buf, len) 
-        })
+        sockname(|buf, len| unsafe { c::getsockname(self.inner.as_raw(), buf, len) })
     }
 
     pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
@@ -592,14 +550,14 @@
     }
 
     pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
-        let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
+        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
         let (dstp, dstlen) = dst.into_inner();
         let ret = cvt(unsafe {
-            libc::sendto(
-                *self.inner.as_inner(),
+            c::sendto(
+                self.inner.as_raw(),
                 buf.as_ptr() as *const c_void,
                 len,
-                libc::MSG_NOSIGNAL,
+                c::MSG_NOSIGNAL,
                 dstp,
                 dstlen,
             )
@@ -612,95 +570,105 @@
     }
 
     pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
-        self.inner.set_timeout(dur, libc::SO_RCVTIMEO)
+        self.inner.set_timeout(dur, c::SO_RCVTIMEO)
     }
 
     pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
-        self.inner.set_timeout(dur, libc::SO_SNDTIMEO)
+        self.inner.set_timeout(dur, c::SO_SNDTIMEO)
     }
 
     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
-        self.inner.timeout(libc::SO_RCVTIMEO)
+        self.inner.timeout(c::SO_RCVTIMEO)
     }
 
     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
-        self.inner.timeout(libc::SO_SNDTIMEO)
+        self.inner.timeout(c::SO_SNDTIMEO)
     }
 
     pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
-        setsockopt(&self.inner, libc::SOL_SOCKET, libc::SO_BROADCAST, broadcast as c_int)
+        setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int)
     }
 
     pub fn broadcast(&self) -> io::Result<bool> {
-        let raw: c_int = getsockopt(&self.inner, libc::SOL_SOCKET, libc::SO_BROADCAST)?;
+        let raw: c_int = getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)?;
         Ok(raw != 0)
     }
 
     pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
+        setsockopt(
+            &self.inner,
+            c::IPPROTO_IP,
+            c::IP_MULTICAST_LOOP,
+            multicast_loop_v4 as IpV4MultiCastType,
+        )
     }
 
     pub fn multicast_loop_v4(&self) -> io::Result<bool> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_MULTICAST_LOOP)?;
+        let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
         Ok(raw != 0)
     }
 
     pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
+        setsockopt(
+            &self.inner,
+            c::IPPROTO_IP,
+            c::IP_MULTICAST_TTL,
+            multicast_ttl_v4 as IpV4MultiCastType,
+        )
     }
 
     pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_MULTICAST_TTL)?;
+        let raw: IpV4MultiCastType = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
         Ok(raw as u32)
     }
 
     pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
+        setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
     }
 
     pub fn multicast_loop_v6(&self) -> io::Result<bool> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_MULTICAST_LOOP)?;
+        let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)?;
         Ok(raw != 0)
     }
 
     pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
-        let mreq = libc::ip_mreq {
-            imr_multiaddr: *multiaddr.as_inner(),
-            imr_interface: *interface.as_inner(),
+        let mreq = c::ip_mreq {
+            imr_multiaddr: multiaddr.into_inner(),
+            imr_interface: interface.into_inner(),
         };
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, mreq)
+        setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq)
     }
 
     pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
-        let mreq = libc::ipv6_mreq {
+        let mreq = c::ipv6_mreq {
             ipv6mr_multiaddr: *multiaddr.as_inner(),
             ipv6mr_interface: to_ipv6mr_interface(interface),
         };
-        setsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_ADD_MEMBERSHIP, mreq)
+        setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_ADD_MEMBERSHIP, mreq)
     }
 
     pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
-        let mreq = libc::ip_mreq {
-            imr_multiaddr: *multiaddr.as_inner(),
-            imr_interface: *interface.as_inner(),
+        let mreq = c::ip_mreq {
+            imr_multiaddr: multiaddr.into_inner(),
+            imr_interface: interface.into_inner(),
         };
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, mreq)
+        setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq)
     }
 
     pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
-        let mreq = libc::ipv6_mreq {
+        let mreq = c::ipv6_mreq {
             ipv6mr_multiaddr: *multiaddr.as_inner(),
             ipv6mr_interface: to_ipv6mr_interface(interface),
         };
-        setsockopt(&self.inner, libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP, mreq)
+        setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_DROP_MEMBERSHIP, mreq)
     }
 
     pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
-        setsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL, ttl as c_int)
+        setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int)
     }
 
     pub fn ttl(&self) -> io::Result<u32> {
-        let raw: c_int = getsockopt(&self.inner, libc::IPPROTO_IP, libc::IP_TTL)?;
+        let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)?;
         Ok(raw as u32)
     }
 
@@ -721,21 +689,16 @@
     }
 
     pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
-        let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
+        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
         let ret = cvt(unsafe {
-            libc::send(
-                *self.inner.as_inner(),
-                buf.as_ptr() as *const c_void,
-                len,
-                libc::MSG_NOSIGNAL,
-            )
+            c::send(self.inner.as_raw(), buf.as_ptr() as *const c_void, len, c::MSG_NOSIGNAL)
         })?;
         Ok(ret as usize)
     }
 
     pub fn connect(&self, addr: io::Result<&SocketAddr>) -> io::Result<()> {
         let (addrp, len) = addr?.into_inner();
-        cvt_r(|| unsafe { libc::connect(*self.inner.as_inner(), addrp, len) }).map(drop)
+        cvt_r(|| unsafe { c::connect(self.inner.as_raw(), addrp, len) }).map(drop)
     }
 }
 
@@ -754,12 +717,13 @@
         }
 
         let name = "fd";
-        res.field(name, &self.inner.as_inner()).finish()
+        res.field(name, &self.inner.as_raw()).finish()
     }
 }
 
-mod libc {
-    pub use sgx_trts::libc::*;
-    pub use sgx_trts::libc::ocall::{bind, listen, connect, setsockopt, getsockopt, send, sendto,
-                                    getpeername, getsockname, getaddrinfo, freeaddrinfo};
-}
\ No newline at end of file
+mod c {
+    pub use sgx_libc::ocall::{
+        bind, connect, freeaddrinfo, getaddrinfo, getpeername, getsockname, getsockopt, listen, send, sendto, setsockopt,
+    };
+    pub use sgx_libc::*;
+}
diff --git a/sgx_tstd/src/sys_common/remutex.rs b/sgx_tstd/src/sys_common/remutex.rs
new file mode 100644
index 0000000..74bd073
--- /dev/null
+++ b/sgx_tstd/src/sys_common/remutex.rs
@@ -0,0 +1,133 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::marker::PhantomPinned;
+use crate::ops::Deref;
+use crate::panic::{RefUnwindSafe, UnwindSafe};
+use crate::pin::Pin;
+use crate::sys::mutex as sys;
+
+/// A re-entrant mutual exclusion
+///
+/// This mutex will block *other* threads waiting for the lock to become
+/// available. The thread which has already locked the mutex can lock it
+/// multiple times without blocking, preventing a common source of deadlocks.
+pub struct SgxReentrantMutex<T> {
+    inner: sys::SgxReentrantThreadMutex,
+    data: T,
+    _pinned: PhantomPinned,
+}
+
+
+unsafe impl<T: Send> Send for SgxReentrantMutex<T> {}
+unsafe impl<T: Send> Sync for SgxReentrantMutex<T> {}
+
+impl<T> UnwindSafe for SgxReentrantMutex<T> {}
+impl<T> RefUnwindSafe for SgxReentrantMutex<T> {}
+
+/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
+/// dropped (falls out of scope), the lock will be unlocked.
+///
+/// The data protected by the mutex can be accessed through this guard via its
+/// Deref implementation.
+///
+/// # Mutability
+///
+/// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
+/// because implementation of the trait would violate Rust’s reference aliasing
+/// rules. Use interior mutability (usually `RefCell`) in order to mutate the
+/// guarded data.
+pub struct SgxReentrantMutexGuard<'a, T: 'a> {
+    lock: Pin<&'a SgxReentrantMutex<T>>,
+}
+
+impl<T> !Send for SgxReentrantMutexGuard<'_, T> {}
+
+impl<T> SgxReentrantMutex<T> {
+    /// Creates a new reentrant mutex in an unlocked state.
+    ///
+    pub const fn new(t: T) -> SgxReentrantMutex<T> {
+        SgxReentrantMutex {
+            inner: sys::SgxReentrantThreadMutex::new(),
+            data: t,
+            _pinned: PhantomPinned,
+        }
+    }
+
+    /// Acquires a mutex, blocking the current thread until it is able to do so.
+    ///
+    /// This function will block the caller until it is available to acquire the mutex.
+    /// Upon returning, the thread is the only thread with the mutex held. When the thread
+    /// calling this method already holds the lock, the call shall succeed without
+    /// blocking.
+    ///
+    /// # Errors
+    ///
+    /// If another user of this mutex panicked while holding the mutex, then
+    /// this call will return failure if the mutex would otherwise be
+    /// acquired.
+    pub fn lock(self: Pin<&Self>) -> SgxReentrantMutexGuard<'_, T> {
+        unsafe { self.inner.lock(); }
+        SgxReentrantMutexGuard { lock: self }
+    }
+
+    /// Attempts to acquire this lock.
+    ///
+    /// If the lock could not be acquired at this time, then `Err` is returned.
+    /// Otherwise, an RAII guard is returned.
+    ///
+    /// This function does not block.
+    ///
+    /// # Errors
+    ///
+    /// If another user of this mutex panicked while holding the mutex, then
+    /// this call will return failure if the mutex would otherwise be
+    /// acquired.
+    pub fn try_lock(self: Pin<&Self>) -> Option<SgxReentrantMutexGuard<'_, T>> {
+        if unsafe { self.inner.try_lock().is_ok() } {
+            Some(SgxReentrantMutexGuard { lock: self })
+        } else {
+            None
+        }
+    }
+}
+
+impl<T> Drop for SgxReentrantMutex<T> {
+    fn drop(&mut self) {
+        // This is actually safe b/c we know that there is no further usage of
+        // this mutex (it's up to the user to arrange for a mutex to get
+        // dropped, that's not our job)
+        unsafe { self.inner.destroy(); }
+    }
+}
+
+impl<T> Deref for SgxReentrantMutexGuard<'_, T> {
+    type Target = T;
+
+    fn deref(&self) -> &T {
+        &self.lock.data
+    }
+}
+
+impl<T> Drop for SgxReentrantMutexGuard<'_, T> {
+    #[inline]
+    fn drop(&mut self) {
+        unsafe {
+            self.lock.inner.unlock();
+        }
+    }
+}
diff --git a/sgx_tstd/src/sys_common/rt.rs b/sgx_tstd/src/sys_common/rt.rs
new file mode 100644
index 0000000..c39e6cd
--- /dev/null
+++ b/sgx_tstd/src/sys_common/rt.rs
@@ -0,0 +1,89 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+#![deny(unsafe_op_in_unsafe_fn)]
+#![allow(unused_macros)]
+
+// One-time runtime cleanup.
+// NOTE: this is not guaranteed to run, for example when the program aborts.
+// pub fn cleanup() {
+//     static CLEANUP: Once = Once::new();
+//     CLEANUP.call_once(|| {
+//         // Flush stdout and disable buffering.
+//         crate::io::cleanup();
+
+//         super::at_exit_imp::cleanup();
+//     });
+// }
+
+/// One-time runtime cleanup.
+pub fn cleanup() {
+    use crate::sync::SgxThreadSpinlock;
+
+    static SPIN_LOCK: SgxThreadSpinlock = SgxThreadSpinlock::new();
+    static mut IS_CLEAUP: bool = false;
+
+    unsafe {
+        SPIN_LOCK.lock();
+        if !IS_CLEAUP {
+            super::at_exit_imp::cleanup();
+            IS_CLEAUP = true;
+        }
+        SPIN_LOCK.unlock();
+    }
+}
+
+// Prints to the "panic output", depending on the platform this may be:
+// - the standard error output
+// - some dedicated platform specific output
+// - nothing (so this macro is a no-op)
+macro_rules! rtprintpanic {
+    ($($t:tt)*) => {
+        if let Some(mut out) = crate::sys::stdio::panic_output() {
+            let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*));
+        }
+    }
+}
+
+macro_rules! rtabort {
+    ($($t:tt)*) => {
+        {
+            rtprintpanic!("fatal runtime error: {}\n", format_args!($($t)*));
+            crate::sys::abort_internal();
+        }
+    }
+}
+
+macro_rules! rtassert {
+    ($e:expr) => {
+        if !$e {
+            rtabort!(concat!("assertion failed: ", stringify!($e)));
+        }
+    };
+}
+
+macro_rules! rtunwrap {
+    ($ok:ident, $e:expr) => {
+        match $e {
+            $ok(v) => v,
+            ref err => {
+                let err = err.as_ref().map(drop); // map Ok/Some which might not be Debug
+                rtabort!(concat!("unwrap failed: ", stringify!($e), " = {:?}"), err)
+            }
+        }
+    };
+}
diff --git a/sgx_tstd/src/sys_common/rwlock.rs b/sgx_tstd/src/sys_common/rwlock.rs
new file mode 100644
index 0000000..47c0522
--- /dev/null
+++ b/sgx_tstd/src/sys_common/rwlock.rs
@@ -0,0 +1,160 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::sys::rwlock as imp;
+use sgx_types::SysError;
+
+/// An OS-based reader-writer lock.
+///
+/// This structure is entirely unsafe and serves as the lowest layer of a
+/// cross-platform binding of system rwlocks. It is recommended to use the
+/// safer types at the top level of this crate instead of this type.
+pub struct SgxThreadRwLock(imp::SgxThreadRwLock);
+
+unsafe impl Send for SgxThreadRwLock {}
+unsafe impl Sync for SgxThreadRwLock {}
+
+impl SgxThreadRwLock {
+
+    /// Creates a new reader-writer lock for use.
+    pub const fn new() -> SgxThreadRwLock {
+        SgxThreadRwLock(imp::SgxThreadRwLock::new())
+    }
+
+    /// Acquires shared access to the underlying lock, blocking the current
+    /// thread to do so.
+    #[inline]
+    pub unsafe fn read(&self) -> SysError {
+        self.0.read()
+    }
+
+    /// Attempts to acquire shared access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    #[inline]
+    pub unsafe fn try_read(&self) -> SysError {
+        self.0.try_read()
+    }
+
+    /// Acquires write access to the underlying lock, blocking the current thread
+    /// to do so.
+    #[inline]
+    pub unsafe fn write(&self) -> SysError {
+        self.0.write()
+    }
+
+    /// Attempts to acquire exclusive access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    #[inline]
+    pub unsafe fn try_write(&self) -> SysError {
+        self.0.try_write()
+    }
+
+    /// Unlocks previously acquired shared access to this lock.
+    #[inline]
+    pub unsafe fn read_unlock(&self) -> SysError {
+        self.0.read_unlock()
+    }
+
+    /// Unlocks previously acquired exclusive access to this lock.
+    #[inline]
+    pub unsafe fn write_unlock(&self) -> SysError {
+        self.0.write_unlock()
+    }
+
+    /// Destroys OS-related resources with this RWLock.
+    #[inline]
+    pub unsafe fn destroy(&self) -> SysError {
+        self.0.destroy()
+    }
+}
+
+pub struct SgxMovableThreadRwLock(imp::SgxMovableThreadRwLock);
+
+unsafe impl Sync for SgxMovableThreadRwLock {}
+
+impl SgxMovableThreadRwLock {
+    /// Creates a new reader-writer lock for use.
+    pub fn new() -> SgxMovableThreadRwLock {
+        SgxMovableThreadRwLock(imp::SgxMovableThreadRwLock::from(imp::SgxThreadRwLock::new()))
+    }
+
+    /// Acquires shared access to the underlying lock, blocking the current
+    /// thread to do so.
+    #[inline]
+    pub fn read(&self) -> SysError {
+        unsafe { self.0.read() }
+    }
+
+    /// Attempts to acquire shared access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    #[inline]
+    pub fn try_read(&self) -> SysError {
+        unsafe { self.0.try_read() }
+    }
+
+    /// Acquires write access to the underlying lock, blocking the current thread
+    /// to do so.
+    ///
+    /// Behavior is undefined if the rwlock has been moved between this and any
+    /// previous method call.
+    #[inline]
+    pub fn write(&self) -> SysError {
+        unsafe { self.0.write() }
+    }
+
+    /// Attempts to acquire exclusive access to this lock, returning whether it
+    /// succeeded or not.
+    ///
+    /// This function does not block the current thread.
+    ///
+    /// Behavior is undefined if the rwlock has been moved between this and any
+    /// previous method call.
+    #[inline]
+    pub fn try_write(&self) -> SysError {
+        unsafe { self.0.try_write() }
+    }
+
+    /// Unlocks previously acquired shared access to this lock.
+    ///
+    /// Behavior is undefined if the current thread does not have shared access.
+    #[inline]
+    pub unsafe fn read_unlock(&self) -> SysError {
+        self.0.read_unlock()
+    }
+
+    /// Unlocks previously acquired exclusive access to this lock.
+    ///
+    /// Behavior is undefined if the current thread does not currently have
+    /// exclusive access.
+    #[inline]
+    pub unsafe fn write_unlock(&self) -> SysError {
+        self.0.write_unlock()
+    }
+}
+
+impl Drop for SgxMovableThreadRwLock {
+    fn drop(&mut self) {
+        let r = unsafe { self.0.destroy() };
+        debug_assert_eq!(r, Ok(()));
+    }
+}
diff --git a/sgx_tstd/src/sys_common/thread_info.rs b/sgx_tstd/src/sys_common/thread_info.rs
index 261c1ab..c70071f 100644
--- a/sgx_tstd/src/sys_common/thread_info.rs
+++ b/sgx_tstd/src/sys_common/thread_info.rs
@@ -15,8 +15,8 @@
 // specific language governing permissions and limitations
 // under the License..
 
+use crate::cell::RefCell;
 use crate::thread::SgxThread;
-use core::cell::RefCell;
 
 struct SgxThreadInfo {
     thread: SgxThread,
diff --git a/sgx_tstd/src/sys_common/thread_local_dtor.rs b/sgx_tstd/src/sys_common/thread_local_dtor.rs
new file mode 100644
index 0000000..0a0a16f
--- /dev/null
+++ b/sgx_tstd/src/sys_common/thread_local_dtor.rs
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+#![allow(dead_code)] // sys isn't exported yet
+
+use crate::ptr;
+use crate::sys_common::thread_local_key::StaticKey;
+
+pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
+    // The fallback implementation uses a vanilla OS-based TLS key to track
+    // the list of destructors that need to be run for this thread. The key
+    // then has its own destructor which runs all the other destructors.
+    //
+    // The destructor for DTORS is a little special in that it has a `while`
+    // loop to continuously drain the list of registered destructors. It
+    // *should* be the case that this loop always terminates because we
+    // provide the guarantee that a TLS key cannot be set after it is
+    // flagged for destruction.
+
+    static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
+    type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
+    if DTORS.get().is_null() {
+        let v: Box<List> = box Vec::new();
+        DTORS.set(Box::into_raw(v) as *mut u8);
+    }
+    let list: &mut List = &mut *(DTORS.get() as *mut List);
+    list.push((t, dtor));
+
+    unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
+        while !ptr.is_null() {
+            let list: Box<List> = Box::from_raw(ptr as *mut List);
+            for (ptr, dtor) in list.into_iter() {
+                dtor(ptr);
+            }
+            ptr = DTORS.get();
+            DTORS.set(ptr::null_mut());
+        }
+    }
+}
\ No newline at end of file
diff --git a/sgx_tstd/src/sys_common/thread_local.rs b/sgx_tstd/src/sys_common/thread_local_key.rs
similarity index 81%
rename from sgx_tstd/src/sys_common/thread_local.rs
rename to sgx_tstd/src/sys_common/thread_local_key.rs
index f373fa5..e124508 100644
--- a/sgx_tstd/src/sys_common/thread_local.rs
+++ b/sgx_tstd/src/sys_common/thread_local_key.rs
@@ -21,7 +21,7 @@
 //! using the native OS-provided facilities (think `TlsAlloc` or
 //! `pthread_setspecific`). The interface of this differs from the other types
 //! of thread-local-storage provided in this crate in that OS-based TLS can only
-//! get/set pointers,
+//! get/set pointer-sized data, possibly with an associated destructor.
 //!
 //! This module also provides two flavors of TLS. One is intended for static
 //! initialization, and does not contain a `Drop` implementation to deallocate
@@ -31,17 +31,41 @@
 //! # Usage
 //!
 //! This module should likely not be used directly unless other primitives are
-//! being built on. types such as `thread_local::spawn::Key` are likely much
+//! being built on. Types such as `thread_local::spawn::Key` are likely much
 //! more useful in practice than this OS-based version which likely requires
 //! unsafe code to interoperate with.
 //!
+//! # Examples
+//!
+//! Using a dynamically allocated TLS key. Note that this key can be shared
+//! among many threads via an `Arc`.
+//!
+//! ```ignore (cannot-doctest-private-modules)
+//! let key = Key::new(None);
+//! assert!(key.get().is_null());
+//! key.set(1 as *mut u8);
+//! assert!(!key.get().is_null());
+//!
+//! drop(key); // deallocate this TLS slot.
+//! ```
+//!
+//! Sometimes a statically allocated key is either required or easier to work
+//! with, however.
+//!
+//! ```ignore (cannot-doctest-private-modules)
+//! static KEY: StaticKey = INIT;
+//!
+//! unsafe {
+//!     assert!(KEY.get().is_null());
+//!     KEY.set(1 as *mut u8);
+//! }
+//! ```
 
 #![allow(non_camel_case_types)]
 #![allow(dead_code)] // sys isn't exported yet
 
-use core::ptr;
 use core::sync::atomic::{self, AtomicUsize, Ordering};
-use crate::sys::thread_local as imp;
+use crate::sys::thread_local_key as imp;
 use crate::sync::SgxThreadMutex;
 
 /// A type for TLS keys that are statically allocated.
@@ -53,6 +77,18 @@
 /// time. The key is also deallocated when the Rust runtime exits or `destroy`
 /// is called, whichever comes first.
 ///
+/// # Examples
+///
+/// ```ignore (cannot-doctest-private-modules)
+/// use tls::os::{StaticKey, INIT};
+///
+/// static KEY: StaticKey = INIT;
+///
+/// unsafe {
+///     assert!(KEY.get().is_null());
+///     KEY.set(1 as *mut u8);
+/// }
+/// ```
 pub struct StaticKey {
     /// Inner static TLS key (internals).
     key: AtomicUsize,
@@ -72,6 +108,18 @@
 /// Implementations will likely, however, contain unsafe code as this type only
 /// operates on `*mut u8`, a raw pointer.
 ///
+/// # Examples
+///
+/// ```ignore (cannot-doctest-private-modules)
+/// use tls::os::Key;
+///
+/// let key = Key::new(None);
+/// assert!(key.get().is_null());
+/// key.set(1 as *mut u8);
+/// assert!(!key.get().is_null());
+///
+/// drop(key); // deallocate this TLS slot.
+/// ```
 pub struct Key {
     key: imp::Key,
 }
@@ -134,7 +182,7 @@
             return key;
         }
 
-        // POSIX allows the key created here to be 0, but the compare_and_swap
+        // POSIX allows the key created here to be 0, but the compare_exchange
         // below relies on using 0 as a sentinel value to check who won the
         // race to set the shared TLS key. As far as I know, there is no
         // guaranteed value that cannot be returned as a posix_key_create key,
@@ -152,11 +200,11 @@
             key2
         };
         rtassert!(key != 0);
-        match self.key.compare_and_swap(0, key as usize, Ordering::SeqCst) {
+        match self.key.compare_exchange(0, key as usize, Ordering::SeqCst, Ordering::SeqCst) {
             // The CAS succeeded, so we've created the actual key
-            0 => key as usize,
+            Ok(_) => key as usize,
             // If someone beat us to the punch, use their key instead
-            n => {
+            Err(n) => {
                 imp::destroy(key);
                 n
             }
@@ -200,34 +248,3 @@
     }
 }
 
-pub unsafe fn register_dtor_fallback(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
-    // The fallback implementation uses a vanilla OS-based TLS key to track
-    // the list of destructors that need to be run for this thread. The key
-    // then has its own destructor which runs all the other destructors.
-    //
-    // The destructor for DTORS is a little special in that it has a `while`
-    // loop to continuously drain the list of registered destructors. It
-    // *should* be the case that this loop always terminates because we
-    // provide the guarantee that a TLS key cannot be set after it is
-    // flagged for destruction.
-
-    static DTORS: StaticKey = StaticKey::new(Some(run_dtors));
-    type List = Vec<(*mut u8, unsafe extern "C" fn(*mut u8))>;
-    if DTORS.get().is_null() {
-        let v: Box<List> = box Vec::new();
-        DTORS.set(Box::into_raw(v) as *mut u8);
-    }
-    let list: &mut List = &mut *(DTORS.get() as *mut List);
-    list.push((t, dtor));
-
-    unsafe extern "C" fn run_dtors(mut ptr: *mut u8) {
-        while !ptr.is_null() {
-            let list: Box<List> = Box::from_raw(ptr as *mut List);
-            for (ptr, dtor) in list.into_iter() {
-                dtor(ptr);
-            }
-            ptr = DTORS.get();
-            DTORS.set(ptr::null_mut());
-        }
-    }
-}
diff --git a/sgx_tstd/src/sys_common/thread_parker/generic.rs b/sgx_tstd/src/sys_common/thread_parker/generic.rs
new file mode 100644
index 0000000..68da129
--- /dev/null
+++ b/sgx_tstd/src/sys_common/thread_parker/generic.rs
@@ -0,0 +1,137 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+//! Parker implementaiton based on a Mutex and Condvar.
+
+use crate::sync::atomic::AtomicUsize;
+use crate::sync::atomic::Ordering::SeqCst;
+use crate::sync::{SgxCondvar as Condvar, SgxMutex as Mutex};
+use crate::time::Duration;
+
+const EMPTY: usize = 0;
+const PARKED: usize = 1;
+const NOTIFIED: usize = 2;
+
+pub struct Parker {
+    state: AtomicUsize,
+    lock: Mutex<()>,
+    cvar: Condvar,
+}
+
+impl Parker {
+    pub fn new() -> Self {
+        Parker { state: AtomicUsize::new(EMPTY), lock: Mutex::new(()), cvar: Condvar::new() }
+    }
+
+    // This implementaiton doesn't require `unsafe`, but other implementations
+    // may assume this is only called by the thread that owns the Parker.
+    #[allow(clippy::single_match)]
+    pub unsafe fn park(&self) {
+        // If we were previously notified then we consume this notification and
+        // return quickly.
+        if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
+            return;
+        }
+
+        // Otherwise we need to coordinate going to sleep
+        let mut m = self.lock.lock().unwrap();
+        match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
+            Ok(_) => {}
+            Err(NOTIFIED) => {
+                // We must read here, even though we know it will be `NOTIFIED`.
+                // This is because `unpark` may have been called again since we read
+                // `NOTIFIED` in the `compare_exchange` above. We must perform an
+                // acquire operation that synchronizes with that `unpark` to observe
+                // any writes it made before the call to unpark. To do that we must
+                // read from the write it made to `state`.
+                let old = self.state.swap(EMPTY, SeqCst);
+                assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
+                return;
+            } // should consume this notification, so prohibit spurious wakeups in next park.
+            Err(_) => panic!("inconsistent park state"),
+        }
+        loop {
+            m = self.cvar.wait(m).unwrap();
+            match self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst) {
+                Ok(_) => return, // got a notification
+                Err(_) => {}     // spurious wakeup, go back to sleep
+            }
+        }
+    }
+
+    // This implementaiton doesn't require `unsafe`, but other implementations
+    // may assume this is only called by the thread that owns the Parker.
+    pub unsafe fn park_timeout(&self, dur: Duration) {
+        // Like `park` above we have a fast path for an already-notified thread, and
+        // afterwards we start coordinating for a sleep.
+        // return quickly.
+        if self.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
+            return;
+        }
+        let m = self.lock.lock().unwrap();
+        match self.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
+            Ok(_) => {}
+            Err(NOTIFIED) => {
+                // We must read again here, see `park`.
+                let old = self.state.swap(EMPTY, SeqCst);
+                assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
+                return;
+            } // should consume this notification, so prohibit spurious wakeups in next park.
+            Err(_) => panic!("inconsistent park_timeout state"),
+        }
+
+        // Wait with a timeout, and if we spuriously wake up or otherwise wake up
+        // from a notification we just want to unconditionally set the state back to
+        // empty, either consuming a notification or un-flagging ourselves as
+        // parked.
+        let (_m, _result) = self.cvar.wait_timeout(m, dur).unwrap();
+        match self.state.swap(EMPTY, SeqCst) {
+            NOTIFIED => {} // got a notification, hurray!
+            PARKED => {}   // no notification, alas
+            n => panic!("inconsistent park_timeout state: {}", n),
+        }
+    }
+
+    pub fn unpark(&self) {
+        // To ensure the unparked thread will observe any writes we made
+        // before this call, we must perform a release operation that `park`
+        // can synchronize with. To do that we must write `NOTIFIED` even if
+        // `state` is already `NOTIFIED`. That is why this must be a swap
+        // rather than a compare-and-swap that returns if it reads `NOTIFIED`
+        // on failure.
+        match self.state.swap(NOTIFIED, SeqCst) {
+            EMPTY => return,    // no one was waiting
+            NOTIFIED => return, // already unparked
+            PARKED => {}        // gotta go wake someone up
+            _ => panic!("inconsistent state in unpark"),
+        }
+
+        // There is a period between when the parked thread sets `state` to
+        // `PARKED` (or last checked `state` in the case of a spurious wake
+        // up) and when it actually waits on `cvar`. If we were to notify
+        // during this period it would be ignored and then when the parked
+        // thread went to sleep it would never wake up. Fortunately, it has
+        // `lock` locked at this stage so we can acquire `lock` to wait until
+        // it is ready to receive the notification.
+        //
+        // Releasing `lock` before the call to `notify_one` means that when the
+        // parked thread wakes it doesn't get woken only to have to wait for us
+        // to release `lock`.
+        drop(self.lock.lock().unwrap());
+        self.cvar.notify_one()
+    }
+}
diff --git a/sgx_tstd/src/sys/ext/ffi.rs b/sgx_tstd/src/sys_common/thread_parker/mod.rs
similarity index 86%
copy from sgx_tstd/src/sys/ext/ffi.rs
copy to sgx_tstd/src/sys_common/thread_parker/mod.rs
index 52ac67a..2a6b897 100644
--- a/sgx_tstd/src/sys/ext/ffi.rs
+++ b/sgx_tstd/src/sys_common/thread_parker/mod.rs
@@ -15,6 +15,5 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Unix-specific extension to the primitives in the `std::ffi` module
-
-pub use crate::sys_common::os_str_bytes::*;
+mod generic;
+pub use generic::Parker;
\ No newline at end of file
diff --git a/sgx_tstd/src/sys_common/util.rs b/sgx_tstd/src/sys_common/util.rs
deleted file mode 100644
index 21f3a15..0000000
--- a/sgx_tstd/src/sys_common/util.rs
+++ /dev/null
@@ -1,45 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License..
-
-use core::fmt;
-use crate::io::prelude::*;
-#[cfg(feature = "stdio")]
-use crate::sys::stdio::panic_output;
-
-#[cfg(feature = "stdio")]
-pub fn dumb_print(args: fmt::Arguments<'_>) {
-    if let Some(mut out) = panic_output() {
-        let _ = out.write_fmt(args);
-    }
-}
-
-#[cfg(not(feature = "stdio"))]
-pub fn dumb_print(args: fmt::Arguments<'_>) {
-
-}
-
-// Other platforms should use the appropriate platform-specific mechanism for
-// aborting the process.  If no platform-specific mechanism is available,
-// crate::intrinsics::abort() may be used instead.  The above implementations cover
-// all targets currently supported by libstd.
-
-pub fn abort(args: fmt::Arguments<'_>) -> ! {
-    dumb_print(format_args!("fatal runtime error: {}\n", args));
-    unsafe {
-        crate::sys::abort_internal();
-    }
-}
diff --git a/sgx_tstd/src/sys_common/wtf8.rs b/sgx_tstd/src/sys_common/wtf8.rs
index 4c9eb7d..66af92e 100644
--- a/sgx_tstd/src/sys_common/wtf8.rs
+++ b/sgx_tstd/src/sys_common/wtf8.rs
@@ -15,25 +15,37 @@
 // specific language governing permissions and limitations
 // under the License..
 
-//! Implementation of [the WTF-8 encoding]
+//! Implementation of [the WTF-8 encoding](https://simonsapin.github.io/wtf-8/).
 //!
+//! This library uses Rust’s type system to maintain
+//! [well-formedness](https://simonsapin.github.io/wtf-8/#well-formed),
+//! like the `String` and `&str` types do for UTF-8.
+//!
+//! Since [WTF-8 must not be used
+//! for interchange](https://simonsapin.github.io/wtf-8/#intended-audience),
+//! this library deliberately does not provide access to the underlying bytes
+//! of WTF-8 strings,
+//! nor can it decode WTF-8 from arbitrary bytes.
+//! WTF-8 strings can be obtained from UTF-8, UTF-16, or code points.
 
 // this module is imported from @SimonSapin's repo and has tons of dead code on
 // unix (it's mostly used on windows), so don't worry about dead code here.
 #![allow(dead_code)]
+#![allow(clippy::derive_hash_xor_eq)]
 
-use core::char;
-use core::fmt;
-use core::str;
 use core::str::next_code_point;
-use core::hash::{Hash, Hasher};
-use core::iter::FromIterator;
-use core::mem;
-use core::ops;
-use alloc_crate::slice;
-use alloc_crate::rc::Rc;
-use alloc_crate::sync::Arc;
-use alloc_crate::borrow::Cow;
+
+use crate::borrow::Cow;
+use crate::char;
+use crate::fmt;
+use crate::hash::{Hash, Hasher};
+use crate::iter::FromIterator;
+use crate::mem;
+use crate::ops;
+use crate::rc::Rc;
+use crate::slice;
+use crate::str;
+use crate::sync::Arc;
 use crate::sys_common::AsInner;
 
 const UTF8_REPLACEMENT_CHARACTER: &str = "\u{FFFD}";
@@ -207,9 +219,8 @@
     /// Copied from String::push
     /// This does **not** include the WTF-8 concatenation check.
     fn push_code_point_unchecked(&mut self, code_point: CodePoint) {
-        let c = unsafe { char::from_u32_unchecked(code_point.value) };
         let mut bytes = [0; 4];
-        let bytes = c.encode_utf8(&mut bytes).as_bytes();
+        let bytes = char::encode_utf8_raw(code_point.value, &mut bytes);
         self.bytes.extend_from_slice(bytes)
     }
 
@@ -392,6 +403,17 @@
         self.bytes.reserve(low);
         iterator.for_each(move |code_point| self.push(code_point));
     }
+
+    #[inline]
+    fn extend_one(&mut self, code_point: CodePoint) {
+        self.push(code_point);
+    }
+
+    #[inline]
+    fn extend_reserve(&mut self, additional: usize) {
+        // Lower bound of one byte per code point (ASCII only)
+        self.bytes.reserve(additional);
+    }
 }
 
 /// A borrowed slice of well-formed WTF-8 data.
@@ -764,6 +786,7 @@
 }
 
 /// Copied from core::str::StrPrelude::is_char_boundary
+#[allow(clippy::manual_range_contains)]
 #[inline]
 pub fn is_code_point_boundary(slice: &Wtf8, index: usize) -> bool {
     if index == slice.len() {
@@ -778,7 +801,7 @@
 /// Copied from core::str::raw::slice_unchecked
 #[inline]
 pub unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 {
-    // memory layout of an &[u8] and &Wtf8 are the same
+    // memory layout of a &[u8] and &Wtf8 are the same
     Wtf8::from_bytes_unchecked(slice::from_raw_parts(s.bytes.as_ptr().add(begin), end - begin))
 }
 
@@ -833,8 +856,7 @@
 
         let mut buf = [0; 2];
         self.code_points.next().map(|code_point| {
-            let c = unsafe { char::from_u32_unchecked(code_point.value) };
-            let n = c.encode_utf16(&mut buf).len();
+            let n = char::encode_utf16_raw(code_point.value, &mut buf).len();
             if n == 2 {
                 self.extra = buf[1];
             }
@@ -845,10 +867,11 @@
     #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) {
         let (low, high) = self.code_points.size_hint();
+        let ext = (self.extra != 0) as usize;
         // every code point gets either one u16 or two u16,
         // so this iterator is between 1 or 2 times as
         // long as the underlying iterator.
-        (low, high.and_then(|n| n.checked_mul(2)))
+        (low + ext, high.and_then(|n| n.checked_mul(2)).and_then(|n| n.checked_add(ext)))
     }
 }
 
diff --git a/sgx_tstd/src/thread/local.rs b/sgx_tstd/src/thread/local.rs
index ce96db4..a3440bd 100644
--- a/sgx_tstd/src/thread/local.rs
+++ b/sgx_tstd/src/thread/local.rs
@@ -16,9 +16,59 @@
 // under the License..
 
 //! Thread local storage
-use core::fmt;
-use crate::error::Error;
 
+use crate::error::Error;
+use crate::fmt;
+
+/// A thread local storage key which owns its contents.
+///
+/// This key uses the fastest possible implementation available to it for the
+/// target platform. It is instantiated with the [`thread_local!`] macro and the
+/// primary method is the [`with`] method.
+///
+/// The [`with`] method yields a reference to the contained value which cannot be
+/// sent across threads or escape the given closure.
+///
+/// # Initialization and Destruction
+///
+/// Initialization is dynamically performed on the first call to [`with`]
+/// within a thread, and values that implement [`Drop`] get destructed when a
+/// thread exits. Some caveats apply, which are explained below.
+///
+/// A `LocalKey`'s initializer cannot recursively depend on itself, and using
+/// a `LocalKey` in this way will cause the initializer to infinitely recurse
+/// on the first call to `with`.
+///
+/// # Examples
+///
+/// ```
+/// use std::cell::RefCell;
+/// use std::thread;
+///
+/// thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
+///
+/// FOO.with(|f| {
+///     assert_eq!(*f.borrow(), 1);
+///     *f.borrow_mut() = 2;
+/// });
+///
+/// // each thread starts out with the initial value of 1
+/// let t = thread::spawn(move|| {
+///     FOO.with(|f| {
+///         assert_eq!(*f.borrow(), 1);
+///         *f.borrow_mut() = 3;
+///     });
+/// });
+///
+/// // wait for the thread to complete and bail out on panic
+/// t.join().unwrap();
+///
+/// // we retain our original value of 2 despite the child thread
+/// FOO.with(|f| {
+///     assert_eq!(*f.borrow(), 2);
+/// });
+/// ```
+///
 pub struct LocalKey<T: 'static> {
     // This outer `LocalKey<T>` type is what's going to be stored in statics,
     // but actual data inside will sometimes be tagged with #[thread_local].
@@ -39,7 +89,7 @@
 
 impl<T: 'static> fmt::Debug for LocalKey<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("LocalKey { .. }")
+        f.debug_struct("LocalKey").finish_non_exhaustive()
     }
 }
 
@@ -51,7 +101,7 @@
 /// Publicity and attributes for each static are allowed. Example:
 ///
 /// ```
-/// use core::cell::RefCell;
+/// use std::cell::RefCell;
 /// thread_local! {
 ///     pub static FOO: RefCell<u32> = RefCell::new(1);
 ///
@@ -61,12 +111,25 @@
 /// # fn main() {}
 /// ```
 ///
+/// See [`LocalKey` documentation][`std::thread::LocalKey`] for more
+/// information.
+///
+/// [`std::thread::LocalKey`]: crate::thread::LocalKey
 #[macro_export]
 #[allow_internal_unstable(thread_local_internals)]
 macro_rules! thread_local {
     // empty (base case for the recursion)
     () => {};
 
+    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => (
+        $crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
+        $crate::thread_local!($($rest)*);
+    );
+
+    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => (
+        $crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
+    );
+
     // process multiple declarations
     ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
         $crate::__thread_local_inner!($(#[$attr])* $vis $name, $t, $init);
@@ -83,6 +146,29 @@
 #[allow_internal_unstable(thread_local_internals, cfg_target_thread_local, thread_local)]
 #[allow_internal_unsafe]
 macro_rules! __thread_local_inner {
+    // used to generate the `LocalKey` value for const-initialized thread locals
+    (@key $t:ty, const $init:expr) => {{
+        #[cfg_attr(not(windows), inline)] // see comments below
+        unsafe fn __getit() -> $crate::result::Result<&'static $t, $crate::thread::AccessError> {
+            const _REQUIRE_UNSTABLE: () = $crate::thread::require_unstable_const_init_thread_local();
+
+            if !$crate::mem::needs_drop::<$t>() || $crate::thread::thread_policy() == $crate::thread::SgxThreadPolicy::Bound {
+                #[thread_local]
+                static mut VAL: $t = $init;
+                Ok(&VAL)
+            } else {
+                Err(AccessError {
+                    msg: "If TLS data needs to be destructed, TCS policy must be bound.",
+                })
+            }
+        }
+
+        unsafe {
+            $crate::thread::LocalKey::new(__getit)
+        }
+    }};
+
+    // used to generate the `LocalKey` value for `thread_local!`
     (@key $t:ty, $init:expr) => {
         {
             #[inline]
@@ -107,9 +193,9 @@
             }
         }
     };
-    ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
+    ($(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $($init:tt)*) => {
         $(#[$attr])* $vis const $name: $crate::thread::LocalKey<$t> =
-            $crate::__thread_local_inner!(@key $t, $init);
+            $crate::__thread_local_inner!(@key $t, $($init)*);
     }
 }
 
@@ -140,24 +226,27 @@
     /// # Panics
     ///
     /// This function will `panic!()` if TLS data needs to be destructed,
-    /// TCS policy must be Bound.
+    /// TCS policy must be bound.
     pub fn with<F, R>(&'static self, f: F) -> R
     where
         F: FnOnce(&T) -> R,
     {
-        self.try_with(f).expect("Can not access a Thread Local Storage value")
+        self.try_with(f).expect(
+            "Cannot access a Thread Local Storage value."
+        )
     }
 
     /// Acquires a reference to the value in this TLS key.
     ///
     /// This will lazily initialize the value if this thread has not referenced
     /// this key yet. If the key has been destroyed (which may happen if this is called
-    /// in a destructor), this function will return an [`AccessError`](struct.AccessError.html).
+    /// in a destructor), this function will return an [`AccessError`].
     ///
     /// # Panics
     ///
     /// This function will still `panic!()` if the key is uninitialized and the
     /// key's initializer panics.
+    #[inline]
     pub fn try_with<F, R>(&'static self, f: F) -> Result<R, AccessError>
     where
         F: FnOnce(&T) -> R,
@@ -170,9 +259,9 @@
 }
 
 mod lazy {
-    use core::cell::UnsafeCell;
-    use core::mem;
-    use core::hint;
+    use crate::cell::UnsafeCell;
+    use crate::hint;
+    use crate::mem;
 
     pub struct LazyKeyInner<T> {
         inner: UnsafeCell<Option<T>>,
@@ -184,15 +273,23 @@
         }
 
         pub unsafe fn get(&self) -> Option<&'static T> {
+            // SAFETY: The caller must ensure no reference is ever handed out to
+            // the inner cell nor mutable reference to the Option<T> inside said
+            // cell. This make it safe to hand a reference, though the lifetime
+            // of 'static is itself unsafe, making the get method unsafe.
             (*self.inner.get()).as_ref()
         }
 
+        /// The caller must ensure that no reference is active: this method
+        /// needs unique access.
         pub unsafe fn initialize<F: FnOnce() -> T>(&self, init: F) -> &'static T {
             // Execute the initialization up front, *then* move it into our slot,
             // just in case initialization fails.
             let value = init();
             let ptr = self.inner.get();
 
+            // SAFETY:
+            //
             // note that this can in theory just be `*ptr = Some(value)`, but due to
             // the compiler will currently codegen that pattern with something like:
             //
@@ -205,8 +302,14 @@
             // value (an aliasing violation). To avoid setting the "I'm running a
             // destructor" flag we just use `mem::replace` which should sequence the
             // operations a little differently and make this safe to call.
-            mem::replace(&mut *ptr, Some(value));
+            //
+            // The precondition also ensures that we are the only one accessing
+            // `self` at the moment so replacing is fine.
+            let _ = mem::replace(&mut *ptr, Some(value));
 
+            // SAFETY: With the call to `mem::replace` it is guaranteed there is
+            // a `Some` behind `ptr`, not a `None` so `unreachable_unchecked`
+            // will never be reached.
             // After storing `Some` we want to get a reference to the contents of
             // what we just stored. While we could use `unwrap` here and it should
             // always work it empirically doesn't seem to always get optimized away,
@@ -218,8 +321,12 @@
             }
         }
 
+        /// The other methods hand out references while taking &self.
+        /// As such, callers of this method must ensure no `&` and `&mut` are
+        /// available and used at the same time.
         #[allow(unused)]
         pub unsafe fn take(&mut self) -> Option<T> {
+            // SAFETY: See doc comment for this method.
             (*self.inner.get()).take()
         }
     }
@@ -228,10 +335,9 @@
 pub mod statik {
     use super::lazy::LazyKeyInner;
     use super::AccessError;
-    use core::fmt;
-    use core::mem;
-    use sgx_trts::enclave::SgxGlobalData;
-    use sgx_trts::enclave::SgxThreadPolicy::*;
+    use crate::fmt;
+    use crate::mem;
+    use crate::thread::{self, SgxThreadPolicy};
 
     pub struct Key<T> {
         inner: LazyKeyInner<T>,
@@ -241,7 +347,7 @@
 
     impl<T> fmt::Debug for Key<T> {
         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-            f.pad("Key { .. }")
+            f.debug_struct("Key").finish_non_exhaustive()
         }
     }
 
@@ -251,14 +357,20 @@
         }
 
         pub unsafe fn get(&self, init: fn() -> T) -> Result<&'static T, AccessError> {
-            if !mem::needs_drop::<T>() || SgxGlobalData::new().thread_policy() == Bound {
+            if !mem::needs_drop::<T>() || thread::thread_policy() == SgxThreadPolicy::Bound {
+	        // SAFETY: The caller must ensure no reference is ever handed out to
+                // the inner cell nor mutable reference to the Option<T> inside said
+                // cell. This make it safe to hand a reference, though the lifetime
+                // of 'static is itself unsafe, making the get method unsafe.
                 let value = match self.inner.get() {
-                    Some(ref value) => value,
+                    Some(value) => value,
                     None => self.inner.initialize(init),
                 };
                 Ok(value)
             } else {
-                Err(AccessError { msg: "If TLS data needs to be destructed, TCS policy must be Bound." })
+                Err(AccessError {
+                    msg: "If TLS data needs to be destructed, TCS policy must be bound.",
+                })
             }
         }
     }
@@ -289,13 +401,12 @@
 pub mod fast {
     use super::lazy::LazyKeyInner;
     use super::AccessError;
-    use core::cell::Cell;
-    use core::fmt;
-    use core::mem;
-    use crate::sys::fast_thread_local::register_dtor;
-    use sgx_trts::enclave::SgxGlobalData;
-    use sgx_trts::enclave::SgxThreadPolicy::*;
-    
+    use crate::cell::Cell;
+    use crate::fmt;
+    use crate::mem;
+    use crate::sys::thread_local_dtor::register_dtor;
+    use crate::thread::{self, SgxThreadPolicy};
+
     #[derive(Copy, Clone)]
     enum DtorState {
         Unregistered,
@@ -326,7 +437,7 @@
 
     impl<T> fmt::Debug for Key<T> {
         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-            f.pad("Key { .. }")
+            f.debug_struct("Key").finish_non_exhaustive()
         }
     }
 
@@ -336,6 +447,13 @@
         }
 
         pub unsafe fn get<F: FnOnce() -> T>(&self, init: F) -> Result<&'static T, AccessError> {
+            // SAFETY: See the definitions of `LazyKeyInner::get` and
+            // `try_initialize` for more informations.
+            //
+            // The caller must ensure no mutable references are ever active to
+            // the inner cell or the inner T when this is called.
+            // The `try_initialize` is dependant on the passed `init` function
+            // for this.
             match self.inner.get() {
                 Some(val) => Ok(val),
                 None => self.try_initialize(init),
@@ -347,18 +465,19 @@
         // thread_local's, or it is being recursively initialized.
         //
         // Macos: Inlining this function can cause two `tlv_get_addr` calls to
-        // be performed for every call to `Key::get`. The #[cold] hint makes
-        // that less likely.
+        // be performed for every call to `Key::get`.
         // LLVM issue: https://bugs.llvm.org/show_bug.cgi?id=41722
-        #[cold]
+        #[inline(never)]
         unsafe fn try_initialize<F: FnOnce() -> T>(&self, init: F) -> Result<&'static T, AccessError> {
-            if mem::needs_drop::<T>() && SgxGlobalData::new().thread_policy() == Unbound {
-                return Err(AccessError{ msg: "If TLS data needs to be destructed, TCS policy must be Bound." });
+            if mem::needs_drop::<T>() && thread::thread_policy() == SgxThreadPolicy::Unbound {
+                return Err(AccessError {
+                    msg: "If TLS data needs to be destructed, TCS policy must be bound.",
+                });
             }
 
             if !super::pthread_info_tls.m_pthread.is_null() {
-                //
-                // note: If the current thread was created by pthread_create, we should call
+                // Note:
+                // If the current thread was created by pthread_create, we should call
                 // the try_register_dtor function. You can know whether the current thread has
                 // been created by pthread_create() through the m_thread member of pthread_info
                 // (thread local storage) of pthread library in intel sgx sdk.
@@ -366,11 +485,13 @@
                 // Destructor will only be called when a thread created by pthread_create exits,
                 // because sys_common::thread_local::StaticKey does not call pthread_key_delete
                 // to trigger the destructor.
-                //
                 if !mem::needs_drop::<T>() || self.try_register_dtor() {
+                // SAFETY: See comment above (his function doc).
                     Ok(self.inner.initialize(init))
                 } else {
-                    Err(AccessError{ msg: "Failed to register destructor." })
+                    Err(AccessError {
+                        msg: "Failed to register destructor.",
+                    })
                 }
             } else {
                 Ok(self.inner.initialize(init))
@@ -383,7 +504,11 @@
         unsafe fn try_register_dtor(&self) -> bool {
             match self.dtor_state.get() {
                 DtorState::Unregistered => {
-                    // dtor registration happens before initialization.
+                    // SAFETY: dtor registration happens before initialization.
+                    // Passing `self` as a pointer while using `destroy_value<T>`
+                    // is safe because the function will build a pointer to a
+                    // Key<T>, which is the type of self and so find the correct
+                    // size.
                     register_dtor(self as *const _ as *mut u8, destroy_value::<T>);
                     self.dtor_state.set(DtorState::Registered);
                     true
@@ -400,6 +525,12 @@
     unsafe extern "C" fn destroy_value<T>(ptr: *mut u8) {
         let ptr = ptr as *mut Key<T>;
 
+        // SAFETY:
+        //
+        // The pointer `ptr` has been built just above and comes from
+        // `try_register_dtor` where it is originally a Key<T> coming from `self`,
+        // making it non-NUL and of the correct type.
+        //
         // Right before we run the user destructor be sure to set the
         // `Option<T>` to `None`, and `dtor_state` to `RunningOrHasRun`. This
         // causes future calls to `get` to run `try_initialize_drop` again,
@@ -414,11 +545,11 @@
 pub mod os {
     use super::lazy::LazyKeyInner;
     use super::AccessError;
-    use core::cell::Cell;
-    use core::fmt;
-    use core::marker;
-    use core::ptr;
-    use crate::sys_common::thread_local::StaticKey as OsStaticKey;
+    use crate::cell::Cell;
+    use crate::fmt;
+    use crate::marker;
+    use crate::ptr;
+    use crate::sys_common::thread_local_key::StaticKey as OsStaticKey;
 
     pub struct Key<T> {
         // OS-TLS key that we'll use to key off.
@@ -428,7 +559,7 @@
 
     impl<T> fmt::Debug for Key<T> {
         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-            f.pad("Key { .. }")
+            f.debug_struct("Key").finish_non_exhaustive()
         }
     }
 
@@ -440,22 +571,27 @@
     }
 
     impl<T: 'static> Key<T> {
-        //
-        // note:
-        // 1. os::Key can be destructed normally when used for threads created by pthread_create().
+        // Note:
+        // 1. os::Key can be destructed normally when used for threads created by `pthread_create`.
         // 2. os::Key used in untrusted thread, the destructor will not be called.
-        //
         pub const fn new() -> Key<T> {
             Key { os: OsStaticKey::new(Some(destroy_value::<T>)), marker: marker::PhantomData }
         }
 
+        /// It is a requirement for the caller to ensure that no mutable
+        /// reference is active when this method is called.
         pub unsafe fn get(&'static self, init: fn() -> T) -> Result<&'static T, AccessError> {
+            // SAFETY: See the documentation for this method.
             let ptr = self.os.get() as *mut Value<T>;
             if ptr as usize > 1 {
-                if let Some(ref value) = (*ptr).inner.get() {
+                // SAFETY: the check ensured the pointer is safe (its destructor
+                // is not running) + it is coming from a trusted source (self).
+                if let Some(value) = (*ptr).inner.get() {
                     return Ok(value);
                 }
             }
+            // SAFETY: At this point we are sure we have no value and so
+            // initializing (or trying to) is safe.
             self.try_initialize(init)
         }
 
@@ -463,10 +599,14 @@
         // except in corner cases where thread_local dtors reference other
         // thread_local's, or it is being recursively initialized.
         unsafe fn try_initialize(&'static self, init: fn() -> T) -> Result<&'static T, AccessError> {
+            // SAFETY: No mutable references are ever handed out meaning getting
+            // the value is ok.
             let ptr = self.os.get() as *mut Value<T>;
             if ptr as usize == 1 {
                 // destructor is running
-                return Err(AccessError{msg: "Destructor is running."});
+                return Err(AccessError {
+                    msg: "Destructor is running.",
+                });
             }
 
             let ptr = if ptr.is_null() {
@@ -474,6 +614,8 @@
                 // local copy, so do that now.
                 let ptr: Box<Value<T>> = box Value { inner: LazyKeyInner::new(), key: self };
                 let ptr = Box::into_raw(ptr);
+                // SAFETY: At this point we are sure there is no value inside
+                // ptr so setting it will not affect anyone else.
                 self.os.set(ptr as *mut u8);
                 ptr
             } else {
@@ -481,12 +623,16 @@
                 ptr
             };
 
+            // SAFETY: ptr has been ensured as non-NUL just above an so can be
+            // dereferenced safely.
             Ok((*ptr).inner.initialize(init))
         }
     }
 
     unsafe extern "C" fn destroy_value<T: 'static>(ptr: *mut u8) {
-        // The OS TLS ensures that this key contains a NULL value when this
+        // SAFETY:
+        //
+        // The OS TLS ensures that this key contains a null value when this
         // destructor starts to run. We set it back to a sentinel value of 1 to
         // ensure that any future calls to `get` for this thread will return
         // `None`.
@@ -499,4 +645,4 @@
         drop(ptr);
         key.os.set(ptr::null_mut());
     }
-}
\ No newline at end of file
+}
diff --git a/sgx_tstd/src/thread/mod.rs b/sgx_tstd/src/thread/mod.rs
index 932d63c..814d124 100644
--- a/sgx_tstd/src/thread/mod.rs
+++ b/sgx_tstd/src/thread/mod.rs
@@ -17,40 +17,57 @@
 
 //! Native threads.
 
-use sgx_types::{sgx_thread_t, sgx_thread_self};
-use sgx_trts::enclave::*;
-use core::any::Any;
-use core::sync::atomic::AtomicUsize;
-use core::sync::atomic::Ordering::SeqCst;
+use crate::any::Any;
 #[cfg(feature = "thread")]
-use core::cell::UnsafeCell;
-#[cfg(feature = "thread")]
-use core::mem;
-use core::fmt;
-use core::num::NonZeroU64;
-use alloc_crate::sync::Arc;
-use alloc_crate::str;
-use crate::panic;
-use crate::panicking;
-use crate::sys_common::thread_info;
-use crate::sync::{SgxThreadMutex, SgxMutex, SgxCondvar};
-use crate::time::Duration;
-#[cfg(feature = "thread")]
-use crate::sys::thread as imp;
+use crate::cell::UnsafeCell;
+use crate::ffi::{CStr, CString};
+use crate::fmt;
 #[cfg(feature = "thread")]
 use crate::io;
-use crate::ffi::{CStr, CString};
+#[cfg(feature = "thread")]
+use crate::mem;
+use crate::num::NonZeroU64;
+#[cfg(feature = "thread")]
+use crate::num::NonZeroUsize;
+#[cfg(feature = "thread")]
+use crate::panic;
+use crate::panicking;
+use crate::str;
+use crate::sync::Arc;
+#[cfg(feature = "thread")]
+use crate::sys::thread as imp;
+use crate::sys_common::mutex;
+use crate::sys_common::thread_info;
+use crate::sys_common::thread_parker::Parker;
 #[cfg(feature = "thread")]
 use crate::sys_common::{AsInner, IntoInner};
+use crate::time::Duration;
 
-#[macro_use] mod local;
-pub use self::local::{LocalKey, AccessError};
+use sgx_types::{sgx_thread_t, sgx_thread_self};
+use sgx_trts::enclave::*;
+
+pub use sgx_trts::enclave::SgxThreadPolicy;
+
+////////////////////////////////////////////////////////////////////////////////
+// Thread-local storage
+////////////////////////////////////////////////////////////////////////////////
+
+#[macro_use]
+mod local;
+
+pub use self::local::{AccessError, LocalKey};
+
 pub use self::local::statik::Key as __StaticLocalKeyInner;
 #[cfg(feature = "thread")]
 pub use self::local::fast::Key as __FastLocalKeyInner;
 #[cfg(feature = "thread")]
 pub use self::local::os::Key as __OsLocalKeyInner;
 
+// This is only used to make thread locals with `const { .. }` initialization
+// expressions unstable. If and/or when that syntax is stabilized with thread
+// locals this will simply be removed.
+pub const fn require_unstable_const_init_thread_local() {}
+
 ////////////////////////////////////////////////////////////////////////////////
 // Builder
 ////////////////////////////////////////////////////////////////////////////////
@@ -63,7 +80,6 @@
 /// The two configurations available are:
 ///
 /// - [`name`]: specifies an [associated name for the thread][naming-threads]
-/// - [`stack_size`]: specifies the [desired stack size for the thread][stack-size]
 ///
 /// The [`spawn`] method will take ownership of the builder and create an
 /// [`io::Result`] to the thread handle with the given configuration.
@@ -75,6 +91,27 @@
 /// to recover from a failure to launch a thread, indeed the free function will
 /// panic where the `Builder` method will return a [`io::Result`].
 ///
+/// # Examples
+///
+/// ```
+/// use std::thread;
+///
+/// let builder = thread::Builder::new();
+///
+/// let handler = builder.spawn(|| {
+///     // thread code
+/// }).unwrap();
+///
+/// handler.join().unwrap();
+/// ```
+///
+/// [`stack_size`]: Builder::stack_size
+/// [`name`]: Builder::name
+/// [`spawn`]: Builder::spawn
+/// [`thread::spawn`]: spawn
+/// [`io::Result`]: crate::io::Result
+/// [`unwrap`]: crate::result::Result::unwrap
+/// [naming-threads]: ./index.html#naming-threads
 #[cfg(feature = "thread")]
 #[derive(Debug)]
 pub struct Builder {
@@ -87,6 +124,21 @@
     /// Generates the base configuration for spawning a thread, from which
     /// configuration methods can be chained.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new()
+    ///                               .name("foo".into());
+    ///
+    ///
+    /// let handler = builder.spawn(|| {
+    ///     // thread code
+    /// }).unwrap();
+    ///
+    /// handler.join().unwrap();
+    /// ```
     pub fn new() -> Builder {
         if rsgx_get_thread_policy() != SgxThreadPolicy::Bound {
             panic!("The sgx thread policy must be Bound!");
@@ -102,6 +154,22 @@
     /// For more information about named threads, see
     /// [this module-level documentation][naming-threads].
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new()
+    ///     .name("foo".into());
+    ///
+    /// let handler = builder.spawn(|| {
+    ///     assert_eq!(thread::current().name(), Some("foo"))
+    /// }).unwrap();
+    ///
+    /// handler.join().unwrap();
+    /// ```
+    ///
+    /// [naming-threads]: ./index.html#naming-threads
     pub fn name(mut self, name: String) -> Builder {
         self.name = Some(name);
         self
@@ -113,7 +181,7 @@
     /// The spawned thread may outlive the caller (unless the caller thread
     /// is the main thread; the whole process is terminated when the main
     /// thread finishes). The join handle can be used to block on
-    /// termination of the child thread, including recovering its panics.
+    /// termination of the spawned thread, including recovering its panics.
     ///
     /// For a more complete documentation see [`thread::spawn`][`spawn`].
     ///
@@ -123,14 +191,25 @@
     /// [`io::Result`] to capture any failure to create the thread at
     /// the OS level.
     ///
-    /// [`spawn`]: ../../std/thread/fn.spawn.html
-    /// [`io::Result`]: ../../std/io/type.Result.html
-    /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html
+    /// [`io::Result`]: crate::io::Result
     ///
     /// # Panics
     ///
     /// Panics if a thread name was set and it contained null bytes.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new();
+    ///
+    /// let handler = builder.spawn(|| {
+    ///     // thread code
+    /// }).unwrap();
+    ///
+    /// handler.join().unwrap();
+    /// ```
     pub fn spawn<F, T>(self, f: F) -> io::Result<JoinHandle<T>>
     where
         F: FnOnce() -> T,
@@ -140,6 +219,64 @@
         unsafe { self.spawn_unchecked(f) }
     }
 
+    /// Spawns a new thread without any lifetime restrictions by taking ownership
+    /// of the `Builder`, and returns an [`io::Result`] to its [`JoinHandle`].
+    ///
+    /// The spawned thread may outlive the caller (unless the caller thread
+    /// is the main thread; the whole process is terminated when the main
+    /// thread finishes). The join handle can be used to block on
+    /// termination of the spawned thread, including recovering its panics.
+    ///
+    /// This method is identical to [`thread::Builder::spawn`][`Builder::spawn`],
+    /// except for the relaxed lifetime bounds, which render it unsafe.
+    /// For a more complete documentation see [`thread::spawn`][`spawn`].
+    ///
+    /// # Errors
+    ///
+    /// Unlike the [`spawn`] free function, this method yields an
+    /// [`io::Result`] to capture any failure to create the thread at
+    /// the OS level.
+    ///
+    /// # Panics
+    ///
+    /// Panics if a thread name was set and it contained null bytes.
+    ///
+    /// # Safety
+    ///
+    /// The caller has to ensure that no references in the supplied thread closure
+    /// or its return type can outlive the spawned thread's lifetime. This can be
+    /// guaranteed in two ways:
+    ///
+    /// - ensure that [`join`][`JoinHandle::join`] is called before any referenced
+    /// data is dropped
+    /// - use only types with `'static` lifetime bounds, i.e., those with no or only
+    /// `'static` references (both [`thread::Builder::spawn`][`Builder::spawn`]
+    /// and [`thread::spawn`][`spawn`] enforce this property statically)
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(thread_spawn_unchecked)]
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new();
+    ///
+    /// let x = 1;
+    /// let thread_x = &x;
+    ///
+    /// let handler = unsafe {
+    ///     builder.spawn_unchecked(move || {
+    ///         println!("x = {}", *thread_x);
+    ///     }).unwrap()
+    /// };
+    ///
+    /// // caller has to ensure `join()` is called, otherwise
+    /// // it is possible to access freed memory if `x` gets
+    /// // dropped before the thread closure is executed!
+    /// handler.join().unwrap();
+    /// ```
+    ///
+    /// [`io::Result`]: crate::io::Result
     pub unsafe fn spawn_unchecked<'a, F, T>(self, f: F) -> io::Result<JoinHandle<T>>
     where
         F: FnOnce() -> T,
@@ -151,8 +288,7 @@
         let my_thread = SgxThread::new(name);
         let their_thread = my_thread.clone();
 
-        let my_packet : Arc<UnsafeCell<Option<Result<T>>>>
-            = Arc::new(UnsafeCell::new(None));
+        let my_packet: Arc<UnsafeCell<Option<Result<T>>>> = Arc::new(UnsafeCell::new(None));
         let their_packet = my_packet.clone();
 
         let main = move || {
@@ -166,10 +302,16 @@
             }));
             #[cfg(not(feature = "backtrace"))]
             let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
+            // SAFETY: `their_packet` as been built just above and moved by the
+            // closure (it is an Arc<...>) and `my_packet` will be stored in the
+            // same `JoinInner` as this closure meaning the mutation will be
+            // safe (not modify it and affect a value far away).
             *their_packet.get() = Some(try_result);
         };
 
         Ok(JoinHandle(JoinInner {
+            // SAFETY:
+            //
             // `imp::Thread::new` takes a closure with a `'static` lifetime, since it's passed
             // through FFI or otherwise used with low-level threading primitives that have no
             // notion of or way to enforce lifetimes.
@@ -182,9 +324,9 @@
             // exist after the thread has terminated, which is signaled by `Thread::join`
             // returning.
             native: Some(imp::Thread::new(
-                mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(Box::new(
-                    main,
-                )),
+                mem::transmute::<Box<dyn FnOnce() + 'a>, Box<dyn FnOnce() + 'static>>(
+                    Box::new(main),
+                ),          
             )?),
             thread: my_thread,
             packet: Packet(my_packet),
@@ -198,15 +340,16 @@
 
 /// Spawns a new thread, returning a [`JoinHandle`] for it.
 ///
-/// The join handle will implicitly *detach* the child thread upon being
-/// dropped. In this case, the child thread may outlive the parent (unless
-/// the parent thread is the main thread; the whole process is terminated when
-/// the main thread finishes). Additionally, the join handle provides a [`join`]
-/// method that can be used to join the child thread. If the child thread
-/// panics, [`join`] will return an [`Err`] containing the argument given to
-/// [`panic`].
+/// The join handle provides a [`join`] method that can be used to join the spawned
+/// thread. If the spawned thread panics, [`join`] will return an [`Err`] containing
+/// the argument given to [`panic!`].
 ///
-/// This will create a thread using default parameters of [`Builder`], if you
+/// If the join handle is dropped, the spawned thread will implicitly be *detached*.
+/// In this case, the spawned thread may no longer be joined.
+/// (It is the responsibility of the program to either eventually join threads it
+/// creates or detach them; otherwise, a resource leak will result.)
+///
+/// This call will create a thread using default parameters of [`Builder`], if you
 /// want to specify the stack size or the name of the thread, use this API
 /// instead.
 ///
@@ -215,8 +358,8 @@
 ///
 /// - The `'static` constraint means that the closure and its return value
 ///   must have a lifetime of the whole program execution. The reason for this
-///   is that threads can `detach` and outlive the lifetime they have been
-///   created in.
+///   is that threads can outlive the lifetime they have been created in.
+///
 ///   Indeed if the thread, and by extension its return value, can outlive their
 ///   caller, we need to make sure that they will be valid afterwards, and since
 ///   we *can't* know when it will return we need to have them valid as long as
@@ -235,6 +378,65 @@
 /// Panics if the OS fails to create a thread; use [`Builder::spawn`]
 /// to recover from such errors.
 ///
+/// # Examples
+///
+/// Creating a thread.
+///
+/// ```
+/// use std::thread;
+///
+/// let handler = thread::spawn(|| {
+///     // thread code
+/// });
+///
+/// handler.join().unwrap();
+/// ```
+///
+/// As mentioned in the module documentation, threads are usually made to
+/// communicate using [`channels`], here is how it usually looks.
+///
+/// This example also shows how to use `move`, in order to give ownership
+/// of values to a thread.
+///
+/// ```
+/// use std::thread;
+/// use std::sync::mpsc::channel;
+///
+/// let (tx, rx) = channel();
+///
+/// let sender = thread::spawn(move || {
+///     tx.send("Hello, thread".to_owned())
+///         .expect("Unable to send on channel");
+/// });
+///
+/// let receiver = thread::spawn(move || {
+///     let value = rx.recv().expect("Unable to receive from channel");
+///     println!("{}", value);
+/// });
+///
+/// sender.join().expect("The sender thread has panicked");
+/// receiver.join().expect("The receiver thread has panicked");
+/// ```
+///
+/// A thread can also return a value through its [`JoinHandle`], you can use
+/// this to make asynchronous computations (futures might be more appropriate
+/// though).
+///
+/// ```
+/// use std::thread;
+///
+/// let computation = thread::spawn(|| {
+///     // Some expensive computation.
+///     42
+/// });
+///
+/// let result = computation.join().unwrap();
+/// println!("{}", result);
+/// ```
+///
+/// [`channels`]: crate::sync::mpsc
+/// [`join`]: JoinHandle::join
+/// [`Err`]: crate::result::Result::Err
 #[cfg(feature = "thread")]
 pub fn spawn<F, T>(f: F) -> JoinHandle<T>
 where
@@ -247,29 +449,61 @@
 
 /// Gets a handle to the thread that invokes it.
 ///
+/// # Examples
+///
+/// Getting a handle to the current thread with `thread::current()`:
+///
+/// ```
+/// use std::thread;
+///
+/// let handler = thread::Builder::new()
+///     .name("named thread".into())
+///     .spawn(|| {
+///         let handle = thread::current();
+///         assert_eq!(handle.name(), Some("named thread"));
+///     })
+///     .unwrap();
+///
+/// handler.join().unwrap();
+/// ```
 pub fn current() -> SgxThread {
-    thread_info::current_thread().expect("use of thread::current() need TCS policy is Bound")
+    thread_info::current_thread().expect(
+        "Use of thread::current() need TCS policy is bound",
+    )
 }
 
 /// Cooperatively gives up a timeslice to the OS scheduler.
 ///
-/// This is used when the programmer knows that the thread will have nothing
-/// to do for some time, and thus avoid wasting computing time.
+/// This calls the underlying OS scheduler's yield primitive, signaling
+/// that the calling thread is willing to give up its remaining timeslice
+/// so that the OS may schedule other threads on the CPU.
 ///
-/// For example when polling on a resource, it is common to check that it is
-/// available, and if not to yield in order to avoid busy waiting.
+/// A drawback of yielding in a loop is that if the OS does not have any
+/// other ready threads to run on the current CPU, the thread will effectively
+/// busy-wait, which wastes CPU time and energy.
 ///
-/// Thus the pattern of `yield`ing after a failed poll is rather common when
-/// implementing low-level shared resources or synchronization primitives.
+/// Therefore, when waiting for events of interest, a programmer's first
+/// choice should be to use synchronization devices such as [`channel`]s,
+/// [`Condvar`]s, [`Mutex`]es or [`join`] since these primitives are
+/// implemented in a blocking manner, giving up the CPU until the event
+/// of interest has occurred which avoids repeated yielding.
 ///
-/// However programmers will usually prefer to use [`channel`]s, [`Condvar`]s,
-/// [`Mutex`]es or [`join`] for their synchronization routines, as they avoid
-/// thinking about thread scheduling.
+/// `yield_now` should thus be used only rarely, mostly in situations where
+/// repeated polling is required because there is no other suitable way to
+/// learn when an event of interest has occurred.
 ///
-/// Note that [`channel`]s for example are implemented using this primitive.
-/// Indeed when you call `send` or `recv`, which are blocking, they will yield
-/// if the channel is not available.
+/// # Examples
 ///
+/// ```
+/// use std::thread;
+///
+/// thread::yield_now();
+/// ```
+///
+/// [`channel`]: crate::sync::mpsc
+/// [`join`]: JoinHandle::join
+/// [`Condvar`]: crate::sync::Condvar
+/// [`Mutex`]: crate::sync::Mutex
 #[cfg(feature = "thread")]
 pub fn yield_now() {
     imp::Thread::yield_now()
@@ -287,6 +521,37 @@
 /// message to other threads warning that a thread has panicked (e.g., for
 /// monitoring purposes).
 ///
+/// # Examples
+///
+/// ```should_panic
+/// use std::thread;
+///
+/// struct SomeStruct;
+///
+/// impl Drop for SomeStruct {
+///     fn drop(&mut self) {
+///         if thread::panicking() {
+///             println!("dropped while unwinding");
+///         } else {
+///             println!("dropped while not unwinding");
+///         }
+///     }
+/// }
+///
+/// {
+///     print!("a: ");
+///     let a = SomeStruct;
+/// }
+///
+/// {
+///     print!("b: ");
+///     let b = SomeStruct;
+///     panic!()
+/// }
+/// ```
+///
+/// [Mutex]: crate::sync::Mutex
+#[inline]
 pub fn panicking() -> bool {
     panicking::panicking()
 }
@@ -296,6 +561,8 @@
 /// The thread may sleep longer than the duration specified due to scheduling
 /// specifics or platform-dependent functionality. It will never sleep less.
 ///
+/// This function is blocking, and should not be used in `async` functions.
+///
 /// # Platform-specific behavior
 ///
 /// On Unix platforms, the underlying syscall may be interrupted by a
@@ -303,6 +570,14 @@
 /// the specified duration, this function may invoke that system call multiple
 /// times.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::thread;
+///
+/// // Let's sleep for 2 seconds:
+/// thread::sleep_ms(2000);
+/// ```
 #[cfg(feature = "thread")]
 pub fn sleep_ms(ms: u32) {
     sleep(Duration::from_millis(ms as u64))
@@ -313,6 +588,8 @@
 /// The thread may sleep longer than the duration specified due to scheduling
 /// specifics or platform-dependent functionality. It will never sleep less.
 ///
+/// This function is blocking, and should not be used in `async` functions.
+///
 /// # Platform-specific behavior
 ///
 /// On Unix platforms, the underlying syscall may be interrupted by a
@@ -322,64 +599,32 @@
 /// Platforms which do not support nanosecond precision for sleeping will
 /// have `dur` rounded up to the nearest granularity of time they can sleep for.
 ///
+/// Currently, specifying a zero duration on Unix platforms returns immediately
+/// without invoking the underlying [`nanosleep`] syscall, whereas on Windows
+/// platforms the underlying [`Sleep`] syscall is always invoked.
+/// If the intention is to yield the current time-slice you may want to use
+/// [`yield_now`] instead.
+///
+/// [`nanosleep`]: https://linux.die.net/man/2/nanosleep
+/// [`Sleep`]: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
+///
+/// # Examples
+///
+/// ```no_run
+/// use std::{thread, time};
+///
+/// let ten_millis = time::Duration::from_millis(10);
+/// let now = time::Instant::now();
+///
+/// thread::sleep(ten_millis);
+///
+/// assert!(now.elapsed() >= ten_millis);
+/// ```
 #[cfg(feature = "thread")]
 pub fn sleep(dur: Duration) {
     imp::Thread::sleep(dur)
 }
 
-///
-/// The rsgx_thread_self function returns the unique thread identification.
-///
-/// # Description
-///
-/// The function is a simple wrap of get_thread_data() provided in the tRTS,
-/// which provides a trusted thread unique identifier.
-///
-/// # Requirements
-///
-/// Library: libsgx_tstdc.a
-///
-/// # Return value
-///
-/// The return value cannot be NULL and is always valid as long as it is invoked by a thread inside the enclave.
-///
-pub fn rsgx_thread_self() -> sgx_thread_t {
-    unsafe { sgx_thread_self() }
-}
-
-///
-/// The rsgx_thread_equal function compares two thread identifiers.
-///
-/// # Description
-///
-/// The function compares two thread identifiers provided by sgx_thread_
-/// self to determine if the IDs refer to the same trusted thread.
-///
-/// # Requirements
-///
-/// Library: libsgx_tstdc.a
-///
-/// # Return value
-///
-/// **true**
-///
-/// The two thread IDs are equal.
-///
-pub fn rsgx_thread_equal(a: sgx_thread_t, b: sgx_thread_t) -> bool {
-    a == b
-}
-
-pub fn current_td() -> SgxThreadData {
-    unsafe {
-        SgxThreadData::from_raw(rsgx_thread_self())
-    }
-}
-
-// constants for park/unpark
-const EMPTY: usize = 0;
-const PARKED: usize = 1;
-const NOTIFIED: usize = 2;
-
 /// Blocks unless or until the current thread's token is made available.
 ///
 /// A call to `park` does not guarantee that the thread will remain parked
@@ -427,38 +672,49 @@
 ///
 /// * It can be implemented very efficiently on many platforms.
 ///
+/// # Examples
+///
+/// ```
+/// use std::thread;
+/// use std::sync::{Arc, atomic::{Ordering, AtomicBool}};
+/// use std::time::Duration;
+///
+/// let flag = Arc::new(AtomicBool::new(false));
+/// let flag2 = Arc::clone(&flag);
+///
+/// let parked_thread = thread::spawn(move || {
+///     // We want to wait until the flag is set. We *could* just spin, but using
+///     // park/unpark is more efficient.
+///     while !flag2.load(Ordering::Acquire) {
+///         println!("Parking thread");
+///         thread::park();
+///         // We *could* get here spuriously, i.e., way before the 10ms below are over!
+///         // But that is no problem, we are in a loop until the flag is set anyway.
+///         println!("Thread unparked");
+///     }
+///     println!("Flag received");
+/// });
+///
+/// // Let some time pass for the thread to be spawned.
+/// thread::sleep(Duration::from_millis(10));
+///
+/// // Set the flag, and let the thread wake up.
+/// // There is no race condition here, if `unpark`
+/// // happens first, `park` will return immediately.
+/// // Hence there is no risk of a deadlock.
+/// flag.store(true, Ordering::Release);
+/// println!("Unpark the thread");
+/// parked_thread.thread().unpark();
+///
+/// parked_thread.join().unwrap();
+/// ```
+///
+/// [`unpark`]: Thread::unpark
+/// [`thread::park_timeout`]: park_timeout
 pub fn park() {
-    let thread = current();
-
-    // If we were previously notified then we consume this notification and
-    // return quickly.
-    if thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
-        return;
-    }
-
-    // Otherwise we need to coordinate going to sleep
-    let mut m = thread.inner.lock.lock().unwrap();
-    match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
-        Ok(_) => {}
-        Err(NOTIFIED) => {
-            // We must read here, even though we know it will be `NOTIFIED`.
-            // This is because `unpark` may have been called again since we read
-            // `NOTIFIED` in the `compare_exchange` above. We must perform an
-            // acquire operation that synchronizes with that `unpark` to observe
-            // any writes it made before the call to unpark. To do that we must
-            // read from the write it made to `state`.
-            let old = thread.inner.state.swap(EMPTY, SeqCst);
-            assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
-            return;
-        } // should consume this notification, so prohibit spurious wakeups in next park.
-        Err(_) => panic!("inconsistent park state"),
-    }
-    loop {
-        m = thread.inner.cvar.wait(m).unwrap();
-        match thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst) {
-            Ok(_) => return, // got a notification
-            Err(_) => {}     // spurious wakeup, go back to sleep
-        }
+    // SAFETY: park_timeout is called on the parker owned by this thread.
+    unsafe {
+        current().inner.parker.park();
     }
 }
 
@@ -470,13 +726,10 @@
 /// The semantics of this function are equivalent to [`park`] except
 /// that the thread will be blocked for roughly no longer than `dur`. This
 /// method should not be used for precise timing due to anomalies such as
-/// preemption or platform differences that may not cause the maximum
+/// preemption or platform differences that might not cause the maximum
 /// amount of time waited to be precisely `ms` long.
 ///
 /// See the [park documentation][`park`] for more detail.
-///
-/// [`park_timeout`]: fn.park_timeout.html
-/// [`park`]: ../../std/thread/fn.park.html
 pub fn park_timeout_ms(ms: u32) {
     park_timeout(Duration::from_millis(ms as u64))
 }
@@ -487,7 +740,7 @@
 /// The semantics of this function are equivalent to [`park`][park] except
 /// that the thread will be blocked for roughly no longer than `dur`. This
 /// method should not be used for precise timing due to anomalies such as
-/// preemption or platform differences that may not cause the maximum
+/// preemption or platform differences that might not cause the maximum
 /// amount of time waited to be precisely `dur` long.
 ///
 /// See the [park documentation][park] for more details.
@@ -519,38 +772,10 @@
 ///     timeout_remaining = timeout - elapsed;
 /// }
 /// ```
-///
-/// [park]: fn.park.html
 pub fn park_timeout(dur: Duration) {
-    let thread = current();
-
-    // Like `park` above we have a fast path for an already-notified thread, and
-    // afterwards we start coordinating for a sleep.
-    // return quickly.
-    if thread.inner.state.compare_exchange(NOTIFIED, EMPTY, SeqCst, SeqCst).is_ok() {
-        return;
-    }
-    let m = thread.inner.lock.lock().unwrap();
-    match thread.inner.state.compare_exchange(EMPTY, PARKED, SeqCst, SeqCst) {
-        Ok(_) => {}
-        Err(NOTIFIED) => {
-            // We must read again here, see `park`.
-            let old = thread.inner.state.swap(EMPTY, SeqCst);
-            assert_eq!(old, NOTIFIED, "park state changed unexpectedly");
-            return;
-        } // should consume this notification, so prohibit spurious wakeups in next park.
-        Err(_) => panic!("inconsistent park_timeout state"),
-    }
-
-    // Wait with a timeout, and if we spuriously wake up or otherwise wake up
-    // from a notification we just want to unconditionally set the state back to
-    // empty, either consuming a notification or un-flagging ourselves as
-    // parked.
-    let (_m, _result) = thread.inner.cvar.wait_timeout(m, dur).unwrap();
-    match thread.inner.state.swap(EMPTY, SeqCst) {
-        NOTIFIED => {} // got a notification, hurray!
-        PARKED => {}   // no notification, alas
-        n => panic!("inconsistent park_timeout state: {}", n),
+    // SAFETY: park_timeout is called on the parker owned by this thread.
+    unsafe {
+        current().inner.parker.park_timeout(dur);
     }
 }
 
@@ -578,33 +803,29 @@
 /// assert!(thread::current().id() != other_thread_id);
 /// ```
 ///
-/// [`id`]: ../../std/thread/struct.Thread.html#method.id
-/// [`Thread`]: ../../std/thread/struct.Thread.html
+/// [`id`]: Thread::id
 #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
 pub struct ThreadId(NonZeroU64);
 
 impl ThreadId {
     // Generate a new unique thread ID.
     fn new() -> ThreadId {
-        // We never call `GUARD.init()`, so it is UB to attempt to
-        // acquire this mutex reentrantly!
-
-        static GUARD: SgxThreadMutex = SgxThreadMutex::new();
+        // It is UB to attempt to acquire this mutex reentrantly!
+        static GUARD: mutex::SgxThreadMutex = mutex::SgxThreadMutex::new();
         static mut COUNTER: u64 = 1;
 
         unsafe {
-            let _ = GUARD.lock();
+            let _guard = GUARD.lock();
 
             // If we somehow use up all our bits, panic so that we're not
             // covering up subtle bugs of IDs being reused.
-            if COUNTER == crate::u64::MAX {
+            if COUNTER == u64::MAX {
                 panic!("failed to generate unique thread ID: bitspace exhausted");
             }
 
             let id = COUNTER;
             COUNTER += 1;
-
-            GUARD.unlock();
+            let _ = GUARD.unlock();
 
             ThreadId(NonZeroU64::new(id).unwrap())
         }
@@ -631,35 +852,40 @@
 struct Inner {
     name: Option<CString>, // Guaranteed to be UTF-8
     id: ThreadId,
-
-    // state for thread park/unpark
-    state: AtomicUsize,
-    lock: SgxMutex<()>,
-    cvar: SgxCondvar,
+    parker: Parker,
 }
 
+#[derive(Clone)]
 /// A handle to a thread.
 ///
-#[derive(Clone)]
+/// Threads are represented via the `Thread` type, which you can get in one of
+/// two ways:
+///
+/// * By spawning a new thread, e.g., using the [`thread::spawn`][`spawn`]
+///   function, and calling [`thread`][`JoinHandle::thread`] on the
+///   [`JoinHandle`].
+/// * By requesting the current thread, using the [`thread::current`] function.
+///
+/// The [`thread::current`] function is available even for threads not spawned
+/// by the APIs of this module.
+///
+/// There is usually no need to create a `Thread` struct yourself, one
+/// should instead use a function like `spawn` to create new threads, see the
+/// docs of [`Builder`] and [`spawn`] for more details.
+///
+/// [`thread::current`]: current
 pub struct SgxThread {
     inner: Arc<Inner>,
 }
 
 impl SgxThread {
-
     // Used only internally to construct a thread object without spawning
     // Panics if the name contains nuls.
     pub(crate) fn new(name: Option<String>) -> SgxThread {
         let cname =
             name.map(|n| CString::new(n).expect("thread name may not contain interior null bytes"));
         SgxThread {
-            inner: Arc::new(Inner {
-                name: cname,
-                id: ThreadId::new(),
-                state: AtomicUsize::new(EMPTY),
-                lock: SgxMutex::new(()),
-                cvar: SgxCondvar::new(),
-            }),
+            inner: Arc::new(Inner { name: cname, id: ThreadId::new(), parker: Parker::new() }),
         }
     }
 
@@ -669,37 +895,49 @@
     /// the [`park`][park] function and the `unpark()` method. These can be
     /// used as a more CPU-efficient implementation of a spinlock.
     ///
+    /// See the [park documentation][park] for more details.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::thread;
+    /// use std::time::Duration;
+    ///
+    /// let parked_thread = thread::Builder::new()
+    ///     .spawn(|| {
+    ///         println!("Parking thread");
+    ///         thread::park();
+    ///         println!("Thread unparked");
+    ///     })
+    ///     .unwrap();
+    ///
+    /// // Let some time pass for the thread to be spawned.
+    /// thread::sleep(Duration::from_millis(10));
+    ///
+    /// println!("Unpark the thread");
+    /// parked_thread.thread().unpark();
+    ///
+    /// parked_thread.join().unwrap();
+    /// ```
+    #[inline]
     pub fn unpark(&self) {
-        // To ensure the unparked thread will observe any writes we made
-        // before this call, we must perform a release operation that `park`
-        // can synchronize with. To do that we must write `NOTIFIED` even if
-        // `state` is already `NOTIFIED`. That is why this must be a swap
-        // rather than a compare-and-swap that returns if it reads `NOTIFIED`
-        // on failure.
-        match self.inner.state.swap(NOTIFIED, SeqCst) {
-            EMPTY => return,    // no one was waiting
-            NOTIFIED => return, // already unparked
-            PARKED => {}        // gotta go wake someone up
-            _ => panic!("inconsistent state in unpark"),
-        }
-
-        // There is a period between when the parked thread sets `state` to
-        // `PARKED` (or last checked `state` in the case of a spurious wake
-        // up) and when it actually waits on `cvar`. If we were to notify
-        // during this period it would be ignored and then when the parked
-        // thread went to sleep it would never wake up. Fortunately, it has
-        // `lock` locked at this stage so we can acquire `lock` to wait until
-        // it is ready to receive the notification.
-        //
-        // Releasing `lock` before the call to `notify_one` means that when the
-        // parked thread wakes it doesn't get woken only to have to wait for us
-        // to release `lock`.
-        drop(self.inner.lock.lock().unwrap());
-        self.inner.cvar.notify_one()
+        self.inner.parker.unpark();
     }
 
     /// Gets the thread's unique identifier.
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let other_thread = thread::spawn(|| {
+    ///     thread::current().id()
+    /// });
+    ///
+    /// let other_thread_id = other_thread.join().unwrap();
+    /// assert!(thread::current().id() != other_thread_id);
+    /// ```
     pub fn id(&self) -> ThreadId {
         self.inner.id
     }
@@ -709,18 +947,53 @@
     /// For more information about named threads, see
     /// [this module-level documentation][naming-threads].
     ///
+    /// # Examples
+    ///
+    /// Threads by default have no name specified:
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new();
+    ///
+    /// let handler = builder.spawn(|| {
+    ///     assert!(thread::current().name().is_none());
+    /// }).unwrap();
+    ///
+    /// handler.join().unwrap();
+    /// ```
+    ///
+    /// Thread with a specified name:
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let builder = thread::Builder::new()
+    ///     .name("foo".into());
+    ///
+    /// let handler = builder.spawn(|| {
+    ///     assert_eq!(thread::current().name(), Some("foo"))
+    /// }).unwrap();
+    ///
+    /// handler.join().unwrap();
+    /// ```
+    ///
+    /// [naming-threads]: ./index.html#naming-threads
     pub fn name(&self) -> Option<&str> {
         self.cname().map(|s| unsafe { str::from_utf8_unchecked(s.to_bytes()) })
     }
 
     fn cname(&self) -> Option<&CStr> {
-        self.inner.name.as_ref().map(|s| &**s)
+        self.inner.name.as_deref()
     }
 }
 
 impl fmt::Debug for SgxThread {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.debug_struct("SgxThread").field("id", &self.id()).field("name", &self.name()).finish()
+        f.debug_struct("SgxThread")
+            .field("id", &self.id())
+            .field("name", &self.name())
+            .finish_non_exhaustive()
     }
 }
 
@@ -739,19 +1012,43 @@
 /// the [`Error`](crate::error::Error) trait.
 ///
 /// Thus, a sensible way to handle a thread panic is to either:
-/// 1. `unwrap` the `Result<T>`, propagating the panic
+///
+/// 1. propagate the panic with [`std::panic::resume_unwind`]
 /// 2. or in case the thread is intended to be a subsystem boundary
 /// that is supposed to isolate system-level failures,
-/// match on the `Err` variant and handle the panic in an appropriate way.
+/// match on the `Err` variant and handle the panic in an appropriate way
 ///
 /// A thread that completes without panicking is considered to exit successfully.
 ///
+/// # Examples
+///
+/// Matching on the result of a joined thread:
+///
+/// ```no_run
+/// use std::{fs, thread, panic};
+///
+/// fn copy_in_thread() -> thread::Result<()> {
+///     thread::spawn(|| {
+///         fs::copy("foo.txt", "bar.txt").unwrap();
+///     }).join()
+/// }
+///
+/// fn main() {
+///     match copy_in_thread() {
+///         Ok(_) => println!("copy succeeded"),
+///         Err(e) => panic::resume_unwind(e),
+///     }
+/// }
+/// ```
+///
+/// [`Result`]: crate::result::Result
+/// [`std::panic::resume_unwind`]: crate::panic::resume_unwind
 pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>;
 
-// This packet is used to communicate the return value between the child thread
-// and the parent thread. Memory is shared through the `Arc` within and there's
+// This packet is used to communicate the return value between the spawned thread
+// and the rest of the program. Memory is shared through the `Arc` within and there's
 // no need for a mutex here because synchronization happens with `join()` (the
-// parent thread never reads this packet until the child has exited).
+// caller will never read this packet until the thread has exited).
 //
 // This packet itself is then stored into a `JoinInner` which in turns is placed
 // in `JoinHandle` and `JoinGuard`. Due to the usage of `UnsafeCell` we need to
@@ -820,7 +1117,7 @@
 /// }).unwrap();
 /// ```
 ///
-/// Child being detached and outliving its parent:
+/// A thread being detached and outliving the thread that spawned it:
 ///
 /// ```no_run
 /// use std::thread;
@@ -844,16 +1141,16 @@
 /// thread::sleep(Duration::from_millis(1000));
 /// ```
 ///
-/// [`Clone`]: ../../std/clone/trait.Clone.html
-/// [`thread::spawn`]: fn.spawn.html
-/// [`thread::Builder::spawn`]: struct.Builder.html#method.spawn
-
+/// [`thread::Builder::spawn`]: Builder::spawn
+/// [`thread::spawn`]: spawn
 #[cfg(feature = "thread")]
 pub struct JoinHandle<T>(JoinInner<T>);
+
 #[cfg(feature = "thread")]
 unsafe impl<T> Send for JoinHandle<T> {}
 #[cfg(feature = "thread")]
 unsafe impl<T> Sync for JoinHandle<T> {}
+
 #[cfg(feature = "thread")]
 impl<T> JoinHandle<T> {
     /// Extracts a handle to the underlying thread.
@@ -878,17 +1175,19 @@
 
     /// Waits for the associated thread to finish.
     ///
+    /// This function will return immediately if the associated thread has already finished.
+    ///
     /// In terms of [atomic memory orderings],  the completion of the associated
     /// thread synchronizes with this function returning. In other words, all
-    /// operations performed by that thread are ordered before all
+    /// operations performed by that thread [happen
+    /// before](https://doc.rust-lang.org/nomicon/atomics.html#data-accesses) all
     /// operations that happen after `join` returns.
     ///
-    /// If the child thread panics, [`Err`] is returned with the parameter given
-    /// to [`panic`].
+    /// If the associated thread panics, [`Err`] is returned with the parameter given
+    /// to [`panic!`].
     ///
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`panic`]: ../../std/macro.panic.html
-    /// [atomic memory orderings]: ../../std/sync/atomic/index.html
+    /// [`Err`]: crate::result::Result::Err
+    /// [atomic memory orderings]: crate::sync::atomic
     ///
     /// # Panics
     ///
@@ -929,7 +1228,7 @@
 #[cfg(feature = "thread")]
 impl<T> fmt::Debug for JoinHandle<T> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        f.pad("JoinHandle { .. }")
+        f.debug_struct("JoinHandle").finish_non_exhaustive()
     }
 }
 
@@ -938,4 +1237,93 @@
     fn _assert_both<T: Send + Sync>() {}
     _assert_both::<JoinHandle<()>>();
     _assert_both::<SgxThread>();
-}
\ No newline at end of file
+}
+
+/// Returns the number of hardware threads available to the program.
+///
+/// This value should be considered only a hint.
+///
+/// # Platform-specific behavior
+///
+/// If interpreted as the number of actual hardware threads, it may undercount on
+/// Windows systems with more than 64 hardware threads. If interpreted as the
+/// available concurrency for that process, it may overcount on Windows systems
+/// when limited by a process wide affinity mask or job object limitations, and
+/// it may overcount on Linux systems when limited by a process wide affinity
+/// mask or affected by cgroups limits.
+///
+/// # Errors
+///
+/// This function will return an error in the following situations, but is not
+/// limited to just these cases:
+///
+/// - If the number of hardware threads is not known for the target platform.
+/// - The process lacks permissions to view the number of hardware threads
+///   available.
+///
+/// # Examples
+///
+/// ```
+/// # #![allow(dead_code)]
+/// #![feature(available_concurrency)]
+/// use std::thread;
+///
+/// let count = thread::available_concurrency().map(|n| n.get()).unwrap_or(1);
+/// ```
+#[cfg(feature = "thread")]
+pub fn available_concurrency() -> io::Result<NonZeroUsize> {
+    imp::available_concurrency()
+}
+
+///
+/// The rsgx_thread_self function returns the unique thread identification.
+///
+/// # Description
+///
+/// The function is a simple wrap of get_thread_data() provided in the tRTS,
+/// which provides a trusted thread unique identifier.
+///
+/// # Requirements
+///
+/// Library: libsgx_tstdc.a
+///
+/// # Return value
+///
+/// The return value cannot be NULL and is always valid as long as it is invoked by a thread inside the enclave.
+///
+pub fn rsgx_thread_self() -> sgx_thread_t {
+    unsafe { sgx_thread_self() }
+}
+
+///
+/// The rsgx_thread_equal function compares two thread identifiers.
+///
+/// # Description
+///
+/// The function compares two thread identifiers provided by sgx_thread_
+/// self to determine if the IDs refer to the same trusted thread.
+///
+/// # Requirements
+///
+/// Library: libsgx_tstdc.a
+///
+/// # Return value
+///
+/// **true**
+///
+/// The two thread IDs are equal.
+///
+pub fn rsgx_thread_equal(a: sgx_thread_t, b: sgx_thread_t) -> bool {
+    a == b
+}
+
+pub fn current_td() -> SgxThreadData {
+    unsafe {
+        SgxThreadData::from_raw(rsgx_thread_self())
+    }
+}
+
+#[inline]
+pub fn thread_policy() -> SgxThreadPolicy {
+    rsgx_get_thread_policy()
+}
diff --git a/sgx_tstd/src/time.rs b/sgx_tstd/src/time.rs
index 75d4fec..eaac371 100644
--- a/sgx_tstd/src/time.rs
+++ b/sgx_tstd/src/time.rs
@@ -16,26 +16,57 @@
 // under the License..
 
 //! Temporal quantification.
+//!
+//! # Examples:
+//!
+//! There are multiple ways to create a new [`Duration`]:
+//!
+//! ```
+//! # use std::time::Duration;
+//! let five_seconds = Duration::from_secs(5);
+//! assert_eq!(five_seconds, Duration::from_millis(5_000));
+//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
+//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
+//!
+//! let ten_seconds = Duration::from_secs(10);
+//! let seven_nanos = Duration::from_nanos(7);
+//! let total = ten_seconds + seven_nanos;
+//! assert_eq!(total, Duration::new(10, 7));
+//! ```
+//!
+//! Using [`Instant`] to calculate how long a function took to run:
+//!
+//! ```ignore (incomplete)
+//! let now = Instant::now();
+//!
+//! // Calling a slow function, it may take a while
+//! slow_function();
+//!
+//! let elapsed_time = now.elapsed();
+//! println!("Running slow_function() took {} seconds.", elapsed_time.as_secs());
+//! ```
 
-use core::cmp;
-use core::fmt;
-use core::ops::{Add, AddAssign, Sub, SubAssign};
+#![allow(clippy::needless_doctest_main)]
+
+mod monotonic;
+
 use crate::error::Error;
+use crate::fmt;
+use crate::ops::{Add, AddAssign, Sub, SubAssign};
 use crate::sys::time;
 use crate::sys_common::FromInner;
-use crate::sync::SgxThreadMutex;
 
 pub use core::time::Duration;
 
 /// A measurement of a monotonically nondecreasing clock.
-/// Opaque and useful only with `Duration`.
+/// Opaque and useful only with [`Duration`].
 ///
 /// Instants are always guaranteed to be no less than any previously measured
 /// instant when created, and are often useful for tasks such as measuring
 /// benchmarks or timing how long an operation takes.
 ///
 /// Note, however, that instants are not guaranteed to be **steady**. In other
-/// words, each tick of the underlying clock may not be the same length (e.g.
+/// words, each tick of the underlying clock might not be the same length (e.g.
 /// some seconds may be longer than others). An instant may jump forwards or
 /// experience time dilation (slow down or speed up), but it will never go
 /// backwards.
@@ -48,6 +79,62 @@
 /// The size of an `Instant` struct may vary depending on the target operating
 /// system.
 ///
+/// Example:
+///
+/// ```no_run
+/// use std::time::{Duration, Instant};
+/// use std::thread::sleep;
+///
+/// fn main() {
+///    let now = Instant::now();
+///
+///    // we sleep for 2 seconds
+///    sleep(Duration::new(2, 0));
+///    // it prints '2'
+///    println!("{}", now.elapsed().as_secs());
+/// }
+/// ```
+///
+/// # OS-specific behaviors
+///
+/// An `Instant` is a wrapper around system-specific types and it may behave
+/// differently depending on the underlying operating system. For example,
+/// the following snippet is fine on Linux but panics on macOS:
+///
+/// ```no_run
+/// use std::time::{Instant, Duration};
+///
+/// let now = Instant::now();
+/// let max_nanoseconds = u64::MAX / 1_000_000_000;
+/// let duration = Duration::new(max_nanoseconds, 0);
+/// println!("{:?}", now + duration);
+/// ```
+///
+/// # Underlying System calls
+/// Currently, the following system calls are being used to get the current time using `now()`:
+///
+/// |  Platform |               System call                                            |
+/// |-----------|----------------------------------------------------------------------|
+/// | SGX       | [`insecure_time` usercall]. More information on [timekeeping in SGX] |
+/// | UNIX      | [clock_gettime (Monotonic Clock)]                                    |
+/// | Darwin    | [mach_absolute_time]                                                 |
+/// | VXWorks   | [clock_gettime (Monotonic Clock)]                                    |
+/// | WASI      | [__wasi_clock_time_get (Monotonic Clock)]                            |
+/// | Windows   | [QueryPerformanceCounter]                                            |
+///
+/// [QueryPerformanceCounter]: https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
+/// [`insecure_time` usercall]: https://edp.fortanix.com/docs/api/fortanix_sgx_abi/struct.Usercalls.html#method.insecure_time
+/// [timekeeping in SGX]: https://edp.fortanix.com/docs/concepts/rust-std/#codestdtimecode
+/// [__wasi_clock_time_get (Monotonic Clock)]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md#clock_time_get
+/// [clock_gettime (Monotonic Clock)]: https://linux.die.net/man/3/clock_gettime
+/// [mach_absolute_time]: https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/services/services.html
+///
+/// **Disclaimer:** These system calls might change over time.
+///
+/// > Note: mathematical operations like [`add`] may panic if the underlying
+/// > structure cannot represent the new point in time.
+///
+/// [`add`]: Instant::add
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
 pub struct Instant(time::Instant);
 
@@ -74,6 +161,57 @@
 /// The size of a `SystemTime` struct may vary depending on the target operating
 /// system.
 ///
+/// Example:
+///
+/// ```no_run
+/// use std::time::{Duration, SystemTime};
+/// use std::thread::sleep;
+/// use std::untrusted::time::SystemTimeEx;
+///
+/// fn main() {
+///    let now = SystemTime::now();
+///
+///    // we sleep for 2 seconds
+///    sleep(Duration::new(2, 0));
+///    match now.elapsed() {
+///        Ok(elapsed) => {
+///            // it prints '2'
+///            println!("{}", elapsed.as_secs());
+///        }
+///        Err(e) => {
+///            // an error occurred!
+///            println!("Error: {:?}", e);
+///        }
+///    }
+/// }
+/// ```
+///
+/// # Underlying System calls
+/// Currently, the following system calls are being used to get the current time using `now()`:
+///
+/// |  Platform |               System call                                            |
+/// |-----------|----------------------------------------------------------------------|
+/// | SGX       | [`insecure_time` usercall]. More information on [timekeeping in SGX] |
+/// | UNIX      | [clock_gettime (Realtime Clock)]                                     |
+/// | Darwin    | [gettimeofday]                                                       |
+/// | VXWorks   | [clock_gettime (Realtime Clock)]                                     |
+/// | WASI      | [__wasi_clock_time_get (Realtime Clock)]                             |
+/// | Windows   | [GetSystemTimePreciseAsFileTime] / [GetSystemTimeAsFileTime]         |
+///
+/// [`insecure_time` usercall]: https://edp.fortanix.com/docs/api/fortanix_sgx_abi/struct.Usercalls.html#method.insecure_time
+/// [timekeeping in SGX]: https://edp.fortanix.com/docs/concepts/rust-std/#codestdtimecode
+/// [gettimeofday]: https://man7.org/linux/man-pages/man2/gettimeofday.2.html
+/// [clock_gettime (Realtime Clock)]: https://linux.die.net/man/3/clock_gettime
+/// [__wasi_clock_time_get (Realtime Clock)]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md#clock_time_get
+/// [GetSystemTimePreciseAsFileTime]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
+/// [GetSystemTimeAsFileTime]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime
+///
+/// **Disclaimer:** These system calls might change over time.
+///
+/// > Note: mathematical operations like [`add`] may panic if the underlying
+/// > structure cannot represent the new point in time.
+///
+/// [`add`]: SystemTime::add
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
 pub struct SystemTime(time::SystemTime);
 
@@ -81,12 +219,35 @@
 /// `SystemTime`, used to learn how far in the opposite direction a system time
 /// lies.
 ///
+/// # Examples
+///
+/// ```no_run
+/// use std::thread::sleep;
+/// use std::time::{Duration, SystemTime};
+/// use std::untrusted::time::SystemTimeEx;
+///
+/// let sys_time = SystemTime::now();
+/// sleep(Duration::from_secs(1));
+/// let new_sys_time = SystemTime::now();
+/// match sys_time.duration_since(new_sys_time) {
+///     Ok(_) => {}
+///     Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
+/// }
+/// ```
 #[derive(Clone, Debug)]
 pub struct SystemTimeError(Duration);
 
 impl Instant {
     /// Returns an instant corresponding to "now".
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::time::Instant;
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let now = Instant::now();
+    /// ```
     #[cfg(feature = "untrusted_time")]
     pub fn now() -> Instant {
         Instant::_now()
@@ -119,25 +280,12 @@
         // returned instead of what the OS says if the OS goes backwards.
         //
         // To hopefully mitigate the impact of this, a few platforms are
-        // whitelisted as "these at least haven't gone backwards yet".
+        // excluded as "these at least haven't gone backwards yet".
         if time::Instant::actually_monotonic() {
             return Instant(os_now);
         }
 
-        static LOCK: SgxThreadMutex = SgxThreadMutex::new();
-        static mut LAST_NOW: time::Instant = time::Instant::zero();
-        unsafe {
-            let r = LOCK.lock();
-            let now = if r.is_ok() {
-                let now = cmp::max(LAST_NOW, os_now);
-                LAST_NOW = now;
-                LOCK.unlock();
-                now
-            } else {
-                os_now
-            };
-            Instant(now)
-        }
+        Instant(monotonic::monotonize(os_now))
     }
 
     /// Returns the amount of time elapsed from another instant to this one.
@@ -146,6 +294,18 @@
     ///
     /// This function will panic if `earlier` is later than `self`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::time::{Duration, Instant};
+    /// use std::thread::sleep;
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let now = Instant::now();
+    /// sleep(Duration::new(1, 0));
+    /// let new_now = Instant::now();
+    /// println!("{:?}", new_now.duration_since(now));
+    /// ```
     pub fn duration_since(&self, earlier: Instant) -> Duration {
         self.0.checked_sub_instant(&earlier.0).expect("supplied instant is later than self")
     }
@@ -153,6 +313,19 @@
     /// Returns the amount of time elapsed from another instant to this one,
     /// or None if that instant is later than this one.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::time::{Duration, Instant};
+    /// use std::thread::sleep;
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let now = Instant::now();
+    /// sleep(Duration::new(1, 0));
+    /// let new_now = Instant::now();
+    /// println!("{:?}", new_now.checked_duration_since(now));
+    /// println!("{:?}", now.checked_duration_since(new_now)); // None
+    /// ```
     pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
         self.0.checked_sub_instant(&earlier.0)
     }
@@ -160,8 +333,21 @@
     /// Returns the amount of time elapsed from another instant to this one,
     /// or zero duration if that instant is later than this one.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::time::{Duration, Instant};
+    /// use std::thread::sleep;
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let now = Instant::now();
+    /// sleep(Duration::new(1, 0));
+    /// let new_now = Instant::now();
+    /// println!("{:?}", new_now.saturating_duration_since(now));
+    /// println!("{:?}", now.saturating_duration_since(new_now)); // 0ns
+    /// ```
     pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
-        self.checked_duration_since(earlier).unwrap_or(Duration::new(0, 0))
+        self.checked_duration_since(earlier).unwrap_or_default()
     }
 
     /// Returns the amount of time elapsed since this instant was created.
@@ -172,6 +358,18 @@
     /// instant, which is something that can happen if an `Instant` is
     /// produced synthetically.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::thread::sleep;
+    /// use std::time::{Duration, Instant};
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let instant = Instant::now();
+    /// let three_secs = Duration::from_secs(3);
+    /// sleep(three_secs);
+    /// assert!(instant.elapsed() >= three_secs);
+    /// ```
     #[cfg(feature = "untrusted_time")]
     pub fn elapsed(&self) -> Duration {
         Instant::now() - *self
@@ -204,9 +402,7 @@
     /// # Panics
     ///
     /// This function may panic if the resulting point in time cannot be represented by the
-    /// underlying data structure. See [`checked_add`] for a version without panic.
-    ///
-    /// [`checked_add`]: ../../std/time/struct.Instant.html#method.checked_add
+    /// underlying data structure. See [`Instant::checked_add`] for a version without panic.
     fn add(self, other: Duration) -> Instant {
         self.checked_add(other).expect("overflow when adding duration to instant")
     }
@@ -256,10 +452,28 @@
     /// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
     /// `SystemTime` instance to represent another fixed point in time.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::time::SystemTime;
+    ///
+    /// match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
+    ///     Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
+    ///     Err(_) => panic!("SystemTime before UNIX EPOCH!"),
+    /// }
+    /// ```
     pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH;
 
     /// Returns the system time corresponding to "now".
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::time::SystemTime;
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// ```
     #[cfg(feature = "untrusted_time")]
     pub fn now() -> SystemTime {
         SystemTime::_now()
@@ -282,11 +496,18 @@
     /// Returns an [`Err`] if `earlier` is later than `self`, and the error
     /// contains how far from `self` the time is.
     ///
-    /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
-    /// [`Duration`]: ../../std/time/struct.Duration.html
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`Instant`]: ../../std/time/struct.Instant.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::time::SystemTime;
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// let new_sys_time = SystemTime::now();
+    /// let difference = new_sys_time.duration_since(sys_time)
+    ///     .expect("Clock may have gone backwards");
+    /// println!("{:?}", difference);
+    /// ```
     pub fn duration_since(&self, earlier: SystemTime) -> Result<Duration, SystemTimeError> {
         self.0.sub_time(&earlier.0).map_err(SystemTimeError)
     }
@@ -296,7 +517,7 @@
     ///
     /// This function may fail as the underlying system clock is susceptible to
     /// drift and updates (e.g., the system clock could go backwards), so this
-    /// function may not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
+    /// function might not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
     /// returned where the duration represents the amount of time elapsed from
     /// this time measurement to the current time.
     ///
@@ -305,11 +526,18 @@
     /// Returns an [`Err`] if `self` is later than the current system time, and
     /// the error contains how far from the current system time `self` is.
     ///
-    /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
-    /// [`Duration`]: ../../std/time/struct.Duration.html
-    /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
-    /// [`Instant`]: ../../std/time/struct.Instant.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::thread::sleep;
+    /// use std::time::{Duration, SystemTime};
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// let one_sec = Duration::from_secs(1);
+    /// sleep(one_sec);
+    /// assert!(sys_time.elapsed().unwrap() >= one_sec);
+    /// ```
     #[cfg(feature = "untrusted_time")]
     pub fn elapsed(&self) -> Result<Duration, SystemTimeError> {
         SystemTime::now().duration_since(*self)
@@ -342,9 +570,7 @@
     /// # Panics
     ///
     /// This function may panic if the resulting point in time cannot be represented by the
-    /// underlying data structure. See [`checked_add`] for a version without panic.
-    ///
-    /// [`checked_add`]: ../../std/time/struct.SystemTime.html#method.checked_add
+    /// underlying data structure. See [`SystemTime::checked_add`] for a version without panic.
     fn add(self, dur: Duration) -> SystemTime {
         self.checked_add(dur).expect("overflow when adding duration to instant")
     }
@@ -385,28 +611,49 @@
 /// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
 /// [`SystemTime`] instance to represent another fixed point in time.
 ///
-/// [`SystemTime`]: ../../std/time/struct.SystemTime.html
+/// # Examples
 ///
+/// ```no_run
+/// use std::time::{SystemTime, UNIX_EPOCH};
+/// use std::untrusted::time::SystemTimeEx;
+///
+/// match SystemTime::now().duration_since(UNIX_EPOCH) {
+///     Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
+///     Err(_) => panic!("SystemTime before UNIX EPOCH!"),
+/// }
+/// ```
 pub const UNIX_EPOCH: SystemTime = SystemTime(time::UNIX_EPOCH);
 
 impl SystemTimeError {
     /// Returns the positive duration which represents how far forward the
     /// second system time was from the first.
     ///
-    /// A `SystemTimeError` is returned from the [`duration_since`] and [`elapsed`]
-    /// methods of [`SystemTime`] whenever the second system time represents a point later
-    /// in time than the `self` of the method call.
+    /// A `SystemTimeError` is returned from the [`SystemTime::duration_since`]
+    /// and [`SystemTime::elapsed`] methods whenever the second system time
+    /// represents a point later in time than the `self` of the method call.
     ///
-    /// [`duration_since`]: ../../std/time/struct.SystemTime.html#method.duration_since
-    /// [`elapsed`]: ../../std/time/struct.SystemTime.html#method.elapsed
-    /// [`SystemTime`]: ../../std/time/struct.SystemTime.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::thread::sleep;
+    /// use std::time::{Duration, SystemTime};
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// sleep(Duration::from_secs(1));
+    /// let new_sys_time = SystemTime::now();
+    /// match sys_time.duration_since(new_sys_time) {
+    ///     Ok(_) => {}
+    ///     Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
+    /// }
+    /// ```
     pub fn duration(&self) -> Duration {
         self.0
     }
 }
 
 impl Error for SystemTimeError {
+    #[allow(deprecated)]
     fn description(&self) -> &str {
         "other time was not earlier than self"
     }
diff --git a/sgx_tstd/src/time/monotonic.rs b/sgx_tstd/src/time/monotonic.rs
new file mode 100644
index 0000000..8c9bf5e
--- /dev/null
+++ b/sgx_tstd/src/time/monotonic.rs
@@ -0,0 +1,133 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License..
+
+use crate::sys::time;
+
+#[inline]
+pub(super) fn monotonize(raw: time::Instant) -> time::Instant {
+    inner::monotonize(raw)
+}
+
+#[cfg(all(target_has_atomic = "64", not(target_has_atomic = "128")))]
+pub mod inner {
+    use crate::sync::atomic::AtomicU64;
+    use crate::sync::atomic::Ordering::*;
+    use crate::sys::time;
+    use crate::time::Duration;
+
+    pub(in crate::time) const ZERO: time::Instant = time::Instant::zero();
+
+    // bits 30 and 31 are never used since the nanoseconds part never exceeds 10^9
+    const UNINITIALIZED: u64 = 0b11 << 30;
+    static MONO: AtomicU64 = AtomicU64::new(UNINITIALIZED);
+
+    #[inline]
+    pub(super) fn monotonize(raw: time::Instant) -> time::Instant {
+        monotonize_impl(&MONO, raw)
+    }
+
+    #[inline]
+    pub(in crate::time) fn monotonize_impl(mono: &AtomicU64, raw: time::Instant) -> time::Instant {
+        let delta = raw.checked_sub_instant(&ZERO).unwrap();
+        let secs = delta.as_secs();
+        // occupies no more than 30 bits (10^9 seconds)
+        let nanos = delta.subsec_nanos() as u64;
+
+        // This wraps around every 136 years (2^32 seconds).
+        // To detect backsliding we use wrapping arithmetic and declare forward steps smaller
+        // than 2^31 seconds as expected and everything else as a backslide which will be
+        // monotonized.
+        // This could be a problem for programs that call instants at intervals greater
+        // than 68 years. Interstellar probes may want to ensure that actually_monotonic() is true.
+        let packed = (secs << 32) | nanos;
+        let old = mono.load(Relaxed);
+
+        if old == UNINITIALIZED || packed.wrapping_sub(old) < u64::MAX / 2 {
+            mono.store(packed, Relaxed);
+            raw
+        } else {
+            // Backslide occurred. We reconstruct monotonized time from the upper 32 bit of the
+            // passed in value and the 64bits loaded from the atomic
+            let seconds_lower = old >> 32;
+            let mut seconds_upper = secs & 0xffff_ffff_0000_0000;
+            if secs & 0xffff_ffff > seconds_lower {
+                // Backslide caused the lower 32bit of the seconds part to wrap.
+                // This must be the case because the seconds part is larger even though
+                // we are in the backslide branch, i.e. the seconds count should be smaller or equal.
+                //
+                // We assume that backslides are smaller than 2^32 seconds
+                // which means we need to add 1 to the upper half to restore it.
+                //
+                // Example:
+                // most recent observed time: 0xA1_0000_0000_0000_0000u128
+                // bits stored in AtomicU64:     0x0000_0000_0000_0000u64
+                // backslide by 1s
+                // caller time is             0xA0_ffff_ffff_0000_0000u128
+                // -> we can fix up the upper half time by adding 1 << 32
+                seconds_upper = seconds_upper.wrapping_add(0x1_0000_0000);
+            }
+            let secs = seconds_upper | seconds_lower;
+            let nanos = old as u32;
+            ZERO.checked_add_duration(&Duration::new(secs, nanos)).unwrap()
+        }
+    }
+}
+
+#[cfg(target_has_atomic = "128")]
+pub mod inner {
+    use crate::sync::atomic::AtomicU128;
+    use crate::sync::atomic::Ordering::*;
+    use crate::sys::time;
+    use crate::time::Duration;
+
+    const ZERO: time::Instant = time::Instant::zero();
+    static MONO: AtomicU128 = AtomicU128::new(0);
+
+    #[inline]
+    pub(super) fn monotonize(raw: time::Instant) -> time::Instant {
+        let delta = raw.checked_sub_instant(&ZERO).unwrap();
+        // Split into seconds and nanos since Duration doesn't have a
+        // constructor that takes a u128
+        let secs = delta.as_secs() as u128;
+        let nanos = delta.subsec_nanos() as u128;
+        let timestamp: u128 = secs << 64 | nanos;
+        let timestamp = MONO.fetch_max(timestamp, Relaxed).max(timestamp);
+        let secs = (timestamp >> 64) as u64;
+        let nanos = timestamp as u32;
+        ZERO.checked_add_duration(&Duration::new(secs, nanos)).unwrap()
+    }
+}
+
+#[cfg(not(any(target_has_atomic = "64", target_has_atomic = "128")))]
+pub mod inner {
+    use crate::cmp;
+    use crate::sys::time;
+    use crate::sys_common::mutex::StaticMutex;
+
+    #[inline]
+    pub(super) fn monotonize(os_now: time::Instant) -> time::Instant {
+        static LOCK: SgxThreadMutex = SgxThreadMutex::new();
+        static mut LAST_NOW: time::Instant = time::Instant::zero();
+        unsafe {
+            let _lock = LOCK.lock();
+            let now = cmp::max(LAST_NOW, os_now);
+            LAST_NOW = now;
+            let _ = LOCK.unlock();
+            now
+        }
+    }
+}
diff --git a/sgx_tstd/src/untrusted/path.rs b/sgx_tstd/src/untrusted/path.rs
index 6f9e605..cadb2bd 100644
--- a/sgx_tstd/src/untrusted/path.rs
+++ b/sgx_tstd/src/untrusted/path.rs
@@ -27,8 +27,10 @@
     fn read_link(&self) -> io::Result<PathBuf>;
     fn read_dir(&self) -> io::Result<fs::ReadDir>;
     fn exists(&self) -> bool;
+    fn try_exists(&self) -> io::Result<bool>;
     fn is_file(&self) -> bool;
     fn is_dir(&self) -> bool;
+    fn is_symlink(&self) -> bool;
 }
 
 impl PathEx for Path {
@@ -39,8 +41,16 @@
     ///
     /// This is an alias to [`fs::metadata`].
     ///
-    /// [`fs::metadata`]: ../fs/fn.metadata.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    ///
+    /// let path = Path::new("/Minas/tirith");
+    /// let metadata = path.metadata().expect("metadata call failed");
+    /// println!("{:?}", metadata.file_type());
+    /// ```
     fn metadata(&self) -> io::Result<fs::Metadata> {
         fs::metadata(self)
     }
@@ -49,19 +59,34 @@
     ///
     /// This is an alias to [`fs::symlink_metadata`].
     ///
-    /// [`fs::symlink_metadata`]: ../fs/fn.symlink_metadata.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    ///
+    /// let path = Path::new("/Minas/tirith");
+    /// let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
+    /// println!("{:?}", metadata.file_type());
+    /// ```
     fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
         fs::symlink_metadata(self)
     }
 
-    /// Returns the canonical form of the path with all intermediate components
-    /// normalized and symbolic links resolved.
+    /// Returns the canonical, absolute form of the path with all intermediate
+    /// components normalized and symbolic links resolved.
     ///
     /// This is an alias to [`fs::canonicalize`].
     ///
-    /// [`fs::canonicalize`]: ../fs/fn.canonicalize.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::{Path, PathBuf};
+    /// use std::untrusted::path::PathEx;
+    ///
+    /// let path = Path::new("/foo/test/../test/bar.rs");
+    /// assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
+    /// ```
     fn canonicalize(&self) -> io::Result<PathBuf> {
         fs::canonicalize(self)
     }
@@ -70,48 +95,174 @@
     ///
     /// This is an alias to [`fs::read_link`].
     ///
-    /// [`fs::read_link`]: ../fs/fn.read_link.html
+    /// # Examples
     ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    ///
+    /// let path = Path::new("/laputa/sky_castle.rs");
+    /// let path_link = path.read_link().expect("read_link call failed");
+    /// ```
     fn read_link(&self) -> io::Result<PathBuf> {
         fs::read_link(self)
     }
 
+    /// Returns an iterator over the entries within a directory.
+    ///
+    /// The iterator will yield instances of [`io::Result`]`<`[`fs::DirEntry`]`>`. New
+    /// errors may be encountered after an iterator is initially constructed.
+    ///
+    /// This is an alias to [`fs::read_dir`].
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    ///
+    /// let path = Path::new("/laputa");
+    /// for entry in path.read_dir().expect("read_dir call failed") {
+    ///     if let Ok(entry) = entry {
+    ///         println!("{:?}", entry.path());
+    ///     }
+    /// }
+    /// ```
     fn read_dir(&self) -> io::Result<fs::ReadDir> {
         fs::read_dir(self)
     }
-    /// Returns whether the path points at an existing entity.
+
+    /// Returns `true` if the path points at an existing entity.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file.
     ///
-    /// If you cannot access the directory containing the file, e.g. because of a
-    /// permission error, this will return `false`.
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    /// assert!(!Path::new("does_not_exist.txt").exists());
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`].
     fn exists(&self) -> bool {
         fs::metadata(self).is_ok()
     }
 
-    /// Returns whether the path exists on disk and is pointing at a regular file.
+    /// Returns `Ok(true)` if the path points at an existing entity.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file. In case of broken symbolic links this will return `Ok(false)`.
     ///
-    /// If you cannot access the directory containing the file, e.g. because of a
-    /// permission error, this will return `false`.
+    /// As opposed to the `exists()` method, this one doesn't silently ignore errors
+    /// unrelated to the path not existing. (E.g. it will return `Err(_)` in case of permission
+    /// denied on some of the parent directories.)
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// #![feature(path_try_exists)]
+    ///
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    /// assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
+    /// assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
+    /// ```
+    // FIXME: stabilization should modify documentation of `exists()` to recommend this method
+    // instead.
+    #[inline]
+    fn try_exists(&self) -> io::Result<bool> {
+        fs::try_exists(self)
+    }
+
+    /// Returns `true` if the path exists on disk and is pointing at a regular file.
+    ///
+    /// This function will traverse symbolic links to query information about the
+    /// destination file.
+    ///
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
+    ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
+    /// assert_eq!(Path::new("a_file.txt").is_file(), true);
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
+    /// [`fs::Metadata::is_file`] if it was [`Ok`].
+    ///
+    /// When the goal is simply to read from (or write to) the source, the most
+    /// reliable way to test the source can be read (or written to) is to open
+    /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
+    /// a Unix-like system for example. See [`fs::File::open`] or
+    /// [`fs::OpenOptions::open`] for more information.
     fn is_file(&self) -> bool {
         fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)
     }
 
-    /// Returns whether the path exists on disk and is pointing at a directory.
+    /// Returns `true` if the path exists on disk and is pointing at a directory.
     ///
     /// This function will traverse symbolic links to query information about the
-    /// destination file. In case of broken symbolic links this will return `false`.
+    /// destination file.
     ///
-    /// If you cannot access the directory containing the file, e.g. because of a
-    /// permission error, this will return `false`.
+    /// If you cannot access the metadata of the file, e.g. because of a
+    /// permission error or broken symbolic links, this will return `false`.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
+    /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
+    /// ```
+    ///
+    /// # See Also
+    ///
+    /// This is a convenience function that coerces errors to false. If you want to
+    /// check errors, call [`fs::metadata`] and handle its [`Result`]. Then call
+    /// [`fs::Metadata::is_dir`] if it was [`Ok`].
     fn is_dir(&self) -> bool {
         fs::metadata(self).map(|m| m.is_dir()).unwrap_or(false)
     }
+
+    /// Returns true if the path exists on disk and is pointing at a symbolic link.
+    ///
+    /// This function will not traverse symbolic links.
+    /// In case of a broken symbolic link this will also return true.
+    ///
+    /// If you cannot access the directory containing the file, e.g., because of a
+    /// permission error, this will return false.
+    ///
+    /// # Examples
+    ///
+    #[cfg_attr(unix, doc = "```no_run")]
+    #[cfg_attr(not(unix), doc = "```ignore")]
+    /// #![feature(is_symlink)]
+    /// use std::path::Path;
+    /// use std::untrusted::path::PathEx;
+    /// use std::os::unix::fs::symlink;
+    ///
+    /// let link_path = Path::new("link");
+    /// symlink("/origin_does_not_exists/", link_path).unwrap();
+    /// assert_eq!(link_path.is_symlink(), true);
+    /// assert_eq!(link_path.exists(), false);
+    /// ```
+    fn is_symlink(&self) -> bool {
+        fs::symlink_metadata(self).map(|m| m.is_symlink()).unwrap_or(false)
+    }
 }
diff --git a/sgx_tstd/src/untrusted/time.rs b/sgx_tstd/src/untrusted/time.rs
index 9893a93..8359644 100644
--- a/sgx_tstd/src/untrusted/time.rs
+++ b/sgx_tstd/src/untrusted/time.rs
@@ -25,6 +25,14 @@
 impl InstantEx for Instant {
     /// Returns an instant corresponding to "now".
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::time::Instant;
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let now = Instant::now();
+    /// ```
     fn now() -> Instant {
         Instant::_now()
     }
@@ -37,6 +45,18 @@
     /// instant, which is something that can happen if an `Instant` is
     /// produced synthetically.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::thread::sleep;
+    /// use std::time::{Duration, Instant};
+    /// use std::untrusted::time::InstantEx;
+    ///
+    /// let instant = Instant::now();
+    /// let three_secs = Duration::from_secs(3);
+    /// sleep(three_secs);
+    /// assert!(instant.elapsed() >= three_secs);
+    /// ```
     fn elapsed(&self) -> Duration {
         Instant::_now() - *self
     }
@@ -50,21 +70,44 @@
 impl SystemTimeEx for SystemTime {
     /// Returns the system time corresponding to "now".
     ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::time::SystemTime;
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// ```
     fn now() -> SystemTime {
         SystemTime::_now()
     }
 
-    /// Returns the amount of time elapsed since this system time was created.
+    /// Returns the difference between the clock time when this
+    /// system time was created, and the current clock time.
     ///
     /// This function may fail as the underlying system clock is susceptible to
-    /// drift and updates (e.g. the system clock could go backwards), so this
+    /// drift and updates (e.g., the system clock could go backwards), so this
     /// function may not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
     /// returned where the duration represents the amount of time elapsed from
     /// this time measurement to the current time.
     ///
+    /// To measure elapsed time reliably, use [`Instant`] instead.
+    ///
     /// Returns an [`Err`] if `self` is later than the current system time, and
     /// the error contains how far from the current system time `self` is.
     ///
+    /// # Examples
+    ///
+    /// ```no_run
+    /// use std::thread::sleep;
+    /// use std::time::{Duration, SystemTime};
+    /// use std::untrusted::time::SystemTimeEx;
+    ///
+    /// let sys_time = SystemTime::now();
+    /// let one_sec = Duration::from_secs(1);
+    /// sleep(one_sec);
+    /// assert!(sys_time.elapsed().unwrap() >= one_sec);
+    /// ```
     fn elapsed(&self) -> Result<Duration, SystemTimeError> {
         SystemTime::_now().duration_since(*self)
     }
diff --git a/sgx_tunittest/Cargo.toml b/sgx_tunittest/Cargo.toml
index a3d1f2b..b218bd0 100644
--- a/sgx_tunittest/Cargo.toml
+++ b/sgx_tunittest/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_tunittest"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_tunittest/src/lib.rs b/sgx_tunittest/src/lib.rs
index 103bc2c..cca050f 100644
--- a/sgx_tunittest/src/lib.rs
+++ b/sgx_tunittest/src/lib.rs
@@ -73,7 +73,6 @@
     all(target_env = "sgx", target_vendor = "mesalock"),
     feature(rustc_private)
 )]
-#![feature(const_fn)]
 
 #[cfg(not(target_env = "sgx"))]
 #[macro_use]
@@ -150,8 +149,8 @@
     ) => {
         {
             rsgx_unit_test_start();
-            let mut ntestcases : u64 = 0u64;
-            let mut failurecases : Vec<String> = Vec::new();
+            let mut ntestcases: u64 = 0u64;
+            let mut failurecases: ::std::vec::Vec<String> = Vec::new();
             $(rsgx_unit_test(&mut ntestcases, &mut failurecases, $f,stringify!($f));)*
             rsgx_unit_test_end(ntestcases, failurecases)
         }
@@ -176,7 +175,7 @@
     let ntotal = ntestcases as usize;
     let nsucc = ntestcases as usize - failurecases.len();
 
-    if failurecases.len() != 0 {
+    if !failurecases.is_empty() {
         print!("\nfailures: ");
         println!(
             "    {}",
@@ -208,11 +207,12 @@
 /// and on test fails, it records the failed test.
 /// Required test function must be `Fn()`, taking nothing as input and returns
 /// nothing.
+#[allow(clippy::print_literal)]
 pub fn rsgx_unit_test<F, R>(ncases: &mut u64, failurecases: &mut Vec<String>, f: F, name: &str)
 where
     F: FnOnce() -> R + std::panic::UnwindSafe,
 {
-    *ncases = *ncases + 1;
+    *ncases += 1;
     match std::panic::catch_unwind(|| {
         f();
     })
diff --git a/sgx_types/Cargo.toml b/sgx_types/Cargo.toml
index 41b6dd5..04dd878 100644
--- a/sgx_types/Cargo.toml
+++ b/sgx_types/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 authors = ["The Teaclave Authors"]
 name = "sgx_types"
-version = "1.1.3"
+version = "1.1.4"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_ucrypto/Cargo.toml b/sgx_ucrypto/Cargo.toml
index 7517958..99d1547 100644
--- a/sgx_ucrypto/Cargo.toml
+++ b/sgx_ucrypto/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_ucrypto"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
diff --git a/sgx_unwind/Cargo.toml b/sgx_unwind/Cargo.toml
index 1a5ed89..feae347 100644
--- a/sgx_unwind/Cargo.toml
+++ b/sgx_unwind/Cargo.toml
@@ -1,17 +1,18 @@
 [package]
 name = "sgx_unwind"
-version = "0.1.1"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 build = "build.rs"
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 exclude = [
     "libunwind/autom4te.cache/*",
     "libunwind/aclocal.m4",
     "libunwind/config/*",
+    "libunwind/m4/*",
     "libunwind/configure",
     "libunwind/INSTALL",
     "libunwind/Makefile.in",
@@ -21,7 +22,6 @@
 
 [lib]
 name = "sgx_unwind"
-path = "lib.rs"
 test = false
 bench = false
 doc = false
diff --git a/sgx_unwind/build.rs b/sgx_unwind/build.rs
index 1f3f228..d76899d 100644
--- a/sgx_unwind/build.rs
+++ b/sgx_unwind/build.rs
@@ -17,9 +17,9 @@
 
 extern crate sgx_build_helper as build_helper;
 
+use build_helper::{native_lib_boilerplate, run};
 use std::env;
 use std::process::Command;
-use build_helper::{run, native_lib_boilerplate};
 
 fn main() {
     println!("cargo:rerun-if-changed=build.rs");
@@ -31,6 +31,7 @@
 
 fn build_libunwind(host: &str, target: &str) -> Result<(), ()> {
     let filter = vec![
+        "m4",
         "config",
         "autom4te.cache",
         "Makefile.in",
@@ -38,13 +39,15 @@
         "config.h.in~",
         "configure",
         "aclocal.m4",
-        "INSTALL"];
+        "INSTALL",
+    ];
     let native = native_lib_boilerplate(
-                    "sgx_unwind/libunwind",
-                    "libunwind",
-                    "unwind",
-                    "src/.libs",
-                    &filter)?;
+        "sgx_unwind/libunwind",
+        "libunwind",
+        "unwind",
+        "src/.libs",
+        &filter,
+    )?;
 
     let mut cflags = String::new();
     cflags += " -fstack-protector -ffreestanding -nostdinc -fvisibility=hidden -fpie -fno-strict-overflow -fno-delete-null-pointer-checks";
@@ -52,33 +55,37 @@
 
     let mitigation_cflags = " -mindirect-branch-register -mfunction-return=thunk-extern";
     let mitigation_asflags = " -fno-plt";
-    let mitigation_loadflags = " -Wa,-mlfence-after-load=yes -Wa,-mlfence-before-ret=not";
-    let mitigation_cfflags = " -Wa,-mlfence-before-indirect-branch=register -Wa,-mlfence-before-ret=not";
+    let mitigation_loadflags =
+        " -Wa,-mlfence-after-load=yes -Wa,-mlfence-before-indirect-branch=memory";
+    let mitigation_cfflags = " -Wa,-mlfence-before-indirect-branch=all";
+    let mitigation_retflags = " -Wa,-mlfence-before-ret=shl";
     let mitigation = env::var("MITIGATION_CVE_2020_0551").unwrap_or_default();
     match mitigation.as_ref() {
         "LOAD" => {
             cflags += mitigation_cflags;
             cflags += mitigation_asflags;
             cflags += mitigation_loadflags;
-        },
+            cflags += mitigation_retflags;
+        }
         "CF" => {
             cflags += mitigation_cflags;
             cflags += mitigation_asflags;
             cflags += mitigation_cfflags;
-        },
-        _  => {},
+            cflags += mitigation_retflags;
+        }
+        _ => {}
     }
 
     run(Command::new("sh")
-                .current_dir(&native.out_dir)
-                .arg(native.src_dir.join("autogen-linux.sh").to_str().unwrap())
-                .arg(format!("--host={}", build_helper::gnu_target(target)))
-                .arg(format!("--build={}", build_helper::gnu_target(host)))
-                .env("CFLAGS", cflags));
+        .current_dir(&native.out_dir)
+        .arg(native.src_dir.join("autogen.sh").to_str().unwrap())
+        .arg(format!("--host={}", build_helper::gnu_target(target)))
+        .arg(format!("--build={}", build_helper::gnu_target(host)))
+        .env("CFLAGS", cflags));
 
     run(Command::new(build_helper::make(host))
-                .current_dir(&native.out_dir)
-                .arg(format!("INCDIR={}", native.src_dir.display()))
-                .arg("-j5"));
+        .current_dir(&native.out_dir)
+        .arg(format!("INCDIR={}", native.src_dir.display()))
+        .arg("-j5"));
     Ok(())
 }
diff --git a/sgx_unwind/libunwind/Makefile.am b/sgx_unwind/libunwind/Makefile.am
index 4896eb8..5ea2e74 100644
--- a/sgx_unwind/libunwind/Makefile.am
+++ b/sgx_unwind/libunwind/Makefile.am
@@ -1,31 +1,46 @@
 include_HEADERS = include/libunwind-dynamic.h
-#    include/libunwind-ptrace.h
-#    include/libunwind-coredump.h
 
-#if ARCH_ARM
-#include_HEADERS += include/libunwind-arm.h
-#endif
-#if ARCH_IA64
-#include_HEADERS += include/libunwind-ia64.h
-#endif
-#if ARCH_HPPA
-#include_HEADERS += include/libunwind-hppa.h
-#endif
-#if ARCH_MIPS
-#include_HEADERS += include/libunwind-mips.h
-#endif
-#if ARCH_X86
-#include_HEADERS += include/libunwind-x86.h
-#endif
+# if BUILD_PTRACE
+# include_HEADERS += include/libunwind-ptrace.h
+# endif BUILD_PTRACE
+
+# if BUILD_COREDUMP
+# include_HEADERS += include/libunwind-coredump.h
+# endif BUILD_COREDUMP
+
+# if ARCH_AARCH64
+# include_HEADERS += include/libunwind-aarch64.h
+# endif
+# if ARCH_ARM
+# include_HEADERS += include/libunwind-arm.h
+# endif
+# if ARCH_IA64
+# include_HEADERS += include/libunwind-ia64.h
+# endif
+# if ARCH_HPPA
+# include_HEADERS += include/libunwind-hppa.h
+# endif
+# if ARCH_MIPS
+# include_HEADERS += include/libunwind-mips.h
+# endif
+# if ARCH_TILEGX
+# include_HEADERS += include/libunwind-tilegx.h
+# endif
+# if ARCH_X86
+# include_HEADERS += include/libunwind-x86.h
+# endif
 if ARCH_X86_64
 include_HEADERS += include/libunwind-x86_64.h
 endif
-#if ARCH_PPC32
-#include_HEADERS += include/libunwind-ppc32.h
-#endif
-#if ARCH_PPC64
-#include_HEADERS += include/libunwind-ppc64.h
-#endif
+# if ARCH_PPC32
+# include_HEADERS += include/libunwind-ppc32.h
+# endif
+# if ARCH_PPC64
+# include_HEADERS += include/libunwind-ppc64.h
+# endif
+# if ARCH_SH
+# include_HEADERS += include/libunwind-sh.h
+# endif
 
 if !REMOTE_ONLY
 include_HEADERS += include/libunwind.h include/unwind.h
@@ -35,8 +50,16 @@
 
 SUBDIRS = src
 
+# if CONFIG_TESTS
+# SUBDIRS += tests
+# endif
+
+# if CONFIG_DOCS
+# SUBDIRS += doc
+# endif
+
 noinst_HEADERS = include/dwarf.h include/dwarf_i.h include/dwarf-eh.h	\
-	include/libunwind_i.h include/mempool.h				\
+	include/compiler.h include/libunwind_i.h include/mempool.h	\
 	include/remote.h						\
 	include/tdep-x86_64/libunwind_i.h				\
 	include/tdep-x86_64/jmpbuf.h include/tdep-x86_64/dwarf-config.h \
diff --git a/sgx_unwind/libunwind/NEWS b/sgx_unwind/libunwind/NEWS
index 2cf7184..ae6cbcf 100644
--- a/sgx_unwind/libunwind/NEWS
+++ b/sgx_unwind/libunwind/NEWS
@@ -1,5 +1,31 @@
 -*-Mode: outline-*-
 
+* News for v1.3:
+
+** Iteration of unwind register states support
+   Doug Moore <dougm@rice.edu>
+** Freebsd/Armv6 support
+   Konstantin Belousov <kib@freebsd.org>
+** Many, many dwarf bugfixes
+** Mips remote unwind support
+** aarch64 ptrace support
+
+* News for v1.2:
+
+** aarch64 port
+** dwarf parsing improvements
+** Fast stacktraces for aarch64 & arm
+** tilegx port
+** powerpc64 port
+
+* News for v1.1:
+
+** coredump unwind support
+** New arch: SuperH
+** Improved support for PowerPC, ARM
+** Lots of cleanups, perf tweaks
+** pkg-config support
+
 * News for v1.0:
 
 ** Fast unwind (rbp, rsp, rip only) on x86_64 with a fallback to 
diff --git a/sgx_unwind/libunwind/README b/sgx_unwind/libunwind/README
index 9f03a18..694f600 100644
--- a/sgx_unwind/libunwind/README
+++ b/sgx_unwind/libunwind/README
@@ -1,25 +1,30 @@
 -*- mode: Outline -*-
 
-This is version 1.0 of the unwind library.  This library supports
+[![Build Status](https://travis-ci.org/libunwind/libunwind.svg?branch=master)](https://travis-ci.org/libunwind/libunwind)
+
+This is version 1.3 of the unwind library.  This library supports
 several architecture/operating-system combinations:
 
  Linux/x86-64:	Works well.
  Linux/x86:	Works well.
  Linux/ARM:	Works well.
- Linux/IA-64:	Fully tested and supported.
+ Linux/IA-64:	Works well.
  Linux/PARISC:	Works well, but C library missing unwind-info.
  HP-UX/IA-64:	Mostly works but known to have some serious limitations.
+ MIPS:          Newly added.
+ Linux/AArch64:	Works well.
  Linux/PPC64:	Newly added.
- FreeBSD/i386:	Newly added.
+ Linux/SuperH:	Newly added.
+ FreeBSD/i386:	Works well.
  FreeBSD/x86-64: Newly added (FreeBSD architecture is known as amd64).
-
+ Linux/Tilegx:  Newly added (64-bit mode only).
 
 * General Build Instructions
 
 In general, this library can be built and installed with the following
 commands:
 
-	$ autoreconf -i # Needed only for building from git. Depends on libtool.
+	$ ./autogen.sh # Needed only for building from git. Depends on libtool.
 	$ ./configure
 	$ make
 	$ make install prefix=PREFIX
@@ -123,26 +128,12 @@
 
 The following tests are expected to fail on x86 Linux:
 
-	Gtest-resume-sig	(fails to get SIGUSR2)
-	Ltest-resume-sig	(likewise)
-	Gtest-dyn1		(no dynamic unwind info support yet)
-	Ltest-dyn1		(no dynamic unwind info support yet)
-	test-setjmp		(longjmp() not implemented yet)
-	run-check-namespace	(no _Ux86_getcontext yet)
 	test-ptrace
 
 ** Expected results on x86-64 Linux
 
 The following tests are expected to fail on x86-64 Linux:
 
-	Gtest-dyn1		(no dynamic unwind info support yet)
-	Ltest-dyn1		(no dynamic unwind info support yet)
-	Gtest-init (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18743)
-	Ltest-init		(likewise)
-	test-async-sig		(crashes due to bad unwind-info?)
-	test-setjmp		(longjmp() not implemented yet)
-	run-check-namespace	(no _Ux86_64_getcontext yet)
-	run-ptrace-mapper	(??? investigate)
 	run-ptrace-misc	(see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18748
 			 and http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18749)
 
@@ -210,3 +201,7 @@
 web-interface at:
 
 	https://savannah.nongnu.org/mail/?group=libunwind
+
+Or interact at the gihub page:
+
+   	https://github.com/libunwind/libunwind
diff --git a/sgx_unwind/libunwind/README.md b/sgx_unwind/libunwind/README.md
new file mode 100644
index 0000000..694f600
--- /dev/null
+++ b/sgx_unwind/libunwind/README.md
@@ -0,0 +1,207 @@
+-*- mode: Outline -*-
+
+[![Build Status](https://travis-ci.org/libunwind/libunwind.svg?branch=master)](https://travis-ci.org/libunwind/libunwind)
+
+This is version 1.3 of the unwind library.  This library supports
+several architecture/operating-system combinations:
+
+ Linux/x86-64:	Works well.
+ Linux/x86:	Works well.
+ Linux/ARM:	Works well.
+ Linux/IA-64:	Works well.
+ Linux/PARISC:	Works well, but C library missing unwind-info.
+ HP-UX/IA-64:	Mostly works but known to have some serious limitations.
+ MIPS:          Newly added.
+ Linux/AArch64:	Works well.
+ Linux/PPC64:	Newly added.
+ Linux/SuperH:	Newly added.
+ FreeBSD/i386:	Works well.
+ FreeBSD/x86-64: Newly added (FreeBSD architecture is known as amd64).
+ Linux/Tilegx:  Newly added (64-bit mode only).
+
+* General Build Instructions
+
+In general, this library can be built and installed with the following
+commands:
+
+	$ ./autogen.sh # Needed only for building from git. Depends on libtool.
+	$ ./configure
+	$ make
+	$ make install prefix=PREFIX
+
+where PREFIX is the installation prefix.  By default, a prefix of
+/usr/local is used, such that libunwind.a is installed in
+/usr/local/lib and unwind.h is installed in /usr/local/include.  For
+testing, you may want to use a prefix of /usr/local instead.
+
+
+* Building with Intel compiler
+
+** Version 8 and later
+
+Starting with version 8, the preferred name for the IA-64 Intel
+compiler is "icc" (same name as on x86).  Thus, the configure-line
+should look like this:
+
+    $ ./configure CC=icc CFLAGS="-g -O3 -ip" CXX=icc CCAS=gcc CCASFLAGS=-g \
+		LDFLAGS="-L$PWD/src/.libs"
+
+
+* Building on HP-UX
+
+For the time being, libunwind must be built with GCC on HP-UX.
+
+libunwind should be configured and installed on HP-UX like this:
+
+    $ ./configure CFLAGS="-g -O2 -mlp64" CXXFLAGS="-g -O2 -mlp64"
+
+Caveat: Unwinding of 32-bit (ILP32) binaries is not supported
+	at the moment.
+
+** Workaround for older versions of GCC
+
+GCC v3.0 and GCC v3.2 ship with a bad version of sys/types.h.  The
+workaround is to issue the following commands before running
+"configure":
+
+    $ mkdir $top_dir/include/sys
+    $ cp /usr/include/sys/types.h $top_dir/include/sys
+
+GCC v3.3.2 or later have been fixed and do not require this
+workaround.
+
+* Building for PowerPC64 / Linux
+
+For building for power64 you should use:
+
+  $ ./configure CFLAGS="-g -O2 -m64" CXXFLAGS="-g -O2 -m64"
+
+If your power support altivec registers:
+  $ ./configure CFLAGS="-g -O2 -m64 -maltivec" CXXFLAGS="-g -O2 -m64 -maltivec"
+
+To check if your processor has support for vector registers (altivec):
+    cat /proc/cpuinfo | grep altivec
+and should have something like this:
+    cpu             : PPC970, altivec supported
+
+If libunwind seems to not work (backtracing failing), try to compile
+it with -O0, without optimizations. There are some compiler problems
+depending on the version of your gcc.
+
+* Building on FreeBSD
+
+General building instructions apply. To build and execute several tests,
+you need libexecinfo library available in ports as devel/libexecinfo.
+
+Development of the port was done of FreeBSD 8.0-STABLE. The library
+was build with the system compiler that is modified version of gcc 4.2.1,
+as well as the gcc 4.4.3.
+
+* Regression Testing
+
+After building the library, you can run a set of regression tests with:
+
+	$ make check
+
+** Expected results on IA-64 Linux
+
+Unless you have a very recent C library and compiler installed, it is
+currently expected to have the following tests fail on IA-64 Linux:
+
+	Gtest-init		(should pass starting with glibc-2.3.x/gcc-3.4)
+	Ltest-init		(should pass starting with glibc-2.3.x/gcc-3.4)
+	test-ptrace		(should pass starting with glibc-2.3.x/gcc-3.4)
+	run-ia64-test-dyn1	(should pass starting with glibc-2.3.x)
+
+This does not mean that libunwind cannot be used with older compilers
+or C libraries, it just means that for certain corner cases, unwinding
+will fail.  Since they're corner cases, it is not likely for
+applications to trigger them.
+
+Note: If you get lots of errors in Gia64-test-nat and Lia64-test-nat, it's
+      almost certainly a sign of an old assembler.  The GNU assembler used
+      to encode previous-stack-pointer-relative offsets incorrectly.
+      This bug was fixed on 21-Sep-2004 so any later assembler will be
+      fine.
+
+** Expected results on x86 Linux
+
+The following tests are expected to fail on x86 Linux:
+
+	test-ptrace
+
+** Expected results on x86-64 Linux
+
+The following tests are expected to fail on x86-64 Linux:
+
+	run-ptrace-misc	(see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18748
+			 and http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18749)
+
+** Expected results on PARISC Linux
+
+Caveat: GCC v3.4 or newer is needed on PA-RISC Linux.  Earlier
+versions of the compiler failed to generate the exception-handling
+program header (GNU_EH_FRAME) needed for unwinding.
+
+The following tests are expected to fail on x86-64 Linux:
+
+	Gtest-bt   (backtrace truncated at kill() due to lack of unwind-info)
+	Ltest-bt   (likewise)
+	Gtest-resume-sig  (Gresume.c:my_rt_sigreturn() is wrong somehow)
+	Ltest-resume-sig  (likewise)
+	Gtest-init (likewise)
+	Ltest-init (likewise)
+	Gtest-dyn1 (no dynamic unwind info support yet)
+	Ltest-dyn1 (no dynamic unwind info support yet)
+	test-setjmp		(longjmp() not implemented yet)
+	run-check-namespace	(toolchain doesn't support HIDDEN yet)
+
+** Expected results on HP-UX
+
+"make check" is currently unsupported for HP-UX.  You can try to run
+it, but most tests will fail (and some may fail to terminate).  The
+only test programs that are known to work at this time are:
+
+     tests/bt
+     tests/Gperf-simple
+     tests/test-proc-info
+     tests/test-static-link
+     tests/Gtest-init
+     tests/Ltest-init
+     tests/Gtest-resume-sig
+     tests/Ltest-resume-sig
+
+** Expected results on PPC64 Linux
+
+"make check" should run with no more than 10 out of 24 tests failed.
+
+
+* Performance Testing
+
+This distribution includes a few simple performance tests which give
+some idea of the basic cost of various libunwind operations.  After
+building the library, you can run these tests with the following
+commands:
+
+ $ cd tests
+ $ make perf
+
+* Contacting the Developers
+
+Please direct all questions regarding this library to:
+
+	libunwind-devel@nongnu.org
+
+You can do this by sending a mail to libunwind-request@nongnu.org with
+a body of:
+
+	subscribe libunwind-devel
+
+or you can subscribe and manage your subscription via the
+web-interface at:
+
+	https://savannah.nongnu.org/mail/?group=libunwind
+
+Or interact at the gihub page:
+
+   	https://github.com/libunwind/libunwind
diff --git a/sgx_unwind/libunwind/autogen-linux.sh b/sgx_unwind/libunwind/autogen-linux.sh
deleted file mode 100644
index f668166..0000000
--- a/sgx_unwind/libunwind/autogen-linux.sh
+++ /dev/null
@@ -1,105 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright (C) 2011-2019 Intel Corporation. All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-#   * Redistributions of source code must retain the above copyright
-#     notice, this list of conditions and the following disclaimer.
-#   * Redistributions in binary form must reproduce the above copyright
-#     notice, this list of conditions and the following disclaimer in
-#     the documentation and/or other materials provided with the
-#     distribution.
-#   * Neither the name of Intel Corporation nor the names of its
-#     contributors may be used to endorse or promote products derived
-#     from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#
-#
-
-
-set -x
-
-srcdir=`dirname $0`
-[ -z "$srcdir" ] && srcdir=.
-
-ORIGDIR=`pwd`
-cd $srcdir
-
-autoreconf -v --force --install || exit $?
-cd $ORIGDIR             || exit $?
-
-CFLAGS="$CFLAGS -std=c99 -fno-builtin -DHAVE_SGX=1 -fPIC -DUNW_LOCAL_ONLY"
-
-# Remove duplicated compiler options and filter out `-nostdinc'
-CFLAGS=`echo $CFLAGS | tr ' ' '\n' | sort | uniq | grep -v nostdinc | tr '\n' ' '`
-
-HOST_OPT=
-if echo $CFLAGS | grep -q -- '-m32'; then
-   HOST_OPT='--host=i386-linux-gnu'
-fi
-
-# Workaround for icc
-#
-# If we are running icc on Debian-like system, add
-#   /usr/include/$(dpkg-architecture -qDEB_BUILD_MULTIARCH)
-# to the header search path.
-if [ -f /usr/bin/dpkg ]; then
-    if [ x$CC != x"" ]; then
-        if $CC -E -dM -xc /dev/null  | grep -q __INTEL_COMPILER; then
-            INCVAR=$(dpkg-architecture -qDEB_BUILD_MULTIARCH)
-            CFLAGS="$CFLAGS -I/usr/include/$INCVAR"
-        fi
-    fi
-fi
-
-sed -i "s/rtmk-nova\*/rtmk-nova\* | linux-sgx/" $srcdir/config/config.sub
-
-export CFLAGS
-
-#Insert following codes into configure after add "-mfunction-return=thunk-extern -mindirect-branch-register" option, Or the "checking whether the C compiler works..." check will fail
-#Insert following codes into configure after add "-mfunction-return=thunk-extern -mindirect-branch-register" option, Or the "checking whether we are cross compiling... " check will fail
-#  #pragma GCC push_options
-#  #pragma GCC optimize ("-fomit-frame-pointer")
-#  void __x86_return_thunk()
-#  {
-#      __asm__("ret\n\t");
-#  }
-#  void __x86_indirect_thunk_rax()
-#  {
-#      __asm__("jmp *%rax\n\t");
-#  }
-#  #pragma GCC pop_options
-line=`grep -n "__x86_return_thunk()" $srcdir/configure | cut -d: -f 1`
-if [ -n "$line" ]; then
-  echo "__x86_return_thunk() already exist..."
-else
-  line_end=`grep -n "\"checking whether the C compiler works... \"" $srcdir/configure | cut -d: -f 1`
-  line_start=`expr $line_end - 30`  #Search an scope
-  sed -i "${line_start},${line_end} s/^_ACEOF/#pragma GCC push_options\r\n#pragma GCC optimize (\"-fomit-frame-pointer\")\r\nvoid __x86_return_thunk(){__asm__(\"ret\\\n\\\t\");}\r\nvoid __x86_indirect_thunk_rax(){__asm__(\"jmp \*%rax\\\n\\\t\");}\r\n#pragma GCC pop_options\r\n_ACEOF/" $srcdir/configure
-
-  line_end=`grep -n "\"checking whether we are cross compiling... \"" $srcdir/configure | cut -d: -f 1`
-  line_start=`expr $line_end - 30`  #Search an scope
-  sed -i "${line_start},${line_end} s/^_ACEOF/#pragma GCC push_options\r\n#pragma GCC optimize (\"-fomit-frame-pointer\")\r\nvoid __x86_return_thunk(){__asm__(\"ret\\\n\\\t\");}\r\nvoid __x86_indirect_thunk_rax(){__asm__(\"jmp \*%rax\\\n\\\t\");}\r\n#pragma GCC pop_options\r\n_ACEOF/" $srcdir/configure
-fi
-
-$srcdir/configure $HOST_OPT --enable-shared=no \
-   --disable-block-signals \
-   --enable-debug=no       \
-   --enable-debug-frame=no \
-   --enable-cxx-exceptions \
-   "$@"
-
diff --git a/sgx_unwind/libunwind/autogen.sh b/sgx_unwind/libunwind/autogen.sh
new file mode 100755
index 0000000..635283b
--- /dev/null
+++ b/sgx_unwind/libunwind/autogen.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+test -n "$srcdir" || srcdir=`dirname "$0"`
+test -n "$srcdir" || srcdir=.
+(
+  cd "$srcdir" &&
+  autoreconf --force -v --install
+) || exit
+
+CFLAGS="$CFLAGS -std=c99 -fno-builtin -DHAVE_SGX=1 -fPIC -DUNW_LOCAL_ONLY -fdebug-prefix-map=$(pwd)=/libunwind"
+# Remove duplicated compiler options and filter out `-nostdinc'
+CFLAGS=`echo $CFLAGS | tr ' ' '\n' | grep -v nostdinc | tr '\n' ' '`
+export CFLAGS
+
+#Insert following codes into configure after add "-mfunction-return=thunk-extern -mindirect-branch-register" option, Or the "checking whether the C compiler works..." check will fail
+#Insert following codes into configure after add "-mfunction-return=thunk-extern -mindirect-branch-register" option, Or the "checking whether we are cross compiling... " check will fail
+#  #pragma GCC push_options
+#  #pragma GCC optimize ("-fomit-frame-pointer")
+#  void __x86_return_thunk()
+#  {
+#      __asm__("ret\n\t");
+#  }
+#  void __x86_indirect_thunk_rax()
+#  {
+#      __asm__("jmp *%rax\n\t");
+#  }
+#  #pragma GCC pop_options 
+line=`grep -n "__x86_return_thunk()" ./configure | cut -d: -f 1`
+if [ -n "$line" ]; then
+  echo "__x86_return_thunk() already exist..."
+else
+  line_end=`grep -n "\"checking whether the C compiler works... \"" ./configure | cut -d: -f 1`
+  line_start=`expr $line_end - 30`  #Search an scope
+  sed -i "${line_start},${line_end} s/^_ACEOF/#pragma GCC push_options\r\n#pragma GCC optimize (\"-fomit-frame-pointer\")\r\nvoid __x86_return_thunk(){__asm__(\"ret\\\n\\\t\");}\r\nvoid __x86_indirect_thunk_rax(){__asm__(\"jmp \*%rax\\\n\\\t\");}\r\n#pragma GCC pop_options\r\n_ACEOF/" ./configure
+
+  line_end=`grep -n "\"checking whether we are cross compiling... \"" ./configure | cut -d: -f 1`
+  line_start=`expr $line_end - 30`  #Search an scope
+  sed -i "${line_start},${line_end} s/^_ACEOF/#pragma GCC push_options\r\n#pragma GCC optimize (\"-fomit-frame-pointer\")\r\nvoid __x86_return_thunk(){__asm__(\"ret\\\n\\\t\");}\r\nvoid __x86_indirect_thunk_rax(){__asm__(\"jmp \*%rax\\\n\\\t\");}\r\n#pragma GCC pop_options\r\n_ACEOF/" ./configure
+fi
+
+test -n "$NOCONFIGURE" || "$srcdir/configure" --enable-shared=no \
+                                              --disable-block-signals \
+                                              --enable-debug=no \
+                                              --enable-debug-frame=no \
+                                              --enable-setjmp=no \
+                                              --enable-cxx-exceptions
+
+#Remove the HAVE_MINCORE because inside SGX doesn't exist mincore() function
+sed -i 's/#define HAVE_MINCORE/\/\/#define HAVE_MINCORE/g' include/config.h
diff --git a/sgx_unwind/libunwind/configure.ac b/sgx_unwind/libunwind/configure.ac
new file mode 100644
index 0000000..20a6bec
--- /dev/null
+++ b/sgx_unwind/libunwind/configure.ac
@@ -0,0 +1,406 @@
+define(pkg_major, 1)
+define(pkg_minor, 3)
+define(pkg_extra, 1)
+define(pkg_maintainer, libunwind-devel@nongnu.org)
+define(mkvers, $1.$2.$3)
+dnl Process this file with autoconf to produce a configure script.
+AC_INIT([libunwind],[mkvers(pkg_major, pkg_minor, pkg_extra)],[pkg_maintainer])
+AC_CONFIG_SRCDIR(src/mi/backtrace.c)
+AC_CONFIG_AUX_DIR(config)
+AC_CANONICAL_TARGET
+AM_INIT_AUTOMAKE([1.6 subdir-objects])
+AM_MAINTAINER_MODE
+AC_CONFIG_HEADERS([include/config.h])
+
+AC_CONFIG_MACRO_DIRS([m4])
+
+dnl Checks for programs.
+AC_PROG_CC
+AC_PROG_CXX
+AC_PROG_INSTALL
+AC_PROG_MAKE_SET
+m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
+LT_INIT
+AM_PROG_AS
+AM_PROG_CC_C_O
+
+dnl Checks for libraries.
+AC_CHECK_LIB(uca, __uc_get_grs)
+OLD_LIBS=${LIBS}
+AC_SEARCH_LIBS(dlopen, dl)
+LIBS=${OLD_LIBS}
+case "$ac_cv_search_dlopen" in
+  -l*) DLLIB=$ac_cv_search_dlopen;;
+  *) DLLIB="";;
+esac
+
+CHECK_ATOMIC_OPS
+
+dnl Checks for header files.
+AC_HEADER_STDC
+AC_CHECK_HEADERS(asm/ptrace_offsets.h endian.h sys/endian.h execinfo.h \
+		ia64intrin.h sys/uc_access.h unistd.h signal.h sys/types.h \
+		sys/procfs.h sys/ptrace.h byteswap.h elf.h sys/elf.h link.h sys/link.h)
+
+dnl Checks for typedefs, structures, and compiler characteristics.
+AC_C_CONST
+AC_C_INLINE
+AC_TYPE_SIZE_T
+AC_CHECK_SIZEOF(off_t)
+
+CPPFLAGS="${CPPFLAGS} -D_GNU_SOURCE"
+
+AC_CHECK_MEMBERS([struct dl_phdr_info.dlpi_subs],,,[#include <link.h>])
+AC_CHECK_TYPES([struct elf_prstatus, struct prstatus], [], [],
+[$ac_includes_default
+#if HAVE_SYS_PROCFS_H
+# include <sys/procfs.h>
+#endif
+])
+
+AC_CHECK_DECLS([PTRACE_POKEUSER, PTRACE_POKEDATA, PTRACE_SETREGSET,
+PTRACE_TRACEME, PTRACE_CONT, PTRACE_SINGLESTEP,
+PTRACE_SYSCALL, PT_IO, PT_GETREGS,
+PT_GETFPREGS, PT_CONTINUE, PT_TRACE_ME,
+PT_STEP, PT_SYSCALL], [], [],
+[$ac_includes_default
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#include <sys/ptrace.h>
+])
+
+dnl Checks for library functions.
+AC_CHECK_FUNCS(dl_iterate_phdr dl_phdr_removals_counter dlmodinfo getunwind \
+		ttrace mincore)
+
+AC_MSG_CHECKING([if building with AltiVec])
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#ifndef __ALTIVEC__
+# error choke
+#endif
+]])], [use_altivec=yes],[use_altivec=no])
+AM_CONDITIONAL(USE_ALTIVEC, [test x$use_altivec = xyes])
+AC_MSG_RESULT([$use_altivec])
+
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#ifndef __powerpc64__
+# error choke
+#endif
+]])], [ppc_bits=64], [ppc_bits=32])
+
+AC_DEFUN([SET_ARCH],[
+    AS_CASE([$1],
+        [aarch64*],[$2=aarch64],
+        [arm*],[$2=arm],
+        [i?86],[$2=x86],
+        [hppa*],[$2=hppa],
+        [mips*],[$2=mips],
+        [powerpc*],[$2=ppc$ppc_bits],
+        [sh*],[$2=sh],
+        [amd64],[$2=x86_64],
+        [tile*],[$2=tilegx],
+        [$2=$1])
+]) dnl SET_ARCH
+
+SET_ARCH([$build_cpu],[build_arch])
+SET_ARCH([$host_cpu],[host_arch])
+SET_ARCH([$target_cpu],[target_arch])
+
+# Check for Android
+AC_MSG_CHECKING([for Android])
+android="no"
+case "$host_os" in
+  *android*)
+    android="yes"
+    AC_MSG_RESULT([yes])
+    ;;
+  *)
+    AC_MSG_RESULT([no])
+    ;;
+esac
+
+AC_ARG_ENABLE(setjmp,
+	AS_HELP_STRING([--enable-setjmp],[building libunwind-setjmp library]),,
+        [AS_IF([test x$target_arch == x$host_arch], [enable_setjmp=yes], [enable_setjmp=no])]
+)
+
+AC_MSG_CHECKING([if we should build libunwind-setjmp])
+AC_MSG_RESULT([$enable_setjmp])
+
+AC_MSG_CHECKING([for build architecture])
+AC_MSG_RESULT([$build_arch])
+AC_MSG_CHECKING([for host architecture])
+AC_MSG_RESULT([$host_arch])
+AC_MSG_CHECKING([for target architecture])
+AC_MSG_RESULT([$target_arch])
+AC_MSG_CHECKING([for target operating system])
+AC_MSG_RESULT([$target_os])
+
+AM_CONDITIONAL(BUILD_COREDUMP, test x$enable_coredump = xyes)
+AM_CONDITIONAL(BUILD_PTRACE, test x$enable_ptrace = xyes)
+AM_CONDITIONAL(BUILD_SETJMP, test x$enable_setjmp = xyes)
+AM_CONDITIONAL(NO_PTRACE_TEST, test x$build_arch != x$host_arch)
+AM_CONDITIONAL(REMOTE_ONLY, test x$target_arch != x$host_arch)
+AM_CONDITIONAL(ARCH_AARCH64, test x$target_arch = xaarch64)
+AM_CONDITIONAL(ARCH_ARM, test x$target_arch = xarm)
+AM_CONDITIONAL(ARCH_IA64, test x$target_arch = xia64)
+AM_CONDITIONAL(ARCH_HPPA, test x$target_arch = xhppa)
+AM_CONDITIONAL(ARCH_MIPS, test x$target_arch = xmips)
+AM_CONDITIONAL(ARCH_X86, test x$target_arch = xx86)
+AM_CONDITIONAL(ARCH_X86_64, test x$target_arch = xx86_64)
+AM_CONDITIONAL(ARCH_PPC32, test x$target_arch = xppc32)
+AM_CONDITIONAL(ARCH_PPC64, test x$target_arch = xppc64)
+AM_CONDITIONAL(ARCH_SH, test x$target_arch = xsh)
+AM_CONDITIONAL(ARCH_TILEGX, test x$target_arch = xtilegx)
+AM_CONDITIONAL(OS_LINUX, expr x$target_os : xlinux >/dev/null)
+AM_CONDITIONAL(OS_HPUX, expr x$target_os : xhpux >/dev/null)
+AM_CONDITIONAL(OS_FREEBSD, expr x$target_os : xfreebsd >/dev/null)
+AM_CONDITIONAL(OS_QNX, expr x$target_os : xnto-qnx >/dev/null)
+
+AC_MSG_CHECKING([for ELF helper width])
+case "${target_arch}" in
+(arm|hppa|ppc32|x86|sh) use_elf32=yes; AC_MSG_RESULT([32]);;
+(aarch64|ia64|ppc64|x86_64|tilegx)  use_elf64=yes; AC_MSG_RESULT([64]);;
+(mips)                 use_elfxx=yes; AC_MSG_RESULT([xx]);;
+*)                     AC_MSG_ERROR([Unknown ELF target: ${target_arch}])
+esac
+AM_CONDITIONAL(USE_ELF32, [test x$use_elf32 = xyes])
+AM_CONDITIONAL(USE_ELF64, [test x$use_elf64 = xyes])
+AM_CONDITIONAL(USE_ELFXX, [test x$use_elfxx = xyes])
+
+AC_MSG_CHECKING([whether to include DWARF support])
+if test x$target_arch != xia64; then
+  use_dwarf=yes
+else
+  use_dwarf=no
+fi
+AM_CONDITIONAL(USE_DWARF, [test x$use_dwarf = xyes])
+AC_MSG_RESULT([$use_dwarf])
+
+if test x$target_arch = xppc64; then
+        libdir='${exec_prefix}/lib64'
+        AC_MSG_NOTICE([PowerPC64 detected, lib will be installed ${libdir}]);
+        AC_SUBST([libdir])
+fi
+
+AC_MSG_CHECKING([whether to restrict build to remote support])
+if test x$target_arch != x$host_arch; then
+  CPPFLAGS="${CPPFLAGS} -DUNW_REMOTE_ONLY"
+  remote_only=yes
+else
+  remote_only=no
+fi
+AC_MSG_RESULT([$remote_only])
+
+AC_MSG_CHECKING([whether to enable debug support])
+AC_ARG_ENABLE(debug,
+AS_HELP_STRING([--enable-debug],[turn on debug support (slows down execution)]))
+if test x$enable_debug = xyes; then
+  CPPFLAGS="${CPPFLAGS} -DDEBUG"
+else
+  CPPFLAGS="${CPPFLAGS} -DNDEBUG"
+fi
+AC_MSG_RESULT([$enable_debug])
+
+AC_MSG_CHECKING([whether to enable C++ exception support])
+AC_ARG_ENABLE(cxx_exceptions,
+AS_HELP_STRING([--enable-cxx-exceptions],[use libunwind to handle C++ exceptions]),,
+[
+# C++ exception handling doesn't work too well on x86
+case $target_arch in
+  x86*) enable_cxx_exceptions=no;;
+  aarch64*) enable_cxx_exceptions=no;;
+  arm*) enable_cxx_exceptions=no;;
+  mips*) enable_cxx_exceptions=no;;
+  tile*) enable_cxx_exceptions=no;;
+  *) enable_cxx_exceptions=yes;;
+esac
+])
+
+AM_CONDITIONAL([SUPPORT_CXX_EXCEPTIONS], [test x$enable_cxx_exceptions = xyes])
+AC_MSG_RESULT([$enable_cxx_exceptions])
+
+AC_MSG_CHECKING([whether to load .debug_frame sections])
+AC_ARG_ENABLE(debug_frame,
+AS_HELP_STRING([--enable-debug-frame],[Load the ".debug_frame" section if available]),, [
+case "${target_arch}" in
+  (arm) enable_debug_frame=yes;;
+  (aarch64) enable_debug_frame=yes;;
+  (*)   enable_debug_frame=no;;
+esac])
+if test x$enable_debug_frame = xyes; then
+  AC_DEFINE([CONFIG_DEBUG_FRAME], [], [Enable Debug Frame])
+fi
+AC_MSG_RESULT([$enable_debug_frame])
+
+AC_MSG_CHECKING([whether to block signals during mutex ops])
+AC_ARG_ENABLE(block_signals,
+AS_HELP_STRING([--enable-block-signals],[Block signals before performing mutex operations]),,
+[enable_block_signals=yes])
+if test x$enable_block_signals = xyes; then
+  AC_DEFINE([CONFIG_BLOCK_SIGNALS], [], [Block signals before mutex operations])
+fi
+AC_MSG_RESULT([$enable_block_signals])
+
+AC_MSG_CHECKING([whether to validate memory addresses before use])
+AC_ARG_ENABLE(conservative_checks,
+AS_HELP_STRING([--enable-conservative-checks],[Validate all memory addresses before use]),,
+[enable_conservative_checks=yes])
+if test x$enable_conservative_checks = xyes; then
+  AC_DEFINE(CONSERVATIVE_CHECKS, 1,
+	[Define to 1 if you want every memory access validated])
+fi
+AC_MSG_RESULT([$enable_conservative_checks])
+
+AC_MSG_CHECKING([whether to enable msabi support])
+AC_ARG_ENABLE(msabi_support,
+AS_HELP_STRING([--enable-msabi-support],[Enables support for Microsoft ABI extensions]))
+if test x$enable_msabi_support = xyes; then
+  AC_DEFINE([CONFIG_MSABI_SUPPORT], [], [Support for Microsoft ABI extensions])
+fi
+AC_MSG_RESULT([$enable_msabi_support])
+
+LIBLZMA=
+AC_MSG_CHECKING([whether to support LZMA-compressed symbol tables])
+AC_ARG_ENABLE(minidebuginfo,
+AS_HELP_STRING([--enable-minidebuginfo], [Enables support for LZMA-compressed symbol tables]),, [enable_minidebuginfo=auto])
+AC_MSG_RESULT([$enable_minidebuginfo])
+if test x$enable_minidebuginfo != xno; then
+   AC_CHECK_LIB([lzma], [lzma_mf_is_supported],
+   [LIBLZMA=-llzma
+    AC_DEFINE([HAVE_LZMA], [1], [Define if you have liblzma])
+    enable_minidebuginfo=yes],
+   [if test x$enable_minidebuginfo = xyes; then
+      AC_MSG_FAILURE([liblzma not found])
+    fi])
+fi
+AC_SUBST([LIBLZMA])
+AM_CONDITIONAL(HAVE_LZMA, test x$enable_minidebuginfo = xyes)
+
+AC_MSG_CHECKING([whether to support UNW_CACHE_PER_THREAD])
+AC_ARG_ENABLE([per-thread-cache],
+AS_HELP_STRING([--enable-per-thread-cache], [build with support for UNW_CACHE_PER_THREAD (which imposes a hight TLS memory usage) (default: disabled)]))
+AC_MSG_RESULT([$enable_per_thread_cache])
+AS_IF([test x$enable_per_thread_cache = xyes], [
+  LIBUNWIND___THREAD
+  AS_IF([test x$libc_cv_gcc___thread = xno], [
+    AC_MSG_FAILURE([UNW_CACHE_PER_THREAD requires __thread])
+  ])
+])
+
+AC_MSG_CHECKING([for Intel compiler])
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[#ifndef __INTEL_COMPILER
+#error choke me
+#endif]])],[intel_compiler=yes],[intel_compiler=no])
+
+if test x$GCC = xyes -a x$intel_compiler != xyes; then
+  CFLAGS="${CFLAGS} -fexceptions -Wall -Wsign-compare"
+fi
+AC_MSG_RESULT([$intel_compiler])
+
+AC_MSG_CHECKING([for QCC compiler])
+AS_CASE([$CC], [qcc*|QCC*], [qcc_compiler=yes], [qcc_compiler=no])
+AC_MSG_RESULT([$qcc_compiler])
+
+if test x$intel_compiler = xyes; then
+  AC_MSG_CHECKING([if linker supports -static-libcxa])
+  save_LDFLAGS="$LDFLAGS"
+  LDFLAGS="$LDFLAGS -static-libcxa"
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],[have_static_libcxa=yes],[have_static_libcxa=no])
+  LDFLAGS="$save_LDFLAGS"
+  if test "x$have_static_libcxa" = xyes; then
+    LDFLAGS_STATIC_LIBCXA="-XCClinker -static-libcxa"
+  fi
+  AC_MSG_RESULT([$have_static_libcxa])
+fi
+
+if test x$qcc_compiler = xyes; then
+    LDFLAGS_NOSTARTFILES="-XCClinker -Wc,-nostartfiles"
+else
+    LDFLAGS_NOSTARTFILES="-XCClinker -nostartfiles"
+fi
+
+if test x$GCC = xyes -a x$intel_compiler != xyes -a x$qcc_compiler != xyes -a x$android != xyes; then
+  LIBCRTS="-lgcc_s"
+fi
+
+AC_MSG_CHECKING([for __builtin___clear_cache])
+AC_LINK_IFELSE(
+  [AC_LANG_PROGRAM([[]], [[__builtin___clear_cache(0, 0)]])],
+  [have__builtin___clear_cache=yes],
+  [have__builtin___clear_cache=no])
+if test x$have__builtin___clear_cache = xyes; then
+  AC_DEFINE([HAVE__BUILTIN___CLEAR_CACHE], [1],
+            [Defined if __builtin___clear_cache() is available])
+fi
+AC_MSG_RESULT([$have__builtin___clear_cache])
+
+AC_MSG_CHECKING([for __builtin_unreachable])
+AC_LINK_IFELSE(
+  [AC_LANG_PROGRAM([[]], [[__builtin_unreachable()]])],
+  [have__builtin_unreachable=yes],
+  [have__builtin_unreachable=no])
+if test x$have__builtin_unreachable = xyes; then
+  AC_DEFINE([HAVE__BUILTIN_UNREACHABLE], [1],
+            [Defined if __builtin_unreachable() is available])
+fi
+AC_MSG_RESULT([$have__builtin_unreachable])
+
+AC_MSG_CHECKING([for __sync atomics])
+AC_LINK_IFELSE(
+  [AC_LANG_PROGRAM([[]], [[
+    __sync_bool_compare_and_swap((int *)0, 0, 1);
+    __sync_fetch_and_add((int *)0, 1);
+    ]])],
+  [have_sync_atomics=yes],
+  [have_sync_atomics=no])
+if test x$have_sync_atomics = xyes; then
+  AC_DEFINE([HAVE_SYNC_ATOMICS], [1],
+            [Defined if __sync atomics are available])
+fi
+AC_MSG_RESULT([$have_sync_atomics])
+
+CCASFLAGS="${CCASFLAGS} ${CPPFLAGS}"
+
+arch="$target_arch"
+ARCH=`echo $target_arch | tr [a-z] [A-Z]`
+
+dnl create shell variables from the M4 macros:
+PKG_MAJOR=pkg_major
+PKG_MINOR=pkg_minor
+PKG_EXTRA=pkg_extra
+PKG_MAINTAINER=pkg_maintainer
+
+old_LIBS="$LIBS"
+LIBS=""
+AC_SEARCH_LIBS(backtrace, execinfo)
+LIBS="$old_LIBS"
+case "$ac_cv_search_backtrace" in
+  -l*) BACKTRACELIB=$ac_cv_search_backtrace;;
+  *) BACKTRACELIB="";;
+esac
+
+
+AC_SUBST(build_arch)
+AC_SUBST(target_os)
+AC_SUBST(arch)
+AC_SUBST(ARCH)
+AC_SUBST(LDFLAGS_STATIC_LIBCXA)
+AC_SUBST(LDFLAGS_NOSTARTFILES)
+AC_SUBST(LIBCRTS)
+AC_SUBST(PKG_MAJOR)
+AC_SUBST(PKG_MINOR)
+AC_SUBST(PKG_EXTRA)
+AC_SUBST(PKG_MAINTAINER)
+AC_SUBST(enable_cxx_exceptions)
+AC_SUBST(enable_debug_frame)
+AC_SUBST(DLLIB)
+AC_SUBST(BACKTRACELIB)
+
+AC_CONFIG_FILES(Makefile src/Makefile
+                include/libunwind-common.h
+                include/libunwind.h include/tdep/libunwind_i.h)
+AC_CONFIG_FILES(src/unwind/libunwind.pc
+                src/libunwind-generic.pc)
+AC_OUTPUT
diff --git a/sgx_unwind/libunwind/configure.in b/sgx_unwind/libunwind/configure.in
deleted file mode 100644
index c25deb1..0000000
--- a/sgx_unwind/libunwind/configure.in
+++ /dev/null
@@ -1,308 +0,0 @@
-define(pkg_major, 1)
-define(pkg_minor, 0)
-define(pkg_extra, )
-define(pkg_maintainer, libunwind-devel@nongnu.org)
-define(mkvers, $1.$2$3)
-dnl Process this file with autoconf to produce a configure script.
-AC_INIT(libunwind, mkvers(pkg_major, pkg_minor, pkg_extra), pkg_maintainer)
-AC_CONFIG_SRCDIR(src/mi/backtrace.c)
-AC_CONFIG_AUX_DIR(config)
-AC_CANONICAL_SYSTEM
-AM_INIT_AUTOMAKE([1.6 subdir-objects])
-AM_MAINTAINER_MODE
-AM_CONFIG_HEADER(include/config.h)
-
-dnl Checks for programs.
-AC_PROG_CC
-AC_PROG_CXX
-AC_PROG_INSTALL
-AC_PROG_MAKE_SET
-AM_PROG_LIBTOOL
-AM_PROG_AS
-AM_PROG_CC_C_O
-
-dnl Checks for libraries.
-AC_CHECK_LIB(uca, __uc_get_grs)
-OLD_LIBS=${LIBS}
-AC_SEARCH_LIBS(dlopen, dl)
-LIBS=${OLD_LIBS}
-case "$ac_cv_search_dlopen" in
-  -l*) DLLIB=$ac_cv_search_dlopen;;
-  *) DLLIB="";;
-esac
-
-CHECK_ATOMIC_OPS
-
-dnl Checks for header files.
-AC_HEADER_STDC
-AC_CHECK_HEADERS(asm/ptrace_offsets.h endian.h sys/endian.h execinfo.h \
-		ia64intrin.h sys/uc_access.h unistd.h signal.h sys/types.h \
-		sys/procfs.h sys/ptrace.h byteswap.h)
-
-dnl Checks for typedefs, structures, and compiler characteristics.
-AC_C_CONST
-AC_C_INLINE
-AC_TYPE_SIGNAL
-AC_TYPE_SIZE_T
-AC_CHECK_SIZEOF(off_t)
-
-CPPFLAGS="${CPPFLAGS} -D_GNU_SOURCE"
-
-AC_CHECK_MEMBERS([struct dl_phdr_info.dlpi_subs],,,[#include <link.h>])
-AC_CHECK_TYPES([sighandler_t], [], [],
-[$ac_includes_default
-#if HAVE_SIGNAL_H
-# include <signal.h>
-#endif
-])
-AC_CHECK_TYPES([struct elf_prstatus, struct prstatus], [], [],
-[$ac_includes_default
-#if HAVE_SYS_PROCFS_H
-# include <sys/procfs.h>
-#endif
-])
-
-AC_CHECK_DECLS([PTRACE_POKEUSER, PTRACE_POKEDATA,
-PTRACE_TRACEME, PTRACE_CONT, PTRACE_SINGLESTEP,
-PTRACE_SYSCALL, PT_IO, PT_GETREGS,
-PT_GETFPREGS, PT_CONTINUE, PT_TRACE_ME,
-PT_STEP, PT_SYSCALL], [], [],
-[$ac_includes_default
-#if HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-#include <sys/ptrace.h>
-])
-
-dnl Checks for library functions.
-AC_FUNC_MEMCMP
-AC_TYPE_SIGNAL
-AC_CHECK_FUNCS(dl_iterate_phdr dl_phdr_removals_counter dlmodinfo getunwind \
-		ttrace mincore)
-is_gcc_m64() {
- if test `echo $CFLAGS | grep "\-m64" -c` -eq 1 ; then echo ppc64;
- else
-  if test `echo $CC | grep "\-m64" -c` -eq 1 ; then echo ppc64; else echo ppc32; fi;
- fi;
-}
-
-is_gcc_altivec() {
- if test `echo $CFLAGS | grep "\-maltivec" -c` -eq 1 ; then echo has_altivec;
- else
-  if test `echo $CC | grep "\-maltivec" -c` -eq 1 ; then echo has_altivec; else echo no_altivec; fi;
- fi;
-}
-
-AC_MSG_CHECKING([if building with AltiVec])
-use_altivec=`is_gcc_altivec`
-AM_CONDITIONAL(USE_ALTIVEC, test x$use_altivec = xhas_altivec)
-if test x$use_altivec = xhas_altivec; then
-  AC_MSG_RESULT([yes])
-else
-  AC_MSG_RESULT([no])
-fi
-
-get_arch() {
- case "$1" in
-  arm*) echo arm;;
-  i?86) echo x86;;
-  hppa*) echo hppa;;
-  mips*) echo mips;;
-  powerpc*) is_gcc_m64;;
-  amd64) echo x86_64;;
-  *) echo $1;;
- esac
-}
-
-build_arch=`get_arch $build_cpu`
-host_arch=`get_arch $host_cpu`
-target_arch=`get_arch $target_cpu`
-
-AC_MSG_CHECKING([for build architecture])
-AC_MSG_RESULT([$build_arch])
-AC_MSG_CHECKING([for host architecture])
-AC_MSG_RESULT([$host_arch])
-AC_MSG_CHECKING([for target architecture])
-AC_MSG_RESULT([$target_arch])
-AC_MSG_CHECKING([for target operating system])
-AC_MSG_RESULT([$target_os])
-
-AM_CONDITIONAL(REMOTE_ONLY, test x$target_arch != x$host_arch)
-AM_CONDITIONAL(ARCH_ARM, test x$target_arch = xarm)
-AM_CONDITIONAL(ARCH_IA64, test x$target_arch = xia64)
-AM_CONDITIONAL(ARCH_HPPA, test x$target_arch = xhppa)
-AM_CONDITIONAL(ARCH_MIPS, test x$target_arch = xmips)
-AM_CONDITIONAL(ARCH_X86, test x$target_arch = xx86)
-AM_CONDITIONAL(ARCH_X86_64, test x$target_arch = xx86_64)
-AM_CONDITIONAL(ARCH_PPC32, test x$target_arch = xppc32)
-AM_CONDITIONAL(ARCH_PPC64, test x$target_arch = xppc64)
-AM_CONDITIONAL(OS_LINUX, expr x$target_os : xlinux >/dev/null)
-AM_CONDITIONAL(OS_HPUX, expr x$target_os : xhpux >/dev/null)
-AM_CONDITIONAL(OS_FREEBSD, expr x$target_os : xfreebsd >/dev/null)
-
-AC_MSG_CHECKING([for ELF helper width])
-case "${target_arch}" in
-(arm|hppa|ppc32|x86)   use_elf32=yes; AC_MSG_RESULT([32]);;
-(ia64|ppc64|x86_64)    use_elf64=yes; AC_MSG_RESULT([64]);;
-(mips)                 use_elfxx=yes; AC_MSG_RESULT([xx]);;
-*)                     AC_MSG_ERROR([Unknown ELF target: ${target_arch}])
-esac
-AM_CONDITIONAL(USE_ELF32, [test x$use_elf32 = xyes])
-AM_CONDITIONAL(USE_ELF64, [test x$use_elf64 = xyes])
-AM_CONDITIONAL(USE_ELFXX, [test x$use_elfxx = xyes])
-
-AC_MSG_CHECKING([whether to include DWARF support])
-if test x$target_arch != xia64; then
-  use_dwarf=yes
-else
-  use_dwarf=no
-fi
-AM_CONDITIONAL(USE_DWARF, [test x$use_dwarf = xyes])
-AC_MSG_RESULT([$use_dwarf])
-
-if test x$target_arch = xppc64; then
-        libdir='${exec_prefix}/lib64'
-        AC_MSG_NOTICE([PowerPC64 detected, lib will be installed ${libdir}]);
-        AC_SUBST([libdir])
-fi
-
-AC_MSG_CHECKING([whether to restrict build to remote support])
-if test x$target_arch != x$host_arch; then
-  CPPFLAGS="${CPPFLAGS} -DUNW_REMOTE_ONLY"
-  remote_only=yes
-else
-  remote_only=no
-fi
-AC_MSG_RESULT([$remote_only])
-
-AC_MSG_CHECKING([whether to enable debug support])
-AC_ARG_ENABLE(debug,
-[  --enable-debug          turn on debug support (slows down execution)],
-[enable_debug=$enableval], [enable_debug=no])
-if test x$enable_debug = xyes; then
-  CPPFLAGS="${CPPFLAGS} -DDEBUG"
-else
-  CPPFLAGS="${CPPFLAGS} -DNDEBUG"
-fi
-AC_MSG_RESULT([$enable_debug])
-
-AC_MSG_CHECKING([whether to enable C++ exception support])
-AC_ARG_ENABLE(cxx_exceptions,
-[  --enable-cxx-exceptions use libunwind to handle C++ exceptions],
-[enable_cxx_exceptions=$enableval], 
-[
-# C++ exception handling doesn't work too well on x86
-case $target_arch in
-  x86*) enable_cxx_exceptions=no;;
-  arm*) enable_cxx_exceptions=no;;
-  mips*) enable_cxx_exceptions=no;;
-  *) enable_cxx_exceptions=yes;;
-esac
-])
-
-AM_CONDITIONAL([SUPPORT_CXX_EXCEPTIONS], [test x$enable_cxx_exceptions = xyes])
-AC_MSG_RESULT([$enable_cxx_exceptions])
-
-AC_MSG_CHECKING([whether to load .debug_frame sections])
-AC_ARG_ENABLE(debug_frame,
-[  --enable-debug-frame Load the ".debug_frame" section if available],
-[enable_debug_frame=$enableval], [
-case "${target_arch}" in
-  (arm) enable_debug_frame=yes;;
-  (*)   enable_debug_frame=no;;
-esac])
-if test x$enable_debug_frame = xyes; then
-  AC_DEFINE([CONFIG_DEBUG_FRAME], [], [Enable Debug Frame])
-fi
-AC_MSG_RESULT([$enable_debug_frame])
-
-AC_MSG_CHECKING([whether to block signals during mutex ops])
-AC_ARG_ENABLE(block_signals,
-[  --enable-block-signals Block signals before performing mutex operations],
-[enable_block_signals=$enableval], [enable_block_signals=yes])
-if test x$enable_block_signals = xyes; then
-  AC_DEFINE([CONFIG_BLOCK_SIGNALS], [], [Block signals before mutex operations])
-fi
-AC_MSG_RESULT([$enable_block_signals])
-
-AC_MSG_CHECKING([whether to validate memory addresses before use])
-AC_ARG_ENABLE(conservative_checks,
-[  --enable-conservative-checks Validate all memory addresses before use],
-[enable_conservative_checks=$enableval], [enable_conservative_checks=yes])
-if test x$enable_conservative_checks = xyes; then
-  AC_DEFINE(CONSERVATIVE_CHECKS, 1,
-	[Define to 1 if you want every memory access validated])
-fi
-AC_MSG_RESULT([$enable_conservative_checks])
-
-AC_MSG_CHECKING([whether to enable msabi support])
-AC_ARG_ENABLE(msabi_support,
-[  --enable-msabi-support Enables support for Microsoft ABI extensions ],
-[enable_msabi_support=$enableval], [enable_msabi_support=no])
-if test x$enable_msabi_support = xyes; then
-  AC_DEFINE([CONFIG_MSABI_SUPPORT], [], [Support for Microsoft ABI extensions])
-fi
-AC_MSG_RESULT([$enable_msabi_support])
-
-LIBUNWIND___THREAD
-
-AC_MSG_CHECKING([for Intel compiler])
-AC_TRY_COMPILE([], [#ifndef __INTEL_COMPILER
-#error choke me
-#endif], [intel_compiler=yes], [intel_compiler=no])
-
-if test x$GCC = xyes -a x$intel_compiler != xyes; then
-  CFLAGS="${CFLAGS} -fexceptions -Wall -Wsign-compare"
-  LIBCRTS="-lgcc"
-fi
-AC_MSG_RESULT([$intel_compiler])
-
-if test x$intel_compiler = xyes; then
-  AC_MSG_CHECKING([if linker supports -static-libcxa])
-  save_LDFLAGS="$LDFLAGS"
-  LDFLAGS="$LDFLAGS -static-libcxa"
-  AC_TRY_LINK([], [], [have_static_libcxa=yes], [have_static_libcxa=no])
-  LDFLAGS="$save_LDFLAGS"
-  if test "x$have_static_libcxa" = xyes; then
-    LDFLAGS_STATIC_LIBCXA="-XCClinker -static-libcxa"
-  fi
-  AC_MSG_RESULT([$have_static_libcxa])
-fi
-
-CCASFLAGS="${CCASFLAGS} ${CPPFLAGS}"
-
-arch="$target_arch"
-ARCH=`echo $target_arch | tr [a-z] [A-Z]`
-
-dnl create shell variables from the M4 macros:
-PKG_MAJOR=pkg_major
-PKG_MINOR=pkg_minor
-PKG_EXTRA=pkg_extra
-PKG_MAINTAINER=pkg_maintainer
-
-old_LIBS="$LIBS"
-LIBS=""
-AC_SEARCH_LIBS(backtrace, execinfo)
-AM_CONDITIONAL(HAVE_BACKTRACE, test "x$ac_cv_search_backtrace" != xno)
-BACKTRACELIB="$LIBS"
-LIBS="$old_LIBS"
-
-AC_SUBST(build_arch)
-AC_SUBST(target_os)
-AC_SUBST(arch)
-AC_SUBST(ARCH)
-AC_SUBST(LDFLAGS_STATIC_LIBCXA)
-AC_SUBST(LIBCRTS)
-AC_SUBST(PKG_MAJOR)
-AC_SUBST(PKG_MINOR)
-AC_SUBST(PKG_EXTRA)
-AC_SUBST(PKG_MAINTAINER)
-AC_SUBST(enable_cxx_exceptions)
-AC_SUBST(enable_debug_frame)
-AC_SUBST(DLLIB)
-AC_SUBST(BACKTRACELIB)
-
-AC_CONFIG_FILES(Makefile src/Makefile
-		include/libunwind-common.h
-                include/libunwind.h include/tdep/libunwind_i.h)
-AC_OUTPUT
diff --git a/sgx_unwind/libunwind/include/compiler.h b/sgx_unwind/libunwind/include/compiler.h
new file mode 100644
index 0000000..2fa59ef
--- /dev/null
+++ b/sgx_unwind/libunwind/include/compiler.h
@@ -0,0 +1,72 @@
+/* libunwind - a platform-independent unwind library
+   Copyright (C) 2001-2005 Hewlett-Packard Co
+   Copyright (C) 2007 David Mosberger-Tang
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+
+This file is part of libunwind.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
+
+/* Compiler specific useful bits that are used in libunwind, and also in the
+ * tests. */
+
+#ifndef COMPILER_H
+#define COMPILER_H
+
+#ifdef __GNUC__
+# define ALIGNED(x)     __attribute__((aligned(x)))
+# define CONST_ATTR     __attribute__((__const__))
+# define UNUSED         __attribute__((unused))
+# define NOINLINE       __attribute__((noinline))
+# define NORETURN       __attribute__((noreturn))
+# define ALIAS2(name)   #name
+# define ALIAS(name)    __attribute__((alias (ALIAS2(name))))
+# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
+#  define ALWAYS_INLINE inline __attribute__((always_inline))
+#  define HIDDEN        __attribute__((visibility ("hidden")))
+# else
+#  define ALWAYS_INLINE
+#  define HIDDEN
+# endif
+# define WEAK           __attribute__((weak))
+# if (__GNUC__ >= 3)
+#  define likely(x)     __builtin_expect ((x), 1)
+#  define unlikely(x)   __builtin_expect ((x), 0)
+# else
+#  define likely(x)     (x)
+#  define unlikely(x)   (x)
+# endif
+#else
+# define ALIGNED(x)
+# define ALWAYS_INLINE
+# define CONST_ATTR
+# define UNUSED
+# define NOINLINE
+# define NORETURN
+# define ALIAS(name)
+# define HIDDEN
+# define WEAK
+# define likely(x)      (x)
+# define unlikely(x)    (x)
+#endif
+
+#define ARRAY_SIZE(a)   (sizeof (a) / sizeof ((a)[0]))
+
+#endif /* COMPILER_H */
diff --git a/sgx_unwind/libunwind/include/dwarf-eh.h b/sgx_unwind/libunwind/include/dwarf-eh.h
index e945014..e037507 100644
--- a/sgx_unwind/libunwind/include/dwarf-eh.h
+++ b/sgx_unwind/libunwind/include/dwarf-eh.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -58,9 +58,9 @@
     string:
 
      'z': The operand for this character is a uleb128 value that gives the
-	  length of the CIE augmentation body, not counting the length
-	  of the uleb128 operand itself.  If present, this code must
-	  appear as the first character in the augmentation body.
+          length of the CIE augmentation body, not counting the length
+          of the uleb128 operand itself.  If present, this code must
+          appear as the first character in the augmentation body.
 
      'L': Indicates that the FDE's augmentation body contains an LSDA
           pointer.  The operand for this character is a single byte
@@ -73,13 +73,13 @@
           operand for this character is a single byte that specifies
           the pointer-encoding (PE) that is used for the
           code-pointers.  Note: the "address_range" member is always
-	  encoded as an absolute value.  Apart from that, the specified
-	  FDE pointer-encoding applies.
+          encoded as an absolute value.  Apart from that, the specified
+          FDE pointer-encoding applies.
 
      'P': Indicates the presence of a personality routine (handler).
           The first operand for this character specifies the
-	  pointer-encoding (PE) that is used for the second operand,
-	  which specifies the address of the personality routine.
+          pointer-encoding (PE) that is used for the second operand,
+          which specifies the address of the personality routine.
 
     If the augmentation string contains any other characters, the
     remainder of the augmentation string should be ignored.
@@ -104,25 +104,25 @@
 
 */
 
-#define DW_EH_VERSION		1	/* The version we're implementing */
+#define DW_EH_VERSION           1       /* The version we're implementing */
 
-struct dwarf_eh_frame_hdr
-{
+struct __attribute__((packed)) dwarf_eh_frame_hdr
+  {
     unsigned char version;
     unsigned char eh_frame_ptr_enc;
     unsigned char fde_count_enc;
     unsigned char table_enc;
+    Elf_W (Addr) eh_frame;
     /* The rest of the header is variable-length and consists of the
        following members:
 
-    encoded_t eh_frame_ptr;
-    encoded_t fde_count;
-    struct
-      {
-        encoded_t start_ip;	// first address covered by this FDE
-        encoded_t fde_addr;	// address of the FDE
-      }
-    binary_search_table[fde_count];  */
-};
+        encoded_t fde_count;
+        struct
+          {
+            encoded_t start_ip; // first address covered by this FDE
+            encoded_t fde_addr; // address of the FDE
+          }
+        binary_search_table[fde_count];  */
+  };
 
 #endif /* dwarf_eh_h */
diff --git a/sgx_unwind/libunwind/include/dwarf.h b/sgx_unwind/libunwind/include/dwarf.h
index 973b6cf..fab93c6 100644
--- a/sgx_unwind/libunwind/include/dwarf.h
+++ b/sgx_unwind/libunwind/include/dwarf.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -26,77 +26,76 @@
 #ifndef dwarf_h
 #define dwarf_h
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#ifdef HAVE_ATOMIC_OPS_H
-# include <atomic_ops.h>
-#endif
-
 #include <libunwind.h>
 
-struct dwarf_cursor;	/* forward-declaration */
+struct dwarf_cursor;    /* forward-declaration */
 struct elf_dyn_info;
 
 #include "dwarf-config.h"
-#ifndef UNW_REMOTE_ONLY
-#include <link.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
 #endif
 
-#if HAVE_SGX
-#include "pthread_compat.h"
-#else
-#include <pthread.h>
+#ifndef UNW_REMOTE_ONLY
+  #if defined(HAVE_LINK_H)
+    #include <link.h>
+  #elif defined(HAVE_SYS_LINK_H)
+    #include <sys/link.h>
+  #else
+    #error Could not find <link.h>
+  #endif
 #endif
 
+#include <pthread.h>
+
 /* DWARF expression opcodes.  */
 
 typedef enum
-{
-    DW_OP_addr			= 0x03,
-    DW_OP_deref			= 0x06,
-    DW_OP_const1u		= 0x08,
-    DW_OP_const1s		= 0x09,
-    DW_OP_const2u		= 0x0a,
-    DW_OP_const2s		= 0x0b,
-    DW_OP_const4u		= 0x0c,
-    DW_OP_const4s		= 0x0d,
-    DW_OP_const8u		= 0x0e,
-    DW_OP_const8s		= 0x0f,
-    DW_OP_constu		= 0x10,
-    DW_OP_consts		= 0x11,
-    DW_OP_dup			= 0x12,
-    DW_OP_drop			= 0x13,
-    DW_OP_over			= 0x14,
-    DW_OP_pick			= 0x15,
-    DW_OP_swap			= 0x16,
-    DW_OP_rot			= 0x17,
-    DW_OP_xderef		= 0x18,
-    DW_OP_abs			= 0x19,
-    DW_OP_and			= 0x1a,
-    DW_OP_div			= 0x1b,
-    DW_OP_minus			= 0x1c,
-    DW_OP_mod			= 0x1d,
-    DW_OP_mul			= 0x1e,
-    DW_OP_neg			= 0x1f,
-    DW_OP_not			= 0x20,
-    DW_OP_or			= 0x21,
-    DW_OP_plus			= 0x22,
-    DW_OP_plus_uconst		= 0x23,
-    DW_OP_shl			= 0x24,
-    DW_OP_shr			= 0x25,
-    DW_OP_shra			= 0x26,
-    DW_OP_xor			= 0x27,
-    DW_OP_skip			= 0x2f,
-    DW_OP_bra			= 0x28,
-    DW_OP_eq			= 0x29,
-    DW_OP_ge			= 0x2a,
-    DW_OP_gt			= 0x2b,
-    DW_OP_le			= 0x2c,
-    DW_OP_lt			= 0x2d,
-    DW_OP_ne			= 0x2e,
-    DW_OP_lit0			= 0x30,
+  {
+    DW_OP_addr                  = 0x03,
+    DW_OP_deref                 = 0x06,
+    DW_OP_const1u               = 0x08,
+    DW_OP_const1s               = 0x09,
+    DW_OP_const2u               = 0x0a,
+    DW_OP_const2s               = 0x0b,
+    DW_OP_const4u               = 0x0c,
+    DW_OP_const4s               = 0x0d,
+    DW_OP_const8u               = 0x0e,
+    DW_OP_const8s               = 0x0f,
+    DW_OP_constu                = 0x10,
+    DW_OP_consts                = 0x11,
+    DW_OP_dup                   = 0x12,
+    DW_OP_drop                  = 0x13,
+    DW_OP_over                  = 0x14,
+    DW_OP_pick                  = 0x15,
+    DW_OP_swap                  = 0x16,
+    DW_OP_rot                   = 0x17,
+    DW_OP_xderef                = 0x18,
+    DW_OP_abs                   = 0x19,
+    DW_OP_and                   = 0x1a,
+    DW_OP_div                   = 0x1b,
+    DW_OP_minus                 = 0x1c,
+    DW_OP_mod                   = 0x1d,
+    DW_OP_mul                   = 0x1e,
+    DW_OP_neg                   = 0x1f,
+    DW_OP_not                   = 0x20,
+    DW_OP_or                    = 0x21,
+    DW_OP_plus                  = 0x22,
+    DW_OP_plus_uconst           = 0x23,
+    DW_OP_shl                   = 0x24,
+    DW_OP_shr                   = 0x25,
+    DW_OP_shra                  = 0x26,
+    DW_OP_xor                   = 0x27,
+    DW_OP_skip                  = 0x2f,
+    DW_OP_bra                   = 0x28,
+    DW_OP_eq                    = 0x29,
+    DW_OP_ge                    = 0x2a,
+    DW_OP_gt                    = 0x2b,
+    DW_OP_le                    = 0x2c,
+    DW_OP_lt                    = 0x2d,
+    DW_OP_ne                    = 0x2e,
+    DW_OP_lit0                  = 0x30,
     DW_OP_lit1,  DW_OP_lit2,  DW_OP_lit3,  DW_OP_lit4,  DW_OP_lit5,
     DW_OP_lit6,  DW_OP_lit7,  DW_OP_lit8,  DW_OP_lit9,  DW_OP_lit10,
     DW_OP_lit11, DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
@@ -104,7 +103,7 @@
     DW_OP_lit21, DW_OP_lit22, DW_OP_lit23, DW_OP_lit24, DW_OP_lit25,
     DW_OP_lit26, DW_OP_lit27, DW_OP_lit28, DW_OP_lit29, DW_OP_lit30,
     DW_OP_lit31,
-    DW_OP_reg0			= 0x50,
+    DW_OP_reg0                  = 0x50,
     DW_OP_reg1,  DW_OP_reg2,  DW_OP_reg3,  DW_OP_reg4,  DW_OP_reg5,
     DW_OP_reg6,  DW_OP_reg7,  DW_OP_reg8,  DW_OP_reg9,  DW_OP_reg10,
     DW_OP_reg11, DW_OP_reg12, DW_OP_reg13, DW_OP_reg14, DW_OP_reg15,
@@ -112,7 +111,7 @@
     DW_OP_reg21, DW_OP_reg22, DW_OP_reg23, DW_OP_reg24, DW_OP_reg25,
     DW_OP_reg26, DW_OP_reg27, DW_OP_reg28, DW_OP_reg29, DW_OP_reg30,
     DW_OP_reg31,
-    DW_OP_breg0			= 0x70,
+    DW_OP_breg0                 = 0x70,
     DW_OP_breg1,  DW_OP_breg2,  DW_OP_breg3,  DW_OP_breg4,  DW_OP_breg5,
     DW_OP_breg6,  DW_OP_breg7,  DW_OP_breg8,  DW_OP_breg9,  DW_OP_breg10,
     DW_OP_breg11, DW_OP_breg12, DW_OP_breg13, DW_OP_breg14, DW_OP_breg15,
@@ -120,59 +119,61 @@
     DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25,
     DW_OP_breg26, DW_OP_breg27, DW_OP_breg28, DW_OP_breg29, DW_OP_breg30,
     DW_OP_breg31,
-    DW_OP_regx			= 0x90,
-    DW_OP_fbreg			= 0x91,
-    DW_OP_bregx			= 0x92,
-    DW_OP_piece			= 0x93,
-    DW_OP_deref_size		= 0x94,
-    DW_OP_xderef_size		= 0x95,
-    DW_OP_nop			= 0x96,
-    DW_OP_push_object_address	= 0x97,
-    DW_OP_call2			= 0x98,
-    DW_OP_call4			= 0x99,
-    DW_OP_call_ref		= 0x9a,
-    DW_OP_lo_user		= 0xe0,
-    DW_OP_hi_user		= 0xff
-}
+    DW_OP_regx                  = 0x90,
+    DW_OP_fbreg                 = 0x91,
+    DW_OP_bregx                 = 0x92,
+    DW_OP_piece                 = 0x93,
+    DW_OP_deref_size            = 0x94,
+    DW_OP_xderef_size           = 0x95,
+    DW_OP_nop                   = 0x96,
+    DW_OP_push_object_address   = 0x97,
+    DW_OP_call2                 = 0x98,
+    DW_OP_call4                 = 0x99,
+    DW_OP_call_ref              = 0x9a,
+    DW_OP_lo_user               = 0xe0,
+    DW_OP_hi_user               = 0xff
+  }
 dwarf_expr_op_t;
 
-#define DWARF_CIE_VERSION	3	/* GCC emits version 1??? */
+#define DWARF_CIE_VERSION       3
+#define DWARF_CIE_VERSION_MAX   4
 
-#define DWARF_CFA_OPCODE_MASK	0xc0
-#define DWARF_CFA_OPERAND_MASK	0x3f
+#define DWARF_CFA_OPCODE_MASK   0xc0
+#define DWARF_CFA_OPERAND_MASK  0x3f
 
 typedef enum
-{
-    DW_CFA_advance_loc		= 0x40,
-    DW_CFA_offset		= 0x80,
-    DW_CFA_restore		= 0xc0,
-    DW_CFA_nop			= 0x00,
-    DW_CFA_set_loc		= 0x01,
-    DW_CFA_advance_loc1		= 0x02,
-    DW_CFA_advance_loc2		= 0x03,
-    DW_CFA_advance_loc4		= 0x04,
-    DW_CFA_offset_extended	= 0x05,
-    DW_CFA_restore_extended	= 0x06,
-    DW_CFA_undefined		= 0x07,
-    DW_CFA_same_value		= 0x08,
-    DW_CFA_register		= 0x09,
-    DW_CFA_remember_state	= 0x0a,
-    DW_CFA_restore_state	= 0x0b,
-    DW_CFA_def_cfa		= 0x0c,
-    DW_CFA_def_cfa_register	= 0x0d,
-    DW_CFA_def_cfa_offset	= 0x0e,
-    DW_CFA_def_cfa_expression	= 0x0f,
-    DW_CFA_expression		= 0x10,
-    DW_CFA_offset_extended_sf	= 0x11,
-    DW_CFA_def_cfa_sf		= 0x12,
-    DW_CFA_def_cfa_offset_sf	= 0x13,
-    DW_CFA_lo_user		= 0x1c,
-    DW_CFA_MIPS_advance_loc8	= 0x1d,
-    DW_CFA_GNU_window_save	= 0x2d,
-    DW_CFA_GNU_args_size	= 0x2e,
-    DW_CFA_GNU_negative_offset_extended	= 0x2f,
-    DW_CFA_hi_user		= 0x3c
-}
+  {
+    DW_CFA_advance_loc          = 0x40,
+    DW_CFA_offset               = 0x80,
+    DW_CFA_restore              = 0xc0,
+    DW_CFA_nop                  = 0x00,
+    DW_CFA_set_loc              = 0x01,
+    DW_CFA_advance_loc1         = 0x02,
+    DW_CFA_advance_loc2         = 0x03,
+    DW_CFA_advance_loc4         = 0x04,
+    DW_CFA_offset_extended      = 0x05,
+    DW_CFA_restore_extended     = 0x06,
+    DW_CFA_undefined            = 0x07,
+    DW_CFA_same_value           = 0x08,
+    DW_CFA_register             = 0x09,
+    DW_CFA_remember_state       = 0x0a,
+    DW_CFA_restore_state        = 0x0b,
+    DW_CFA_def_cfa              = 0x0c,
+    DW_CFA_def_cfa_register     = 0x0d,
+    DW_CFA_def_cfa_offset       = 0x0e,
+    DW_CFA_def_cfa_expression   = 0x0f,
+    DW_CFA_expression           = 0x10,
+    DW_CFA_offset_extended_sf   = 0x11,
+    DW_CFA_def_cfa_sf           = 0x12,
+    DW_CFA_def_cfa_offset_sf    = 0x13,
+    DW_CFA_val_expression       = 0x16,
+    DW_CFA_lo_user              = 0x1c,
+    DW_CFA_MIPS_advance_loc8    = 0x1d,
+    DW_CFA_GNU_window_save      = 0x2d,
+    DW_CFA_GNU_args_size        = 0x2e,
+    DW_CFA_GNU_negative_offset_extended = 0x2f,
+    DW_CFA_hi_user              = 0x3c
+  }
 dwarf_cfa_t;
 
 /* DWARF Pointer-Encoding (PEs).
@@ -186,55 +187,49 @@
    engineered from GCC.
 
 */
-#define DW_EH_PE_FORMAT_MASK	0x0f	/* format of the encoded value */
-#define DW_EH_PE_APPL_MASK	0x70	/* how the value is to be applied */
+#define DW_EH_PE_FORMAT_MASK    0x0f    /* format of the encoded value */
+#define DW_EH_PE_APPL_MASK      0x70    /* how the value is to be applied */
 /* Flag bit.  If set, the resulting pointer is the address of the word
    that contains the final address.  */
-#define DW_EH_PE_indirect	0x80
+#define DW_EH_PE_indirect       0x80
 
 /* Pointer-encoding formats: */
-#define DW_EH_PE_omit		0xff
-#define DW_EH_PE_ptr		0x00	/* pointer-sized unsigned value */
-#define DW_EH_PE_uleb128	0x01	/* unsigned LE base-128 value */
-#define DW_EH_PE_udata2		0x02	/* unsigned 16-bit value */
-#define DW_EH_PE_udata4		0x03	/* unsigned 32-bit value */
-#define DW_EH_PE_udata8		0x04	/* unsigned 64-bit value */
-#define DW_EH_PE_sleb128	0x09	/* signed LE base-128 value */
-#define DW_EH_PE_sdata2		0x0a	/* signed 16-bit value */
-#define DW_EH_PE_sdata4		0x0b	/* signed 32-bit value */
-#define DW_EH_PE_sdata8		0x0c	/* signed 64-bit value */
+#define DW_EH_PE_omit           0xff
+#define DW_EH_PE_ptr            0x00    /* pointer-sized unsigned value */
+#define DW_EH_PE_uleb128        0x01    /* unsigned LE base-128 value */
+#define DW_EH_PE_udata2         0x02    /* unsigned 16-bit value */
+#define DW_EH_PE_udata4         0x03    /* unsigned 32-bit value */
+#define DW_EH_PE_udata8         0x04    /* unsigned 64-bit value */
+#define DW_EH_PE_sleb128        0x09    /* signed LE base-128 value */
+#define DW_EH_PE_sdata2         0x0a    /* signed 16-bit value */
+#define DW_EH_PE_sdata4         0x0b    /* signed 32-bit value */
+#define DW_EH_PE_sdata8         0x0c    /* signed 64-bit value */
 
 /* Pointer-encoding application: */
-#define DW_EH_PE_absptr		0x00	/* absolute value */
-#define DW_EH_PE_pcrel		0x10	/* rel. to addr. of encoded value */
-#define DW_EH_PE_textrel	0x20	/* text-relative (GCC-specific???) */
-#define DW_EH_PE_datarel	0x30	/* data-relative */
+#define DW_EH_PE_absptr         0x00    /* absolute value */
+#define DW_EH_PE_pcrel          0x10    /* rel. to addr. of encoded value */
+#define DW_EH_PE_textrel        0x20    /* text-relative (GCC-specific???) */
+#define DW_EH_PE_datarel        0x30    /* data-relative */
 /* The following are not documented by LSB v1.3, yet they are used by
    GCC, presumably they aren't documented by LSB since they aren't
    used on Linux:  */
-#define DW_EH_PE_funcrel	0x40	/* start-of-procedure-relative */
-#define DW_EH_PE_aligned	0x50	/* aligned pointer */
+#define DW_EH_PE_funcrel        0x40    /* start-of-procedure-relative */
+#define DW_EH_PE_aligned        0x50    /* aligned pointer */
 
 extern struct mempool dwarf_reg_state_pool;
 extern struct mempool dwarf_cie_info_pool;
 
 typedef enum
-{
-    DWARF_WHERE_UNDEF,		/* register isn't saved at all */
-    DWARF_WHERE_SAME,		/* register has same value as in prev. frame */
-    DWARF_WHERE_CFAREL,		/* register saved at CFA-relative address */
-    DWARF_WHERE_REG,		/* register saved in another register */
-    DWARF_WHERE_EXPR,		/* register saved */
-}
+  {
+    DWARF_WHERE_UNDEF,          /* register isn't saved at all */
+    DWARF_WHERE_SAME,           /* register has same value as in prev. frame */
+    DWARF_WHERE_CFAREL,         /* register saved at CFA-relative address */
+    DWARF_WHERE_REG,            /* register saved in another register */
+    DWARF_WHERE_EXPR,           /* register saved */
+    DWARF_WHERE_VAL_EXPR,       /* register has computed value */
+  }
 dwarf_where_t;
 
-typedef struct
-{
-    dwarf_where_t where;	/* how is the register saved? */
-    unw_word_t val;		/* where it's saved */
-}
-dwarf_save_loc_t;
-
 /* For uniformity, we'd like to treat the CFA save-location like any
    other register save-location, but this doesn't quite work, because
    the CFA can be expressed as a (REGISTER,OFFSET) pair.  To handle
@@ -245,33 +240,50 @@
    case of DWARF_WHERE_REG, member "val" gives the number of the
    base-register and the "val" member of DWARF_CFA_OFF_COLUMN gives
    the offset value.  */
-#define DWARF_CFA_REG_COLUMN	DWARF_NUM_PRESERVED_REGS
-#define DWARF_CFA_OFF_COLUMN	(DWARF_NUM_PRESERVED_REGS + 1)
+#define DWARF_CFA_REG_COLUMN    DWARF_NUM_PRESERVED_REGS
+#define DWARF_CFA_OFF_COLUMN    (DWARF_NUM_PRESERVED_REGS + 1)
+
+typedef struct dwarf_reg_only_state
+  {
+    char where[DWARF_NUM_PRESERVED_REGS + 2];        /* how is the register saved? */
+    unw_word_t val[DWARF_NUM_PRESERVED_REGS + 2];             /* where it's saved */
+  }
+dwarf_reg_only_state_t;
 
 typedef struct dwarf_reg_state
-{
-    struct dwarf_reg_state *next;	/* for rs_stack */
-    dwarf_save_loc_t reg[DWARF_NUM_PRESERVED_REGS + 2];
-    unw_word_t ip;		          /* ip this rs is for */
-    unw_word_t ret_addr_column;           /* indicates which column in the rule table represents return address */
-    unsigned short lru_chain;	  /* used for least-recently-used chain */
-    unsigned short coll_chain;	/* used for hash collisions */
-    unsigned short hint;	      /* hint for next rs to try (or -1) */
-    unsigned short valid : 1;         /* optional machine-dependent signal info */
-    unsigned short signal_frame : 1;  /* optional machine-dependent signal info */
-}
+  {
+    unw_word_t ret_addr_column;	/* which column in rule table represents return address */
+    dwarf_reg_only_state_t reg;
+  }
 dwarf_reg_state_t;
 
+typedef struct dwarf_stackable_reg_state
+  {
+    struct dwarf_stackable_reg_state *next;       /* for rs_stack */
+    dwarf_reg_state_t state;
+  }
+dwarf_stackable_reg_state_t;
+
+typedef struct dwarf_reg_cache_entry
+  {
+    unw_word_t ip;                        /* ip this rs is for */
+    unsigned short coll_chain;  /* used for hash collisions */
+    unsigned short hint;              /* hint for next rs to try (or -1) */
+    unsigned short valid : 1;         /* optional machine-dependent signal info */
+    unsigned short signal_frame : 1;  /* optional machine-dependent signal info */
+  }
+dwarf_reg_cache_entry_t;
+
 typedef struct dwarf_cie_info
-{
-    unw_word_t cie_instr_start;	/* start addr. of CIE "initial_instructions" */
-    unw_word_t cie_instr_end;	/* end addr. of CIE "initial_instructions" */
-    unw_word_t fde_instr_start;	/* start addr. of FDE "instructions" */
-    unw_word_t fde_instr_end;	/* end addr. of FDE "instructions" */
-    unw_word_t code_align;	/* code-alignment factor */
-    unw_word_t data_align;	/* data-alignment factor */
-    unw_word_t ret_addr_column;	/* column of return-address register */
-    unw_word_t handler;		/* address of personality-routine */
+  {
+    unw_word_t cie_instr_start; /* start addr. of CIE "initial_instructions" */
+    unw_word_t cie_instr_end;   /* end addr. of CIE "initial_instructions" */
+    unw_word_t fde_instr_start; /* start addr. of FDE "instructions" */
+    unw_word_t fde_instr_end;   /* end addr. of FDE "instructions" */
+    unw_word_t code_align;      /* code-alignment factor */
+    unw_word_t data_align;      /* data-alignment factor */
+    unw_word_t ret_addr_column; /* column of return-address register */
+    unw_word_t handler;         /* address of personality-routine */
     uint16_t abi;
     uint16_t tag;
     uint8_t fde_encoding;
@@ -279,28 +291,27 @@
     unsigned int sized_augmentation : 1;
     unsigned int have_abi_marker : 1;
     unsigned int signal_frame : 1;
-}
+  }
 dwarf_cie_info_t;
 
 typedef struct dwarf_state_record
-{
+  {
     unsigned char fde_encoding;
     unw_word_t args_size;
 
-    dwarf_reg_state_t rs_initial;	/* reg-state after CIE instructions */
-    dwarf_reg_state_t rs_current;	/* current reg-state */
-}
+    dwarf_reg_state_t rs_initial;       /* reg-state after CIE instructions */
+    dwarf_reg_state_t rs_current;       /* current reg-state */
+  }
 dwarf_state_record_t;
 
 typedef struct dwarf_cursor
-{
-    void *as_arg;		/* argument to address-space callbacks */
-    unw_addr_space_t as;	/* reference to per-address-space info */
+  {
+    void *as_arg;               /* argument to address-space callbacks */
+    unw_addr_space_t as;        /* reference to per-address-space info */
 
-    unw_word_t cfa;	/* canonical frame address; aka frame-/stack-pointer */
-    unw_word_t ip;		/* instruction pointer */
-    unw_word_t args_size;	/* size of arguments */
-    unw_word_t ret_addr_column;	/* column for return-address */
+    unw_word_t cfa;     /* canonical frame address; aka frame-/stack-pointer */
+    unw_word_t ip;              /* instruction pointer */
+    unw_word_t args_size;       /* size of arguments */
     unw_word_t eh_args[UNW_TDEP_NUM_EH_REGS];
     unsigned int eh_valid_mask;
 
@@ -308,42 +319,50 @@
 
     unsigned int stash_frames :1; /* stash frames for fast lookup */
     unsigned int use_prev_instr :1; /* use previous (= call) or current (= signal) instruction? */
-    unsigned int pi_valid :1;	/* is proc_info valid? */
+    unsigned int pi_valid :1;   /* is proc_info valid? */
     unsigned int pi_is_dynamic :1; /* proc_info found via dynamic proc info? */
-    unw_proc_info_t pi;		/* info about current procedure */
+    unw_proc_info_t pi;         /* info about current procedure */
 
     short hint; /* faster lookup of the rs cache */
     short prev_rs;
-}
+  }
 dwarf_cursor_t;
 
-#define DWARF_LOG_UNW_CACHE_SIZE	7
-#define DWARF_UNW_CACHE_SIZE	(1 << DWARF_LOG_UNW_CACHE_SIZE)
+#define DWARF_DEFAULT_LOG_UNW_CACHE_SIZE        7
+#define DWARF_DEFAULT_UNW_CACHE_SIZE    (1 << DWARF_DEFAULT_LOG_UNW_CACHE_SIZE)
 
-#define DWARF_LOG_UNW_HASH_SIZE	(DWARF_LOG_UNW_CACHE_SIZE + 1)
-#define DWARF_UNW_HASH_SIZE	(1 << DWARF_LOG_UNW_HASH_SIZE)
+#define DWARF_DEFAULT_LOG_UNW_HASH_SIZE (DWARF_DEFAULT_LOG_UNW_CACHE_SIZE + 1)
+#define DWARF_DEFAULT_UNW_HASH_SIZE     (1 << DWARF_DEFAULT_LOG_UNW_HASH_SIZE)
 
 typedef unsigned char unw_hash_index_t;
 
 struct dwarf_rs_cache
-{
+  {
     pthread_mutex_t lock;
-    unsigned short lru_head;	/* index of lead-recently used rs */
-    unsigned short lru_tail;	/* index of most-recently used rs */
+    unsigned short rr_head;    /* index of least-recently allocated rs */
+
+    unsigned short log_size;
+    unsigned short prev_log_size;
 
     /* hash table that maps instruction pointer to rs index: */
-    unsigned short hash[DWARF_UNW_HASH_SIZE];
+    unsigned short *hash;
 
-    uint32_t generation;	/* generation number */
+    uint32_t generation;        /* generation number */
 
     /* rs cache: */
-    dwarf_reg_state_t buckets[DWARF_UNW_CACHE_SIZE];
-};
+    dwarf_reg_state_t *buckets;
+    dwarf_reg_cache_entry_t *links;
+
+    /* default memory, loaded in BSS segment */
+    unsigned short default_hash[DWARF_DEFAULT_UNW_HASH_SIZE];
+    dwarf_reg_state_t default_buckets[DWARF_DEFAULT_UNW_CACHE_SIZE];
+    dwarf_reg_cache_entry_t default_links[DWARF_DEFAULT_UNW_CACHE_SIZE];
+  };
 
 /* A list of descriptors for loaded .debug_frame sections.  */
 
 struct unw_debug_frame_list
-{
+  {
     /* The start (inclusive) and end (exclusive) of the described region.  */
     unw_word_t start;
     unw_word_t end;
@@ -355,37 +374,28 @@
     size_t index_size;
     /* Pointer to next descriptor.  */
     struct unw_debug_frame_list *next;
-};
-
-struct dwarf_callback_data
-{
-    /* in: */
-    unw_word_t ip;		/* instruction-pointer we're looking for */
-    unw_proc_info_t *pi;	/* proc-info pointer */
-    int need_unwind_info;
-    /* out: */
-    int single_fde;		/* did we find a single FDE? (vs. a table) */
-    unw_dyn_info_t di;		/* table info (if single_fde is false) */
-    unw_dyn_info_t di_debug;	/* additional table info for .debug_frame */
-};
+  };
 
 /* Convenience macros: */
-#define dwarf_init			UNW_ARCH_OBJ (dwarf_init)
-#define dwarf_callback			UNW_OBJ (dwarf_callback)
-#define dwarf_find_proc_info		UNW_OBJ (dwarf_find_proc_info)
-#define dwarf_find_debug_frame		UNW_OBJ (dwarf_find_debug_frame)
-#define dwarf_search_unwind_table	UNW_OBJ (dwarf_search_unwind_table)
-#define dwarf_find_unwind_table		UNW_OBJ (dwarf_find_unwind_table)
-#define dwarf_put_unwind_info		UNW_OBJ (dwarf_put_unwind_info)
-#define dwarf_put_unwind_info		UNW_OBJ (dwarf_put_unwind_info)
-#define dwarf_eval_expr			UNW_OBJ (dwarf_eval_expr)
+#define dwarf_init                      UNW_ARCH_OBJ (dwarf_init)
+#define dwarf_callback                  UNW_OBJ (dwarf_callback)
+#define dwarf_find_proc_info            UNW_OBJ (dwarf_find_proc_info)
+#define dwarf_find_debug_frame          UNW_OBJ (dwarf_find_debug_frame)
+#define dwarf_search_unwind_table       UNW_OBJ (dwarf_search_unwind_table)
+#define dwarf_find_unwind_table         UNW_OBJ (dwarf_find_unwind_table)
+#define dwarf_put_unwind_info           UNW_OBJ (dwarf_put_unwind_info)
+#define dwarf_put_unwind_info           UNW_OBJ (dwarf_put_unwind_info)
+#define dwarf_eval_expr                 UNW_OBJ (dwarf_eval_expr)
+#define dwarf_stack_aligned             UNW_OBJ (dwarf_stack_aligned)
 #define dwarf_extract_proc_info_from_fde \
-		UNW_OBJ (dwarf_extract_proc_info_from_fde)
-#define dwarf_find_save_locs		UNW_OBJ (dwarf_find_save_locs)
-#define dwarf_create_state_record	UNW_OBJ (dwarf_create_state_record)
-#define dwarf_make_proc_info		UNW_OBJ (dwarf_make_proc_info)
-#define dwarf_read_encoded_pointer	UNW_OBJ (dwarf_read_encoded_pointer)
-#define dwarf_step			UNW_OBJ (dwarf_step)
+                UNW_OBJ (dwarf_extract_proc_info_from_fde)
+#define dwarf_find_save_locs            UNW_OBJ (dwarf_find_save_locs)
+#define dwarf_make_proc_info            UNW_OBJ (dwarf_make_proc_info)
+#define dwarf_apply_reg_state           UNW_OBJ (dwarf_apply_reg_state)
+#define dwarf_reg_states_iterate        UNW_OBJ (dwarf_reg_states_iterate)
+#define dwarf_read_encoded_pointer      UNW_OBJ (dwarf_read_encoded_pointer)
+#define dwarf_step                      UNW_OBJ (dwarf_step)
+#define dwarf_flush_rs_cache            UNW_OBJ (dwarf_flush_rs_cache)
 
 extern int dwarf_init (void);
 #ifndef UNW_REMOTE_ONLY
@@ -403,6 +413,7 @@
                                       unw_dyn_info_t *di,
                                       unw_proc_info_t *pi,
                                       int need_unwind_info, void *arg);
+
 extern int dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
                                     char *path, unw_word_t segbase, unw_word_t mapoff,
                                     unw_word_t ip);
@@ -411,17 +422,22 @@
 extern int dwarf_eval_expr (struct dwarf_cursor *c, unw_word_t *addr,
                             unw_word_t len, unw_word_t *valp,
                             int *is_register);
+extern int
+dwarf_stack_aligned(struct dwarf_cursor *c, unw_word_t cfa_addr,
+                    unw_word_t rbp_addr, unw_word_t *offset);
+
 extern int dwarf_extract_proc_info_from_fde (unw_addr_space_t as,
-        unw_accessors_t *a,
-        unw_word_t *fde_addr,
-        unw_proc_info_t *pi,
-        int need_unwind_info,
-        unw_word_t base,
-        void *arg);
+                                             unw_accessors_t *a,
+                                             unw_word_t *fde_addr,
+                                             unw_proc_info_t *pi,
+                                             unw_word_t base,
+                                             int need_unwind_info,
+                                             int is_debug_frame,
+                                             void *arg);
 extern int dwarf_find_save_locs (struct dwarf_cursor *c);
-extern int dwarf_create_state_record (struct dwarf_cursor *c,
-                                      dwarf_state_record_t *sr);
 extern int dwarf_make_proc_info (struct dwarf_cursor *c);
+extern int dwarf_apply_reg_state (struct dwarf_cursor *c, struct dwarf_reg_state *rs);
+extern int dwarf_reg_states_iterate (struct dwarf_cursor *c, unw_reg_states_callback cb, void *token);
 extern int dwarf_read_encoded_pointer (unw_addr_space_t as,
                                        unw_accessors_t *a,
                                        unw_word_t *addr,
@@ -429,5 +445,6 @@
                                        const unw_proc_info_t *pi,
                                        unw_word_t *valp, void *arg);
 extern int dwarf_step (struct dwarf_cursor *c);
+extern int dwarf_flush_rs_cache (struct dwarf_rs_cache *cache);
 
 #endif /* dwarf_h */
diff --git a/sgx_unwind/libunwind/include/dwarf_i.h b/sgx_unwind/libunwind/include/dwarf_i.h
index 11843a5..983b9f5 100644
--- a/sgx_unwind/libunwind/include/dwarf_i.h
+++ b/sgx_unwind/libunwind/include/dwarf_i.h
@@ -15,21 +15,21 @@
 # define dwarf_addr_size(as) (sizeof (unw_word_t))
 #endif
 
-#define dwarf_to_unw_regnum_map		UNW_OBJ (dwarf_to_unw_regnum_map)
-
-extern uint8_t dwarf_to_unw_regnum_map[DWARF_REGNUM_MAP_LENGTH];
-
+#ifndef dwarf_to_unw_regnum
+# define dwarf_to_unw_regnum_map                UNW_OBJ (dwarf_to_unw_regnum_map)
+extern const uint8_t dwarf_to_unw_regnum_map[DWARF_REGNUM_MAP_LENGTH];
 /* REG is evaluated multiple times; it better be side-effects free!  */
-#define dwarf_to_unw_regnum(reg)					  \
-  (((reg) <= DWARF_REGNUM_MAP_LENGTH) ? dwarf_to_unw_regnum_map[reg] : 0)
+# define dwarf_to_unw_regnum(reg)                                         \
+  (((reg) < DWARF_REGNUM_MAP_LENGTH) ? dwarf_to_unw_regnum_map[reg] : 0)
+#endif
 
 #ifdef UNW_LOCAL_ONLY
 
 /* In the local-only case, we can let the compiler directly access
    memory and don't need to worry about differing byte-order.  */
 
-union __attribute__ ((packed)) _dwarf_misaligned_value_t
-{
+typedef union __attribute__ ((packed))
+  {
     int8_t s8;
     int16_t s16;
     int32_t s32;
@@ -39,95 +39,95 @@
     uint32_t u32;
     uint64_t u64;
     void *ptr;
-};
-typedef union _dwarf_misaligned_value_t dwarf_misaligned_value_t;
+  }
+dwarf_misaligned_value_t;
 
 static inline int
 dwarf_reads8 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
               int8_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->s8;
-    *addr += sizeof (mvp->s8);
-    return 0;
+  *val = mvp->s8;
+  *addr += sizeof (mvp->s8);
+  return 0;
 }
 
 static inline int
 dwarf_reads16 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int16_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->s16;
-    *addr += sizeof (mvp->s16);
-    return 0;
+  *val = mvp->s16;
+  *addr += sizeof (mvp->s16);
+  return 0;
 }
 
 static inline int
 dwarf_reads32 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int32_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->s32;
-    *addr += sizeof (mvp->s32);
-    return 0;
+  *val = mvp->s32;
+  *addr += sizeof (mvp->s32);
+  return 0;
 }
 
 static inline int
 dwarf_reads64 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int64_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->s64;
-    *addr += sizeof (mvp->s64);
-    return 0;
+  *val = mvp->s64;
+  *addr += sizeof (mvp->s64);
+  return 0;
 }
 
 static inline int
 dwarf_readu8 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
               uint8_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->u8;
-    *addr += sizeof (mvp->u8);
-    return 0;
+  *val = mvp->u8;
+  *addr += sizeof (mvp->u8);
+  return 0;
 }
 
 static inline int
 dwarf_readu16 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint16_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->u16;
-    *addr += sizeof (mvp->u16);
-    return 0;
+  *val = mvp->u16;
+  *addr += sizeof (mvp->u16);
+  return 0;
 }
 
 static inline int
 dwarf_readu32 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint32_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->u32;
-    *addr += sizeof (mvp->u32);
-    return 0;
+  *val = mvp->u32;
+  *addr += sizeof (mvp->u32);
+  return 0;
 }
 
 static inline int
 dwarf_readu64 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint64_t *val, void *arg)
 {
-    dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
+  dwarf_misaligned_value_t *mvp = (void *) (uintptr_t) *addr;
 
-    *val = mvp->u64;
-    *addr += sizeof (mvp->u64);
-    return 0;
+  *val = mvp->u64;
+  *addr += sizeof (mvp->u64);
+  return 0;
 }
 
 #else /* !UNW_LOCAL_ONLY */
@@ -136,125 +136,125 @@
 dwarf_readu8 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
               uint8_t *valp, void *arg)
 {
-    unw_word_t val, aligned_addr = *addr & -sizeof (unw_word_t);
-    unw_word_t off = *addr - aligned_addr;
-    int ret;
+  unw_word_t val, aligned_addr = *addr & -sizeof (unw_word_t);
+  unw_word_t off = *addr - aligned_addr;
+  int ret;
 
-    *addr += 1;
-    ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
+  *addr += 1;
+  ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-    val >>= 8*off;
+  val >>= 8*off;
 #else
-    val >>= 8*(sizeof (unw_word_t) - 1 - off);
+  val >>= 8*(sizeof (unw_word_t) - 1 - off);
 #endif
-    *valp = (uint8_t) val;
-    return ret;
+  *valp = (uint8_t) val;
+  return ret;
 }
 
 static inline int
 dwarf_readu16 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint16_t *val, void *arg)
 {
-    uint8_t v0, v1;
-    int ret;
+  uint8_t v0, v1;
+  int ret;
 
-    if ((ret = dwarf_readu8 (as, a, addr, &v0, arg)) < 0
-            || (ret = dwarf_readu8 (as, a, addr, &v1, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_readu8 (as, a, addr, &v0, arg)) < 0
+      || (ret = dwarf_readu8 (as, a, addr, &v1, arg)) < 0)
+    return ret;
 
-    if (tdep_big_endian (as))
-        *val = (uint16_t) v0 << 8 | v1;
-    else
-        *val = (uint16_t) v1 << 8 | v0;
-    return 0;
+  if (tdep_big_endian (as))
+    *val = (uint16_t) v0 << 8 | v1;
+  else
+    *val = (uint16_t) v1 << 8 | v0;
+  return 0;
 }
 
 static inline int
 dwarf_readu32 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint32_t *val, void *arg)
 {
-    uint16_t v0, v1;
-    int ret;
+  uint16_t v0, v1;
+  int ret;
 
-    if ((ret = dwarf_readu16 (as, a, addr, &v0, arg)) < 0
-            || (ret = dwarf_readu16 (as, a, addr, &v1, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_readu16 (as, a, addr, &v0, arg)) < 0
+      || (ret = dwarf_readu16 (as, a, addr, &v1, arg)) < 0)
+    return ret;
 
-    if (tdep_big_endian (as))
-        *val = (uint32_t) v0 << 16 | v1;
-    else
-        *val = (uint32_t) v1 << 16 | v0;
-    return 0;
+  if (tdep_big_endian (as))
+    *val = (uint32_t) v0 << 16 | v1;
+  else
+    *val = (uint32_t) v1 << 16 | v0;
+  return 0;
 }
 
 static inline int
 dwarf_readu64 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                uint64_t *val, void *arg)
 {
-    uint32_t v0, v1;
-    int ret;
+  uint32_t v0, v1;
+  int ret;
 
-    if ((ret = dwarf_readu32 (as, a, addr, &v0, arg)) < 0
-            || (ret = dwarf_readu32 (as, a, addr, &v1, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_readu32 (as, a, addr, &v0, arg)) < 0
+      || (ret = dwarf_readu32 (as, a, addr, &v1, arg)) < 0)
+    return ret;
 
-    if (tdep_big_endian (as))
-        *val = (uint64_t) v0 << 32 | v1;
-    else
-        *val = (uint64_t) v1 << 32 | v0;
-    return 0;
+  if (tdep_big_endian (as))
+    *val = (uint64_t) v0 << 32 | v1;
+  else
+    *val = (uint64_t) v1 << 32 | v0;
+  return 0;
 }
 
 static inline int
 dwarf_reads8 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
               int8_t *val, void *arg)
 {
-    uint8_t uval;
-    int ret;
+  uint8_t uval;
+  int ret;
 
-    if ((ret = dwarf_readu8 (as, a, addr, &uval, arg)) < 0)
-        return ret;
-    *val = (int8_t) uval;
-    return 0;
+  if ((ret = dwarf_readu8 (as, a, addr, &uval, arg)) < 0)
+    return ret;
+  *val = (int8_t) uval;
+  return 0;
 }
 
 static inline int
 dwarf_reads16 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int16_t *val, void *arg)
 {
-    uint16_t uval;
-    int ret;
+  uint16_t uval;
+  int ret;
 
-    if ((ret = dwarf_readu16 (as, a, addr, &uval, arg)) < 0)
-        return ret;
-    *val = (int16_t) uval;
-    return 0;
+  if ((ret = dwarf_readu16 (as, a, addr, &uval, arg)) < 0)
+    return ret;
+  *val = (int16_t) uval;
+  return 0;
 }
 
 static inline int
 dwarf_reads32 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int32_t *val, void *arg)
 {
-    uint32_t uval;
-    int ret;
+  uint32_t uval;
+  int ret;
 
-    if ((ret = dwarf_readu32 (as, a, addr, &uval, arg)) < 0)
-        return ret;
-    *val = (int32_t) uval;
-    return 0;
+  if ((ret = dwarf_readu32 (as, a, addr, &uval, arg)) < 0)
+    return ret;
+  *val = (int32_t) uval;
+  return 0;
 }
 
 static inline int
 dwarf_reads64 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                int64_t *val, void *arg)
 {
-    uint64_t uval;
-    int ret;
+  uint64_t uval;
+  int ret;
 
-    if ((ret = dwarf_readu64 (as, a, addr, &uval, arg)) < 0)
-        return ret;
-    *val = (int64_t) uval;
-    return 0;
+  if ((ret = dwarf_readu64 (as, a, addr, &uval, arg)) < 0)
+    return ret;
+  *val = (int64_t) uval;
+  return 0;
 }
 
 #endif /* !UNW_LOCAL_ONLY */
@@ -263,28 +263,28 @@
 dwarf_readw (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
              unw_word_t *val, void *arg)
 {
-    uint32_t u32;
-    uint64_t u64;
-    int ret;
+  uint32_t u32;
+  uint64_t u64;
+  int ret;
 
-    switch (dwarf_addr_size (as))
+  switch (dwarf_addr_size (as))
     {
     case 4:
-        ret = dwarf_readu32 (as, a, addr, &u32, arg);
-        if (ret < 0)
-            return ret;
-        *val = u32;
+      ret = dwarf_readu32 (as, a, addr, &u32, arg);
+      if (ret < 0)
         return ret;
+      *val = u32;
+      return ret;
 
     case 8:
-        ret = dwarf_readu64 (as, a, addr, &u64, arg);
-        if (ret < 0)
-            return ret;
-        *val = u64;
+      ret = dwarf_readu64 (as, a, addr, &u64, arg);
+      if (ret < 0)
         return ret;
+      *val = u64;
+      return ret;
 
     default:
-        abort ();
+      abort ();
     }
 }
 
@@ -295,22 +295,22 @@
 dwarf_read_uleb128 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                     unw_word_t *valp, void *arg)
 {
-    unw_word_t val = 0, shift = 0;
-    unsigned char byte;
-    int ret;
+  unw_word_t val = 0, shift = 0;
+  unsigned char byte;
+  int ret;
 
-    do
+  do
     {
-        if ((ret = dwarf_readu8 (as, a, addr, &byte, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_readu8 (as, a, addr, &byte, arg)) < 0)
+        return ret;
 
-        val |= ((unw_word_t) byte & 0x7f) << shift;
-        shift += 7;
+      val |= ((unw_word_t) byte & 0x7f) << shift;
+      shift += 7;
     }
-    while (byte & 0x80);
+  while (byte & 0x80);
 
-    *valp = val;
-    return 0;
+  *valp = val;
+  return 0;
 }
 
 /* Read a signed "little-endian base 128" value.  See Chapter 7.6 of
@@ -320,26 +320,26 @@
 dwarf_read_sleb128 (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
                     unw_word_t *valp, void *arg)
 {
-    unw_word_t val = 0, shift = 0;
-    unsigned char byte;
-    int ret;
+  unw_word_t val = 0, shift = 0;
+  unsigned char byte;
+  int ret;
 
-    do
+  do
     {
-        if ((ret = dwarf_readu8 (as, a, addr, &byte, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_readu8 (as, a, addr, &byte, arg)) < 0)
+        return ret;
 
-        val |= ((unw_word_t) byte & 0x7f) << shift;
-        shift += 7;
+      val |= ((unw_word_t) byte & 0x7f) << shift;
+      shift += 7;
     }
-    while (byte & 0x80);
+  while (byte & 0x80);
 
-    if (shift < 8 * sizeof (unw_word_t) && (byte & 0x40) != 0)
-        /* sign-extend negative value */
-        val |= ((unw_word_t) -1) << shift;
+  if (shift < 8 * sizeof (unw_word_t) && (byte & 0x40) != 0)
+    /* sign-extend negative value */
+    val |= ((unw_word_t) -1) << shift;
 
-    *valp = val;
-    return 0;
+  *valp = val;
+  return 0;
 }
 
 static ALWAYS_INLINE int
@@ -348,143 +348,143 @@
                                     const unw_proc_info_t *pi,
                                     unw_word_t *valp, void *arg)
 {
-    unw_word_t val, initial_addr = *addr;
-    uint16_t uval16;
-    uint32_t uval32;
-    uint64_t uval64;
-    int16_t sval16;
-    int32_t sval32;
-    int64_t sval64;
-    int ret;
+  unw_word_t val, initial_addr = *addr;
+  uint16_t uval16;
+  uint32_t uval32;
+  uint64_t uval64;
+  int16_t sval16 = 0;
+  int32_t sval32 = 0;
+  int64_t sval64 = 0;
+  int ret;
 
-    /* DW_EH_PE_omit and DW_EH_PE_aligned don't follow the normal
-       format/application encoding.  Handle them first.  */
-    if (encoding == DW_EH_PE_omit)
+  /* DW_EH_PE_omit and DW_EH_PE_aligned don't follow the normal
+     format/application encoding.  Handle them first.  */
+  if (encoding == DW_EH_PE_omit)
     {
-        *valp = 0;
-        return 0;
+      *valp = 0;
+      return 0;
     }
-    else if (encoding == DW_EH_PE_aligned)
+  else if (encoding == DW_EH_PE_aligned)
     {
-        int size = dwarf_addr_size (as);
-        *addr = (initial_addr + size - 1) & -size;
-        return dwarf_readw (as, a, addr, valp, arg);
+      int size = dwarf_addr_size (as);
+      *addr = (initial_addr + size - 1) & -size;
+      return dwarf_readw (as, a, addr, valp, arg);
     }
 
-    switch (encoding & DW_EH_PE_FORMAT_MASK)
+  switch (encoding & DW_EH_PE_FORMAT_MASK)
     {
     case DW_EH_PE_ptr:
-        if ((ret = dwarf_readw (as, a, addr, &val, arg)) < 0)
-            return ret;
-        break;
+      if ((ret = dwarf_readw (as, a, addr, &val, arg)) < 0)
+        return ret;
+      break;
 
     case DW_EH_PE_uleb128:
-        if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
-            return ret;
-        break;
+      if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
+        return ret;
+      break;
 
     case DW_EH_PE_udata2:
-        if ((ret = dwarf_readu16 (as, a, addr, &uval16, arg)) < 0)
-            return ret;
-        val = uval16;
-        break;
+      if ((ret = dwarf_readu16 (as, a, addr, &uval16, arg)) < 0)
+        return ret;
+      val = uval16;
+      break;
 
     case DW_EH_PE_udata4:
-        if ((ret = dwarf_readu32 (as, a, addr, &uval32, arg)) < 0)
-            return ret;
-        val = uval32;
-        break;
+      if ((ret = dwarf_readu32 (as, a, addr, &uval32, arg)) < 0)
+        return ret;
+      val = uval32;
+      break;
 
     case DW_EH_PE_udata8:
-        if ((ret = dwarf_readu64 (as, a, addr, &uval64, arg)) < 0)
-            return ret;
-        val = uval64;
-        break;
+      if ((ret = dwarf_readu64 (as, a, addr, &uval64, arg)) < 0)
+        return ret;
+      val = uval64;
+      break;
 
     case DW_EH_PE_sleb128:
-        if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
-            return ret;
-        break;
+      if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
+        return ret;
+      break;
 
     case DW_EH_PE_sdata2:
-        if ((ret = dwarf_reads16 (as, a, addr, &sval16, arg)) < 0)
-            return ret;
-        val = sval16;
-        break;
+      if ((ret = dwarf_reads16 (as, a, addr, &sval16, arg)) < 0)
+        return ret;
+      val = sval16;
+      break;
 
     case DW_EH_PE_sdata4:
-        if ((ret = dwarf_reads32 (as, a, addr, &sval32, arg)) < 0)
-            return ret;
-        val = sval32;
-        break;
+      if ((ret = dwarf_reads32 (as, a, addr, &sval32, arg)) < 0)
+        return ret;
+      val = sval32;
+      break;
 
     case DW_EH_PE_sdata8:
-        if ((ret = dwarf_reads64 (as, a, addr, &sval64, arg)) < 0)
-            return ret;
-        val = sval64;
-        break;
+      if ((ret = dwarf_reads64 (as, a, addr, &sval64, arg)) < 0)
+        return ret;
+      val = sval64;
+      break;
 
     default:
-        Debug (1, "unexpected encoding format 0x%x\n",
-               encoding & DW_EH_PE_FORMAT_MASK);
-        return -UNW_EINVAL;
+      Debug (1, "unexpected encoding format 0x%x\n",
+             encoding & DW_EH_PE_FORMAT_MASK);
+      return -UNW_EINVAL;
     }
 
-    if (val == 0)
+  if (val == 0)
     {
-        /* 0 is a special value and always absolute.  */
-        *valp = 0;
-        return 0;
+      /* 0 is a special value and always absolute.  */
+      *valp = 0;
+      return 0;
     }
 
-    switch (encoding & DW_EH_PE_APPL_MASK)
+  switch (encoding & DW_EH_PE_APPL_MASK)
     {
     case DW_EH_PE_absptr:
-        break;
+      break;
 
     case DW_EH_PE_pcrel:
-        val += initial_addr;
-        break;
+      val += initial_addr;
+      break;
 
     case DW_EH_PE_datarel:
-        /* XXX For now, assume that data-relative addresses are relative
-           to the global pointer.  */
-        val += pi->gp;
-        break;
+      /* XXX For now, assume that data-relative addresses are relative
+         to the global pointer.  */
+      val += pi->gp;
+      break;
 
     case DW_EH_PE_funcrel:
-        val += pi->start_ip;
-        break;
+      val += pi->start_ip;
+      break;
 
     case DW_EH_PE_textrel:
-    /* XXX For now we don't support text-rel values.  If there is a
-       platform which needs this, we probably would have to add a
-       "segbase" member to unw_proc_info_t.  */
+      /* XXX For now we don't support text-rel values.  If there is a
+         platform which needs this, we probably would have to add a
+         "segbase" member to unw_proc_info_t.  */
     default:
-        Debug (1, "unexpected application type 0x%x\n",
-               encoding & DW_EH_PE_APPL_MASK);
-        return -UNW_EINVAL;
+      Debug (1, "unexpected application type 0x%x\n",
+             encoding & DW_EH_PE_APPL_MASK);
+      return -UNW_EINVAL;
     }
 
-    /* Trim off any extra bits.  Assume that sign extension isn't
-       required; the only place it is needed is MIPS kernel space
-       addresses.  */
-    if (sizeof (val) > dwarf_addr_size (as))
+  /* Trim off any extra bits.  Assume that sign extension isn't
+     required; the only place it is needed is MIPS kernel space
+     addresses.  */
+  if (sizeof (val) > dwarf_addr_size (as))
     {
-        assert (dwarf_addr_size (as) == 4);
-        val = (uint32_t) val;
+      assert (dwarf_addr_size (as) == 4);
+      val = (uint32_t) val;
     }
 
-    if (encoding & DW_EH_PE_indirect)
+  if (encoding & DW_EH_PE_indirect)
     {
-        unw_word_t indirect_addr = val;
+      unw_word_t indirect_addr = val;
 
-        if ((ret = dwarf_readw (as, a, &indirect_addr, &val, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_readw (as, a, &indirect_addr, &val, arg)) < 0)
+        return ret;
     }
 
-    *valp = val;
-    return 0;
+  *valp = val;
+  return 0;
 }
 
 #endif /* DWARF_I_H */
diff --git a/sgx_unwind/libunwind/include/libunwind-common.h.in b/sgx_unwind/libunwind/include/libunwind-common.h.in
index fa753ba..8d96ddc 100644
--- a/sgx_unwind/libunwind/include/libunwind-common.h.in
+++ b/sgx_unwind/libunwind/include/libunwind-common.h.in
@@ -86,6 +86,12 @@
   }
 unw_caching_policy_t;
 
+typedef enum
+  {
+    UNW_INIT_SIGNAL_FRAME = 1,          /* We know this is a signal frame */
+  }
+unw_init_local2_flags_t;
+
 typedef int unw_regnum_t;
 
 /* The unwind cursor starts at the youngest (most deeply nested) frame
@@ -126,6 +132,9 @@
   {
     unw_word_t start_ip;	/* first IP covered by this procedure */
     unw_word_t end_ip;		/* first IP NOT covered by this procedure */
+#if defined(NEED_LAST_IP)
+    unw_word_t last_ip;		/* first IP that could begin another procedure */
+#endif
     unw_word_t lsda;		/* address of lang.-spec. data area (if any) */
     unw_word_t handler;		/* optional personality routine */
     unw_word_t gp;		/* global-pointer value for this procedure */
@@ -138,6 +147,11 @@
   }
 unw_proc_info_t;
 
+typedef int (*unw_reg_states_callback)(void *token,
+				       void *reg_states_data,
+				       size_t reg_states_data_size,
+				       unw_word_t start_ip, unw_word_t end_ip);
+
 /* These are backend callback routines that provide access to the
    state of a "remote" process.  This can be used, for example, to
    unwind another process through the ptrace() interface.  */
@@ -210,12 +224,16 @@
 #define unw_create_addr_space	UNW_OBJ(create_addr_space)
 #define unw_destroy_addr_space	UNW_OBJ(destroy_addr_space)
 #define unw_get_accessors	UNW_ARCH_OBJ(get_accessors)
+#define unw_get_accessors_int	UNW_ARCH_OBJ(get_accessors_int)
 #define unw_init_local		UNW_OBJ(init_local)
+#define unw_init_local2	        UNW_OBJ(init_local2)
 #define unw_init_remote		UNW_OBJ(init_remote)
 #define unw_step		UNW_OBJ(step)
 #define unw_resume		UNW_OBJ(resume)
 #define unw_get_proc_info	UNW_OBJ(get_proc_info)
 #define unw_get_proc_info_by_ip	UNW_OBJ(get_proc_info_by_ip)
+#define unw_reg_states_iterate  UNW_OBJ(reg_states_iterate)
+#define unw_apply_reg_state     UNW_OBJ(apply_reg_state)
 #define unw_get_reg		UNW_OBJ(get_reg)
 #define unw_set_reg		UNW_OBJ(set_reg)
 #define unw_get_fpreg		UNW_OBJ(get_fpreg)
@@ -225,6 +243,7 @@
 #define unw_handle_signal_frame	UNW_OBJ(handle_signal_frame)
 #define unw_get_proc_name	UNW_OBJ(get_proc_name)
 #define unw_set_caching_policy	UNW_OBJ(set_caching_policy)
+#define unw_set_cache_size	UNW_OBJ(set_cache_size)
 #define unw_regname		UNW_ARCH_OBJ(regname)
 #define unw_flush_cache		UNW_ARCH_OBJ(flush_cache)
 #define unw_strerror		UNW_ARCH_OBJ(strerror)
@@ -232,17 +251,22 @@
 extern unw_addr_space_t unw_create_addr_space (unw_accessors_t *, int);
 extern void unw_destroy_addr_space (unw_addr_space_t);
 extern unw_accessors_t *unw_get_accessors (unw_addr_space_t);
+extern unw_accessors_t *unw_get_accessors_int (unw_addr_space_t);
 extern void unw_flush_cache (unw_addr_space_t, unw_word_t, unw_word_t);
 extern int unw_set_caching_policy (unw_addr_space_t, unw_caching_policy_t);
+extern int unw_set_cache_size (unw_addr_space_t, size_t, int);
 extern const char *unw_regname (unw_regnum_t);
 
 extern int unw_init_local (unw_cursor_t *, unw_context_t *);
+extern int unw_init_local2 (unw_cursor_t *, unw_context_t *, int);
 extern int unw_init_remote (unw_cursor_t *, unw_addr_space_t, void *);
 extern int unw_step (unw_cursor_t *);
 extern int unw_resume (unw_cursor_t *);
 extern int unw_get_proc_info (unw_cursor_t *, unw_proc_info_t *);
 extern int unw_get_proc_info_by_ip (unw_addr_space_t, unw_word_t,
 				    unw_proc_info_t *, void *);
+extern int unw_reg_states_iterate (unw_cursor_t *, unw_reg_states_callback, void *);
+extern int unw_apply_reg_state (unw_cursor_t *, void *);
 extern int unw_get_reg (unw_cursor_t *, int, unw_word_t *);
 extern int unw_set_reg (unw_cursor_t *, int, unw_word_t);
 extern int unw_get_fpreg (unw_cursor_t *, int, unw_fpreg_t *);
diff --git a/sgx_unwind/libunwind/include/libunwind-dynamic.h b/sgx_unwind/libunwind/include/libunwind-dynamic.h
index ce15bec..edb0bbd 100644
--- a/sgx_unwind/libunwind/include/libunwind-dynamic.h
+++ b/sgx_unwind/libunwind/include/libunwind-dynamic.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -58,108 +58,112 @@
      int32_t instead.  */
 
 typedef enum
-{
-    UNW_DYN_STOP = 0,		/* end-of-unwind-info marker */
-    UNW_DYN_SAVE_REG,		/* save register to another register */
-    UNW_DYN_SPILL_FP_REL,	/* frame-pointer-relative register spill */
-    UNW_DYN_SPILL_SP_REL,	/* stack-pointer-relative register spill */
-    UNW_DYN_ADD,		/* add constant value to a register */
-    UNW_DYN_POP_FRAMES,		/* drop one or more stack frames */
-    UNW_DYN_LABEL_STATE,	/* name the current state */
-    UNW_DYN_COPY_STATE,		/* set the region's entry-state */
-    UNW_DYN_ALIAS		/* get unwind info from an alias */
-}
+  {
+    UNW_DYN_STOP = 0,           /* end-of-unwind-info marker */
+    UNW_DYN_SAVE_REG,           /* save register to another register */
+    UNW_DYN_SPILL_FP_REL,       /* frame-pointer-relative register spill */
+    UNW_DYN_SPILL_SP_REL,       /* stack-pointer-relative register spill */
+    UNW_DYN_ADD,                /* add constant value to a register */
+    UNW_DYN_POP_FRAMES,         /* drop one or more stack frames */
+    UNW_DYN_LABEL_STATE,        /* name the current state */
+    UNW_DYN_COPY_STATE,         /* set the region's entry-state */
+    UNW_DYN_ALIAS               /* get unwind info from an alias */
+  }
 unw_dyn_operation_t;
 
 typedef enum
-{
-    UNW_INFO_FORMAT_DYNAMIC,		/* unw_dyn_proc_info_t */
-    UNW_INFO_FORMAT_TABLE,		/* unw_dyn_table_t */
-    UNW_INFO_FORMAT_REMOTE_TABLE,	/* unw_dyn_remote_table_t */
-    UNW_INFO_FORMAT_ARM_EXIDX		/* ARM specific unwind info */
-}
+  {
+    UNW_INFO_FORMAT_DYNAMIC,            /* unw_dyn_proc_info_t */
+    UNW_INFO_FORMAT_TABLE,              /* unw_dyn_table_t */
+    UNW_INFO_FORMAT_REMOTE_TABLE,       /* unw_dyn_remote_table_t */
+    UNW_INFO_FORMAT_ARM_EXIDX,          /* ARM specific unwind info */
+    UNW_INFO_FORMAT_IP_OFFSET,          /* Like UNW_INFO_FORMAT_REMOTE_TABLE, but
+                                           table entries are considered
+                                           relative to di->start_ip, rather
+                                           than di->segbase */
+  }
 unw_dyn_info_format_t;
 
 typedef struct unw_dyn_op
-{
-    int8_t tag;				/* what operation? */
-    int8_t qp;				/* qualifying predicate register */
-    int16_t reg;			/* what register */
-    int32_t when;			/* when does it take effect? */
-    unw_word_t val;			/* auxiliary value */
-}
+  {
+    int8_t tag;                         /* what operation? */
+    int8_t qp;                          /* qualifying predicate register */
+    int16_t reg;                        /* what register */
+    int32_t when;                       /* when does it take effect? */
+    unw_word_t val;                     /* auxiliary value */
+  }
 unw_dyn_op_t;
 
 typedef struct unw_dyn_region_info
-{
-    struct unw_dyn_region_info *next;	/* linked list of regions */
-    int32_t insn_count;			/* region length (# of instructions) */
-    uint32_t op_count;			/* length of op-array */
-    unw_dyn_op_t op[1];			/* variable-length op-array */
-}
+  {
+    struct unw_dyn_region_info *next;   /* linked list of regions */
+    int32_t insn_count;                 /* region length (# of instructions) */
+    uint32_t op_count;                  /* length of op-array */
+    unw_dyn_op_t op[1];                 /* variable-length op-array */
+  }
 unw_dyn_region_info_t;
 
 typedef struct unw_dyn_proc_info
-{
-    unw_word_t name_ptr;	/* address of human-readable procedure name */
-    unw_word_t handler;		/* address of personality routine */
+  {
+    unw_word_t name_ptr;        /* address of human-readable procedure name */
+    unw_word_t handler;         /* address of personality routine */
     uint32_t flags;
     int32_t pad0;
     unw_dyn_region_info_t *regions;
-}
+  }
 unw_dyn_proc_info_t;
 
 typedef struct unw_dyn_table_info
-{
-    unw_word_t name_ptr;	/* addr. of table name (e.g., library name) */
-    unw_word_t segbase;		/* segment base */
-    unw_word_t table_len;	/* must be a multiple of sizeof(unw_word_t)! */
+  {
+    unw_word_t name_ptr;        /* addr. of table name (e.g., library name) */
+    unw_word_t segbase;         /* segment base */
+    unw_word_t table_len;       /* must be a multiple of sizeof(unw_word_t)! */
     unw_word_t *table_data;
-}
+  }
 unw_dyn_table_info_t;
 
 typedef struct unw_dyn_remote_table_info
-{
-    unw_word_t name_ptr;	/* addr. of table name (e.g., library name) */
-    unw_word_t segbase;		/* segment base */
-    unw_word_t table_len;	/* must be a multiple of sizeof(unw_word_t)! */
+  {
+    unw_word_t name_ptr;        /* addr. of table name (e.g., library name) */
+    unw_word_t segbase;         /* segment base */
+    unw_word_t table_len;       /* must be a multiple of sizeof(unw_word_t)! */
     unw_word_t table_data;
-}
+  }
 unw_dyn_remote_table_info_t;
 
 typedef struct unw_dyn_info
-{
+  {
     /* doubly-linked list of dyn-info structures: */
     struct unw_dyn_info *next;
     struct unw_dyn_info *prev;
-    unw_word_t start_ip;	/* first IP covered by this entry */
-    unw_word_t end_ip;		/* first IP NOT covered by this entry */
-    unw_word_t gp;		/* global-pointer in effect for this entry */
-    int32_t format;		/* real type: unw_dyn_info_format_t */
+    unw_word_t start_ip;        /* first IP covered by this entry */
+    unw_word_t end_ip;          /* first IP NOT covered by this entry */
+    unw_word_t gp;              /* global-pointer in effect for this entry */
+    int32_t format;             /* real type: unw_dyn_info_format_t */
     int32_t pad;
     union
-    {
+      {
         unw_dyn_proc_info_t pi;
         unw_dyn_table_info_t ti;
         unw_dyn_remote_table_info_t rti;
-    }
+      }
     u;
-}
+  }
 unw_dyn_info_t;
 
 typedef struct unw_dyn_info_list
-{
+  {
     uint32_t version;
     uint32_t generation;
     unw_dyn_info_t *first;
-}
+  }
 unw_dyn_info_list_t;
 
 /* Return the size (in bytes) of an unw_dyn_region_info_t structure that can
    hold OP_COUNT ops.  */
-#define _U_dyn_region_info_size(op_count)				\
-	((char *) (((unw_dyn_region_info_t *) NULL)->op + (op_count))	\
-	 - (char *) NULL)
+#define _U_dyn_region_info_size(op_count)                               \
+        ((char *) (((unw_dyn_region_info_t *) NULL)->op + (op_count))   \
+         - (char *) NULL)
 
 /* Register the unwind info for a single procedure.
    This routine is NOT signal-safe.  */
@@ -172,39 +176,39 @@
 
 /* Convenience routines.  */
 
-#define _U_dyn_op(_tag, _qp, _when, _reg, _val)				\
-	((unw_dyn_op_t) { (_tag), (_qp), (_reg), (_when), (_val) })
+#define _U_dyn_op(_tag, _qp, _when, _reg, _val)                         \
+        ((unw_dyn_op_t) { (_tag), (_qp), (_reg), (_when), (_val) })
 
-#define _U_dyn_op_save_reg(op, qp, when, reg, dst)			\
-	(*(op) = _U_dyn_op (UNW_DYN_SAVE_REG, (qp), (when), (reg), (dst)))
+#define _U_dyn_op_save_reg(op, qp, when, reg, dst)                      \
+        (*(op) = _U_dyn_op (UNW_DYN_SAVE_REG, (qp), (when), (reg), (dst)))
 
-#define _U_dyn_op_spill_fp_rel(op, qp, when, reg, offset)		\
-	(*(op) = _U_dyn_op (UNW_DYN_SPILL_FP_REL, (qp), (when), (reg),	\
-			    (offset)))
+#define _U_dyn_op_spill_fp_rel(op, qp, when, reg, offset)               \
+        (*(op) = _U_dyn_op (UNW_DYN_SPILL_FP_REL, (qp), (when), (reg),  \
+                            (offset)))
 
-#define _U_dyn_op_spill_sp_rel(op, qp, when, reg, offset)		\
-	(*(op) = _U_dyn_op (UNW_DYN_SPILL_SP_REL, (qp), (when), (reg),	\
-			    (offset)))
+#define _U_dyn_op_spill_sp_rel(op, qp, when, reg, offset)               \
+        (*(op) = _U_dyn_op (UNW_DYN_SPILL_SP_REL, (qp), (when), (reg),  \
+                            (offset)))
 
-#define _U_dyn_op_add(op, qp, when, reg, value)				\
-	(*(op) = _U_dyn_op (UNW_DYN_ADD, (qp), (when), (reg), (value)))
+#define _U_dyn_op_add(op, qp, when, reg, value)                         \
+        (*(op) = _U_dyn_op (UNW_DYN_ADD, (qp), (when), (reg), (value)))
 
-#define _U_dyn_op_pop_frames(op, qp, when, num_frames)			\
-	(*(op) = _U_dyn_op (UNW_DYN_POP_FRAMES, (qp), (when), 0, (num_frames)))
+#define _U_dyn_op_pop_frames(op, qp, when, num_frames)                  \
+        (*(op) = _U_dyn_op (UNW_DYN_POP_FRAMES, (qp), (when), 0, (num_frames)))
 
-#define _U_dyn_op_label_state(op, label)				\
-	(*(op) = _U_dyn_op (UNW_DYN_LABEL_STATE, _U_QP_TRUE, -1, 0, (label)))
+#define _U_dyn_op_label_state(op, label)                                \
+        (*(op) = _U_dyn_op (UNW_DYN_LABEL_STATE, _U_QP_TRUE, -1, 0, (label)))
 
-#define _U_dyn_op_copy_state(op, label)					\
-	(*(op) = _U_dyn_op (UNW_DYN_COPY_STATE, _U_QP_TRUE, -1, 0, (label)))
+#define _U_dyn_op_copy_state(op, label)                                 \
+        (*(op) = _U_dyn_op (UNW_DYN_COPY_STATE, _U_QP_TRUE, -1, 0, (label)))
 
-#define _U_dyn_op_alias(op, qp, when, addr)				\
-	(*(op) = _U_dyn_op (UNW_DYN_ALIAS, (qp), (when), 0, (addr)))
+#define _U_dyn_op_alias(op, qp, when, addr)                             \
+        (*(op) = _U_dyn_op (UNW_DYN_ALIAS, (qp), (when), 0, (addr)))
 
-#define _U_dyn_op_stop(op)						\
-	(*(op) = _U_dyn_op (UNW_DYN_STOP, _U_QP_TRUE, -1, 0, 0))
+#define _U_dyn_op_stop(op)                                              \
+        (*(op) = _U_dyn_op (UNW_DYN_STOP, _U_QP_TRUE, -1, 0, 0))
 
 /* The target-dependent qualifying predicate which is always TRUE.  On
    IA-64, that's p0 (0), on non-predicated architectures, the value is
    ignored.  */
-#define _U_QP_TRUE	_U_TDEP_QP_TRUE
+#define _U_QP_TRUE      _U_TDEP_QP_TRUE
diff --git a/sgx_unwind/libunwind/include/libunwind-x86_64.h b/sgx_unwind/libunwind/include/libunwind-x86_64.h
index 4c8bcde..78eb541 100644
--- a/sgx_unwind/libunwind/include/libunwind-x86_64.h
+++ b/sgx_unwind/libunwind/include/libunwind-x86_64.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -36,17 +36,17 @@
 #include <inttypes.h>
 #include <ucontext.h>
 
-#define UNW_TARGET		x86_64
-#define UNW_TARGET_X86_64	1
+#define UNW_TARGET              x86_64
+#define UNW_TARGET_X86_64       1
 
-#define _U_TDEP_QP_TRUE	0	/* see libunwind-dynamic.h  */
+#define _U_TDEP_QP_TRUE 0       /* see libunwind-dynamic.h  */
 
 /* This needs to be big enough to accommodate "struct cursor", while
    leaving some slack for future expansion.  Changing this value will
    require recompiling all users of this library.  Stack allocation is
    relatively cheap and unwind-state copying is relatively rare, so we
    want to err on making it rather too big than too small.  */
-#define UNW_TDEP_CURSOR_LEN	127
+#define UNW_TDEP_CURSOR_LEN     127
 
 typedef uint64_t unw_word_t;
 typedef int64_t unw_sword_t;
@@ -54,7 +54,7 @@
 typedef long double unw_tdep_fpreg_t;
 
 typedef enum
-{
+  {
     UNW_X86_64_RAX,
     UNW_X86_64_RDX,
     UNW_X86_64_RCX,
@@ -103,31 +103,33 @@
     UNW_TDEP_SP = UNW_X86_64_RSP,
     UNW_TDEP_BP = UNW_X86_64_RBP,
     UNW_TDEP_EH = UNW_X86_64_RAX
-}
+  }
 x86_64_regnum_t;
 
-#define UNW_TDEP_NUM_EH_REGS	2	/* XXX Not sure what this means */
+#define UNW_TDEP_NUM_EH_REGS    2       /* XXX Not sure what this means */
 
 typedef struct unw_tdep_save_loc
-{
+  {
     /* Additional target-dependent info on a save location.  */
-}
+    char unused;
+  }
 unw_tdep_save_loc_t;
 
 /* On x86_64, we can directly use ucontext_t as the unwind context.  */
 typedef ucontext_t unw_tdep_context_t;
 
 typedef struct
-{
+  {
     /* no x86-64-specific auxiliary proc-info */
-}
+    char unused;
+  }
 unw_tdep_proc_info_t;
 
 #include "libunwind-dynamic.h"
 #include "libunwind-common.h"
 
-#define unw_tdep_getcontext		UNW_ARCH_OBJ(getcontext)
-#define unw_tdep_is_fpreg		UNW_ARCH_OBJ(is_fpreg)
+#define unw_tdep_getcontext             UNW_ARCH_OBJ(getcontext)
+#define unw_tdep_is_fpreg               UNW_ARCH_OBJ(is_fpreg)
 
 extern int unw_tdep_getcontext (unw_tdep_context_t *);
 extern int unw_tdep_is_fpreg (int);
diff --git a/sgx_unwind/libunwind/include/libunwind.h.in b/sgx_unwind/libunwind/include/libunwind.h.in
index 6ed4725..7a56168 100644
--- a/sgx_unwind/libunwind/include/libunwind.h.in
+++ b/sgx_unwind/libunwind/include/libunwind.h.in
@@ -3,7 +3,9 @@
 
 #ifndef UNW_REMOTE_ONLY
 
-#if defined __arm__
+#if defined __aarch64__
+#include "libunwind-aarch64.h"
+#elif defined __arm__
 # include "libunwind-arm.h"
 #elif defined __hppa__
 # include "libunwind-hppa.h"
@@ -15,10 +17,14 @@
 # include "libunwind-ppc32.h"
 #elif defined __powerpc64__
 # include "libunwind-ppc64.h"
+#elif defined __sh__
+# include "libunwind-sh.h"
 #elif defined __i386__
 # include "libunwind-x86.h"
 #elif defined __x86_64__
 # include "libunwind-x86_64.h"
+#elif defined __tilegx__
+# include "libunwind-tilegx.h"
 #else
 # error "Unsupported arch"
 #endif
diff --git a/sgx_unwind/libunwind/include/libunwind_i.h b/sgx_unwind/libunwind/include/libunwind_i.h
index 3b4e7d8..ea983b8 100644
--- a/sgx_unwind/libunwind/include/libunwind_i.h
+++ b/sgx_unwind/libunwind/include/libunwind_i.h
@@ -1,7 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2005 Hewlett-Packard Co
    Copyright (C) 2007 David Mosberger-Tang
-	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
 
 This file is part of libunwind.
 
@@ -35,24 +35,22 @@
 # include "config.h"
 #endif
 
-#ifdef HAVE___THREAD
-/* For now, turn off per-thread caching.  It uses up too much TLS
-   memory per thread even when the thread never uses libunwind at
-   all.  */
-# undef HAVE___THREAD
+#include "compiler.h"
+
+#if defined(HAVE___THREAD) && HAVE___THREAD
+#define UNWI_DEFAULT_CACHING_POLICY UNW_CACHE_PER_THREAD
+#else
+#define UNWI_DEFAULT_CACHING_POLICY UNW_CACHE_GLOBAL
 #endif
 
 /* Platform-independent libunwind-internal declarations.  */
 
-#include <sys/types.h>	/* HP-UX needs this before include of pthread.h */
+#include <sys/types.h>  /* HP-UX needs this before include of pthread.h */
 
 #include <assert.h>
 #include <libunwind.h>
-
-#if HAVE_SGX
-#include "pthread_compat.h"
-#else
 #include <pthread.h>
+#ifndef HAVE_SGX
 #include <signal.h>
 #endif
 
@@ -60,61 +58,58 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <elf.h>
+
+#if defined(HAVE_ELF_H)
+# include <elf.h>
+#elif defined(HAVE_SYS_ELF_H)
+# include <sys/elf.h>
+#else
+# error Could not locate <elf.h>
+#endif
 
 #if defined(HAVE_ENDIAN_H)
 # include <endian.h>
 #elif defined(HAVE_SYS_ENDIAN_H)
 # include <sys/endian.h>
+# if defined(_LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN)
+#   define __LITTLE_ENDIAN _LITTLE_ENDIAN
+# endif
+# if defined(_BIG_ENDIAN) && !defined(__BIG_ENDIAN)
+#   define __BIG_ENDIAN _BIG_ENDIAN
+# endif
+# if defined(_BYTE_ORDER) && !defined(__BYTE_ORDER)
+#   define __BYTE_ORDER _BYTE_ORDER
+# endif
 #else
-# define __LITTLE_ENDIAN	1234
-# define __BIG_ENDIAN		4321
+# define __LITTLE_ENDIAN        1234
+# define __BIG_ENDIAN           4321
 # if defined(__hpux)
 #   define __BYTE_ORDER __BIG_ENDIAN
+# elif defined(__QNX__)
+#   if defined(__BIGENDIAN__)
+#     define __BYTE_ORDER __BIG_ENDIAN
+#   elif defined(__LITTLEENDIAN__)
+#     define __BYTE_ORDER __LITTLE_ENDIAN
+#   else
+#     error Host has unknown byte-order.
+#   endif
 # else
 #   error Host has unknown byte-order.
 # endif
 #endif
 
-#ifdef __GNUC__
-# define UNUSED		__attribute__((unused))
-# define NORETURN	__attribute__((noreturn))
-# define ALIAS(name)	__attribute__((alias (#name)))
-# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
-#  define ALWAYS_INLINE	inline __attribute__((always_inline))
-#  define HIDDEN	__attribute__((visibility ("hidden")))
-#  define PROTECTED	__attribute__((visibility ("protected")))
-# else
-#  define ALWAYS_INLINE
-#  define HIDDEN
-#  define PROTECTED
-# endif
-# if (__GNUC__ >= 3)
-#  define likely(x)	__builtin_expect ((x), 1)
-#  define unlikely(x)	__builtin_expect ((x), 0)
-# else
-#  define likely(x)	(x)
-#  define unlikely(x)	(x)
-# endif
+#if defined(HAVE__BUILTIN_UNREACHABLE)
+# define unreachable() __builtin_unreachable()
 #else
-# define ALWAYS_INLINE
-# define UNUSED
-# define NORETURN
-# define ALIAS(name)
-# define HIDDEN
-# define PROTECTED
-# define likely(x)	(x)
-# define unlikely(x)	(x)
+# define unreachable() do { } while (1)
 #endif
 
-#ifdef DEBUG
-# define UNW_DEBUG	1
+#if defined(DEBUG) && !HAVE_SGX
+# define UNW_DEBUG      1
 #else
-# define UNW_DEBUG	0
+# define UNW_DEBUG      0
 #endif
 
-#define ARRAY_SIZE(a)	(sizeof (a) / sizeof ((a)[0]))
-
 /* Make it easy to write thread-safe code which may or may not be
    linked against libpthread.  The macros below can be used
    unconditionally and if -lpthread is around, they'll call the
@@ -129,59 +124,62 @@
 #pragma weak pthread_mutex_lock
 #pragma weak pthread_mutex_unlock
 
-#define mutex_init(l)							\
-	(pthread_mutex_init != 0 ? pthread_mutex_init ((l), 0) : 0)
-#define mutex_lock(l)							\
-	(pthread_mutex_lock != 0 ? pthread_mutex_lock (l) : 0)
-#define mutex_unlock(l)							\
-	(pthread_mutex_unlock != 0 ? pthread_mutex_unlock (l) : 0)
+#define mutex_init(l)                                                   \
+        (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
+#define mutex_lock(l)                                                   \
+        (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
+#define mutex_unlock(l)                                                 \
+        (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
 #endif
 
+
 #ifdef HAVE_ATOMIC_OPS_H
 # include <atomic_ops.h>
 static inline int
 cmpxchg_ptr (void *addr, void *old, void *new)
 {
-    union
+  union
     {
-        void *vp;
-        AO_t *aop;
+      void *vp;
+      AO_t *aop;
     }
-    u;
+  u;
 
-    u.vp = addr;
-    return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
+  u.vp = addr;
+  return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
 }
-# define fetch_and_add1(_ptr)		AO_fetch_and_add1(_ptr)
-/* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
+# define fetch_and_add1(_ptr)           AO_fetch_and_add1(_ptr)
+# define fetch_and_add(_ptr, value)     AO_fetch_and_add(_ptr, value)
+   /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
 #  if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
 #   define HAVE_CMPXCHG
 #  endif
-# define HAVE_FETCH_AND_ADD1
-#else
+# define HAVE_FETCH_AND_ADD
+#elif defined(HAVE_SYNC_ATOMICS) || defined(HAVE_IA64INTRIN_H)
 # ifdef HAVE_IA64INTRIN_H
 #  include <ia64intrin.h>
+# endif
 static inline int
 cmpxchg_ptr (void *addr, void *old, void *new)
 {
-    union
+  union
     {
-        void *vp;
-        long *vlp;
+      void *vp;
+      long *vlp;
     }
-    u;
+  u;
 
-    u.vp = addr;
-    return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
+  u.vp = addr;
+  return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
 }
-#  define fetch_and_add1(_ptr)		__sync_fetch_and_add(_ptr, 1)
-#  define HAVE_CMPXCHG
-#  define HAVE_FETCH_AND_ADD1
-# endif
+# define fetch_and_add1(_ptr)           __sync_fetch_and_add(_ptr, 1)
+# define fetch_and_add(_ptr, value)     __sync_fetch_and_add(_ptr, value)
+# define HAVE_CMPXCHG
+# define HAVE_FETCH_AND_ADD
 #endif
-#define atomic_read(ptr)	(*(ptr))
+#define atomic_read(ptr)        (*(ptr))
 
-#define UNWI_OBJ(fn)	  UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
+#define UNWI_OBJ(fn)      UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
 #define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
 
 #define unwi_full_mask    UNWI_ARCH_OBJ(full_mask)
@@ -196,8 +194,7 @@
 
 /* Silence compiler warnings about variables which are used only if libunwind
    is configured in a certain way */
-static inline void mark_as_used(void *v)
-{
+static inline void mark_as_used(void *v UNUSED) {
 }
 
 #if defined(CONFIG_BLOCK_SIGNALS)
@@ -207,122 +204,130 @@
 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
 #endif
 
+/* Prefer adaptive mutexes if available */
+#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+#define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+#else
+#define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#endif
+
 #define define_lock(name) \
-  pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
-#define lock_init(l)		mutex_init (l)
-#define lock_acquire(l,m)				\
-do {							\
-  SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m));	\
-  mutex_lock (l);					\
+  pthread_mutex_t name = UNW_PTHREAD_MUTEX_INITIALIZER
+#define lock_init(l)            mutex_init (l)
+#define lock_acquire(l,m)                               \
+do {                                                    \
+  SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m));     \
+  mutex_lock (l);                                       \
 } while (0)
-#define lock_release(l,m)			\
-do {						\
-  mutex_unlock (l);				\
-  SIGPROCMASK (SIG_SETMASK, &(m), NULL);	\
+#define lock_release(l,m)                       \
+do {                                            \
+  mutex_unlock (l);                             \
+  SIGPROCMASK (SIG_SETMASK, &(m), NULL);        \
 } while (0)
 
-#define SOS_MEMORY_SIZE 16384	/* see src/mi/mempool.c */
+#define SOS_MEMORY_SIZE 16384   /* see src/mi/mempool.c */
 
 #if HAVE_SGX /* Running inside Enclave */
 #define GET_MEMORY(mem, size) do { mem = malloc(size); } while (0)
-#else  /* Normal application */
+#define FREE_MEMORY(mem, size)      \
+do {                                                          \
+  if (mem != NULL)                                  \
+    free(mem);                                          \
+} while (0)
+
+#else
 #ifndef MAP_ANONYMOUS
 # define MAP_ANONYMOUS MAP_ANON
 #endif
-#define GET_MEMORY(mem, size)				    		    \
-do {									    \
+#define GET_MEMORY(mem, size)                                               \
+do {                                                                        \
   /* Hopefully, mmap() goes straight through to a system call stub...  */   \
-  mem = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \
-	      -1, 0);							    \
-  if (mem == MAP_FAILED)						    \
-    mem = NULL;								    \
+  mem = mmap (NULL, size, PROT_READ | PROT_WRITE,                           \
+              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);                          \
+  if (mem == MAP_FAILED)                                                    \
+    mem = NULL;                                                             \
 } while (0)
 #endif
 
-#define unwi_find_dynamic_proc_info	UNWI_OBJ(find_dynamic_proc_info)
-#define unwi_extract_dynamic_proc_info	UNWI_OBJ(extract_dynamic_proc_info)
-#define unwi_put_dynamic_unwind_info	UNWI_OBJ(put_dynamic_unwind_info)
-#define unwi_dyn_remote_find_proc_info	UNWI_OBJ(dyn_remote_find_proc_info)
-#define unwi_dyn_remote_put_unwind_info	UNWI_OBJ(dyn_remote_put_unwind_info)
-#define unwi_dyn_validate_cache		UNWI_OBJ(dyn_validate_cache)
+#define unwi_find_dynamic_proc_info     UNWI_OBJ(find_dynamic_proc_info)
+#define unwi_extract_dynamic_proc_info  UNWI_OBJ(extract_dynamic_proc_info)
+#define unwi_put_dynamic_unwind_info    UNWI_OBJ(put_dynamic_unwind_info)
+#define unwi_dyn_remote_find_proc_info  UNWI_OBJ(dyn_remote_find_proc_info)
+#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
+#define unwi_dyn_validate_cache         UNWI_OBJ(dyn_validate_cache)
 
 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
                                         unw_word_t ip,
                                         unw_proc_info_t *pi,
                                         int need_unwind_info, void *arg);
 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
-        unw_word_t ip,
-        unw_proc_info_t *pi,
-        unw_dyn_info_t *di,
-        int need_unwind_info,
-        void *arg);
+                                           unw_word_t ip,
+                                           unw_proc_info_t *pi,
+                                           unw_dyn_info_t *di,
+                                           int need_unwind_info,
+                                           void *arg);
 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
-        unw_proc_info_t *pi, void *arg);
+                                          unw_proc_info_t *pi, void *arg);
 
 /* These handle the remote (cross-address-space) case of accessing
    dynamic unwind info. */
 
 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
-        unw_word_t ip,
-        unw_proc_info_t *pi,
-        int need_unwind_info,
-        void *arg);
+                                           unw_word_t ip,
+                                           unw_proc_info_t *pi,
+                                           int need_unwind_info,
+                                           void *arg);
 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
-        unw_proc_info_t *pi,
-        void *arg);
+                                             unw_proc_info_t *pi,
+                                             void *arg);
 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
 
+extern unw_dyn_info_list_t _U_dyn_info_list;
 extern pthread_mutex_t _U_dyn_info_list_lock;
 
-#if UNW_DEBUG && !HAVE_SGX
-#define unwi_debug_level		UNWI_ARCH_OBJ(debug_level)
+#if UNW_DEBUG
+#define unwi_debug_level                UNWI_ARCH_OBJ(debug_level)
 extern long unwi_debug_level;
 
 # include <stdio.h>
-# define Debug(level,format...)						\
-do {									\
-  if (unwi_debug_level >= level)					\
-    {									\
-      int _n = level;							\
-      if (_n > 16)							\
-	_n = 16;							\
-      fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__);		\
-      fprintf (stderr, format);						\
-    }									\
+# define Debug(level,format...)                                         \
+do {                                                                    \
+  if (unwi_debug_level >= level)                                        \
+    {                                                                   \
+      int _n = level;                                                   \
+      if (_n > 16)                                                      \
+        _n = 16;                                                        \
+      fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__);              \
+      fprintf (stderr, format);                                         \
+    }                                                                   \
 } while (0)
-# define Dprintf(format...) 	    fprintf (stderr, format)
-# ifdef __GNUC__
-#  undef inline
-#  define inline	UNUSED
-# endif
+# define Dprintf(format...)         fprintf (stderr, format)
 #else
 # define Debug(level,format...)
 # define Dprintf(format...)
 #endif
 
-static ALWAYS_INLINE void
+static ALWAYS_INLINE int
 print_error (const char *string)
 {
-#if !HAVE_SGX
-    write (2, string, strlen (string));
-#endif
+  return write (2, string, strlen (string));
 }
 
-#define mi_init		UNWI_ARCH_OBJ(mi_init)
+#define mi_init         UNWI_ARCH_OBJ(mi_init)
 
-extern void mi_init (void);	/* machine-independent initializations */
+extern void mi_init (void);     /* machine-independent initializations */
 extern unw_word_t _U_dyn_info_list_addr (void);
 
 /* This is needed/used by ELF targets only.  */
 
 struct elf_image
-{
-    void *image;		/* pointer to mmap'd image */
-    size_t size;		/* (file-) size of the image */
-};
+  {
+    void *image;                /* pointer to mmap'd image */
+    size_t size;                /* (file-) size of the image */
+  };
 
 struct elf_dyn_info
-{
+  {
     struct elf_image ei;
     unw_dyn_info_t di_cache;
     unw_dyn_info_t di_debug;    /* additional table info for .debug_frame */
@@ -332,21 +337,17 @@
 #if UNW_TARGET_ARM
     unw_dyn_info_t di_arm;      /* additional table info for .ARM.exidx */
 #endif
-};
+  };
 
-static void inline invalidate_edi (struct elf_dyn_info *edi)
+static inline void invalidate_edi (struct elf_dyn_info *edi)
 {
-    if (edi->ei.image)
-#if !HAVE_SGX
-        munmap (edi->ei.image, edi->ei.size);
-#else  /* to align with GET_MEMORY() */
-        free(edi->ei.image);
-#endif
-    memset (edi, 0, sizeof (*edi));
-    edi->di_cache.format = -1;
-    edi->di_debug.format = -1;
+  if (edi->ei.image)
+    FREE_MEMORY (edi->ei.image, edi->ei.size);
+  memset (edi, 0, sizeof (*edi));
+  edi->di_cache.format = -1;
+  edi->di_debug.format = -1;
 #if UNW_TARGET_ARM
-    edi->di_arm.format = -1;
+  edi->di_arm.format = -1;
 #endif
 }
 
@@ -362,16 +363,23 @@
 /* Define GNU and processor specific values for the Phdr p_type field in case
    they aren't defined by <elf.h>.  */
 #ifndef PT_GNU_EH_FRAME
-# define PT_GNU_EH_FRAME	0x6474e550
+# define PT_GNU_EH_FRAME        0x6474e550
 #endif /* !PT_GNU_EH_FRAME */
 #ifndef PT_ARM_EXIDX
-# define PT_ARM_EXIDX		0x70000001	/* ARM unwind segment */
+# define PT_ARM_EXIDX           0x70000001      /* ARM unwind segment */
 #endif /* !PT_ARM_EXIDX */
 
 #include "tdep/libunwind_i.h"
 
 #ifndef tdep_get_func_addr
-# define tdep_get_func_addr(as,addr,v)		(*(v) = addr, 0)
+# define tdep_get_func_addr(as,addr,v)          (*(v) = addr, 0)
 #endif
 
+#ifndef DWARF_VAL_LOC
+# define DWARF_IS_VAL_LOC(l)    0
+# define DWARF_VAL_LOC(c,v)     DWARF_NULL_LOC
+#endif
+
+#define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
+
 #endif /* libunwind_i_h */
diff --git a/sgx_unwind/libunwind/include/mempool.h b/sgx_unwind/libunwind/include/mempool.h
index bfd74a2..1f1c770 100644
--- a/sgx_unwind/libunwind/include/mempool.h
+++ b/sgx_unwind/libunwind/include/mempool.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2003 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -51,26 +51,26 @@
 
 #include "libunwind_i.h"
 
-#define sos_alloc(s)		UNWI_ARCH_OBJ(_sos_alloc)(s)
-#define mempool_init(p,s,r)	UNWI_ARCH_OBJ(_mempool_init)(p,s,r)
-#define mempool_alloc(p)	UNWI_ARCH_OBJ(_mempool_alloc)(p)
-#define mempool_free(p,o)	UNWI_ARCH_OBJ(_mempool_free)(p,o)
+#define sos_alloc(s)            UNWI_ARCH_OBJ(_sos_alloc)(s)
+#define mempool_init(p,s,r)     UNWI_ARCH_OBJ(_mempool_init)(p,s,r)
+#define mempool_alloc(p)        UNWI_ARCH_OBJ(_mempool_alloc)(p)
+#define mempool_free(p,o)       UNWI_ARCH_OBJ(_mempool_free)(p,o)
 
 /* The mempool structure should be treated as an opaque object.  It's
    declared here only to enable static allocation of mempools.  */
 struct mempool
-{
+  {
     pthread_mutex_t lock;
-    size_t obj_size;		/* object size (rounded up for alignment) */
-    size_t chunk_size;		/* allocation granularity */
-    unsigned int reserve;	/* minimum (desired) size of the free-list */
-    unsigned int num_free;	/* number of objects on the free-list */
+    size_t obj_size;            /* object size (rounded up for alignment) */
+    size_t chunk_size;          /* allocation granularity */
+    unsigned int reserve;       /* minimum (desired) size of the free-list */
+    unsigned int num_free;      /* number of objects on the free-list */
     struct object
-    {
+      {
         struct object *next;
-    }
+      }
     *free_list;
-};
+  };
 
 /* Emergency allocation for one-time stuff that doesn't fit the memory
    pool model.  A limited amount of memory is available in this
diff --git a/sgx_unwind/libunwind/include/pthread.h b/sgx_unwind/libunwind/include/pthread.h
new file mode 100644
index 0000000..81926ab
--- /dev/null
+++ b/sgx_unwind/libunwind/include/pthread.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2011-2021 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in
+ *     the documentation and/or other materials provided with the
+ *     distribution.
+ *   * Neither the name of Intel Corporation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+// file: pthread.h
+//
+
+#ifndef PTHREAD_H
+#define PTHREAD_H
+
+#include "pthread_compat.h"
+
+#endif
diff --git a/sgx_unwind/libunwind/include/pthread_compat.h b/sgx_unwind/libunwind/include/pthread_compat.h
index 55d1c77..e367891 100644
--- a/sgx_unwind/libunwind/include/pthread_compat.h
+++ b/sgx_unwind/libunwind/include/pthread_compat.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2019 Intel Corporation. All rights reserved.
+ * Copyright (C) 2011-2021 Intel Corporation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
diff --git a/sgx_unwind/libunwind/include/remote.h b/sgx_unwind/libunwind/include/remote.h
index e6d1d58..064d630 100644
--- a/sgx_unwind/libunwind/include/remote.h
+++ b/sgx_unwind/libunwind/include/remote.h
@@ -11,115 +11,117 @@
 fetch8 (unw_addr_space_t as, unw_accessors_t *a,
         unw_word_t *addr, int8_t *valp, void *arg)
 {
-    *valp = *(int8_t *) (uintptr_t) *addr;
-    *addr += 1;
-    return 0;
+  *valp = *(int8_t *) (uintptr_t) *addr;
+  *addr += 1;
+  return 0;
 }
 
 static inline int
 fetch16 (unw_addr_space_t as, unw_accessors_t *a,
          unw_word_t *addr, int16_t *valp, void *arg)
 {
-    *valp = *(int16_t *) (uintptr_t) *addr;
-    *addr += 2;
-    return 0;
+  *valp = *(int16_t *) (uintptr_t) *addr;
+  *addr += 2;
+  return 0;
 }
 
 static inline int
 fetch32 (unw_addr_space_t as, unw_accessors_t *a,
          unw_word_t *addr, int32_t *valp, void *arg)
 {
-    *valp = *(int32_t *) (uintptr_t) *addr;
-    *addr += 4;
-    return 0;
+  *valp = *(int32_t *) (uintptr_t) *addr;
+  *addr += 4;
+  return 0;
 }
 
 static inline int
 fetchw (unw_addr_space_t as, unw_accessors_t *a,
         unw_word_t *addr, unw_word_t *valp, void *arg)
 {
-    *valp = *(unw_word_t *) (uintptr_t) *addr;
-    *addr += sizeof (unw_word_t);
-    return 0;
+  *valp = *(unw_word_t *) (uintptr_t) *addr;
+  *addr += sizeof (unw_word_t);
+  return 0;
 }
 
 #else /* !UNW_LOCAL_ONLY */
 
-#define WSIZE	(sizeof (unw_word_t))
+#define WSIZE   (sizeof (unw_word_t))
 
 static inline int
 fetch8 (unw_addr_space_t as, unw_accessors_t *a,
         unw_word_t *addr, int8_t *valp, void *arg)
 {
-    unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
-    int ret;
+  unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
+  int ret;
 
-    *addr += 1;
+  *addr += 1;
 
-    ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
+  ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-    val >>= 8*off;
+  val >>= 8*off;
 #else
-    val >>= 8*(WSIZE - 1 - off);
+  val >>= 8*(WSIZE - 1 - off);
 #endif
-    *valp = val & 0xff;
-    return ret;
+  *valp = val & 0xff;
+  return ret;
 }
 
 static inline int
 fetch16 (unw_addr_space_t as, unw_accessors_t *a,
          unw_word_t *addr, int16_t *valp, void *arg)
 {
-    unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
-    int ret;
+  unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
+  int ret;
 
-    assert ((off & 0x1) == 0);
+  if ((off & 0x1) != 0)
+    return -UNW_EINVAL;
 
-    *addr += 2;
+  *addr += 2;
 
-    ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
+  ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-    val >>= 8*off;
+  val >>= 8*off;
 #else
-    val >>= 8*(WSIZE - 2 - off);
+  val >>= 8*(WSIZE - 2 - off);
 #endif
-    *valp = val & 0xffff;
-    return ret;
+  *valp = val & 0xffff;
+  return ret;
 }
 
 static inline int
 fetch32 (unw_addr_space_t as, unw_accessors_t *a,
          unw_word_t *addr, int32_t *valp, void *arg)
 {
-    unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
-    int ret;
+  unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
+  int ret;
 
-    assert ((off & 0x3) == 0);
+  if ((off & 0x3) != 0)
+    return -UNW_EINVAL;
 
-    *addr += 4;
+  *addr += 4;
 
-    ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
+  ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-    val >>= 8*off;
+  val >>= 8*off;
 #else
-    val >>= 8*(WSIZE - 4 - off);
+  val >>= 8*(WSIZE - 4 - off);
 #endif
-    *valp = val & 0xffffffff;
-    return ret;
+  *valp = val & 0xffffffff;
+  return ret;
 }
 
 static inline int
 fetchw (unw_addr_space_t as, unw_accessors_t *a,
         unw_word_t *addr, unw_word_t *valp, void *arg)
 {
-    int ret;
+  int ret;
 
-    ret = (*a->access_mem) (as, *addr, valp, 0, arg);
-    *addr += WSIZE;
-    return ret;
+  ret = (*a->access_mem) (as, *addr, valp, 0, arg);
+  *addr += WSIZE;
+  return ret;
 }
 
 #endif /* !UNW_LOCAL_ONLY */
diff --git a/sgx_unwind/libunwind/include/tdep-x86_64/dwarf-config.h b/sgx_unwind/libunwind/include/tdep-x86_64/dwarf-config.h
index af6816a..ff77808 100644
--- a/sgx_unwind/libunwind/include/tdep-x86_64/dwarf-config.h
+++ b/sgx_unwind/libunwind/include/tdep-x86_64/dwarf-config.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -33,27 +33,25 @@
 
 /* XXX need to verify if this value is correct */
 #ifdef CONFIG_MSABI_SUPPORT
-#define DWARF_NUM_PRESERVED_REGS	33
+#define DWARF_NUM_PRESERVED_REGS        33
 #else
-#define DWARF_NUM_PRESERVED_REGS	17
-#endif
+#define DWARF_NUM_PRESERVED_REGS        17
+#endif 
 
-#define DWARF_REGNUM_MAP_LENGTH		DWARF_NUM_PRESERVED_REGS
+#define DWARF_REGNUM_MAP_LENGTH         DWARF_NUM_PRESERVED_REGS
 
 /* Return TRUE if the ADDR_SPACE uses big-endian byte-order.  */
-#define dwarf_is_big_endian(addr_space)	0
+#define dwarf_is_big_endian(addr_space) 0
 
 /* Convert a pointer to a dwarf_cursor structure to a pointer to
    unw_cursor_t.  */
-#define dwarf_to_cursor(c)	((unw_cursor_t *) (c))
+#define dwarf_to_cursor(c)      ((unw_cursor_t *) (c))
 
 typedef struct dwarf_loc
-{
+  {
     unw_word_t val;
-#ifndef UNW_LOCAL_ONLY
-    unw_word_t type;		/* see X86_LOC_TYPE_* macros.  */
-#endif
-}
+    unw_word_t type;            /* see X86_LOC_TYPE_* macros.  */
+  }
 dwarf_loc_t;
 
 #endif /* dwarf_config_h */
diff --git a/sgx_unwind/libunwind/include/tdep-x86_64/jmpbuf.h b/sgx_unwind/libunwind/include/tdep-x86_64/jmpbuf.h
index 44fcd69..d571966 100644
--- a/sgx_unwind/libunwind/include/tdep-x86_64/jmpbuf.h
+++ b/sgx_unwind/libunwind/include/tdep-x86_64/jmpbuf.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -27,17 +27,17 @@
 
 /* Use glibc's jump-buffer indices; NPTL peeks at SP: */
 
-#define JB_SP		6
-#define JB_RP		7
-#define JB_MASK_SAVED	8
-#define JB_MASK		9
+#define JB_SP           6
+#define JB_RP           7
+#define JB_MASK_SAVED   8
+#define JB_MASK         9
 
 #elif defined __FreeBSD__
 
-#define JB_SP		2
-#define JB_RP		0
+#define JB_SP           2
+#define JB_RP           0
 /* Pretend the ip cannot be 0 and mask is always saved */
-#define JB_MASK_SAVED	0
-#define JB_MASK		9
+#define JB_MASK_SAVED   0
+#define JB_MASK         9
 
 #endif
diff --git a/sgx_unwind/libunwind/include/tdep-x86_64/libunwind_i.h b/sgx_unwind/libunwind/include/tdep-x86_64/libunwind_i.h
index 4f8e398..283525c 100644
--- a/sgx_unwind/libunwind/include/tdep-x86_64/libunwind_i.h
+++ b/sgx_unwind/libunwind/include/tdep-x86_64/libunwind_i.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -39,28 +39,29 @@
 #include "dwarf.h"
 
 typedef enum
-{
+  {
+    UNW_X86_64_FRAME_ALIGNED = -3,       /* frame stack pointer aligned */
     UNW_X86_64_FRAME_STANDARD = -2,     /* regular rbp, rsp +/- offset */
     UNW_X86_64_FRAME_SIGRETURN = -1,    /* special sigreturn frame */
     UNW_X86_64_FRAME_OTHER = 0,         /* not cacheable (special or unrecognised) */
     UNW_X86_64_FRAME_GUESSED = 1        /* guessed it was regular, but not known */
-}
+  }
 unw_tdep_frame_type_t;
 
 typedef struct
-{
+  {
     uint64_t virtual_address;
-    int64_t frame_type     : 2;  /* unw_tdep_frame_type_t classification */
+    int64_t frame_type     : 3;  /* unw_tdep_frame_type_t classification */
     int64_t last_frame     : 1;  /* non-zero if last frame in chain */
     int64_t cfa_reg_rsp    : 1;  /* cfa dwarf base register is rsp vs. rbp */
-    int64_t cfa_reg_offset : 30; /* cfa is at this offset from base register value */
+    int64_t cfa_reg_offset : 29; /* cfa is at this offset from base register value */
     int64_t rbp_cfa_offset : 15; /* rbp saved at this offset from cfa (-1 = not saved) */
     int64_t rsp_cfa_offset : 15; /* rsp saved at this offset from cfa (-1 = not saved) */
-}
+  }
 unw_tdep_frame_t;
 
 struct unw_addr_space
-{
+  {
     struct unw_accessors acc;
     unw_caching_policy_t caching_policy;
 #ifdef HAVE_ATOMIC_OPS_H
@@ -68,163 +69,170 @@
 #else
     uint32_t cache_generation;
 #endif
-    unw_word_t dyn_generation;		/* see dyn-common.h */
-    unw_word_t dyn_info_list_addr;	/* (cached) dyn_info_list_addr */
+    unw_word_t dyn_generation;          /* see dyn-common.h */
+    unw_word_t dyn_info_list_addr;      /* (cached) dyn_info_list_addr */
     struct dwarf_rs_cache global_cache;
     struct unw_debug_frame_list *debug_frames;
-};
+   };
 
 struct cursor
-{
-    struct dwarf_cursor dwarf;		/* must be first */
+  {
+    struct dwarf_cursor dwarf;          /* must be first */
 
-    unw_tdep_frame_t frame_info;	/* quick tracing assist info */
+    unw_tdep_frame_t frame_info;        /* quick tracing assist info */
 
     /* Format of sigcontext structure and address at which it is
        stored: */
     enum
-    {
-        X86_64_SCF_NONE,		/* no signal frame encountered */
-        X86_64_SCF_LINUX_RT_SIGFRAME,	/* Linux ucontext_t */
-        X86_64_SCF_FREEBSD_SIGFRAME,	/* FreeBSD signal frame */
-        X86_64_SCF_FREEBSD_SYSCALL,	/* FreeBSD syscall */
-    }
+      {
+        X86_64_SCF_NONE,                /* no signal frame encountered */
+        X86_64_SCF_LINUX_RT_SIGFRAME,   /* Linux ucontext_t */
+        X86_64_SCF_FREEBSD_SIGFRAME,    /* FreeBSD signal frame */
+        X86_64_SCF_FREEBSD_SYSCALL,     /* FreeBSD syscall */
+      }
     sigcontext_format;
     unw_word_t sigcontext_addr;
     int validate;
     ucontext_t *uc;
-};
+  };
 
 static inline ucontext_t *
 dwarf_get_uc(const struct dwarf_cursor *cursor)
 {
-    const struct cursor *c = (struct cursor *) cursor->as_arg;
-    return c->uc;
+  const struct cursor *c = (struct cursor *) cursor->as_arg;
+  return c->uc;
 }
 
-#define DWARF_GET_LOC(l)	((l).val)
+#define DWARF_GET_LOC(l)        ((l).val)
+# define DWARF_LOC_TYPE_MEM     (0 << 0)
+# define DWARF_LOC_TYPE_FP      (1 << 0)
+# define DWARF_LOC_TYPE_REG     (1 << 1)
+# define DWARF_LOC_TYPE_VAL     (1 << 2)
+
+# define DWARF_IS_REG_LOC(l)    (((l).type & DWARF_LOC_TYPE_REG) != 0)
+# define DWARF_IS_FP_LOC(l)     (((l).type & DWARF_LOC_TYPE_FP) != 0)
+# define DWARF_IS_MEM_LOC(l)    ((l).type == DWARF_LOC_TYPE_MEM)
+# define DWARF_IS_VAL_LOC(l)    (((l).type & DWARF_LOC_TYPE_VAL) != 0)
+
+# define DWARF_LOC(r, t)        ((dwarf_loc_t) { .val = (r), .type = (t) })
+# define DWARF_VAL_LOC(c,v)     DWARF_LOC ((v), DWARF_LOC_TYPE_VAL)
+# define DWARF_MEM_LOC(c,m)     DWARF_LOC ((m), DWARF_LOC_TYPE_MEM)
 
 #ifdef UNW_LOCAL_ONLY
-# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
-# define DWARF_IS_NULL_LOC(l)	(DWARF_GET_LOC (l) == 0)
-# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r) })
-# define DWARF_IS_REG_LOC(l)	0
-# define DWARF_REG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
-				 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
-# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
-# define DWARF_FPREG_LOC(c,r)	(DWARF_LOC((unw_word_t)			     \
-				 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
+# define DWARF_NULL_LOC         DWARF_LOC (0, 0)
+# define DWARF_IS_NULL_LOC(l)   (DWARF_GET_LOC (l) == 0)
+# define DWARF_REG_LOC(c,r)     (DWARF_LOC((unw_word_t)                      \
+                                 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
+# define DWARF_FPREG_LOC(c,r)   (DWARF_LOC((unw_word_t)                      \
+                                 x86_64_r_uc_addr(dwarf_get_uc(c), (r)), 0))
+
 #else /* !UNW_LOCAL_ONLY */
 
-# define DWARF_LOC_TYPE_FP	(1 << 0)
-# define DWARF_LOC_TYPE_REG	(1 << 1)
-# define DWARF_NULL_LOC		DWARF_LOC (0, 0)
-# define DWARF_IS_NULL_LOC(l)						\
-		({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
-# define DWARF_LOC(r, t)	((dwarf_loc_t) { .val = (r), .type = (t) })
-# define DWARF_IS_REG_LOC(l)	(((l).type & DWARF_LOC_TYPE_REG) != 0)
-# define DWARF_IS_FP_LOC(l)	(((l).type & DWARF_LOC_TYPE_FP) != 0)
-# define DWARF_REG_LOC(c,r)	DWARF_LOC((r), DWARF_LOC_TYPE_REG)
-# define DWARF_MEM_LOC(c,m)	DWARF_LOC ((m), 0)
-# define DWARF_FPREG_LOC(c,r)	DWARF_LOC((r), (DWARF_LOC_TYPE_REG	\
-						| DWARF_LOC_TYPE_FP))
+# define DWARF_NULL_LOC         DWARF_LOC (0, 0)
+# define DWARF_IS_NULL_LOC(l)                                           \
+                ({ dwarf_loc_t _l = (l); _l.val == 0 && _l.type == 0; })
+# define DWARF_REG_LOC(c,r)     DWARF_LOC((r), DWARF_LOC_TYPE_REG)
+# define DWARF_FPREG_LOC(c,r)   DWARF_LOC((r), (DWARF_LOC_TYPE_REG      \
+                                                | DWARF_LOC_TYPE_FP))
 
 #endif /* !UNW_LOCAL_ONLY */
 
 static inline int
 dwarf_getfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t *val)
 {
-    if (DWARF_IS_NULL_LOC (loc))
-        return -UNW_EBADREG;
+  if (DWARF_IS_NULL_LOC (loc))
+    return -UNW_EBADREG;
 
-    abort ();
+  abort ();
 }
 
 static inline int
 dwarf_putfp (struct dwarf_cursor *c, dwarf_loc_t loc, unw_fpreg_t val)
 {
-    if (DWARF_IS_NULL_LOC (loc))
-        return -UNW_EBADREG;
+  if (DWARF_IS_NULL_LOC (loc))
+    return -UNW_EBADREG;
 
-    abort ();
+  abort ();
 }
 
 static inline int
 dwarf_get (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t *val)
 {
-    if (DWARF_IS_NULL_LOC (loc))
-        return -UNW_EBADREG;
+  if (DWARF_IS_NULL_LOC (loc))
+    return -UNW_EBADREG;
 
-    if (DWARF_IS_REG_LOC (loc))
-        return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
-                                         0, c->as_arg);
-    else
-        return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
-                                         0, c->as_arg);
+  if (DWARF_IS_REG_LOC (loc))
+    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), val,
+                                     0, c->as_arg);
+  if (DWARF_IS_MEM_LOC (loc))
+    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), val,
+                                     0, c->as_arg);
+  assert(DWARF_IS_VAL_LOC (loc));
+  *val = DWARF_GET_LOC (loc);
+  return 0;
 }
 
 static inline int
 dwarf_put (struct dwarf_cursor *c, dwarf_loc_t loc, unw_word_t val)
 {
-    if (DWARF_IS_NULL_LOC (loc))
-        return -UNW_EBADREG;
+  assert(!DWARF_IS_VAL_LOC (loc));
 
-    if (DWARF_IS_REG_LOC (loc))
-        return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
-                                         1, c->as_arg);
-    else
-        return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
-                                         1, c->as_arg);
+  if (DWARF_IS_NULL_LOC (loc))
+    return -UNW_EBADREG;
+
+  if (DWARF_IS_REG_LOC (loc))
+    return (*c->as->acc.access_reg) (c->as, DWARF_GET_LOC (loc), &val,
+                                     1, c->as_arg);
+  else
+    return (*c->as->acc.access_mem) (c->as, DWARF_GET_LOC (loc), &val,
+                                     1, c->as_arg);
 }
 
-#define tdep_getcontext_trace	        UNW_ARCH_OBJ(getcontext_trace)
-#define tdep_needs_initialization	UNW_OBJ(needs_initialization)
-#define tdep_init_mem_validate		UNW_OBJ(init_mem_validate)
-#define tdep_init			UNW_OBJ(init)
+#define tdep_getcontext_trace           UNW_ARCH_OBJ(getcontext_trace)
+#define tdep_init_done                  UNW_OBJ(init_done)
+#define tdep_init_mem_validate          UNW_OBJ(init_mem_validate)
+#define tdep_init                       UNW_OBJ(init)
 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
    tdep_search_unwind_table.  */
-#define tdep_search_unwind_table	dwarf_search_unwind_table
-#define tdep_find_unwind_table		dwarf_find_unwind_table
-#define tdep_get_elf_image		UNW_ARCH_OBJ(get_elf_image)
-#define tdep_access_reg			UNW_OBJ(access_reg)
-#define tdep_access_fpreg		UNW_OBJ(access_fpreg)
+#define tdep_search_unwind_table        dwarf_search_unwind_table
+#define tdep_find_unwind_table          dwarf_find_unwind_table
+#define tdep_get_elf_image              UNW_ARCH_OBJ(get_elf_image)
+#define tdep_get_exe_image_path         UNW_ARCH_OBJ(get_exe_image_path)
+#define tdep_access_reg                 UNW_OBJ(access_reg)
+#define tdep_access_fpreg               UNW_OBJ(access_fpreg)
 #if __linux__
-# define tdep_fetch_frame		UNW_OBJ(fetch_frame)
-# define tdep_cache_frame		UNW_OBJ(cache_frame)
-# define tdep_reuse_frame		UNW_OBJ(reuse_frame)
+# define tdep_fetch_frame               UNW_OBJ(fetch_frame)
+# define tdep_cache_frame               UNW_OBJ(cache_frame)
+# define tdep_reuse_frame               UNW_OBJ(reuse_frame)
 #else
-# define tdep_fetch_frame(c,ip,n)	do {} while(0)
-# define tdep_cache_frame(c,rs)		do {} while(0)
-# define tdep_reuse_frame(c,rs)		do {} while(0)
+# define tdep_fetch_frame(c,ip,n)       do {} while(0)
+# define tdep_cache_frame(c)            0
+# define tdep_reuse_frame(c,frame)      do {} while(0)
 #endif
-#define tdep_stash_frame		UNW_OBJ(stash_frame)
-#if HAVE_SGX
-#define tdep_trace(cur,addr,n)		(-UNW_ENOINFO)
-#else
-#define tdep_trace			UNW_OBJ(tdep_trace)
-#endif
+#define tdep_stash_frame                UNW_OBJ(stash_frame)
+#define tdep_trace                      UNW_OBJ(tdep_trace)
 #define x86_64_r_uc_addr                UNW_OBJ(r_uc_addr)
 
 #ifdef UNW_LOCAL_ONLY
-# define tdep_find_proc_info(c,ip,n)				\
-	dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),	\
-				       (c)->as_arg)
-# define tdep_put_unwind_info(as,pi,arg)		\
-	dwarf_put_unwind_info((as), (pi), (arg))
+# define tdep_find_proc_info(c,ip,n)                            \
+        dwarf_find_proc_info((c)->as, (ip), &(c)->pi, (n),      \
+                                       (c)->as_arg)
+# define tdep_put_unwind_info(as,pi,arg)                \
+        dwarf_put_unwind_info((as), (pi), (arg))
 #else
-# define tdep_find_proc_info(c,ip,n)					\
-	(*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),	\
-				       (c)->as_arg)
-# define tdep_put_unwind_info(as,pi,arg)			\
-	(*(as)->acc.put_unwind_info)((as), (pi), (arg))
+# define tdep_find_proc_info(c,ip,n)                                    \
+        (*(c)->as->acc.find_proc_info)((c)->as, (ip), &(c)->pi, (n),    \
+                                       (c)->as_arg)
+# define tdep_put_unwind_info(as,pi,arg)                        \
+        (*(as)->acc.put_unwind_info)((as), (pi), (arg))
 #endif
 
-#define tdep_get_as(c)			((c)->dwarf.as)
-#define tdep_get_as_arg(c)		((c)->dwarf.as_arg)
-#define tdep_get_ip(c)			((c)->dwarf.ip)
-#define tdep_big_endian(as)		0
+#define tdep_get_as(c)                  ((c)->dwarf.as)
+#define tdep_get_as_arg(c)              ((c)->dwarf.as_arg)
+#define tdep_get_ip(c)                  ((c)->dwarf.ip)
+#define tdep_big_endian(as)             0
 
-extern int tdep_needs_initialization;
+extern int tdep_init_done;
 
 extern void tdep_init (void);
 extern void tdep_init_mem_validate (void);
@@ -235,6 +243,7 @@
 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
                                unsigned long *segbase, unsigned long *mapoff,
                                char *path, size_t pathlen);
+extern void tdep_get_exe_image_path (char *path);
 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
                             unw_word_t *valp, int write);
 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
@@ -242,18 +251,14 @@
 #if __linux__
 extern void tdep_fetch_frame (struct dwarf_cursor *c, unw_word_t ip,
                               int need_unwind_info);
-extern void tdep_cache_frame (struct dwarf_cursor *c,
-                              struct dwarf_reg_state *rs);
+extern int tdep_cache_frame (struct dwarf_cursor *c);
 extern void tdep_reuse_frame (struct dwarf_cursor *c,
-                              struct dwarf_reg_state *rs);
+                              int frame);
 extern void tdep_stash_frame (struct dwarf_cursor *c,
                               struct dwarf_reg_state *rs);
 #endif
 
 extern int tdep_getcontext_trace (unw_tdep_context_t *);
-
-#if (!HAVE_SGX)
 extern int tdep_trace (unw_cursor_t *cursor, void **addresses, int *n);
-#endif
 
 #endif /* X86_64_LIBUNWIND_I_H */
diff --git a/sgx_unwind/libunwind/include/tdep/dwarf-config.h b/sgx_unwind/libunwind/include/tdep/dwarf-config.h
index ff5715f..c759a46 100644
--- a/sgx_unwind/libunwind/include/tdep/dwarf-config.h
+++ b/sgx_unwind/libunwind/include/tdep/dwarf-config.h
@@ -1,7 +1,9 @@
 /* Provide a real file - not a symlink - as it would cause multiarch conflicts
    when multiple different arch releases are installed simultaneously.  */
 
-#if defined __arm__
+#if defined __aarch64__
+# include "tdep-aarch64/dwarf-config.h"
+#elif defined __arm__
 # include "tdep-arm/dwarf-config.h"
 #elif defined __hppa__
 # include "tdep-hppa/dwarf-config.h"
@@ -13,10 +15,14 @@
 # include "tdep-ppc32/dwarf-config.h"
 #elif defined __powerpc64__
 # include "tdep-ppc64/dwarf-config.h"
+#elif defined __sh__
+# include "tdep-sh/dwarf-config.h"
 #elif defined __i386__
 # include "tdep-x86/dwarf-config.h"
 #elif defined __x86_64__ || defined __amd64__
 # include "tdep-x86_64/dwarf-config.h"
+#elif defined __tilegx__
+# include "tdep-tilegx/dwarf-config.h"
 #else
 # error "Unsupported arch"
 #endif
diff --git a/sgx_unwind/libunwind/include/tdep/jmpbuf.h b/sgx_unwind/libunwind/include/tdep/jmpbuf.h
index 7d04a42..13093a0 100644
--- a/sgx_unwind/libunwind/include/tdep/jmpbuf.h
+++ b/sgx_unwind/libunwind/include/tdep/jmpbuf.h
@@ -3,7 +3,9 @@
 
 #ifndef UNW_REMOTE_ONLY
 
-#if defined __arm__
+#if defined __aarch64__
+# include "tdep-aarch64/jmpbuf.h"
+#elif defined __arm__
 # include "tdep-arm/jmpbuf.h"
 #elif defined __hppa__
 # include "tdep-hppa/jmpbuf.h"
@@ -19,6 +21,8 @@
 # include "tdep-x86/jmpbuf.h"
 #elif defined __x86_64__
 # include "tdep-x86_64/jmpbuf.h"
+#elif defined __tilegx__
+# include "tdep-tilegx/jmpbuf.h"
 #else
 # error "Unsupported arch"
 #endif
diff --git a/sgx_unwind/libunwind/include/tdep/libunwind_i.h.in b/sgx_unwind/libunwind/include/tdep/libunwind_i.h.in
index 96f49fb..af05a7f 100644
--- a/sgx_unwind/libunwind/include/tdep/libunwind_i.h.in
+++ b/sgx_unwind/libunwind/include/tdep/libunwind_i.h.in
@@ -3,7 +3,9 @@
 
 #ifndef UNW_REMOTE_ONLY
 
-#if defined __arm__
+#if defined __aarch64__
+# include "tdep-aarch64/libunwind_i.h"
+#elif defined __arm__
 # include "tdep-arm/libunwind_i.h"
 #elif defined __hppa__
 # include "tdep-hppa/libunwind_i.h"
@@ -15,10 +17,14 @@
 # include "tdep-ppc32/libunwind_i.h"
 #elif defined __powerpc64__
 # include "tdep-ppc64/libunwind_i.h"
+#elif defined __sh__
+# include "tdep-sh/libunwind_i.h"
 #elif defined __i386__
 # include "tdep-x86/libunwind_i.h"
 #elif defined __x86_64__
 # include "tdep-x86_64/libunwind_i.h"
+#elif defined __tilegx__
+# include "tdep-tilegx/libunwind_i.h"
 #else
 # error "Unsupported arch"
 #endif
diff --git a/sgx_unwind/libunwind/include/unwind.h b/sgx_unwind/libunwind/include/unwind.h
index f091bbb..7cf128d 100644
--- a/sgx_unwind/libunwind/include/unwind.h
+++ b/sgx_unwind/libunwind/include/unwind.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -35,10 +35,10 @@
 
 /* Minimal interface as per C++ ABI draft standard:
 
-	http://www.codesourcery.com/cxx-abi/abi-eh.html */
+        http://www.codesourcery.com/cxx-abi/abi-eh.html */
 
 typedef enum
-{
+  {
     _URC_NO_REASON = 0,
     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
     _URC_FATAL_PHASE2_ERROR = 2,
@@ -48,43 +48,43 @@
     _URC_HANDLER_FOUND = 6,
     _URC_INSTALL_CONTEXT = 7,
     _URC_CONTINUE_UNWIND = 8
-}
+  }
 _Unwind_Reason_Code;
 
 typedef int _Unwind_Action;
 
-#define _UA_SEARCH_PHASE	1
-#define _UA_CLEANUP_PHASE	2
-#define _UA_HANDLER_FRAME	4
-#define _UA_FORCE_UNWIND	8
+#define _UA_SEARCH_PHASE        1
+#define _UA_CLEANUP_PHASE       2
+#define _UA_HANDLER_FRAME       4
+#define _UA_FORCE_UNWIND        8
 
-struct _Unwind_Context;		/* opaque data-structure */
-struct _Unwind_Exception;	/* forward-declaration */
+struct _Unwind_Context;         /* opaque data-structure */
+struct _Unwind_Exception;       /* forward-declaration */
 
 typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code,
-        struct _Unwind_Exception *);
+                                              struct _Unwind_Exception *);
 
 typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn) (int, _Unwind_Action,
-        uint64_t,
-        struct _Unwind_Exception *,
-        struct _Unwind_Context *,
-        void *);
+                                                uint64_t,
+                                                struct _Unwind_Exception *,
+                                                struct _Unwind_Context *,
+                                                void *);
 
 /* The C++ ABI requires exception_class, private_1, and private_2 to
    be of type uint64 and the entire structure to be
-   double-word-aligned. Please note that exception_class stays 64-bit
+   double-word-aligned. Please note that exception_class stays 64-bit 
    even on 32-bit machines for gcc compatibility.  */
 struct _Unwind_Exception
-{
+  {
     uint64_t exception_class;
     _Unwind_Exception_Cleanup_Fn exception_cleanup;
     unsigned long private_1;
     unsigned long private_2;
-} __attribute__((__aligned__));
+  } __attribute__((__aligned__));
 
 extern _Unwind_Reason_Code _Unwind_RaiseException (struct _Unwind_Exception *);
 extern _Unwind_Reason_Code _Unwind_ForcedUnwind (struct _Unwind_Exception *,
-        _Unwind_Stop_Fn, void *);
+                                                 _Unwind_Stop_Fn, void *);
 extern void _Unwind_Resume (struct _Unwind_Exception *);
 extern void _Unwind_DeleteException (struct _Unwind_Exception *);
 extern unsigned long _Unwind_GetGR (struct _Unwind_Context *, int);
@@ -100,17 +100,17 @@
 /* Callback for _Unwind_Backtrace().  The backtrace stops immediately
    if the callback returns any value other than _URC_NO_REASON. */
 typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn) (struct _Unwind_Context *,
-        void *);
+                                                 void *);
 
 /* See http://gcc.gnu.org/ml/gcc-patches/2001-09/msg00082.html for why
    _UA_END_OF_STACK exists.  */
-# define _UA_END_OF_STACK	16
+# define _UA_END_OF_STACK       16
 
 /* If the unwind was initiated due to a forced unwind, resume that
    operation, else re-raise the exception.  This is used by
    __cxa_rethrow().  */
 extern _Unwind_Reason_Code
-_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
+          _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *);
 
 /* See http://gcc.gnu.org/ml/gcc-patches/2003-09/msg00154.html for why
    _Unwind_GetBSP() exists.  */
diff --git a/sgx_unwind/libunwind/include/x86/jmpbuf.h b/sgx_unwind/libunwind/include/x86/jmpbuf.h
index a0eb072..94d5984 100644
--- a/sgx_unwind/libunwind/include/x86/jmpbuf.h
+++ b/sgx_unwind/libunwind/include/x86/jmpbuf.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,7 +25,7 @@
 
 /* Use glibc's jump-buffer indices; NPTL peeks at SP: */
 
-#define JB_SP		4
-#define JB_RP		5
-#define JB_MASK_SAVED	6
-#define JB_MASK		7
+#define JB_SP           4
+#define JB_RP           5
+#define JB_MASK_SAVED   6
+#define JB_MASK         7
diff --git a/sgx_unwind/libunwind/m4/libtool.m4 b/sgx_unwind/libunwind/m4/libtool.m4
new file mode 100644
index 0000000..ee80844
--- /dev/null
+++ b/sgx_unwind/libunwind/m4/libtool.m4
@@ -0,0 +1,8387 @@
+# libtool.m4 - Configure libtool for the host system. -*-Autoconf-*-
+#
+#   Copyright (C) 1996-2001, 2003-2015 Free Software Foundation, Inc.
+#   Written by Gordon Matzigkeit, 1996
+#
+# This file is free software; the Free Software Foundation gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+m4_define([_LT_COPYING], [dnl
+# Copyright (C) 2014 Free Software Foundation, Inc.
+# This is free software; see the source for copying conditions.  There is NO
+# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+# GNU Libtool 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 2 of of the License, or
+# (at your option) any later version.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program or library that is built
+# using GNU Libtool, you may include this file under the  same
+# distribution terms that you use for the rest of that program.
+#
+# GNU Libtool 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, see <http://www.gnu.org/licenses/>.
+])
+
+# serial 58 LT_INIT
+
+
+# LT_PREREQ(VERSION)
+# ------------------
+# Complain and exit if this libtool version is less that VERSION.
+m4_defun([LT_PREREQ],
+[m4_if(m4_version_compare(m4_defn([LT_PACKAGE_VERSION]), [$1]), -1,
+       [m4_default([$3],
+		   [m4_fatal([Libtool version $1 or higher is required],
+		             63)])],
+       [$2])])
+
+
+# _LT_CHECK_BUILDDIR
+# ------------------
+# Complain if the absolute build directory name contains unusual characters
+m4_defun([_LT_CHECK_BUILDDIR],
+[case `pwd` in
+  *\ * | *\	*)
+    AC_MSG_WARN([Libtool does not cope well with whitespace in `pwd`]) ;;
+esac
+])
+
+
+# LT_INIT([OPTIONS])
+# ------------------
+AC_DEFUN([LT_INIT],
+[AC_PREREQ([2.62])dnl We use AC_PATH_PROGS_FEATURE_CHECK
+AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT])dnl
+AC_BEFORE([$0], [LT_LANG])dnl
+AC_BEFORE([$0], [LT_OUTPUT])dnl
+AC_BEFORE([$0], [LTDL_INIT])dnl
+m4_require([_LT_CHECK_BUILDDIR])dnl
+
+dnl Autoconf doesn't catch unexpanded LT_ macros by default:
+m4_pattern_forbid([^_?LT_[A-Z_]+$])dnl
+m4_pattern_allow([^(_LT_EOF|LT_DLGLOBAL|LT_DLLAZY_OR_NOW|LT_MULTI_MODULE)$])dnl
+dnl aclocal doesn't pull ltoptions.m4, ltsugar.m4, or ltversion.m4
+dnl unless we require an AC_DEFUNed macro:
+AC_REQUIRE([LTOPTIONS_VERSION])dnl
+AC_REQUIRE([LTSUGAR_VERSION])dnl
+AC_REQUIRE([LTVERSION_VERSION])dnl
+AC_REQUIRE([LTOBSOLETE_VERSION])dnl
+m4_require([_LT_PROG_LTMAIN])dnl
+
+_LT_SHELL_INIT([SHELL=${CONFIG_SHELL-/bin/sh}])
+
+dnl Parse OPTIONS
+_LT_SET_OPTIONS([$0], [$1])
+
+# This can be used to rebuild libtool when needed
+LIBTOOL_DEPS=$ltmain
+
+# Always use our own libtool.
+LIBTOOL='$(SHELL) $(top_builddir)/libtool'
+AC_SUBST(LIBTOOL)dnl
+
+_LT_SETUP
+
+# Only expand once:
+m4_define([LT_INIT])
+])# LT_INIT
+
+# Old names:
+AU_ALIAS([AC_PROG_LIBTOOL], [LT_INIT])
+AU_ALIAS([AM_PROG_LIBTOOL], [LT_INIT])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_PROG_LIBTOOL], [])
+dnl AC_DEFUN([AM_PROG_LIBTOOL], [])
+
+
+# _LT_PREPARE_CC_BASENAME
+# -----------------------
+m4_defun([_LT_PREPARE_CC_BASENAME], [
+# Calculate cc_basename.  Skip known compiler wrappers and cross-prefix.
+func_cc_basename ()
+{
+    for cc_temp in @S|@*""; do
+      case $cc_temp in
+        compile | *[[\\/]]compile | ccache | *[[\\/]]ccache ) ;;
+        distcc | *[[\\/]]distcc | purify | *[[\\/]]purify ) ;;
+        \-*) ;;
+        *) break;;
+      esac
+    done
+    func_cc_basename_result=`$ECHO "$cc_temp" | $SED "s%.*/%%; s%^$host_alias-%%"`
+}
+])# _LT_PREPARE_CC_BASENAME
+
+
+# _LT_CC_BASENAME(CC)
+# -------------------
+# It would be clearer to call AC_REQUIREs from _LT_PREPARE_CC_BASENAME,
+# but that macro is also expanded into generated libtool script, which
+# arranges for $SED and $ECHO to be set by different means.
+m4_defun([_LT_CC_BASENAME],
+[m4_require([_LT_PREPARE_CC_BASENAME])dnl
+AC_REQUIRE([_LT_DECL_SED])dnl
+AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl
+func_cc_basename $1
+cc_basename=$func_cc_basename_result
+])
+
+
+# _LT_FILEUTILS_DEFAULTS
+# ----------------------
+# It is okay to use these file commands and assume they have been set
+# sensibly after 'm4_require([_LT_FILEUTILS_DEFAULTS])'.
+m4_defun([_LT_FILEUTILS_DEFAULTS],
+[: ${CP="cp -f"}
+: ${MV="mv -f"}
+: ${RM="rm -f"}
+])# _LT_FILEUTILS_DEFAULTS
+
+
+# _LT_SETUP
+# ---------
+m4_defun([_LT_SETUP],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_CANONICAL_BUILD])dnl
+AC_REQUIRE([_LT_PREPARE_SED_QUOTE_VARS])dnl
+AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])dnl
+
+_LT_DECL([], [PATH_SEPARATOR], [1], [The PATH separator for the build system])dnl
+dnl
+_LT_DECL([], [host_alias], [0], [The host system])dnl
+_LT_DECL([], [host], [0])dnl
+_LT_DECL([], [host_os], [0])dnl
+dnl
+_LT_DECL([], [build_alias], [0], [The build system])dnl
+_LT_DECL([], [build], [0])dnl
+_LT_DECL([], [build_os], [0])dnl
+dnl
+AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([LT_PATH_LD])dnl
+AC_REQUIRE([LT_PATH_NM])dnl
+dnl
+AC_REQUIRE([AC_PROG_LN_S])dnl
+test -z "$LN_S" && LN_S="ln -s"
+_LT_DECL([], [LN_S], [1], [Whether we need soft or hard links])dnl
+dnl
+AC_REQUIRE([LT_CMD_MAX_LEN])dnl
+_LT_DECL([objext], [ac_objext], [0], [Object file suffix (normally "o")])dnl
+_LT_DECL([], [exeext], [0], [Executable file suffix (normally "")])dnl
+dnl
+m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_CHECK_SHELL_FEATURES])dnl
+m4_require([_LT_PATH_CONVERSION_FUNCTIONS])dnl
+m4_require([_LT_CMD_RELOAD])dnl
+m4_require([_LT_CHECK_MAGIC_METHOD])dnl
+m4_require([_LT_CHECK_SHAREDLIB_FROM_LINKLIB])dnl
+m4_require([_LT_CMD_OLD_ARCHIVE])dnl
+m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl
+m4_require([_LT_WITH_SYSROOT])dnl
+m4_require([_LT_CMD_TRUNCATE])dnl
+
+_LT_CONFIG_LIBTOOL_INIT([
+# See if we are running on zsh, and set the options that allow our
+# commands through without removal of \ escapes INIT.
+if test -n "\${ZSH_VERSION+set}"; then
+   setopt NO_GLOB_SUBST
+fi
+])
+if test -n "${ZSH_VERSION+set}"; then
+   setopt NO_GLOB_SUBST
+fi
+
+_LT_CHECK_OBJDIR
+
+m4_require([_LT_TAG_COMPILER])dnl
+
+case $host_os in
+aix3*)
+  # AIX sometimes has problems with the GCC collect2 program.  For some
+  # reason, if we set the COLLECT_NAMES environment variable, the problems
+  # vanish in a puff of smoke.
+  if test set != "${COLLECT_NAMES+set}"; then
+    COLLECT_NAMES=
+    export COLLECT_NAMES
+  fi
+  ;;
+esac
+
+# Global variables:
+ofile=libtool
+can_build_shared=yes
+
+# All known linkers require a '.a' archive for static linking (except MSVC,
+# which needs '.lib').
+libext=a
+
+with_gnu_ld=$lt_cv_prog_gnu_ld
+
+old_CC=$CC
+old_CFLAGS=$CFLAGS
+
+# Set sane defaults for various variables
+test -z "$CC" && CC=cc
+test -z "$LTCC" && LTCC=$CC
+test -z "$LTCFLAGS" && LTCFLAGS=$CFLAGS
+test -z "$LD" && LD=ld
+test -z "$ac_objext" && ac_objext=o
+
+_LT_CC_BASENAME([$compiler])
+
+# Only perform the check for file, if the check method requires it
+test -z "$MAGIC_CMD" && MAGIC_CMD=file
+case $deplibs_check_method in
+file_magic*)
+  if test "$file_magic_cmd" = '$MAGIC_CMD'; then
+    _LT_PATH_MAGIC
+  fi
+  ;;
+esac
+
+# Use C for the default configuration in the libtool script
+LT_SUPPORTED_TAG([CC])
+_LT_LANG_C_CONFIG
+_LT_LANG_DEFAULT_CONFIG
+_LT_CONFIG_COMMANDS
+])# _LT_SETUP
+
+
+# _LT_PREPARE_SED_QUOTE_VARS
+# --------------------------
+# Define a few sed substitution that help us do robust quoting.
+m4_defun([_LT_PREPARE_SED_QUOTE_VARS],
+[# Backslashify metacharacters that are still active within
+# double-quoted strings.
+sed_quote_subst='s/\([["`$\\]]\)/\\\1/g'
+
+# Same as above, but do not quote variable references.
+double_quote_subst='s/\([["`\\]]\)/\\\1/g'
+
+# Sed substitution to delay expansion of an escaped shell variable in a
+# double_quote_subst'ed string.
+delay_variable_subst='s/\\\\\\\\\\\$/\\\\\\$/g'
+
+# Sed substitution to delay expansion of an escaped single quote.
+delay_single_quote_subst='s/'\''/'\'\\\\\\\'\''/g'
+
+# Sed substitution to avoid accidental globbing in evaled expressions
+no_glob_subst='s/\*/\\\*/g'
+])
+
+# _LT_PROG_LTMAIN
+# ---------------
+# Note that this code is called both from 'configure', and 'config.status'
+# now that we use AC_CONFIG_COMMANDS to generate libtool.  Notably,
+# 'config.status' has no value for ac_aux_dir unless we are using Automake,
+# so we pass a copy along to make sure it has a sensible value anyway.
+m4_defun([_LT_PROG_LTMAIN],
+[m4_ifdef([AC_REQUIRE_AUX_FILE], [AC_REQUIRE_AUX_FILE([ltmain.sh])])dnl
+_LT_CONFIG_LIBTOOL_INIT([ac_aux_dir='$ac_aux_dir'])
+ltmain=$ac_aux_dir/ltmain.sh
+])# _LT_PROG_LTMAIN
+
+
+## ------------------------------------- ##
+## Accumulate code for creating libtool. ##
+## ------------------------------------- ##
+
+# So that we can recreate a full libtool script including additional
+# tags, we accumulate the chunks of code to send to AC_CONFIG_COMMANDS
+# in macros and then make a single call at the end using the 'libtool'
+# label.
+
+
+# _LT_CONFIG_LIBTOOL_INIT([INIT-COMMANDS])
+# ----------------------------------------
+# Register INIT-COMMANDS to be passed to AC_CONFIG_COMMANDS later.
+m4_define([_LT_CONFIG_LIBTOOL_INIT],
+[m4_ifval([$1],
+          [m4_append([_LT_OUTPUT_LIBTOOL_INIT],
+                     [$1
+])])])
+
+# Initialize.
+m4_define([_LT_OUTPUT_LIBTOOL_INIT])
+
+
+# _LT_CONFIG_LIBTOOL([COMMANDS])
+# ------------------------------
+# Register COMMANDS to be passed to AC_CONFIG_COMMANDS later.
+m4_define([_LT_CONFIG_LIBTOOL],
+[m4_ifval([$1],
+          [m4_append([_LT_OUTPUT_LIBTOOL_COMMANDS],
+                     [$1
+])])])
+
+# Initialize.
+m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS])
+
+
+# _LT_CONFIG_SAVE_COMMANDS([COMMANDS], [INIT_COMMANDS])
+# -----------------------------------------------------
+m4_defun([_LT_CONFIG_SAVE_COMMANDS],
+[_LT_CONFIG_LIBTOOL([$1])
+_LT_CONFIG_LIBTOOL_INIT([$2])
+])
+
+
+# _LT_FORMAT_COMMENT([COMMENT])
+# -----------------------------
+# Add leading comment marks to the start of each line, and a trailing
+# full-stop to the whole comment if one is not present already.
+m4_define([_LT_FORMAT_COMMENT],
+[m4_ifval([$1], [
+m4_bpatsubst([m4_bpatsubst([$1], [^ *], [# ])],
+              [['`$\]], [\\\&])]m4_bmatch([$1], [[!?.]$], [], [.])
+)])
+
+
+
+## ------------------------ ##
+## FIXME: Eliminate VARNAME ##
+## ------------------------ ##
+
+
+# _LT_DECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION], [IS-TAGGED?])
+# -------------------------------------------------------------------
+# CONFIGNAME is the name given to the value in the libtool script.
+# VARNAME is the (base) name used in the configure script.
+# VALUE may be 0, 1 or 2 for a computed quote escaped value based on
+# VARNAME.  Any other value will be used directly.
+m4_define([_LT_DECL],
+[lt_if_append_uniq([lt_decl_varnames], [$2], [, ],
+    [lt_dict_add_subkey([lt_decl_dict], [$2], [libtool_name],
+	[m4_ifval([$1], [$1], [$2])])
+    lt_dict_add_subkey([lt_decl_dict], [$2], [value], [$3])
+    m4_ifval([$4],
+	[lt_dict_add_subkey([lt_decl_dict], [$2], [description], [$4])])
+    lt_dict_add_subkey([lt_decl_dict], [$2],
+	[tagged?], [m4_ifval([$5], [yes], [no])])])
+])
+
+
+# _LT_TAGDECL([CONFIGNAME], VARNAME, VALUE, [DESCRIPTION])
+# --------------------------------------------------------
+m4_define([_LT_TAGDECL], [_LT_DECL([$1], [$2], [$3], [$4], [yes])])
+
+
+# lt_decl_tag_varnames([SEPARATOR], [VARNAME1...])
+# ------------------------------------------------
+m4_define([lt_decl_tag_varnames],
+[_lt_decl_filter([tagged?], [yes], $@)])
+
+
+# _lt_decl_filter(SUBKEY, VALUE, [SEPARATOR], [VARNAME1..])
+# ---------------------------------------------------------
+m4_define([_lt_decl_filter],
+[m4_case([$#],
+  [0], [m4_fatal([$0: too few arguments: $#])],
+  [1], [m4_fatal([$0: too few arguments: $#: $1])],
+  [2], [lt_dict_filter([lt_decl_dict], [$1], [$2], [], lt_decl_varnames)],
+  [3], [lt_dict_filter([lt_decl_dict], [$1], [$2], [$3], lt_decl_varnames)],
+  [lt_dict_filter([lt_decl_dict], $@)])[]dnl
+])
+
+
+# lt_decl_quote_varnames([SEPARATOR], [VARNAME1...])
+# --------------------------------------------------
+m4_define([lt_decl_quote_varnames],
+[_lt_decl_filter([value], [1], $@)])
+
+
+# lt_decl_dquote_varnames([SEPARATOR], [VARNAME1...])
+# ---------------------------------------------------
+m4_define([lt_decl_dquote_varnames],
+[_lt_decl_filter([value], [2], $@)])
+
+
+# lt_decl_varnames_tagged([SEPARATOR], [VARNAME1...])
+# ---------------------------------------------------
+m4_define([lt_decl_varnames_tagged],
+[m4_assert([$# <= 2])dnl
+_$0(m4_quote(m4_default([$1], [[, ]])),
+    m4_ifval([$2], [[$2]], [m4_dquote(lt_decl_tag_varnames)]),
+    m4_split(m4_normalize(m4_quote(_LT_TAGS)), [ ]))])
+m4_define([_lt_decl_varnames_tagged],
+[m4_ifval([$3], [lt_combine([$1], [$2], [_], $3)])])
+
+
+# lt_decl_all_varnames([SEPARATOR], [VARNAME1...])
+# ------------------------------------------------
+m4_define([lt_decl_all_varnames],
+[_$0(m4_quote(m4_default([$1], [[, ]])),
+     m4_if([$2], [],
+	   m4_quote(lt_decl_varnames),
+	m4_quote(m4_shift($@))))[]dnl
+])
+m4_define([_lt_decl_all_varnames],
+[lt_join($@, lt_decl_varnames_tagged([$1],
+			lt_decl_tag_varnames([[, ]], m4_shift($@))))dnl
+])
+
+
+# _LT_CONFIG_STATUS_DECLARE([VARNAME])
+# ------------------------------------
+# Quote a variable value, and forward it to 'config.status' so that its
+# declaration there will have the same value as in 'configure'.  VARNAME
+# must have a single quote delimited value for this to work.
+m4_define([_LT_CONFIG_STATUS_DECLARE],
+[$1='`$ECHO "$][$1" | $SED "$delay_single_quote_subst"`'])
+
+
+# _LT_CONFIG_STATUS_DECLARATIONS
+# ------------------------------
+# We delimit libtool config variables with single quotes, so when
+# we write them to config.status, we have to be sure to quote all
+# embedded single quotes properly.  In configure, this macro expands
+# each variable declared with _LT_DECL (and _LT_TAGDECL) into:
+#
+#    <var>='`$ECHO "$<var>" | $SED "$delay_single_quote_subst"`'
+m4_defun([_LT_CONFIG_STATUS_DECLARATIONS],
+[m4_foreach([_lt_var], m4_quote(lt_decl_all_varnames),
+    [m4_n([_LT_CONFIG_STATUS_DECLARE(_lt_var)])])])
+
+
+# _LT_LIBTOOL_TAGS
+# ----------------
+# Output comment and list of tags supported by the script
+m4_defun([_LT_LIBTOOL_TAGS],
+[_LT_FORMAT_COMMENT([The names of the tagged configurations supported by this script])dnl
+available_tags='_LT_TAGS'dnl
+])
+
+
+# _LT_LIBTOOL_DECLARE(VARNAME, [TAG])
+# -----------------------------------
+# Extract the dictionary values for VARNAME (optionally with TAG) and
+# expand to a commented shell variable setting:
+#
+#    # Some comment about what VAR is for.
+#    visible_name=$lt_internal_name
+m4_define([_LT_LIBTOOL_DECLARE],
+[_LT_FORMAT_COMMENT(m4_quote(lt_dict_fetch([lt_decl_dict], [$1],
+					   [description])))[]dnl
+m4_pushdef([_libtool_name],
+    m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [libtool_name])))[]dnl
+m4_case(m4_quote(lt_dict_fetch([lt_decl_dict], [$1], [value])),
+    [0], [_libtool_name=[$]$1],
+    [1], [_libtool_name=$lt_[]$1],
+    [2], [_libtool_name=$lt_[]$1],
+    [_libtool_name=lt_dict_fetch([lt_decl_dict], [$1], [value])])[]dnl
+m4_ifval([$2], [_$2])[]m4_popdef([_libtool_name])[]dnl
+])
+
+
+# _LT_LIBTOOL_CONFIG_VARS
+# -----------------------
+# Produce commented declarations of non-tagged libtool config variables
+# suitable for insertion in the LIBTOOL CONFIG section of the 'libtool'
+# script.  Tagged libtool config variables (even for the LIBTOOL CONFIG
+# section) are produced by _LT_LIBTOOL_TAG_VARS.
+m4_defun([_LT_LIBTOOL_CONFIG_VARS],
+[m4_foreach([_lt_var],
+    m4_quote(_lt_decl_filter([tagged?], [no], [], lt_decl_varnames)),
+    [m4_n([_LT_LIBTOOL_DECLARE(_lt_var)])])])
+
+
+# _LT_LIBTOOL_TAG_VARS(TAG)
+# -------------------------
+m4_define([_LT_LIBTOOL_TAG_VARS],
+[m4_foreach([_lt_var], m4_quote(lt_decl_tag_varnames),
+    [m4_n([_LT_LIBTOOL_DECLARE(_lt_var, [$1])])])])
+
+
+# _LT_TAGVAR(VARNAME, [TAGNAME])
+# ------------------------------
+m4_define([_LT_TAGVAR], [m4_ifval([$2], [$1_$2], [$1])])
+
+
+# _LT_CONFIG_COMMANDS
+# -------------------
+# Send accumulated output to $CONFIG_STATUS.  Thanks to the lists of
+# variables for single and double quote escaping we saved from calls
+# to _LT_DECL, we can put quote escaped variables declarations
+# into 'config.status', and then the shell code to quote escape them in
+# for loops in 'config.status'.  Finally, any additional code accumulated
+# from calls to _LT_CONFIG_LIBTOOL_INIT is expanded.
+m4_defun([_LT_CONFIG_COMMANDS],
+[AC_PROVIDE_IFELSE([LT_OUTPUT],
+	dnl If the libtool generation code has been placed in $CONFIG_LT,
+	dnl instead of duplicating it all over again into config.status,
+	dnl then we will have config.status run $CONFIG_LT later, so it
+	dnl needs to know what name is stored there:
+        [AC_CONFIG_COMMANDS([libtool],
+            [$SHELL $CONFIG_LT || AS_EXIT(1)], [CONFIG_LT='$CONFIG_LT'])],
+    dnl If the libtool generation code is destined for config.status,
+    dnl expand the accumulated commands and init code now:
+    [AC_CONFIG_COMMANDS([libtool],
+        [_LT_OUTPUT_LIBTOOL_COMMANDS], [_LT_OUTPUT_LIBTOOL_COMMANDS_INIT])])
+])#_LT_CONFIG_COMMANDS
+
+
+# Initialize.
+m4_define([_LT_OUTPUT_LIBTOOL_COMMANDS_INIT],
+[
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
+
+sed_quote_subst='$sed_quote_subst'
+double_quote_subst='$double_quote_subst'
+delay_variable_subst='$delay_variable_subst'
+_LT_CONFIG_STATUS_DECLARATIONS
+LTCC='$LTCC'
+LTCFLAGS='$LTCFLAGS'
+compiler='$compiler_DEFAULT'
+
+# A function that is used when there is no print builtin or printf.
+func_fallback_echo ()
+{
+  eval 'cat <<_LTECHO_EOF
+\$[]1
+_LTECHO_EOF'
+}
+
+# Quote evaled strings.
+for var in lt_decl_all_varnames([[ \
+]], lt_decl_quote_varnames); do
+    case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in
+    *[[\\\\\\\`\\"\\\$]]*)
+      eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED \\"\\\$sed_quote_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes
+      ;;
+    *)
+      eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\""
+      ;;
+    esac
+done
+
+# Double-quote double-evaled strings.
+for var in lt_decl_all_varnames([[ \
+]], lt_decl_dquote_varnames); do
+    case \`eval \\\\\$ECHO \\\\""\\\\\$\$var"\\\\"\` in
+    *[[\\\\\\\`\\"\\\$]]*)
+      eval "lt_\$var=\\\\\\"\\\`\\\$ECHO \\"\\\$\$var\\" | \\\$SED -e \\"\\\$double_quote_subst\\" -e \\"\\\$sed_quote_subst\\" -e \\"\\\$delay_variable_subst\\"\\\`\\\\\\"" ## exclude from sc_prohibit_nested_quotes
+      ;;
+    *)
+      eval "lt_\$var=\\\\\\"\\\$\$var\\\\\\""
+      ;;
+    esac
+done
+
+_LT_OUTPUT_LIBTOOL_INIT
+])
+
+# _LT_GENERATED_FILE_INIT(FILE, [COMMENT])
+# ------------------------------------
+# Generate a child script FILE with all initialization necessary to
+# reuse the environment learned by the parent script, and make the
+# file executable.  If COMMENT is supplied, it is inserted after the
+# '#!' sequence but before initialization text begins.  After this
+# macro, additional text can be appended to FILE to form the body of
+# the child script.  The macro ends with non-zero status if the
+# file could not be fully written (such as if the disk is full).
+m4_ifdef([AS_INIT_GENERATED],
+[m4_defun([_LT_GENERATED_FILE_INIT],[AS_INIT_GENERATED($@)])],
+[m4_defun([_LT_GENERATED_FILE_INIT],
+[m4_require([AS_PREPARE])]dnl
+[m4_pushdef([AS_MESSAGE_LOG_FD])]dnl
+[lt_write_fail=0
+cat >$1 <<_ASEOF || lt_write_fail=1
+#! $SHELL
+# Generated by $as_me.
+$2
+SHELL=\${CONFIG_SHELL-$SHELL}
+export SHELL
+_ASEOF
+cat >>$1 <<\_ASEOF || lt_write_fail=1
+AS_SHELL_SANITIZE
+_AS_PREPARE
+exec AS_MESSAGE_FD>&1
+_ASEOF
+test 0 = "$lt_write_fail" && chmod +x $1[]dnl
+m4_popdef([AS_MESSAGE_LOG_FD])])])# _LT_GENERATED_FILE_INIT
+
+# LT_OUTPUT
+# ---------
+# This macro allows early generation of the libtool script (before
+# AC_OUTPUT is called), incase it is used in configure for compilation
+# tests.
+AC_DEFUN([LT_OUTPUT],
+[: ${CONFIG_LT=./config.lt}
+AC_MSG_NOTICE([creating $CONFIG_LT])
+_LT_GENERATED_FILE_INIT(["$CONFIG_LT"],
+[# Run this file to recreate a libtool stub with the current configuration.])
+
+cat >>"$CONFIG_LT" <<\_LTEOF
+lt_cl_silent=false
+exec AS_MESSAGE_LOG_FD>>config.log
+{
+  echo
+  AS_BOX([Running $as_me.])
+} >&AS_MESSAGE_LOG_FD
+
+lt_cl_help="\
+'$as_me' creates a local libtool stub from the current configuration,
+for use in further configure time tests before the real libtool is
+generated.
+
+Usage: $[0] [[OPTIONS]]
+
+  -h, --help      print this help, then exit
+  -V, --version   print version number, then exit
+  -q, --quiet     do not print progress messages
+  -d, --debug     don't remove temporary files
+
+Report bugs to <bug-libtool@gnu.org>."
+
+lt_cl_version="\
+m4_ifset([AC_PACKAGE_NAME], [AC_PACKAGE_NAME ])config.lt[]dnl
+m4_ifset([AC_PACKAGE_VERSION], [ AC_PACKAGE_VERSION])
+configured by $[0], generated by m4_PACKAGE_STRING.
+
+Copyright (C) 2011 Free Software Foundation, Inc.
+This config.lt script is free software; the Free Software Foundation
+gives unlimited permision to copy, distribute and modify it."
+
+while test 0 != $[#]
+do
+  case $[1] in
+    --version | --v* | -V )
+      echo "$lt_cl_version"; exit 0 ;;
+    --help | --h* | -h )
+      echo "$lt_cl_help"; exit 0 ;;
+    --debug | --d* | -d )
+      debug=: ;;
+    --quiet | --q* | --silent | --s* | -q )
+      lt_cl_silent=: ;;
+
+    -*) AC_MSG_ERROR([unrecognized option: $[1]
+Try '$[0] --help' for more information.]) ;;
+
+    *) AC_MSG_ERROR([unrecognized argument: $[1]
+Try '$[0] --help' for more information.]) ;;
+  esac
+  shift
+done
+
+if $lt_cl_silent; then
+  exec AS_MESSAGE_FD>/dev/null
+fi
+_LTEOF
+
+cat >>"$CONFIG_LT" <<_LTEOF
+_LT_OUTPUT_LIBTOOL_COMMANDS_INIT
+_LTEOF
+
+cat >>"$CONFIG_LT" <<\_LTEOF
+AC_MSG_NOTICE([creating $ofile])
+_LT_OUTPUT_LIBTOOL_COMMANDS
+AS_EXIT(0)
+_LTEOF
+chmod +x "$CONFIG_LT"
+
+# configure is writing to config.log, but config.lt does its own redirection,
+# appending to config.log, which fails on DOS, as config.log is still kept
+# open by configure.  Here we exec the FD to /dev/null, effectively closing
+# config.log, so it can be properly (re)opened and appended to by config.lt.
+lt_cl_success=:
+test yes = "$silent" &&
+  lt_config_lt_args="$lt_config_lt_args --quiet"
+exec AS_MESSAGE_LOG_FD>/dev/null
+$SHELL "$CONFIG_LT" $lt_config_lt_args || lt_cl_success=false
+exec AS_MESSAGE_LOG_FD>>config.log
+$lt_cl_success || AS_EXIT(1)
+])# LT_OUTPUT
+
+
+# _LT_CONFIG(TAG)
+# ---------------
+# If TAG is the built-in tag, create an initial libtool script with a
+# default configuration from the untagged config vars.  Otherwise add code
+# to config.status for appending the configuration named by TAG from the
+# matching tagged config vars.
+m4_defun([_LT_CONFIG],
+[m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+_LT_CONFIG_SAVE_COMMANDS([
+  m4_define([_LT_TAG], m4_if([$1], [], [C], [$1]))dnl
+  m4_if(_LT_TAG, [C], [
+    # See if we are running on zsh, and set the options that allow our
+    # commands through without removal of \ escapes.
+    if test -n "${ZSH_VERSION+set}"; then
+      setopt NO_GLOB_SUBST
+    fi
+
+    cfgfile=${ofile}T
+    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
+    $RM "$cfgfile"
+
+    cat <<_LT_EOF >> "$cfgfile"
+#! $SHELL
+# Generated automatically by $as_me ($PACKAGE) $VERSION
+# NOTE: Changes made to this file will be lost: look at ltmain.sh.
+
+# Provide generalized library-building support services.
+# Written by Gordon Matzigkeit, 1996
+
+_LT_COPYING
+_LT_LIBTOOL_TAGS
+
+# Configured defaults for sys_lib_dlsearch_path munging.
+: \${LT_SYS_LIBRARY_PATH="$configure_time_lt_sys_library_path"}
+
+# ### BEGIN LIBTOOL CONFIG
+_LT_LIBTOOL_CONFIG_VARS
+_LT_LIBTOOL_TAG_VARS
+# ### END LIBTOOL CONFIG
+
+_LT_EOF
+
+    cat <<'_LT_EOF' >> "$cfgfile"
+
+# ### BEGIN FUNCTIONS SHARED WITH CONFIGURE
+
+_LT_PREPARE_MUNGE_PATH_LIST
+_LT_PREPARE_CC_BASENAME
+
+# ### END FUNCTIONS SHARED WITH CONFIGURE
+
+_LT_EOF
+
+  case $host_os in
+  aix3*)
+    cat <<\_LT_EOF >> "$cfgfile"
+# AIX sometimes has problems with the GCC collect2 program.  For some
+# reason, if we set the COLLECT_NAMES environment variable, the problems
+# vanish in a puff of smoke.
+if test set != "${COLLECT_NAMES+set}"; then
+  COLLECT_NAMES=
+  export COLLECT_NAMES
+fi
+_LT_EOF
+    ;;
+  esac
+
+  _LT_PROG_LTMAIN
+
+  # We use sed instead of cat because bash on DJGPP gets confused if
+  # if finds mixed CR/LF and LF-only lines.  Since sed operates in
+  # text mode, it properly converts lines to CR/LF.  This bash problem
+  # is reportedly fixed, but why not run on old versions too?
+  sed '$q' "$ltmain" >> "$cfgfile" \
+     || (rm -f "$cfgfile"; exit 1)
+
+   mv -f "$cfgfile" "$ofile" ||
+    (rm -f "$ofile" && cp "$cfgfile" "$ofile" && rm -f "$cfgfile")
+  chmod +x "$ofile"
+],
+[cat <<_LT_EOF >> "$ofile"
+
+dnl Unfortunately we have to use $1 here, since _LT_TAG is not expanded
+dnl in a comment (ie after a #).
+# ### BEGIN LIBTOOL TAG CONFIG: $1
+_LT_LIBTOOL_TAG_VARS(_LT_TAG)
+# ### END LIBTOOL TAG CONFIG: $1
+_LT_EOF
+])dnl /m4_if
+],
+[m4_if([$1], [], [
+    PACKAGE='$PACKAGE'
+    VERSION='$VERSION'
+    RM='$RM'
+    ofile='$ofile'], [])
+])dnl /_LT_CONFIG_SAVE_COMMANDS
+])# _LT_CONFIG
+
+
+# LT_SUPPORTED_TAG(TAG)
+# ---------------------
+# Trace this macro to discover what tags are supported by the libtool
+# --tag option, using:
+#    autoconf --trace 'LT_SUPPORTED_TAG:$1'
+AC_DEFUN([LT_SUPPORTED_TAG], [])
+
+
+# C support is built-in for now
+m4_define([_LT_LANG_C_enabled], [])
+m4_define([_LT_TAGS], [])
+
+
+# LT_LANG(LANG)
+# -------------
+# Enable libtool support for the given language if not already enabled.
+AC_DEFUN([LT_LANG],
+[AC_BEFORE([$0], [LT_OUTPUT])dnl
+m4_case([$1],
+  [C],			[_LT_LANG(C)],
+  [C++],		[_LT_LANG(CXX)],
+  [Go],			[_LT_LANG(GO)],
+  [Java],		[_LT_LANG(GCJ)],
+  [Fortran 77],		[_LT_LANG(F77)],
+  [Fortran],		[_LT_LANG(FC)],
+  [Windows Resource],	[_LT_LANG(RC)],
+  [m4_ifdef([_LT_LANG_]$1[_CONFIG],
+    [_LT_LANG($1)],
+    [m4_fatal([$0: unsupported language: "$1"])])])dnl
+])# LT_LANG
+
+
+# _LT_LANG(LANGNAME)
+# ------------------
+m4_defun([_LT_LANG],
+[m4_ifdef([_LT_LANG_]$1[_enabled], [],
+  [LT_SUPPORTED_TAG([$1])dnl
+  m4_append([_LT_TAGS], [$1 ])dnl
+  m4_define([_LT_LANG_]$1[_enabled], [])dnl
+  _LT_LANG_$1_CONFIG($1)])dnl
+])# _LT_LANG
+
+
+m4_ifndef([AC_PROG_GO], [
+############################################################
+# NOTE: This macro has been submitted for inclusion into   #
+#  GNU Autoconf as AC_PROG_GO.  When it is available in    #
+#  a released version of Autoconf we should remove this    #
+#  macro and use it instead.                               #
+############################################################
+m4_defun([AC_PROG_GO],
+[AC_LANG_PUSH(Go)dnl
+AC_ARG_VAR([GOC],     [Go compiler command])dnl
+AC_ARG_VAR([GOFLAGS], [Go compiler flags])dnl
+_AC_ARG_VAR_LDFLAGS()dnl
+AC_CHECK_TOOL(GOC, gccgo)
+if test -z "$GOC"; then
+  if test -n "$ac_tool_prefix"; then
+    AC_CHECK_PROG(GOC, [${ac_tool_prefix}gccgo], [${ac_tool_prefix}gccgo])
+  fi
+fi
+if test -z "$GOC"; then
+  AC_CHECK_PROG(GOC, gccgo, gccgo, false)
+fi
+])#m4_defun
+])#m4_ifndef
+
+
+# _LT_LANG_DEFAULT_CONFIG
+# -----------------------
+m4_defun([_LT_LANG_DEFAULT_CONFIG],
+[AC_PROVIDE_IFELSE([AC_PROG_CXX],
+  [LT_LANG(CXX)],
+  [m4_define([AC_PROG_CXX], defn([AC_PROG_CXX])[LT_LANG(CXX)])])
+
+AC_PROVIDE_IFELSE([AC_PROG_F77],
+  [LT_LANG(F77)],
+  [m4_define([AC_PROG_F77], defn([AC_PROG_F77])[LT_LANG(F77)])])
+
+AC_PROVIDE_IFELSE([AC_PROG_FC],
+  [LT_LANG(FC)],
+  [m4_define([AC_PROG_FC], defn([AC_PROG_FC])[LT_LANG(FC)])])
+
+dnl The call to [A][M_PROG_GCJ] is quoted like that to stop aclocal
+dnl pulling things in needlessly.
+AC_PROVIDE_IFELSE([AC_PROG_GCJ],
+  [LT_LANG(GCJ)],
+  [AC_PROVIDE_IFELSE([A][M_PROG_GCJ],
+    [LT_LANG(GCJ)],
+    [AC_PROVIDE_IFELSE([LT_PROG_GCJ],
+      [LT_LANG(GCJ)],
+      [m4_ifdef([AC_PROG_GCJ],
+	[m4_define([AC_PROG_GCJ], defn([AC_PROG_GCJ])[LT_LANG(GCJ)])])
+       m4_ifdef([A][M_PROG_GCJ],
+	[m4_define([A][M_PROG_GCJ], defn([A][M_PROG_GCJ])[LT_LANG(GCJ)])])
+       m4_ifdef([LT_PROG_GCJ],
+	[m4_define([LT_PROG_GCJ], defn([LT_PROG_GCJ])[LT_LANG(GCJ)])])])])])
+
+AC_PROVIDE_IFELSE([AC_PROG_GO],
+  [LT_LANG(GO)],
+  [m4_define([AC_PROG_GO], defn([AC_PROG_GO])[LT_LANG(GO)])])
+
+AC_PROVIDE_IFELSE([LT_PROG_RC],
+  [LT_LANG(RC)],
+  [m4_define([LT_PROG_RC], defn([LT_PROG_RC])[LT_LANG(RC)])])
+])# _LT_LANG_DEFAULT_CONFIG
+
+# Obsolete macros:
+AU_DEFUN([AC_LIBTOOL_CXX], [LT_LANG(C++)])
+AU_DEFUN([AC_LIBTOOL_F77], [LT_LANG(Fortran 77)])
+AU_DEFUN([AC_LIBTOOL_FC], [LT_LANG(Fortran)])
+AU_DEFUN([AC_LIBTOOL_GCJ], [LT_LANG(Java)])
+AU_DEFUN([AC_LIBTOOL_RC], [LT_LANG(Windows Resource)])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_CXX], [])
+dnl AC_DEFUN([AC_LIBTOOL_F77], [])
+dnl AC_DEFUN([AC_LIBTOOL_FC], [])
+dnl AC_DEFUN([AC_LIBTOOL_GCJ], [])
+dnl AC_DEFUN([AC_LIBTOOL_RC], [])
+
+
+# _LT_TAG_COMPILER
+# ----------------
+m4_defun([_LT_TAG_COMPILER],
+[AC_REQUIRE([AC_PROG_CC])dnl
+
+_LT_DECL([LTCC], [CC], [1], [A C compiler])dnl
+_LT_DECL([LTCFLAGS], [CFLAGS], [1], [LTCC compiler flags])dnl
+_LT_TAGDECL([CC], [compiler], [1], [A language specific compiler])dnl
+_LT_TAGDECL([with_gcc], [GCC], [0], [Is the compiler the GNU compiler?])dnl
+
+# If no C compiler was specified, use CC.
+LTCC=${LTCC-"$CC"}
+
+# If no C compiler flags were specified, use CFLAGS.
+LTCFLAGS=${LTCFLAGS-"$CFLAGS"}
+
+# Allow CC to be a program name with arguments.
+compiler=$CC
+])# _LT_TAG_COMPILER
+
+
+# _LT_COMPILER_BOILERPLATE
+# ------------------------
+# Check for compiler boilerplate output or warnings with
+# the simple compiler test code.
+m4_defun([_LT_COMPILER_BOILERPLATE],
+[m4_require([_LT_DECL_SED])dnl
+ac_outfile=conftest.$ac_objext
+echo "$lt_simple_compile_test_code" >conftest.$ac_ext
+eval "$ac_compile" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err
+_lt_compiler_boilerplate=`cat conftest.err`
+$RM conftest*
+])# _LT_COMPILER_BOILERPLATE
+
+
+# _LT_LINKER_BOILERPLATE
+# ----------------------
+# Check for linker boilerplate output or warnings with
+# the simple link test code.
+m4_defun([_LT_LINKER_BOILERPLATE],
+[m4_require([_LT_DECL_SED])dnl
+ac_outfile=conftest.$ac_objext
+echo "$lt_simple_link_test_code" >conftest.$ac_ext
+eval "$ac_link" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err
+_lt_linker_boilerplate=`cat conftest.err`
+$RM -r conftest*
+])# _LT_LINKER_BOILERPLATE
+
+# _LT_REQUIRED_DARWIN_CHECKS
+# -------------------------
+m4_defun_once([_LT_REQUIRED_DARWIN_CHECKS],[
+  case $host_os in
+    rhapsody* | darwin*)
+    AC_CHECK_TOOL([DSYMUTIL], [dsymutil], [:])
+    AC_CHECK_TOOL([NMEDIT], [nmedit], [:])
+    AC_CHECK_TOOL([LIPO], [lipo], [:])
+    AC_CHECK_TOOL([OTOOL], [otool], [:])
+    AC_CHECK_TOOL([OTOOL64], [otool64], [:])
+    _LT_DECL([], [DSYMUTIL], [1],
+      [Tool to manipulate archived DWARF debug symbol files on Mac OS X])
+    _LT_DECL([], [NMEDIT], [1],
+      [Tool to change global to local symbols on Mac OS X])
+    _LT_DECL([], [LIPO], [1],
+      [Tool to manipulate fat objects and archives on Mac OS X])
+    _LT_DECL([], [OTOOL], [1],
+      [ldd/readelf like tool for Mach-O binaries on Mac OS X])
+    _LT_DECL([], [OTOOL64], [1],
+      [ldd/readelf like tool for 64 bit Mach-O binaries on Mac OS X 10.4])
+
+    AC_CACHE_CHECK([for -single_module linker flag],[lt_cv_apple_cc_single_mod],
+      [lt_cv_apple_cc_single_mod=no
+      if test -z "$LT_MULTI_MODULE"; then
+	# By default we will add the -single_module flag. You can override
+	# by either setting the environment variable LT_MULTI_MODULE
+	# non-empty at configure time, or by adding -multi_module to the
+	# link flags.
+	rm -rf libconftest.dylib*
+	echo "int foo(void){return 1;}" > conftest.c
+	echo "$LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \
+-dynamiclib -Wl,-single_module conftest.c" >&AS_MESSAGE_LOG_FD
+	$LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \
+	  -dynamiclib -Wl,-single_module conftest.c 2>conftest.err
+        _lt_result=$?
+	# If there is a non-empty error log, and "single_module"
+	# appears in it, assume the flag caused a linker warning
+        if test -s conftest.err && $GREP single_module conftest.err; then
+	  cat conftest.err >&AS_MESSAGE_LOG_FD
+	# Otherwise, if the output was created with a 0 exit code from
+	# the compiler, it worked.
+	elif test -f libconftest.dylib && test 0 = "$_lt_result"; then
+	  lt_cv_apple_cc_single_mod=yes
+	else
+	  cat conftest.err >&AS_MESSAGE_LOG_FD
+	fi
+	rm -rf libconftest.dylib*
+	rm -f conftest.*
+      fi])
+
+    AC_CACHE_CHECK([for -exported_symbols_list linker flag],
+      [lt_cv_ld_exported_symbols_list],
+      [lt_cv_ld_exported_symbols_list=no
+      save_LDFLAGS=$LDFLAGS
+      echo "_main" > conftest.sym
+      LDFLAGS="$LDFLAGS -Wl,-exported_symbols_list,conftest.sym"
+      AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
+	[lt_cv_ld_exported_symbols_list=yes],
+	[lt_cv_ld_exported_symbols_list=no])
+	LDFLAGS=$save_LDFLAGS
+    ])
+
+    AC_CACHE_CHECK([for -force_load linker flag],[lt_cv_ld_force_load],
+      [lt_cv_ld_force_load=no
+      cat > conftest.c << _LT_EOF
+int forced_loaded() { return 2;}
+_LT_EOF
+      echo "$LTCC $LTCFLAGS -c -o conftest.o conftest.c" >&AS_MESSAGE_LOG_FD
+      $LTCC $LTCFLAGS -c -o conftest.o conftest.c 2>&AS_MESSAGE_LOG_FD
+      echo "$AR cru libconftest.a conftest.o" >&AS_MESSAGE_LOG_FD
+      $AR cru libconftest.a conftest.o 2>&AS_MESSAGE_LOG_FD
+      echo "$RANLIB libconftest.a" >&AS_MESSAGE_LOG_FD
+      $RANLIB libconftest.a 2>&AS_MESSAGE_LOG_FD
+      cat > conftest.c << _LT_EOF
+int main() { return 0;}
+_LT_EOF
+      echo "$LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a" >&AS_MESSAGE_LOG_FD
+      $LTCC $LTCFLAGS $LDFLAGS -o conftest conftest.c -Wl,-force_load,./libconftest.a 2>conftest.err
+      _lt_result=$?
+      if test -s conftest.err && $GREP force_load conftest.err; then
+	cat conftest.err >&AS_MESSAGE_LOG_FD
+      elif test -f conftest && test 0 = "$_lt_result" && $GREP forced_load conftest >/dev/null 2>&1; then
+	lt_cv_ld_force_load=yes
+      else
+	cat conftest.err >&AS_MESSAGE_LOG_FD
+      fi
+        rm -f conftest.err libconftest.a conftest conftest.c
+        rm -rf conftest.dSYM
+    ])
+    case $host_os in
+    rhapsody* | darwin1.[[012]])
+      _lt_dar_allow_undefined='$wl-undefined ${wl}suppress' ;;
+    darwin1.*)
+      _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;;
+    darwin*) # darwin 5.x on
+      # if running on 10.5 or later, the deployment target defaults
+      # to the OS version, if on x86, and 10.4, the deployment
+      # target defaults to 10.4. Don't you love it?
+      case ${MACOSX_DEPLOYMENT_TARGET-10.0},$host in
+	10.0,*86*-darwin8*|10.0,*-darwin[[91]]*)
+	  _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;;
+	10.[[012]][[,.]]*)
+	  _lt_dar_allow_undefined='$wl-flat_namespace $wl-undefined ${wl}suppress' ;;
+	10.*)
+	  _lt_dar_allow_undefined='$wl-undefined ${wl}dynamic_lookup' ;;
+      esac
+    ;;
+  esac
+    if test yes = "$lt_cv_apple_cc_single_mod"; then
+      _lt_dar_single_mod='$single_module'
+    fi
+    if test yes = "$lt_cv_ld_exported_symbols_list"; then
+      _lt_dar_export_syms=' $wl-exported_symbols_list,$output_objdir/$libname-symbols.expsym'
+    else
+      _lt_dar_export_syms='~$NMEDIT -s $output_objdir/$libname-symbols.expsym $lib'
+    fi
+    if test : != "$DSYMUTIL" && test no = "$lt_cv_ld_force_load"; then
+      _lt_dsymutil='~$DSYMUTIL $lib || :'
+    else
+      _lt_dsymutil=
+    fi
+    ;;
+  esac
+])
+
+
+# _LT_DARWIN_LINKER_FEATURES([TAG])
+# ---------------------------------
+# Checks for linker and compiler features on darwin
+m4_defun([_LT_DARWIN_LINKER_FEATURES],
+[
+  m4_require([_LT_REQUIRED_DARWIN_CHECKS])
+  _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+  _LT_TAGVAR(hardcode_direct, $1)=no
+  _LT_TAGVAR(hardcode_automatic, $1)=yes
+  _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+  if test yes = "$lt_cv_ld_force_load"; then
+    _LT_TAGVAR(whole_archive_flag_spec, $1)='`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience $wl-force_load,$conv\"; done; func_echo_all \"$new_convenience\"`'
+    m4_case([$1], [F77], [_LT_TAGVAR(compiler_needs_object, $1)=yes],
+                  [FC],  [_LT_TAGVAR(compiler_needs_object, $1)=yes])
+  else
+    _LT_TAGVAR(whole_archive_flag_spec, $1)=''
+  fi
+  _LT_TAGVAR(link_all_deplibs, $1)=yes
+  _LT_TAGVAR(allow_undefined_flag, $1)=$_lt_dar_allow_undefined
+  case $cc_basename in
+     ifort*|nagfor*) _lt_dar_can_shared=yes ;;
+     *) _lt_dar_can_shared=$GCC ;;
+  esac
+  if test yes = "$_lt_dar_can_shared"; then
+    output_verbose_link_cmd=func_echo_all
+    _LT_TAGVAR(archive_cmds, $1)="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dsymutil"
+    _LT_TAGVAR(module_cmds, $1)="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dsymutil"
+    _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod$_lt_dar_export_syms$_lt_dsymutil"
+    _LT_TAGVAR(module_expsym_cmds, $1)="sed -e 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags$_lt_dar_export_syms$_lt_dsymutil"
+    m4_if([$1], [CXX],
+[   if test yes != "$lt_cv_apple_cc_single_mod"; then
+      _LT_TAGVAR(archive_cmds, $1)="\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dsymutil"
+      _LT_TAGVAR(archive_expsym_cmds, $1)="sed 's|^|_|' < \$export_symbols > \$output_objdir/\$libname-symbols.expsym~\$CC -r -keep_private_externs -nostdlib -o \$lib-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$lib-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring$_lt_dar_export_syms$_lt_dsymutil"
+    fi
+],[])
+  else
+  _LT_TAGVAR(ld_shlibs, $1)=no
+  fi
+])
+
+# _LT_SYS_MODULE_PATH_AIX([TAGNAME])
+# ----------------------------------
+# Links a minimal program and checks the executable
+# for the system default hardcoded library path. In most cases,
+# this is /usr/lib:/lib, but when the MPI compilers are used
+# the location of the communication and MPI libs are included too.
+# If we don't find anything, use the default library path according
+# to the aix ld manual.
+# Store the results from the different compilers for each TAGNAME.
+# Allow to override them for all tags through lt_cv_aix_libpath.
+m4_defun([_LT_SYS_MODULE_PATH_AIX],
+[m4_require([_LT_DECL_SED])dnl
+if test set = "${lt_cv_aix_libpath+set}"; then
+  aix_libpath=$lt_cv_aix_libpath
+else
+  AC_CACHE_VAL([_LT_TAGVAR([lt_cv_aix_libpath_], [$1])],
+  [AC_LINK_IFELSE([AC_LANG_PROGRAM],[
+  lt_aix_libpath_sed='[
+      /Import File Strings/,/^$/ {
+	  /^0/ {
+	      s/^0  *\([^ ]*\) *$/\1/
+	      p
+	  }
+      }]'
+  _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"`
+  # Check for a 64-bit object if we didn't find anything.
+  if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then
+    _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"`
+  fi],[])
+  if test -z "$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])"; then
+    _LT_TAGVAR([lt_cv_aix_libpath_], [$1])=/usr/lib:/lib
+  fi
+  ])
+  aix_libpath=$_LT_TAGVAR([lt_cv_aix_libpath_], [$1])
+fi
+])# _LT_SYS_MODULE_PATH_AIX
+
+
+# _LT_SHELL_INIT(ARG)
+# -------------------
+m4_define([_LT_SHELL_INIT],
+[m4_divert_text([M4SH-INIT], [$1
+])])# _LT_SHELL_INIT
+
+
+
+# _LT_PROG_ECHO_BACKSLASH
+# -----------------------
+# Find how we can fake an echo command that does not interpret backslash.
+# In particular, with Autoconf 2.60 or later we add some code to the start
+# of the generated configure script that will find a shell with a builtin
+# printf (that we can use as an echo command).
+m4_defun([_LT_PROG_ECHO_BACKSLASH],
+[ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
+ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO
+ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO
+
+AC_MSG_CHECKING([how to print strings])
+# Test print first, because it will be a builtin if present.
+if test "X`( print -r -- -n ) 2>/dev/null`" = X-n && \
+   test "X`print -r -- $ECHO 2>/dev/null`" = "X$ECHO"; then
+  ECHO='print -r --'
+elif test "X`printf %s $ECHO 2>/dev/null`" = "X$ECHO"; then
+  ECHO='printf %s\n'
+else
+  # Use this function as a fallback that always works.
+  func_fallback_echo ()
+  {
+    eval 'cat <<_LTECHO_EOF
+$[]1
+_LTECHO_EOF'
+  }
+  ECHO='func_fallback_echo'
+fi
+
+# func_echo_all arg...
+# Invoke $ECHO with all args, space-separated.
+func_echo_all ()
+{
+    $ECHO "$*"
+}
+
+case $ECHO in
+  printf*) AC_MSG_RESULT([printf]) ;;
+  print*) AC_MSG_RESULT([print -r]) ;;
+  *) AC_MSG_RESULT([cat]) ;;
+esac
+
+m4_ifdef([_AS_DETECT_SUGGESTED],
+[_AS_DETECT_SUGGESTED([
+  test -n "${ZSH_VERSION+set}${BASH_VERSION+set}" || (
+    ECHO='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
+    ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO
+    ECHO=$ECHO$ECHO$ECHO$ECHO$ECHO$ECHO
+    PATH=/empty FPATH=/empty; export PATH FPATH
+    test "X`printf %s $ECHO`" = "X$ECHO" \
+      || test "X`print -r -- $ECHO`" = "X$ECHO" )])])
+
+_LT_DECL([], [SHELL], [1], [Shell to use when invoking shell scripts])
+_LT_DECL([], [ECHO], [1], [An echo program that protects backslashes])
+])# _LT_PROG_ECHO_BACKSLASH
+
+
+# _LT_WITH_SYSROOT
+# ----------------
+AC_DEFUN([_LT_WITH_SYSROOT],
+[AC_MSG_CHECKING([for sysroot])
+AC_ARG_WITH([sysroot],
+[AS_HELP_STRING([--with-sysroot@<:@=DIR@:>@],
+  [Search for dependent libraries within DIR (or the compiler's sysroot
+   if not specified).])],
+[], [with_sysroot=no])
+
+dnl lt_sysroot will always be passed unquoted.  We quote it here
+dnl in case the user passed a directory name.
+lt_sysroot=
+case $with_sysroot in #(
+ yes)
+   if test yes = "$GCC"; then
+     lt_sysroot=`$CC --print-sysroot 2>/dev/null`
+   fi
+   ;; #(
+ /*)
+   lt_sysroot=`echo "$with_sysroot" | sed -e "$sed_quote_subst"`
+   ;; #(
+ no|'')
+   ;; #(
+ *)
+   AC_MSG_RESULT([$with_sysroot])
+   AC_MSG_ERROR([The sysroot must be an absolute path.])
+   ;;
+esac
+
+ AC_MSG_RESULT([${lt_sysroot:-no}])
+_LT_DECL([], [lt_sysroot], [0], [The root where to search for ]dnl
+[dependent libraries, and where our libraries should be installed.])])
+
+# _LT_ENABLE_LOCK
+# ---------------
+m4_defun([_LT_ENABLE_LOCK],
+[AC_ARG_ENABLE([libtool-lock],
+  [AS_HELP_STRING([--disable-libtool-lock],
+    [avoid locking (might break parallel builds)])])
+test no = "$enable_libtool_lock" || enable_libtool_lock=yes
+
+# Some flags need to be propagated to the compiler or linker for good
+# libtool support.
+case $host in
+ia64-*-hpux*)
+  # Find out what ABI is being produced by ac_compile, and set mode
+  # options accordingly.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.$ac_objext` in
+      *ELF-32*)
+	HPUX_IA64_MODE=32
+	;;
+      *ELF-64*)
+	HPUX_IA64_MODE=64
+	;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+*-*-irix6*)
+  # Find out what ABI is being produced by ac_compile, and set linker
+  # options accordingly.
+  echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    if test yes = "$lt_cv_prog_gnu_ld"; then
+      case `/usr/bin/file conftest.$ac_objext` in
+	*32-bit*)
+	  LD="${LD-ld} -melf32bsmip"
+	  ;;
+	*N32*)
+	  LD="${LD-ld} -melf32bmipn32"
+	  ;;
+	*64-bit*)
+	  LD="${LD-ld} -melf64bmip"
+	;;
+      esac
+    else
+      case `/usr/bin/file conftest.$ac_objext` in
+	*32-bit*)
+	  LD="${LD-ld} -32"
+	  ;;
+	*N32*)
+	  LD="${LD-ld} -n32"
+	  ;;
+	*64-bit*)
+	  LD="${LD-ld} -64"
+	  ;;
+      esac
+    fi
+  fi
+  rm -rf conftest*
+  ;;
+
+mips64*-*linux*)
+  # Find out what ABI is being produced by ac_compile, and set linker
+  # options accordingly.
+  echo '[#]line '$LINENO' "configure"' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    emul=elf
+    case `/usr/bin/file conftest.$ac_objext` in
+      *32-bit*)
+	emul="${emul}32"
+	;;
+      *64-bit*)
+	emul="${emul}64"
+	;;
+    esac
+    case `/usr/bin/file conftest.$ac_objext` in
+      *MSB*)
+	emul="${emul}btsmip"
+	;;
+      *LSB*)
+	emul="${emul}ltsmip"
+	;;
+    esac
+    case `/usr/bin/file conftest.$ac_objext` in
+      *N32*)
+	emul="${emul}n32"
+	;;
+    esac
+    LD="${LD-ld} -m $emul"
+  fi
+  rm -rf conftest*
+  ;;
+
+x86_64-*kfreebsd*-gnu|x86_64-*linux*|powerpc*-*linux*| \
+s390*-*linux*|s390*-*tpf*|sparc*-*linux*)
+  # Find out what ABI is being produced by ac_compile, and set linker
+  # options accordingly.  Note that the listed cases only cover the
+  # situations where additional linker options are needed (such as when
+  # doing 32-bit compilation for a host where ld defaults to 64-bit, or
+  # vice versa); the common cases where no linker options are needed do
+  # not appear in the list.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.o` in
+      *32-bit*)
+	case $host in
+	  x86_64-*kfreebsd*-gnu)
+	    LD="${LD-ld} -m elf_i386_fbsd"
+	    ;;
+	  x86_64-*linux*)
+	    case `/usr/bin/file conftest.o` in
+	      *x86-64*)
+		LD="${LD-ld} -m elf32_x86_64"
+		;;
+	      *)
+		LD="${LD-ld} -m elf_i386"
+		;;
+	    esac
+	    ;;
+	  powerpc64le-*linux*)
+	    LD="${LD-ld} -m elf32lppclinux"
+	    ;;
+	  powerpc64-*linux*)
+	    LD="${LD-ld} -m elf32ppclinux"
+	    ;;
+	  s390x-*linux*)
+	    LD="${LD-ld} -m elf_s390"
+	    ;;
+	  sparc64-*linux*)
+	    LD="${LD-ld} -m elf32_sparc"
+	    ;;
+	esac
+	;;
+      *64-bit*)
+	case $host in
+	  x86_64-*kfreebsd*-gnu)
+	    LD="${LD-ld} -m elf_x86_64_fbsd"
+	    ;;
+	  x86_64-*linux*)
+	    LD="${LD-ld} -m elf_x86_64"
+	    ;;
+	  powerpcle-*linux*)
+	    LD="${LD-ld} -m elf64lppc"
+	    ;;
+	  powerpc-*linux*)
+	    LD="${LD-ld} -m elf64ppc"
+	    ;;
+	  s390*-*linux*|s390*-*tpf*)
+	    LD="${LD-ld} -m elf64_s390"
+	    ;;
+	  sparc*-*linux*)
+	    LD="${LD-ld} -m elf64_sparc"
+	    ;;
+	esac
+	;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+
+*-*-sco3.2v5*)
+  # On SCO OpenServer 5, we need -belf to get full-featured binaries.
+  SAVE_CFLAGS=$CFLAGS
+  CFLAGS="$CFLAGS -belf"
+  AC_CACHE_CHECK([whether the C compiler needs -belf], lt_cv_cc_needs_belf,
+    [AC_LANG_PUSH(C)
+     AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],[[]])],[lt_cv_cc_needs_belf=yes],[lt_cv_cc_needs_belf=no])
+     AC_LANG_POP])
+  if test yes != "$lt_cv_cc_needs_belf"; then
+    # this is probably gcc 2.8.0, egcs 1.0 or newer; no need for -belf
+    CFLAGS=$SAVE_CFLAGS
+  fi
+  ;;
+*-*solaris*)
+  # Find out what ABI is being produced by ac_compile, and set linker
+  # options accordingly.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.o` in
+    *64-bit*)
+      case $lt_cv_prog_gnu_ld in
+      yes*)
+        case $host in
+        i?86-*-solaris*|x86_64-*-solaris*)
+          LD="${LD-ld} -m elf_x86_64"
+          ;;
+        sparc*-*-solaris*)
+          LD="${LD-ld} -m elf64_sparc"
+          ;;
+        esac
+        # GNU ld 2.21 introduced _sol2 emulations.  Use them if available.
+        if ${LD-ld} -V | grep _sol2 >/dev/null 2>&1; then
+          LD=${LD-ld}_sol2
+        fi
+        ;;
+      *)
+	if ${LD-ld} -64 -r -o conftest2.o conftest.o >/dev/null 2>&1; then
+	  LD="${LD-ld} -64"
+	fi
+	;;
+      esac
+      ;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+esac
+
+need_locks=$enable_libtool_lock
+])# _LT_ENABLE_LOCK
+
+
+# _LT_PROG_AR
+# -----------
+m4_defun([_LT_PROG_AR],
+[AC_CHECK_TOOLS(AR, [ar], false)
+: ${AR=ar}
+: ${AR_FLAGS=cru}
+_LT_DECL([], [AR], [1], [The archiver])
+_LT_DECL([], [AR_FLAGS], [1], [Flags to create an archive])
+
+AC_CACHE_CHECK([for archiver @FILE support], [lt_cv_ar_at_file],
+  [lt_cv_ar_at_file=no
+   AC_COMPILE_IFELSE([AC_LANG_PROGRAM],
+     [echo conftest.$ac_objext > conftest.lst
+      lt_ar_try='$AR $AR_FLAGS libconftest.a @conftest.lst >&AS_MESSAGE_LOG_FD'
+      AC_TRY_EVAL([lt_ar_try])
+      if test 0 -eq "$ac_status"; then
+	# Ensure the archiver fails upon bogus file names.
+	rm -f conftest.$ac_objext libconftest.a
+	AC_TRY_EVAL([lt_ar_try])
+	if test 0 -ne "$ac_status"; then
+          lt_cv_ar_at_file=@
+        fi
+      fi
+      rm -f conftest.* libconftest.a
+     ])
+  ])
+
+if test no = "$lt_cv_ar_at_file"; then
+  archiver_list_spec=
+else
+  archiver_list_spec=$lt_cv_ar_at_file
+fi
+_LT_DECL([], [archiver_list_spec], [1],
+  [How to feed a file listing to the archiver])
+])# _LT_PROG_AR
+
+
+# _LT_CMD_OLD_ARCHIVE
+# -------------------
+m4_defun([_LT_CMD_OLD_ARCHIVE],
+[_LT_PROG_AR
+
+AC_CHECK_TOOL(STRIP, strip, :)
+test -z "$STRIP" && STRIP=:
+_LT_DECL([], [STRIP], [1], [A symbol stripping program])
+
+AC_CHECK_TOOL(RANLIB, ranlib, :)
+test -z "$RANLIB" && RANLIB=:
+_LT_DECL([], [RANLIB], [1],
+    [Commands used to install an old-style archive])
+
+# Determine commands to create old-style static archives.
+old_archive_cmds='$AR $AR_FLAGS $oldlib$oldobjs'
+old_postinstall_cmds='chmod 644 $oldlib'
+old_postuninstall_cmds=
+
+if test -n "$RANLIB"; then
+  case $host_os in
+  bitrig* | openbsd*)
+    old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB -t \$tool_oldlib"
+    ;;
+  *)
+    old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB \$tool_oldlib"
+    ;;
+  esac
+  old_archive_cmds="$old_archive_cmds~\$RANLIB \$tool_oldlib"
+fi
+
+case $host_os in
+  darwin*)
+    lock_old_archive_extraction=yes ;;
+  *)
+    lock_old_archive_extraction=no ;;
+esac
+_LT_DECL([], [old_postinstall_cmds], [2])
+_LT_DECL([], [old_postuninstall_cmds], [2])
+_LT_TAGDECL([], [old_archive_cmds], [2],
+    [Commands used to build an old-style archive])
+_LT_DECL([], [lock_old_archive_extraction], [0],
+    [Whether to use a lock for old archive extraction])
+])# _LT_CMD_OLD_ARCHIVE
+
+
+# _LT_COMPILER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS,
+#		[OUTPUT-FILE], [ACTION-SUCCESS], [ACTION-FAILURE])
+# ----------------------------------------------------------------
+# Check whether the given compiler option works
+AC_DEFUN([_LT_COMPILER_OPTION],
+[m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_DECL_SED])dnl
+AC_CACHE_CHECK([$1], [$2],
+  [$2=no
+   m4_if([$4], , [ac_outfile=conftest.$ac_objext], [ac_outfile=$4])
+   echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+   lt_compiler_flag="$3"  ## exclude from sc_useless_quotes_in_assignment
+   # Insert the option either (1) after the last *FLAGS variable, or
+   # (2) before a word containing "conftest.", or (3) at the end.
+   # Note that $ac_compile itself does not contain backslashes and begins
+   # with a dollar sign (not a hyphen), so the echo should work correctly.
+   # The option is referenced via a variable to avoid confusing sed.
+   lt_compile=`echo "$ac_compile" | $SED \
+   -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
+   -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \
+   -e 's:$: $lt_compiler_flag:'`
+   (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD)
+   (eval "$lt_compile" 2>conftest.err)
+   ac_status=$?
+   cat conftest.err >&AS_MESSAGE_LOG_FD
+   echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD
+   if (exit $ac_status) && test -s "$ac_outfile"; then
+     # The compiler can only warn and ignore the option if not recognized
+     # So say no if there are warnings other than the usual output.
+     $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' >conftest.exp
+     $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
+     if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
+       $2=yes
+     fi
+   fi
+   $RM conftest*
+])
+
+if test yes = "[$]$2"; then
+    m4_if([$5], , :, [$5])
+else
+    m4_if([$6], , :, [$6])
+fi
+])# _LT_COMPILER_OPTION
+
+# Old name:
+AU_ALIAS([AC_LIBTOOL_COMPILER_OPTION], [_LT_COMPILER_OPTION])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_COMPILER_OPTION], [])
+
+
+# _LT_LINKER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS,
+#                  [ACTION-SUCCESS], [ACTION-FAILURE])
+# ----------------------------------------------------
+# Check whether the given linker option works
+AC_DEFUN([_LT_LINKER_OPTION],
+[m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_DECL_SED])dnl
+AC_CACHE_CHECK([$1], [$2],
+  [$2=no
+   save_LDFLAGS=$LDFLAGS
+   LDFLAGS="$LDFLAGS $3"
+   echo "$lt_simple_link_test_code" > conftest.$ac_ext
+   if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then
+     # The linker can only warn and ignore the option if not recognized
+     # So say no if there are warnings
+     if test -s conftest.err; then
+       # Append any errors to the config.log.
+       cat conftest.err 1>&AS_MESSAGE_LOG_FD
+       $ECHO "$_lt_linker_boilerplate" | $SED '/^$/d' > conftest.exp
+       $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
+       if diff conftest.exp conftest.er2 >/dev/null; then
+         $2=yes
+       fi
+     else
+       $2=yes
+     fi
+   fi
+   $RM -r conftest*
+   LDFLAGS=$save_LDFLAGS
+])
+
+if test yes = "[$]$2"; then
+    m4_if([$4], , :, [$4])
+else
+    m4_if([$5], , :, [$5])
+fi
+])# _LT_LINKER_OPTION
+
+# Old name:
+AU_ALIAS([AC_LIBTOOL_LINKER_OPTION], [_LT_LINKER_OPTION])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_LINKER_OPTION], [])
+
+
+# LT_CMD_MAX_LEN
+#---------------
+AC_DEFUN([LT_CMD_MAX_LEN],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+# find the maximum length of command line arguments
+AC_MSG_CHECKING([the maximum length of command line arguments])
+AC_CACHE_VAL([lt_cv_sys_max_cmd_len], [dnl
+  i=0
+  teststring=ABCD
+
+  case $build_os in
+  msdosdjgpp*)
+    # On DJGPP, this test can blow up pretty badly due to problems in libc
+    # (any single argument exceeding 2000 bytes causes a buffer overrun
+    # during glob expansion).  Even if it were fixed, the result of this
+    # check would be larger than it should be.
+    lt_cv_sys_max_cmd_len=12288;    # 12K is about right
+    ;;
+
+  gnu*)
+    # Under GNU Hurd, this test is not required because there is
+    # no limit to the length of command line arguments.
+    # Libtool will interpret -1 as no limit whatsoever
+    lt_cv_sys_max_cmd_len=-1;
+    ;;
+
+  cygwin* | mingw* | cegcc*)
+    # On Win9x/ME, this test blows up -- it succeeds, but takes
+    # about 5 minutes as the teststring grows exponentially.
+    # Worse, since 9x/ME are not pre-emptively multitasking,
+    # you end up with a "frozen" computer, even though with patience
+    # the test eventually succeeds (with a max line length of 256k).
+    # Instead, let's just punt: use the minimum linelength reported by
+    # all of the supported platforms: 8192 (on NT/2K/XP).
+    lt_cv_sys_max_cmd_len=8192;
+    ;;
+
+  mint*)
+    # On MiNT this can take a long time and run out of memory.
+    lt_cv_sys_max_cmd_len=8192;
+    ;;
+
+  amigaos*)
+    # On AmigaOS with pdksh, this test takes hours, literally.
+    # So we just punt and use a minimum line length of 8192.
+    lt_cv_sys_max_cmd_len=8192;
+    ;;
+
+  bitrig* | darwin* | dragonfly* | freebsd* | netbsd* | openbsd*)
+    # This has been around since 386BSD, at least.  Likely further.
+    if test -x /sbin/sysctl; then
+      lt_cv_sys_max_cmd_len=`/sbin/sysctl -n kern.argmax`
+    elif test -x /usr/sbin/sysctl; then
+      lt_cv_sys_max_cmd_len=`/usr/sbin/sysctl -n kern.argmax`
+    else
+      lt_cv_sys_max_cmd_len=65536	# usable default for all BSDs
+    fi
+    # And add a safety zone
+    lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4`
+    lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3`
+    ;;
+
+  interix*)
+    # We know the value 262144 and hardcode it with a safety zone (like BSD)
+    lt_cv_sys_max_cmd_len=196608
+    ;;
+
+  os2*)
+    # The test takes a long time on OS/2.
+    lt_cv_sys_max_cmd_len=8192
+    ;;
+
+  osf*)
+    # Dr. Hans Ekkehard Plesser reports seeing a kernel panic running configure
+    # due to this test when exec_disable_arg_limit is 1 on Tru64. It is not
+    # nice to cause kernel panics so lets avoid the loop below.
+    # First set a reasonable default.
+    lt_cv_sys_max_cmd_len=16384
+    #
+    if test -x /sbin/sysconfig; then
+      case `/sbin/sysconfig -q proc exec_disable_arg_limit` in
+        *1*) lt_cv_sys_max_cmd_len=-1 ;;
+      esac
+    fi
+    ;;
+  sco3.2v5*)
+    lt_cv_sys_max_cmd_len=102400
+    ;;
+  sysv5* | sco5v6* | sysv4.2uw2*)
+    kargmax=`grep ARG_MAX /etc/conf/cf.d/stune 2>/dev/null`
+    if test -n "$kargmax"; then
+      lt_cv_sys_max_cmd_len=`echo $kargmax | sed 's/.*[[	 ]]//'`
+    else
+      lt_cv_sys_max_cmd_len=32768
+    fi
+    ;;
+  *)
+    lt_cv_sys_max_cmd_len=`(getconf ARG_MAX) 2> /dev/null`
+    if test -n "$lt_cv_sys_max_cmd_len" && \
+       test undefined != "$lt_cv_sys_max_cmd_len"; then
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4`
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3`
+    else
+      # Make teststring a little bigger before we do anything with it.
+      # a 1K string should be a reasonable start.
+      for i in 1 2 3 4 5 6 7 8; do
+        teststring=$teststring$teststring
+      done
+      SHELL=${SHELL-${CONFIG_SHELL-/bin/sh}}
+      # If test is not a shell built-in, we'll probably end up computing a
+      # maximum length that is only half of the actual maximum length, but
+      # we can't tell.
+      while { test X`env echo "$teststring$teststring" 2>/dev/null` \
+	         = "X$teststring$teststring"; } >/dev/null 2>&1 &&
+	      test 17 != "$i" # 1/2 MB should be enough
+      do
+        i=`expr $i + 1`
+        teststring=$teststring$teststring
+      done
+      # Only check the string length outside the loop.
+      lt_cv_sys_max_cmd_len=`expr "X$teststring" : ".*" 2>&1`
+      teststring=
+      # Add a significant safety factor because C++ compilers can tack on
+      # massive amounts of additional arguments before passing them to the
+      # linker.  It appears as though 1/2 is a usable value.
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 2`
+    fi
+    ;;
+  esac
+])
+if test -n "$lt_cv_sys_max_cmd_len"; then
+  AC_MSG_RESULT($lt_cv_sys_max_cmd_len)
+else
+  AC_MSG_RESULT(none)
+fi
+max_cmd_len=$lt_cv_sys_max_cmd_len
+_LT_DECL([], [max_cmd_len], [0],
+    [What is the maximum length of a command?])
+])# LT_CMD_MAX_LEN
+
+# Old name:
+AU_ALIAS([AC_LIBTOOL_SYS_MAX_CMD_LEN], [LT_CMD_MAX_LEN])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_SYS_MAX_CMD_LEN], [])
+
+
+# _LT_HEADER_DLFCN
+# ----------------
+m4_defun([_LT_HEADER_DLFCN],
+[AC_CHECK_HEADERS([dlfcn.h], [], [], [AC_INCLUDES_DEFAULT])dnl
+])# _LT_HEADER_DLFCN
+
+
+# _LT_TRY_DLOPEN_SELF (ACTION-IF-TRUE, ACTION-IF-TRUE-W-USCORE,
+#                      ACTION-IF-FALSE, ACTION-IF-CROSS-COMPILING)
+# ----------------------------------------------------------------
+m4_defun([_LT_TRY_DLOPEN_SELF],
+[m4_require([_LT_HEADER_DLFCN])dnl
+if test yes = "$cross_compiling"; then :
+  [$4]
+else
+  lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
+  lt_status=$lt_dlunknown
+  cat > conftest.$ac_ext <<_LT_EOF
+[#line $LINENO "configure"
+#include "confdefs.h"
+
+#if HAVE_DLFCN_H
+#include <dlfcn.h>
+#endif
+
+#include <stdio.h>
+
+#ifdef RTLD_GLOBAL
+#  define LT_DLGLOBAL		RTLD_GLOBAL
+#else
+#  ifdef DL_GLOBAL
+#    define LT_DLGLOBAL		DL_GLOBAL
+#  else
+#    define LT_DLGLOBAL		0
+#  endif
+#endif
+
+/* We may have to define LT_DLLAZY_OR_NOW in the command line if we
+   find out it does not work in some platform. */
+#ifndef LT_DLLAZY_OR_NOW
+#  ifdef RTLD_LAZY
+#    define LT_DLLAZY_OR_NOW		RTLD_LAZY
+#  else
+#    ifdef DL_LAZY
+#      define LT_DLLAZY_OR_NOW		DL_LAZY
+#    else
+#      ifdef RTLD_NOW
+#        define LT_DLLAZY_OR_NOW	RTLD_NOW
+#      else
+#        ifdef DL_NOW
+#          define LT_DLLAZY_OR_NOW	DL_NOW
+#        else
+#          define LT_DLLAZY_OR_NOW	0
+#        endif
+#      endif
+#    endif
+#  endif
+#endif
+
+/* When -fvisibility=hidden is used, assume the code has been annotated
+   correspondingly for the symbols needed.  */
+#if defined __GNUC__ && (((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 3))
+int fnord () __attribute__((visibility("default")));
+#endif
+
+int fnord () { return 42; }
+int main ()
+{
+  void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW);
+  int status = $lt_dlunknown;
+
+  if (self)
+    {
+      if (dlsym (self,"fnord"))       status = $lt_dlno_uscore;
+      else
+        {
+	  if (dlsym( self,"_fnord"))  status = $lt_dlneed_uscore;
+          else puts (dlerror ());
+	}
+      /* dlclose (self); */
+    }
+  else
+    puts (dlerror ());
+
+  return status;
+}]
+_LT_EOF
+  if AC_TRY_EVAL(ac_link) && test -s "conftest$ac_exeext" 2>/dev/null; then
+    (./conftest; exit; ) >&AS_MESSAGE_LOG_FD 2>/dev/null
+    lt_status=$?
+    case x$lt_status in
+      x$lt_dlno_uscore) $1 ;;
+      x$lt_dlneed_uscore) $2 ;;
+      x$lt_dlunknown|x*) $3 ;;
+    esac
+  else :
+    # compilation failed
+    $3
+  fi
+fi
+rm -fr conftest*
+])# _LT_TRY_DLOPEN_SELF
+
+
+# LT_SYS_DLOPEN_SELF
+# ------------------
+AC_DEFUN([LT_SYS_DLOPEN_SELF],
+[m4_require([_LT_HEADER_DLFCN])dnl
+if test yes != "$enable_dlopen"; then
+  enable_dlopen=unknown
+  enable_dlopen_self=unknown
+  enable_dlopen_self_static=unknown
+else
+  lt_cv_dlopen=no
+  lt_cv_dlopen_libs=
+
+  case $host_os in
+  beos*)
+    lt_cv_dlopen=load_add_on
+    lt_cv_dlopen_libs=
+    lt_cv_dlopen_self=yes
+    ;;
+
+  mingw* | pw32* | cegcc*)
+    lt_cv_dlopen=LoadLibrary
+    lt_cv_dlopen_libs=
+    ;;
+
+  cygwin*)
+    lt_cv_dlopen=dlopen
+    lt_cv_dlopen_libs=
+    ;;
+
+  darwin*)
+    # if libdl is installed we need to link against it
+    AC_CHECK_LIB([dl], [dlopen],
+		[lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl],[
+    lt_cv_dlopen=dyld
+    lt_cv_dlopen_libs=
+    lt_cv_dlopen_self=yes
+    ])
+    ;;
+
+  tpf*)
+    # Don't try to run any link tests for TPF.  We know it's impossible
+    # because TPF is a cross-compiler, and we know how we open DSOs.
+    lt_cv_dlopen=dlopen
+    lt_cv_dlopen_libs=
+    lt_cv_dlopen_self=no
+    ;;
+
+  *)
+    AC_CHECK_FUNC([shl_load],
+	  [lt_cv_dlopen=shl_load],
+      [AC_CHECK_LIB([dld], [shl_load],
+	    [lt_cv_dlopen=shl_load lt_cv_dlopen_libs=-ldld],
+	[AC_CHECK_FUNC([dlopen],
+	      [lt_cv_dlopen=dlopen],
+	  [AC_CHECK_LIB([dl], [dlopen],
+		[lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-ldl],
+	    [AC_CHECK_LIB([svld], [dlopen],
+		  [lt_cv_dlopen=dlopen lt_cv_dlopen_libs=-lsvld],
+	      [AC_CHECK_LIB([dld], [dld_link],
+		    [lt_cv_dlopen=dld_link lt_cv_dlopen_libs=-ldld])
+	      ])
+	    ])
+	  ])
+	])
+      ])
+    ;;
+  esac
+
+  if test no = "$lt_cv_dlopen"; then
+    enable_dlopen=no
+  else
+    enable_dlopen=yes
+  fi
+
+  case $lt_cv_dlopen in
+  dlopen)
+    save_CPPFLAGS=$CPPFLAGS
+    test yes = "$ac_cv_header_dlfcn_h" && CPPFLAGS="$CPPFLAGS -DHAVE_DLFCN_H"
+
+    save_LDFLAGS=$LDFLAGS
+    wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $export_dynamic_flag_spec\"
+
+    save_LIBS=$LIBS
+    LIBS="$lt_cv_dlopen_libs $LIBS"
+
+    AC_CACHE_CHECK([whether a program can dlopen itself],
+	  lt_cv_dlopen_self, [dnl
+	  _LT_TRY_DLOPEN_SELF(
+	    lt_cv_dlopen_self=yes, lt_cv_dlopen_self=yes,
+	    lt_cv_dlopen_self=no, lt_cv_dlopen_self=cross)
+    ])
+
+    if test yes = "$lt_cv_dlopen_self"; then
+      wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $lt_prog_compiler_static\"
+      AC_CACHE_CHECK([whether a statically linked program can dlopen itself],
+	  lt_cv_dlopen_self_static, [dnl
+	  _LT_TRY_DLOPEN_SELF(
+	    lt_cv_dlopen_self_static=yes, lt_cv_dlopen_self_static=yes,
+	    lt_cv_dlopen_self_static=no,  lt_cv_dlopen_self_static=cross)
+      ])
+    fi
+
+    CPPFLAGS=$save_CPPFLAGS
+    LDFLAGS=$save_LDFLAGS
+    LIBS=$save_LIBS
+    ;;
+  esac
+
+  case $lt_cv_dlopen_self in
+  yes|no) enable_dlopen_self=$lt_cv_dlopen_self ;;
+  *) enable_dlopen_self=unknown ;;
+  esac
+
+  case $lt_cv_dlopen_self_static in
+  yes|no) enable_dlopen_self_static=$lt_cv_dlopen_self_static ;;
+  *) enable_dlopen_self_static=unknown ;;
+  esac
+fi
+_LT_DECL([dlopen_support], [enable_dlopen], [0],
+	 [Whether dlopen is supported])
+_LT_DECL([dlopen_self], [enable_dlopen_self], [0],
+	 [Whether dlopen of programs is supported])
+_LT_DECL([dlopen_self_static], [enable_dlopen_self_static], [0],
+	 [Whether dlopen of statically linked programs is supported])
+])# LT_SYS_DLOPEN_SELF
+
+# Old name:
+AU_ALIAS([AC_LIBTOOL_DLOPEN_SELF], [LT_SYS_DLOPEN_SELF])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_DLOPEN_SELF], [])
+
+
+# _LT_COMPILER_C_O([TAGNAME])
+# ---------------------------
+# Check to see if options -c and -o are simultaneously supported by compiler.
+# This macro does not hard code the compiler like AC_PROG_CC_C_O.
+m4_defun([_LT_COMPILER_C_O],
+[m4_require([_LT_DECL_SED])dnl
+m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_TAG_COMPILER])dnl
+AC_CACHE_CHECK([if $compiler supports -c -o file.$ac_objext],
+  [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)],
+  [_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=no
+   $RM -r conftest 2>/dev/null
+   mkdir conftest
+   cd conftest
+   mkdir out
+   echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+
+   lt_compiler_flag="-o out/conftest2.$ac_objext"
+   # Insert the option either (1) after the last *FLAGS variable, or
+   # (2) before a word containing "conftest.", or (3) at the end.
+   # Note that $ac_compile itself does not contain backslashes and begins
+   # with a dollar sign (not a hyphen), so the echo should work correctly.
+   lt_compile=`echo "$ac_compile" | $SED \
+   -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
+   -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \
+   -e 's:$: $lt_compiler_flag:'`
+   (eval echo "\"\$as_me:$LINENO: $lt_compile\"" >&AS_MESSAGE_LOG_FD)
+   (eval "$lt_compile" 2>out/conftest.err)
+   ac_status=$?
+   cat out/conftest.err >&AS_MESSAGE_LOG_FD
+   echo "$as_me:$LINENO: \$? = $ac_status" >&AS_MESSAGE_LOG_FD
+   if (exit $ac_status) && test -s out/conftest2.$ac_objext
+   then
+     # The compiler can only warn and ignore the option if not recognized
+     # So say no if there are warnings
+     $ECHO "$_lt_compiler_boilerplate" | $SED '/^$/d' > out/conftest.exp
+     $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
+     if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
+       _LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes
+     fi
+   fi
+   chmod u+w . 2>&AS_MESSAGE_LOG_FD
+   $RM conftest*
+   # SGI C++ compiler will create directory out/ii_files/ for
+   # template instantiation
+   test -d out/ii_files && $RM out/ii_files/* && rmdir out/ii_files
+   $RM out/* && rmdir out
+   cd ..
+   $RM -r conftest
+   $RM conftest*
+])
+_LT_TAGDECL([compiler_c_o], [lt_cv_prog_compiler_c_o], [1],
+	[Does compiler simultaneously support -c and -o options?])
+])# _LT_COMPILER_C_O
+
+
+# _LT_COMPILER_FILE_LOCKS([TAGNAME])
+# ----------------------------------
+# Check to see if we can do hard links to lock some files if needed
+m4_defun([_LT_COMPILER_FILE_LOCKS],
+[m4_require([_LT_ENABLE_LOCK])dnl
+m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+_LT_COMPILER_C_O([$1])
+
+hard_links=nottested
+if test no = "$_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)" && test no != "$need_locks"; then
+  # do not overwrite the value of need_locks provided by the user
+  AC_MSG_CHECKING([if we can lock with hard links])
+  hard_links=yes
+  $RM conftest*
+  ln conftest.a conftest.b 2>/dev/null && hard_links=no
+  touch conftest.a
+  ln conftest.a conftest.b 2>&5 || hard_links=no
+  ln conftest.a conftest.b 2>/dev/null && hard_links=no
+  AC_MSG_RESULT([$hard_links])
+  if test no = "$hard_links"; then
+    AC_MSG_WARN(['$CC' does not support '-c -o', so 'make -j' may be unsafe])
+    need_locks=warn
+  fi
+else
+  need_locks=no
+fi
+_LT_DECL([], [need_locks], [1], [Must we lock files when doing compilation?])
+])# _LT_COMPILER_FILE_LOCKS
+
+
+# _LT_CHECK_OBJDIR
+# ----------------
+m4_defun([_LT_CHECK_OBJDIR],
+[AC_CACHE_CHECK([for objdir], [lt_cv_objdir],
+[rm -f .libs 2>/dev/null
+mkdir .libs 2>/dev/null
+if test -d .libs; then
+  lt_cv_objdir=.libs
+else
+  # MS-DOS does not allow filenames that begin with a dot.
+  lt_cv_objdir=_libs
+fi
+rmdir .libs 2>/dev/null])
+objdir=$lt_cv_objdir
+_LT_DECL([], [objdir], [0],
+         [The name of the directory that contains temporary libtool files])dnl
+m4_pattern_allow([LT_OBJDIR])dnl
+AC_DEFINE_UNQUOTED([LT_OBJDIR], "$lt_cv_objdir/",
+  [Define to the sub-directory where libtool stores uninstalled libraries.])
+])# _LT_CHECK_OBJDIR
+
+
+# _LT_LINKER_HARDCODE_LIBPATH([TAGNAME])
+# --------------------------------------
+# Check hardcoding attributes.
+m4_defun([_LT_LINKER_HARDCODE_LIBPATH],
+[AC_MSG_CHECKING([how to hardcode library paths into programs])
+_LT_TAGVAR(hardcode_action, $1)=
+if test -n "$_LT_TAGVAR(hardcode_libdir_flag_spec, $1)" ||
+   test -n "$_LT_TAGVAR(runpath_var, $1)" ||
+   test yes = "$_LT_TAGVAR(hardcode_automatic, $1)"; then
+
+  # We can hardcode non-existent directories.
+  if test no != "$_LT_TAGVAR(hardcode_direct, $1)" &&
+     # If the only mechanism to avoid hardcoding is shlibpath_var, we
+     # have to relink, otherwise we might link with an installed library
+     # when we should be linking with a yet-to-be-installed one
+     ## test no != "$_LT_TAGVAR(hardcode_shlibpath_var, $1)" &&
+     test no != "$_LT_TAGVAR(hardcode_minus_L, $1)"; then
+    # Linking always hardcodes the temporary library directory.
+    _LT_TAGVAR(hardcode_action, $1)=relink
+  else
+    # We can link without hardcoding, and we can hardcode nonexisting dirs.
+    _LT_TAGVAR(hardcode_action, $1)=immediate
+  fi
+else
+  # We cannot hardcode anything, or else we can only hardcode existing
+  # directories.
+  _LT_TAGVAR(hardcode_action, $1)=unsupported
+fi
+AC_MSG_RESULT([$_LT_TAGVAR(hardcode_action, $1)])
+
+if test relink = "$_LT_TAGVAR(hardcode_action, $1)" ||
+   test yes = "$_LT_TAGVAR(inherit_rpath, $1)"; then
+  # Fast installation is not supported
+  enable_fast_install=no
+elif test yes = "$shlibpath_overrides_runpath" ||
+     test no = "$enable_shared"; then
+  # Fast installation is not necessary
+  enable_fast_install=needless
+fi
+_LT_TAGDECL([], [hardcode_action], [0],
+    [How to hardcode a shared library path into an executable])
+])# _LT_LINKER_HARDCODE_LIBPATH
+
+
+# _LT_CMD_STRIPLIB
+# ----------------
+m4_defun([_LT_CMD_STRIPLIB],
+[m4_require([_LT_DECL_EGREP])
+striplib=
+old_striplib=
+AC_MSG_CHECKING([whether stripping libraries is possible])
+if test -n "$STRIP" && $STRIP -V 2>&1 | $GREP "GNU strip" >/dev/null; then
+  test -z "$old_striplib" && old_striplib="$STRIP --strip-debug"
+  test -z "$striplib" && striplib="$STRIP --strip-unneeded"
+  AC_MSG_RESULT([yes])
+else
+# FIXME - insert some real tests, host_os isn't really good enough
+  case $host_os in
+  darwin*)
+    if test -n "$STRIP"; then
+      striplib="$STRIP -x"
+      old_striplib="$STRIP -S"
+      AC_MSG_RESULT([yes])
+    else
+      AC_MSG_RESULT([no])
+    fi
+    ;;
+  *)
+    AC_MSG_RESULT([no])
+    ;;
+  esac
+fi
+_LT_DECL([], [old_striplib], [1], [Commands to strip libraries])
+_LT_DECL([], [striplib], [1])
+])# _LT_CMD_STRIPLIB
+
+
+# _LT_PREPARE_MUNGE_PATH_LIST
+# ---------------------------
+# Make sure func_munge_path_list() is defined correctly.
+m4_defun([_LT_PREPARE_MUNGE_PATH_LIST],
+[[# func_munge_path_list VARIABLE PATH
+# -----------------------------------
+# VARIABLE is name of variable containing _space_ separated list of
+# directories to be munged by the contents of PATH, which is string
+# having a format:
+# "DIR[:DIR]:"
+#       string "DIR[ DIR]" will be prepended to VARIABLE
+# ":DIR[:DIR]"
+#       string "DIR[ DIR]" will be appended to VARIABLE
+# "DIRP[:DIRP]::[DIRA:]DIRA"
+#       string "DIRP[ DIRP]" will be prepended to VARIABLE and string
+#       "DIRA[ DIRA]" will be appended to VARIABLE
+# "DIR[:DIR]"
+#       VARIABLE will be replaced by "DIR[ DIR]"
+func_munge_path_list ()
+{
+    case x@S|@2 in
+    x)
+        ;;
+    *:)
+        eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'` \@S|@@S|@1\"
+        ;;
+    x:*)
+        eval @S|@1=\"\@S|@@S|@1 `$ECHO @S|@2 | $SED 's/:/ /g'`\"
+        ;;
+    *::*)
+        eval @S|@1=\"\@S|@@S|@1\ `$ECHO @S|@2 | $SED -e 's/.*:://' -e 's/:/ /g'`\"
+        eval @S|@1=\"`$ECHO @S|@2 | $SED -e 's/::.*//' -e 's/:/ /g'`\ \@S|@@S|@1\"
+        ;;
+    *)
+        eval @S|@1=\"`$ECHO @S|@2 | $SED 's/:/ /g'`\"
+        ;;
+    esac
+}
+]])# _LT_PREPARE_PATH_LIST
+
+
+# _LT_SYS_DYNAMIC_LINKER([TAG])
+# -----------------------------
+# PORTME Fill in your ld.so characteristics
+m4_defun([_LT_SYS_DYNAMIC_LINKER],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+m4_require([_LT_DECL_EGREP])dnl
+m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_DECL_OBJDUMP])dnl
+m4_require([_LT_DECL_SED])dnl
+m4_require([_LT_CHECK_SHELL_FEATURES])dnl
+m4_require([_LT_PREPARE_MUNGE_PATH_LIST])dnl
+AC_MSG_CHECKING([dynamic linker characteristics])
+m4_if([$1],
+	[], [
+if test yes = "$GCC"; then
+  case $host_os in
+    darwin*) lt_awk_arg='/^libraries:/,/LR/' ;;
+    *) lt_awk_arg='/^libraries:/' ;;
+  esac
+  case $host_os in
+    mingw* | cegcc*) lt_sed_strip_eq='s|=\([[A-Za-z]]:\)|\1|g' ;;
+    *) lt_sed_strip_eq='s|=/|/|g' ;;
+  esac
+  lt_search_path_spec=`$CC -print-search-dirs | awk $lt_awk_arg | $SED -e "s/^libraries://" -e $lt_sed_strip_eq`
+  case $lt_search_path_spec in
+  *\;*)
+    # if the path contains ";" then we assume it to be the separator
+    # otherwise default to the standard path separator (i.e. ":") - it is
+    # assumed that no part of a normal pathname contains ";" but that should
+    # okay in the real world where ";" in dirpaths is itself problematic.
+    lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED 's/;/ /g'`
+    ;;
+  *)
+    lt_search_path_spec=`$ECHO "$lt_search_path_spec" | $SED "s/$PATH_SEPARATOR/ /g"`
+    ;;
+  esac
+  # Ok, now we have the path, separated by spaces, we can step through it
+  # and add multilib dir if necessary...
+  lt_tmp_lt_search_path_spec=
+  lt_multi_os_dir=/`$CC $CPPFLAGS $CFLAGS $LDFLAGS -print-multi-os-directory 2>/dev/null`
+  # ...but if some path component already ends with the multilib dir we assume
+  # that all is fine and trust -print-search-dirs as is (GCC 4.2? or newer).
+  case "$lt_multi_os_dir; $lt_search_path_spec " in
+  "/; "* | "/.; "* | "/./; "* | *"$lt_multi_os_dir "* | *"$lt_multi_os_dir/ "*)
+    lt_multi_os_dir=
+    ;;
+  esac
+  for lt_sys_path in $lt_search_path_spec; do
+    if test -d "$lt_sys_path$lt_multi_os_dir"; then
+      lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path$lt_multi_os_dir"
+    elif test -n "$lt_multi_os_dir"; then
+      test -d "$lt_sys_path" && \
+	lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path"
+    fi
+  done
+  lt_search_path_spec=`$ECHO "$lt_tmp_lt_search_path_spec" | awk '
+BEGIN {RS = " "; FS = "/|\n";} {
+  lt_foo = "";
+  lt_count = 0;
+  for (lt_i = NF; lt_i > 0; lt_i--) {
+    if ($lt_i != "" && $lt_i != ".") {
+      if ($lt_i == "..") {
+        lt_count++;
+      } else {
+        if (lt_count == 0) {
+          lt_foo = "/" $lt_i lt_foo;
+        } else {
+          lt_count--;
+        }
+      }
+    }
+  }
+  if (lt_foo != "") { lt_freq[[lt_foo]]++; }
+  if (lt_freq[[lt_foo]] == 1) { print lt_foo; }
+}'`
+  # AWK program above erroneously prepends '/' to C:/dos/paths
+  # for these hosts.
+  case $host_os in
+    mingw* | cegcc*) lt_search_path_spec=`$ECHO "$lt_search_path_spec" |\
+      $SED 's|/\([[A-Za-z]]:\)|\1|g'` ;;
+  esac
+  sys_lib_search_path_spec=`$ECHO "$lt_search_path_spec" | $lt_NL2SP`
+else
+  sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib"
+fi])
+library_names_spec=
+libname_spec='lib$name'
+soname_spec=
+shrext_cmds=.so
+postinstall_cmds=
+postuninstall_cmds=
+finish_cmds=
+finish_eval=
+shlibpath_var=
+shlibpath_overrides_runpath=unknown
+version_type=none
+dynamic_linker="$host_os ld.so"
+sys_lib_dlsearch_path_spec="/lib /usr/lib"
+need_lib_prefix=unknown
+hardcode_into_libs=no
+
+# when you set need_version to no, make sure it does not cause -set_version
+# flags to be left without arguments
+need_version=unknown
+
+AC_ARG_VAR([LT_SYS_LIBRARY_PATH],
+[User-defined run-time library search path.])
+
+case $host_os in
+aix3*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  library_names_spec='$libname$release$shared_ext$versuffix $libname.a'
+  shlibpath_var=LIBPATH
+
+  # AIX 3 has no versioning support, so we append a major version to the name.
+  soname_spec='$libname$release$shared_ext$major'
+  ;;
+
+aix[[4-9]]*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  hardcode_into_libs=yes
+  if test ia64 = "$host_cpu"; then
+    # AIX 5 supports IA64
+    library_names_spec='$libname$release$shared_ext$major $libname$release$shared_ext$versuffix $libname$shared_ext'
+    shlibpath_var=LD_LIBRARY_PATH
+  else
+    # With GCC up to 2.95.x, collect2 would create an import file
+    # for dependence libraries.  The import file would start with
+    # the line '#! .'.  This would cause the generated library to
+    # depend on '.', always an invalid library.  This was fixed in
+    # development snapshots of GCC prior to 3.0.
+    case $host_os in
+      aix4 | aix4.[[01]] | aix4.[[01]].*)
+      if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)'
+	   echo ' yes '
+	   echo '#endif'; } | $CC -E - | $GREP yes > /dev/null; then
+	:
+      else
+	can_build_shared=no
+      fi
+      ;;
+    esac
+    # Using Import Files as archive members, it is possible to support
+    # filename-based versioning of shared library archives on AIX. While
+    # this would work for both with and without runtime linking, it will
+    # prevent static linking of such archives. So we do filename-based
+    # shared library versioning with .so extension only, which is used
+    # when both runtime linking and shared linking is enabled.
+    # Unfortunately, runtime linking may impact performance, so we do
+    # not want this to be the default eventually. Also, we use the
+    # versioned .so libs for executables only if there is the -brtl
+    # linker flag in LDFLAGS as well, or --with-aix-soname=svr4 only.
+    # To allow for filename-based versioning support, we need to create
+    # libNAME.so.V as an archive file, containing:
+    # *) an Import File, referring to the versioned filename of the
+    #    archive as well as the shared archive member, telling the
+    #    bitwidth (32 or 64) of that shared object, and providing the
+    #    list of exported symbols of that shared object, eventually
+    #    decorated with the 'weak' keyword
+    # *) the shared object with the F_LOADONLY flag set, to really avoid
+    #    it being seen by the linker.
+    # At run time we better use the real file rather than another symlink,
+    # but for link time we create the symlink libNAME.so -> libNAME.so.V
+
+    case $with_aix_soname,$aix_use_runtimelinking in
+    # AIX (on Power*) has no versioning support, so currently we cannot hardcode correct
+    # soname into executable. Probably we can add versioning support to
+    # collect2, so additional links can be useful in future.
+    aix,yes) # traditional libtool
+      dynamic_linker='AIX unversionable lib.so'
+      # If using run time linking (on AIX 4.2 or later) use lib<name>.so
+      # instead of lib<name>.a to let people know that these are not
+      # typical AIX shared libraries.
+      library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+      ;;
+    aix,no) # traditional AIX only
+      dynamic_linker='AIX lib.a[(]lib.so.V[)]'
+      # We preserve .a as extension for shared libraries through AIX4.2
+      # and later when we are not doing run time linking.
+      library_names_spec='$libname$release.a $libname.a'
+      soname_spec='$libname$release$shared_ext$major'
+      ;;
+    svr4,*) # full svr4 only
+      dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)]"
+      library_names_spec='$libname$release$shared_ext$major $libname$shared_ext'
+      # We do not specify a path in Import Files, so LIBPATH fires.
+      shlibpath_overrides_runpath=yes
+      ;;
+    *,yes) # both, prefer svr4
+      dynamic_linker="AIX lib.so.V[(]$shared_archive_member_spec.o[)], lib.a[(]lib.so.V[)]"
+      library_names_spec='$libname$release$shared_ext$major $libname$shared_ext'
+      # unpreferred sharedlib libNAME.a needs extra handling
+      postinstall_cmds='test -n "$linkname" || linkname="$realname"~func_stripname "" ".so" "$linkname"~$install_shared_prog "$dir/$func_stripname_result.$libext" "$destdir/$func_stripname_result.$libext"~test -z "$tstripme" || test -z "$striplib" || $striplib "$destdir/$func_stripname_result.$libext"'
+      postuninstall_cmds='for n in $library_names $old_library; do :; done~func_stripname "" ".so" "$n"~test "$func_stripname_result" = "$n" || func_append rmfiles " $odir/$func_stripname_result.$libext"'
+      # We do not specify a path in Import Files, so LIBPATH fires.
+      shlibpath_overrides_runpath=yes
+      ;;
+    *,no) # both, prefer aix
+      dynamic_linker="AIX lib.a[(]lib.so.V[)], lib.so.V[(]$shared_archive_member_spec.o[)]"
+      library_names_spec='$libname$release.a $libname.a'
+      soname_spec='$libname$release$shared_ext$major'
+      # unpreferred sharedlib libNAME.so.V and symlink libNAME.so need extra handling
+      postinstall_cmds='test -z "$dlname" || $install_shared_prog $dir/$dlname $destdir/$dlname~test -z "$tstripme" || test -z "$striplib" || $striplib $destdir/$dlname~test -n "$linkname" || linkname=$realname~func_stripname "" ".a" "$linkname"~(cd "$destdir" && $LN_S -f $dlname $func_stripname_result.so)'
+      postuninstall_cmds='test -z "$dlname" || func_append rmfiles " $odir/$dlname"~for n in $old_library $library_names; do :; done~func_stripname "" ".a" "$n"~func_append rmfiles " $odir/$func_stripname_result.so"'
+      ;;
+    esac
+    shlibpath_var=LIBPATH
+  fi
+  ;;
+
+amigaos*)
+  case $host_cpu in
+  powerpc)
+    # Since July 2007 AmigaOS4 officially supports .so libraries.
+    # When compiling the executable, add -use-dynld -Lsobjs: to the compileline.
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+    ;;
+  m68k)
+    library_names_spec='$libname.ixlibrary $libname.a'
+    # Create ${libname}_ixlibrary.a entries in /sys/libs.
+    finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`func_echo_all "$lib" | $SED '\''s%^.*/\([[^/]]*\)\.ixlibrary$%\1%'\''`; $RM /sys/libs/${libname}_ixlibrary.a; $show "cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a"; cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a || exit 1; done'
+    ;;
+  esac
+  ;;
+
+beos*)
+  library_names_spec='$libname$shared_ext'
+  dynamic_linker="$host_os ld.so"
+  shlibpath_var=LIBRARY_PATH
+  ;;
+
+bsdi[[45]]*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  sys_lib_search_path_spec="/shlib /usr/lib /usr/X11/lib /usr/contrib/lib /lib /usr/local/lib"
+  sys_lib_dlsearch_path_spec="/shlib /usr/lib /usr/local/lib"
+  # the default ld.so.conf also contains /usr/contrib/lib and
+  # /usr/X11R6/lib (/usr/X11 is a link to /usr/X11R6), but let us allow
+  # libtool to hard-code these into programs
+  ;;
+
+cygwin* | mingw* | pw32* | cegcc*)
+  version_type=windows
+  shrext_cmds=.dll
+  need_version=no
+  need_lib_prefix=no
+
+  case $GCC,$cc_basename in
+  yes,*)
+    # gcc
+    library_names_spec='$libname.dll.a'
+    # DLL is installed to $(libdir)/../bin by postinstall_cmds
+    postinstall_cmds='base_file=`basename \$file`~
+      dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~
+      dldir=$destdir/`dirname \$dlpath`~
+      test -d \$dldir || mkdir -p \$dldir~
+      $install_prog $dir/$dlname \$dldir/$dlname~
+      chmod a+x \$dldir/$dlname~
+      if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then
+        eval '\''$striplib \$dldir/$dlname'\'' || exit \$?;
+      fi'
+    postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~
+      dlpath=$dir/\$dldll~
+       $RM \$dlpath'
+    shlibpath_overrides_runpath=yes
+
+    case $host_os in
+    cygwin*)
+      # Cygwin DLLs use 'cyg' prefix rather than 'lib'
+      soname_spec='`echo $libname | sed -e 's/^lib/cyg/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext'
+m4_if([$1], [],[
+      sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/lib/w32api"])
+      ;;
+    mingw* | cegcc*)
+      # MinGW DLLs use traditional 'lib' prefix
+      soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext'
+      ;;
+    pw32*)
+      # pw32 DLLs use 'pw' prefix rather than 'lib'
+      library_names_spec='`echo $libname | sed -e 's/^lib/pw/'``echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext'
+      ;;
+    esac
+    dynamic_linker='Win32 ld.exe'
+    ;;
+
+  *,cl*)
+    # Native MSVC
+    libname_spec='$name'
+    soname_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext'
+    library_names_spec='$libname.dll.lib'
+
+    case $build_os in
+    mingw*)
+      sys_lib_search_path_spec=
+      lt_save_ifs=$IFS
+      IFS=';'
+      for lt_path in $LIB
+      do
+        IFS=$lt_save_ifs
+        # Let DOS variable expansion print the short 8.3 style file name.
+        lt_path=`cd "$lt_path" 2>/dev/null && cmd //C "for %i in (".") do @echo %~si"`
+        sys_lib_search_path_spec="$sys_lib_search_path_spec $lt_path"
+      done
+      IFS=$lt_save_ifs
+      # Convert to MSYS style.
+      sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | sed -e 's|\\\\|/|g' -e 's| \\([[a-zA-Z]]\\):| /\\1|g' -e 's|^ ||'`
+      ;;
+    cygwin*)
+      # Convert to unix form, then to dos form, then back to unix form
+      # but this time dos style (no spaces!) so that the unix form looks
+      # like /cygdrive/c/PROGRA~1:/cygdr...
+      sys_lib_search_path_spec=`cygpath --path --unix "$LIB"`
+      sys_lib_search_path_spec=`cygpath --path --dos "$sys_lib_search_path_spec" 2>/dev/null`
+      sys_lib_search_path_spec=`cygpath --path --unix "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"`
+      ;;
+    *)
+      sys_lib_search_path_spec=$LIB
+      if $ECHO "$sys_lib_search_path_spec" | [$GREP ';[c-zC-Z]:/' >/dev/null]; then
+        # It is most probably a Windows format PATH.
+        sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e 's/;/ /g'`
+      else
+        sys_lib_search_path_spec=`$ECHO "$sys_lib_search_path_spec" | $SED -e "s/$PATH_SEPARATOR/ /g"`
+      fi
+      # FIXME: find the short name or the path components, as spaces are
+      # common. (e.g. "Program Files" -> "PROGRA~1")
+      ;;
+    esac
+
+    # DLL is installed to $(libdir)/../bin by postinstall_cmds
+    postinstall_cmds='base_file=`basename \$file`~
+      dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; echo \$dlname'\''`~
+      dldir=$destdir/`dirname \$dlpath`~
+      test -d \$dldir || mkdir -p \$dldir~
+      $install_prog $dir/$dlname \$dldir/$dlname'
+    postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~
+      dlpath=$dir/\$dldll~
+       $RM \$dlpath'
+    shlibpath_overrides_runpath=yes
+    dynamic_linker='Win32 link.exe'
+    ;;
+
+  *)
+    # Assume MSVC wrapper
+    library_names_spec='$libname`echo $release | $SED -e 's/[[.]]/-/g'`$versuffix$shared_ext $libname.lib'
+    dynamic_linker='Win32 ld.exe'
+    ;;
+  esac
+  # FIXME: first we should search . and the directory the executable is in
+  shlibpath_var=PATH
+  ;;
+
+darwin* | rhapsody*)
+  dynamic_linker="$host_os dyld"
+  version_type=darwin
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$major$shared_ext $libname$shared_ext'
+  soname_spec='$libname$release$major$shared_ext'
+  shlibpath_overrides_runpath=yes
+  shlibpath_var=DYLD_LIBRARY_PATH
+  shrext_cmds='`test .$module = .yes && echo .so || echo .dylib`'
+m4_if([$1], [],[
+  sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/local/lib"])
+  sys_lib_dlsearch_path_spec='/usr/local/lib /lib /usr/lib'
+  ;;
+
+dgux*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  ;;
+
+freebsd* | dragonfly*)
+  # DragonFly does not have aout.  When/if they implement a new
+  # versioning mechanism, adjust this.
+  if test -x /usr/bin/objformat; then
+    objformat=`/usr/bin/objformat`
+  else
+    case $host_os in
+    freebsd[[23]].*) objformat=aout ;;
+    *) objformat=elf ;;
+    esac
+  fi
+  version_type=freebsd-$objformat
+  case $version_type in
+    freebsd-elf*)
+      library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+      soname_spec='$libname$release$shared_ext$major'
+      need_version=no
+      need_lib_prefix=no
+      ;;
+    freebsd-*)
+      library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix'
+      need_version=yes
+      ;;
+  esac
+  shlibpath_var=LD_LIBRARY_PATH
+  case $host_os in
+  freebsd2.*)
+    shlibpath_overrides_runpath=yes
+    ;;
+  freebsd3.[[01]]* | freebsdelf3.[[01]]*)
+    shlibpath_overrides_runpath=yes
+    hardcode_into_libs=yes
+    ;;
+  freebsd3.[[2-9]]* | freebsdelf3.[[2-9]]* | \
+  freebsd4.[[0-5]] | freebsdelf4.[[0-5]] | freebsd4.1.1 | freebsdelf4.1.1)
+    shlibpath_overrides_runpath=no
+    hardcode_into_libs=yes
+    ;;
+  *) # from 4.6 on, and DragonFly
+    shlibpath_overrides_runpath=yes
+    hardcode_into_libs=yes
+    ;;
+  esac
+  ;;
+
+haiku*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  dynamic_linker="$host_os runtime_loader"
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  sys_lib_dlsearch_path_spec='/boot/home/config/lib /boot/common/lib /boot/system/lib'
+  hardcode_into_libs=yes
+  ;;
+
+hpux9* | hpux10* | hpux11*)
+  # Give a soname corresponding to the major version so that dld.sl refuses to
+  # link against other versions.
+  version_type=sunos
+  need_lib_prefix=no
+  need_version=no
+  case $host_cpu in
+  ia64*)
+    shrext_cmds='.so'
+    hardcode_into_libs=yes
+    dynamic_linker="$host_os dld.so"
+    shlibpath_var=LD_LIBRARY_PATH
+    shlibpath_overrides_runpath=yes # Unless +noenvvar is specified.
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+    soname_spec='$libname$release$shared_ext$major'
+    if test 32 = "$HPUX_IA64_MODE"; then
+      sys_lib_search_path_spec="/usr/lib/hpux32 /usr/local/lib/hpux32 /usr/local/lib"
+      sys_lib_dlsearch_path_spec=/usr/lib/hpux32
+    else
+      sys_lib_search_path_spec="/usr/lib/hpux64 /usr/local/lib/hpux64"
+      sys_lib_dlsearch_path_spec=/usr/lib/hpux64
+    fi
+    ;;
+  hppa*64*)
+    shrext_cmds='.sl'
+    hardcode_into_libs=yes
+    dynamic_linker="$host_os dld.sl"
+    shlibpath_var=LD_LIBRARY_PATH # How should we handle SHLIB_PATH
+    shlibpath_overrides_runpath=yes # Unless +noenvvar is specified.
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+    soname_spec='$libname$release$shared_ext$major'
+    sys_lib_search_path_spec="/usr/lib/pa20_64 /usr/ccs/lib/pa20_64"
+    sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec
+    ;;
+  *)
+    shrext_cmds='.sl'
+    dynamic_linker="$host_os dld.sl"
+    shlibpath_var=SHLIB_PATH
+    shlibpath_overrides_runpath=no # +s is required to enable SHLIB_PATH
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+    soname_spec='$libname$release$shared_ext$major'
+    ;;
+  esac
+  # HP-UX runs *really* slowly unless shared libraries are mode 555, ...
+  postinstall_cmds='chmod 555 $lib'
+  # or fails outright, so override atomically:
+  install_override_mode=555
+  ;;
+
+interix[[3-9]]*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  dynamic_linker='Interix 3.x ld.so.1 (PE, like ELF)'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  hardcode_into_libs=yes
+  ;;
+
+irix5* | irix6* | nonstopux*)
+  case $host_os in
+    nonstopux*) version_type=nonstopux ;;
+    *)
+	if test yes = "$lt_cv_prog_gnu_ld"; then
+		version_type=linux # correct to gnu/linux during the next big refactor
+	else
+		version_type=irix
+	fi ;;
+  esac
+  need_lib_prefix=no
+  need_version=no
+  soname_spec='$libname$release$shared_ext$major'
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$release$shared_ext $libname$shared_ext'
+  case $host_os in
+  irix5* | nonstopux*)
+    libsuff= shlibsuff=
+    ;;
+  *)
+    case $LD in # libtool.m4 will add one of these switches to LD
+    *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ")
+      libsuff= shlibsuff= libmagic=32-bit;;
+    *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ")
+      libsuff=32 shlibsuff=N32 libmagic=N32;;
+    *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ")
+      libsuff=64 shlibsuff=64 libmagic=64-bit;;
+    *) libsuff= shlibsuff= libmagic=never-match;;
+    esac
+    ;;
+  esac
+  shlibpath_var=LD_LIBRARY${shlibsuff}_PATH
+  shlibpath_overrides_runpath=no
+  sys_lib_search_path_spec="/usr/lib$libsuff /lib$libsuff /usr/local/lib$libsuff"
+  sys_lib_dlsearch_path_spec="/usr/lib$libsuff /lib$libsuff"
+  hardcode_into_libs=yes
+  ;;
+
+# No shared lib support for Linux oldld, aout, or coff.
+linux*oldld* | linux*aout* | linux*coff*)
+  dynamic_linker=no
+  ;;
+
+linux*android*)
+  version_type=none # Android doesn't support versioned libraries.
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext'
+  soname_spec='$libname$release$shared_ext'
+  finish_cmds=
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+
+  # This implies no fast_install, which is unacceptable.
+  # Some rework will be needed to allow for fast_install
+  # before this can be enabled.
+  hardcode_into_libs=yes
+
+  dynamic_linker='Android linker'
+  # Don't embed -rpath directories since the linker doesn't support them.
+  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+  ;;
+
+# This must be glibc/ELF.
+linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig -n $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+
+  # Some binutils ld are patched to set DT_RUNPATH
+  AC_CACHE_VAL([lt_cv_shlibpath_overrides_runpath],
+    [lt_cv_shlibpath_overrides_runpath=no
+    save_LDFLAGS=$LDFLAGS
+    save_libdir=$libdir
+    eval "libdir=/foo; wl=\"$_LT_TAGVAR(lt_prog_compiler_wl, $1)\"; \
+	 LDFLAGS=\"\$LDFLAGS $_LT_TAGVAR(hardcode_libdir_flag_spec, $1)\""
+    AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
+      [AS_IF([ ($OBJDUMP -p conftest$ac_exeext) 2>/dev/null | grep "RUNPATH.*$libdir" >/dev/null],
+	 [lt_cv_shlibpath_overrides_runpath=yes])])
+    LDFLAGS=$save_LDFLAGS
+    libdir=$save_libdir
+    ])
+  shlibpath_overrides_runpath=$lt_cv_shlibpath_overrides_runpath
+
+  # This implies no fast_install, which is unacceptable.
+  # Some rework will be needed to allow for fast_install
+  # before this can be enabled.
+  hardcode_into_libs=yes
+
+  # Ideally, we could use ldconfig to report *all* directores which are
+  # searched for libraries, however this is still not possible.  Aside from not
+  # being certain /sbin/ldconfig is available, command
+  # 'ldconfig -N -X -v | grep ^/' on 64bit Fedora does not report /usr/lib64,
+  # even though it is searched at run-time.  Try to do the best guess by
+  # appending ld.so.conf contents (and includes) to the search path.
+  if test -f /etc/ld.so.conf; then
+    lt_ld_extra=`awk '/^include / { system(sprintf("cd /etc; cat %s 2>/dev/null", \[$]2)); skip = 1; } { if (!skip) print \[$]0; skip = 0; }' < /etc/ld.so.conf | $SED -e 's/#.*//;/^[	 ]*hwcap[	 ]/d;s/[:,	]/ /g;s/=[^=]*$//;s/=[^= ]* / /g;s/"//g;/^$/d' | tr '\n' ' '`
+    sys_lib_dlsearch_path_spec="/lib /usr/lib $lt_ld_extra"
+  fi
+
+  # We used to test for /lib/ld.so.1 and disable shared libraries on
+  # powerpc, because MkLinux only supported shared libraries with the
+  # GNU dynamic linker.  Since this was broken with cross compilers,
+  # most powerpc-linux boxes support dynamic linking these days and
+  # people can always --disable-shared, the test was removed, and we
+  # assume the GNU/Linux dynamic linker is in use.
+  dynamic_linker='GNU/Linux ld.so'
+  ;;
+
+netbsdelf*-gnu)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major ${libname}${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  hardcode_into_libs=yes
+  dynamic_linker='NetBSD ld.elf_so'
+  ;;
+
+netbsd*)
+  version_type=sunos
+  need_lib_prefix=no
+  need_version=no
+  if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix'
+    finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir'
+    dynamic_linker='NetBSD (a.out) ld.so'
+  else
+    library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+    soname_spec='$libname$release$shared_ext$major'
+    dynamic_linker='NetBSD ld.elf_so'
+  fi
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  hardcode_into_libs=yes
+  ;;
+
+newsos6)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  ;;
+
+*nto* | *qnx*)
+  version_type=qnx
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  hardcode_into_libs=yes
+  dynamic_linker='ldqnx.so'
+  ;;
+
+openbsd* | bitrig*)
+  version_type=sunos
+  sys_lib_dlsearch_path_spec=/usr/lib
+  need_lib_prefix=no
+  if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then
+    need_version=no
+  else
+    need_version=yes
+  fi
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  ;;
+
+os2*)
+  libname_spec='$name'
+  version_type=windows
+  shrext_cmds=.dll
+  need_version=no
+  need_lib_prefix=no
+  # OS/2 can only load a DLL with a base name of 8 characters or less.
+  soname_spec='`test -n "$os2dllname" && libname="$os2dllname";
+    v=$($ECHO $release$versuffix | tr -d .-);
+    n=$($ECHO $libname | cut -b -$((8 - ${#v})) | tr . _);
+    $ECHO $n$v`$shared_ext'
+  library_names_spec='${libname}_dll.$libext'
+  dynamic_linker='OS/2 ld.exe'
+  shlibpath_var=BEGINLIBPATH
+  sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib"
+  sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec
+  postinstall_cmds='base_file=`basename \$file`~
+    dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\$base_file'\''i; $ECHO \$dlname'\''`~
+    dldir=$destdir/`dirname \$dlpath`~
+    test -d \$dldir || mkdir -p \$dldir~
+    $install_prog $dir/$dlname \$dldir/$dlname~
+    chmod a+x \$dldir/$dlname~
+    if test -n '\''$stripme'\'' && test -n '\''$striplib'\''; then
+      eval '\''$striplib \$dldir/$dlname'\'' || exit \$?;
+    fi'
+  postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; $ECHO \$dlname'\''`~
+    dlpath=$dir/\$dldll~
+    $RM \$dlpath'
+  ;;
+
+osf3* | osf4* | osf5*)
+  version_type=osf
+  need_lib_prefix=no
+  need_version=no
+  soname_spec='$libname$release$shared_ext$major'
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  shlibpath_var=LD_LIBRARY_PATH
+  sys_lib_search_path_spec="/usr/shlib /usr/ccs/lib /usr/lib/cmplrs/cc /usr/lib /usr/local/lib /var/shlib"
+  sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec
+  ;;
+
+rdos*)
+  dynamic_linker=no
+  ;;
+
+solaris*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  hardcode_into_libs=yes
+  # ldd complains unless libraries are executable
+  postinstall_cmds='chmod +x $lib'
+  ;;
+
+sunos4*)
+  version_type=sunos
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$shared_ext$versuffix'
+  finish_cmds='PATH="\$PATH:/usr/etc" ldconfig $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  if test yes = "$with_gnu_ld"; then
+    need_lib_prefix=no
+  fi
+  need_version=yes
+  ;;
+
+sysv4 | sysv4.3*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  case $host_vendor in
+    sni)
+      shlibpath_overrides_runpath=no
+      need_lib_prefix=no
+      runpath_var=LD_RUN_PATH
+      ;;
+    siemens)
+      need_lib_prefix=no
+      ;;
+    motorola)
+      need_lib_prefix=no
+      need_version=no
+      shlibpath_overrides_runpath=no
+      sys_lib_search_path_spec='/lib /usr/lib /usr/ccs/lib'
+      ;;
+  esac
+  ;;
+
+sysv4*MP*)
+  if test -d /usr/nec; then
+    version_type=linux # correct to gnu/linux during the next big refactor
+    library_names_spec='$libname$shared_ext.$versuffix $libname$shared_ext.$major $libname$shared_ext'
+    soname_spec='$libname$shared_ext.$major'
+    shlibpath_var=LD_LIBRARY_PATH
+  fi
+  ;;
+
+sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*)
+  version_type=sco
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  hardcode_into_libs=yes
+  if test yes = "$with_gnu_ld"; then
+    sys_lib_search_path_spec='/usr/local/lib /usr/gnu/lib /usr/ccs/lib /usr/lib /lib'
+  else
+    sys_lib_search_path_spec='/usr/ccs/lib /usr/lib'
+    case $host_os in
+      sco3.2v5*)
+        sys_lib_search_path_spec="$sys_lib_search_path_spec /lib"
+	;;
+    esac
+  fi
+  sys_lib_dlsearch_path_spec='/usr/lib'
+  ;;
+
+tpf*)
+  # TPF is a cross-target only.  Preferred cross-host = GNU/Linux.
+  version_type=linux # correct to gnu/linux during the next big refactor
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  hardcode_into_libs=yes
+  ;;
+
+uts4*)
+  version_type=linux # correct to gnu/linux during the next big refactor
+  library_names_spec='$libname$release$shared_ext$versuffix $libname$release$shared_ext$major $libname$shared_ext'
+  soname_spec='$libname$release$shared_ext$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  ;;
+
+*)
+  dynamic_linker=no
+  ;;
+esac
+AC_MSG_RESULT([$dynamic_linker])
+test no = "$dynamic_linker" && can_build_shared=no
+
+variables_saved_for_relink="PATH $shlibpath_var $runpath_var"
+if test yes = "$GCC"; then
+  variables_saved_for_relink="$variables_saved_for_relink GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH"
+fi
+
+if test set = "${lt_cv_sys_lib_search_path_spec+set}"; then
+  sys_lib_search_path_spec=$lt_cv_sys_lib_search_path_spec
+fi
+
+if test set = "${lt_cv_sys_lib_dlsearch_path_spec+set}"; then
+  sys_lib_dlsearch_path_spec=$lt_cv_sys_lib_dlsearch_path_spec
+fi
+
+# remember unaugmented sys_lib_dlsearch_path content for libtool script decls...
+configure_time_dlsearch_path=$sys_lib_dlsearch_path_spec
+
+# ... but it needs LT_SYS_LIBRARY_PATH munging for other configure-time code
+func_munge_path_list sys_lib_dlsearch_path_spec "$LT_SYS_LIBRARY_PATH"
+
+# to be used as default LT_SYS_LIBRARY_PATH value in generated libtool
+configure_time_lt_sys_library_path=$LT_SYS_LIBRARY_PATH
+
+_LT_DECL([], [variables_saved_for_relink], [1],
+    [Variables whose values should be saved in libtool wrapper scripts and
+    restored at link time])
+_LT_DECL([], [need_lib_prefix], [0],
+    [Do we need the "lib" prefix for modules?])
+_LT_DECL([], [need_version], [0], [Do we need a version for libraries?])
+_LT_DECL([], [version_type], [0], [Library versioning type])
+_LT_DECL([], [runpath_var], [0],  [Shared library runtime path variable])
+_LT_DECL([], [shlibpath_var], [0],[Shared library path variable])
+_LT_DECL([], [shlibpath_overrides_runpath], [0],
+    [Is shlibpath searched before the hard-coded library search path?])
+_LT_DECL([], [libname_spec], [1], [Format of library name prefix])
+_LT_DECL([], [library_names_spec], [1],
+    [[List of archive names.  First name is the real one, the rest are links.
+    The last name is the one that the linker finds with -lNAME]])
+_LT_DECL([], [soname_spec], [1],
+    [[The coded name of the library, if different from the real name]])
+_LT_DECL([], [install_override_mode], [1],
+    [Permission mode override for installation of shared libraries])
+_LT_DECL([], [postinstall_cmds], [2],
+    [Command to use after installation of a shared archive])
+_LT_DECL([], [postuninstall_cmds], [2],
+    [Command to use after uninstallation of a shared archive])
+_LT_DECL([], [finish_cmds], [2],
+    [Commands used to finish a libtool library installation in a directory])
+_LT_DECL([], [finish_eval], [1],
+    [[As "finish_cmds", except a single script fragment to be evaled but
+    not shown]])
+_LT_DECL([], [hardcode_into_libs], [0],
+    [Whether we should hardcode library paths into libraries])
+_LT_DECL([], [sys_lib_search_path_spec], [2],
+    [Compile-time system search path for libraries])
+_LT_DECL([sys_lib_dlsearch_path_spec], [configure_time_dlsearch_path], [2],
+    [Detected run-time system search path for libraries])
+_LT_DECL([], [configure_time_lt_sys_library_path], [2],
+    [Explicit LT_SYS_LIBRARY_PATH set during ./configure time])
+])# _LT_SYS_DYNAMIC_LINKER
+
+
+# _LT_PATH_TOOL_PREFIX(TOOL)
+# --------------------------
+# find a file program that can recognize shared library
+AC_DEFUN([_LT_PATH_TOOL_PREFIX],
+[m4_require([_LT_DECL_EGREP])dnl
+AC_MSG_CHECKING([for $1])
+AC_CACHE_VAL(lt_cv_path_MAGIC_CMD,
+[case $MAGIC_CMD in
+[[\\/*] |  ?:[\\/]*])
+  lt_cv_path_MAGIC_CMD=$MAGIC_CMD # Let the user override the test with a path.
+  ;;
+*)
+  lt_save_MAGIC_CMD=$MAGIC_CMD
+  lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR
+dnl $ac_dummy forces splitting on constant user-supplied paths.
+dnl POSIX.2 word splitting is done only on the output of word expansions,
+dnl not every word.  This closes a longstanding sh security hole.
+  ac_dummy="m4_if([$2], , $PATH, [$2])"
+  for ac_dir in $ac_dummy; do
+    IFS=$lt_save_ifs
+    test -z "$ac_dir" && ac_dir=.
+    if test -f "$ac_dir/$1"; then
+      lt_cv_path_MAGIC_CMD=$ac_dir/"$1"
+      if test -n "$file_magic_test_file"; then
+	case $deplibs_check_method in
+	"file_magic "*)
+	  file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"`
+	  MAGIC_CMD=$lt_cv_path_MAGIC_CMD
+	  if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null |
+	    $EGREP "$file_magic_regex" > /dev/null; then
+	    :
+	  else
+	    cat <<_LT_EOF 1>&2
+
+*** Warning: the command libtool uses to detect shared libraries,
+*** $file_magic_cmd, produces output that libtool cannot recognize.
+*** The result is that libtool may fail to recognize shared libraries
+*** as such.  This will affect the creation of libtool libraries that
+*** depend on shared libraries, but programs linked with such libtool
+*** libraries will work regardless of this problem.  Nevertheless, you
+*** may want to report the problem to your system manager and/or to
+*** bug-libtool@gnu.org
+
+_LT_EOF
+	  fi ;;
+	esac
+      fi
+      break
+    fi
+  done
+  IFS=$lt_save_ifs
+  MAGIC_CMD=$lt_save_MAGIC_CMD
+  ;;
+esac])
+MAGIC_CMD=$lt_cv_path_MAGIC_CMD
+if test -n "$MAGIC_CMD"; then
+  AC_MSG_RESULT($MAGIC_CMD)
+else
+  AC_MSG_RESULT(no)
+fi
+_LT_DECL([], [MAGIC_CMD], [0],
+	 [Used to examine libraries when file_magic_cmd begins with "file"])dnl
+])# _LT_PATH_TOOL_PREFIX
+
+# Old name:
+AU_ALIAS([AC_PATH_TOOL_PREFIX], [_LT_PATH_TOOL_PREFIX])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_PATH_TOOL_PREFIX], [])
+
+
+# _LT_PATH_MAGIC
+# --------------
+# find a file program that can recognize a shared library
+m4_defun([_LT_PATH_MAGIC],
+[_LT_PATH_TOOL_PREFIX(${ac_tool_prefix}file, /usr/bin$PATH_SEPARATOR$PATH)
+if test -z "$lt_cv_path_MAGIC_CMD"; then
+  if test -n "$ac_tool_prefix"; then
+    _LT_PATH_TOOL_PREFIX(file, /usr/bin$PATH_SEPARATOR$PATH)
+  else
+    MAGIC_CMD=:
+  fi
+fi
+])# _LT_PATH_MAGIC
+
+
+# LT_PATH_LD
+# ----------
+# find the pathname to the GNU or non-GNU linker
+AC_DEFUN([LT_PATH_LD],
+[AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_CANONICAL_BUILD])dnl
+m4_require([_LT_DECL_SED])dnl
+m4_require([_LT_DECL_EGREP])dnl
+m4_require([_LT_PROG_ECHO_BACKSLASH])dnl
+
+AC_ARG_WITH([gnu-ld],
+    [AS_HELP_STRING([--with-gnu-ld],
+	[assume the C compiler uses GNU ld @<:@default=no@:>@])],
+    [test no = "$withval" || with_gnu_ld=yes],
+    [with_gnu_ld=no])dnl
+
+ac_prog=ld
+if test yes = "$GCC"; then
+  # Check if gcc -print-prog-name=ld gives a path.
+  AC_MSG_CHECKING([for ld used by $CC])
+  case $host in
+  *-*-mingw*)
+    # gcc leaves a trailing carriage return, which upsets mingw
+    ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;;
+  *)
+    ac_prog=`($CC -print-prog-name=ld) 2>&5` ;;
+  esac
+  case $ac_prog in
+    # Accept absolute paths.
+    [[\\/]]* | ?:[[\\/]]*)
+      re_direlt='/[[^/]][[^/]]*/\.\./'
+      # Canonicalize the pathname of ld
+      ac_prog=`$ECHO "$ac_prog"| $SED 's%\\\\%/%g'`
+      while $ECHO "$ac_prog" | $GREP "$re_direlt" > /dev/null 2>&1; do
+	ac_prog=`$ECHO $ac_prog| $SED "s%$re_direlt%/%"`
+      done
+      test -z "$LD" && LD=$ac_prog
+      ;;
+  "")
+    # If it fails, then pretend we aren't using GCC.
+    ac_prog=ld
+    ;;
+  *)
+    # If it is relative, then search for the first ld in PATH.
+    with_gnu_ld=unknown
+    ;;
+  esac
+elif test yes = "$with_gnu_ld"; then
+  AC_MSG_CHECKING([for GNU ld])
+else
+  AC_MSG_CHECKING([for non-GNU ld])
+fi
+AC_CACHE_VAL(lt_cv_path_LD,
+[if test -z "$LD"; then
+  lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR
+  for ac_dir in $PATH; do
+    IFS=$lt_save_ifs
+    test -z "$ac_dir" && ac_dir=.
+    if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then
+      lt_cv_path_LD=$ac_dir/$ac_prog
+      # Check to see if the program is GNU ld.  I'd rather use --version,
+      # but apparently some variants of GNU ld only accept -v.
+      # Break only if it was the GNU/non-GNU ld that we prefer.
+      case `"$lt_cv_path_LD" -v 2>&1 </dev/null` in
+      *GNU* | *'with BFD'*)
+	test no != "$with_gnu_ld" && break
+	;;
+      *)
+	test yes != "$with_gnu_ld" && break
+	;;
+      esac
+    fi
+  done
+  IFS=$lt_save_ifs
+else
+  lt_cv_path_LD=$LD # Let the user override the test with a path.
+fi])
+LD=$lt_cv_path_LD
+if test -n "$LD"; then
+  AC_MSG_RESULT($LD)
+else
+  AC_MSG_RESULT(no)
+fi
+test -z "$LD" && AC_MSG_ERROR([no acceptable ld found in \$PATH])
+_LT_PATH_LD_GNU
+AC_SUBST([LD])
+
+_LT_TAGDECL([], [LD], [1], [The linker used to build libraries])
+])# LT_PATH_LD
+
+# Old names:
+AU_ALIAS([AM_PROG_LD], [LT_PATH_LD])
+AU_ALIAS([AC_PROG_LD], [LT_PATH_LD])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AM_PROG_LD], [])
+dnl AC_DEFUN([AC_PROG_LD], [])
+
+
+# _LT_PATH_LD_GNU
+#- --------------
+m4_defun([_LT_PATH_LD_GNU],
+[AC_CACHE_CHECK([if the linker ($LD) is GNU ld], lt_cv_prog_gnu_ld,
+[# I'd rather use --version here, but apparently some GNU lds only accept -v.
+case `$LD -v 2>&1 </dev/null` in
+*GNU* | *'with BFD'*)
+  lt_cv_prog_gnu_ld=yes
+  ;;
+*)
+  lt_cv_prog_gnu_ld=no
+  ;;
+esac])
+with_gnu_ld=$lt_cv_prog_gnu_ld
+])# _LT_PATH_LD_GNU
+
+
+# _LT_CMD_RELOAD
+# --------------
+# find reload flag for linker
+#   -- PORTME Some linkers may need a different reload flag.
+m4_defun([_LT_CMD_RELOAD],
+[AC_CACHE_CHECK([for $LD option to reload object files],
+  lt_cv_ld_reload_flag,
+  [lt_cv_ld_reload_flag='-r'])
+reload_flag=$lt_cv_ld_reload_flag
+case $reload_flag in
+"" | " "*) ;;
+*) reload_flag=" $reload_flag" ;;
+esac
+reload_cmds='$LD$reload_flag -o $output$reload_objs'
+case $host_os in
+  cygwin* | mingw* | pw32* | cegcc*)
+    if test yes != "$GCC"; then
+      reload_cmds=false
+    fi
+    ;;
+  darwin*)
+    if test yes = "$GCC"; then
+      reload_cmds='$LTCC $LTCFLAGS -nostdlib $wl-r -o $output$reload_objs'
+    else
+      reload_cmds='$LD$reload_flag -o $output$reload_objs'
+    fi
+    ;;
+esac
+_LT_TAGDECL([], [reload_flag], [1], [How to create reloadable object files])dnl
+_LT_TAGDECL([], [reload_cmds], [2])dnl
+])# _LT_CMD_RELOAD
+
+
+# _LT_PATH_DD
+# -----------
+# find a working dd
+m4_defun([_LT_PATH_DD],
+[AC_CACHE_CHECK([for a working dd], [ac_cv_path_lt_DD],
+[printf 0123456789abcdef0123456789abcdef >conftest.i
+cat conftest.i conftest.i >conftest2.i
+: ${lt_DD:=$DD}
+AC_PATH_PROGS_FEATURE_CHECK([lt_DD], [dd],
+[if "$ac_path_lt_DD" bs=32 count=1 <conftest2.i >conftest.out 2>/dev/null; then
+  cmp -s conftest.i conftest.out \
+  && ac_cv_path_lt_DD="$ac_path_lt_DD" ac_path_lt_DD_found=:
+fi])
+rm -f conftest.i conftest2.i conftest.out])
+])# _LT_PATH_DD
+
+
+# _LT_CMD_TRUNCATE
+# ----------------
+# find command to truncate a binary pipe
+m4_defun([_LT_CMD_TRUNCATE],
+[m4_require([_LT_PATH_DD])
+AC_CACHE_CHECK([how to truncate binary pipes], [lt_cv_truncate_bin],
+[printf 0123456789abcdef0123456789abcdef >conftest.i
+cat conftest.i conftest.i >conftest2.i
+lt_cv_truncate_bin=
+if "$ac_cv_path_lt_DD" bs=32 count=1 <conftest2.i >conftest.out 2>/dev/null; then
+  cmp -s conftest.i conftest.out \
+  && lt_cv_truncate_bin="$ac_cv_path_lt_DD bs=4096 count=1"
+fi
+rm -f conftest.i conftest2.i conftest.out
+test -z "$lt_cv_truncate_bin" && lt_cv_truncate_bin="$SED -e 4q"])
+_LT_DECL([lt_truncate_bin], [lt_cv_truncate_bin], [1],
+  [Command to truncate a binary pipe])
+])# _LT_CMD_TRUNCATE
+
+
+# _LT_CHECK_MAGIC_METHOD
+# ----------------------
+# how to check for library dependencies
+#  -- PORTME fill in with the dynamic library characteristics
+m4_defun([_LT_CHECK_MAGIC_METHOD],
+[m4_require([_LT_DECL_EGREP])
+m4_require([_LT_DECL_OBJDUMP])
+AC_CACHE_CHECK([how to recognize dependent libraries],
+lt_cv_deplibs_check_method,
+[lt_cv_file_magic_cmd='$MAGIC_CMD'
+lt_cv_file_magic_test_file=
+lt_cv_deplibs_check_method='unknown'
+# Need to set the preceding variable on all platforms that support
+# interlibrary dependencies.
+# 'none' -- dependencies not supported.
+# 'unknown' -- same as none, but documents that we really don't know.
+# 'pass_all' -- all dependencies passed with no checks.
+# 'test_compile' -- check by making test program.
+# 'file_magic [[regex]]' -- check by looking for files in library path
+# that responds to the $file_magic_cmd with a given extended regex.
+# If you have 'file' or equivalent on your system and you're not sure
+# whether 'pass_all' will *always* work, you probably want this one.
+
+case $host_os in
+aix[[4-9]]*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+beos*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+bsdi[[45]]*)
+  lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib)'
+  lt_cv_file_magic_cmd='/usr/bin/file -L'
+  lt_cv_file_magic_test_file=/shlib/libc.so
+  ;;
+
+cygwin*)
+  # func_win32_libid is a shell function defined in ltmain.sh
+  lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL'
+  lt_cv_file_magic_cmd='func_win32_libid'
+  ;;
+
+mingw* | pw32*)
+  # Base MSYS/MinGW do not provide the 'file' command needed by
+  # func_win32_libid shell function, so use a weaker test based on 'objdump',
+  # unless we find 'file', for example because we are cross-compiling.
+  if ( file / ) >/dev/null 2>&1; then
+    lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL'
+    lt_cv_file_magic_cmd='func_win32_libid'
+  else
+    # Keep this pattern in sync with the one in func_win32_libid.
+    lt_cv_deplibs_check_method='file_magic file format (pei*-i386(.*architecture: i386)?|pe-arm-wince|pe-x86-64)'
+    lt_cv_file_magic_cmd='$OBJDUMP -f'
+  fi
+  ;;
+
+cegcc*)
+  # use the weaker test based on 'objdump'. See mingw*.
+  lt_cv_deplibs_check_method='file_magic file format pe-arm-.*little(.*architecture: arm)?'
+  lt_cv_file_magic_cmd='$OBJDUMP -f'
+  ;;
+
+darwin* | rhapsody*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+freebsd* | dragonfly*)
+  if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then
+    case $host_cpu in
+    i*86 )
+      # Not sure whether the presence of OpenBSD here was a mistake.
+      # Let's accept both of them until this is cleared up.
+      lt_cv_deplibs_check_method='file_magic (FreeBSD|OpenBSD|DragonFly)/i[[3-9]]86 (compact )?demand paged shared library'
+      lt_cv_file_magic_cmd=/usr/bin/file
+      lt_cv_file_magic_test_file=`echo /usr/lib/libc.so.*`
+      ;;
+    esac
+  else
+    lt_cv_deplibs_check_method=pass_all
+  fi
+  ;;
+
+haiku*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+hpux10.20* | hpux11*)
+  lt_cv_file_magic_cmd=/usr/bin/file
+  case $host_cpu in
+  ia64*)
+    lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|ELF-[[0-9]][[0-9]]) shared object file - IA64'
+    lt_cv_file_magic_test_file=/usr/lib/hpux32/libc.so
+    ;;
+  hppa*64*)
+    [lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF[ -][0-9][0-9])(-bit)?( [LM]SB)? shared object( file)?[, -]* PA-RISC [0-9]\.[0-9]']
+    lt_cv_file_magic_test_file=/usr/lib/pa20_64/libc.sl
+    ;;
+  *)
+    lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|PA-RISC[[0-9]]\.[[0-9]]) shared library'
+    lt_cv_file_magic_test_file=/usr/lib/libc.sl
+    ;;
+  esac
+  ;;
+
+interix[[3-9]]*)
+  # PIC code is broken on Interix 3.x, that's why |\.a not |_pic\.a here
+  lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|\.a)$'
+  ;;
+
+irix5* | irix6* | nonstopux*)
+  case $LD in
+  *-32|*"-32 ") libmagic=32-bit;;
+  *-n32|*"-n32 ") libmagic=N32;;
+  *-64|*"-64 ") libmagic=64-bit;;
+  *) libmagic=never-match;;
+  esac
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+# This must be glibc/ELF.
+linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+netbsd* | netbsdelf*-gnu)
+  if echo __ELF__ | $CC -E - | $GREP __ELF__ > /dev/null; then
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$'
+  else
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|_pic\.a)$'
+  fi
+  ;;
+
+newos6*)
+  lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (executable|dynamic lib)'
+  lt_cv_file_magic_cmd=/usr/bin/file
+  lt_cv_file_magic_test_file=/usr/lib/libnls.so
+  ;;
+
+*nto* | *qnx*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+openbsd* | bitrig*)
+  if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|\.so|_pic\.a)$'
+  else
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$'
+  fi
+  ;;
+
+osf3* | osf4* | osf5*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+rdos*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+solaris*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+sysv4 | sysv4.3*)
+  case $host_vendor in
+  motorola)
+    lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib) M[[0-9]][[0-9]]* Version [[0-9]]'
+    lt_cv_file_magic_test_file=`echo /usr/lib/libc.so*`
+    ;;
+  ncr)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  sequent)
+    lt_cv_file_magic_cmd='/bin/file'
+    lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB (shared object|dynamic lib )'
+    ;;
+  sni)
+    lt_cv_file_magic_cmd='/bin/file'
+    lt_cv_deplibs_check_method="file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB dynamic lib"
+    lt_cv_file_magic_test_file=/lib/libc.so
+    ;;
+  siemens)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  pc)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  esac
+  ;;
+
+tpf*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+os2*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+esac
+])
+
+file_magic_glob=
+want_nocaseglob=no
+if test "$build" = "$host"; then
+  case $host_os in
+  mingw* | pw32*)
+    if ( shopt | grep nocaseglob ) >/dev/null 2>&1; then
+      want_nocaseglob=yes
+    else
+      file_magic_glob=`echo aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ | $SED -e "s/\(..\)/s\/[[\1]]\/[[\1]]\/g;/g"`
+    fi
+    ;;
+  esac
+fi
+
+file_magic_cmd=$lt_cv_file_magic_cmd
+deplibs_check_method=$lt_cv_deplibs_check_method
+test -z "$deplibs_check_method" && deplibs_check_method=unknown
+
+_LT_DECL([], [deplibs_check_method], [1],
+    [Method to check whether dependent libraries are shared objects])
+_LT_DECL([], [file_magic_cmd], [1],
+    [Command to use when deplibs_check_method = "file_magic"])
+_LT_DECL([], [file_magic_glob], [1],
+    [How to find potential files when deplibs_check_method = "file_magic"])
+_LT_DECL([], [want_nocaseglob], [1],
+    [Find potential files using nocaseglob when deplibs_check_method = "file_magic"])
+])# _LT_CHECK_MAGIC_METHOD
+
+
+# LT_PATH_NM
+# ----------
+# find the pathname to a BSD- or MS-compatible name lister
+AC_DEFUN([LT_PATH_NM],
+[AC_REQUIRE([AC_PROG_CC])dnl
+AC_CACHE_CHECK([for BSD- or MS-compatible name lister (nm)], lt_cv_path_NM,
+[if test -n "$NM"; then
+  # Let the user override the test.
+  lt_cv_path_NM=$NM
+else
+  lt_nm_to_check=${ac_tool_prefix}nm
+  if test -n "$ac_tool_prefix" && test "$build" = "$host"; then
+    lt_nm_to_check="$lt_nm_to_check nm"
+  fi
+  for lt_tmp_nm in $lt_nm_to_check; do
+    lt_save_ifs=$IFS; IFS=$PATH_SEPARATOR
+    for ac_dir in $PATH /usr/ccs/bin/elf /usr/ccs/bin /usr/ucb /bin; do
+      IFS=$lt_save_ifs
+      test -z "$ac_dir" && ac_dir=.
+      tmp_nm=$ac_dir/$lt_tmp_nm
+      if test -f "$tmp_nm" || test -f "$tmp_nm$ac_exeext"; then
+	# Check to see if the nm accepts a BSD-compat flag.
+	# Adding the 'sed 1q' prevents false positives on HP-UX, which says:
+	#   nm: unknown option "B" ignored
+	# Tru64's nm complains that /dev/null is an invalid object file
+	# MSYS converts /dev/null to NUL, MinGW nm treats NUL as empty
+	case $build_os in
+	mingw*) lt_bad_file=conftest.nm/nofile ;;
+	*) lt_bad_file=/dev/null ;;
+	esac
+	case `"$tmp_nm" -B $lt_bad_file 2>&1 | sed '1q'` in
+	*$lt_bad_file* | *'Invalid file or object type'*)
+	  lt_cv_path_NM="$tmp_nm -B"
+	  break 2
+	  ;;
+	*)
+	  case `"$tmp_nm" -p /dev/null 2>&1 | sed '1q'` in
+	  */dev/null*)
+	    lt_cv_path_NM="$tmp_nm -p"
+	    break 2
+	    ;;
+	  *)
+	    lt_cv_path_NM=${lt_cv_path_NM="$tmp_nm"} # keep the first match, but
+	    continue # so that we can try to find one that supports BSD flags
+	    ;;
+	  esac
+	  ;;
+	esac
+      fi
+    done
+    IFS=$lt_save_ifs
+  done
+  : ${lt_cv_path_NM=no}
+fi])
+if test no != "$lt_cv_path_NM"; then
+  NM=$lt_cv_path_NM
+else
+  # Didn't find any BSD compatible name lister, look for dumpbin.
+  if test -n "$DUMPBIN"; then :
+    # Let the user override the test.
+  else
+    AC_CHECK_TOOLS(DUMPBIN, [dumpbin "link -dump"], :)
+    case `$DUMPBIN -symbols -headers /dev/null 2>&1 | sed '1q'` in
+    *COFF*)
+      DUMPBIN="$DUMPBIN -symbols -headers"
+      ;;
+    *)
+      DUMPBIN=:
+      ;;
+    esac
+  fi
+  AC_SUBST([DUMPBIN])
+  if test : != "$DUMPBIN"; then
+    NM=$DUMPBIN
+  fi
+fi
+test -z "$NM" && NM=nm
+AC_SUBST([NM])
+_LT_DECL([], [NM], [1], [A BSD- or MS-compatible name lister])dnl
+
+AC_CACHE_CHECK([the name lister ($NM) interface], [lt_cv_nm_interface],
+  [lt_cv_nm_interface="BSD nm"
+  echo "int some_variable = 0;" > conftest.$ac_ext
+  (eval echo "\"\$as_me:$LINENO: $ac_compile\"" >&AS_MESSAGE_LOG_FD)
+  (eval "$ac_compile" 2>conftest.err)
+  cat conftest.err >&AS_MESSAGE_LOG_FD
+  (eval echo "\"\$as_me:$LINENO: $NM \\\"conftest.$ac_objext\\\"\"" >&AS_MESSAGE_LOG_FD)
+  (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
+  cat conftest.err >&AS_MESSAGE_LOG_FD
+  (eval echo "\"\$as_me:$LINENO: output\"" >&AS_MESSAGE_LOG_FD)
+  cat conftest.out >&AS_MESSAGE_LOG_FD
+  if $GREP 'External.*some_variable' conftest.out > /dev/null; then
+    lt_cv_nm_interface="MS dumpbin"
+  fi
+  rm -f conftest*])
+])# LT_PATH_NM
+
+# Old names:
+AU_ALIAS([AM_PROG_NM], [LT_PATH_NM])
+AU_ALIAS([AC_PROG_NM], [LT_PATH_NM])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AM_PROG_NM], [])
+dnl AC_DEFUN([AC_PROG_NM], [])
+
+# _LT_CHECK_SHAREDLIB_FROM_LINKLIB
+# --------------------------------
+# how to determine the name of the shared library
+# associated with a specific link library.
+#  -- PORTME fill in with the dynamic library characteristics
+m4_defun([_LT_CHECK_SHAREDLIB_FROM_LINKLIB],
+[m4_require([_LT_DECL_EGREP])
+m4_require([_LT_DECL_OBJDUMP])
+m4_require([_LT_DECL_DLLTOOL])
+AC_CACHE_CHECK([how to associate runtime and link libraries],
+lt_cv_sharedlib_from_linklib_cmd,
+[lt_cv_sharedlib_from_linklib_cmd='unknown'
+
+case $host_os in
+cygwin* | mingw* | pw32* | cegcc*)
+  # two different shell functions defined in ltmain.sh;
+  # decide which one to use based on capabilities of $DLLTOOL
+  case `$DLLTOOL --help 2>&1` in
+  *--identify-strict*)
+    lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib
+    ;;
+  *)
+    lt_cv_sharedlib_from_linklib_cmd=func_cygming_dll_for_implib_fallback
+    ;;
+  esac
+  ;;
+*)
+  # fallback: assume linklib IS sharedlib
+  lt_cv_sharedlib_from_linklib_cmd=$ECHO
+  ;;
+esac
+])
+sharedlib_from_linklib_cmd=$lt_cv_sharedlib_from_linklib_cmd
+test -z "$sharedlib_from_linklib_cmd" && sharedlib_from_linklib_cmd=$ECHO
+
+_LT_DECL([], [sharedlib_from_linklib_cmd], [1],
+    [Command to associate shared and link libraries])
+])# _LT_CHECK_SHAREDLIB_FROM_LINKLIB
+
+
+# _LT_PATH_MANIFEST_TOOL
+# ----------------------
+# locate the manifest tool
+m4_defun([_LT_PATH_MANIFEST_TOOL],
+[AC_CHECK_TOOL(MANIFEST_TOOL, mt, :)
+test -z "$MANIFEST_TOOL" && MANIFEST_TOOL=mt
+AC_CACHE_CHECK([if $MANIFEST_TOOL is a manifest tool], [lt_cv_path_mainfest_tool],
+  [lt_cv_path_mainfest_tool=no
+  echo "$as_me:$LINENO: $MANIFEST_TOOL '-?'" >&AS_MESSAGE_LOG_FD
+  $MANIFEST_TOOL '-?' 2>conftest.err > conftest.out
+  cat conftest.err >&AS_MESSAGE_LOG_FD
+  if $GREP 'Manifest Tool' conftest.out > /dev/null; then
+    lt_cv_path_mainfest_tool=yes
+  fi
+  rm -f conftest*])
+if test yes != "$lt_cv_path_mainfest_tool"; then
+  MANIFEST_TOOL=:
+fi
+_LT_DECL([], [MANIFEST_TOOL], [1], [Manifest tool])dnl
+])# _LT_PATH_MANIFEST_TOOL
+
+
+# _LT_DLL_DEF_P([FILE])
+# ---------------------
+# True iff FILE is a Windows DLL '.def' file.
+# Keep in sync with func_dll_def_p in the libtool script
+AC_DEFUN([_LT_DLL_DEF_P],
+[dnl
+  test DEF = "`$SED -n dnl
+    -e '\''s/^[[	 ]]*//'\'' dnl Strip leading whitespace
+    -e '\''/^\(;.*\)*$/d'\'' dnl      Delete empty lines and comments
+    -e '\''s/^\(EXPORTS\|LIBRARY\)\([[	 ]].*\)*$/DEF/p'\'' dnl
+    -e q dnl                          Only consider the first "real" line
+    $1`" dnl
+])# _LT_DLL_DEF_P
+
+
+# LT_LIB_M
+# --------
+# check for math library
+AC_DEFUN([LT_LIB_M],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+LIBM=
+case $host in
+*-*-beos* | *-*-cegcc* | *-*-cygwin* | *-*-haiku* | *-*-pw32* | *-*-darwin*)
+  # These system don't have libm, or don't need it
+  ;;
+*-ncr-sysv4.3*)
+  AC_CHECK_LIB(mw, _mwvalidcheckl, LIBM=-lmw)
+  AC_CHECK_LIB(m, cos, LIBM="$LIBM -lm")
+  ;;
+*)
+  AC_CHECK_LIB(m, cos, LIBM=-lm)
+  ;;
+esac
+AC_SUBST([LIBM])
+])# LT_LIB_M
+
+# Old name:
+AU_ALIAS([AC_CHECK_LIBM], [LT_LIB_M])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_CHECK_LIBM], [])
+
+
+# _LT_COMPILER_NO_RTTI([TAGNAME])
+# -------------------------------
+m4_defun([_LT_COMPILER_NO_RTTI],
+[m4_require([_LT_TAG_COMPILER])dnl
+
+_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=
+
+if test yes = "$GCC"; then
+  case $cc_basename in
+  nvcc*)
+    _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -Xcompiler -fno-builtin' ;;
+  *)
+    _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin' ;;
+  esac
+
+  _LT_COMPILER_OPTION([if $compiler supports -fno-rtti -fno-exceptions],
+    lt_cv_prog_compiler_rtti_exceptions,
+    [-fno-rtti -fno-exceptions], [],
+    [_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)="$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1) -fno-rtti -fno-exceptions"])
+fi
+_LT_TAGDECL([no_builtin_flag], [lt_prog_compiler_no_builtin_flag], [1],
+	[Compiler flag to turn off builtin functions])
+])# _LT_COMPILER_NO_RTTI
+
+
+# _LT_CMD_GLOBAL_SYMBOLS
+# ----------------------
+m4_defun([_LT_CMD_GLOBAL_SYMBOLS],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([AC_PROG_AWK])dnl
+AC_REQUIRE([LT_PATH_NM])dnl
+AC_REQUIRE([LT_PATH_LD])dnl
+m4_require([_LT_DECL_SED])dnl
+m4_require([_LT_DECL_EGREP])dnl
+m4_require([_LT_TAG_COMPILER])dnl
+
+# Check for command to grab the raw symbol name followed by C symbol from nm.
+AC_MSG_CHECKING([command to parse $NM output from $compiler object])
+AC_CACHE_VAL([lt_cv_sys_global_symbol_pipe],
+[
+# These are sane defaults that work on at least a few old systems.
+# [They come from Ultrix.  What could be older than Ultrix?!! ;)]
+
+# Character class describing NM global symbol codes.
+symcode='[[BCDEGRST]]'
+
+# Regexp to match symbols that can be accessed directly from C.
+sympat='\([[_A-Za-z]][[_A-Za-z0-9]]*\)'
+
+# Define system-specific variables.
+case $host_os in
+aix*)
+  symcode='[[BCDT]]'
+  ;;
+cygwin* | mingw* | pw32* | cegcc*)
+  symcode='[[ABCDGISTW]]'
+  ;;
+hpux*)
+  if test ia64 = "$host_cpu"; then
+    symcode='[[ABCDEGRST]]'
+  fi
+  ;;
+irix* | nonstopux*)
+  symcode='[[BCDEGRST]]'
+  ;;
+osf*)
+  symcode='[[BCDEGQRST]]'
+  ;;
+solaris*)
+  symcode='[[BDRT]]'
+  ;;
+sco3.2v5*)
+  symcode='[[DT]]'
+  ;;
+sysv4.2uw2*)
+  symcode='[[DT]]'
+  ;;
+sysv5* | sco5v6* | unixware* | OpenUNIX*)
+  symcode='[[ABDT]]'
+  ;;
+sysv4)
+  symcode='[[DFNSTU]]'
+  ;;
+esac
+
+# If we're using GNU nm, then use its standard symbol codes.
+case `$NM -V 2>&1` in
+*GNU* | *'with BFD'*)
+  symcode='[[ABCDGIRSTW]]' ;;
+esac
+
+if test "$lt_cv_nm_interface" = "MS dumpbin"; then
+  # Gets list of data symbols to import.
+  lt_cv_sys_global_symbol_to_import="sed -n -e 's/^I .* \(.*\)$/\1/p'"
+  # Adjust the below global symbol transforms to fixup imported variables.
+  lt_cdecl_hook=" -e 's/^I .* \(.*\)$/extern __declspec(dllimport) char \1;/p'"
+  lt_c_name_hook=" -e 's/^I .* \(.*\)$/  {\"\1\", (void *) 0},/p'"
+  lt_c_name_lib_hook="\
+  -e 's/^I .* \(lib.*\)$/  {\"\1\", (void *) 0},/p'\
+  -e 's/^I .* \(.*\)$/  {\"lib\1\", (void *) 0},/p'"
+else
+  # Disable hooks by default.
+  lt_cv_sys_global_symbol_to_import=
+  lt_cdecl_hook=
+  lt_c_name_hook=
+  lt_c_name_lib_hook=
+fi
+
+# Transform an extracted symbol line into a proper C declaration.
+# Some systems (esp. on ia64) link data and code symbols differently,
+# so use this general approach.
+lt_cv_sys_global_symbol_to_cdecl="sed -n"\
+$lt_cdecl_hook\
+" -e 's/^T .* \(.*\)$/extern int \1();/p'"\
+" -e 's/^$symcode$symcode* .* \(.*\)$/extern char \1;/p'"
+
+# Transform an extracted symbol line into symbol name and symbol address
+lt_cv_sys_global_symbol_to_c_name_address="sed -n"\
+$lt_c_name_hook\
+" -e 's/^: \(.*\) .*$/  {\"\1\", (void *) 0},/p'"\
+" -e 's/^$symcode$symcode* .* \(.*\)$/  {\"\1\", (void *) \&\1},/p'"
+
+# Transform an extracted symbol line into symbol name with lib prefix and
+# symbol address.
+lt_cv_sys_global_symbol_to_c_name_address_lib_prefix="sed -n"\
+$lt_c_name_lib_hook\
+" -e 's/^: \(.*\) .*$/  {\"\1\", (void *) 0},/p'"\
+" -e 's/^$symcode$symcode* .* \(lib.*\)$/  {\"\1\", (void *) \&\1},/p'"\
+" -e 's/^$symcode$symcode* .* \(.*\)$/  {\"lib\1\", (void *) \&\1},/p'"
+
+# Handle CRLF in mingw tool chain
+opt_cr=
+case $build_os in
+mingw*)
+  opt_cr=`$ECHO 'x\{0,1\}' | tr x '\015'` # option cr in regexp
+  ;;
+esac
+
+# Try without a prefix underscore, then with it.
+for ac_symprfx in "" "_"; do
+
+  # Transform symcode, sympat, and symprfx into a raw symbol and a C symbol.
+  symxfrm="\\1 $ac_symprfx\\2 \\2"
+
+  # Write the raw and C identifiers.
+  if test "$lt_cv_nm_interface" = "MS dumpbin"; then
+    # Fake it for dumpbin and say T for any non-static function,
+    # D for any global variable and I for any imported variable.
+    # Also find C++ and __fastcall symbols from MSVC++,
+    # which start with @ or ?.
+    lt_cv_sys_global_symbol_pipe="$AWK ['"\
+"     {last_section=section; section=\$ 3};"\
+"     /^COFF SYMBOL TABLE/{for(i in hide) delete hide[i]};"\
+"     /Section length .*#relocs.*(pick any)/{hide[last_section]=1};"\
+"     /^ *Symbol name *: /{split(\$ 0,sn,\":\"); si=substr(sn[2],2)};"\
+"     /^ *Type *: code/{print \"T\",si,substr(si,length(prfx))};"\
+"     /^ *Type *: data/{print \"I\",si,substr(si,length(prfx))};"\
+"     \$ 0!~/External *\|/{next};"\
+"     / 0+ UNDEF /{next}; / UNDEF \([^|]\)*()/{next};"\
+"     {if(hide[section]) next};"\
+"     {f=\"D\"}; \$ 0~/\(\).*\|/{f=\"T\"};"\
+"     {split(\$ 0,a,/\||\r/); split(a[2],s)};"\
+"     s[1]~/^[@?]/{print f,s[1],s[1]; next};"\
+"     s[1]~prfx {split(s[1],t,\"@\"); print f,t[1],substr(t[1],length(prfx))}"\
+"     ' prfx=^$ac_symprfx]"
+  else
+    lt_cv_sys_global_symbol_pipe="sed -n -e 's/^.*[[	 ]]\($symcode$symcode*\)[[	 ]][[	 ]]*$ac_symprfx$sympat$opt_cr$/$symxfrm/p'"
+  fi
+  lt_cv_sys_global_symbol_pipe="$lt_cv_sys_global_symbol_pipe | sed '/ __gnu_lto/d'"
+
+  # Check to see that the pipe works correctly.
+  pipe_works=no
+
+  rm -f conftest*
+  cat > conftest.$ac_ext <<_LT_EOF
+#ifdef __cplusplus
+extern "C" {
+#endif
+char nm_test_var;
+void nm_test_func(void);
+void nm_test_func(void){}
+#ifdef __cplusplus
+}
+#endif
+int main(){nm_test_var='a';nm_test_func();return(0);}
+_LT_EOF
+
+  if AC_TRY_EVAL(ac_compile); then
+    # Now try to grab the symbols.
+    nlist=conftest.nm
+    if AC_TRY_EVAL(NM conftest.$ac_objext \| "$lt_cv_sys_global_symbol_pipe" \> $nlist) && test -s "$nlist"; then
+      # Try sorting and uniquifying the output.
+      if sort "$nlist" | uniq > "$nlist"T; then
+	mv -f "$nlist"T "$nlist"
+      else
+	rm -f "$nlist"T
+      fi
+
+      # Make sure that we snagged all the symbols we need.
+      if $GREP ' nm_test_var$' "$nlist" >/dev/null; then
+	if $GREP ' nm_test_func$' "$nlist" >/dev/null; then
+	  cat <<_LT_EOF > conftest.$ac_ext
+/* Keep this code in sync between libtool.m4, ltmain, lt_system.h, and tests.  */
+#if defined _WIN32 || defined __CYGWIN__ || defined _WIN32_WCE
+/* DATA imports from DLLs on WIN32 can't be const, because runtime
+   relocations are performed -- see ld's documentation on pseudo-relocs.  */
+# define LT@&t@_DLSYM_CONST
+#elif defined __osf__
+/* This system does not cope well with relocations in const data.  */
+# define LT@&t@_DLSYM_CONST
+#else
+# define LT@&t@_DLSYM_CONST const
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+_LT_EOF
+	  # Now generate the symbol file.
+	  eval "$lt_cv_sys_global_symbol_to_cdecl"' < "$nlist" | $GREP -v main >> conftest.$ac_ext'
+
+	  cat <<_LT_EOF >> conftest.$ac_ext
+
+/* The mapping between symbol names and symbols.  */
+LT@&t@_DLSYM_CONST struct {
+  const char *name;
+  void       *address;
+}
+lt__PROGRAM__LTX_preloaded_symbols[[]] =
+{
+  { "@PROGRAM@", (void *) 0 },
+_LT_EOF
+	  $SED "s/^$symcode$symcode* .* \(.*\)$/  {\"\1\", (void *) \&\1},/" < "$nlist" | $GREP -v main >> conftest.$ac_ext
+	  cat <<\_LT_EOF >> conftest.$ac_ext
+  {0, (void *) 0}
+};
+
+/* This works around a problem in FreeBSD linker */
+#ifdef FREEBSD_WORKAROUND
+static const void *lt_preloaded_setup() {
+  return lt__PROGRAM__LTX_preloaded_symbols;
+}
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+_LT_EOF
+	  # Now try linking the two files.
+	  mv conftest.$ac_objext conftstm.$ac_objext
+	  lt_globsym_save_LIBS=$LIBS
+	  lt_globsym_save_CFLAGS=$CFLAGS
+	  LIBS=conftstm.$ac_objext
+	  CFLAGS="$CFLAGS$_LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)"
+	  if AC_TRY_EVAL(ac_link) && test -s conftest$ac_exeext; then
+	    pipe_works=yes
+	  fi
+	  LIBS=$lt_globsym_save_LIBS
+	  CFLAGS=$lt_globsym_save_CFLAGS
+	else
+	  echo "cannot find nm_test_func in $nlist" >&AS_MESSAGE_LOG_FD
+	fi
+      else
+	echo "cannot find nm_test_var in $nlist" >&AS_MESSAGE_LOG_FD
+      fi
+    else
+      echo "cannot run $lt_cv_sys_global_symbol_pipe" >&AS_MESSAGE_LOG_FD
+    fi
+  else
+    echo "$progname: failed program was:" >&AS_MESSAGE_LOG_FD
+    cat conftest.$ac_ext >&5
+  fi
+  rm -rf conftest* conftst*
+
+  # Do not use the global_symbol_pipe unless it works.
+  if test yes = "$pipe_works"; then
+    break
+  else
+    lt_cv_sys_global_symbol_pipe=
+  fi
+done
+])
+if test -z "$lt_cv_sys_global_symbol_pipe"; then
+  lt_cv_sys_global_symbol_to_cdecl=
+fi
+if test -z "$lt_cv_sys_global_symbol_pipe$lt_cv_sys_global_symbol_to_cdecl"; then
+  AC_MSG_RESULT(failed)
+else
+  AC_MSG_RESULT(ok)
+fi
+
+# Response file support.
+if test "$lt_cv_nm_interface" = "MS dumpbin"; then
+  nm_file_list_spec='@'
+elif $NM --help 2>/dev/null | grep '[[@]]FILE' >/dev/null; then
+  nm_file_list_spec='@'
+fi
+
+_LT_DECL([global_symbol_pipe], [lt_cv_sys_global_symbol_pipe], [1],
+    [Take the output of nm and produce a listing of raw symbols and C names])
+_LT_DECL([global_symbol_to_cdecl], [lt_cv_sys_global_symbol_to_cdecl], [1],
+    [Transform the output of nm in a proper C declaration])
+_LT_DECL([global_symbol_to_import], [lt_cv_sys_global_symbol_to_import], [1],
+    [Transform the output of nm into a list of symbols to manually relocate])
+_LT_DECL([global_symbol_to_c_name_address],
+    [lt_cv_sys_global_symbol_to_c_name_address], [1],
+    [Transform the output of nm in a C name address pair])
+_LT_DECL([global_symbol_to_c_name_address_lib_prefix],
+    [lt_cv_sys_global_symbol_to_c_name_address_lib_prefix], [1],
+    [Transform the output of nm in a C name address pair when lib prefix is needed])
+_LT_DECL([nm_interface], [lt_cv_nm_interface], [1],
+    [The name lister interface])
+_LT_DECL([], [nm_file_list_spec], [1],
+    [Specify filename containing input files for $NM])
+]) # _LT_CMD_GLOBAL_SYMBOLS
+
+
+# _LT_COMPILER_PIC([TAGNAME])
+# ---------------------------
+m4_defun([_LT_COMPILER_PIC],
+[m4_require([_LT_TAG_COMPILER])dnl
+_LT_TAGVAR(lt_prog_compiler_wl, $1)=
+_LT_TAGVAR(lt_prog_compiler_pic, $1)=
+_LT_TAGVAR(lt_prog_compiler_static, $1)=
+
+m4_if([$1], [CXX], [
+  # C++ specific cases for pic, static, wl, etc.
+  if test yes = "$GXX"; then
+    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+    _LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+
+    case $host_os in
+    aix*)
+      # All AIX code is PIC.
+      if test ia64 = "$host_cpu"; then
+	# AIX 5 now supports IA64 processor
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+
+    amigaos*)
+      case $host_cpu in
+      powerpc)
+            # see comment about AmigaOS4 .so support
+            _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+        ;;
+      m68k)
+            # FIXME: we need at least 68020 code to build shared libraries, but
+            # adding the '-m68020' flag to GCC prevents building anything better,
+            # like '-m68040'.
+            _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4'
+        ;;
+      esac
+      ;;
+
+    beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*)
+      # PIC is the default for these OSes.
+      ;;
+    mingw* | cygwin* | os2* | pw32* | cegcc*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      # Although the cygwin gcc ignores -fPIC, still need this for old-style
+      # (--disable-auto-import) libraries
+      m4_if([$1], [GCJ], [],
+	[_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      case $host_os in
+      os2*)
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static'
+	;;
+      esac
+      ;;
+    darwin* | rhapsody*)
+      # PIC is the default on this platform
+      # Common symbols not allowed in MH_DYLIB files
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common'
+      ;;
+    *djgpp*)
+      # DJGPP does not support shared libraries at all
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)=
+      ;;
+    haiku*)
+      # PIC is the default for Haiku.
+      # The "-static" flag exists, but is broken.
+      _LT_TAGVAR(lt_prog_compiler_static, $1)=
+      ;;
+    interix[[3-9]]*)
+      # Interix 3.x gcc -fpic/-fPIC options generate broken code.
+      # Instead, we relocate shared libraries at runtime.
+      ;;
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic
+      fi
+      ;;
+    hpux*)
+      # PIC is the default for 64-bit PA HP-UX, but not for 32-bit
+      # PA HP-UX.  On IA64 HP-UX, PIC is the default but the pic flag
+      # sets the default TLS model and affects inlining.
+      case $host_cpu in
+      hppa*64*)
+	;;
+      *)
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	;;
+      esac
+      ;;
+    *qnx* | *nto*)
+      # QNX uses GNU C++, but need to define -shared option too, otherwise
+      # it will coredump.
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared'
+      ;;
+    *)
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+    esac
+  else
+    case $host_os in
+      aix[[4-9]]*)
+	# All AIX code is PIC.
+	if test ia64 = "$host_cpu"; then
+	  # AIX 5 now supports IA64 processor
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	else
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp'
+	fi
+	;;
+      chorus*)
+	case $cc_basename in
+	cxch68*)
+	  # Green Hills C++ Compiler
+	  # _LT_TAGVAR(lt_prog_compiler_static, $1)="--no_auto_instantiation -u __main -u __premain -u _abort -r $COOL_DIR/lib/libOrb.a $MVME_DIR/lib/CC/libC.a $MVME_DIR/lib/classix/libcx.s.a"
+	  ;;
+	esac
+	;;
+      mingw* | cygwin* | os2* | pw32* | cegcc*)
+	# This hack is so that the source file can tell whether it is being
+	# built for inclusion in a dll (and should export symbols for example).
+	m4_if([$1], [GCJ], [],
+	  [_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+	;;
+      dgux*)
+	case $cc_basename in
+	  ec++*)
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    ;;
+	  ghcx*)
+	    # Green Hills C++ Compiler
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      freebsd* | dragonfly*)
+	# FreeBSD uses GNU C++
+	;;
+      hpux9* | hpux10* | hpux11*)
+	case $cc_basename in
+	  CC*)
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive'
+	    if test ia64 != "$host_cpu"; then
+	      _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	    fi
+	    ;;
+	  aCC*)
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive'
+	    case $host_cpu in
+	    hppa*64*|ia64*)
+	      # +Z the default
+	      ;;
+	    *)
+	      _LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	      ;;
+	    esac
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      interix*)
+	# This is c89, which is MS Visual C++ (no shared libs)
+	# Anyone wants to do a port?
+	;;
+      irix5* | irix6* | nonstopux*)
+	case $cc_basename in
+	  CC*)
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    # CC pic flag -KPIC is the default.
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*)
+	case $cc_basename in
+	  KCC*)
+	    # KAI C++ Compiler
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	    ;;
+	  ecpc* )
+	    # old Intel C++ for x86_64, which still supported -KPIC.
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+	    ;;
+	  icpc* )
+	    # Intel C++, used to be incompatible with GCC.
+	    # ICC 10 doesn't accept -KPIC any more.
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+	    ;;
+	  pgCC* | pgcpp*)
+	    # Portland Group C++ compiler
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	  cxx*)
+	    # Compaq C++
+	    # Make sure the PIC flag is empty.  It appears that all Alpha
+	    # Linux and Compaq Tru64 Unix objects are PIC.
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)=
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    ;;
+	  xlc* | xlC* | bgxl[[cC]]* | mpixl[[cC]]*)
+	    # IBM XL 8.0, 9.0 on PPC and BlueGene
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink'
+	    ;;
+	  *)
+	    case `$CC -V 2>&1 | sed 5q` in
+	    *Sun\ C*)
+	      # Sun C++ 5.9
+	      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+	      ;;
+	    esac
+	    ;;
+	esac
+	;;
+      lynxos*)
+	;;
+      m88k*)
+	;;
+      mvs*)
+	case $cc_basename in
+	  cxx*)
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-W c,exportall'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      netbsd* | netbsdelf*-gnu)
+	;;
+      *qnx* | *nto*)
+        # QNX uses GNU C++, but need to define -shared option too, otherwise
+        # it will coredump.
+        _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared'
+        ;;
+      osf3* | osf4* | osf5*)
+	case $cc_basename in
+	  KCC*)
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,'
+	    ;;
+	  RCC*)
+	    # Rational C++ 2.4.1
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  cxx*)
+	    # Digital/Compaq C++
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    # Make sure the PIC flag is empty.  It appears that all Alpha
+	    # Linux and Compaq Tru64 Unix objects are PIC.
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)=
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      psos*)
+	;;
+      solaris*)
+	case $cc_basename in
+	  CC* | sunCC*)
+	    # Sun C++ 4.2, 5.x and Centerline C++
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+	    ;;
+	  gcx*)
+	    # Green Hills C++ Compiler
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      sunos4*)
+	case $cc_basename in
+	  CC*)
+	    # Sun C++ 4.x
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	  lcc*)
+	    # Lucid
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*)
+	case $cc_basename in
+	  CC*)
+	    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	esac
+	;;
+      tandem*)
+	case $cc_basename in
+	  NCC*)
+	    # NonStop-UX NCC 3.20
+	    _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      vxworks*)
+	;;
+      *)
+	_LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+	;;
+    esac
+  fi
+],
+[
+  if test yes = "$GCC"; then
+    _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+    _LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+
+    case $host_os in
+      aix*)
+      # All AIX code is PIC.
+      if test ia64 = "$host_cpu"; then
+	# AIX 5 now supports IA64 processor
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+
+    amigaos*)
+      case $host_cpu in
+      powerpc)
+            # see comment about AmigaOS4 .so support
+            _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+        ;;
+      m68k)
+            # FIXME: we need at least 68020 code to build shared libraries, but
+            # adding the '-m68020' flag to GCC prevents building anything better,
+            # like '-m68040'.
+            _LT_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4'
+        ;;
+      esac
+      ;;
+
+    beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*)
+      # PIC is the default for these OSes.
+      ;;
+
+    mingw* | cygwin* | pw32* | os2* | cegcc*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      # Although the cygwin gcc ignores -fPIC, still need this for old-style
+      # (--disable-auto-import) libraries
+      m4_if([$1], [GCJ], [],
+	[_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      case $host_os in
+      os2*)
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static'
+	;;
+      esac
+      ;;
+
+    darwin* | rhapsody*)
+      # PIC is the default on this platform
+      # Common symbols not allowed in MH_DYLIB files
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common'
+      ;;
+
+    haiku*)
+      # PIC is the default for Haiku.
+      # The "-static" flag exists, but is broken.
+      _LT_TAGVAR(lt_prog_compiler_static, $1)=
+      ;;
+
+    hpux*)
+      # PIC is the default for 64-bit PA HP-UX, but not for 32-bit
+      # PA HP-UX.  On IA64 HP-UX, PIC is the default but the pic flag
+      # sets the default TLS model and affects inlining.
+      case $host_cpu in
+      hppa*64*)
+	# +Z the default
+	;;
+      *)
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	;;
+      esac
+      ;;
+
+    interix[[3-9]]*)
+      # Interix 3.x gcc -fpic/-fPIC options generate broken code.
+      # Instead, we relocate shared libraries at runtime.
+      ;;
+
+    msdosdjgpp*)
+      # Just because we use GCC doesn't mean we suddenly get shared libraries
+      # on systems that don't support them.
+      _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      enable_shared=no
+      ;;
+
+    *nto* | *qnx*)
+      # QNX uses GNU C++, but need to define -shared option too, otherwise
+      # it will coredump.
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared'
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic
+      fi
+      ;;
+
+    *)
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+    esac
+
+    case $cc_basename in
+    nvcc*) # Cuda Compiler Driver 2.2
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Xlinker '
+      if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then
+        _LT_TAGVAR(lt_prog_compiler_pic, $1)="-Xcompiler $_LT_TAGVAR(lt_prog_compiler_pic, $1)"
+      fi
+      ;;
+    esac
+  else
+    # PORTME Check for flag to pass linker flags through the system compiler.
+    case $host_os in
+    aix*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      if test ia64 = "$host_cpu"; then
+	# AIX 5 now supports IA64 processor
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      else
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp'
+      fi
+      ;;
+
+    darwin* | rhapsody*)
+      # PIC is the default on this platform
+      # Common symbols not allowed in MH_DYLIB files
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common'
+      case $cc_basename in
+      nagfor*)
+        # NAG Fortran compiler
+        _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,'
+        _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+        _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+        ;;
+      esac
+      ;;
+
+    mingw* | cygwin* | pw32* | os2* | cegcc*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      m4_if([$1], [GCJ], [],
+	[_LT_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      case $host_os in
+      os2*)
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-static'
+	;;
+      esac
+      ;;
+
+    hpux9* | hpux10* | hpux11*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but
+      # not for PA HP-UX.
+      case $host_cpu in
+      hppa*64*|ia64*)
+	# +Z the default
+	;;
+      *)
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	;;
+      esac
+      # Is there a better lt_prog_compiler_static that works with the bundled CC?
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='$wl-a ${wl}archive'
+      ;;
+
+    irix5* | irix6* | nonstopux*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # PIC (with -KPIC) is the default.
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*)
+      case $cc_basename in
+      # old Intel for x86_64, which still supported -KPIC.
+      ecc*)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+        ;;
+      # icc used to be incompatible with GCC.
+      # ICC 10 doesn't accept -KPIC any more.
+      icc* | ifort*)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+        ;;
+      # Lahey Fortran 8.1.
+      lf95*)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='--shared'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='--static'
+	;;
+      nagfor*)
+	# NAG Fortran compiler
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,-Wl,,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	;;
+      tcc*)
+	# Fabrice Bellard et al's Tiny C Compiler
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+	;;
+      pgcc* | pgf77* | pgf90* | pgf95* | pgfortran*)
+        # Portland Group compilers (*not* the Pentium gcc compiler,
+	# which looks to be a dead project)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+        ;;
+      ccc*)
+        _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+        # All Alpha code is PIC.
+        _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+        ;;
+      xl* | bgxl* | bgf* | mpixl*)
+	# IBM XL C 8.0/Fortran 10.1, 11.1 on PPC and BlueGene
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-qpic'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-qstaticlink'
+	;;
+      *)
+	case `$CC -V 2>&1 | sed 5q` in
+	*Sun\ Ceres\ Fortran* | *Sun*Fortran*\ [[1-7]].* | *Sun*Fortran*\ 8.[[0-3]]*)
+	  # Sun Fortran 8.3 passes all unrecognized flags to the linker
+	  _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  _LT_TAGVAR(lt_prog_compiler_wl, $1)=''
+	  ;;
+	*Sun\ F* | *Sun*Fortran*)
+	  _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+	  ;;
+	*Sun\ C*)
+	  # Sun C 5.9
+	  _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	  ;;
+        *Intel*\ [[CF]]*Compiler*)
+	  _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	  _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-static'
+	  ;;
+	*Portland\ Group*)
+	  _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	  _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fpic'
+	  _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  ;;
+	esac
+	;;
+      esac
+      ;;
+
+    newsos6)
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    *nto* | *qnx*)
+      # QNX uses GNU C++, but need to define -shared option too, otherwise
+      # it will coredump.
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC -shared'
+      ;;
+
+    osf3* | osf4* | osf5*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # All OSF/1 code is PIC.
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    rdos*)
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    solaris*)
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      case $cc_basename in
+      f77* | f90* | f95* | sunf77* | sunf90* | sunf95*)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ';;
+      *)
+	_LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,';;
+      esac
+      ;;
+
+    sunos4*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    sysv4 | sysv4.2uw2* | sysv4.3*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_TAGVAR(lt_prog_compiler_pic, $1)='-Kconform_pic'
+	_LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      ;;
+
+    sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    unicos*)
+      _LT_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      ;;
+
+    uts4*)
+      _LT_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+      _LT_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    *)
+      _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      ;;
+    esac
+  fi
+])
+case $host_os in
+  # For platforms that do not support PIC, -DPIC is meaningless:
+  *djgpp*)
+    _LT_TAGVAR(lt_prog_compiler_pic, $1)=
+    ;;
+  *)
+    _LT_TAGVAR(lt_prog_compiler_pic, $1)="$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])"
+    ;;
+esac
+
+AC_CACHE_CHECK([for $compiler option to produce PIC],
+  [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)],
+  [_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_prog_compiler_pic, $1)])
+_LT_TAGVAR(lt_prog_compiler_pic, $1)=$_LT_TAGVAR(lt_cv_prog_compiler_pic, $1)
+
+#
+# Check to make sure the PIC flag actually works.
+#
+if test -n "$_LT_TAGVAR(lt_prog_compiler_pic, $1)"; then
+  _LT_COMPILER_OPTION([if $compiler PIC flag $_LT_TAGVAR(lt_prog_compiler_pic, $1) works],
+    [_LT_TAGVAR(lt_cv_prog_compiler_pic_works, $1)],
+    [$_LT_TAGVAR(lt_prog_compiler_pic, $1)@&t@m4_if([$1],[],[ -DPIC],[m4_if([$1],[CXX],[ -DPIC],[])])], [],
+    [case $_LT_TAGVAR(lt_prog_compiler_pic, $1) in
+     "" | " "*) ;;
+     *) _LT_TAGVAR(lt_prog_compiler_pic, $1)=" $_LT_TAGVAR(lt_prog_compiler_pic, $1)" ;;
+     esac],
+    [_LT_TAGVAR(lt_prog_compiler_pic, $1)=
+     _LT_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no])
+fi
+_LT_TAGDECL([pic_flag], [lt_prog_compiler_pic], [1],
+	[Additional compiler flags for building library objects])
+
+_LT_TAGDECL([wl], [lt_prog_compiler_wl], [1],
+	[How to pass a linker flag through the compiler])
+#
+# Check to make sure the static flag actually works.
+#
+wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1) eval lt_tmp_static_flag=\"$_LT_TAGVAR(lt_prog_compiler_static, $1)\"
+_LT_LINKER_OPTION([if $compiler static flag $lt_tmp_static_flag works],
+  _LT_TAGVAR(lt_cv_prog_compiler_static_works, $1),
+  $lt_tmp_static_flag,
+  [],
+  [_LT_TAGVAR(lt_prog_compiler_static, $1)=])
+_LT_TAGDECL([link_static_flag], [lt_prog_compiler_static], [1],
+	[Compiler flag to prevent dynamic linking])
+])# _LT_COMPILER_PIC
+
+
+# _LT_LINKER_SHLIBS([TAGNAME])
+# ----------------------------
+# See if the linker supports building shared libraries.
+m4_defun([_LT_LINKER_SHLIBS],
+[AC_REQUIRE([LT_PATH_LD])dnl
+AC_REQUIRE([LT_PATH_NM])dnl
+m4_require([_LT_PATH_MANIFEST_TOOL])dnl
+m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_DECL_EGREP])dnl
+m4_require([_LT_DECL_SED])dnl
+m4_require([_LT_CMD_GLOBAL_SYMBOLS])dnl
+m4_require([_LT_TAG_COMPILER])dnl
+AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries])
+m4_if([$1], [CXX], [
+  _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+  _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*']
+  case $host_os in
+  aix[[4-9]]*)
+    # If we're using GNU nm, then we don't want the "-C" option.
+    # -C means demangle to GNU nm, but means don't demangle to AIX nm.
+    # Without the "-l" option, or with the "-B" option, AIX nm treats
+    # weak defined symbols like other global defined symbols, whereas
+    # GNU nm marks them as "W".
+    # While the 'weak' keyword is ignored in the Export File, we need
+    # it in the Import File for the 'aix-soname' feature, so we have
+    # to replace the "-B" option with "-P" for AIX nm.
+    if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then
+      _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols'
+    else
+      _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols'
+    fi
+    ;;
+  pw32*)
+    _LT_TAGVAR(export_symbols_cmds, $1)=$ltdll_cmds
+    ;;
+  cygwin* | mingw* | cegcc*)
+    case $cc_basename in
+    cl*)
+      _LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*'
+      ;;
+    *)
+      _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols'
+      _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname']
+      ;;
+    esac
+    ;;
+  linux* | k*bsd*-gnu | gnu*)
+    _LT_TAGVAR(link_all_deplibs, $1)=no
+    ;;
+  *)
+    _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+    ;;
+  esac
+], [
+  runpath_var=
+  _LT_TAGVAR(allow_undefined_flag, $1)=
+  _LT_TAGVAR(always_export_symbols, $1)=no
+  _LT_TAGVAR(archive_cmds, $1)=
+  _LT_TAGVAR(archive_expsym_cmds, $1)=
+  _LT_TAGVAR(compiler_needs_object, $1)=no
+  _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+  _LT_TAGVAR(export_dynamic_flag_spec, $1)=
+  _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+  _LT_TAGVAR(hardcode_automatic, $1)=no
+  _LT_TAGVAR(hardcode_direct, $1)=no
+  _LT_TAGVAR(hardcode_direct_absolute, $1)=no
+  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=
+  _LT_TAGVAR(hardcode_libdir_separator, $1)=
+  _LT_TAGVAR(hardcode_minus_L, $1)=no
+  _LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+  _LT_TAGVAR(inherit_rpath, $1)=no
+  _LT_TAGVAR(link_all_deplibs, $1)=unknown
+  _LT_TAGVAR(module_cmds, $1)=
+  _LT_TAGVAR(module_expsym_cmds, $1)=
+  _LT_TAGVAR(old_archive_from_new_cmds, $1)=
+  _LT_TAGVAR(old_archive_from_expsyms_cmds, $1)=
+  _LT_TAGVAR(thread_safe_flag_spec, $1)=
+  _LT_TAGVAR(whole_archive_flag_spec, $1)=
+  # include_expsyms should be a list of space-separated symbols to be *always*
+  # included in the symbol list
+  _LT_TAGVAR(include_expsyms, $1)=
+  # exclude_expsyms can be an extended regexp of symbols to exclude
+  # it will be wrapped by ' (' and ')$', so one must not match beginning or
+  # end of line.  Example: 'a|bc|.*d.*' will exclude the symbols 'a' and 'bc',
+  # as well as any symbol that contains 'd'.
+  _LT_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*']
+  # Although _GLOBAL_OFFSET_TABLE_ is a valid symbol C name, most a.out
+  # platforms (ab)use it in PIC code, but their linkers get confused if
+  # the symbol is explicitly referenced.  Since portable code cannot
+  # rely on this symbol name, it's probably fine to never include it in
+  # preloaded symbol tables.
+  # Exclude shared library initialization/finalization symbols.
+dnl Note also adjust exclude_expsyms for C++ above.
+  extract_expsyms_cmds=
+
+  case $host_os in
+  cygwin* | mingw* | pw32* | cegcc*)
+    # FIXME: the MSVC++ port hasn't been tested in a loooong time
+    # When not using gcc, we currently assume that we are using
+    # Microsoft Visual C++.
+    if test yes != "$GCC"; then
+      with_gnu_ld=no
+    fi
+    ;;
+  interix*)
+    # we just hope/assume this is gcc and not c89 (= MSVC++)
+    with_gnu_ld=yes
+    ;;
+  openbsd* | bitrig*)
+    with_gnu_ld=no
+    ;;
+  linux* | k*bsd*-gnu | gnu*)
+    _LT_TAGVAR(link_all_deplibs, $1)=no
+    ;;
+  esac
+
+  _LT_TAGVAR(ld_shlibs, $1)=yes
+
+  # On some targets, GNU ld is compatible enough with the native linker
+  # that we're better off using the native interface for both.
+  lt_use_gnu_ld_interface=no
+  if test yes = "$with_gnu_ld"; then
+    case $host_os in
+      aix*)
+	# The AIX port of GNU ld has always aspired to compatibility
+	# with the native linker.  However, as the warning in the GNU ld
+	# block says, versions before 2.19.5* couldn't really create working
+	# shared libraries, regardless of the interface used.
+	case `$LD -v 2>&1` in
+	  *\ \(GNU\ Binutils\)\ 2.19.5*) ;;
+	  *\ \(GNU\ Binutils\)\ 2.[[2-9]]*) ;;
+	  *\ \(GNU\ Binutils\)\ [[3-9]]*) ;;
+	  *)
+	    lt_use_gnu_ld_interface=yes
+	    ;;
+	esac
+	;;
+      *)
+	lt_use_gnu_ld_interface=yes
+	;;
+    esac
+  fi
+
+  if test yes = "$lt_use_gnu_ld_interface"; then
+    # If archive_cmds runs LD, not CC, wlarc should be empty
+    wlarc='$wl'
+
+    # Set some defaults for GNU ld with shared library support. These
+    # are reset later if shared libraries are not supported. Putting them
+    # here allows them to be overridden if necessary.
+    runpath_var=LD_RUN_PATH
+    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+    # ancient GNU ld didn't support --whole-archive et. al.
+    if $LD --help 2>&1 | $GREP 'no-whole-archive' > /dev/null; then
+      _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive'
+    else
+      _LT_TAGVAR(whole_archive_flag_spec, $1)=
+    fi
+    supports_anon_versioning=no
+    case `$LD -v | $SED -e 's/([^)]\+)\s\+//' 2>&1` in
+      *GNU\ gold*) supports_anon_versioning=yes ;;
+      *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.10.*) ;; # catch versions < 2.11
+      *\ 2.11.93.0.2\ *) supports_anon_versioning=yes ;; # RH7.3 ...
+      *\ 2.11.92.0.12\ *) supports_anon_versioning=yes ;; # Mandrake 8.2 ...
+      *\ 2.11.*) ;; # other 2.11 versions
+      *) supports_anon_versioning=yes ;;
+    esac
+
+    # See if GNU ld supports shared libraries.
+    case $host_os in
+    aix[[3-9]]*)
+      # On AIX/PPC, the GNU linker is very broken
+      if test ia64 != "$host_cpu"; then
+	_LT_TAGVAR(ld_shlibs, $1)=no
+	cat <<_LT_EOF 1>&2
+
+*** Warning: the GNU linker, at least up to release 2.19, is reported
+*** to be unable to reliably create shared libraries on AIX.
+*** Therefore, libtool is disabling shared libraries support.  If you
+*** really care for shared libraries, you may want to install binutils
+*** 2.20 or above, or modify your PATH so that a non-GNU linker is found.
+*** You will then need to restart the configuration process.
+
+_LT_EOF
+      fi
+      ;;
+
+    amigaos*)
+      case $host_cpu in
+      powerpc)
+            # see comment about AmigaOS4 .so support
+            _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+            _LT_TAGVAR(archive_expsym_cmds, $1)=''
+        ;;
+      m68k)
+            _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)'
+            _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+            _LT_TAGVAR(hardcode_minus_L, $1)=yes
+        ;;
+      esac
+      ;;
+
+    beos*)
+      if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then
+	_LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	# Joseph Beckenbach <jrb3@best.com> says some releases of gcc
+	# support --undefined.  This deserves some investigation.  FIXME
+	_LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+      else
+	_LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    cygwin* | mingw* | pw32* | cegcc*)
+      # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless,
+      # as there is no search path for DLLs.
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols'
+      _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+      _LT_TAGVAR(always_export_symbols, $1)=no
+      _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+      _LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.* //'\'' | sort | uniq > $export_symbols'
+      _LT_TAGVAR(exclude_expsyms, $1)=['[_]+GLOBAL_OFFSET_TABLE_|[_]+GLOBAL__[FID]_.*|[_]+head_[A-Za-z0-9_]+_dll|[A-Za-z0-9_]+_dll_iname']
+
+      if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then
+        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+	# If the export-symbols file already is a .def file, use it as
+	# is; otherwise, prepend EXPORTS...
+	_LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then
+          cp $export_symbols $output_objdir/$soname.def;
+        else
+          echo EXPORTS > $output_objdir/$soname.def;
+          cat $export_symbols >> $output_objdir/$soname.def;
+        fi~
+        $CC -shared $output_objdir/$soname.def $libobjs $deplibs $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+      else
+	_LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    haiku*)
+      _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+      _LT_TAGVAR(link_all_deplibs, $1)=yes
+      ;;
+
+    os2*)
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+      shrext_cmds=.dll
+      _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	$ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	$ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	$ECHO EXPORTS >> $output_objdir/$libname.def~
+	emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~
+	$CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	emximp -o $lib $output_objdir/$libname.def'
+      _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	$ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	$ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	$ECHO EXPORTS >> $output_objdir/$libname.def~
+	prefix_cmds="$SED"~
+	if test EXPORTS = "`$SED 1q $export_symbols`"; then
+	  prefix_cmds="$prefix_cmds -e 1d";
+	fi~
+	prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~
+	cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~
+	$CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	emximp -o $lib $output_objdir/$libname.def'
+      _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def'
+      _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+      ;;
+
+    interix[[3-9]]*)
+      _LT_TAGVAR(hardcode_direct, $1)=no
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+      # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc.
+      # Instead, shared libraries are loaded at an image base (0x10000000 by
+      # default) and relocated if they conflict, which is a slow very memory
+      # consuming and fragmenting process.  To avoid this, we pick a random,
+      # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link
+      # time.  Moving up from 0x10000000 also allows more sbrk(2) space.
+      _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+      _LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+      ;;
+
+    gnu* | linux* | tpf* | k*bsd*-gnu | kopensolaris*-gnu)
+      tmp_diet=no
+      if test linux-dietlibc = "$host_os"; then
+	case $cc_basename in
+	  diet\ *) tmp_diet=yes;;	# linux-dietlibc with static linking (!diet-dyn)
+	esac
+      fi
+      if $LD --help 2>&1 | $EGREP ': supported targets:.* elf' > /dev/null \
+	 && test no = "$tmp_diet"
+      then
+	tmp_addflag=' $pic_flag'
+	tmp_sharedflag='-shared'
+	case $cc_basename,$host_cpu in
+        pgcc*)				# Portland Group C compiler
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+	  tmp_addflag=' $pic_flag'
+	  ;;
+	pgf77* | pgf90* | pgf95* | pgfortran*)
+					# Portland Group f77 and f90 compilers
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+	  tmp_addflag=' $pic_flag -Mnomain' ;;
+	ecc*,ia64* | icc*,ia64*)	# Intel C compiler on ia64
+	  tmp_addflag=' -i_dynamic' ;;
+	efc*,ia64* | ifort*,ia64*)	# Intel Fortran compiler on ia64
+	  tmp_addflag=' -i_dynamic -nofor_main' ;;
+	ifc* | ifort*)			# Intel Fortran compiler
+	  tmp_addflag=' -nofor_main' ;;
+	lf95*)				# Lahey Fortran 8.1
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)=
+	  tmp_sharedflag='--shared' ;;
+        nagfor*)                        # NAGFOR 5.3
+          tmp_sharedflag='-Wl,-shared' ;;
+	xl[[cC]]* | bgxl[[cC]]* | mpixl[[cC]]*) # IBM XL C 8.0 on PPC (deal with xlf below)
+	  tmp_sharedflag='-qmkshrobj'
+	  tmp_addflag= ;;
+	nvcc*)	# Cuda Compiler Driver 2.2
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+	  _LT_TAGVAR(compiler_needs_object, $1)=yes
+	  ;;
+	esac
+	case `$CC -V 2>&1 | sed 5q` in
+	*Sun\ C*)			# Sun C 5.9
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+	  _LT_TAGVAR(compiler_needs_object, $1)=yes
+	  tmp_sharedflag='-G' ;;
+	*Sun\ F*)			# Sun Fortran 8.3
+	  tmp_sharedflag='-G' ;;
+	esac
+	_LT_TAGVAR(archive_cmds, $1)='$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+
+        if test yes = "$supports_anon_versioning"; then
+          _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~
+            cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~
+            echo "local: *; };" >> $output_objdir/$libname.ver~
+            $CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib'
+        fi
+
+	case $cc_basename in
+	tcc*)
+	  _LT_TAGVAR(export_dynamic_flag_spec, $1)='-rdynamic'
+	  ;;
+	xlf* | bgf* | bgxlf* | mpixlf*)
+	  # IBM XL Fortran 10.1 on PPC cannot create shared libs itself
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='--whole-archive$convenience --no-whole-archive'
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -shared $libobjs $deplibs $linker_flags -soname $soname -o $lib'
+	  if test yes = "$supports_anon_versioning"; then
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~
+              cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~
+              echo "local: *; };" >> $output_objdir/$libname.ver~
+              $LD -shared $libobjs $deplibs $linker_flags -soname $soname -version-script $output_objdir/$libname.ver -o $lib'
+	  fi
+	  ;;
+	esac
+      else
+        _LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    netbsd* | netbsdelf*-gnu)
+      if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then
+	_LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable $libobjs $deplibs $linker_flags -o $lib'
+	wlarc=
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+      fi
+      ;;
+
+    solaris*)
+      if $LD -v 2>&1 | $GREP 'BFD 2\.8' > /dev/null; then
+	_LT_TAGVAR(ld_shlibs, $1)=no
+	cat <<_LT_EOF 1>&2
+
+*** Warning: The releases 2.8.* of the GNU linker cannot reliably
+*** create shared libraries on Solaris systems.  Therefore, libtool
+*** is disabling shared libraries support.  We urge you to upgrade GNU
+*** binutils to release 2.9.1 or newer.  Another option is to modify
+*** your PATH or compiler configuration so that the native linker is
+*** used, and then restart.
+
+_LT_EOF
+      elif $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+      else
+	_LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*)
+      case `$LD -v 2>&1` in
+        *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.1[[0-5]].*)
+	_LT_TAGVAR(ld_shlibs, $1)=no
+	cat <<_LT_EOF 1>&2
+
+*** Warning: Releases of the GNU linker prior to 2.16.91.0.3 cannot
+*** reliably create shared libraries on SCO systems.  Therefore, libtool
+*** is disabling shared libraries support.  We urge you to upgrade GNU
+*** binutils to release 2.16.91.0.3 or newer.  Another option is to modify
+*** your PATH or compiler configuration so that the native linker is
+*** used, and then restart.
+
+_LT_EOF
+	;;
+	*)
+	  # For security reasons, it is highly recommended that you always
+	  # use absolute paths for naming shared libraries, and exclude the
+	  # DT_RUNPATH tag from executables and libraries.  But doing so
+	  # requires that you compile everything twice, which is a pain.
+	  if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+	  else
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	  fi
+	;;
+      esac
+      ;;
+
+    sunos4*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bshareable -o $lib $libobjs $deplibs $linker_flags'
+      wlarc=
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    *)
+      if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+      else
+	_LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+    esac
+
+    if test no = "$_LT_TAGVAR(ld_shlibs, $1)"; then
+      runpath_var=
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)=
+      _LT_TAGVAR(whole_archive_flag_spec, $1)=
+    fi
+  else
+    # PORTME fill in a description of your system's linker (not GNU ld)
+    case $host_os in
+    aix3*)
+      _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+      _LT_TAGVAR(always_export_symbols, $1)=yes
+      _LT_TAGVAR(archive_expsym_cmds, $1)='$LD -o $output_objdir/$soname $libobjs $deplibs $linker_flags -bE:$export_symbols -T512 -H512 -bM:SRE~$AR $AR_FLAGS $lib $output_objdir/$soname'
+      # Note: this linker hardcodes the directories in LIBPATH if there
+      # are no directories specified by -L.
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      if test yes = "$GCC" && test -z "$lt_prog_compiler_static"; then
+	# Neither direct hardcoding nor static linking is supported with a
+	# broken collect2.
+	_LT_TAGVAR(hardcode_direct, $1)=unsupported
+      fi
+      ;;
+
+    aix[[4-9]]*)
+      if test ia64 = "$host_cpu"; then
+	# On IA64, the linker does run time linking by default, so we don't
+	# have to do anything special.
+	aix_use_runtimelinking=no
+	exp_sym_flag='-Bexport'
+	no_entry_flag=
+      else
+	# If we're using GNU nm, then we don't want the "-C" option.
+	# -C means demangle to GNU nm, but means don't demangle to AIX nm.
+	# Without the "-l" option, or with the "-B" option, AIX nm treats
+	# weak defined symbols like other global defined symbols, whereas
+	# GNU nm marks them as "W".
+	# While the 'weak' keyword is ignored in the Export File, we need
+	# it in the Import File for the 'aix-soname' feature, so we have
+	# to replace the "-B" option with "-P" for AIX nm.
+	if $NM -V 2>&1 | $GREP 'GNU' > /dev/null; then
+	  _LT_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W")) && ([substr](\$ 3,1,1) != ".")) { if (\$ 2 == "W") { print \$ 3 " weak" } else { print \$ 3 } } }'\'' | sort -u > $export_symbols'
+	else
+	  _LT_TAGVAR(export_symbols_cmds, $1)='`func_echo_all $NM | $SED -e '\''s/B\([[^B]]*\)$/P\1/'\''` -PCpgl $libobjs $convenience | awk '\''{ if (((\$ 2 == "T") || (\$ 2 == "D") || (\$ 2 == "B") || (\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) && ([substr](\$ 1,1,1) != ".")) { if ((\$ 2 == "W") || (\$ 2 == "V") || (\$ 2 == "Z")) { print \$ 1 " weak" } else { print \$ 1 } } }'\'' | sort -u > $export_symbols'
+	fi
+	aix_use_runtimelinking=no
+
+	# Test if we are trying to use run time linking or normal
+	# AIX style linking. If -brtl is somewhere in LDFLAGS, we
+	# have runtime linking enabled, and use it for executables.
+	# For shared libraries, we enable/disable runtime linking
+	# depending on the kind of the shared library created -
+	# when "with_aix_soname,aix_use_runtimelinking" is:
+	# "aix,no"   lib.a(lib.so.V) shared, rtl:no,  for executables
+	# "aix,yes"  lib.so          shared, rtl:yes, for executables
+	#            lib.a           static archive
+	# "both,no"  lib.so.V(shr.o) shared, rtl:yes
+	#            lib.a(lib.so.V) shared, rtl:no,  for executables
+	# "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables
+	#            lib.a(lib.so.V) shared, rtl:no
+	# "svr4,*"   lib.so.V(shr.o) shared, rtl:yes, for executables
+	#            lib.a           static archive
+	case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*)
+	  for ld_flag in $LDFLAGS; do
+	  if (test x-brtl = "x$ld_flag" || test x-Wl,-brtl = "x$ld_flag"); then
+	    aix_use_runtimelinking=yes
+	    break
+	  fi
+	  done
+	  if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then
+	    # With aix-soname=svr4, we create the lib.so.V shared archives only,
+	    # so we don't have lib.a shared libs to link our executables.
+	    # We have to force runtime linking in this case.
+	    aix_use_runtimelinking=yes
+	    LDFLAGS="$LDFLAGS -Wl,-brtl"
+	  fi
+	  ;;
+	esac
+
+	exp_sym_flag='-bexport'
+	no_entry_flag='-bnoentry'
+      fi
+
+      # When large executables or shared objects are built, AIX ld can
+      # have problems creating the table of contents.  If linking a library
+      # or program results in "error TOC overflow" add -mminimal-toc to
+      # CXXFLAGS/CFLAGS for g++/gcc.  In the cases where that is not
+      # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS.
+
+      _LT_TAGVAR(archive_cmds, $1)=''
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=':'
+      _LT_TAGVAR(link_all_deplibs, $1)=yes
+      _LT_TAGVAR(file_list_spec, $1)='$wl-f,'
+      case $with_aix_soname,$aix_use_runtimelinking in
+      aix,*) ;; # traditional, no import file
+      svr4,* | *,yes) # use import file
+	# The Import File defines what to hardcode.
+	_LT_TAGVAR(hardcode_direct, $1)=no
+	_LT_TAGVAR(hardcode_direct_absolute, $1)=no
+	;;
+      esac
+
+      if test yes = "$GCC"; then
+	case $host_os in aix4.[[012]]|aix4.[[012]].*)
+	# We only want to do this on AIX 4.2 and lower, the check
+	# below for broken collect2 doesn't work under 4.3+
+	  collect2name=`$CC -print-prog-name=collect2`
+	  if test -f "$collect2name" &&
+	   strings "$collect2name" | $GREP resolve_lib_name >/dev/null
+	  then
+	  # We have reworked collect2
+	  :
+	  else
+	  # We have old collect2
+	  _LT_TAGVAR(hardcode_direct, $1)=unsupported
+	  # It fails to find uninstalled libraries when the uninstalled
+	  # path is not listed in the libpath.  Setting hardcode_minus_L
+	  # to unsupported forces relinking
+	  _LT_TAGVAR(hardcode_minus_L, $1)=yes
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+	  _LT_TAGVAR(hardcode_libdir_separator, $1)=
+	  fi
+	  ;;
+	esac
+	shared_flag='-shared'
+	if test yes = "$aix_use_runtimelinking"; then
+	  shared_flag="$shared_flag "'$wl-G'
+	fi
+	# Need to ensure runtime linking is disabled for the traditional
+	# shared library, or the linker may eventually find shared libraries
+	# /with/ Import File - we do not want to mix them.
+	shared_flag_aix='-shared'
+	shared_flag_svr4='-shared $wl-G'
+      else
+	# not using gcc
+	if test ia64 = "$host_cpu"; then
+	# VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release
+	# chokes on -Wl,-G. The following line is correct:
+	  shared_flag='-G'
+	else
+	  if test yes = "$aix_use_runtimelinking"; then
+	    shared_flag='$wl-G'
+	  else
+	    shared_flag='$wl-bM:SRE'
+	  fi
+	  shared_flag_aix='$wl-bM:SRE'
+	  shared_flag_svr4='$wl-G'
+	fi
+      fi
+
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall'
+      # It seems that -bexpall does not export symbols beginning with
+      # underscore (_), so it is better to generate a list of symbols to export.
+      _LT_TAGVAR(always_export_symbols, $1)=yes
+      if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then
+	# Warning - without using the other runtime loading flags (-brtl),
+	# -berok will link without error, but may produce a broken library.
+	_LT_TAGVAR(allow_undefined_flag, $1)='-berok'
+        # Determine the default libpath from the value encoded in an
+        # empty executable.
+        _LT_SYS_MODULE_PATH_AIX([$1])
+        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath"
+        _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag
+      else
+	if test ia64 = "$host_cpu"; then
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib'
+	  _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs"
+	  _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols"
+	else
+	 # Determine the default libpath from the value encoded in an
+	 # empty executable.
+	 _LT_SYS_MODULE_PATH_AIX([$1])
+	 _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath"
+	  # Warning - without using the other run time loading flags,
+	  # -berok will link without error, but may produce a broken library.
+	  _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok'
+	  _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok'
+	  if test yes = "$with_gnu_ld"; then
+	    # We only use this code for GNU lds that support --whole-archive.
+	    _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive'
+	  else
+	    # Exported symbols can be pulled into shared objects from archives
+	    _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience'
+	  fi
+	  _LT_TAGVAR(archive_cmds_need_lc, $1)=yes
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d'
+	  # -brtl affects multiple linker settings, -berok does not and is overridden later
+	  compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`'
+	  if test svr4 != "$with_aix_soname"; then
+	    # This is similar to how AIX traditionally builds its shared libraries.
+	    _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname'
+	  fi
+	  if test aix != "$with_aix_soname"; then
+	    _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp'
+	  else
+	    # used by -dlpreopen to get the symbols
+	    _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV  $output_objdir/$realname.d/$soname $output_objdir'
+	  fi
+	  _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d'
+	fi
+      fi
+      ;;
+
+    amigaos*)
+      case $host_cpu in
+      powerpc)
+            # see comment about AmigaOS4 .so support
+            _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+            _LT_TAGVAR(archive_expsym_cmds, $1)=''
+        ;;
+      m68k)
+            _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/a2ixlibrary.data~$ECHO "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$ECHO "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$ECHO "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$ECHO "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)'
+            _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+            _LT_TAGVAR(hardcode_minus_L, $1)=yes
+        ;;
+      esac
+      ;;
+
+    bsdi[[45]]*)
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)=-rdynamic
+      ;;
+
+    cygwin* | mingw* | pw32* | cegcc*)
+      # When not using gcc, we currently assume that we are using
+      # Microsoft Visual C++.
+      # hardcode_libdir_flag_spec is actually meaningless, as there is
+      # no search path for DLLs.
+      case $cc_basename in
+      cl*)
+	# Native MSVC
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' '
+	_LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	_LT_TAGVAR(always_export_symbols, $1)=yes
+	_LT_TAGVAR(file_list_spec, $1)='@'
+	# Tell ltmain to make .lib files, not .a files.
+	libext=lib
+	# Tell ltmain to make .dll files, not .so files.
+	shrext_cmds=.dll
+	# FIXME: Setting linknames here is a bad hack.
+	_LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames='
+	_LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then
+            cp "$export_symbols" "$output_objdir/$soname.def";
+            echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp";
+          else
+            $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp;
+          fi~
+          $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~
+          linknames='
+	# The linker will not automatically build a static lib if we build a DLL.
+	# _LT_TAGVAR(old_archive_from_new_cmds, $1)='true'
+	_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+	_LT_TAGVAR(exclude_expsyms, $1)='_NULL_IMPORT_DESCRIPTOR|_IMPORT_DESCRIPTOR_.*'
+	_LT_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1,DATA/'\'' | $SED -e '\''/^[[AITW]][[ ]]/s/.*[[ ]]//'\'' | sort | uniq > $export_symbols'
+	# Don't use ranlib
+	_LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib'
+	_LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~
+          lt_tool_outputfile="@TOOL_OUTPUT@"~
+          case $lt_outputfile in
+            *.exe|*.EXE) ;;
+            *)
+              lt_outputfile=$lt_outputfile.exe
+              lt_tool_outputfile=$lt_tool_outputfile.exe
+              ;;
+          esac~
+          if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then
+            $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1;
+            $RM "$lt_outputfile.manifest";
+          fi'
+	;;
+      *)
+	# Assume MSVC wrapper
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' '
+	_LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	# Tell ltmain to make .lib files, not .a files.
+	libext=lib
+	# Tell ltmain to make .dll files, not .so files.
+	shrext_cmds=.dll
+	# FIXME: Setting linknames here is a bad hack.
+	_LT_TAGVAR(archive_cmds, $1)='$CC -o $lib $libobjs $compiler_flags `func_echo_all "$deplibs" | $SED '\''s/ -lc$//'\''` -link -dll~linknames='
+	# The linker will automatically build a .lib file if we build a DLL.
+	_LT_TAGVAR(old_archive_from_new_cmds, $1)='true'
+	# FIXME: Should let the user specify the lib program.
+	_LT_TAGVAR(old_archive_cmds, $1)='lib -OUT:$oldlib$oldobjs$old_deplibs'
+	_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+	;;
+      esac
+      ;;
+
+    darwin* | rhapsody*)
+      _LT_DARWIN_LINKER_FEATURES($1)
+      ;;
+
+    dgux*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor
+    # support.  Future versions do this automatically, but an explicit c++rt0.o
+    # does not break anything, and helps significantly (at the cost of a little
+    # extra space).
+    freebsd2.2*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags /usr/lib/c++rt0.o'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    # Unfortunately, older versions of FreeBSD 2 do not have this feature.
+    freebsd2.*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    # FreeBSD 3 and greater uses gcc -shared to do shared libraries.
+    freebsd* | dragonfly*)
+      _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    hpux9*)
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $libobjs $deplibs $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib'
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$LD -b +b $install_libdir -o $output_objdir/$soname $libobjs $deplibs $linker_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib'
+      fi
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+
+      # hardcode_minus_L: Not really in the search PATH,
+      # but as the default location of the library.
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+      ;;
+
+    hpux10*)
+      if test yes,no = "$GCC,$with_gnu_ld"; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags'
+      fi
+      if test no = "$with_gnu_ld"; then
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir'
+	_LT_TAGVAR(hardcode_libdir_separator, $1)=:
+	_LT_TAGVAR(hardcode_direct, $1)=yes
+	_LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+	_LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+	# hardcode_minus_L: Not really in the search PATH,
+	# but as the default location of the library.
+	_LT_TAGVAR(hardcode_minus_L, $1)=yes
+      fi
+      ;;
+
+    hpux11*)
+      if test yes,no = "$GCC,$with_gnu_ld"; then
+	case $host_cpu in
+	hppa*64*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	ia64*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	esac
+      else
+	case $host_cpu in
+	hppa*64*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	ia64*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	*)
+	m4_if($1, [], [
+	  # Older versions of the 11.00 compiler do not understand -b yet
+	  # (HP92453-01 A.11.01.20 doesn't, HP92453-01 B.11.X.35175-35176.GP does)
+	  _LT_LINKER_OPTION([if $CC understands -b],
+	    _LT_TAGVAR(lt_cv_prog_compiler__b, $1), [-b],
+	    [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'],
+	    [_LT_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags'])],
+	  [_LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $libobjs $deplibs $compiler_flags'])
+	  ;;
+	esac
+      fi
+      if test no = "$with_gnu_ld"; then
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir'
+	_LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	case $host_cpu in
+	hppa*64*|ia64*)
+	  _LT_TAGVAR(hardcode_direct, $1)=no
+	  _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	  ;;
+	*)
+	  _LT_TAGVAR(hardcode_direct, $1)=yes
+	  _LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+	  _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+
+	  # hardcode_minus_L: Not really in the search PATH,
+	  # but as the default location of the library.
+	  _LT_TAGVAR(hardcode_minus_L, $1)=yes
+	  ;;
+	esac
+      fi
+      ;;
+
+    irix5* | irix6* | nonstopux*)
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+	# Try to use the -exported_symbol ld option, if it does not
+	# work, assume that -exports_file does not work either and
+	# implicitly export all symbols.
+	# This should be the same for all languages, so no per-tag cache variable.
+	AC_CACHE_CHECK([whether the $host_os linker accepts -exported_symbol],
+	  [lt_cv_irix_exported_symbol],
+	  [save_LDFLAGS=$LDFLAGS
+	   LDFLAGS="$LDFLAGS -shared $wl-exported_symbol ${wl}foo $wl-update_registry $wl/dev/null"
+	   AC_LINK_IFELSE(
+	     [AC_LANG_SOURCE(
+	        [AC_LANG_CASE([C], [[int foo (void) { return 0; }]],
+			      [C++], [[int foo (void) { return 0; }]],
+			      [Fortran 77], [[
+      subroutine foo
+      end]],
+			      [Fortran], [[
+      subroutine foo
+      end]])])],
+	      [lt_cv_irix_exported_symbol=yes],
+	      [lt_cv_irix_exported_symbol=no])
+           LDFLAGS=$save_LDFLAGS])
+	if test yes = "$lt_cv_irix_exported_symbol"; then
+          _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations $wl-exports_file $wl$export_symbols -o $lib'
+	fi
+	_LT_TAGVAR(link_all_deplibs, $1)=no
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -exports_file $export_symbols -o $lib'
+      fi
+      _LT_TAGVAR(archive_cmds_need_lc, $1)='no'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_TAGVAR(inherit_rpath, $1)=yes
+      _LT_TAGVAR(link_all_deplibs, $1)=yes
+      ;;
+
+    linux*)
+      case $cc_basename in
+      tcc*)
+	# Fabrice Bellard et al's Tiny C Compiler
+	_LT_TAGVAR(ld_shlibs, $1)=yes
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+	;;
+      esac
+      ;;
+
+    netbsd* | netbsdelf*-gnu)
+      if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then
+	_LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags'  # a.out
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$LD -shared -o $lib $libobjs $deplibs $linker_flags'      # ELF
+      fi
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    newsos6)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    *nto* | *qnx*)
+      ;;
+
+    openbsd* | bitrig*)
+      if test -f /usr/libexec/ld.so; then
+	_LT_TAGVAR(hardcode_direct, $1)=yes
+	_LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	_LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+	if test -z "`echo __ELF__ | $CC -E - | $GREP __ELF__`"; then
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags $wl-retain-symbols-file,$export_symbols'
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	  _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+	else
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	fi
+      else
+	_LT_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    os2*)
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+      shrext_cmds=.dll
+      _LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	$ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	$ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	$ECHO EXPORTS >> $output_objdir/$libname.def~
+	emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~
+	$CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	emximp -o $lib $output_objdir/$libname.def'
+      _LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	$ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	$ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	$ECHO EXPORTS >> $output_objdir/$libname.def~
+	prefix_cmds="$SED"~
+	if test EXPORTS = "`$SED 1q $export_symbols`"; then
+	  prefix_cmds="$prefix_cmds -e 1d";
+	fi~
+	prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~
+	cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~
+	$CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	emximp -o $lib $output_objdir/$libname.def'
+      _LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def'
+      _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+      ;;
+
+    osf3*)
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*'
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+      else
+	_LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+      fi
+      _LT_TAGVAR(archive_cmds_need_lc, $1)='no'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+      ;;
+
+    osf4* | osf5*)	# as osf3* with the addition of -msym flag
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*'
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $pic_flag $libobjs $deplibs $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+      else
+	_LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $libobjs $deplibs $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done; printf "%s\\n" "-hidden">> $lib.exp~
+          $CC -shared$allow_undefined_flag $wl-input $wl$lib.exp $compiler_flags $libobjs $deplibs -soname $soname `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~$RM $lib.exp'
+
+	# Both c and cxx compiler support -rpath directly
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+      fi
+      _LT_TAGVAR(archive_cmds_need_lc, $1)='no'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+      ;;
+
+    solaris*)
+      _LT_TAGVAR(no_undefined_flag, $1)=' -z defs'
+      if test yes = "$GCC"; then
+	wlarc='$wl'
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $wl-z ${wl}text $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+          $CC -shared $pic_flag $wl-z ${wl}text $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp'
+      else
+	case `$CC -V 2>&1` in
+	*"Compilers 5.0"*)
+	  wlarc=''
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+            $LD -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $linker_flags~$RM $lib.exp'
+	  ;;
+	*)
+	  wlarc='$wl'
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h $soname -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+            $CC -G$allow_undefined_flag -M $lib.exp -h $soname -o $lib $libobjs $deplibs $compiler_flags~$RM $lib.exp'
+	  ;;
+	esac
+      fi
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      case $host_os in
+      solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+      *)
+	# The compiler driver will combine and reorder linker options,
+	# but understands '-z linker_flag'.  GCC discards it without '$wl',
+	# but is careful enough not to reorder.
+	# Supported since Solaris 2.6 (maybe 2.5.1?)
+	if test yes = "$GCC"; then
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract'
+	else
+	  _LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract'
+	fi
+	;;
+      esac
+      _LT_TAGVAR(link_all_deplibs, $1)=yes
+      ;;
+
+    sunos4*)
+      if test sequent = "$host_vendor"; then
+	# Use $CC to link under sequent, because it throws in some extra .o
+	# files that make .init and .fini sections work.
+	_LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h $soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bstatic -o $lib $libobjs $deplibs $linker_flags'
+      fi
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(hardcode_direct, $1)=yes
+      _LT_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    sysv4)
+      case $host_vendor in
+	sni)
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_TAGVAR(hardcode_direct, $1)=yes # is this really true???
+	;;
+	siemens)
+	  ## LD is ld it makes a PLAMLIB
+	  ## CC just makes a GrossModule.
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -G -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_TAGVAR(reload_cmds, $1)='$CC -r -o $output$reload_objs'
+	  _LT_TAGVAR(hardcode_direct, $1)=no
+        ;;
+	motorola)
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_TAGVAR(hardcode_direct, $1)=no #Motorola manual says yes, but my tests say they lie
+	;;
+      esac
+      runpath_var='LD_RUN_PATH'
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    sysv4.3*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='-Bexport'
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	_LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	runpath_var=LD_RUN_PATH
+	hardcode_runpath_var=yes
+	_LT_TAGVAR(ld_shlibs, $1)=yes
+      fi
+      ;;
+
+    sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*)
+      _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text'
+      _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      runpath_var='LD_RUN_PATH'
+
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      fi
+      ;;
+
+    sysv5* | sco3.2v5* | sco5v6*)
+      # Note: We CANNOT use -z defs as we might desire, because we do not
+      # link with -lc, and that would cause any symbols used from libc to
+      # always be unresolved, which means just about no library would
+      # ever link correctly.  If we're not using GNU ld we use -z text
+      # though, which does catch some bad symbols but isn't as heavy-handed
+      # as -z defs.
+      _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text'
+      _LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs'
+      _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir'
+      _LT_TAGVAR(hardcode_libdir_separator, $1)=':'
+      _LT_TAGVAR(link_all_deplibs, $1)=yes
+      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport'
+      runpath_var='LD_RUN_PATH'
+
+      if test yes = "$GCC"; then
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      fi
+      ;;
+
+    uts4*)
+      _LT_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    *)
+      _LT_TAGVAR(ld_shlibs, $1)=no
+      ;;
+    esac
+
+    if test sni = "$host_vendor"; then
+      case $host in
+      sysv4 | sysv4.2uw2* | sysv4.3* | sysv5*)
+	_LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Blargedynsym'
+	;;
+      esac
+    fi
+  fi
+])
+AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)])
+test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no
+
+_LT_TAGVAR(with_gnu_ld, $1)=$with_gnu_ld
+
+_LT_DECL([], [libext], [0], [Old archive suffix (normally "a")])dnl
+_LT_DECL([], [shrext_cmds], [1], [Shared library suffix (normally ".so")])dnl
+_LT_DECL([], [extract_expsyms_cmds], [2],
+    [The commands to extract the exported symbol list from a shared archive])
+
+#
+# Do we need to explicitly link libc?
+#
+case "x$_LT_TAGVAR(archive_cmds_need_lc, $1)" in
+x|xyes)
+  # Assume -lc should be added
+  _LT_TAGVAR(archive_cmds_need_lc, $1)=yes
+
+  if test yes,yes = "$GCC,$enable_shared"; then
+    case $_LT_TAGVAR(archive_cmds, $1) in
+    *'~'*)
+      # FIXME: we may have to deal with multi-command sequences.
+      ;;
+    '$CC '*)
+      # Test whether the compiler implicitly links with -lc since on some
+      # systems, -lgcc has to come before -lc. If gcc already passes -lc
+      # to ld, don't add -lc before -lgcc.
+      AC_CACHE_CHECK([whether -lc should be explicitly linked in],
+	[lt_cv_]_LT_TAGVAR(archive_cmds_need_lc, $1),
+	[$RM conftest*
+	echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+
+	if AC_TRY_EVAL(ac_compile) 2>conftest.err; then
+	  soname=conftest
+	  lib=conftest
+	  libobjs=conftest.$ac_objext
+	  deplibs=
+	  wl=$_LT_TAGVAR(lt_prog_compiler_wl, $1)
+	  pic_flag=$_LT_TAGVAR(lt_prog_compiler_pic, $1)
+	  compiler_flags=-v
+	  linker_flags=-v
+	  verstring=
+	  output_objdir=.
+	  libname=conftest
+	  lt_save_allow_undefined_flag=$_LT_TAGVAR(allow_undefined_flag, $1)
+	  _LT_TAGVAR(allow_undefined_flag, $1)=
+	  if AC_TRY_EVAL(_LT_TAGVAR(archive_cmds, $1) 2\>\&1 \| $GREP \" -lc \" \>/dev/null 2\>\&1)
+	  then
+	    lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+	  else
+	    lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)=yes
+	  fi
+	  _LT_TAGVAR(allow_undefined_flag, $1)=$lt_save_allow_undefined_flag
+	else
+	  cat conftest.err 1>&5
+	fi
+	$RM conftest*
+	])
+      _LT_TAGVAR(archive_cmds_need_lc, $1)=$lt_cv_[]_LT_TAGVAR(archive_cmds_need_lc, $1)
+      ;;
+    esac
+  fi
+  ;;
+esac
+
+_LT_TAGDECL([build_libtool_need_lc], [archive_cmds_need_lc], [0],
+    [Whether or not to add -lc for building shared libraries])
+_LT_TAGDECL([allow_libtool_libs_with_static_runtimes],
+    [enable_shared_with_static_runtimes], [0],
+    [Whether or not to disallow shared libs when runtime libs are static])
+_LT_TAGDECL([], [export_dynamic_flag_spec], [1],
+    [Compiler flag to allow reflexive dlopens])
+_LT_TAGDECL([], [whole_archive_flag_spec], [1],
+    [Compiler flag to generate shared objects directly from archives])
+_LT_TAGDECL([], [compiler_needs_object], [1],
+    [Whether the compiler copes with passing no objects directly])
+_LT_TAGDECL([], [old_archive_from_new_cmds], [2],
+    [Create an old-style archive from a shared archive])
+_LT_TAGDECL([], [old_archive_from_expsyms_cmds], [2],
+    [Create a temporary old-style archive to link instead of a shared archive])
+_LT_TAGDECL([], [archive_cmds], [2], [Commands used to build a shared archive])
+_LT_TAGDECL([], [archive_expsym_cmds], [2])
+_LT_TAGDECL([], [module_cmds], [2],
+    [Commands used to build a loadable module if different from building
+    a shared archive.])
+_LT_TAGDECL([], [module_expsym_cmds], [2])
+_LT_TAGDECL([], [with_gnu_ld], [1],
+    [Whether we are building with GNU ld or not])
+_LT_TAGDECL([], [allow_undefined_flag], [1],
+    [Flag that allows shared libraries with undefined symbols to be built])
+_LT_TAGDECL([], [no_undefined_flag], [1],
+    [Flag that enforces no undefined symbols])
+_LT_TAGDECL([], [hardcode_libdir_flag_spec], [1],
+    [Flag to hardcode $libdir into a binary during linking.
+    This must work even if $libdir does not exist])
+_LT_TAGDECL([], [hardcode_libdir_separator], [1],
+    [Whether we need a single "-rpath" flag with a separated argument])
+_LT_TAGDECL([], [hardcode_direct], [0],
+    [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes
+    DIR into the resulting binary])
+_LT_TAGDECL([], [hardcode_direct_absolute], [0],
+    [Set to "yes" if using DIR/libNAME$shared_ext during linking hardcodes
+    DIR into the resulting binary and the resulting library dependency is
+    "absolute", i.e impossible to change by setting $shlibpath_var if the
+    library is relocated])
+_LT_TAGDECL([], [hardcode_minus_L], [0],
+    [Set to "yes" if using the -LDIR flag during linking hardcodes DIR
+    into the resulting binary])
+_LT_TAGDECL([], [hardcode_shlibpath_var], [0],
+    [Set to "yes" if using SHLIBPATH_VAR=DIR during linking hardcodes DIR
+    into the resulting binary])
+_LT_TAGDECL([], [hardcode_automatic], [0],
+    [Set to "yes" if building a shared library automatically hardcodes DIR
+    into the library and all subsequent libraries and executables linked
+    against it])
+_LT_TAGDECL([], [inherit_rpath], [0],
+    [Set to yes if linker adds runtime paths of dependent libraries
+    to runtime path list])
+_LT_TAGDECL([], [link_all_deplibs], [0],
+    [Whether libtool must link a program against all its dependency libraries])
+_LT_TAGDECL([], [always_export_symbols], [0],
+    [Set to "yes" if exported symbols are required])
+_LT_TAGDECL([], [export_symbols_cmds], [2],
+    [The commands to list exported symbols])
+_LT_TAGDECL([], [exclude_expsyms], [1],
+    [Symbols that should not be listed in the preloaded symbols])
+_LT_TAGDECL([], [include_expsyms], [1],
+    [Symbols that must always be exported])
+_LT_TAGDECL([], [prelink_cmds], [2],
+    [Commands necessary for linking programs (against libraries) with templates])
+_LT_TAGDECL([], [postlink_cmds], [2],
+    [Commands necessary for finishing linking programs])
+_LT_TAGDECL([], [file_list_spec], [1],
+    [Specify filename containing input files])
+dnl FIXME: Not yet implemented
+dnl _LT_TAGDECL([], [thread_safe_flag_spec], [1],
+dnl    [Compiler flag to generate thread safe objects])
+])# _LT_LINKER_SHLIBS
+
+
+# _LT_LANG_C_CONFIG([TAG])
+# ------------------------
+# Ensure that the configuration variables for a C compiler are suitably
+# defined.  These variables are subsequently used by _LT_CONFIG to write
+# the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_C_CONFIG],
+[m4_require([_LT_DECL_EGREP])dnl
+lt_save_CC=$CC
+AC_LANG_PUSH(C)
+
+# Source file extension for C test sources.
+ac_ext=c
+
+# Object file extension for compiled C test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="int some_variable = 0;"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='int main(){return(0);}'
+
+_LT_TAG_COMPILER
+# Save the default compiler, since it gets overwritten when the other
+# tags are being tested, and _LT_TAGVAR(compiler, []) is a NOP.
+compiler_DEFAULT=$CC
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+## CAVEAT EMPTOR:
+## There is no encapsulation within the following macros, do not change
+## the running order or otherwise move them around unless you know exactly
+## what you are doing...
+if test -n "$compiler"; then
+  _LT_COMPILER_NO_RTTI($1)
+  _LT_COMPILER_PIC($1)
+  _LT_COMPILER_C_O($1)
+  _LT_COMPILER_FILE_LOCKS($1)
+  _LT_LINKER_SHLIBS($1)
+  _LT_SYS_DYNAMIC_LINKER($1)
+  _LT_LINKER_HARDCODE_LIBPATH($1)
+  LT_SYS_DLOPEN_SELF
+  _LT_CMD_STRIPLIB
+
+  # Report what library types will actually be built
+  AC_MSG_CHECKING([if libtool supports shared libraries])
+  AC_MSG_RESULT([$can_build_shared])
+
+  AC_MSG_CHECKING([whether to build shared libraries])
+  test no = "$can_build_shared" && enable_shared=no
+
+  # On AIX, shared libraries and static libraries use the same namespace, and
+  # are all built from PIC.
+  case $host_os in
+  aix3*)
+    test yes = "$enable_shared" && enable_static=no
+    if test -n "$RANLIB"; then
+      archive_cmds="$archive_cmds~\$RANLIB \$lib"
+      postinstall_cmds='$RANLIB $lib'
+    fi
+    ;;
+
+  aix[[4-9]]*)
+    if test ia64 != "$host_cpu"; then
+      case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in
+      yes,aix,yes) ;;			# shared object as lib.so file only
+      yes,svr4,*) ;;			# shared object as lib.so archive member only
+      yes,*) enable_static=no ;;	# shared object in lib.a archive as well
+      esac
+    fi
+    ;;
+  esac
+  AC_MSG_RESULT([$enable_shared])
+
+  AC_MSG_CHECKING([whether to build static libraries])
+  # Make sure either enable_shared or enable_static is yes.
+  test yes = "$enable_shared" || enable_static=yes
+  AC_MSG_RESULT([$enable_static])
+
+  _LT_CONFIG($1)
+fi
+AC_LANG_POP
+CC=$lt_save_CC
+])# _LT_LANG_C_CONFIG
+
+
+# _LT_LANG_CXX_CONFIG([TAG])
+# --------------------------
+# Ensure that the configuration variables for a C++ compiler are suitably
+# defined.  These variables are subsequently used by _LT_CONFIG to write
+# the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_CXX_CONFIG],
+[m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+m4_require([_LT_DECL_EGREP])dnl
+m4_require([_LT_PATH_MANIFEST_TOOL])dnl
+if test -n "$CXX" && ( test no != "$CXX" &&
+    ( (test g++ = "$CXX" && `g++ -v >/dev/null 2>&1` ) ||
+    (test g++ != "$CXX"))); then
+  AC_PROG_CXXCPP
+else
+  _lt_caught_CXX_error=yes
+fi
+
+AC_LANG_PUSH(C++)
+_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+_LT_TAGVAR(allow_undefined_flag, $1)=
+_LT_TAGVAR(always_export_symbols, $1)=no
+_LT_TAGVAR(archive_expsym_cmds, $1)=
+_LT_TAGVAR(compiler_needs_object, $1)=no
+_LT_TAGVAR(export_dynamic_flag_spec, $1)=
+_LT_TAGVAR(hardcode_direct, $1)=no
+_LT_TAGVAR(hardcode_direct_absolute, $1)=no
+_LT_TAGVAR(hardcode_libdir_flag_spec, $1)=
+_LT_TAGVAR(hardcode_libdir_separator, $1)=
+_LT_TAGVAR(hardcode_minus_L, $1)=no
+_LT_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+_LT_TAGVAR(hardcode_automatic, $1)=no
+_LT_TAGVAR(inherit_rpath, $1)=no
+_LT_TAGVAR(module_cmds, $1)=
+_LT_TAGVAR(module_expsym_cmds, $1)=
+_LT_TAGVAR(link_all_deplibs, $1)=unknown
+_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_TAGVAR(reload_flag, $1)=$reload_flag
+_LT_TAGVAR(reload_cmds, $1)=$reload_cmds
+_LT_TAGVAR(no_undefined_flag, $1)=
+_LT_TAGVAR(whole_archive_flag_spec, $1)=
+_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+
+# Source file extension for C++ test sources.
+ac_ext=cpp
+
+# Object file extension for compiled C++ test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# No sense in running all these tests if we already determined that
+# the CXX compiler isn't working.  Some variables (like enable_shared)
+# are currently assumed to apply to all compilers on this platform,
+# and will be corrupted by setting them based on a non-working compiler.
+if test yes != "$_lt_caught_CXX_error"; then
+  # Code to be used in simple compile tests
+  lt_simple_compile_test_code="int some_variable = 0;"
+
+  # Code to be used in simple link tests
+  lt_simple_link_test_code='int main(int, char *[[]]) { return(0); }'
+
+  # ltmain only uses $CC for tagged configurations so make sure $CC is set.
+  _LT_TAG_COMPILER
+
+  # save warnings/boilerplate of simple test code
+  _LT_COMPILER_BOILERPLATE
+  _LT_LINKER_BOILERPLATE
+
+  # Allow CC to be a program name with arguments.
+  lt_save_CC=$CC
+  lt_save_CFLAGS=$CFLAGS
+  lt_save_LD=$LD
+  lt_save_GCC=$GCC
+  GCC=$GXX
+  lt_save_with_gnu_ld=$with_gnu_ld
+  lt_save_path_LD=$lt_cv_path_LD
+  if test -n "${lt_cv_prog_gnu_ldcxx+set}"; then
+    lt_cv_prog_gnu_ld=$lt_cv_prog_gnu_ldcxx
+  else
+    $as_unset lt_cv_prog_gnu_ld
+  fi
+  if test -n "${lt_cv_path_LDCXX+set}"; then
+    lt_cv_path_LD=$lt_cv_path_LDCXX
+  else
+    $as_unset lt_cv_path_LD
+  fi
+  test -z "${LDCXX+set}" || LD=$LDCXX
+  CC=${CXX-"c++"}
+  CFLAGS=$CXXFLAGS
+  compiler=$CC
+  _LT_TAGVAR(compiler, $1)=$CC
+  _LT_CC_BASENAME([$compiler])
+
+  if test -n "$compiler"; then
+    # We don't want -fno-exception when compiling C++ code, so set the
+    # no_builtin_flag separately
+    if test yes = "$GXX"; then
+      _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin'
+    else
+      _LT_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=
+    fi
+
+    if test yes = "$GXX"; then
+      # Set up default GNU C++ configuration
+
+      LT_PATH_LD
+
+      # Check if GNU C++ uses GNU ld as the underlying linker, since the
+      # archiving commands below assume that GNU ld is being used.
+      if test yes = "$with_gnu_ld"; then
+        _LT_TAGVAR(archive_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib'
+        _LT_TAGVAR(archive_expsym_cmds, $1)='$CC $pic_flag -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+
+        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+        _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+
+        # If archive_cmds runs LD, not CC, wlarc should be empty
+        # XXX I think wlarc can be eliminated in ltcf-cxx, but I need to
+        #     investigate it a little bit more. (MM)
+        wlarc='$wl'
+
+        # ancient GNU ld didn't support --whole-archive et. al.
+        if eval "`$CC -print-prog-name=ld` --help 2>&1" |
+	  $GREP 'no-whole-archive' > /dev/null; then
+          _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive'
+        else
+          _LT_TAGVAR(whole_archive_flag_spec, $1)=
+        fi
+      else
+        with_gnu_ld=no
+        wlarc=
+
+        # A generic and very simple default shared library creation
+        # command for GNU C++ for the case where it uses the native
+        # linker, instead of GNU ld.  If possible, this setting should
+        # overridden to take advantage of the native linker features on
+        # the platform it is being used on.
+        _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib'
+      fi
+
+      # Commands to make compiler produce verbose output that lists
+      # what "hidden" libraries, object files and flags are used when
+      # linking a shared library.
+      output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"'
+
+    else
+      GXX=no
+      with_gnu_ld=no
+      wlarc=
+    fi
+
+    # PORTME: fill in a description of your system's C++ link characteristics
+    AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries])
+    _LT_TAGVAR(ld_shlibs, $1)=yes
+    case $host_os in
+      aix3*)
+        # FIXME: insert proper C++ library support
+        _LT_TAGVAR(ld_shlibs, $1)=no
+        ;;
+      aix[[4-9]]*)
+        if test ia64 = "$host_cpu"; then
+          # On IA64, the linker does run time linking by default, so we don't
+          # have to do anything special.
+          aix_use_runtimelinking=no
+          exp_sym_flag='-Bexport'
+          no_entry_flag=
+        else
+          aix_use_runtimelinking=no
+
+          # Test if we are trying to use run time linking or normal
+          # AIX style linking. If -brtl is somewhere in LDFLAGS, we
+          # have runtime linking enabled, and use it for executables.
+          # For shared libraries, we enable/disable runtime linking
+          # depending on the kind of the shared library created -
+          # when "with_aix_soname,aix_use_runtimelinking" is:
+          # "aix,no"   lib.a(lib.so.V) shared, rtl:no,  for executables
+          # "aix,yes"  lib.so          shared, rtl:yes, for executables
+          #            lib.a           static archive
+          # "both,no"  lib.so.V(shr.o) shared, rtl:yes
+          #            lib.a(lib.so.V) shared, rtl:no,  for executables
+          # "both,yes" lib.so.V(shr.o) shared, rtl:yes, for executables
+          #            lib.a(lib.so.V) shared, rtl:no
+          # "svr4,*"   lib.so.V(shr.o) shared, rtl:yes, for executables
+          #            lib.a           static archive
+          case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*)
+	    for ld_flag in $LDFLAGS; do
+	      case $ld_flag in
+	      *-brtl*)
+	        aix_use_runtimelinking=yes
+	        break
+	        ;;
+	      esac
+	    done
+	    if test svr4,no = "$with_aix_soname,$aix_use_runtimelinking"; then
+	      # With aix-soname=svr4, we create the lib.so.V shared archives only,
+	      # so we don't have lib.a shared libs to link our executables.
+	      # We have to force runtime linking in this case.
+	      aix_use_runtimelinking=yes
+	      LDFLAGS="$LDFLAGS -Wl,-brtl"
+	    fi
+	    ;;
+          esac
+
+          exp_sym_flag='-bexport'
+          no_entry_flag='-bnoentry'
+        fi
+
+        # When large executables or shared objects are built, AIX ld can
+        # have problems creating the table of contents.  If linking a library
+        # or program results in "error TOC overflow" add -mminimal-toc to
+        # CXXFLAGS/CFLAGS for g++/gcc.  In the cases where that is not
+        # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS.
+
+        _LT_TAGVAR(archive_cmds, $1)=''
+        _LT_TAGVAR(hardcode_direct, $1)=yes
+        _LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+        _LT_TAGVAR(hardcode_libdir_separator, $1)=':'
+        _LT_TAGVAR(link_all_deplibs, $1)=yes
+        _LT_TAGVAR(file_list_spec, $1)='$wl-f,'
+        case $with_aix_soname,$aix_use_runtimelinking in
+        aix,*) ;;	# no import file
+        svr4,* | *,yes) # use import file
+          # The Import File defines what to hardcode.
+          _LT_TAGVAR(hardcode_direct, $1)=no
+          _LT_TAGVAR(hardcode_direct_absolute, $1)=no
+          ;;
+        esac
+
+        if test yes = "$GXX"; then
+          case $host_os in aix4.[[012]]|aix4.[[012]].*)
+          # We only want to do this on AIX 4.2 and lower, the check
+          # below for broken collect2 doesn't work under 4.3+
+	  collect2name=`$CC -print-prog-name=collect2`
+	  if test -f "$collect2name" &&
+	     strings "$collect2name" | $GREP resolve_lib_name >/dev/null
+	  then
+	    # We have reworked collect2
+	    :
+	  else
+	    # We have old collect2
+	    _LT_TAGVAR(hardcode_direct, $1)=unsupported
+	    # It fails to find uninstalled libraries when the uninstalled
+	    # path is not listed in the libpath.  Setting hardcode_minus_L
+	    # to unsupported forces relinking
+	    _LT_TAGVAR(hardcode_minus_L, $1)=yes
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+	    _LT_TAGVAR(hardcode_libdir_separator, $1)=
+	  fi
+          esac
+          shared_flag='-shared'
+	  if test yes = "$aix_use_runtimelinking"; then
+	    shared_flag=$shared_flag' $wl-G'
+	  fi
+	  # Need to ensure runtime linking is disabled for the traditional
+	  # shared library, or the linker may eventually find shared libraries
+	  # /with/ Import File - we do not want to mix them.
+	  shared_flag_aix='-shared'
+	  shared_flag_svr4='-shared $wl-G'
+        else
+          # not using gcc
+          if test ia64 = "$host_cpu"; then
+	  # VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release
+	  # chokes on -Wl,-G. The following line is correct:
+	  shared_flag='-G'
+          else
+	    if test yes = "$aix_use_runtimelinking"; then
+	      shared_flag='$wl-G'
+	    else
+	      shared_flag='$wl-bM:SRE'
+	    fi
+	    shared_flag_aix='$wl-bM:SRE'
+	    shared_flag_svr4='$wl-G'
+          fi
+        fi
+
+        _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-bexpall'
+        # It seems that -bexpall does not export symbols beginning with
+        # underscore (_), so it is better to generate a list of symbols to
+	# export.
+        _LT_TAGVAR(always_export_symbols, $1)=yes
+	if test aix,yes = "$with_aix_soname,$aix_use_runtimelinking"; then
+          # Warning - without using the other runtime loading flags (-brtl),
+          # -berok will link without error, but may produce a broken library.
+          # The "-G" linker flag allows undefined symbols.
+          _LT_TAGVAR(no_undefined_flag, $1)='-bernotok'
+          # Determine the default libpath from the value encoded in an empty
+          # executable.
+          _LT_SYS_MODULE_PATH_AIX([$1])
+          _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath"
+
+          _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $deplibs $wl'$no_entry_flag' $compiler_flags `if test -n "$allow_undefined_flag"; then func_echo_all "$wl$allow_undefined_flag"; else :; fi` $wl'$exp_sym_flag:\$export_symbols' '$shared_flag
+        else
+          if test ia64 = "$host_cpu"; then
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $libdir:/usr/lib:/lib'
+	    _LT_TAGVAR(allow_undefined_flag, $1)="-z nodefs"
+	    _LT_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\$wl$no_entry_flag"' $compiler_flags $wl$allow_undefined_flag '"\$wl$exp_sym_flag:\$export_symbols"
+          else
+	    # Determine the default libpath from the value encoded in an
+	    # empty executable.
+	    _LT_SYS_MODULE_PATH_AIX([$1])
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-blibpath:$libdir:'"$aix_libpath"
+	    # Warning - without using the other run time loading flags,
+	    # -berok will link without error, but may produce a broken library.
+	    _LT_TAGVAR(no_undefined_flag, $1)=' $wl-bernotok'
+	    _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-berok'
+	    if test yes = "$with_gnu_ld"; then
+	      # We only use this code for GNU lds that support --whole-archive.
+	      _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive'
+	    else
+	      # Exported symbols can be pulled into shared objects from archives
+	      _LT_TAGVAR(whole_archive_flag_spec, $1)='$convenience'
+	    fi
+	    _LT_TAGVAR(archive_cmds_need_lc, $1)=yes
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$RM -r $output_objdir/$realname.d~$MKDIR $output_objdir/$realname.d'
+	    # -brtl affects multiple linker settings, -berok does not and is overridden later
+	    compiler_flags_filtered='`func_echo_all "$compiler_flags " | $SED -e "s%-brtl\\([[, ]]\\)%-berok\\1%g"`'
+	    if test svr4 != "$with_aix_soname"; then
+	      # This is similar to how AIX traditionally builds its shared
+	      # libraries. Need -bnortl late, we may have -brtl in LDFLAGS.
+	      _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_aix' -o $output_objdir/$realname.d/$soname $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$realname.d/$soname'
+	    fi
+	    if test aix != "$with_aix_soname"; then
+	      _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$CC '$shared_flag_svr4' -o $output_objdir/$realname.d/$shared_archive_member_spec.o $libobjs $deplibs $wl-bnoentry '$compiler_flags_filtered'$wl-bE:$export_symbols$allow_undefined_flag~$STRIP -e $output_objdir/$realname.d/$shared_archive_member_spec.o~( func_echo_all "#! $soname($shared_archive_member_spec.o)"; if test shr_64 = "$shared_archive_member_spec"; then func_echo_all "# 64"; else func_echo_all "# 32"; fi; cat $export_symbols ) > $output_objdir/$realname.d/$shared_archive_member_spec.imp~$AR $AR_FLAGS $output_objdir/$soname $output_objdir/$realname.d/$shared_archive_member_spec.o $output_objdir/$realname.d/$shared_archive_member_spec.imp'
+	    else
+	      # used by -dlpreopen to get the symbols
+	      _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$MV  $output_objdir/$realname.d/$soname $output_objdir'
+	    fi
+	    _LT_TAGVAR(archive_expsym_cmds, $1)="$_LT_TAGVAR(archive_expsym_cmds, $1)"'~$RM -r $output_objdir/$realname.d'
+          fi
+        fi
+        ;;
+
+      beos*)
+	if $LD --help 2>&1 | $GREP ': supported targets:.* elf' > /dev/null; then
+	  _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	  # Joseph Beckenbach <jrb3@best.com> says some releases of gcc
+	  # support --undefined.  This deserves some investigation.  FIXME
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	else
+	  _LT_TAGVAR(ld_shlibs, $1)=no
+	fi
+	;;
+
+      chorus*)
+        case $cc_basename in
+          *)
+	  # FIXME: insert proper C++ library support
+	  _LT_TAGVAR(ld_shlibs, $1)=no
+	  ;;
+        esac
+        ;;
+
+      cygwin* | mingw* | pw32* | cegcc*)
+	case $GXX,$cc_basename in
+	,cl* | no,cl*)
+	  # Native MSVC
+	  # hardcode_libdir_flag_spec is actually meaningless, as there is
+	  # no search path for DLLs.
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)=' '
+	  _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	  _LT_TAGVAR(always_export_symbols, $1)=yes
+	  _LT_TAGVAR(file_list_spec, $1)='@'
+	  # Tell ltmain to make .lib files, not .a files.
+	  libext=lib
+	  # Tell ltmain to make .dll files, not .so files.
+	  shrext_cmds=.dll
+	  # FIXME: Setting linknames here is a bad hack.
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -o $output_objdir/$soname $libobjs $compiler_flags $deplibs -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~linknames='
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then
+              cp "$export_symbols" "$output_objdir/$soname.def";
+              echo "$tool_output_objdir$soname.def" > "$output_objdir/$soname.exp";
+            else
+              $SED -e '\''s/^/-link -EXPORT:/'\'' < $export_symbols > $output_objdir/$soname.exp;
+            fi~
+            $CC -o $tool_output_objdir$soname $libobjs $compiler_flags $deplibs "@$tool_output_objdir$soname.exp" -Wl,-DLL,-IMPLIB:"$tool_output_objdir$libname.dll.lib"~
+            linknames='
+	  # The linker will not automatically build a static lib if we build a DLL.
+	  # _LT_TAGVAR(old_archive_from_new_cmds, $1)='true'
+	  _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+	  # Don't use ranlib
+	  _LT_TAGVAR(old_postinstall_cmds, $1)='chmod 644 $oldlib'
+	  _LT_TAGVAR(postlink_cmds, $1)='lt_outputfile="@OUTPUT@"~
+            lt_tool_outputfile="@TOOL_OUTPUT@"~
+            case $lt_outputfile in
+              *.exe|*.EXE) ;;
+              *)
+                lt_outputfile=$lt_outputfile.exe
+                lt_tool_outputfile=$lt_tool_outputfile.exe
+                ;;
+            esac~
+            func_to_tool_file "$lt_outputfile"~
+            if test : != "$MANIFEST_TOOL" && test -f "$lt_outputfile.manifest"; then
+              $MANIFEST_TOOL -manifest "$lt_tool_outputfile.manifest" -outputresource:"$lt_tool_outputfile" || exit 1;
+              $RM "$lt_outputfile.manifest";
+            fi'
+	  ;;
+	*)
+	  # g++
+	  # _LT_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless,
+	  # as there is no search path for DLLs.
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+	  _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-all-symbols'
+	  _LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	  _LT_TAGVAR(always_export_symbols, $1)=no
+	  _LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+
+	  if $LD --help 2>&1 | $GREP 'auto-import' > /dev/null; then
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+	    # If the export-symbols file already is a .def file, use it as
+	    # is; otherwise, prepend EXPORTS...
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='if _LT_DLL_DEF_P([$export_symbols]); then
+              cp $export_symbols $output_objdir/$soname.def;
+            else
+              echo EXPORTS > $output_objdir/$soname.def;
+              cat $export_symbols >> $output_objdir/$soname.def;
+            fi~
+            $CC -shared -nostdlib $output_objdir/$soname.def $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname $wl--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+	  else
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	  fi
+	  ;;
+	esac
+	;;
+      darwin* | rhapsody*)
+        _LT_DARWIN_LINKER_FEATURES($1)
+	;;
+
+      os2*)
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+	_LT_TAGVAR(hardcode_minus_L, $1)=yes
+	_LT_TAGVAR(allow_undefined_flag, $1)=unsupported
+	shrext_cmds=.dll
+	_LT_TAGVAR(archive_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	  $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	  $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	  $ECHO EXPORTS >> $output_objdir/$libname.def~
+	  emxexp $libobjs | $SED /"_DLL_InitTerm"/d >> $output_objdir/$libname.def~
+	  $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	  emximp -o $lib $output_objdir/$libname.def'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='$ECHO "LIBRARY ${soname%$shared_ext} INITINSTANCE TERMINSTANCE" > $output_objdir/$libname.def~
+	  $ECHO "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~
+	  $ECHO "DATA MULTIPLE NONSHARED" >> $output_objdir/$libname.def~
+	  $ECHO EXPORTS >> $output_objdir/$libname.def~
+	  prefix_cmds="$SED"~
+	  if test EXPORTS = "`$SED 1q $export_symbols`"; then
+	    prefix_cmds="$prefix_cmds -e 1d";
+	  fi~
+	  prefix_cmds="$prefix_cmds -e \"s/^\(.*\)$/_\1/g\""~
+	  cat $export_symbols | $prefix_cmds >> $output_objdir/$libname.def~
+	  $CC -Zdll -Zcrtdll -o $output_objdir/$soname $libobjs $deplibs $compiler_flags $output_objdir/$libname.def~
+	  emximp -o $lib $output_objdir/$libname.def'
+	_LT_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/${libname}_dll.a $output_objdir/$libname.def'
+	_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+	;;
+
+      dgux*)
+        case $cc_basename in
+          ec++*)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          ghcx*)
+	    # Green Hills C++ Compiler
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          *)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+        esac
+        ;;
+
+      freebsd2.*)
+        # C++ shared libraries reported to be fairly broken before
+	# switch to ELF
+        _LT_TAGVAR(ld_shlibs, $1)=no
+        ;;
+
+      freebsd-elf*)
+        _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+        ;;
+
+      freebsd* | dragonfly*)
+        # FreeBSD 3 and later use GNU C++ and GNU ld with standard ELF
+        # conventions
+        _LT_TAGVAR(ld_shlibs, $1)=yes
+        ;;
+
+      haiku*)
+        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+        _LT_TAGVAR(link_all_deplibs, $1)=yes
+        ;;
+
+      hpux9*)
+        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir'
+        _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+        _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+        _LT_TAGVAR(hardcode_direct, $1)=yes
+        _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH,
+				             # but as the default
+				             # location of the library.
+
+        case $cc_basename in
+          CC*)
+            # FIXME: insert proper C++ library support
+            _LT_TAGVAR(ld_shlibs, $1)=no
+            ;;
+          aCC*)
+            _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -b $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib'
+            # Commands to make compiler produce verbose output that lists
+            # what "hidden" libraries, object files and flags are used when
+            # linking a shared library.
+            #
+            # There doesn't appear to be a way to prevent this compiler from
+            # explicitly linking system object files so we need to strip them
+            # from the output so that they don't get included in the library
+            # dependencies.
+            output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $EGREP "\-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"'
+            ;;
+          *)
+            if test yes = "$GXX"; then
+              _LT_TAGVAR(archive_cmds, $1)='$RM $output_objdir/$soname~$CC -shared -nostdlib $pic_flag $wl+b $wl$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test "x$output_objdir/$soname" = "x$lib" || mv $output_objdir/$soname $lib'
+            else
+              # FIXME: insert proper C++ library support
+              _LT_TAGVAR(ld_shlibs, $1)=no
+            fi
+            ;;
+        esac
+        ;;
+
+      hpux10*|hpux11*)
+        if test no = "$with_gnu_ld"; then
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl+b $wl$libdir'
+	  _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+          case $host_cpu in
+            hppa*64*|ia64*)
+              ;;
+            *)
+	      _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+              ;;
+          esac
+        fi
+        case $host_cpu in
+          hppa*64*|ia64*)
+            _LT_TAGVAR(hardcode_direct, $1)=no
+            _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+            ;;
+          *)
+            _LT_TAGVAR(hardcode_direct, $1)=yes
+            _LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+            _LT_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH,
+					         # but as the default
+					         # location of the library.
+            ;;
+        esac
+
+        case $cc_basename in
+          CC*)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          aCC*)
+	    case $host_cpu in
+	      hppa*64*)
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	        ;;
+	      ia64*)
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	        ;;
+	      *)
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -b $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	        ;;
+	    esac
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    #
+	    # There doesn't appear to be a way to prevent this compiler from
+	    # explicitly linking system object files so we need to strip them
+	    # from the output so that they don't get included in the library
+	    # dependencies.
+	    output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | $GREP "\-L"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"'
+	    ;;
+          *)
+	    if test yes = "$GXX"; then
+	      if test no = "$with_gnu_ld"; then
+	        case $host_cpu in
+	          hppa*64*)
+	            _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC $wl+h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	            ;;
+	          ia64*)
+	            _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	            ;;
+	          *)
+	            _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $pic_flag $wl+h $wl$soname $wl+b $wl$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	            ;;
+	        esac
+	      fi
+	    else
+	      # FIXME: insert proper C++ library support
+	      _LT_TAGVAR(ld_shlibs, $1)=no
+	    fi
+	    ;;
+        esac
+        ;;
+
+      interix[[3-9]]*)
+	_LT_TAGVAR(hardcode_direct, $1)=no
+	_LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	_LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+	# Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc.
+	# Instead, shared libraries are loaded at an image base (0x10000000 by
+	# default) and relocated if they conflict, which is a slow very memory
+	# consuming and fragmenting process.  To avoid this, we pick a random,
+	# 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link
+	# time.  Moving up from 0x10000000 also allows more sbrk(2) space.
+	_LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+	_LT_TAGVAR(archive_expsym_cmds, $1)='sed "s|^|_|" $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags $wl-h,$soname $wl--retain-symbols-file,$output_objdir/$soname.expsym $wl--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+	;;
+      irix5* | irix6*)
+        case $cc_basename in
+          CC*)
+	    # SGI C++
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared -all -multigot $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+
+	    # Archives containing C++ object files must be created using
+	    # "CC -ar", where "CC" is the IRIX C++ compiler.  This is
+	    # necessary to make sure instantiated templates are included
+	    # in the archive.
+	    _LT_TAGVAR(old_archive_cmds, $1)='$CC -ar -WR,-u -o $oldlib $oldobjs'
+	    ;;
+          *)
+	    if test yes = "$GXX"; then
+	      if test no = "$with_gnu_ld"; then
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+	      else
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` -o $lib'
+	      fi
+	    fi
+	    _LT_TAGVAR(link_all_deplibs, $1)=yes
+	    ;;
+        esac
+        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+        _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+        _LT_TAGVAR(inherit_rpath, $1)=yes
+        ;;
+
+      linux* | k*bsd*-gnu | kopensolaris*-gnu | gnu*)
+        case $cc_basename in
+          KCC*)
+	    # Kuck and Associates, Inc. (KAI) C++ Compiler
+
+	    # KCC will only create a shared library if the output file
+	    # ends with ".so" (or ".sl" for HP-UX), so rename the library
+	    # to its proper name (with version) after linking.
+	    _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib $wl-retain-symbols-file,$export_symbols; mv \$templib $lib'
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    #
+	    # There doesn't appear to be a way to prevent this compiler from
+	    # explicitly linking system object files so we need to strip them
+	    # from the output so that they don't get included in the library
+	    # dependencies.
+	    output_verbose_link_cmd='templist=`$CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 | $GREP "ld"`; rm -f libconftest$shared_ext; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"'
+
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+
+	    # Archives containing C++ object files must be created using
+	    # "CC -Bstatic", where "CC" is the KAI C++ compiler.
+	    _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs'
+	    ;;
+	  icpc* | ecpc* )
+	    # Intel C++
+	    with_gnu_ld=yes
+	    # version 8.0 and above of icpc choke on multiply defined symbols
+	    # if we add $predep_objects and $postdep_objects, however 7.1 and
+	    # earlier do not add the objects themselves.
+	    case `$CC -V 2>&1` in
+	      *"Version 7."*)
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib'
+		_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+		;;
+	      *)  # Version 8.0 or newer
+	        tmp_idyn=
+	        case $host_cpu in
+		  ia64*) tmp_idyn=' -i_dynamic';;
+		esac
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+		_LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+		;;
+	    esac
+	    _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+	    _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive$convenience $wl--no-whole-archive'
+	    ;;
+          pgCC* | pgcpp*)
+            # Portland Group C++ compiler
+	    case `$CC -V` in
+	    *pgCC\ [[1-5]].* | *pgcpp\ [[1-5]].*)
+	      _LT_TAGVAR(prelink_cmds, $1)='tpldir=Template.dir~
+               rm -rf $tpldir~
+               $CC --prelink_objects --instantiation_dir $tpldir $objs $libobjs $compile_deplibs~
+               compile_command="$compile_command `find $tpldir -name \*.o | sort | $NL2SP`"'
+	      _LT_TAGVAR(old_archive_cmds, $1)='tpldir=Template.dir~
+                rm -rf $tpldir~
+                $CC --prelink_objects --instantiation_dir $tpldir $oldobjs$old_deplibs~
+                $AR $AR_FLAGS $oldlib$oldobjs$old_deplibs `find $tpldir -name \*.o | sort | $NL2SP`~
+                $RANLIB $oldlib'
+	      _LT_TAGVAR(archive_cmds, $1)='tpldir=Template.dir~
+                rm -rf $tpldir~
+                $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~
+                $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib'
+	      _LT_TAGVAR(archive_expsym_cmds, $1)='tpldir=Template.dir~
+                rm -rf $tpldir~
+                $CC --prelink_objects --instantiation_dir $tpldir $predep_objects $libobjs $deplibs $convenience $postdep_objects~
+                $CC -shared $pic_flag $predep_objects $libobjs $deplibs `find $tpldir -name \*.o | sort | $NL2SP` $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+	      ;;
+	    *) # Version 6 and above use weak symbols
+	      _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib'
+	      _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname $wl-retain-symbols-file $wl$export_symbols -o $lib'
+	      ;;
+	    esac
+
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl--rpath $wl$libdir'
+	    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+	    _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+            ;;
+	  cxx*)
+	    # Compaq C++
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname -o $lib'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname  -o $lib $wl-retain-symbols-file $wl$export_symbols'
+
+	    runpath_var=LD_RUN_PATH
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+	    _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    #
+	    # There doesn't appear to be a way to prevent this compiler from
+	    # explicitly linking system object files so we need to strip them
+	    # from the output so that they don't get included in the library
+	    # dependencies.
+	    output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld .*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "X$list" | $Xsed'
+	    ;;
+	  xl* | mpixl* | bgxl*)
+	    # IBM XL 8.0 on PPC, with GNU ld
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+	    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl--export-dynamic'
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname -o $lib'
+	    if test yes = "$supports_anon_versioning"; then
+	      _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $output_objdir/$libname.ver~
+                cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~
+                echo "local: *; };" >> $output_objdir/$libname.ver~
+                $CC -qmkshrobj $libobjs $deplibs $compiler_flags $wl-soname $wl$soname $wl-version-script $wl$output_objdir/$libname.ver -o $lib'
+	    fi
+	    ;;
+	  *)
+	    case `$CC -V 2>&1 | sed 5q` in
+	    *Sun\ C*)
+	      # Sun C++ 5.9
+	      _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs'
+	      _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	      _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file $wl$export_symbols'
+	      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	      _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; func_echo_all \"$new_convenience\"` $wl--no-whole-archive'
+	      _LT_TAGVAR(compiler_needs_object, $1)=yes
+
+	      # Not sure whether something based on
+	      # $CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1
+	      # would be better.
+	      output_verbose_link_cmd='func_echo_all'
+
+	      # Archives containing C++ object files must be created using
+	      # "CC -xar", where "CC" is the Sun C++ compiler.  This is
+	      # necessary to make sure instantiated templates are included
+	      # in the archive.
+	      _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs'
+	      ;;
+	    esac
+	    ;;
+	esac
+	;;
+
+      lynxos*)
+        # FIXME: insert proper C++ library support
+	_LT_TAGVAR(ld_shlibs, $1)=no
+	;;
+
+      m88k*)
+        # FIXME: insert proper C++ library support
+        _LT_TAGVAR(ld_shlibs, $1)=no
+	;;
+
+      mvs*)
+        case $cc_basename in
+          cxx*)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+	  *)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+	esac
+	;;
+
+      netbsd*)
+        if echo __ELF__ | $CC -E - | $GREP __ELF__ >/dev/null; then
+	  _LT_TAGVAR(archive_cmds, $1)='$LD -Bshareable  -o $lib $predep_objects $libobjs $deplibs $postdep_objects $linker_flags'
+	  wlarc=
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	  _LT_TAGVAR(hardcode_direct, $1)=yes
+	  _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	fi
+	# Workaround some broken pre-1.5 toolchains
+	output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP conftest.$objext | $SED -e "s:-lgcc -lc -lgcc::"'
+	;;
+
+      *nto* | *qnx*)
+        _LT_TAGVAR(ld_shlibs, $1)=yes
+	;;
+
+      openbsd* | bitrig*)
+	if test -f /usr/libexec/ld.so; then
+	  _LT_TAGVAR(hardcode_direct, $1)=yes
+	  _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	  _LT_TAGVAR(hardcode_direct_absolute, $1)=yes
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib'
+	  _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	  if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`"; then
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-retain-symbols-file,$export_symbols -o $lib'
+	    _LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-E'
+	    _LT_TAGVAR(whole_archive_flag_spec, $1)=$wlarc'--whole-archive$convenience '$wlarc'--no-whole-archive'
+	  fi
+	  output_verbose_link_cmd=func_echo_all
+	else
+	  _LT_TAGVAR(ld_shlibs, $1)=no
+	fi
+	;;
+
+      osf3* | osf4* | osf5*)
+        case $cc_basename in
+          KCC*)
+	    # Kuck and Associates, Inc. (KAI) C++ Compiler
+
+	    # KCC will only create a shared library if the output file
+	    # ends with ".so" (or ".sl" for HP-UX), so rename the library
+	    # to its proper name (with version) after linking.
+	    _LT_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo "$lib" | $SED -e "s/\$tempext\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib'
+
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath,$libdir'
+	    _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	    # Archives containing C++ object files must be created using
+	    # the KAI C++ compiler.
+	    case $host in
+	      osf3*) _LT_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs' ;;
+	      *) _LT_TAGVAR(old_archive_cmds, $1)='$CC -o $oldlib $oldobjs' ;;
+	    esac
+	    ;;
+          RCC*)
+	    # Rational C++ 2.4.1
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          cxx*)
+	    case $host in
+	      osf3*)
+	        _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*'
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $soname `test -n "$verstring" && func_echo_all "$wl-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+	        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+		;;
+	      *)
+	        _LT_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname `test -n "$verstring" && func_echo_all "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib'
+	        _LT_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done~
+                  echo "-hidden">> $lib.exp~
+                  $CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname $wl-input $wl$lib.exp  `test -n "$verstring" && $ECHO "-set_version $verstring"` -update_registry $output_objdir/so_locations -o $lib~
+                  $RM $lib.exp'
+	        _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+		;;
+	    esac
+
+	    _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    #
+	    # There doesn't appear to be a way to prevent this compiler from
+	    # explicitly linking system object files so we need to strip them
+	    # from the output so that they don't get included in the library
+	    # dependencies.
+	    output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP "ld" | $GREP -v "ld:"`; templist=`func_echo_all "$templist" | $SED "s/\(^.*ld.*\)\( .*ld.*$\)/\1/"`; list= ; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; func_echo_all "$list"'
+	    ;;
+	  *)
+	    if test yes,no = "$GXX,$with_gnu_ld"; then
+	      _LT_TAGVAR(allow_undefined_flag, $1)=' $wl-expect_unresolved $wl\*'
+	      case $host in
+	        osf3*)
+	          _LT_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+		  ;;
+	        *)
+	          _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-msym $wl-soname $wl$soname `test -n "$verstring" && func_echo_all "$wl-set_version $wl$verstring"` $wl-update_registry $wl$output_objdir/so_locations -o $lib'
+		  ;;
+	      esac
+
+	      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-rpath $wl$libdir'
+	      _LT_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	      # Commands to make compiler produce verbose output that lists
+	      # what "hidden" libraries, object files and flags are used when
+	      # linking a shared library.
+	      output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"'
+
+	    else
+	      # FIXME: insert proper C++ library support
+	      _LT_TAGVAR(ld_shlibs, $1)=no
+	    fi
+	    ;;
+        esac
+        ;;
+
+      psos*)
+        # FIXME: insert proper C++ library support
+        _LT_TAGVAR(ld_shlibs, $1)=no
+        ;;
+
+      sunos4*)
+        case $cc_basename in
+          CC*)
+	    # Sun C++ 4.x
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          lcc*)
+	    # Lucid
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          *)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+        esac
+        ;;
+
+      solaris*)
+        case $cc_basename in
+          CC* | sunCC*)
+	    # Sun C++ 4.2, 5.x and Centerline C++
+            _LT_TAGVAR(archive_cmds_need_lc,$1)=yes
+	    _LT_TAGVAR(no_undefined_flag, $1)=' -zdefs'
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -G$allow_undefined_flag -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+              $CC -G$allow_undefined_flag $wl-M $wl$lib.exp -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp'
+
+	    _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	    _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	    case $host_os in
+	      solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+	      *)
+		# The compiler driver will combine and reorder linker options,
+		# but understands '-z linker_flag'.
+	        # Supported since Solaris 2.6 (maybe 2.5.1?)
+		_LT_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract'
+	        ;;
+	    esac
+	    _LT_TAGVAR(link_all_deplibs, $1)=yes
+
+	    output_verbose_link_cmd='func_echo_all'
+
+	    # Archives containing C++ object files must be created using
+	    # "CC -xar", where "CC" is the Sun C++ compiler.  This is
+	    # necessary to make sure instantiated templates are included
+	    # in the archive.
+	    _LT_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs'
+	    ;;
+          gcx*)
+	    # Green Hills C++ Compiler
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib'
+
+	    # The C++ compiler must be used to create the archive.
+	    _LT_TAGVAR(old_archive_cmds, $1)='$CC $LDFLAGS -archive -o $oldlib $oldobjs'
+	    ;;
+          *)
+	    # GNU C++ compiler with Solaris linker
+	    if test yes,no = "$GXX,$with_gnu_ld"; then
+	      _LT_TAGVAR(no_undefined_flag, $1)=' $wl-z ${wl}defs'
+	      if $CC --version | $GREP -v '^2\.7' > /dev/null; then
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib'
+	        _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+                  $CC -shared $pic_flag -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp'
+
+	        # Commands to make compiler produce verbose output that lists
+	        # what "hidden" libraries, object files and flags are used when
+	        # linking a shared library.
+	        output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"'
+	      else
+	        # g++ 2.7 appears to require '-G' NOT '-shared' on this
+	        # platform.
+	        _LT_TAGVAR(archive_cmds, $1)='$CC -G -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags $wl-h $wl$soname -o $lib'
+	        _LT_TAGVAR(archive_expsym_cmds, $1)='echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~echo "local: *; };" >> $lib.exp~
+                  $CC -G -nostdlib $wl-M $wl$lib.exp $wl-h $wl$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$RM $lib.exp'
+
+	        # Commands to make compiler produce verbose output that lists
+	        # what "hidden" libraries, object files and flags are used when
+	        # linking a shared library.
+	        output_verbose_link_cmd='$CC -G $CFLAGS -v conftest.$objext 2>&1 | $GREP -v "^Configured with:" | $GREP "\-L"'
+	      fi
+
+	      _LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R $wl$libdir'
+	      case $host_os in
+		solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+		*)
+		  _LT_TAGVAR(whole_archive_flag_spec, $1)='$wl-z ${wl}allextract$convenience $wl-z ${wl}defaultextract'
+		  ;;
+	      esac
+	    fi
+	    ;;
+        esac
+        ;;
+
+    sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*)
+      _LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text'
+      _LT_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+      runpath_var='LD_RUN_PATH'
+
+      case $cc_basename in
+        CC*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	*)
+	  _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+      esac
+      ;;
+
+      sysv5* | sco3.2v5* | sco5v6*)
+	# Note: We CANNOT use -z defs as we might desire, because we do not
+	# link with -lc, and that would cause any symbols used from libc to
+	# always be unresolved, which means just about no library would
+	# ever link correctly.  If we're not using GNU ld we use -z text
+	# though, which does catch some bad symbols but isn't as heavy-handed
+	# as -z defs.
+	_LT_TAGVAR(no_undefined_flag, $1)='$wl-z,text'
+	_LT_TAGVAR(allow_undefined_flag, $1)='$wl-z,nodefs'
+	_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+	_LT_TAGVAR(hardcode_shlibpath_var, $1)=no
+	_LT_TAGVAR(hardcode_libdir_flag_spec, $1)='$wl-R,$libdir'
+	_LT_TAGVAR(hardcode_libdir_separator, $1)=':'
+	_LT_TAGVAR(link_all_deplibs, $1)=yes
+	_LT_TAGVAR(export_dynamic_flag_spec, $1)='$wl-Bexport'
+	runpath_var='LD_RUN_PATH'
+
+	case $cc_basename in
+          CC*)
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -G $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -G $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	    _LT_TAGVAR(old_archive_cmds, $1)='$CC -Tprelink_objects $oldobjs~
+              '"$_LT_TAGVAR(old_archive_cmds, $1)"
+	    _LT_TAGVAR(reload_cmds, $1)='$CC -Tprelink_objects $reload_objs~
+              '"$_LT_TAGVAR(reload_cmds, $1)"
+	    ;;
+	  *)
+	    _LT_TAGVAR(archive_cmds, $1)='$CC -shared $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	    _LT_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $wl-Bexport:$export_symbols $wl-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	    ;;
+	esac
+      ;;
+
+      tandem*)
+        case $cc_basename in
+          NCC*)
+	    # NonStop-UX NCC 3.20
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+          *)
+	    # FIXME: insert proper C++ library support
+	    _LT_TAGVAR(ld_shlibs, $1)=no
+	    ;;
+        esac
+        ;;
+
+      vxworks*)
+        # FIXME: insert proper C++ library support
+        _LT_TAGVAR(ld_shlibs, $1)=no
+        ;;
+
+      *)
+        # FIXME: insert proper C++ library support
+        _LT_TAGVAR(ld_shlibs, $1)=no
+        ;;
+    esac
+
+    AC_MSG_RESULT([$_LT_TAGVAR(ld_shlibs, $1)])
+    test no = "$_LT_TAGVAR(ld_shlibs, $1)" && can_build_shared=no
+
+    _LT_TAGVAR(GCC, $1)=$GXX
+    _LT_TAGVAR(LD, $1)=$LD
+
+    ## CAVEAT EMPTOR:
+    ## There is no encapsulation within the following macros, do not change
+    ## the running order or otherwise move them around unless you know exactly
+    ## what you are doing...
+    _LT_SYS_HIDDEN_LIBDEPS($1)
+    _LT_COMPILER_PIC($1)
+    _LT_COMPILER_C_O($1)
+    _LT_COMPILER_FILE_LOCKS($1)
+    _LT_LINKER_SHLIBS($1)
+    _LT_SYS_DYNAMIC_LINKER($1)
+    _LT_LINKER_HARDCODE_LIBPATH($1)
+
+    _LT_CONFIG($1)
+  fi # test -n "$compiler"
+
+  CC=$lt_save_CC
+  CFLAGS=$lt_save_CFLAGS
+  LDCXX=$LD
+  LD=$lt_save_LD
+  GCC=$lt_save_GCC
+  with_gnu_ld=$lt_save_with_gnu_ld
+  lt_cv_path_LDCXX=$lt_cv_path_LD
+  lt_cv_path_LD=$lt_save_path_LD
+  lt_cv_prog_gnu_ldcxx=$lt_cv_prog_gnu_ld
+  lt_cv_prog_gnu_ld=$lt_save_with_gnu_ld
+fi # test yes != "$_lt_caught_CXX_error"
+
+AC_LANG_POP
+])# _LT_LANG_CXX_CONFIG
+
+
+# _LT_FUNC_STRIPNAME_CNF
+# ----------------------
+# func_stripname_cnf prefix suffix name
+# strip PREFIX and SUFFIX off of NAME.
+# PREFIX and SUFFIX must not contain globbing or regex special
+# characters, hashes, percent signs, but SUFFIX may contain a leading
+# dot (in which case that matches only a dot).
+#
+# This function is identical to the (non-XSI) version of func_stripname,
+# except this one can be used by m4 code that may be executed by configure,
+# rather than the libtool script.
+m4_defun([_LT_FUNC_STRIPNAME_CNF],[dnl
+AC_REQUIRE([_LT_DECL_SED])
+AC_REQUIRE([_LT_PROG_ECHO_BACKSLASH])
+func_stripname_cnf ()
+{
+  case @S|@2 in
+  .*) func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%\\\\@S|@2\$%%"`;;
+  *)  func_stripname_result=`$ECHO "@S|@3" | $SED "s%^@S|@1%%; s%@S|@2\$%%"`;;
+  esac
+} # func_stripname_cnf
+])# _LT_FUNC_STRIPNAME_CNF
+
+
+# _LT_SYS_HIDDEN_LIBDEPS([TAGNAME])
+# ---------------------------------
+# Figure out "hidden" library dependencies from verbose
+# compiler output when linking a shared library.
+# Parse the compiler output and extract the necessary
+# objects, libraries and library flags.
+m4_defun([_LT_SYS_HIDDEN_LIBDEPS],
+[m4_require([_LT_FILEUTILS_DEFAULTS])dnl
+AC_REQUIRE([_LT_FUNC_STRIPNAME_CNF])dnl
+# Dependencies to place before and after the object being linked:
+_LT_TAGVAR(predep_objects, $1)=
+_LT_TAGVAR(postdep_objects, $1)=
+_LT_TAGVAR(predeps, $1)=
+_LT_TAGVAR(postdeps, $1)=
+_LT_TAGVAR(compiler_lib_search_path, $1)=
+
+dnl we can't use the lt_simple_compile_test_code here,
+dnl because it contains code intended for an executable,
+dnl not a library.  It's possible we should let each
+dnl tag define a new lt_????_link_test_code variable,
+dnl but it's only used here...
+m4_if([$1], [], [cat > conftest.$ac_ext <<_LT_EOF
+int a;
+void foo (void) { a = 0; }
+_LT_EOF
+], [$1], [CXX], [cat > conftest.$ac_ext <<_LT_EOF
+class Foo
+{
+public:
+  Foo (void) { a = 0; }
+private:
+  int a;
+};
+_LT_EOF
+], [$1], [F77], [cat > conftest.$ac_ext <<_LT_EOF
+      subroutine foo
+      implicit none
+      integer*4 a
+      a=0
+      return
+      end
+_LT_EOF
+], [$1], [FC], [cat > conftest.$ac_ext <<_LT_EOF
+      subroutine foo
+      implicit none
+      integer a
+      a=0
+      return
+      end
+_LT_EOF
+], [$1], [GCJ], [cat > conftest.$ac_ext <<_LT_EOF
+public class foo {
+  private int a;
+  public void bar (void) {
+    a = 0;
+  }
+};
+_LT_EOF
+], [$1], [GO], [cat > conftest.$ac_ext <<_LT_EOF
+package foo
+func foo() {
+}
+_LT_EOF
+])
+
+_lt_libdeps_save_CFLAGS=$CFLAGS
+case "$CC $CFLAGS " in #(
+*\ -flto*\ *) CFLAGS="$CFLAGS -fno-lto" ;;
+*\ -fwhopr*\ *) CFLAGS="$CFLAGS -fno-whopr" ;;
+*\ -fuse-linker-plugin*\ *) CFLAGS="$CFLAGS -fno-use-linker-plugin" ;;
+esac
+
+dnl Parse the compiler output and extract the necessary
+dnl objects, libraries and library flags.
+if AC_TRY_EVAL(ac_compile); then
+  # Parse the compiler output and extract the necessary
+  # objects, libraries and library flags.
+
+  # Sentinel used to keep track of whether or not we are before
+  # the conftest object file.
+  pre_test_object_deps_done=no
+
+  for p in `eval "$output_verbose_link_cmd"`; do
+    case $prev$p in
+
+    -L* | -R* | -l*)
+       # Some compilers place space between "-{L,R}" and the path.
+       # Remove the space.
+       if test x-L = "$p" ||
+          test x-R = "$p"; then
+	 prev=$p
+	 continue
+       fi
+
+       # Expand the sysroot to ease extracting the directories later.
+       if test -z "$prev"; then
+         case $p in
+         -L*) func_stripname_cnf '-L' '' "$p"; prev=-L; p=$func_stripname_result ;;
+         -R*) func_stripname_cnf '-R' '' "$p"; prev=-R; p=$func_stripname_result ;;
+         -l*) func_stripname_cnf '-l' '' "$p"; prev=-l; p=$func_stripname_result ;;
+         esac
+       fi
+       case $p in
+       =*) func_stripname_cnf '=' '' "$p"; p=$lt_sysroot$func_stripname_result ;;
+       esac
+       if test no = "$pre_test_object_deps_done"; then
+	 case $prev in
+	 -L | -R)
+	   # Internal compiler library paths should come after those
+	   # provided the user.  The postdeps already come after the
+	   # user supplied libs so there is no need to process them.
+	   if test -z "$_LT_TAGVAR(compiler_lib_search_path, $1)"; then
+	     _LT_TAGVAR(compiler_lib_search_path, $1)=$prev$p
+	   else
+	     _LT_TAGVAR(compiler_lib_search_path, $1)="${_LT_TAGVAR(compiler_lib_search_path, $1)} $prev$p"
+	   fi
+	   ;;
+	 # The "-l" case would never come before the object being
+	 # linked, so don't bother handling this case.
+	 esac
+       else
+	 if test -z "$_LT_TAGVAR(postdeps, $1)"; then
+	   _LT_TAGVAR(postdeps, $1)=$prev$p
+	 else
+	   _LT_TAGVAR(postdeps, $1)="${_LT_TAGVAR(postdeps, $1)} $prev$p"
+	 fi
+       fi
+       prev=
+       ;;
+
+    *.lto.$objext) ;; # Ignore GCC LTO objects
+    *.$objext)
+       # This assumes that the test object file only shows up
+       # once in the compiler output.
+       if test "$p" = "conftest.$objext"; then
+	 pre_test_object_deps_done=yes
+	 continue
+       fi
+
+       if test no = "$pre_test_object_deps_done"; then
+	 if test -z "$_LT_TAGVAR(predep_objects, $1)"; then
+	   _LT_TAGVAR(predep_objects, $1)=$p
+	 else
+	   _LT_TAGVAR(predep_objects, $1)="$_LT_TAGVAR(predep_objects, $1) $p"
+	 fi
+       else
+	 if test -z "$_LT_TAGVAR(postdep_objects, $1)"; then
+	   _LT_TAGVAR(postdep_objects, $1)=$p
+	 else
+	   _LT_TAGVAR(postdep_objects, $1)="$_LT_TAGVAR(postdep_objects, $1) $p"
+	 fi
+       fi
+       ;;
+
+    *) ;; # Ignore the rest.
+
+    esac
+  done
+
+  # Clean up.
+  rm -f a.out a.exe
+else
+  echo "libtool.m4: error: problem compiling $1 test program"
+fi
+
+$RM -f confest.$objext
+CFLAGS=$_lt_libdeps_save_CFLAGS
+
+# PORTME: override above test on systems where it is broken
+m4_if([$1], [CXX],
+[case $host_os in
+interix[[3-9]]*)
+  # Interix 3.5 installs completely hosed .la files for C++, so rather than
+  # hack all around it, let's just trust "g++" to DTRT.
+  _LT_TAGVAR(predep_objects,$1)=
+  _LT_TAGVAR(postdep_objects,$1)=
+  _LT_TAGVAR(postdeps,$1)=
+  ;;
+esac
+])
+
+case " $_LT_TAGVAR(postdeps, $1) " in
+*" -lc "*) _LT_TAGVAR(archive_cmds_need_lc, $1)=no ;;
+esac
+ _LT_TAGVAR(compiler_lib_search_dirs, $1)=
+if test -n "${_LT_TAGVAR(compiler_lib_search_path, $1)}"; then
+ _LT_TAGVAR(compiler_lib_search_dirs, $1)=`echo " ${_LT_TAGVAR(compiler_lib_search_path, $1)}" | $SED -e 's! -L! !g' -e 's!^ !!'`
+fi
+_LT_TAGDECL([], [compiler_lib_search_dirs], [1],
+    [The directories searched by this compiler when creating a shared library])
+_LT_TAGDECL([], [predep_objects], [1],
+    [Dependencies to place before and after the objects being linked to
+    create a shared library])
+_LT_TAGDECL([], [postdep_objects], [1])
+_LT_TAGDECL([], [predeps], [1])
+_LT_TAGDECL([], [postdeps], [1])
+_LT_TAGDECL([], [compiler_lib_search_path], [1],
+    [The library search path used internally by the compiler when linking
+    a shared library])
+])# _LT_SYS_HIDDEN_LIBDEPS
+
+
+# _LT_LANG_F77_CONFIG([TAG])
+# --------------------------
+# Ensure that the configuration variables for a Fortran 77 compiler are
+# suitably defined.  These variables are subsequently used by _LT_CONFIG
+# to write the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_F77_CONFIG],
+[AC_LANG_PUSH(Fortran 77)
+if test -z "$F77" || test no = "$F77"; then
+  _lt_disable_F77=yes
+fi
+
+_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+_LT_TAGVAR(allow_undefined_flag, $1)=
+_LT_TAGVAR(always_export_symbols, $1)=no
+_LT_TAGVAR(archive_expsym_cmds, $1)=
+_LT_TAGVAR(export_dynamic_flag_spec, $1)=
+_LT_TAGVAR(hardcode_direct, $1)=no
+_LT_TAGVAR(hardcode_direct_absolute, $1)=no
+_LT_TAGVAR(hardcode_libdir_flag_spec, $1)=
+_LT_TAGVAR(hardcode_libdir_separator, $1)=
+_LT_TAGVAR(hardcode_minus_L, $1)=no
+_LT_TAGVAR(hardcode_automatic, $1)=no
+_LT_TAGVAR(inherit_rpath, $1)=no
+_LT_TAGVAR(module_cmds, $1)=
+_LT_TAGVAR(module_expsym_cmds, $1)=
+_LT_TAGVAR(link_all_deplibs, $1)=unknown
+_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_TAGVAR(reload_flag, $1)=$reload_flag
+_LT_TAGVAR(reload_cmds, $1)=$reload_cmds
+_LT_TAGVAR(no_undefined_flag, $1)=
+_LT_TAGVAR(whole_archive_flag_spec, $1)=
+_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+
+# Source file extension for f77 test sources.
+ac_ext=f
+
+# Object file extension for compiled f77 test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# No sense in running all these tests if we already determined that
+# the F77 compiler isn't working.  Some variables (like enable_shared)
+# are currently assumed to apply to all compilers on this platform,
+# and will be corrupted by setting them based on a non-working compiler.
+if test yes != "$_lt_disable_F77"; then
+  # Code to be used in simple compile tests
+  lt_simple_compile_test_code="\
+      subroutine t
+      return
+      end
+"
+
+  # Code to be used in simple link tests
+  lt_simple_link_test_code="\
+      program t
+      end
+"
+
+  # ltmain only uses $CC for tagged configurations so make sure $CC is set.
+  _LT_TAG_COMPILER
+
+  # save warnings/boilerplate of simple test code
+  _LT_COMPILER_BOILERPLATE
+  _LT_LINKER_BOILERPLATE
+
+  # Allow CC to be a program name with arguments.
+  lt_save_CC=$CC
+  lt_save_GCC=$GCC
+  lt_save_CFLAGS=$CFLAGS
+  CC=${F77-"f77"}
+  CFLAGS=$FFLAGS
+  compiler=$CC
+  _LT_TAGVAR(compiler, $1)=$CC
+  _LT_CC_BASENAME([$compiler])
+  GCC=$G77
+  if test -n "$compiler"; then
+    AC_MSG_CHECKING([if libtool supports shared libraries])
+    AC_MSG_RESULT([$can_build_shared])
+
+    AC_MSG_CHECKING([whether to build shared libraries])
+    test no = "$can_build_shared" && enable_shared=no
+
+    # On AIX, shared libraries and static libraries use the same namespace, and
+    # are all built from PIC.
+    case $host_os in
+      aix3*)
+        test yes = "$enable_shared" && enable_static=no
+        if test -n "$RANLIB"; then
+          archive_cmds="$archive_cmds~\$RANLIB \$lib"
+          postinstall_cmds='$RANLIB $lib'
+        fi
+        ;;
+      aix[[4-9]]*)
+	if test ia64 != "$host_cpu"; then
+	  case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in
+	  yes,aix,yes) ;;		# shared object as lib.so file only
+	  yes,svr4,*) ;;		# shared object as lib.so archive member only
+	  yes,*) enable_static=no ;;	# shared object in lib.a archive as well
+	  esac
+	fi
+        ;;
+    esac
+    AC_MSG_RESULT([$enable_shared])
+
+    AC_MSG_CHECKING([whether to build static libraries])
+    # Make sure either enable_shared or enable_static is yes.
+    test yes = "$enable_shared" || enable_static=yes
+    AC_MSG_RESULT([$enable_static])
+
+    _LT_TAGVAR(GCC, $1)=$G77
+    _LT_TAGVAR(LD, $1)=$LD
+
+    ## CAVEAT EMPTOR:
+    ## There is no encapsulation within the following macros, do not change
+    ## the running order or otherwise move them around unless you know exactly
+    ## what you are doing...
+    _LT_COMPILER_PIC($1)
+    _LT_COMPILER_C_O($1)
+    _LT_COMPILER_FILE_LOCKS($1)
+    _LT_LINKER_SHLIBS($1)
+    _LT_SYS_DYNAMIC_LINKER($1)
+    _LT_LINKER_HARDCODE_LIBPATH($1)
+
+    _LT_CONFIG($1)
+  fi # test -n "$compiler"
+
+  GCC=$lt_save_GCC
+  CC=$lt_save_CC
+  CFLAGS=$lt_save_CFLAGS
+fi # test yes != "$_lt_disable_F77"
+
+AC_LANG_POP
+])# _LT_LANG_F77_CONFIG
+
+
+# _LT_LANG_FC_CONFIG([TAG])
+# -------------------------
+# Ensure that the configuration variables for a Fortran compiler are
+# suitably defined.  These variables are subsequently used by _LT_CONFIG
+# to write the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_FC_CONFIG],
+[AC_LANG_PUSH(Fortran)
+
+if test -z "$FC" || test no = "$FC"; then
+  _lt_disable_FC=yes
+fi
+
+_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+_LT_TAGVAR(allow_undefined_flag, $1)=
+_LT_TAGVAR(always_export_symbols, $1)=no
+_LT_TAGVAR(archive_expsym_cmds, $1)=
+_LT_TAGVAR(export_dynamic_flag_spec, $1)=
+_LT_TAGVAR(hardcode_direct, $1)=no
+_LT_TAGVAR(hardcode_direct_absolute, $1)=no
+_LT_TAGVAR(hardcode_libdir_flag_spec, $1)=
+_LT_TAGVAR(hardcode_libdir_separator, $1)=
+_LT_TAGVAR(hardcode_minus_L, $1)=no
+_LT_TAGVAR(hardcode_automatic, $1)=no
+_LT_TAGVAR(inherit_rpath, $1)=no
+_LT_TAGVAR(module_cmds, $1)=
+_LT_TAGVAR(module_expsym_cmds, $1)=
+_LT_TAGVAR(link_all_deplibs, $1)=unknown
+_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_TAGVAR(reload_flag, $1)=$reload_flag
+_LT_TAGVAR(reload_cmds, $1)=$reload_cmds
+_LT_TAGVAR(no_undefined_flag, $1)=
+_LT_TAGVAR(whole_archive_flag_spec, $1)=
+_LT_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+
+# Source file extension for fc test sources.
+ac_ext=${ac_fc_srcext-f}
+
+# Object file extension for compiled fc test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# No sense in running all these tests if we already determined that
+# the FC compiler isn't working.  Some variables (like enable_shared)
+# are currently assumed to apply to all compilers on this platform,
+# and will be corrupted by setting them based on a non-working compiler.
+if test yes != "$_lt_disable_FC"; then
+  # Code to be used in simple compile tests
+  lt_simple_compile_test_code="\
+      subroutine t
+      return
+      end
+"
+
+  # Code to be used in simple link tests
+  lt_simple_link_test_code="\
+      program t
+      end
+"
+
+  # ltmain only uses $CC for tagged configurations so make sure $CC is set.
+  _LT_TAG_COMPILER
+
+  # save warnings/boilerplate of simple test code
+  _LT_COMPILER_BOILERPLATE
+  _LT_LINKER_BOILERPLATE
+
+  # Allow CC to be a program name with arguments.
+  lt_save_CC=$CC
+  lt_save_GCC=$GCC
+  lt_save_CFLAGS=$CFLAGS
+  CC=${FC-"f95"}
+  CFLAGS=$FCFLAGS
+  compiler=$CC
+  GCC=$ac_cv_fc_compiler_gnu
+
+  _LT_TAGVAR(compiler, $1)=$CC
+  _LT_CC_BASENAME([$compiler])
+
+  if test -n "$compiler"; then
+    AC_MSG_CHECKING([if libtool supports shared libraries])
+    AC_MSG_RESULT([$can_build_shared])
+
+    AC_MSG_CHECKING([whether to build shared libraries])
+    test no = "$can_build_shared" && enable_shared=no
+
+    # On AIX, shared libraries and static libraries use the same namespace, and
+    # are all built from PIC.
+    case $host_os in
+      aix3*)
+        test yes = "$enable_shared" && enable_static=no
+        if test -n "$RANLIB"; then
+          archive_cmds="$archive_cmds~\$RANLIB \$lib"
+          postinstall_cmds='$RANLIB $lib'
+        fi
+        ;;
+      aix[[4-9]]*)
+	if test ia64 != "$host_cpu"; then
+	  case $enable_shared,$with_aix_soname,$aix_use_runtimelinking in
+	  yes,aix,yes) ;;		# shared object as lib.so file only
+	  yes,svr4,*) ;;		# shared object as lib.so archive member only
+	  yes,*) enable_static=no ;;	# shared object in lib.a archive as well
+	  esac
+	fi
+        ;;
+    esac
+    AC_MSG_RESULT([$enable_shared])
+
+    AC_MSG_CHECKING([whether to build static libraries])
+    # Make sure either enable_shared or enable_static is yes.
+    test yes = "$enable_shared" || enable_static=yes
+    AC_MSG_RESULT([$enable_static])
+
+    _LT_TAGVAR(GCC, $1)=$ac_cv_fc_compiler_gnu
+    _LT_TAGVAR(LD, $1)=$LD
+
+    ## CAVEAT EMPTOR:
+    ## There is no encapsulation within the following macros, do not change
+    ## the running order or otherwise move them around unless you know exactly
+    ## what you are doing...
+    _LT_SYS_HIDDEN_LIBDEPS($1)
+    _LT_COMPILER_PIC($1)
+    _LT_COMPILER_C_O($1)
+    _LT_COMPILER_FILE_LOCKS($1)
+    _LT_LINKER_SHLIBS($1)
+    _LT_SYS_DYNAMIC_LINKER($1)
+    _LT_LINKER_HARDCODE_LIBPATH($1)
+
+    _LT_CONFIG($1)
+  fi # test -n "$compiler"
+
+  GCC=$lt_save_GCC
+  CC=$lt_save_CC
+  CFLAGS=$lt_save_CFLAGS
+fi # test yes != "$_lt_disable_FC"
+
+AC_LANG_POP
+])# _LT_LANG_FC_CONFIG
+
+
+# _LT_LANG_GCJ_CONFIG([TAG])
+# --------------------------
+# Ensure that the configuration variables for the GNU Java Compiler compiler
+# are suitably defined.  These variables are subsequently used by _LT_CONFIG
+# to write the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_GCJ_CONFIG],
+[AC_REQUIRE([LT_PROG_GCJ])dnl
+AC_LANG_SAVE
+
+# Source file extension for Java test sources.
+ac_ext=java
+
+# Object file extension for compiled Java test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="class foo {}"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='public class conftest { public static void main(String[[]] argv) {}; }'
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_TAG_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC=$CC
+lt_save_CFLAGS=$CFLAGS
+lt_save_GCC=$GCC
+GCC=yes
+CC=${GCJ-"gcj"}
+CFLAGS=$GCJFLAGS
+compiler=$CC
+_LT_TAGVAR(compiler, $1)=$CC
+_LT_TAGVAR(LD, $1)=$LD
+_LT_CC_BASENAME([$compiler])
+
+# GCJ did not exist at the time GCC didn't implicitly link libc in.
+_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+
+_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_TAGVAR(reload_flag, $1)=$reload_flag
+_LT_TAGVAR(reload_cmds, $1)=$reload_cmds
+
+## CAVEAT EMPTOR:
+## There is no encapsulation within the following macros, do not change
+## the running order or otherwise move them around unless you know exactly
+## what you are doing...
+if test -n "$compiler"; then
+  _LT_COMPILER_NO_RTTI($1)
+  _LT_COMPILER_PIC($1)
+  _LT_COMPILER_C_O($1)
+  _LT_COMPILER_FILE_LOCKS($1)
+  _LT_LINKER_SHLIBS($1)
+  _LT_LINKER_HARDCODE_LIBPATH($1)
+
+  _LT_CONFIG($1)
+fi
+
+AC_LANG_RESTORE
+
+GCC=$lt_save_GCC
+CC=$lt_save_CC
+CFLAGS=$lt_save_CFLAGS
+])# _LT_LANG_GCJ_CONFIG
+
+
+# _LT_LANG_GO_CONFIG([TAG])
+# --------------------------
+# Ensure that the configuration variables for the GNU Go compiler
+# are suitably defined.  These variables are subsequently used by _LT_CONFIG
+# to write the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_GO_CONFIG],
+[AC_REQUIRE([LT_PROG_GO])dnl
+AC_LANG_SAVE
+
+# Source file extension for Go test sources.
+ac_ext=go
+
+# Object file extension for compiled Go test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="package main; func main() { }"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='package main; func main() { }'
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_TAG_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC=$CC
+lt_save_CFLAGS=$CFLAGS
+lt_save_GCC=$GCC
+GCC=yes
+CC=${GOC-"gccgo"}
+CFLAGS=$GOFLAGS
+compiler=$CC
+_LT_TAGVAR(compiler, $1)=$CC
+_LT_TAGVAR(LD, $1)=$LD
+_LT_CC_BASENAME([$compiler])
+
+# Go did not exist at the time GCC didn't implicitly link libc in.
+_LT_TAGVAR(archive_cmds_need_lc, $1)=no
+
+_LT_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_TAGVAR(reload_flag, $1)=$reload_flag
+_LT_TAGVAR(reload_cmds, $1)=$reload_cmds
+
+## CAVEAT EMPTOR:
+## There is no encapsulation within the following macros, do not change
+## the running order or otherwise move them around unless you know exactly
+## what you are doing...
+if test -n "$compiler"; then
+  _LT_COMPILER_NO_RTTI($1)
+  _LT_COMPILER_PIC($1)
+  _LT_COMPILER_C_O($1)
+  _LT_COMPILER_FILE_LOCKS($1)
+  _LT_LINKER_SHLIBS($1)
+  _LT_LINKER_HARDCODE_LIBPATH($1)
+
+  _LT_CONFIG($1)
+fi
+
+AC_LANG_RESTORE
+
+GCC=$lt_save_GCC
+CC=$lt_save_CC
+CFLAGS=$lt_save_CFLAGS
+])# _LT_LANG_GO_CONFIG
+
+
+# _LT_LANG_RC_CONFIG([TAG])
+# -------------------------
+# Ensure that the configuration variables for the Windows resource compiler
+# are suitably defined.  These variables are subsequently used by _LT_CONFIG
+# to write the compiler configuration to 'libtool'.
+m4_defun([_LT_LANG_RC_CONFIG],
+[AC_REQUIRE([LT_PROG_RC])dnl
+AC_LANG_SAVE
+
+# Source file extension for RC test sources.
+ac_ext=rc
+
+# Object file extension for compiled RC test sources.
+objext=o
+_LT_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code='sample MENU { MENUITEM "&Soup", 100, CHECKED }'
+
+# Code to be used in simple link tests
+lt_simple_link_test_code=$lt_simple_compile_test_code
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_TAG_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC=$CC
+lt_save_CFLAGS=$CFLAGS
+lt_save_GCC=$GCC
+GCC=
+CC=${RC-"windres"}
+CFLAGS=
+compiler=$CC
+_LT_TAGVAR(compiler, $1)=$CC
+_LT_CC_BASENAME([$compiler])
+_LT_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes
+
+if test -n "$compiler"; then
+  :
+  _LT_CONFIG($1)
+fi
+
+GCC=$lt_save_GCC
+AC_LANG_RESTORE
+CC=$lt_save_CC
+CFLAGS=$lt_save_CFLAGS
+])# _LT_LANG_RC_CONFIG
+
+
+# LT_PROG_GCJ
+# -----------
+AC_DEFUN([LT_PROG_GCJ],
+[m4_ifdef([AC_PROG_GCJ], [AC_PROG_GCJ],
+  [m4_ifdef([A][M_PROG_GCJ], [A][M_PROG_GCJ],
+    [AC_CHECK_TOOL(GCJ, gcj,)
+      test set = "${GCJFLAGS+set}" || GCJFLAGS="-g -O2"
+      AC_SUBST(GCJFLAGS)])])[]dnl
+])
+
+# Old name:
+AU_ALIAS([LT_AC_PROG_GCJ], [LT_PROG_GCJ])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([LT_AC_PROG_GCJ], [])
+
+
+# LT_PROG_GO
+# ----------
+AC_DEFUN([LT_PROG_GO],
+[AC_CHECK_TOOL(GOC, gccgo,)
+])
+
+
+# LT_PROG_RC
+# ----------
+AC_DEFUN([LT_PROG_RC],
+[AC_CHECK_TOOL(RC, windres,)
+])
+
+# Old name:
+AU_ALIAS([LT_AC_PROG_RC], [LT_PROG_RC])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([LT_AC_PROG_RC], [])
+
+
+# _LT_DECL_EGREP
+# --------------
+# If we don't have a new enough Autoconf to choose the best grep
+# available, choose the one first in the user's PATH.
+m4_defun([_LT_DECL_EGREP],
+[AC_REQUIRE([AC_PROG_EGREP])dnl
+AC_REQUIRE([AC_PROG_FGREP])dnl
+test -z "$GREP" && GREP=grep
+_LT_DECL([], [GREP], [1], [A grep program that handles long lines])
+_LT_DECL([], [EGREP], [1], [An ERE matcher])
+_LT_DECL([], [FGREP], [1], [A literal string matcher])
+dnl Non-bleeding-edge autoconf doesn't subst GREP, so do it here too
+AC_SUBST([GREP])
+])
+
+
+# _LT_DECL_OBJDUMP
+# --------------
+# If we don't have a new enough Autoconf to choose the best objdump
+# available, choose the one first in the user's PATH.
+m4_defun([_LT_DECL_OBJDUMP],
+[AC_CHECK_TOOL(OBJDUMP, objdump, false)
+test -z "$OBJDUMP" && OBJDUMP=objdump
+_LT_DECL([], [OBJDUMP], [1], [An object symbol dumper])
+AC_SUBST([OBJDUMP])
+])
+
+# _LT_DECL_DLLTOOL
+# ----------------
+# Ensure DLLTOOL variable is set.
+m4_defun([_LT_DECL_DLLTOOL],
+[AC_CHECK_TOOL(DLLTOOL, dlltool, false)
+test -z "$DLLTOOL" && DLLTOOL=dlltool
+_LT_DECL([], [DLLTOOL], [1], [DLL creation program])
+AC_SUBST([DLLTOOL])
+])
+
+# _LT_DECL_SED
+# ------------
+# Check for a fully-functional sed program, that truncates
+# as few characters as possible.  Prefer GNU sed if found.
+m4_defun([_LT_DECL_SED],
+[AC_PROG_SED
+test -z "$SED" && SED=sed
+Xsed="$SED -e 1s/^X//"
+_LT_DECL([], [SED], [1], [A sed program that does not truncate output])
+_LT_DECL([], [Xsed], ["\$SED -e 1s/^X//"],
+    [Sed that helps us avoid accidentally triggering echo(1) options like -n])
+])# _LT_DECL_SED
+
+m4_ifndef([AC_PROG_SED], [
+############################################################
+# NOTE: This macro has been submitted for inclusion into   #
+#  GNU Autoconf as AC_PROG_SED.  When it is available in   #
+#  a released version of Autoconf we should remove this    #
+#  macro and use it instead.                               #
+############################################################
+
+m4_defun([AC_PROG_SED],
+[AC_MSG_CHECKING([for a sed that does not truncate output])
+AC_CACHE_VAL(lt_cv_path_SED,
+[# Loop through the user's path and test for sed and gsed.
+# Then use that list of sed's as ones to test for truncation.
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for lt_ac_prog in sed gsed; do
+    for ac_exec_ext in '' $ac_executable_extensions; do
+      if $as_executable_p "$as_dir/$lt_ac_prog$ac_exec_ext"; then
+        lt_ac_sed_list="$lt_ac_sed_list $as_dir/$lt_ac_prog$ac_exec_ext"
+      fi
+    done
+  done
+done
+IFS=$as_save_IFS
+lt_ac_max=0
+lt_ac_count=0
+# Add /usr/xpg4/bin/sed as it is typically found on Solaris
+# along with /bin/sed that truncates output.
+for lt_ac_sed in $lt_ac_sed_list /usr/xpg4/bin/sed; do
+  test ! -f "$lt_ac_sed" && continue
+  cat /dev/null > conftest.in
+  lt_ac_count=0
+  echo $ECHO_N "0123456789$ECHO_C" >conftest.in
+  # Check for GNU sed and select it if it is found.
+  if "$lt_ac_sed" --version 2>&1 < /dev/null | grep 'GNU' > /dev/null; then
+    lt_cv_path_SED=$lt_ac_sed
+    break
+  fi
+  while true; do
+    cat conftest.in conftest.in >conftest.tmp
+    mv conftest.tmp conftest.in
+    cp conftest.in conftest.nl
+    echo >>conftest.nl
+    $lt_ac_sed -e 's/a$//' < conftest.nl >conftest.out || break
+    cmp -s conftest.out conftest.nl || break
+    # 10000 chars as input seems more than enough
+    test 10 -lt "$lt_ac_count" && break
+    lt_ac_count=`expr $lt_ac_count + 1`
+    if test "$lt_ac_count" -gt "$lt_ac_max"; then
+      lt_ac_max=$lt_ac_count
+      lt_cv_path_SED=$lt_ac_sed
+    fi
+  done
+done
+])
+SED=$lt_cv_path_SED
+AC_SUBST([SED])
+AC_MSG_RESULT([$SED])
+])#AC_PROG_SED
+])#m4_ifndef
+
+# Old name:
+AU_ALIAS([LT_AC_PROG_SED], [AC_PROG_SED])
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([LT_AC_PROG_SED], [])
+
+
+# _LT_CHECK_SHELL_FEATURES
+# ------------------------
+# Find out whether the shell is Bourne or XSI compatible,
+# or has some other useful features.
+m4_defun([_LT_CHECK_SHELL_FEATURES],
+[if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
+  lt_unset=unset
+else
+  lt_unset=false
+fi
+_LT_DECL([], [lt_unset], [0], [whether the shell understands "unset"])dnl
+
+# test EBCDIC or ASCII
+case `echo X|tr X '\101'` in
+ A) # ASCII based system
+    # \n is not interpreted correctly by Solaris 8 /usr/ucb/tr
+  lt_SP2NL='tr \040 \012'
+  lt_NL2SP='tr \015\012 \040\040'
+  ;;
+ *) # EBCDIC based system
+  lt_SP2NL='tr \100 \n'
+  lt_NL2SP='tr \r\n \100\100'
+  ;;
+esac
+_LT_DECL([SP2NL], [lt_SP2NL], [1], [turn spaces into newlines])dnl
+_LT_DECL([NL2SP], [lt_NL2SP], [1], [turn newlines into spaces])dnl
+])# _LT_CHECK_SHELL_FEATURES
+
+
+# _LT_PATH_CONVERSION_FUNCTIONS
+# -----------------------------
+# Determine what file name conversion functions should be used by
+# func_to_host_file (and, implicitly, by func_to_host_path).  These are needed
+# for certain cross-compile configurations and native mingw.
+m4_defun([_LT_PATH_CONVERSION_FUNCTIONS],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_CANONICAL_BUILD])dnl
+AC_MSG_CHECKING([how to convert $build file names to $host format])
+AC_CACHE_VAL(lt_cv_to_host_file_cmd,
+[case $host in
+  *-*-mingw* )
+    case $build in
+      *-*-mingw* ) # actually msys
+        lt_cv_to_host_file_cmd=func_convert_file_msys_to_w32
+        ;;
+      *-*-cygwin* )
+        lt_cv_to_host_file_cmd=func_convert_file_cygwin_to_w32
+        ;;
+      * ) # otherwise, assume *nix
+        lt_cv_to_host_file_cmd=func_convert_file_nix_to_w32
+        ;;
+    esac
+    ;;
+  *-*-cygwin* )
+    case $build in
+      *-*-mingw* ) # actually msys
+        lt_cv_to_host_file_cmd=func_convert_file_msys_to_cygwin
+        ;;
+      *-*-cygwin* )
+        lt_cv_to_host_file_cmd=func_convert_file_noop
+        ;;
+      * ) # otherwise, assume *nix
+        lt_cv_to_host_file_cmd=func_convert_file_nix_to_cygwin
+        ;;
+    esac
+    ;;
+  * ) # unhandled hosts (and "normal" native builds)
+    lt_cv_to_host_file_cmd=func_convert_file_noop
+    ;;
+esac
+])
+to_host_file_cmd=$lt_cv_to_host_file_cmd
+AC_MSG_RESULT([$lt_cv_to_host_file_cmd])
+_LT_DECL([to_host_file_cmd], [lt_cv_to_host_file_cmd],
+         [0], [convert $build file names to $host format])dnl
+
+AC_MSG_CHECKING([how to convert $build file names to toolchain format])
+AC_CACHE_VAL(lt_cv_to_tool_file_cmd,
+[#assume ordinary cross tools, or native build.
+lt_cv_to_tool_file_cmd=func_convert_file_noop
+case $host in
+  *-*-mingw* )
+    case $build in
+      *-*-mingw* ) # actually msys
+        lt_cv_to_tool_file_cmd=func_convert_file_msys_to_w32
+        ;;
+    esac
+    ;;
+esac
+])
+to_tool_file_cmd=$lt_cv_to_tool_file_cmd
+AC_MSG_RESULT([$lt_cv_to_tool_file_cmd])
+_LT_DECL([to_tool_file_cmd], [lt_cv_to_tool_file_cmd],
+         [0], [convert $build files to toolchain format])dnl
+])# _LT_PATH_CONVERSION_FUNCTIONS
diff --git a/sgx_unwind/libunwind/m4/ltoptions.m4 b/sgx_unwind/libunwind/m4/ltoptions.m4
new file mode 100644
index 0000000..94b0829
--- /dev/null
+++ b/sgx_unwind/libunwind/m4/ltoptions.m4
@@ -0,0 +1,437 @@
+# Helper functions for option handling.                    -*- Autoconf -*-
+#
+#   Copyright (C) 2004-2005, 2007-2009, 2011-2015 Free Software
+#   Foundation, Inc.
+#   Written by Gary V. Vaughan, 2004
+#
+# This file is free software; the Free Software Foundation gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+# serial 8 ltoptions.m4
+
+# This is to help aclocal find these macros, as it can't see m4_define.
+AC_DEFUN([LTOPTIONS_VERSION], [m4_if([1])])
+
+
+# _LT_MANGLE_OPTION(MACRO-NAME, OPTION-NAME)
+# ------------------------------------------
+m4_define([_LT_MANGLE_OPTION],
+[[_LT_OPTION_]m4_bpatsubst($1__$2, [[^a-zA-Z0-9_]], [_])])
+
+
+# _LT_SET_OPTION(MACRO-NAME, OPTION-NAME)
+# ---------------------------------------
+# Set option OPTION-NAME for macro MACRO-NAME, and if there is a
+# matching handler defined, dispatch to it.  Other OPTION-NAMEs are
+# saved as a flag.
+m4_define([_LT_SET_OPTION],
+[m4_define(_LT_MANGLE_OPTION([$1], [$2]))dnl
+m4_ifdef(_LT_MANGLE_DEFUN([$1], [$2]),
+        _LT_MANGLE_DEFUN([$1], [$2]),
+    [m4_warning([Unknown $1 option '$2'])])[]dnl
+])
+
+
+# _LT_IF_OPTION(MACRO-NAME, OPTION-NAME, IF-SET, [IF-NOT-SET])
+# ------------------------------------------------------------
+# Execute IF-SET if OPTION is set, IF-NOT-SET otherwise.
+m4_define([_LT_IF_OPTION],
+[m4_ifdef(_LT_MANGLE_OPTION([$1], [$2]), [$3], [$4])])
+
+
+# _LT_UNLESS_OPTIONS(MACRO-NAME, OPTION-LIST, IF-NOT-SET)
+# -------------------------------------------------------
+# Execute IF-NOT-SET unless all options in OPTION-LIST for MACRO-NAME
+# are set.
+m4_define([_LT_UNLESS_OPTIONS],
+[m4_foreach([_LT_Option], m4_split(m4_normalize([$2])),
+	    [m4_ifdef(_LT_MANGLE_OPTION([$1], _LT_Option),
+		      [m4_define([$0_found])])])[]dnl
+m4_ifdef([$0_found], [m4_undefine([$0_found])], [$3
+])[]dnl
+])
+
+
+# _LT_SET_OPTIONS(MACRO-NAME, OPTION-LIST)
+# ----------------------------------------
+# OPTION-LIST is a space-separated list of Libtool options associated
+# with MACRO-NAME.  If any OPTION has a matching handler declared with
+# LT_OPTION_DEFINE, dispatch to that macro; otherwise complain about
+# the unknown option and exit.
+m4_defun([_LT_SET_OPTIONS],
+[# Set options
+m4_foreach([_LT_Option], m4_split(m4_normalize([$2])),
+    [_LT_SET_OPTION([$1], _LT_Option)])
+
+m4_if([$1],[LT_INIT],[
+  dnl
+  dnl Simply set some default values (i.e off) if boolean options were not
+  dnl specified:
+  _LT_UNLESS_OPTIONS([LT_INIT], [dlopen], [enable_dlopen=no
+  ])
+  _LT_UNLESS_OPTIONS([LT_INIT], [win32-dll], [enable_win32_dll=no
+  ])
+  dnl
+  dnl If no reference was made to various pairs of opposing options, then
+  dnl we run the default mode handler for the pair.  For example, if neither
+  dnl 'shared' nor 'disable-shared' was passed, we enable building of shared
+  dnl archives by default:
+  _LT_UNLESS_OPTIONS([LT_INIT], [shared disable-shared], [_LT_ENABLE_SHARED])
+  _LT_UNLESS_OPTIONS([LT_INIT], [static disable-static], [_LT_ENABLE_STATIC])
+  _LT_UNLESS_OPTIONS([LT_INIT], [pic-only no-pic], [_LT_WITH_PIC])
+  _LT_UNLESS_OPTIONS([LT_INIT], [fast-install disable-fast-install],
+		   [_LT_ENABLE_FAST_INSTALL])
+  _LT_UNLESS_OPTIONS([LT_INIT], [aix-soname=aix aix-soname=both aix-soname=svr4],
+		   [_LT_WITH_AIX_SONAME([aix])])
+  ])
+])# _LT_SET_OPTIONS
+
+
+## --------------------------------- ##
+## Macros to handle LT_INIT options. ##
+## --------------------------------- ##
+
+# _LT_MANGLE_DEFUN(MACRO-NAME, OPTION-NAME)
+# -----------------------------------------
+m4_define([_LT_MANGLE_DEFUN],
+[[_LT_OPTION_DEFUN_]m4_bpatsubst(m4_toupper([$1__$2]), [[^A-Z0-9_]], [_])])
+
+
+# LT_OPTION_DEFINE(MACRO-NAME, OPTION-NAME, CODE)
+# -----------------------------------------------
+m4_define([LT_OPTION_DEFINE],
+[m4_define(_LT_MANGLE_DEFUN([$1], [$2]), [$3])[]dnl
+])# LT_OPTION_DEFINE
+
+
+# dlopen
+# ------
+LT_OPTION_DEFINE([LT_INIT], [dlopen], [enable_dlopen=yes
+])
+
+AU_DEFUN([AC_LIBTOOL_DLOPEN],
+[_LT_SET_OPTION([LT_INIT], [dlopen])
+AC_DIAGNOSE([obsolete],
+[$0: Remove this warning and the call to _LT_SET_OPTION when you
+put the 'dlopen' option into LT_INIT's first parameter.])
+])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_DLOPEN], [])
+
+
+# win32-dll
+# ---------
+# Declare package support for building win32 dll's.
+LT_OPTION_DEFINE([LT_INIT], [win32-dll],
+[enable_win32_dll=yes
+
+case $host in
+*-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-cegcc*)
+  AC_CHECK_TOOL(AS, as, false)
+  AC_CHECK_TOOL(DLLTOOL, dlltool, false)
+  AC_CHECK_TOOL(OBJDUMP, objdump, false)
+  ;;
+esac
+
+test -z "$AS" && AS=as
+_LT_DECL([], [AS],      [1], [Assembler program])dnl
+
+test -z "$DLLTOOL" && DLLTOOL=dlltool
+_LT_DECL([], [DLLTOOL], [1], [DLL creation program])dnl
+
+test -z "$OBJDUMP" && OBJDUMP=objdump
+_LT_DECL([], [OBJDUMP], [1], [Object dumper program])dnl
+])# win32-dll
+
+AU_DEFUN([AC_LIBTOOL_WIN32_DLL],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+_LT_SET_OPTION([LT_INIT], [win32-dll])
+AC_DIAGNOSE([obsolete],
+[$0: Remove this warning and the call to _LT_SET_OPTION when you
+put the 'win32-dll' option into LT_INIT's first parameter.])
+])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_WIN32_DLL], [])
+
+
+# _LT_ENABLE_SHARED([DEFAULT])
+# ----------------------------
+# implement the --enable-shared flag, and supports the 'shared' and
+# 'disable-shared' LT_INIT options.
+# DEFAULT is either 'yes' or 'no'.  If omitted, it defaults to 'yes'.
+m4_define([_LT_ENABLE_SHARED],
+[m4_define([_LT_ENABLE_SHARED_DEFAULT], [m4_if($1, no, no, yes)])dnl
+AC_ARG_ENABLE([shared],
+    [AS_HELP_STRING([--enable-shared@<:@=PKGS@:>@],
+	[build shared libraries @<:@default=]_LT_ENABLE_SHARED_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_shared=yes ;;
+    no) enable_shared=no ;;
+    *)
+      enable_shared=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR,
+      for pkg in $enableval; do
+	IFS=$lt_save_ifs
+	if test "X$pkg" = "X$p"; then
+	  enable_shared=yes
+	fi
+      done
+      IFS=$lt_save_ifs
+      ;;
+    esac],
+    [enable_shared=]_LT_ENABLE_SHARED_DEFAULT)
+
+    _LT_DECL([build_libtool_libs], [enable_shared], [0],
+	[Whether or not to build shared libraries])
+])# _LT_ENABLE_SHARED
+
+LT_OPTION_DEFINE([LT_INIT], [shared], [_LT_ENABLE_SHARED([yes])])
+LT_OPTION_DEFINE([LT_INIT], [disable-shared], [_LT_ENABLE_SHARED([no])])
+
+# Old names:
+AC_DEFUN([AC_ENABLE_SHARED],
+[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[shared])
+])
+
+AC_DEFUN([AC_DISABLE_SHARED],
+[_LT_SET_OPTION([LT_INIT], [disable-shared])
+])
+
+AU_DEFUN([AM_ENABLE_SHARED], [AC_ENABLE_SHARED($@)])
+AU_DEFUN([AM_DISABLE_SHARED], [AC_DISABLE_SHARED($@)])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AM_ENABLE_SHARED], [])
+dnl AC_DEFUN([AM_DISABLE_SHARED], [])
+
+
+
+# _LT_ENABLE_STATIC([DEFAULT])
+# ----------------------------
+# implement the --enable-static flag, and support the 'static' and
+# 'disable-static' LT_INIT options.
+# DEFAULT is either 'yes' or 'no'.  If omitted, it defaults to 'yes'.
+m4_define([_LT_ENABLE_STATIC],
+[m4_define([_LT_ENABLE_STATIC_DEFAULT], [m4_if($1, no, no, yes)])dnl
+AC_ARG_ENABLE([static],
+    [AS_HELP_STRING([--enable-static@<:@=PKGS@:>@],
+	[build static libraries @<:@default=]_LT_ENABLE_STATIC_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_static=yes ;;
+    no) enable_static=no ;;
+    *)
+     enable_static=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR,
+      for pkg in $enableval; do
+	IFS=$lt_save_ifs
+	if test "X$pkg" = "X$p"; then
+	  enable_static=yes
+	fi
+      done
+      IFS=$lt_save_ifs
+      ;;
+    esac],
+    [enable_static=]_LT_ENABLE_STATIC_DEFAULT)
+
+    _LT_DECL([build_old_libs], [enable_static], [0],
+	[Whether or not to build static libraries])
+])# _LT_ENABLE_STATIC
+
+LT_OPTION_DEFINE([LT_INIT], [static], [_LT_ENABLE_STATIC([yes])])
+LT_OPTION_DEFINE([LT_INIT], [disable-static], [_LT_ENABLE_STATIC([no])])
+
+# Old names:
+AC_DEFUN([AC_ENABLE_STATIC],
+[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[static])
+])
+
+AC_DEFUN([AC_DISABLE_STATIC],
+[_LT_SET_OPTION([LT_INIT], [disable-static])
+])
+
+AU_DEFUN([AM_ENABLE_STATIC], [AC_ENABLE_STATIC($@)])
+AU_DEFUN([AM_DISABLE_STATIC], [AC_DISABLE_STATIC($@)])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AM_ENABLE_STATIC], [])
+dnl AC_DEFUN([AM_DISABLE_STATIC], [])
+
+
+
+# _LT_ENABLE_FAST_INSTALL([DEFAULT])
+# ----------------------------------
+# implement the --enable-fast-install flag, and support the 'fast-install'
+# and 'disable-fast-install' LT_INIT options.
+# DEFAULT is either 'yes' or 'no'.  If omitted, it defaults to 'yes'.
+m4_define([_LT_ENABLE_FAST_INSTALL],
+[m4_define([_LT_ENABLE_FAST_INSTALL_DEFAULT], [m4_if($1, no, no, yes)])dnl
+AC_ARG_ENABLE([fast-install],
+    [AS_HELP_STRING([--enable-fast-install@<:@=PKGS@:>@],
+    [optimize for fast installation @<:@default=]_LT_ENABLE_FAST_INSTALL_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_fast_install=yes ;;
+    no) enable_fast_install=no ;;
+    *)
+      enable_fast_install=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR,
+      for pkg in $enableval; do
+	IFS=$lt_save_ifs
+	if test "X$pkg" = "X$p"; then
+	  enable_fast_install=yes
+	fi
+      done
+      IFS=$lt_save_ifs
+      ;;
+    esac],
+    [enable_fast_install=]_LT_ENABLE_FAST_INSTALL_DEFAULT)
+
+_LT_DECL([fast_install], [enable_fast_install], [0],
+	 [Whether or not to optimize for fast installation])dnl
+])# _LT_ENABLE_FAST_INSTALL
+
+LT_OPTION_DEFINE([LT_INIT], [fast-install], [_LT_ENABLE_FAST_INSTALL([yes])])
+LT_OPTION_DEFINE([LT_INIT], [disable-fast-install], [_LT_ENABLE_FAST_INSTALL([no])])
+
+# Old names:
+AU_DEFUN([AC_ENABLE_FAST_INSTALL],
+[_LT_SET_OPTION([LT_INIT], m4_if([$1], [no], [disable-])[fast-install])
+AC_DIAGNOSE([obsolete],
+[$0: Remove this warning and the call to _LT_SET_OPTION when you put
+the 'fast-install' option into LT_INIT's first parameter.])
+])
+
+AU_DEFUN([AC_DISABLE_FAST_INSTALL],
+[_LT_SET_OPTION([LT_INIT], [disable-fast-install])
+AC_DIAGNOSE([obsolete],
+[$0: Remove this warning and the call to _LT_SET_OPTION when you put
+the 'disable-fast-install' option into LT_INIT's first parameter.])
+])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_ENABLE_FAST_INSTALL], [])
+dnl AC_DEFUN([AM_DISABLE_FAST_INSTALL], [])
+
+
+# _LT_WITH_AIX_SONAME([DEFAULT])
+# ----------------------------------
+# implement the --with-aix-soname flag, and support the `aix-soname=aix'
+# and `aix-soname=both' and `aix-soname=svr4' LT_INIT options. DEFAULT
+# is either `aix', `both' or `svr4'.  If omitted, it defaults to `aix'.
+m4_define([_LT_WITH_AIX_SONAME],
+[m4_define([_LT_WITH_AIX_SONAME_DEFAULT], [m4_if($1, svr4, svr4, m4_if($1, both, both, aix))])dnl
+shared_archive_member_spec=
+case $host,$enable_shared in
+power*-*-aix[[5-9]]*,yes)
+  AC_MSG_CHECKING([which variant of shared library versioning to provide])
+  AC_ARG_WITH([aix-soname],
+    [AS_HELP_STRING([--with-aix-soname=aix|svr4|both],
+      [shared library versioning (aka "SONAME") variant to provide on AIX, @<:@default=]_LT_WITH_AIX_SONAME_DEFAULT[@:>@.])],
+    [case $withval in
+    aix|svr4|both)
+      ;;
+    *)
+      AC_MSG_ERROR([Unknown argument to --with-aix-soname])
+      ;;
+    esac
+    lt_cv_with_aix_soname=$with_aix_soname],
+    [AC_CACHE_VAL([lt_cv_with_aix_soname],
+      [lt_cv_with_aix_soname=]_LT_WITH_AIX_SONAME_DEFAULT)
+    with_aix_soname=$lt_cv_with_aix_soname])
+  AC_MSG_RESULT([$with_aix_soname])
+  if test aix != "$with_aix_soname"; then
+    # For the AIX way of multilib, we name the shared archive member
+    # based on the bitwidth used, traditionally 'shr.o' or 'shr_64.o',
+    # and 'shr.imp' or 'shr_64.imp', respectively, for the Import File.
+    # Even when GNU compilers ignore OBJECT_MODE but need '-maix64' flag,
+    # the AIX toolchain works better with OBJECT_MODE set (default 32).
+    if test 64 = "${OBJECT_MODE-32}"; then
+      shared_archive_member_spec=shr_64
+    else
+      shared_archive_member_spec=shr
+    fi
+  fi
+  ;;
+*)
+  with_aix_soname=aix
+  ;;
+esac
+
+_LT_DECL([], [shared_archive_member_spec], [0],
+    [Shared archive member basename, for filename based shared library versioning on AIX])dnl
+])# _LT_WITH_AIX_SONAME
+
+LT_OPTION_DEFINE([LT_INIT], [aix-soname=aix], [_LT_WITH_AIX_SONAME([aix])])
+LT_OPTION_DEFINE([LT_INIT], [aix-soname=both], [_LT_WITH_AIX_SONAME([both])])
+LT_OPTION_DEFINE([LT_INIT], [aix-soname=svr4], [_LT_WITH_AIX_SONAME([svr4])])
+
+
+# _LT_WITH_PIC([MODE])
+# --------------------
+# implement the --with-pic flag, and support the 'pic-only' and 'no-pic'
+# LT_INIT options.
+# MODE is either 'yes' or 'no'.  If omitted, it defaults to 'both'.
+m4_define([_LT_WITH_PIC],
+[AC_ARG_WITH([pic],
+    [AS_HELP_STRING([--with-pic@<:@=PKGS@:>@],
+	[try to use only PIC/non-PIC objects @<:@default=use both@:>@])],
+    [lt_p=${PACKAGE-default}
+    case $withval in
+    yes|no) pic_mode=$withval ;;
+    *)
+      pic_mode=default
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs=$IFS; IFS=$IFS$PATH_SEPARATOR,
+      for lt_pkg in $withval; do
+	IFS=$lt_save_ifs
+	if test "X$lt_pkg" = "X$lt_p"; then
+	  pic_mode=yes
+	fi
+      done
+      IFS=$lt_save_ifs
+      ;;
+    esac],
+    [pic_mode=m4_default([$1], [default])])
+
+_LT_DECL([], [pic_mode], [0], [What type of objects to build])dnl
+])# _LT_WITH_PIC
+
+LT_OPTION_DEFINE([LT_INIT], [pic-only], [_LT_WITH_PIC([yes])])
+LT_OPTION_DEFINE([LT_INIT], [no-pic], [_LT_WITH_PIC([no])])
+
+# Old name:
+AU_DEFUN([AC_LIBTOOL_PICMODE],
+[_LT_SET_OPTION([LT_INIT], [pic-only])
+AC_DIAGNOSE([obsolete],
+[$0: Remove this warning and the call to _LT_SET_OPTION when you
+put the 'pic-only' option into LT_INIT's first parameter.])
+])
+
+dnl aclocal-1.4 backwards compatibility:
+dnl AC_DEFUN([AC_LIBTOOL_PICMODE], [])
+
+## ----------------- ##
+## LTDL_INIT Options ##
+## ----------------- ##
+
+m4_define([_LTDL_MODE], [])
+LT_OPTION_DEFINE([LTDL_INIT], [nonrecursive],
+		 [m4_define([_LTDL_MODE], [nonrecursive])])
+LT_OPTION_DEFINE([LTDL_INIT], [recursive],
+		 [m4_define([_LTDL_MODE], [recursive])])
+LT_OPTION_DEFINE([LTDL_INIT], [subproject],
+		 [m4_define([_LTDL_MODE], [subproject])])
+
+m4_define([_LTDL_TYPE], [])
+LT_OPTION_DEFINE([LTDL_INIT], [installable],
+		 [m4_define([_LTDL_TYPE], [installable])])
+LT_OPTION_DEFINE([LTDL_INIT], [convenience],
+		 [m4_define([_LTDL_TYPE], [convenience])])
diff --git a/sgx_unwind/libunwind/m4/ltsugar.m4 b/sgx_unwind/libunwind/m4/ltsugar.m4
new file mode 100644
index 0000000..48bc934
--- /dev/null
+++ b/sgx_unwind/libunwind/m4/ltsugar.m4
@@ -0,0 +1,124 @@
+# ltsugar.m4 -- libtool m4 base layer.                         -*-Autoconf-*-
+#
+# Copyright (C) 2004-2005, 2007-2008, 2011-2015 Free Software
+# Foundation, Inc.
+# Written by Gary V. Vaughan, 2004
+#
+# This file is free software; the Free Software Foundation gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+# serial 6 ltsugar.m4
+
+# This is to help aclocal find these macros, as it can't see m4_define.
+AC_DEFUN([LTSUGAR_VERSION], [m4_if([0.1])])
+
+
+# lt_join(SEP, ARG1, [ARG2...])
+# -----------------------------
+# Produce ARG1SEPARG2...SEPARGn, omitting [] arguments and their
+# associated separator.
+# Needed until we can rely on m4_join from Autoconf 2.62, since all earlier
+# versions in m4sugar had bugs.
+m4_define([lt_join],
+[m4_if([$#], [1], [],
+       [$#], [2], [[$2]],
+       [m4_if([$2], [], [], [[$2]_])$0([$1], m4_shift(m4_shift($@)))])])
+m4_define([_lt_join],
+[m4_if([$#$2], [2], [],
+       [m4_if([$2], [], [], [[$1$2]])$0([$1], m4_shift(m4_shift($@)))])])
+
+
+# lt_car(LIST)
+# lt_cdr(LIST)
+# ------------
+# Manipulate m4 lists.
+# These macros are necessary as long as will still need to support
+# Autoconf-2.59, which quotes differently.
+m4_define([lt_car], [[$1]])
+m4_define([lt_cdr],
+[m4_if([$#], 0, [m4_fatal([$0: cannot be called without arguments])],
+       [$#], 1, [],
+       [m4_dquote(m4_shift($@))])])
+m4_define([lt_unquote], $1)
+
+
+# lt_append(MACRO-NAME, STRING, [SEPARATOR])
+# ------------------------------------------
+# Redefine MACRO-NAME to hold its former content plus 'SEPARATOR''STRING'.
+# Note that neither SEPARATOR nor STRING are expanded; they are appended
+# to MACRO-NAME as is (leaving the expansion for when MACRO-NAME is invoked).
+# No SEPARATOR is output if MACRO-NAME was previously undefined (different
+# than defined and empty).
+#
+# This macro is needed until we can rely on Autoconf 2.62, since earlier
+# versions of m4sugar mistakenly expanded SEPARATOR but not STRING.
+m4_define([lt_append],
+[m4_define([$1],
+	   m4_ifdef([$1], [m4_defn([$1])[$3]])[$2])])
+
+
+
+# lt_combine(SEP, PREFIX-LIST, INFIX, SUFFIX1, [SUFFIX2...])
+# ----------------------------------------------------------
+# Produce a SEP delimited list of all paired combinations of elements of
+# PREFIX-LIST with SUFFIX1 through SUFFIXn.  Each element of the list
+# has the form PREFIXmINFIXSUFFIXn.
+# Needed until we can rely on m4_combine added in Autoconf 2.62.
+m4_define([lt_combine],
+[m4_if(m4_eval([$# > 3]), [1],
+       [m4_pushdef([_Lt_sep], [m4_define([_Lt_sep], m4_defn([lt_car]))])]]dnl
+[[m4_foreach([_Lt_prefix], [$2],
+	     [m4_foreach([_Lt_suffix],
+		]m4_dquote(m4_dquote(m4_shift(m4_shift(m4_shift($@)))))[,
+	[_Lt_sep([$1])[]m4_defn([_Lt_prefix])[$3]m4_defn([_Lt_suffix])])])])])
+
+
+# lt_if_append_uniq(MACRO-NAME, VARNAME, [SEPARATOR], [UNIQ], [NOT-UNIQ])
+# -----------------------------------------------------------------------
+# Iff MACRO-NAME does not yet contain VARNAME, then append it (delimited
+# by SEPARATOR if supplied) and expand UNIQ, else NOT-UNIQ.
+m4_define([lt_if_append_uniq],
+[m4_ifdef([$1],
+	  [m4_if(m4_index([$3]m4_defn([$1])[$3], [$3$2$3]), [-1],
+		 [lt_append([$1], [$2], [$3])$4],
+		 [$5])],
+	  [lt_append([$1], [$2], [$3])$4])])
+
+
+# lt_dict_add(DICT, KEY, VALUE)
+# -----------------------------
+m4_define([lt_dict_add],
+[m4_define([$1($2)], [$3])])
+
+
+# lt_dict_add_subkey(DICT, KEY, SUBKEY, VALUE)
+# --------------------------------------------
+m4_define([lt_dict_add_subkey],
+[m4_define([$1($2:$3)], [$4])])
+
+
+# lt_dict_fetch(DICT, KEY, [SUBKEY])
+# ----------------------------------
+m4_define([lt_dict_fetch],
+[m4_ifval([$3],
+	m4_ifdef([$1($2:$3)], [m4_defn([$1($2:$3)])]),
+    m4_ifdef([$1($2)], [m4_defn([$1($2)])]))])
+
+
+# lt_if_dict_fetch(DICT, KEY, [SUBKEY], VALUE, IF-TRUE, [IF-FALSE])
+# -----------------------------------------------------------------
+m4_define([lt_if_dict_fetch],
+[m4_if(lt_dict_fetch([$1], [$2], [$3]), [$4],
+	[$5],
+    [$6])])
+
+
+# lt_dict_filter(DICT, [SUBKEY], VALUE, [SEPARATOR], KEY, [...])
+# --------------------------------------------------------------
+m4_define([lt_dict_filter],
+[m4_if([$5], [], [],
+  [lt_join(m4_quote(m4_default([$4], [[, ]])),
+           lt_unquote(m4_split(m4_normalize(m4_foreach(_Lt_key, lt_car([m4_shiftn(4, $@)]),
+		      [lt_if_dict_fetch([$1], _Lt_key, [$2], [$3], [_Lt_key ])])))))])[]dnl
+])
diff --git a/sgx_unwind/libunwind/m4/ltversion.m4 b/sgx_unwind/libunwind/m4/ltversion.m4
new file mode 100644
index 0000000..fa04b52
--- /dev/null
+++ b/sgx_unwind/libunwind/m4/ltversion.m4
@@ -0,0 +1,23 @@
+# ltversion.m4 -- version numbers			-*- Autoconf -*-
+#
+#   Copyright (C) 2004, 2011-2015 Free Software Foundation, Inc.
+#   Written by Scott James Remnant, 2004
+#
+# This file is free software; the Free Software Foundation gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+# @configure_input@
+
+# serial 4179 ltversion.m4
+# This file is part of GNU Libtool
+
+m4_define([LT_PACKAGE_VERSION], [2.4.6])
+m4_define([LT_PACKAGE_REVISION], [2.4.6])
+
+AC_DEFUN([LTVERSION_VERSION],
+[macro_version='2.4.6'
+macro_revision='2.4.6'
+_LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?])
+_LT_DECL(, macro_revision, 0)
+])
diff --git a/sgx_unwind/libunwind/m4/lt~obsolete.m4 b/sgx_unwind/libunwind/m4/lt~obsolete.m4
new file mode 100644
index 0000000..c6b26f8
--- /dev/null
+++ b/sgx_unwind/libunwind/m4/lt~obsolete.m4
@@ -0,0 +1,99 @@
+# lt~obsolete.m4 -- aclocal satisfying obsolete definitions.    -*-Autoconf-*-
+#
+#   Copyright (C) 2004-2005, 2007, 2009, 2011-2015 Free Software
+#   Foundation, Inc.
+#   Written by Scott James Remnant, 2004.
+#
+# This file is free software; the Free Software Foundation gives
+# unlimited permission to copy and/or distribute it, with or without
+# modifications, as long as this notice is preserved.
+
+# serial 5 lt~obsolete.m4
+
+# These exist entirely to fool aclocal when bootstrapping libtool.
+#
+# In the past libtool.m4 has provided macros via AC_DEFUN (or AU_DEFUN),
+# which have later been changed to m4_define as they aren't part of the
+# exported API, or moved to Autoconf or Automake where they belong.
+#
+# The trouble is, aclocal is a bit thick.  It'll see the old AC_DEFUN
+# in /usr/share/aclocal/libtool.m4 and remember it, then when it sees us
+# using a macro with the same name in our local m4/libtool.m4 it'll
+# pull the old libtool.m4 in (it doesn't see our shiny new m4_define
+# and doesn't know about Autoconf macros at all.)
+#
+# So we provide this file, which has a silly filename so it's always
+# included after everything else.  This provides aclocal with the
+# AC_DEFUNs it wants, but when m4 processes it, it doesn't do anything
+# because those macros already exist, or will be overwritten later.
+# We use AC_DEFUN over AU_DEFUN for compatibility with aclocal-1.6.
+#
+# Anytime we withdraw an AC_DEFUN or AU_DEFUN, remember to add it here.
+# Yes, that means every name once taken will need to remain here until
+# we give up compatibility with versions before 1.7, at which point
+# we need to keep only those names which we still refer to.
+
+# This is to help aclocal find these macros, as it can't see m4_define.
+AC_DEFUN([LTOBSOLETE_VERSION], [m4_if([1])])
+
+m4_ifndef([AC_LIBTOOL_LINKER_OPTION],	[AC_DEFUN([AC_LIBTOOL_LINKER_OPTION])])
+m4_ifndef([AC_PROG_EGREP],		[AC_DEFUN([AC_PROG_EGREP])])
+m4_ifndef([_LT_AC_PROG_ECHO_BACKSLASH],	[AC_DEFUN([_LT_AC_PROG_ECHO_BACKSLASH])])
+m4_ifndef([_LT_AC_SHELL_INIT],		[AC_DEFUN([_LT_AC_SHELL_INIT])])
+m4_ifndef([_LT_AC_SYS_LIBPATH_AIX],	[AC_DEFUN([_LT_AC_SYS_LIBPATH_AIX])])
+m4_ifndef([_LT_PROG_LTMAIN],		[AC_DEFUN([_LT_PROG_LTMAIN])])
+m4_ifndef([_LT_AC_TAGVAR],		[AC_DEFUN([_LT_AC_TAGVAR])])
+m4_ifndef([AC_LTDL_ENABLE_INSTALL],	[AC_DEFUN([AC_LTDL_ENABLE_INSTALL])])
+m4_ifndef([AC_LTDL_PREOPEN],		[AC_DEFUN([AC_LTDL_PREOPEN])])
+m4_ifndef([_LT_AC_SYS_COMPILER],	[AC_DEFUN([_LT_AC_SYS_COMPILER])])
+m4_ifndef([_LT_AC_LOCK],		[AC_DEFUN([_LT_AC_LOCK])])
+m4_ifndef([AC_LIBTOOL_SYS_OLD_ARCHIVE],	[AC_DEFUN([AC_LIBTOOL_SYS_OLD_ARCHIVE])])
+m4_ifndef([_LT_AC_TRY_DLOPEN_SELF],	[AC_DEFUN([_LT_AC_TRY_DLOPEN_SELF])])
+m4_ifndef([AC_LIBTOOL_PROG_CC_C_O],	[AC_DEFUN([AC_LIBTOOL_PROG_CC_C_O])])
+m4_ifndef([AC_LIBTOOL_SYS_HARD_LINK_LOCKS], [AC_DEFUN([AC_LIBTOOL_SYS_HARD_LINK_LOCKS])])
+m4_ifndef([AC_LIBTOOL_OBJDIR],		[AC_DEFUN([AC_LIBTOOL_OBJDIR])])
+m4_ifndef([AC_LTDL_OBJDIR],		[AC_DEFUN([AC_LTDL_OBJDIR])])
+m4_ifndef([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH], [AC_DEFUN([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH])])
+m4_ifndef([AC_LIBTOOL_SYS_LIB_STRIP],	[AC_DEFUN([AC_LIBTOOL_SYS_LIB_STRIP])])
+m4_ifndef([AC_PATH_MAGIC],		[AC_DEFUN([AC_PATH_MAGIC])])
+m4_ifndef([AC_PROG_LD_GNU],		[AC_DEFUN([AC_PROG_LD_GNU])])
+m4_ifndef([AC_PROG_LD_RELOAD_FLAG],	[AC_DEFUN([AC_PROG_LD_RELOAD_FLAG])])
+m4_ifndef([AC_DEPLIBS_CHECK_METHOD],	[AC_DEFUN([AC_DEPLIBS_CHECK_METHOD])])
+m4_ifndef([AC_LIBTOOL_PROG_COMPILER_NO_RTTI], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_NO_RTTI])])
+m4_ifndef([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE], [AC_DEFUN([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE])])
+m4_ifndef([AC_LIBTOOL_PROG_COMPILER_PIC], [AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_PIC])])
+m4_ifndef([AC_LIBTOOL_PROG_LD_SHLIBS],	[AC_DEFUN([AC_LIBTOOL_PROG_LD_SHLIBS])])
+m4_ifndef([AC_LIBTOOL_POSTDEP_PREDEP],	[AC_DEFUN([AC_LIBTOOL_POSTDEP_PREDEP])])
+m4_ifndef([LT_AC_PROG_EGREP],		[AC_DEFUN([LT_AC_PROG_EGREP])])
+m4_ifndef([LT_AC_PROG_SED],		[AC_DEFUN([LT_AC_PROG_SED])])
+m4_ifndef([_LT_CC_BASENAME],		[AC_DEFUN([_LT_CC_BASENAME])])
+m4_ifndef([_LT_COMPILER_BOILERPLATE],	[AC_DEFUN([_LT_COMPILER_BOILERPLATE])])
+m4_ifndef([_LT_LINKER_BOILERPLATE],	[AC_DEFUN([_LT_LINKER_BOILERPLATE])])
+m4_ifndef([_AC_PROG_LIBTOOL],		[AC_DEFUN([_AC_PROG_LIBTOOL])])
+m4_ifndef([AC_LIBTOOL_SETUP],		[AC_DEFUN([AC_LIBTOOL_SETUP])])
+m4_ifndef([_LT_AC_CHECK_DLFCN],		[AC_DEFUN([_LT_AC_CHECK_DLFCN])])
+m4_ifndef([AC_LIBTOOL_SYS_DYNAMIC_LINKER],	[AC_DEFUN([AC_LIBTOOL_SYS_DYNAMIC_LINKER])])
+m4_ifndef([_LT_AC_TAGCONFIG],		[AC_DEFUN([_LT_AC_TAGCONFIG])])
+m4_ifndef([AC_DISABLE_FAST_INSTALL],	[AC_DEFUN([AC_DISABLE_FAST_INSTALL])])
+m4_ifndef([_LT_AC_LANG_CXX],		[AC_DEFUN([_LT_AC_LANG_CXX])])
+m4_ifndef([_LT_AC_LANG_F77],		[AC_DEFUN([_LT_AC_LANG_F77])])
+m4_ifndef([_LT_AC_LANG_GCJ],		[AC_DEFUN([_LT_AC_LANG_GCJ])])
+m4_ifndef([AC_LIBTOOL_LANG_C_CONFIG],	[AC_DEFUN([AC_LIBTOOL_LANG_C_CONFIG])])
+m4_ifndef([_LT_AC_LANG_C_CONFIG],	[AC_DEFUN([_LT_AC_LANG_C_CONFIG])])
+m4_ifndef([AC_LIBTOOL_LANG_CXX_CONFIG],	[AC_DEFUN([AC_LIBTOOL_LANG_CXX_CONFIG])])
+m4_ifndef([_LT_AC_LANG_CXX_CONFIG],	[AC_DEFUN([_LT_AC_LANG_CXX_CONFIG])])
+m4_ifndef([AC_LIBTOOL_LANG_F77_CONFIG],	[AC_DEFUN([AC_LIBTOOL_LANG_F77_CONFIG])])
+m4_ifndef([_LT_AC_LANG_F77_CONFIG],	[AC_DEFUN([_LT_AC_LANG_F77_CONFIG])])
+m4_ifndef([AC_LIBTOOL_LANG_GCJ_CONFIG],	[AC_DEFUN([AC_LIBTOOL_LANG_GCJ_CONFIG])])
+m4_ifndef([_LT_AC_LANG_GCJ_CONFIG],	[AC_DEFUN([_LT_AC_LANG_GCJ_CONFIG])])
+m4_ifndef([AC_LIBTOOL_LANG_RC_CONFIG],	[AC_DEFUN([AC_LIBTOOL_LANG_RC_CONFIG])])
+m4_ifndef([_LT_AC_LANG_RC_CONFIG],	[AC_DEFUN([_LT_AC_LANG_RC_CONFIG])])
+m4_ifndef([AC_LIBTOOL_CONFIG],		[AC_DEFUN([AC_LIBTOOL_CONFIG])])
+m4_ifndef([_LT_AC_FILE_LTDLL_C],	[AC_DEFUN([_LT_AC_FILE_LTDLL_C])])
+m4_ifndef([_LT_REQUIRED_DARWIN_CHECKS],	[AC_DEFUN([_LT_REQUIRED_DARWIN_CHECKS])])
+m4_ifndef([_LT_AC_PROG_CXXCPP],		[AC_DEFUN([_LT_AC_PROG_CXXCPP])])
+m4_ifndef([_LT_PREPARE_SED_QUOTE_VARS],	[AC_DEFUN([_LT_PREPARE_SED_QUOTE_VARS])])
+m4_ifndef([_LT_PROG_ECHO_BACKSLASH],	[AC_DEFUN([_LT_PROG_ECHO_BACKSLASH])])
+m4_ifndef([_LT_PROG_F77],		[AC_DEFUN([_LT_PROG_F77])])
+m4_ifndef([_LT_PROG_FC],		[AC_DEFUN([_LT_PROG_FC])])
+m4_ifndef([_LT_PROG_CXX],		[AC_DEFUN([_LT_PROG_CXX])])
diff --git a/sgx_unwind/libunwind/src/Makefile.am b/sgx_unwind/libunwind/src/Makefile.am
index 17fce10..633b9ac 100644
--- a/sgx_unwind/libunwind/src/Makefile.am
+++ b/sgx_unwind/libunwind/src/Makefile.am
@@ -1,48 +1,76 @@
-SOVERSION=8:0:0		# See comments at end of file.
+SOVERSION=8:1:0		# See comments at end of file.
 SETJMP_SO_VERSION=0:0:0
+COREDUMP_SO_VERSION=0:0:0
 #
 # Don't link with start-files since we don't use any constructors/destructors:
 #
-COMMON_SO_LDFLAGS =	-XCClinker -nostartfiles
+COMMON_SO_LDFLAGS = $(LDFLAGS_NOSTARTFILES)
 SGX_SDK ?= /opt/sgxsdk
 
 lib_LIBRARIES =
 lib_LTLIBRARIES =
 if !REMOTE_ONLY
-#lib_LIBRARIES += libunwind-ptrace.a libunwind-coredump.a
 lib_LTLIBRARIES += libunwind.la
+# if BUILD_PTRACE
+# lib_LTLIBRARIES += libunwind-ptrace.la
+# endif
+# if BUILD_COREDUMP
+# lib_LTLIBRARIES += libunwind-coredump.la
+# endif
 endif
 
 noinst_HEADERS =
 noinst_LTLIBRARIES =
 
-### libunwind-ptrace:
-#libunwind_ptrace_a_SOURCES =						  \
-#	ptrace/_UPT_elf.c						  \
-#	ptrace/_UPT_accessors.c ptrace/_UPT_access_fpreg.c		  \
-#	ptrace/_UPT_access_mem.c ptrace/_UPT_access_reg.c		  \
-#	ptrace/_UPT_create.c ptrace/_UPT_destroy.c			  \
-#	ptrace/_UPT_find_proc_info.c ptrace/_UPT_get_dyn_info_list_addr.c \
-#	ptrace/_UPT_put_unwind_info.c ptrace/_UPT_get_proc_name.c	  \
-#	ptrace/_UPT_reg_offset.c ptrace/_UPT_resume.c
-#noinst_HEADERS += ptrace/_UPT_internal.h
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = libunwind-generic.pc
 
-#### libunwind-coredump:
-#libunwind_coredump_a_SOURCES = \
-#	coredump/_UCD_accessors.c \
-#	coredump/_UCD_create.c \
-#	coredump/_UCD_destroy.c \
-#	coredump/_UCD_access_mem.c \
-#	coredump/_UCD_elf_map_image.c \
-#	coredump/_UCD_find_proc_info.c \
-#	coredump/_UCD_get_proc_name.c \
-#	\
-#	coredump/_UPT_elf.c \
-#	coredump/_UPT_access_fpreg.c \
-#	coredump/_UPT_get_dyn_info_list_addr.c \
-#	coredump/_UPT_put_unwind_info.c \
-#	coredump/_UPT_resume.c
-#noinst_HEADERS += coredump/_UCD_internal.h
+if !REMOTE_ONLY
+pkgconfig_DATA += unwind/libunwind.pc
+endif
+
+# if BUILD_PTRACE
+# pkgconfig_DATA += ptrace/libunwind-ptrace.pc
+# endif
+
+if BUILD_SETJMP
+pkgconfig_DATA += setjmp/libunwind-setjmp.pc
+endif
+
+# if BUILD_COREDUMP
+# pkgconfig_DATA += coredump/libunwind-coredump.pc
+# endif
+
+# ### libunwind-ptrace:
+# libunwind_ptrace_la_SOURCES =						  \
+# 	ptrace/_UPT_elf.c						  \
+# 	ptrace/_UPT_accessors.c ptrace/_UPT_access_fpreg.c		  \
+# 	ptrace/_UPT_access_mem.c ptrace/_UPT_access_reg.c		  \
+# 	ptrace/_UPT_create.c ptrace/_UPT_destroy.c			  \
+# 	ptrace/_UPT_find_proc_info.c ptrace/_UPT_get_dyn_info_list_addr.c \
+# 	ptrace/_UPT_put_unwind_info.c ptrace/_UPT_get_proc_name.c	  \
+# 	ptrace/_UPT_reg_offset.c ptrace/_UPT_resume.c
+# noinst_HEADERS += ptrace/_UPT_internal.h
+
+# ### libunwind-coredump:
+# libunwind_coredump_la_SOURCES = \
+# 	coredump/_UCD_accessors.c \
+# 	coredump/_UCD_create.c \
+# 	coredump/_UCD_destroy.c \
+# 	coredump/_UCD_access_mem.c \
+# 	coredump/_UCD_elf_map_image.c \
+# 	coredump/_UCD_find_proc_info.c \
+# 	coredump/_UCD_get_proc_name.c \
+# 	\
+# 	coredump/_UPT_elf.c \
+# 	coredump/_UPT_access_fpreg.c \
+# 	coredump/_UPT_get_dyn_info_list_addr.c \
+# 	coredump/_UPT_put_unwind_info.c \
+# 	coredump/_UPT_resume.c
+# libunwind_coredump_la_LDFLAGS = $(COMMON_SO_LDFLAGS) \
+# 				-version-info $(COREDUMP_SO_VERSION)
+# libunwind_coredump_la_LIBADD = $(LIBLZMA)
+# noinst_HEADERS += coredump/_UCD_internal.h coredump/_UCD_lib.h
 
 ### libunwind-setjmp:
 libunwind_setjmp_la_LDFLAGS		= $(COMMON_SO_LDFLAGS)		     \
@@ -72,43 +100,46 @@
 # libraries:
 libunwind_la_SOURCES_common =					\
 	$(libunwind_la_SOURCES_os)				\
-	mi/init.c mi/flush_cache.c mi/mempool.c mi/strerror.c
+	mi/init.c mi/flush_cache.c mi/mempool.c
 
 # List of arch-independent files needed by generic library (libunwind-$ARCH):
 libunwind_la_SOURCES_generic =						\
+	mi/Gdyn-extract.c mi/Gfind_dynamic_proc_info.c	\
 	mi/Gget_accessors.c						\
-	mi/Gput_dynamic_unwind_info.c mi/Gdestroy_addr_space.c		\
+	mi/Gget_proc_name.c			\
+	mi/Gput_dynamic_unwind_info.c		\
 	mi/Gget_reg.c mi/Gset_reg.c					\
-	mi/Gget_fpreg.c mi/Gset_fpreg.c					\
 	mi/Gset_caching_policy.c
 
 if SUPPORT_CXX_EXCEPTIONS
 libunwind_la_SOURCES_local_unwind =					\
-	unwind/Backtrace.c unwind/DeleteException.c			\
-	unwind/FindEnclosingFunction.c unwind/ForcedUnwind.c		\
-	unwind/GetBSP.c unwind/GetCFA.c unwind/GetDataRelBase.c		\
-	unwind/GetGR.c unwind/GetIP.c unwind/GetLanguageSpecificData.c	\
+	unwind/Backtrace.c			\
+	unwind/GetDataRelBase.c			\
+	unwind/GetCFA.c			\
+	unwind/FindEnclosingFunction.c		\
+	unwind/DeleteException.c			\
+	unwind/GetIPInfo.c		\
+	unwind/GetIP.c unwind/GetLanguageSpecificData.c	\
 	unwind/GetRegionStart.c unwind/GetTextRelBase.c			\
 	unwind/RaiseException.c unwind/Resume.c				\
-	unwind/Resume_or_Rethrow.c unwind/SetGR.c unwind/SetIP.c	\
-	unwind/GetIPInfo.c
+	unwind/Resume_or_Rethrow.c unwind/SetGR.c unwind/SetIP.c
 
 #  _ReadULEB()/_ReadSLEB() are needed for Intel C++ 8.0 compatibility
-libunwind_la_SOURCES_os_linux_local = mi/_ReadULEB.c mi/_ReadSLEB.c
+#libunwind_la_SOURCES_os_linux_local = mi/_ReadULEB.c mi/_ReadSLEB.c
 endif
 
 # List of arch-independent files needed by local-only library (libunwind):
 libunwind_la_SOURCES_local_nounwind =					\
 	$(libunwind_la_SOURCES_os_local)				\
-	mi/backtrace.c							\
-	mi/dyn-info-list.c						\
+	mi/dyn-info-list.c		\
 	mi/Ldyn-extract.c mi/Lfind_dynamic_proc_info.c			\
 	mi/Lget_accessors.c						\
 	mi/Lget_proc_info_by_ip.c mi/Lget_proc_name.c			\
 	mi/Lput_dynamic_unwind_info.c mi/Ldestroy_addr_space.c		\
 	mi/Lget_reg.c   mi/Lset_reg.c					\
 	mi/Lget_fpreg.c mi/Lset_fpreg.c					\
-	mi/Lset_caching_policy.c
+	mi/Lset_caching_policy.c					\
+	mi/Lset_cache_size.c
 
 libunwind_la_SOURCES_local =						\
 	$(libunwind_la_SOURCES_local_nounwind)				\
@@ -117,25 +148,27 @@
 noinst_HEADERS += os-linux.h
 libunwind_la_SOURCES_os_linux = os-linux.c
 
-libunwind_la_SOURCES_os_hpux = os-hpux.c
+# libunwind_la_SOURCES_os_hpux = os-hpux.c
 
-libunwind_la_SOURCES_os_freebsd = os-freebsd.c
+# libunwind_la_SOURCES_os_freebsd = os-freebsd.c
+
+# libunwind_la_SOURCES_os_qnx = os-qnx.c
 
 libunwind_dwarf_common_la_SOURCES = dwarf/global.c
 
 libunwind_dwarf_local_la_SOURCES = \
-	dwarf/Lexpr.c dwarf/Lfde.c dwarf/Lparser.c dwarf/Lpe.c dwarf/Lstep.c \
+	dwarf/Lexpr.c dwarf/Lfde.c dwarf/Lparser.c dwarf/Lpe.c \
 	dwarf/Lfind_proc_info-lsb.c \
 	dwarf/Lfind_unwind_table.c
 libunwind_dwarf_local_la_LIBADD = libunwind-dwarf-common.la
 
 libunwind_dwarf_generic_la_SOURCES = \
-	dwarf/Gexpr.c dwarf/Gfde.c dwarf/Gparser.c dwarf/Gpe.c dwarf/Gstep.c \
-	dwarf/Gfind_unwind_table.c
+	dwarf/Gexpr.c dwarf/Gfde.c dwarf/Gparser.c dwarf/Gpe.c \
+	dwarf/Gfind_proc_info-lsb.c
 libunwind_dwarf_generic_la_LIBADD = libunwind-dwarf-common.la
 
 if USE_DWARF
- noinst_LTLIBRARIES += libunwind-dwarf-common.la libunwind-dwarf-generic.la
+ noinst_LTLIBRARIES += libunwind-dwarf-common.la #libunwind-dwarf-generic.la
 if !REMOTE_ONLY
  noinst_LTLIBRARIES += libunwind-dwarf-local.la
 endif
@@ -146,135 +179,198 @@
 
 libunwind_elf32_la_SOURCES = se-libc-stubs.c se-iterate-phdr.c
 libunwind_elf64_la_SOURCES = se-libc-stubs.c se-iterate-phdr.c
-libunwind_elfxx_la_SOURCES = elfxx.c
+#libunwind_elfxx_la_SOURCES = elfxx.c
+libunwind_elf32_la_LIBADD  = $(LIBLZMA)
+libunwind_elf64_la_LIBADD  = $(LIBLZMA)
+libunwind_elfxx_la_LIBADD  = $(LIBLZMA)
 
 noinst_LTLIBRARIES += $(LIBUNWIND_ELF)
 libunwind_la_LIBADD += $(LIBUNWIND_ELF)
 
-## The list of files that go into libunwind and libunwind-arm:
-#noinst_HEADERS += arm/init.h arm/offsets.h arm/unwind_i.h
-#libunwind_la_SOURCES_arm_common = $(libunwind_la_SOURCES_common)	    \
-#	arm/is_fpreg.c arm/regname.c					    \
-#	arm/ex_tables.h
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_arm = $(libunwind_la_SOURCES_arm_common)		    \
-#	$(libunwind_la_SOURCES_local)					    \
-#	arm/getcontext.S						    \
-#	arm/Lcreate_addr_space.c arm/Lget_proc_info.c arm/Lget_save_loc.c   \
-#	arm/Lglobal.c arm/Linit.c arm/Linit_local.c arm/Linit_remote.c	    \
-#	arm/Lis_signal_frame.c arm/Lregs.c arm/Lresume.c arm/Lstep.c	    \
-#	arm/Lex_tables.c
-#
-#libunwind_arm_la_SOURCES_arm = $(libunwind_la_SOURCES_arm_common)	    \
-#	$(libunwind_la_SOURCES_generic)					    \
-#	arm/Gcreate_addr_space.c arm/Gget_proc_info.c arm/Gget_save_loc.c   \
-#	arm/Gglobal.c arm/Ginit.c arm/Ginit_local.c arm/Ginit_remote.c	    \
-#	arm/Gis_signal_frame.c arm/Gregs.c arm/Gresume.c arm/Gstep.c	    \
-#	arm/Gex_tables.c
-#
-## The list of files that go both into libunwind and libunwind-ia64:
-#noinst_HEADERS += ia64/init.h ia64/offsets.h ia64/regs.h		    \
-#	ia64/ucontext_i.h ia64/unwind_decoder.h ia64/unwind_i.h
-#libunwind_la_SOURCES_ia64_common = $(libunwind_la_SOURCES_common)	    \
-#	ia64/regname.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_ia64 = $(libunwind_la_SOURCES_ia64_common)		     \
-#	$(libunwind_la_SOURCES_local)					     \
-#									     \
-#	ia64/dyn_info_list.S ia64/getcontext.S				     \
-#									     \
-#	ia64/Lcreate_addr_space.c ia64/Lget_proc_info.c ia64/Lget_save_loc.c \
-#	ia64/Lglobal.c ia64/Linit.c ia64/Linit_local.c ia64/Linit_remote.c   \
-#	ia64/Linstall_cursor.S ia64/Lis_signal_frame.c ia64/Lparser.c	     \
-#	ia64/Lrbs.c ia64/Lregs.c ia64/Lresume.c ia64/Lscript.c ia64/Lstep.c  \
-#	ia64/Ltables.c ia64/Lfind_unwind_table.c
-#
-## The list of files that go into libunwind-ia64:
-#libunwind_ia64_la_SOURCES_ia64 = $(libunwind_la_SOURCES_ia64_common)	     \
-#	$(libunwind_la_SOURCES_generic)					     \
-#	ia64/Gcreate_addr_space.c ia64/Gget_proc_info.c ia64/Gget_save_loc.c \
-#	ia64/Gglobal.c ia64/Ginit.c ia64/Ginit_local.c ia64/Ginit_remote.c   \
-#	ia64/Ginstall_cursor.S ia64/Gis_signal_frame.c ia64/Gparser.c	     \
-#	ia64/Grbs.c ia64/Gregs.c ia64/Gresume.c ia64/Gscript.c ia64/Gstep.c  \
-#	ia64/Gtables.c ia64/Gfind_unwind_table.c
-#
-## The list of files that go both into libunwind and libunwind-hppa:
-#noinst_HEADERS += hppa/init.h hppa/offsets.h hppa/unwind_i.h
-#libunwind_la_SOURCES_hppa_common = $(libunwind_la_SOURCES_common)	\
-#	hppa/regname.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_hppa = $(libunwind_la_SOURCES_hppa_common)		\
-#	$(libunwind_la_SOURCES_local)					\
-#	hppa/getcontext.S hppa/setcontext.S				\
-#	hppa/Lcreate_addr_space.c hppa/Lget_save_loc.c hppa/Lglobal.c	\
-#	hppa/Linit.c hppa/Linit_local.c hppa/Linit_remote.c		\
-#	hppa/Lis_signal_frame.c hppa/Lget_proc_info.c hppa/Lregs.c	\
-#	hppa/Lresume.c hppa/Lstep.c
-#
-## The list of files that go into libunwind-hppa:
-#libunwind_hppa_la_SOURCES_hppa = $(libunwind_la_SOURCES_hppa_common)	\
-#	$(libunwind_la_SOURCES_generic)					\
-#	hppa/Gcreate_addr_space.c hppa/Gget_save_loc.c hppa/Gglobal.c	\
-#	hppa/Ginit.c hppa/Ginit_local.c hppa/Ginit_remote.c		\
-#	hppa/Gis_signal_frame.c hppa/Gget_proc_info.c hppa/Gregs.c	\
-#	hppa/Gresume.c hppa/Gstep.c
-#
-## The list of files that go info libunwind and libunwind-mips:
-#noinst_HEADERS += mips/init.h mips/offsets.h
-#libunwind_la_SOURCES_mips_common = $(libunwind_la_SOURCES_common)	    \
-#	mips/is_fpreg.c mips/regname.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_mips = $(libunwind_la_SOURCES_mips_common)		    \
-#	$(libunwind_la_SOURCES_local)					    \
-#	mips/getcontext.S						    \
-#	mips/Lcreate_addr_space.c mips/Lget_proc_info.c mips/Lget_save_loc.c   \
-#	mips/Lglobal.c mips/Linit.c mips/Linit_local.c mips/Linit_remote.c  \
-#	mips/Lis_signal_frame.c mips/Lregs.c mips/Lresume.c mips/Lstep.c
-#
-#libunwind_mips_la_SOURCES_mips = $(libunwind_la_SOURCES_mips_common)	    \
-#	$(libunwind_la_SOURCES_generic)					    \
-#	mips/Gcreate_addr_space.c mips/Gget_proc_info.c mips/Gget_save_loc.c   \
-#	mips/Gglobal.c mips/Ginit.c mips/Ginit_local.c mips/Ginit_remote.c	    \
-#	mips/Gis_signal_frame.c mips/Gregs.c mips/Gresume.c mips/Gstep.c
-#
-## The list of files that go both into libunwind and libunwind-x86:
-#noinst_HEADERS += x86/init.h x86/offsets.h x86/unwind_i.h
-#libunwind_la_SOURCES_x86_common = $(libunwind_la_SOURCES_common)	\
-#	x86/is_fpreg.c x86/regname.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_x86 = $(libunwind_la_SOURCES_x86_common)		\
-#        $(libunwind_la_SOURCES_x86_os_local)				\
-#	$(libunwind_la_SOURCES_local)					\
-#	x86/Lcreate_addr_space.c x86/Lget_save_loc.c x86/Lglobal.c	\
-#	x86/Linit.c x86/Linit_local.c x86/Linit_remote.c		\
-#	x86/Lget_proc_info.c x86/Lregs.c				\
-#	x86/Lresume.c x86/Lstep.c
-#
-## The list of files that go into libunwind-x86:
-#libunwind_x86_la_SOURCES_x86 = $(libunwind_la_SOURCES_x86_common)	\
-#        $(libunwind_la_SOURCES_x86_os)					\
-#	$(libunwind_la_SOURCES_generic)					\
-#	x86/Gcreate_addr_space.c x86/Gget_save_loc.c x86/Gglobal.c	\
-#	x86/Ginit.c x86/Ginit_local.c x86/Ginit_remote.c		\
-#	x86/Gget_proc_info.c x86/Gregs.c				\
-#	x86/Gresume.c x86/Gstep.c
-#
+# # The list of files that go into libunwind and libunwind-aarch64:
+# noinst_HEADERS += aarch64/init.h aarch64/offsets.h aarch64/unwind_i.h
+# libunwind_la_SOURCES_aarch64_common = $(libunwind_la_SOURCES_common)	    \
+# 	aarch64/is_fpreg.c aarch64/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_aarch64 = $(libunwind_la_SOURCES_aarch64_common)	    \
+# 	$(libunwind_la_SOURCES_local)					    \
+# 	aarch64/Lapply_reg_state.c aarch64/Lreg_states_iterate.c	    \
+# 	aarch64/Lcreate_addr_space.c aarch64/Lget_proc_info.c 		    \
+# 	aarch64/Lget_save_loc.c aarch64/Lglobal.c aarch64/Linit.c	    \
+# 	aarch64/Linit_local.c aarch64/Linit_remote.c 			    \
+# 	aarch64/Lis_signal_frame.c aarch64/Lregs.c aarch64/Lresume.c 	    \
+# 	aarch64/Lstash_frame.c aarch64/Lstep.c aarch64/Ltrace.c		    \
+# 	aarch64/getcontext.S
+
+# libunwind_aarch64_la_SOURCES_aarch64 = $(libunwind_la_SOURCES_aarch64_common) \
+# 	$(libunwind_la_SOURCES_generic)					      \
+# 	aarch64/Gapply_reg_state.c aarch64/Greg_states_iterate.c	      \
+# 	aarch64/Gcreate_addr_space.c aarch64/Gget_proc_info.c 		      \
+# 	aarch64/Gget_save_loc.c aarch64/Gglobal.c aarch64/Ginit.c 	      \
+# 	aarch64/Ginit_local.c aarch64/Ginit_remote.c			      \
+# 	aarch64/Gis_signal_frame.c aarch64/Gregs.c aarch64/Gresume.c	      \
+# 	aarch64/Gstash_frame.c aarch64/Gstep.c aarch64/Gtrace.c
+
+# # The list of files that go into libunwind and libunwind-arm:
+# noinst_HEADERS += arm/init.h arm/offsets.h arm/unwind_i.h
+# libunwind_la_SOURCES_arm_common = $(libunwind_la_SOURCES_common)	    \
+# 	arm/is_fpreg.c arm/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_arm = $(libunwind_la_SOURCES_arm_common)		    \
+# 	$(libunwind_la_SOURCES_arm_os_local)				    \
+# 	$(libunwind_la_SOURCES_local)					    \
+# 	arm/getcontext.S						    \
+# 	arm/Lapply_reg_state.c arm/Lreg_states_iterate.c		    \
+# 	arm/Lcreate_addr_space.c arm/Lget_proc_info.c arm/Lget_save_loc.c   \
+# 	arm/Lglobal.c arm/Linit.c arm/Linit_local.c arm/Linit_remote.c	    \
+# 	arm/Lregs.c arm/Lresume.c arm/Lstep.c				    \
+# 	arm/Lex_tables.c arm/Lstash_frame.c arm/Ltrace.c
+
+# # The list of files that go into libunwind-arm:
+# libunwind_arm_la_SOURCES_arm = $(libunwind_la_SOURCES_arm_common)	    \
+# 	$(libunwind_la_SOURCES_arm_os)					    \
+# 	$(libunwind_la_SOURCES_generic)					    \
+# 	arm/Gapply_reg_state.c arm/Greg_states_iterate.c		    \
+# 	arm/Gcreate_addr_space.c arm/Gget_proc_info.c arm/Gget_save_loc.c   \
+# 	arm/Gglobal.c arm/Ginit.c arm/Ginit_local.c arm/Ginit_remote.c	    \
+# 	arm/Gregs.c arm/Gresume.c arm/Gstep.c				    \
+# 	arm/Gex_tables.c arm/Gstash_frame.c arm/Gtrace.c
+
+# # The list of files that go both into libunwind and libunwind-ia64:
+# noinst_HEADERS += ia64/init.h ia64/offsets.h ia64/regs.h		    \
+# 	ia64/ucontext_i.h ia64/unwind_decoder.h ia64/unwind_i.h
+# libunwind_la_SOURCES_ia64_common = $(libunwind_la_SOURCES_common)	    \
+# 	ia64/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_ia64 = $(libunwind_la_SOURCES_ia64_common)		     \
+# 	$(libunwind_la_SOURCES_local)					     \
+# 									     \
+# 	ia64/dyn_info_list.S ia64/getcontext.S				     \
+# 									     \
+# 	ia64/Lapply_reg_state.c ia64/Lreg_states_iterate.c		     \
+# 	ia64/Lcreate_addr_space.c ia64/Lget_proc_info.c ia64/Lget_save_loc.c \
+# 	ia64/Lglobal.c ia64/Linit.c ia64/Linit_local.c ia64/Linit_remote.c   \
+# 	ia64/Linstall_cursor.S ia64/Lis_signal_frame.c ia64/Lparser.c	     \
+# 	ia64/Lrbs.c ia64/Lregs.c ia64/Lresume.c ia64/Lscript.c ia64/Lstep.c  \
+# 	ia64/Ltables.c ia64/Lfind_unwind_table.c
+
+# # The list of files that go into libunwind-ia64:
+# libunwind_ia64_la_SOURCES_ia64 = $(libunwind_la_SOURCES_ia64_common)	     \
+# 	$(libunwind_la_SOURCES_generic)					     \
+# 	ia64/Gapply_reg_state.c ia64/Greg_states_iterate.c		     \
+# 	ia64/Gcreate_addr_space.c ia64/Gget_proc_info.c ia64/Gget_save_loc.c \
+# 	ia64/Gglobal.c ia64/Ginit.c ia64/Ginit_local.c ia64/Ginit_remote.c   \
+# 	ia64/Ginstall_cursor.S ia64/Gis_signal_frame.c ia64/Gparser.c	     \
+# 	ia64/Grbs.c ia64/Gregs.c ia64/Gresume.c ia64/Gscript.c ia64/Gstep.c  \
+# 	ia64/Gtables.c ia64/Gfind_unwind_table.c
+
+# # The list of files that go both into libunwind and libunwind-hppa:
+# noinst_HEADERS += hppa/init.h hppa/offsets.h hppa/unwind_i.h
+# libunwind_la_SOURCES_hppa_common = $(libunwind_la_SOURCES_common)	\
+# 	hppa/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_hppa = $(libunwind_la_SOURCES_hppa_common)		\
+# 	$(libunwind_la_SOURCES_local)					\
+# 	hppa/getcontext.S hppa/setcontext.S				\
+# 	hppa/Lapply_reg_state.c hppa/Lreg_states_iterate.c		\
+# 	hppa/Lcreate_addr_space.c hppa/Lget_save_loc.c hppa/Lglobal.c	\
+# 	hppa/Linit.c hppa/Linit_local.c hppa/Linit_remote.c		\
+# 	hppa/Lis_signal_frame.c hppa/Lget_proc_info.c hppa/Lregs.c	\
+# 	hppa/Lresume.c hppa/Lstep.c
+
+# # The list of files that go into libunwind-hppa:
+# libunwind_hppa_la_SOURCES_hppa = $(libunwind_la_SOURCES_hppa_common)	\
+# 	$(libunwind_la_SOURCES_generic)					\
+# 	hppa/Gapply_reg_state.c hppa/Greg_states_iterate.c		\
+# 	hppa/Gcreate_addr_space.c hppa/Gget_save_loc.c hppa/Gglobal.c	\
+# 	hppa/Ginit.c hppa/Ginit_local.c hppa/Ginit_remote.c		\
+# 	hppa/Gis_signal_frame.c hppa/Gget_proc_info.c hppa/Gregs.c	\
+# 	hppa/Gresume.c hppa/Gstep.c
+
+# # The list of files that go info libunwind and libunwind-mips:
+# noinst_HEADERS += mips/init.h mips/offsets.h mips/unwind_i.h
+# libunwind_la_SOURCES_mips_common = $(libunwind_la_SOURCES_common)	    \
+# 	mips/is_fpreg.c mips/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_mips = $(libunwind_la_SOURCES_mips_common)		    \
+# 	$(libunwind_la_SOURCES_local)					    \
+# 	mips/getcontext.S						    \
+# 	mips/Lapply_reg_state.c mips/Lreg_states_iterate.c		    \
+# 	mips/Lcreate_addr_space.c mips/Lget_proc_info.c mips/Lget_save_loc.c   \
+# 	mips/Lglobal.c mips/Linit.c mips/Linit_local.c mips/Linit_remote.c  \
+# 	mips/Lis_signal_frame.c mips/Lregs.c mips/Lresume.c mips/Lstep.c
+
+# libunwind_mips_la_SOURCES_mips = $(libunwind_la_SOURCES_mips_common)	    \
+# 	$(libunwind_la_SOURCES_generic)					    \
+# 	mips/Gapply_reg_state.c mips/Greg_states_iterate.c		    \
+# 	mips/Gcreate_addr_space.c mips/Gget_proc_info.c mips/Gget_save_loc.c   \
+# 	mips/Gglobal.c mips/Ginit.c mips/Ginit_local.c mips/Ginit_remote.c	    \
+# 	mips/Gis_signal_frame.c mips/Gregs.c mips/Gresume.c mips/Gstep.c
+
+# # The list of files that go info libunwind and libunwind-tilegx:
+# noinst_HEADERS += tilegx/init.h tilegx/offsets.h tilegx/unwind_i.h
+# libunwind_la_SOURCES_tilegx_common = $(libunwind_la_SOURCES_common)	    \
+# 	tilegx/is_fpreg.c tilegx/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_tilegx = $(libunwind_la_SOURCES_tilegx_common)		    \
+# 	$(libunwind_la_SOURCES_local)					    \
+# 	tilegx/getcontext.S						\
+# 	tilegx/Lapply_reg_state.c tilegx/Lreg_states_iterate.c		    \
+# 	tilegx/Lcreate_addr_space.c tilegx/Lget_proc_info.c tilegx/Lget_save_loc.c   \
+# 	tilegx/Lglobal.c tilegx/Linit.c tilegx/Linit_local.c tilegx/Linit_remote.c  \
+# 	tilegx/Lis_signal_frame.c tilegx/Lregs.c tilegx/Lresume.c tilegx/Lstep.c
+
+# libunwind_tilegx_la_SOURCES_tilegx = $(libunwind_la_SOURCES_tilegx_common)	    \
+# 	$(libunwind_la_SOURCES_generic)					    \
+# 	tilegx/Gapply_reg_state.c tilegx/Greg_states_iterate.c			     \
+# 	tilegx/Gcreate_addr_space.c tilegx/Gget_proc_info.c tilegx/Gget_save_loc.c   \
+# 	tilegx/Gglobal.c tilegx/Ginit.c tilegx/Ginit_local.c tilegx/Ginit_remote.c	    \
+# 	tilegx/Gis_signal_frame.c tilegx/Gregs.c tilegx/Gresume.c tilegx/Gstep.c
+
+
+# # The list of files that go both into libunwind and libunwind-x86:
+# noinst_HEADERS += x86/init.h x86/offsets.h x86/unwind_i.h
+# libunwind_la_SOURCES_x86_common = $(libunwind_la_SOURCES_common)	\
+# 	x86/is_fpreg.c x86/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_x86 = $(libunwind_la_SOURCES_x86_common)		\
+#         $(libunwind_la_SOURCES_x86_os_local)				\
+# 	$(libunwind_la_SOURCES_local)					\
+# 	x86/Lapply_reg_state.c x86/Lreg_states_iterate.c		\
+# 	x86/Lcreate_addr_space.c x86/Lget_save_loc.c x86/Lglobal.c	\
+# 	x86/Linit.c x86/Linit_local.c x86/Linit_remote.c		\
+# 	x86/Lget_proc_info.c x86/Lregs.c				\
+# 	x86/Lresume.c x86/Lstep.c
+
+# # The list of files that go into libunwind-x86:
+# libunwind_x86_la_SOURCES_x86 = $(libunwind_la_SOURCES_x86_common)	\
+#         $(libunwind_la_SOURCES_x86_os)					\
+# 	$(libunwind_la_SOURCES_generic)					\
+# 	x86/Gapply_reg_state.c x86/Greg_states_iterate.c		\
+# 	x86/Gcreate_addr_space.c x86/Gget_save_loc.c x86/Gglobal.c	\
+# 	x86/Ginit.c x86/Ginit_local.c x86/Ginit_remote.c		\
+# 	x86/Gget_proc_info.c x86/Gregs.c				\
+# 	x86/Gresume.c x86/Gstep.c
+
 # The list of files that go both into libunwind and libunwind-x86_64:
 noinst_HEADERS += x86_64/offsets.h					\
 	x86_64/init.h x86_64/unwind_i.h x86_64/ucontext_i.h
 libunwind_la_SOURCES_x86_64_common = $(libunwind_la_SOURCES_common)	\
-	x86_64/is_fpreg.c x86_64/regname.c
+	x86_64/is_fpreg.c
 
 # The list of files that go into libunwind:
 libunwind_la_SOURCES_x86_64 = $(libunwind_la_SOURCES_x86_64_common)	    \
         $(libunwind_la_SOURCES_x86_64_os_local)			    	    \
-	$(libunwind_la_SOURCES_local)					    \
-	x86_64/setcontext.S                                                 \				
+	$(libunwind_la_SOURCES_local)					    \    
+        x86_64/setcontext.S   \              
+	x86_64/Lapply_reg_state.c x86_64/Lreg_states_iterate.c		    \
 	x86_64/Lcreate_addr_space.c x86_64/Lget_save_loc.c x86_64/Lglobal.c \
 	x86_64/Linit.c x86_64/Linit_local.c x86_64/Linit_remote.c	    \
 	x86_64/Lget_proc_info.c x86_64/Lregs.c x86_64/Lresume.c		    \
@@ -284,59 +380,87 @@
 libunwind_x86_64_la_SOURCES_x86_64 = $(libunwind_la_SOURCES_x86_64_common)  \
         $(libunwind_la_SOURCES_x86_64_os)			    	    \
 	$(libunwind_la_SOURCES_generic)					    \
-	x86_64/Gcreate_addr_space.c x86_64/Gget_save_loc.c x86_64/Gglobal.c \
-	x86_64/Ginit.c x86_64/Ginit_local.c x86_64/Ginit_remote.c	    \
+	x86_64/Gglobal.c \
+	x86_64/Ginit.c x86_64/Ginit_local.c	    \
 	x86_64/Gget_proc_info.c x86_64/Gregs.c x86_64/Gresume.c		    \
 	x86_64/Gstash_frame.c x86_64/Gstep.c
 
-## The list of local files that go to Power 64 and 32:
-#libunwind_la_SOURCES_ppc = ppc/Lcreate_addr_space.c 			\
-#	ppc/Lget_proc_info.c ppc/Lget_save_loc.c ppc/Linit_local.c	\
-#	ppc/Linit_remote.c ppc/Lis_signal_frame.c
-#
-## The list of generic files that go to Power 64 and 32:
-#libunwind_ppc_la_SOURCES_ppc_generic = ppc/Gcreate_addr_space.c		\
-#	ppc/Gget_proc_info.c ppc/Gget_save_loc.c ppc/Ginit_local.c	\
-#	ppc/Ginit_remote.c ppc/Gis_signal_frame.c
-#
-## The list of files that go both into libunwind and libunwind-ppc32:
-#noinst_HEADERS += ppc32/init.h ppc32/unwind_i.h ppc32/ucontext_i.h
-#libunwind_la_SOURCES_ppc32_common = $(libunwind_la_SOURCES_common)      \
-#	ppc32/is_fpreg.c ppc32/regname.c ppc32/get_func_addr.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_ppc32 = $(libunwind_la_SOURCES_ppc32_common)       \
-#	$(libunwind_la_SOURCES_local)                                   \
-#	$(libunwind_la_SOURCES_ppc)					\
-#	ppc32/Lglobal.c ppc32/Linit.c					\
-#	ppc32/Lregs.c ppc32/Lresume.c ppc32/Lstep.c
-#
-## The list of files that go into libunwind-ppc32:
-#libunwind_ppc32_la_SOURCES_ppc32 = $(libunwind_la_SOURCES_ppc32_common) \
-#	$(libunwind_la_SOURCES_generic)                                 \
-#	$(libunwind_ppc_la_SOURCES_ppc_generic)				\
-#	ppc32/Gglobal.c ppc32/Ginit.c					\
-#	ppc32/Gregs.c ppc32/Gresume.c ppc32/Gstep.c
-#
-## The list of files that go both into libunwind and libunwind-ppc64:
-#noinst_HEADERS += ppc64/init.h ppc64/unwind_i.h ppc64/ucontext_i.h
-#libunwind_la_SOURCES_ppc64_common = $(libunwind_la_SOURCES_common)      \
-#        ppc64/is_fpreg.c ppc64/regname.c ppc64/get_func_addr.c
-#
-## The list of files that go into libunwind:
-#libunwind_la_SOURCES_ppc64 = $(libunwind_la_SOURCES_ppc64_common)       \
-#        $(libunwind_la_SOURCES_local)                                   \
-#	$(libunwind_la_SOURCES_ppc)					\
-#	ppc64/Lglobal.c ppc64/Linit.c					\
-#	ppc64/Lregs.c ppc64/Lresume.c ppc64/Lstep.c
-#
-## The list of files that go into libunwind-ppc64:
-#libunwind_ppc64_la_SOURCES_ppc64 = $(libunwind_la_SOURCES_ppc64_common) \
-#        $(libunwind_la_SOURCES_generic)                                 \
-#	$(libunwind_ppc_la_SOURCES_ppc_generic)				\
-#	ppc64/Gglobal.c ppc64/Ginit.c					\
-#	ppc64/Gregs.c ppc64/Gresume.c ppc64/Gstep.c
-#
+# # The list of local files that go to Power 64 and 32:
+# libunwind_la_SOURCES_ppc = \
+# 	ppc/Lget_proc_info.c ppc/Lget_save_loc.c ppc/Linit_local.c	\
+# 	ppc/Linit_remote.c ppc/Lis_signal_frame.c
+
+# # The list of generic files that go to Power 64 and 32:
+# libunwind_ppc_la_SOURCES_ppc_generic = \
+# 	ppc/Gget_proc_info.c ppc/Gget_save_loc.c ppc/Ginit_local.c	\
+# 	ppc/Ginit_remote.c ppc/Gis_signal_frame.c
+
+# # The list of files that go both into libunwind and libunwind-ppc32:
+# noinst_HEADERS += ppc32/init.h ppc32/unwind_i.h ppc32/ucontext_i.h
+# libunwind_la_SOURCES_ppc32_common = $(libunwind_la_SOURCES_common)      \
+# 	ppc32/is_fpreg.c ppc32/regname.c ppc32/get_func_addr.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_ppc32 = $(libunwind_la_SOURCES_ppc32_common)       \
+# 	$(libunwind_la_SOURCES_local)                                   \
+# 	$(libunwind_la_SOURCES_ppc)					\
+# 	ppc32/Lapply_reg_state.c ppc32/Lreg_states_iterate.c		\
+# 	ppc32/Lcreate_addr_space.c					\
+# 	ppc32/Lglobal.c ppc32/Linit.c					\
+# 	ppc32/Lregs.c ppc32/Lresume.c ppc32/Lstep.c
+
+# # The list of files that go into libunwind-ppc32:
+# libunwind_ppc32_la_SOURCES_ppc32 = $(libunwind_la_SOURCES_ppc32_common) \
+# 	$(libunwind_la_SOURCES_generic)                                 \
+# 	$(libunwind_ppc_la_SOURCES_ppc_generic)				\
+# 	ppc32/Gapply_reg_state.c ppc32/Greg_states_iterate.c		\
+# 	ppc32/Gcreate_addr_space.c					\
+# 	ppc32/Gglobal.c ppc32/Ginit.c					\
+# 	ppc32/Gregs.c ppc32/Gresume.c ppc32/Gstep.c
+
+# # The list of files that go both into libunwind and libunwind-ppc64:
+# noinst_HEADERS += ppc64/init.h ppc64/unwind_i.h ppc64/ucontext_i.h
+# libunwind_la_SOURCES_ppc64_common = $(libunwind_la_SOURCES_common)      \
+#         ppc64/is_fpreg.c ppc64/regname.c ppc64/get_func_addr.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_ppc64 = $(libunwind_la_SOURCES_ppc64_common)       \
+#         $(libunwind_la_SOURCES_local)                                   \
+# 	$(libunwind_la_SOURCES_ppc)					\
+# 	ppc64/Lapply_reg_state.c ppc64/Lreg_states_iterate.c		\
+# 	ppc64/Lcreate_addr_space.c					\
+# 	ppc64/Lglobal.c ppc64/Linit.c					\
+# 	ppc64/Lregs.c ppc64/Lresume.c ppc64/Lstep.c
+
+# # The list of files that go into libunwind-ppc64:
+# libunwind_ppc64_la_SOURCES_ppc64 = $(libunwind_la_SOURCES_ppc64_common) \
+#         $(libunwind_la_SOURCES_generic)                                 \
+# 	$(libunwind_ppc_la_SOURCES_ppc_generic)				\
+# 	ppc64/Gapply_reg_state.c ppc64/Greg_states_iterate.c		\
+# 	ppc64/Gcreate_addr_space.c					\
+# 	ppc64/Gglobal.c ppc64/Ginit.c					\
+# 	ppc64/Gregs.c ppc64/Gresume.c ppc64/Gstep.c
+
+# # The list of files that go into libunwind and libunwind-sh:
+# noinst_HEADERS += sh/init.h sh/offsets.h sh/unwind_i.h
+# libunwind_la_SOURCES_sh_common = $(libunwind_la_SOURCES_common)		\
+# 	sh/is_fpreg.c sh/regname.c
+
+# # The list of files that go into libunwind:
+# libunwind_la_SOURCES_sh = $(libunwind_la_SOURCES_sh_common)		\
+# 	$(libunwind_la_SOURCES_local)					\
+# 	sh/Lapply_reg_state.c sh/Lreg_states_iterate.c			\
+# 	sh/Lcreate_addr_space.c sh/Lget_proc_info.c sh/Lget_save_loc.c	\
+# 	sh/Lglobal.c sh/Linit.c sh/Linit_local.c sh/Linit_remote.c	\
+# 	sh/Lis_signal_frame.c sh/Lregs.c sh/Lresume.c sh/Lstep.c
+
+# libunwind_sh_la_SOURCES_sh = $(libunwind_la_SOURCES_sh_common)		\
+# 	$(libunwind_la_SOURCES_generic)					\
+# 	sh/Gapply_reg_state.c sh/Greg_states_iterate.c			\
+# 	sh/Gcreate_addr_space.c sh/Gget_proc_info.c sh/Gget_save_loc.c	\
+# 	sh/Gglobal.c sh/Ginit.c sh/Ginit_local.c sh/Ginit_remote.c	\
+# 	sh/Gis_signal_frame.c sh/Gregs.c sh/Gresume.c sh/Gstep.c
+
 if REMOTE_ONLY
 install-exec-hook:
 #	Nothing to do here....
@@ -348,10 +472,12 @@
 # to be there if the user configured with --disable-shared.
 #
 install-exec-hook:
-	$(LN_S) -f libunwind-$(arch).a  $(DESTDIR)$(libdir)/libunwind-generic.a
+	if test -f $(DESTDIR)$(libdir)/libunwind-$(arch).a; then \
+		cd $(DESTDIR)$(libdir) && $(LN_S) -f libunwind-$(arch).a libunwind-generic.a; \
+	fi
 	if test -f $(DESTDIR)$(libdir)/libunwind-$(arch).so; then \
-	    $(LN_S) -f libunwind-$(arch).so \
-		$(DESTDIR)$(libdir)/libunwind-generic.so; \
+		cd $(DESTDIR)$(libdir) && $(LN_S) -f libunwind-$(arch).so \
+		libunwind-generic.so; \
 	fi
 endif
 
@@ -363,7 +489,9 @@
  libunwind_la_SOURCES_x86_os_local    = x86/Los-linux.c
  libunwind_la_SOURCES_x86_64_os       = x86_64/Gos-linux.c
  libunwind_la_SOURCES_x86_64_os_local = x86_64/Los-linux.c
-# libunwind_coredump_a_SOURCES += coredump/_UCD_access_reg_linux.c
+ libunwind_la_SOURCES_arm_os          = arm/Gos-linux.c
+ libunwind_la_SOURCES_arm_os_local    = arm/Los-linux.c
+# libunwind_coredump_la_SOURCES += coredump/_UCD_access_reg_linux.c
 endif
 
 if OS_HPUX
@@ -379,9 +507,30 @@
  libunwind_la_SOURCES_x86_os_local    = x86/Los-freebsd.c
  libunwind_la_SOURCES_x86_64_os       = x86_64/Gos-freebsd.c
  libunwind_la_SOURCES_x86_64_os_local = x86_64/Los-freebsd.c
-# libunwind_coredump_a_SOURCES += coredump/_UCD_access_reg_freebsd.c
+ libunwind_la_SOURCES_arm_os          = arm/Gos-freebsd.c
+ libunwind_la_SOURCES_arm_os_local    = arm/Los-freebsd.c
+# libunwind_coredump_la_SOURCES += coredump/_UCD_access_reg_freebsd.c
 endif
 
+if OS_QNX
+ libunwind_la_SOURCES_os	= $(libunwind_la_SOURCES_os_qnx)
+ libunwind_la_SOURCES_os_local	= $(libunwind_la_SOURCES_os_qnx_local)
+ libunwind_la_SOURCES_arm_os          = arm/Gos-other.c
+ libunwind_la_SOURCES_arm_os_local    = arm/Los-other.c
+endif
+
+if ARCH_AARCH64
+ lib_LTLIBRARIES += libunwind-aarch64.la
+ libunwind_la_SOURCES = $(libunwind_la_SOURCES_aarch64)
+ libunwind_aarch64_la_SOURCES = $(libunwind_aarch64_la_SOURCES_aarch64)
+ libunwind_aarch64_la_LDFLAGS = $(COMMON_SO_LDFLAGS) -version-info $(SOVERSION)
+ libunwind_aarch64_la_LIBADD = libunwind-dwarf-generic.la
+ libunwind_aarch64_la_LIBADD += libunwind-elf64.la
+if !REMOTE_ONLY
+ libunwind_aarch64_la_LIBADD += libunwind.la -lc
+endif
+ libunwind_setjmp_la_SOURCES += aarch64/siglongjmp.S
+else
 if ARCH_ARM
  lib_LTLIBRARIES += libunwind-arm.la
  libunwind_la_SOURCES = $(libunwind_la_SOURCES_arm)
@@ -395,14 +544,15 @@
  libunwind_setjmp_la_SOURCES += arm/siglongjmp.S
 else
 if ARCH_IA64
- ia64_mk_Gcursor_i_SOURCES = ia64/mk_Gcursor_i.c
- ia64_mk_Lcursor_i_SOURCES = ia64/mk_Lcursor_i.c
- noinst_PROGRAMS = ia64/mk_Gcursor_i ia64/mk_Lcursor_i
  BUILT_SOURCES = Gcursor_i.h Lcursor_i.h
-Gcursor_i.h: ia64/mk_Gcursor_i
-	ia64/mk_Gcursor_i > $@
-Lcursor_i.h: ia64/mk_Lcursor_i
-	ia64/mk_Lcursor_i > $@
+mk_Gcursor_i.s: $(srcdir)/ia64/mk_Gcursor_i.c
+	$(COMPILE) -S "$(srcdir)/ia64/mk_Gcursor_i.c" -o mk_Gcursor_i.s
+mk_Lcursor_i.s: $(srcdir)/ia64/mk_Lcursor_i.c
+	$(COMPILE) -S "$(srcdir)/ia64/mk_Lcursor_i.c" -o mk_Lcursor_i.s
+Gcursor_i.h: mk_Gcursor_i.s
+	"$(srcdir)/ia64/mk_cursor_i" mk_Gcursor_i.s > Gcursor_i.h
+Lcursor_i.h: mk_Lcursor_i.s
+	"$(srcdir)/ia64/mk_cursor_i" mk_Lcursor_i.s > Lcursor_i.h
 
  lib_LTLIBRARIES += libunwind-ia64.la
  libunwind_la_SOURCES = $(libunwind_la_SOURCES_ia64)
@@ -439,6 +589,18 @@
 endif
  libunwind_setjmp_la_SOURCES += mips/siglongjmp.S
 else
+if ARCH_TILEGX
+ lib_LTLIBRARIES += libunwind-tilegx.la
+ libunwind_la_SOURCES = $(libunwind_la_SOURCES_tilegx)
+ libunwind_tilegx_la_SOURCES = $(libunwind_tilegx_la_SOURCES_tilegx)
+ libunwind_tilegx_la_LDFLAGS = $(COMMON_SO_LDFLAGS) -version-info $(SOVERSION)
+ libunwind_tilegx_la_LIBADD = libunwind-dwarf-generic.la
+ libunwind_tilegx_la_LIBADD += libunwind-elfxx.la
+if !REMOTE_ONLY
+ libunwind_tilegx_la_LIBADD += libunwind.la -lc
+endif
+ libunwind_setjmp_la_SOURCES += tilegx/siglongjmp.S
+else
 if ARCH_X86
  lib_LTLIBRARIES += libunwind-x86.la
  libunwind_la_SOURCES = $(libunwind_la_SOURCES_x86) $(libunwind_x86_la_SOURCES_os)
@@ -452,7 +614,7 @@
  libunwind_setjmp_la_SOURCES += x86/longjmp.S x86/siglongjmp.S
 else
 if ARCH_X86_64
- lib_LTLIBRARIES += libunwind-x86_64.la
+# lib_LTLIBRARIES += libunwind-x86_64.la
  libunwind_la_SOURCES = $(libunwind_la_SOURCES_x86_64)
  libunwind_x86_64_la_SOURCES = $(libunwind_x86_64_la_SOURCES_x86_64)
  libunwind_x86_64_la_LDFLAGS = $(COMMON_SO_LDFLAGS) -version-info $(SOVERSION)
@@ -486,19 +648,34 @@
  libunwind_ppc64_la_LIBADD += libunwind.la -lc
 endif
  libunwind_setjmp_la_SOURCES += ppc/longjmp.S ppc/siglongjmp.S
+else
+if ARCH_SH
+ lib_LTLIBRARIES += libunwind-sh.la
+ libunwind_la_SOURCES = $(libunwind_la_SOURCES_sh)
+ libunwind_sh_la_SOURCES = $(libunwind_sh_la_SOURCES_sh)
+ libunwind_sh_la_LDFLAGS = $(COMMON_SO_LDFLAGS) -version-info $(SOVERSION)
+ libunwind_sh_la_LIBADD = libunwind-dwarf-generic.la
+ libunwind_sh_la_LIBADD += libunwind-elf32.la
+if !REMOTE_ONLY
+ libunwind_sh_la_LIBADD += libunwind.la -lc
+endif
+ libunwind_setjmp_la_SOURCES += sh/siglongjmp.S
 
+endif # ARCH_SH
 endif # ARCH_PPC64
 endif # ARCH_PPC32
 endif # ARCH_X86_64
 endif # ARCH_X86
+endif # ARCH_TILEGX
 endif # ARCH_MIPS
 endif # ARCH_HPPA
 endif # ARCH_IA64
 endif # ARCH_ARM
+endif # ARCH_AARCH64
 
 # libunwind-setjmp depends on libunwind-$(arch). Therefore must be added
 # at the end.
-if !REMOTE_ONLY
+if BUILD_SETJMP
 lib_LTLIBRARIES += libunwind-setjmp.la
 endif
 
@@ -509,27 +686,33 @@
 libunwind_la_LDFLAGS =	$(COMMON_SO_LDFLAGS) -XCClinker -nostdlib \
 			$(LDFLAGS_STATIC_LIBCXA) -version-info $(SOVERSION)
 libunwind_la_LIBADD  += -lc $(LIBCRTS)
+libunwind_la_LIBADD += $(LIBLZMA)
 
 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/include/tdep-$(arch) -I.
 AM_CPPFLAGS+= -I$(SGX_SDK)/include/
 AM_CCASFLAGS = $(AM_CPPFLAGS)
 noinst_HEADERS += unwind/unwind-internal.h
 
-EXTRA_DIST =	$(libunwind_la_SOURCES_arm)			\
+EXTRA_DIST =	$(libunwind_la_SOURCES_aarch64)			\
+		$(libunwind_la_SOURCES_arm)			\
 		$(libunwind_la_SOURCES_hppa)			\
 		$(libunwind_la_SOURCES_ia64)			\
 		$(libunwind_la_SOURCES_mips)			\
+		$(libunwind_la_SOURCES_sh)			\
 		$(libunwind_la_SOURCES_x86)			\
 		$(libunwind_la_SOURCES_os_freebsd)		\
 		$(libunwind_la_SOURCES_os_linux)		\
 		$(libunwind_la_SOURCES_os_hpux)			\
+		$(libunwind_la_SOURCES_os_qnx)			\
 		$(libunwind_la_SOURCES_common)			\
 		$(libunwind_la_SOURCES_local)			\
 		$(libunwind_la_SOURCES_generic)			\
+		$(libunwind_aarch64_la_SOURCES_aarch64)		\
 		$(libunwind_arm_la_SOURCES_arm)			\
 		$(libunwind_hppa_la_SOURCES_hppa)		\
 		$(libunwind_ia64_la_SOURCES_ia64)		\
 		$(libunwind_mips_la_SOURCES_mips)		\
+		$(libunwind_sh_la_SOURCES_sh)			\
 		$(libunwind_x86_la_SOURCES_x86)			\
 		$(libunwind_x86_64_la_SOURCES_x86_64)
 
diff --git a/sgx_unwind/libunwind/src/dwarf/Gexpr.c b/sgx_unwind/libunwind/src/dwarf/Gexpr.c
index b609c35..f63c3d2 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gexpr.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gexpr.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -30,98 +30,93 @@
    that the stack could at least have a depth of up to 256 elements,
    but the GCC unwinder restricts the depth to 64, which seems
    reasonable so we use the same value here.  */
-#define MAX_EXPR_STACK_SIZE	64
+#define MAX_EXPR_STACK_SIZE     64
 
-#define NUM_OPERANDS(signature)	(((signature) >> 6) & 0x3)
-#define OPND1_TYPE(signature)	(((signature) >> 3) & 0x7)
-#define OPND2_TYPE(signature)	(((signature) >> 0) & 0x7)
+#define NUM_OPERANDS(signature) (((signature) >> 6) & 0x3)
+#define OPND1_TYPE(signature)   (((signature) >> 3) & 0x7)
+#define OPND2_TYPE(signature)   (((signature) >> 0) & 0x7)
 
 #define OPND_SIGNATURE(n, t1, t2) (((n) << 6) | ((t1) << 3) | ((t2) << 0))
-#define OPND1(t1)		OPND_SIGNATURE(1, t1, 0)
-#define OPND2(t1, t2)		OPND_SIGNATURE(2, t1, t2)
+#define OPND1(t1)               OPND_SIGNATURE(1, t1, 0)
+#define OPND2(t1, t2)           OPND_SIGNATURE(2, t1, t2)
 
-#define VAL8	0x0
-#define VAL16	0x1
-#define VAL32	0x2
-#define VAL64	0x3
-#define ULEB128	0x4
-#define SLEB128	0x5
-#define OFFSET	0x6	/* 32-bit offset for 32-bit DWARF, 64-bit otherwise */
-#define ADDR	0x7	/* Machine address.  */
+#define VAL8    0x0
+#define VAL16   0x1
+#define VAL32   0x2
+#define VAL64   0x3
+#define ULEB128 0x4
+#define SLEB128 0x5
+#define OFFSET  0x6     /* 32-bit offset for 32-bit DWARF, 64-bit otherwise */
+#define ADDR    0x7     /* Machine address.  */
 
-static uint8_t operands[256] =
-{
-    [DW_OP_addr] =		OPND1 (ADDR),
-    [DW_OP_const1u] =		OPND1 (VAL8),
-    [DW_OP_const1s] =		OPND1 (VAL8),
-    [DW_OP_const2u] =		OPND1 (VAL16),
-    [DW_OP_const2s] =		OPND1 (VAL16),
-    [DW_OP_const4u] =		OPND1 (VAL32),
-    [DW_OP_const4s] =		OPND1 (VAL32),
-    [DW_OP_const8u] =		OPND1 (VAL64),
-    [DW_OP_const8s] =		OPND1 (VAL64),
-    [DW_OP_pick] =		OPND1 (VAL8),
-    [DW_OP_plus_uconst] =	OPND1 (ULEB128),
-    [DW_OP_skip] =		OPND1 (VAL16),
-    [DW_OP_bra] =		OPND1 (VAL16),
-    [DW_OP_breg0 +  0] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  1] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  2] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  3] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  4] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  5] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  6] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  7] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  8] =	OPND1 (SLEB128),
-    [DW_OP_breg0 +  9] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 10] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 11] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 12] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 13] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 14] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 15] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 16] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 17] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 18] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 19] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 20] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 21] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 22] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 23] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 24] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 25] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 26] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 27] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 28] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 29] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 30] =	OPND1 (SLEB128),
-    [DW_OP_breg0 + 31] =	OPND1 (SLEB128),
-    [DW_OP_regx] =		OPND1 (ULEB128),
-    [DW_OP_fbreg] =		OPND1 (SLEB128),
-    [DW_OP_bregx] =		OPND2 (ULEB128, SLEB128),
-    [DW_OP_piece] =		OPND1 (ULEB128),
-    [DW_OP_deref_size] =	OPND1 (VAL8),
-    [DW_OP_xderef_size] =	OPND1 (VAL8),
-    [DW_OP_call2] =		OPND1 (VAL16),
-    [DW_OP_call4] =		OPND1 (VAL32),
-    [DW_OP_call_ref] =		OPND1 (OFFSET)
-};
+static const uint8_t operands[256] =
+  {
+    [DW_OP_addr] =              OPND1 (ADDR),
+    [DW_OP_const1u] =           OPND1 (VAL8),
+    [DW_OP_const1s] =           OPND1 (VAL8),
+    [DW_OP_const2u] =           OPND1 (VAL16),
+    [DW_OP_const2s] =           OPND1 (VAL16),
+    [DW_OP_const4u] =           OPND1 (VAL32),
+    [DW_OP_const4s] =           OPND1 (VAL32),
+    [DW_OP_const8u] =           OPND1 (VAL64),
+    [DW_OP_const8s] =           OPND1 (VAL64),
+    [DW_OP_pick] =              OPND1 (VAL8),
+    [DW_OP_plus_uconst] =       OPND1 (ULEB128),
+    [DW_OP_skip] =              OPND1 (VAL16),
+    [DW_OP_bra] =               OPND1 (VAL16),
+    [DW_OP_breg0 +  0] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  1] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  2] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  3] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  4] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  5] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  6] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  7] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  8] =        OPND1 (SLEB128),
+    [DW_OP_breg0 +  9] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 10] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 11] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 12] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 13] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 14] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 15] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 16] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 17] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 18] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 19] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 20] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 21] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 22] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 23] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 24] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 25] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 26] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 27] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 28] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 29] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 30] =        OPND1 (SLEB128),
+    [DW_OP_breg0 + 31] =        OPND1 (SLEB128),
+    [DW_OP_regx] =              OPND1 (ULEB128),
+    [DW_OP_fbreg] =             OPND1 (SLEB128),
+    [DW_OP_bregx] =             OPND2 (ULEB128, SLEB128),
+    [DW_OP_piece] =             OPND1 (ULEB128),
+    [DW_OP_deref_size] =        OPND1 (VAL8),
+    [DW_OP_xderef_size] =       OPND1 (VAL8),
+    [DW_OP_call2] =             OPND1 (VAL16),
+    [DW_OP_call4] =             OPND1 (VAL32),
+    [DW_OP_call_ref] =          OPND1 (OFFSET)
+  };
 
 static inline unw_sword_t
 sword (unw_addr_space_t as, unw_word_t val)
 {
-    switch (dwarf_addr_size (as))
+  switch (dwarf_addr_size (as))
     {
-    case 1:
-        return (int8_t) val;
-    case 2:
-        return (int16_t) val;
-    case 4:
-        return (int32_t) val;
-    case 8:
-        return (int64_t) val;
-    default:
-        abort ();
+    case 1: return (int8_t) val;
+    case 2: return (int16_t) val;
+    case 4: return (int32_t) val;
+    case 8: return (int64_t) val;
+    default: abort ();
     }
 }
 
@@ -129,282 +124,259 @@
 read_operand (unw_addr_space_t as, unw_accessors_t *a,
               unw_word_t *addr, int operand_type, unw_word_t *val, void *arg)
 {
-    uint8_t u8;
-    uint16_t u16;
-    uint32_t u32;
-    uint64_t u64;
-    int ret;
+  uint8_t u8;
+  uint16_t u16;
+  uint32_t u32;
+  uint64_t u64;
+  int ret;
 
-    if (operand_type == ADDR)
-        switch (dwarf_addr_size (as))
-        {
-        case 1:
-            operand_type = VAL8;
-            break;
-        case 2:
-            operand_type = VAL16;
-            break;
-        case 4:
-            operand_type = VAL32;
-            break;
-        case 8:
-            operand_type = VAL64;
-            break;
-        default:
-            abort ();
-        }
+  if (operand_type == ADDR)
+    switch (dwarf_addr_size (as))
+      {
+      case 1: operand_type = VAL8; break;
+      case 2: operand_type = VAL16; break;
+      case 4: operand_type = VAL32; break;
+      case 8: operand_type = VAL64; break;
+      default: abort ();
+      }
 
-    switch (operand_type)
+  switch (operand_type)
     {
     case VAL8:
-        ret = dwarf_readu8 (as, a, addr, &u8, arg);
-        if (ret < 0)
-            return ret;
-        *val = u8;
-        break;
+      ret = dwarf_readu8 (as, a, addr, &u8, arg);
+      if (ret < 0)
+        return ret;
+      *val = u8;
+      break;
 
     case VAL16:
-        ret = dwarf_readu16 (as, a, addr, &u16, arg);
-        if (ret < 0)
-            return ret;
-        *val = u16;
-        break;
+      ret = dwarf_readu16 (as, a, addr, &u16, arg);
+      if (ret < 0)
+        return ret;
+      *val = u16;
+      break;
 
     case VAL32:
-        ret = dwarf_readu32 (as, a, addr, &u32, arg);
-        if (ret < 0)
-            return ret;
-        *val = u32;
-        break;
+      ret = dwarf_readu32 (as, a, addr, &u32, arg);
+      if (ret < 0)
+        return ret;
+      *val = u32;
+      break;
 
     case VAL64:
-        ret = dwarf_readu64 (as, a, addr, &u64, arg);
-        if (ret < 0)
-            return ret;
-        *val = u64;
-        break;
+      ret = dwarf_readu64 (as, a, addr, &u64, arg);
+      if (ret < 0)
+        return ret;
+      *val = u64;
+      break;
 
     case ULEB128:
-        ret = dwarf_read_uleb128 (as, a, addr, val, arg);
-        break;
+      ret = dwarf_read_uleb128 (as, a, addr, val, arg);
+      break;
 
     case SLEB128:
-        ret = dwarf_read_sleb128 (as, a, addr, val, arg);
-        break;
+      ret = dwarf_read_sleb128 (as, a, addr, val, arg);
+      break;
 
     case OFFSET: /* only used by DW_OP_call_ref, which we don't implement */
     default:
-        Debug (1, "Unexpected operand type %d\n", operand_type);
-        ret = -UNW_EINVAL;
+      Debug (1, "Unexpected operand type %d\n", operand_type);
+      ret = -UNW_EINVAL;
     }
-    return ret;
+  return ret;
+}
+
+HIDDEN int
+dwarf_stack_aligned(struct dwarf_cursor *c, unw_word_t cfa_addr,
+                    unw_word_t rbp_addr, unw_word_t *cfa_offset) {
+  unw_accessors_t *a;
+  int ret;
+  void *arg;
+  unw_word_t len;
+  uint8_t opcode;
+  unw_word_t operand1;
+
+  a = unw_get_accessors_int (c->as);
+  arg = c->as_arg;
+
+  ret = dwarf_read_uleb128(c->as, a, &rbp_addr, &len, arg);
+  if (len != 2 || ret < 0)
+    return 0;
+
+  ret = dwarf_readu8(c->as, a, &rbp_addr, &opcode, arg);
+  if (ret < 0 || opcode != DW_OP_breg6)
+    return 0;
+
+  ret = read_operand(c->as, a, &rbp_addr,
+                     OPND1_TYPE(operands[opcode]), &operand1, arg);
+
+  if (ret < 0 || operand1 != 0)
+    return 0;
+
+  ret = dwarf_read_uleb128(c->as, a, &cfa_addr, &len, arg);
+  if (ret < 0 || len != 3)
+    return 0;
+
+  ret = dwarf_readu8(c->as, a, &cfa_addr, &opcode, arg);
+  if (ret < 0 || opcode != DW_OP_breg6)
+    return 0;
+
+  ret = read_operand(c->as, a, &cfa_addr,
+                     OPND1_TYPE(operands[opcode]), &operand1, arg);
+  if (ret < 0)
+    return 0;
+
+  ret = dwarf_readu8(c->as, a, &cfa_addr, &opcode, arg);
+  if (ret < 0 || opcode != DW_OP_deref)
+    return 0;
+
+  *cfa_offset = operand1;
+  return 1;
 }
 
 HIDDEN int
 dwarf_eval_expr (struct dwarf_cursor *c, unw_word_t *addr, unw_word_t len,
                  unw_word_t *valp, int *is_register)
 {
-    unw_word_t operand1 = 0, operand2 = 0, tmp1, tmp2, tmp3, end_addr;
-    uint8_t opcode, operands_signature, u8;
-    unw_addr_space_t as;
-    unw_accessors_t *a;
-    void *arg;
-    unw_word_t stack[MAX_EXPR_STACK_SIZE];
-    unsigned int tos = 0;
-    uint16_t u16;
-    uint32_t u32;
-    uint64_t u64;
-    int ret;
-# define pop()					\
-({						\
-  if ((tos - 1) >= MAX_EXPR_STACK_SIZE)		\
-    {						\
-      Debug (1, "Stack underflow\n");		\
-      return -UNW_EINVAL;			\
-    }						\
-  stack[--tos];					\
+  unw_word_t operand1 = 0, operand2 = 0, tmp1, tmp2 = 0, tmp3, end_addr;
+  uint8_t opcode, operands_signature, u8;
+  unw_addr_space_t as;
+  unw_accessors_t *a;
+  void *arg;
+  unw_word_t stack[MAX_EXPR_STACK_SIZE];
+  unsigned int tos = 0;
+  uint16_t u16;
+  uint32_t u32;
+  uint64_t u64;
+  int ret;
+# define pop()                                  \
+({                                              \
+  if ((tos - 1) >= MAX_EXPR_STACK_SIZE)         \
+    {                                           \
+      Debug (1, "Stack underflow\n");           \
+      return -UNW_EINVAL;                       \
+    }                                           \
+  stack[--tos];                                 \
 })
-# define push(x)				\
-do {						\
-  if (tos >= MAX_EXPR_STACK_SIZE)		\
-    {						\
-      Debug (1, "Stack overflow\n");		\
-      return -UNW_EINVAL;			\
-    }						\
-  stack[tos++] = (x);				\
+# define push(x)                                \
+do {                                            \
+  unw_word_t _x = (x);                          \
+  if (tos >= MAX_EXPR_STACK_SIZE)               \
+    {                                           \
+      Debug (1, "Stack overflow\n");            \
+      return -UNW_EINVAL;                       \
+    }                                           \
+  stack[tos++] = _x;                            \
 } while (0)
-# define pick(n)				\
-({						\
-  unsigned int _index = tos - 1 - (n);		\
-  if (_index >= MAX_EXPR_STACK_SIZE)		\
-    {						\
-      Debug (1, "Out-of-stack pick\n");		\
-      return -UNW_EINVAL;			\
-    }						\
-  stack[_index];				\
+# define pick(n)                                \
+({                                              \
+  unsigned int _index = tos - 1 - (n);          \
+  if (_index >= MAX_EXPR_STACK_SIZE)            \
+    {                                           \
+      Debug (1, "Out-of-stack pick\n");         \
+      return -UNW_EINVAL;                       \
+    }                                           \
+  stack[_index];                                \
 })
 
-    as = c->as;
-    arg = c->as_arg;
-    a = unw_get_accessors (as);
-    end_addr = *addr + len;
-    *is_register = 0;
+  as = c->as;
+  arg = c->as_arg;
+  a = unw_get_accessors_int (as);
+  end_addr = *addr + len;
+  *is_register = 0;
 
-    Debug (14, "len=%lu, pushing cfa=0x%lx\n",
-           (unsigned long) len, (unsigned long) c->cfa);
+  Debug (14, "len=%lu, pushing cfa=0x%lx\n",
+         (unsigned long) len, (unsigned long) c->cfa);
 
-    push (c->cfa);	/* push current CFA as required by DWARF spec */
+  push (c->cfa);        /* push current CFA as required by DWARF spec */
 
-    while (*addr < end_addr)
+  while (*addr < end_addr)
     {
-        if ((ret = dwarf_readu8 (as, a, addr, &opcode, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_readu8 (as, a, addr, &opcode, arg)) < 0)
+        return ret;
 
-        operands_signature = operands[opcode];
+      operands_signature = operands[opcode];
 
-        if (unlikely (NUM_OPERANDS (operands_signature) > 0))
+      if (unlikely (NUM_OPERANDS (operands_signature) > 0))
         {
+          if ((ret = read_operand (as, a, addr,
+                                   OPND1_TYPE (operands_signature),
+                                   &operand1, arg)) < 0)
+            return ret;
+          if (NUM_OPERANDS (operands_signature) > 1)
             if ((ret = read_operand (as, a, addr,
-                                     OPND1_TYPE (operands_signature),
-                                     &operand1, arg)) < 0)
-                return ret;
-            if (NUM_OPERANDS (operands_signature > 1))
-                if ((ret = read_operand (as, a, addr,
-                                         OPND2_TYPE (operands_signature),
-                                         &operand2, arg)) < 0)
-                    return ret;
+                                     OPND2_TYPE (operands_signature),
+                                     &operand2, arg)) < 0)
+              return ret;
         }
 
-        switch ((dwarf_expr_op_t) opcode)
+      switch ((dwarf_expr_op_t) opcode)
         {
-        case DW_OP_lit0:
-        case DW_OP_lit1:
-        case DW_OP_lit2:
-        case DW_OP_lit3:
-        case DW_OP_lit4:
-        case DW_OP_lit5:
-        case DW_OP_lit6:
-        case DW_OP_lit7:
-        case DW_OP_lit8:
-        case DW_OP_lit9:
-        case DW_OP_lit10:
-        case DW_OP_lit11:
-        case DW_OP_lit12:
-        case DW_OP_lit13:
-        case DW_OP_lit14:
-        case DW_OP_lit15:
-        case DW_OP_lit16:
-        case DW_OP_lit17:
-        case DW_OP_lit18:
-        case DW_OP_lit19:
-        case DW_OP_lit20:
-        case DW_OP_lit21:
-        case DW_OP_lit22:
-        case DW_OP_lit23:
-        case DW_OP_lit24:
-        case DW_OP_lit25:
-        case DW_OP_lit26:
-        case DW_OP_lit27:
-        case DW_OP_lit28:
-        case DW_OP_lit29:
-        case DW_OP_lit30:
-        case DW_OP_lit31:
-            Debug (15, "OP_lit(%d)\n", (int) opcode - DW_OP_lit0);
-            push (opcode - DW_OP_lit0);
-            break;
+        case DW_OP_lit0:  case DW_OP_lit1:  case DW_OP_lit2:
+        case DW_OP_lit3:  case DW_OP_lit4:  case DW_OP_lit5:
+        case DW_OP_lit6:  case DW_OP_lit7:  case DW_OP_lit8:
+        case DW_OP_lit9:  case DW_OP_lit10: case DW_OP_lit11:
+        case DW_OP_lit12: case DW_OP_lit13: case DW_OP_lit14:
+        case DW_OP_lit15: case DW_OP_lit16: case DW_OP_lit17:
+        case DW_OP_lit18: case DW_OP_lit19: case DW_OP_lit20:
+        case DW_OP_lit21: case DW_OP_lit22: case DW_OP_lit23:
+        case DW_OP_lit24: case DW_OP_lit25: case DW_OP_lit26:
+        case DW_OP_lit27: case DW_OP_lit28: case DW_OP_lit29:
+        case DW_OP_lit30: case DW_OP_lit31:
+          Debug (15, "OP_lit(%d)\n", (int) opcode - DW_OP_lit0);
+          push (opcode - DW_OP_lit0);
+          break;
 
-        case DW_OP_breg0:
-        case DW_OP_breg1:
-        case DW_OP_breg2:
-        case DW_OP_breg3:
-        case DW_OP_breg4:
-        case DW_OP_breg5:
-        case DW_OP_breg6:
-        case DW_OP_breg7:
-        case DW_OP_breg8:
-        case DW_OP_breg9:
-        case DW_OP_breg10:
-        case DW_OP_breg11:
-        case DW_OP_breg12:
-        case DW_OP_breg13:
-        case DW_OP_breg14:
-        case DW_OP_breg15:
-        case DW_OP_breg16:
-        case DW_OP_breg17:
-        case DW_OP_breg18:
-        case DW_OP_breg19:
-        case DW_OP_breg20:
-        case DW_OP_breg21:
-        case DW_OP_breg22:
-        case DW_OP_breg23:
-        case DW_OP_breg24:
-        case DW_OP_breg25:
-        case DW_OP_breg26:
-        case DW_OP_breg27:
-        case DW_OP_breg28:
-        case DW_OP_breg29:
-        case DW_OP_breg30:
-        case DW_OP_breg31:
-            Debug (15, "OP_breg(r%d,0x%lx)\n",
-                   (int) opcode - DW_OP_breg0, (unsigned long) operand1);
-            if ((ret = unw_get_reg (dwarf_to_cursor (c),
-                                    dwarf_to_unw_regnum (opcode - DW_OP_breg0),
-                                    &tmp1)) < 0)
-                return ret;
-            push (tmp1 + operand1);
-            break;
+        case DW_OP_breg0:  case DW_OP_breg1:  case DW_OP_breg2:
+        case DW_OP_breg3:  case DW_OP_breg4:  case DW_OP_breg5:
+        case DW_OP_breg6:  case DW_OP_breg7:  case DW_OP_breg8:
+        case DW_OP_breg9:  case DW_OP_breg10: case DW_OP_breg11:
+        case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14:
+        case DW_OP_breg15: case DW_OP_breg16: case DW_OP_breg17:
+        case DW_OP_breg18: case DW_OP_breg19: case DW_OP_breg20:
+        case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
+        case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26:
+        case DW_OP_breg27: case DW_OP_breg28: case DW_OP_breg29:
+        case DW_OP_breg30: case DW_OP_breg31:
+          Debug (15, "OP_breg(r%d,0x%lx)\n",
+                 (int) opcode - DW_OP_breg0, (unsigned long) operand1);
+          if ((ret = unw_get_reg (dwarf_to_cursor (c),
+                                  dwarf_to_unw_regnum (opcode - DW_OP_breg0),
+                                  &tmp1)) < 0)
+            return ret;
+          push (tmp1 + operand1);
+          break;
 
         case DW_OP_bregx:
-            Debug (15, "OP_bregx(r%d,0x%lx)\n",
-                   (int) operand1, (unsigned long) operand2);
-            if ((ret = unw_get_reg (dwarf_to_cursor (c),
-                                    dwarf_to_unw_regnum (operand1), &tmp1)) < 0)
-                return ret;
-            push (tmp1 + operand2);
-            break;
+          Debug (15, "OP_bregx(r%d,0x%lx)\n",
+                 (int) operand1, (unsigned long) operand2);
+          if ((ret = unw_get_reg (dwarf_to_cursor (c),
+                                  dwarf_to_unw_regnum (operand1), &tmp1)) < 0)
+            return ret;
+          push (tmp1 + operand2);
+          break;
 
-        case DW_OP_reg0:
-        case DW_OP_reg1:
-        case DW_OP_reg2:
-        case DW_OP_reg3:
-        case DW_OP_reg4:
-        case DW_OP_reg5:
-        case DW_OP_reg6:
-        case DW_OP_reg7:
-        case DW_OP_reg8:
-        case DW_OP_reg9:
-        case DW_OP_reg10:
-        case DW_OP_reg11:
-        case DW_OP_reg12:
-        case DW_OP_reg13:
-        case DW_OP_reg14:
-        case DW_OP_reg15:
-        case DW_OP_reg16:
-        case DW_OP_reg17:
-        case DW_OP_reg18:
-        case DW_OP_reg19:
-        case DW_OP_reg20:
-        case DW_OP_reg21:
-        case DW_OP_reg22:
-        case DW_OP_reg23:
-        case DW_OP_reg24:
-        case DW_OP_reg25:
-        case DW_OP_reg26:
-        case DW_OP_reg27:
-        case DW_OP_reg28:
-        case DW_OP_reg29:
-        case DW_OP_reg30:
-        case DW_OP_reg31:
-            Debug (15, "OP_reg(r%d)\n", (int) opcode - DW_OP_reg0);
-            *valp = dwarf_to_unw_regnum (opcode - DW_OP_reg0);
-            *is_register = 1;
-            return 0;
+        case DW_OP_reg0:  case DW_OP_reg1:  case DW_OP_reg2:
+        case DW_OP_reg3:  case DW_OP_reg4:  case DW_OP_reg5:
+        case DW_OP_reg6:  case DW_OP_reg7:  case DW_OP_reg8:
+        case DW_OP_reg9:  case DW_OP_reg10: case DW_OP_reg11:
+        case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14:
+        case DW_OP_reg15: case DW_OP_reg16: case DW_OP_reg17:
+        case DW_OP_reg18: case DW_OP_reg19: case DW_OP_reg20:
+        case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
+        case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26:
+        case DW_OP_reg27: case DW_OP_reg28: case DW_OP_reg29:
+        case DW_OP_reg30: case DW_OP_reg31:
+          Debug (15, "OP_reg(r%d)\n", (int) opcode - DW_OP_reg0);
+          *valp = dwarf_to_unw_regnum (opcode - DW_OP_reg0);
+          *is_register = 1;
+          return 0;
 
         case DW_OP_regx:
-            Debug (15, "OP_regx(r%d)\n", (int) operand1);
-            *valp = dwarf_to_unw_regnum (operand1);
-            *is_register = 1;
-            return 0;
+          Debug (15, "OP_regx(r%d)\n", (int) operand1);
+          *valp = dwarf_to_unw_regnum (operand1);
+          *is_register = 1;
+          return 0;
 
         case DW_OP_addr:
         case DW_OP_const1u:
@@ -414,296 +386,296 @@
         case DW_OP_constu:
         case DW_OP_const8s:
         case DW_OP_consts:
-            Debug (15, "OP_const(0x%lx)\n", (unsigned long) operand1);
-            push (operand1);
-            break;
+          Debug (15, "OP_const(0x%lx)\n", (unsigned long) operand1);
+          push (operand1);
+          break;
 
         case DW_OP_const1s:
-            if (operand1 & 0x80)
-                operand1 |= ((unw_word_t) -1) << 8;
-            Debug (15, "OP_const1s(%ld)\n", (long) operand1);
-            push (operand1);
-            break;
+          if (operand1 & 0x80)
+            operand1 |= ((unw_word_t) -1) << 8;
+          Debug (15, "OP_const1s(%ld)\n", (long) operand1);
+          push (operand1);
+          break;
 
         case DW_OP_const2s:
-            if (operand1 & 0x8000)
-                operand1 |= ((unw_word_t) -1) << 16;
-            Debug (15, "OP_const2s(%ld)\n", (long) operand1);
-            push (operand1);
-            break;
+          if (operand1 & 0x8000)
+            operand1 |= ((unw_word_t) -1) << 16;
+          Debug (15, "OP_const2s(%ld)\n", (long) operand1);
+          push (operand1);
+          break;
 
         case DW_OP_const4s:
-            if (operand1 & 0x80000000)
-                operand1 |= (((unw_word_t) -1) << 16) << 16;
-            Debug (15, "OP_const4s(%ld)\n", (long) operand1);
-            push (operand1);
-            break;
+          if (operand1 & 0x80000000)
+            operand1 |= (((unw_word_t) -1) << 16) << 16;
+          Debug (15, "OP_const4s(%ld)\n", (long) operand1);
+          push (operand1);
+          break;
 
         case DW_OP_deref:
-            Debug (15, "OP_deref\n");
-            tmp1 = pop ();
-            if ((ret = dwarf_readw (as, a, &tmp1, &tmp2, arg)) < 0)
-                return ret;
-            push (tmp2);
-            break;
+          Debug (15, "OP_deref\n");
+          tmp1 = pop ();
+          if ((ret = dwarf_readw (as, a, &tmp1, &tmp2, arg)) < 0)
+            return ret;
+          push (tmp2);
+          break;
 
         case DW_OP_deref_size:
-            Debug (15, "OP_deref_size(%d)\n", (int) operand1);
-            tmp1 = pop ();
-            switch (operand1)
+          Debug (15, "OP_deref_size(%d)\n", (int) operand1);
+          tmp1 = pop ();
+          switch (operand1)
             {
             default:
-                Debug (1, "Unexpected DW_OP_deref_size size %d\n",
-                       (int) operand1);
-                return -UNW_EINVAL;
+              Debug (1, "Unexpected DW_OP_deref_size size %d\n",
+                     (int) operand1);
+              return -UNW_EINVAL;
 
             case 1:
-                if ((ret = dwarf_readu8 (as, a, &tmp1, &u8, arg)) < 0)
-                    return ret;
-                tmp2 = u8;
-                break;
+              if ((ret = dwarf_readu8 (as, a, &tmp1, &u8, arg)) < 0)
+                return ret;
+              tmp2 = u8;
+              break;
 
             case 2:
-                if ((ret = dwarf_readu16 (as, a, &tmp1, &u16, arg)) < 0)
-                    return ret;
-                tmp2 = u16;
-                break;
+              if ((ret = dwarf_readu16 (as, a, &tmp1, &u16, arg)) < 0)
+                return ret;
+              tmp2 = u16;
+              break;
 
             case 3:
             case 4:
-                if ((ret = dwarf_readu32 (as, a, &tmp1, &u32, arg)) < 0)
-                    return ret;
-                tmp2 = u32;
-                if (operand1 == 3)
+              if ((ret = dwarf_readu32 (as, a, &tmp1, &u32, arg)) < 0)
+                return ret;
+              tmp2 = u32;
+              if (operand1 == 3)
                 {
-                    if (dwarf_is_big_endian (as))
-                        tmp2 >>= 8;
-                    else
-                        tmp2 &= 0xffffff;
+                  if (dwarf_is_big_endian (as))
+                    tmp2 >>= 8;
+                  else
+                    tmp2 &= 0xffffff;
                 }
-                break;
+              break;
             case 5:
             case 6:
             case 7:
             case 8:
-                if ((ret = dwarf_readu64 (as, a, &tmp1, &u64, arg)) < 0)
-                    return ret;
-                tmp2 = u64;
-                if (operand1 != 8)
+              if ((ret = dwarf_readu64 (as, a, &tmp1, &u64, arg)) < 0)
+                return ret;
+              tmp2 = u64;
+              if (operand1 != 8)
                 {
-                    if (dwarf_is_big_endian (as))
-                        tmp2 >>= 64 - 8 * operand1;
-                    else
-                        tmp2 &= (~ (unw_word_t) 0) << (8 * operand1);
+                  if (dwarf_is_big_endian (as))
+                    tmp2 >>= 64 - 8 * operand1;
+                  else
+                    tmp2 &= (~ (unw_word_t) 0) << (8 * operand1);
                 }
-                break;
+              break;
             }
-            push (tmp2);
-            break;
+          push (tmp2);
+          break;
 
         case DW_OP_dup:
-            Debug (15, "OP_dup\n");
-            push (pick (0));
-            break;
+          Debug (15, "OP_dup\n");
+          push (pick (0));
+          break;
 
         case DW_OP_drop:
-            Debug (15, "OP_drop\n");
-            pop ();
-            break;
+          Debug (15, "OP_drop\n");
+          (void) pop ();
+          break;
 
         case DW_OP_pick:
-            Debug (15, "OP_pick(%d)\n", (int) operand1);
-            push (pick (operand1));
-            break;
+          Debug (15, "OP_pick(%d)\n", (int) operand1);
+          push (pick (operand1));
+          break;
 
         case DW_OP_over:
-            Debug (15, "OP_over\n");
-            push (pick (1));
-            break;
+          Debug (15, "OP_over\n");
+          push (pick (1));
+          break;
 
         case DW_OP_swap:
-            Debug (15, "OP_swap\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp1);
-            push (tmp2);
-            break;
+          Debug (15, "OP_swap\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp1);
+          push (tmp2);
+          break;
 
         case DW_OP_rot:
-            Debug (15, "OP_rot\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            tmp3 = pop ();
-            push (tmp1);
-            push (tmp3);
-            push (tmp2);
-            break;
+          Debug (15, "OP_rot\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          tmp3 = pop ();
+          push (tmp1);
+          push (tmp3);
+          push (tmp2);
+          break;
 
         case DW_OP_abs:
-            Debug (15, "OP_abs\n");
-            tmp1 = pop ();
-            if (tmp1 & ((unw_word_t) 1 << (8 * dwarf_addr_size (as) - 1)))
-                tmp1 = -tmp1;
-            push (tmp1);
-            break;
+          Debug (15, "OP_abs\n");
+          tmp1 = pop ();
+          if (tmp1 & ((unw_word_t) 1 << (8 * dwarf_addr_size (as) - 1)))
+            tmp1 = -tmp1;
+          push (tmp1);
+          break;
 
         case DW_OP_and:
-            Debug (15, "OP_and\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp1 & tmp2);
-            break;
+          Debug (15, "OP_and\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp1 & tmp2);
+          break;
 
         case DW_OP_div:
-            Debug (15, "OP_div\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            if (tmp1)
-                tmp1 = sword (as, tmp2) / sword (as, tmp1);
-            push (tmp1);
-            break;
+          Debug (15, "OP_div\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          if (tmp1)
+            tmp1 = sword (as, tmp2) / sword (as, tmp1);
+          push (tmp1);
+          break;
 
         case DW_OP_minus:
-            Debug (15, "OP_minus\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            tmp1 = tmp2 - tmp1;
-            push (tmp1);
-            break;
+          Debug (15, "OP_minus\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          tmp1 = tmp2 - tmp1;
+          push (tmp1);
+          break;
 
         case DW_OP_mod:
-            Debug (15, "OP_mod\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            if (tmp1)
-                tmp1 = tmp2 % tmp1;
-            push (tmp1);
-            break;
+          Debug (15, "OP_mod\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          if (tmp1)
+            tmp1 = tmp2 % tmp1;
+          push (tmp1);
+          break;
 
         case DW_OP_mul:
-            Debug (15, "OP_mul\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            if (tmp1)
-                tmp1 = tmp2 * tmp1;
-            push (tmp1);
-            break;
+          Debug (15, "OP_mul\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          if (tmp1)
+            tmp1 = tmp2 * tmp1;
+          push (tmp1);
+          break;
 
         case DW_OP_neg:
-            Debug (15, "OP_neg\n");
-            push (-pop ());
-            break;
+          Debug (15, "OP_neg\n");
+          push (-pop ());
+          break;
 
         case DW_OP_not:
-            Debug (15, "OP_not\n");
-            push (~pop ());
-            break;
+          Debug (15, "OP_not\n");
+          push (~pop ());
+          break;
 
         case DW_OP_or:
-            Debug (15, "OP_or\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp1 | tmp2);
-            break;
+          Debug (15, "OP_or\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp1 | tmp2);
+          break;
 
         case DW_OP_plus:
-            Debug (15, "OP_plus\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp1 + tmp2);
-            break;
+          Debug (15, "OP_plus\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp1 + tmp2);
+          break;
 
         case DW_OP_plus_uconst:
-            Debug (15, "OP_plus_uconst(%lu)\n", (unsigned long) operand1);
-            tmp1 = pop ();
-            push (tmp1 + operand1);
-            break;
+          Debug (15, "OP_plus_uconst(%lu)\n", (unsigned long) operand1);
+          tmp1 = pop ();
+          push (tmp1 + operand1);
+          break;
 
         case DW_OP_shl:
-            Debug (15, "OP_shl\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp2 << tmp1);
-            break;
+          Debug (15, "OP_shl\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp2 << tmp1);
+          break;
 
         case DW_OP_shr:
-            Debug (15, "OP_shr\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp2 >> tmp1);
-            break;
+          Debug (15, "OP_shr\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp2 >> tmp1);
+          break;
 
         case DW_OP_shra:
-            Debug (15, "OP_shra\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp2) >> tmp1);
-            break;
+          Debug (15, "OP_shra\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) >> tmp1);
+          break;
 
         case DW_OP_xor:
-            Debug (15, "OP_xor\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (tmp1 ^ tmp2);
-            break;
+          Debug (15, "OP_xor\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (tmp1 ^ tmp2);
+          break;
 
         case DW_OP_le:
-            Debug (15, "OP_le\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) <= sword (as, tmp2));
-            break;
+          Debug (15, "OP_le\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) <= sword (as, tmp1));
+          break;
 
         case DW_OP_ge:
-            Debug (15, "OP_ge\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) >= sword (as, tmp2));
-            break;
+          Debug (15, "OP_ge\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) >= sword (as, tmp1));
+          break;
 
         case DW_OP_eq:
-            Debug (15, "OP_eq\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) == sword (as, tmp2));
-            break;
+          Debug (15, "OP_eq\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) == sword (as, tmp1));
+          break;
 
         case DW_OP_lt:
-            Debug (15, "OP_lt\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) < sword (as, tmp2));
-            break;
+          Debug (15, "OP_lt\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) < sword (as, tmp1));
+          break;
 
         case DW_OP_gt:
-            Debug (15, "OP_gt\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) > sword (as, tmp2));
-            break;
+          Debug (15, "OP_gt\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) > sword (as, tmp1));
+          break;
 
         case DW_OP_ne:
-            Debug (15, "OP_ne\n");
-            tmp1 = pop ();
-            tmp2 = pop ();
-            push (sword (as, tmp1) != sword (as, tmp2));
-            break;
+          Debug (15, "OP_ne\n");
+          tmp1 = pop ();
+          tmp2 = pop ();
+          push (sword (as, tmp2) != sword (as, tmp1));
+          break;
 
         case DW_OP_skip:
-            Debug (15, "OP_skip(%d)\n", (int16_t) operand1);
-            *addr += (int16_t) operand1;
-            break;
+          Debug (15, "OP_skip(%d)\n", (int16_t) operand1);
+          *addr += (int16_t) operand1;
+          break;
 
         case DW_OP_bra:
-            Debug (15, "OP_skip(%d)\n", (int16_t) operand1);
-            tmp1 = pop ();
-            if (tmp1)
-                *addr += (int16_t) operand1;
-            break;
+          Debug (15, "OP_skip(%d)\n", (int16_t) operand1);
+          tmp1 = pop ();
+          if (tmp1)
+            *addr += (int16_t) operand1;
+          break;
 
         case DW_OP_nop:
-            Debug (15, "OP_nop\n");
-            break;
+          Debug (15, "OP_nop\n");
+          break;
 
         case DW_OP_call2:
         case DW_OP_call4:
@@ -714,11 +686,11 @@
         case DW_OP_xderef:
         case DW_OP_xderef_size:
         default:
-            Debug (1, "Unexpected opcode 0x%x\n", opcode);
-            return -UNW_EINVAL;
+          Debug (1, "Unexpected opcode 0x%x\n", opcode);
+          return -UNW_EINVAL;
         }
     }
-    *valp = pop ();
-    Debug (14, "final value = 0x%lx\n", (unsigned long) *valp);
-    return 0;
+  *valp = pop ();
+  Debug (14, "final value = 0x%lx\n", (unsigned long) *valp);
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Gfde.c b/sgx_unwind/libunwind/src/dwarf/Gfde.c
index 817ed75..9250b89 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gfde.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gfde.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -28,13 +28,13 @@
 static inline int
 is_cie_id (unw_word_t val, int is_debug_frame)
 {
-    /* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
-       0xffffffffffffffff (for 64-bit ELF).  However, .eh_frame
-       uses 0.  */
-    if (is_debug_frame)
-        return (val == - (uint32_t) 1 || val == - (uint64_t) 1);
-    else
-        return (val == 0);
+  /* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
+     0xffffffffffffffff (for 64-bit ELF).  However, .eh_frame
+     uses 0.  */
+  if (is_debug_frame)
+      return (val == (uint32_t)(-1) || val == (uint64_t)(-1));
+  else
+    return (val == 0);
 }
 
 /* Note: we don't need to keep track of more than the first four
@@ -45,319 +45,315 @@
 static inline int
 parse_cie (unw_addr_space_t as, unw_accessors_t *a, unw_word_t addr,
            const unw_proc_info_t *pi, struct dwarf_cie_info *dci,
-           unw_word_t base, void *arg)
+           int is_debug_frame, void *arg)
 {
-    uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
-    unw_word_t len, cie_end_addr, aug_size;
-    uint32_t u32val;
-    uint64_t u64val;
-    size_t i;
-    int ret;
-# define STR2(x)	#x
-# define STR(x)		STR2(x)
+  uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
+  unw_word_t len, cie_end_addr, aug_size;
+  uint32_t u32val;
+  uint64_t u64val;
+  size_t i;
+  int ret;
+# define STR2(x)        #x
+# define STR(x)         STR2(x)
 
-    /* Pick appropriate default for FDE-encoding.  DWARF spec says
-       start-IP (initial_location) and the code-size (address_range) are
-       "address-unit sized constants".  The `R' augmentation can be used
-       to override this, but by default, we pick an address-sized unit
-       for fde_encoding.  */
-    switch (dwarf_addr_size (as))
+  /* Pick appropriate default for FDE-encoding.  DWARF spec says
+     start-IP (initial_location) and the code-size (address_range) are
+     "address-unit sized constants".  The `R' augmentation can be used
+     to override this, but by default, we pick an address-sized unit
+     for fde_encoding.  */
+  switch (dwarf_addr_size (as))
     {
-    case 4:
-        fde_encoding = DW_EH_PE_udata4;
-        break;
-    case 8:
-        fde_encoding = DW_EH_PE_udata8;
-        break;
-    default:
-        fde_encoding = DW_EH_PE_omit;
-        break;
+    case 4:     fde_encoding = DW_EH_PE_udata4; break;
+    case 8:     fde_encoding = DW_EH_PE_udata8; break;
+    default:    fde_encoding = DW_EH_PE_omit; break;
     }
 
-    dci->lsda_encoding = DW_EH_PE_omit;
-    dci->handler = 0;
+  dci->lsda_encoding = DW_EH_PE_omit;
+  dci->handler = 0;
 
-    if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
+    return ret;
 
-    if (u32val != 0xffffffff)
+  if (u32val != 0xffffffff)
     {
-        /* the CIE is in the 32-bit DWARF format */
-        uint32_t cie_id;
-        /* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
-        const uint32_t expected_id = (base) ? 0xffffffff : 0;
+      /* the CIE is in the 32-bit DWARF format */
+      uint32_t cie_id;
+      /* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
+      const uint32_t expected_id = (is_debug_frame) ? 0xffffffff : 0;
 
-        len = u32val;
-        cie_end_addr = addr + len;
-        if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
-            return ret;
-        if (cie_id != expected_id)
+      len = u32val;
+      cie_end_addr = addr + len;
+      if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
+        return ret;
+      if (cie_id != expected_id)
         {
-            Debug (1, "Unexpected CIE id %x\n", cie_id);
-            return -UNW_EINVAL;
+          Debug (1, "Unexpected CIE id %x\n", cie_id);
+          return -UNW_EINVAL;
         }
     }
-    else
+  else
     {
-        /* the CIE is in the 64-bit DWARF format */
-        uint64_t cie_id;
-        /* DWARF says CIE id should be 0xffffffffffffffff, but in
-        .eh_frame, it's 0 */
-        const uint64_t expected_id = (base) ? 0xffffffffffffffffull : 0;
+      /* the CIE is in the 64-bit DWARF format */
+      uint64_t cie_id;
+      /* DWARF says CIE id should be 0xffffffffffffffff, but in
+         .eh_frame, it's 0 */
+      const uint64_t expected_id = (is_debug_frame) ? 0xffffffffffffffffull : 0;
 
-        if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
-            return ret;
-        len = u64val;
-        cie_end_addr = addr + len;
-        if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
-            return ret;
-        if (cie_id != expected_id)
+      if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
+        return ret;
+      len = u64val;
+      cie_end_addr = addr + len;
+      if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
+        return ret;
+      if (cie_id != expected_id)
         {
-            Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
-            return -UNW_EINVAL;
+          Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
+          return -UNW_EINVAL;
         }
     }
-    dci->cie_instr_end = cie_end_addr;
+  dci->cie_instr_end = cie_end_addr;
 
-    if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
+    return ret;
 
-    if (version != 1 && version != DWARF_CIE_VERSION)
+  /* GCC emits version 1??? */
+  if (version != 1 && (version < DWARF_CIE_VERSION || version > DWARF_CIE_VERSION_MAX))
     {
-        Debug (1, "Got CIE version %u, expected version 1 or "
-               STR (DWARF_CIE_VERSION) "\n", version);
-        return -UNW_EBADVERSION;
+      Debug (1, "Got CIE version %u, expected version 1 or between "
+             STR (DWARF_CIE_VERSION) " and " STR (DWARF_CIE_VERSION_MAX) "\n", version);
+      return -UNW_EBADVERSION;
     }
 
-    /* read and parse the augmentation string: */
-    memset (augstr, 0, sizeof (augstr));
-    for (i = 0;;)
+  /* read and parse the augmentation string: */
+  memset (augstr, 0, sizeof (augstr));
+  for (i = 0;;)
     {
+      if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
+        return ret;
+
+      if (!ch)
+        break;  /* end of augmentation string */
+
+      if (i < sizeof (augstr) - 1)
+        augstr[i++] = ch;
+    }
+
+  if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
+      || (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
+    return ret;
+
+  /* Read the return-address column either as a u8 or as a uleb128.  */
+  if (version == 1)
+    {
+      if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
+        return ret;
+      dci->ret_addr_column = ch;
+    }
+  else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
+                                      arg)) < 0)
+    return ret;
+
+  i = 0;
+  if (augstr[0] == 'z')
+    {
+      dci->sized_augmentation = 1;
+      if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
+        return ret;
+      i++;
+    }
+
+  for (; i < sizeof (augstr) && augstr[i]; ++i)
+    switch (augstr[i])
+      {
+      case 'L':
+        /* read the LSDA pointer-encoding format.  */
         if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
-            return ret;
+          return ret;
+        dci->lsda_encoding = ch;
+        break;
 
-        if (!ch)
-            break;	/* end of augmentation string */
+      case 'R':
+        /* read the FDE pointer-encoding format.  */
+        if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
+          return ret;
+        break;
 
-        if (i < sizeof (augstr) - 1)
-            augstr[i++] = ch;
-    }
+      case 'P':
+        /* read the personality-routine pointer-encoding format.  */
+        if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
+          return ret;
+        if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
+                                               pi, &dci->handler, arg)) < 0)
+          return ret;
+        break;
 
-    if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
-            || (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
-        return ret;
+      case 'S':
+        /* This is a signal frame. */
+        dci->signal_frame = 1;
 
-    /* Read the return-address column either as a u8 or as a uleb128.  */
-    if (version == 1)
-    {
-        if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
-            return ret;
-        dci->ret_addr_column = ch;
-    }
-    else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
-                                        arg)) < 0)
-        return ret;
+        /* Temporarily set it to one so dwarf_parse_fde() knows that
+           it should fetch the actual ABI/TAG pair from the FDE.  */
+        dci->have_abi_marker = 1;
+        break;
 
-    i = 0;
-    if (augstr[0] == 'z')
-    {
-        dci->sized_augmentation = 1;
-        if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
-            return ret;
-        i++;
-    }
-
-    for (; i < sizeof (augstr) && augstr[i]; ++i)
-        switch (augstr[i])
-        {
-        case 'L':
-            /* read the LSDA pointer-encoding format.  */
-            if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
-                return ret;
-            dci->lsda_encoding = ch;
-            break;
-
-        case 'R':
-            /* read the FDE pointer-encoding format.  */
-            if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
-                return ret;
-            break;
-
-        case 'P':
-            /* read the personality-routine pointer-encoding format.  */
-            if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
-                return ret;
-            if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
-                                                   pi, &dci->handler, arg)) < 0)
-                return ret;
-            break;
-
-        case 'S':
-            /* This is a signal frame. */
-            dci->signal_frame = 1;
-
-            /* Temporarily set it to one so dwarf_parse_fde() knows that
-               it should fetch the actual ABI/TAG pair from the FDE.  */
-            dci->have_abi_marker = 1;
-            break;
-
-        default:
-            Debug (1, "Unexpected augmentation string `%s'\n", augstr);
-            if (dci->sized_augmentation)
-                /* If we have the size of the augmentation body, we can skip
-                   over the parts that we don't understand, so we're OK. */
-                goto done;
-            else
-                return -UNW_EINVAL;
-        }
-done:
-    dci->fde_encoding = fde_encoding;
-    dci->cie_instr_start = addr;
-    Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
-           augstr, (long) dci->handler);
-    return 0;
+      default:
+        Debug (1, "Unexpected augmentation string `%s'\n", augstr);
+        if (dci->sized_augmentation)
+          /* If we have the size of the augmentation body, we can skip
+             over the parts that we don't understand, so we're OK. */
+          goto done;
+        else
+          return -UNW_EINVAL;
+      }
+ done:
+  dci->fde_encoding = fde_encoding;
+  dci->cie_instr_start = addr;
+  Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
+         augstr, (long) dci->handler);
+  return 0;
 }
 
 /* Extract proc-info from the FDE starting at adress ADDR.
-
+   
    Pass BASE as zero for eh_frame behaviour, or a pointer to
    debug_frame base for debug_frame behaviour.  */
 
 HIDDEN int
 dwarf_extract_proc_info_from_fde (unw_addr_space_t as, unw_accessors_t *a,
                                   unw_word_t *addrp, unw_proc_info_t *pi,
-                                  int need_unwind_info, unw_word_t base,
+                                  unw_word_t base,
+                                  int need_unwind_info, int is_debug_frame,
                                   void *arg)
 {
-    unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
-    unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
-    int ret, ip_range_encoding;
-    struct dwarf_cie_info dci;
-    uint64_t u64val;
-    uint32_t u32val;
+  unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
+  unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
+  int ret, ip_range_encoding;
+  struct dwarf_cie_info dci;
+  uint64_t u64val;
+  uint32_t u32val;
 
-    Debug (12, "FDE @ 0x%lx\n", (long) addr);
+  Debug (12, "FDE @ 0x%lx\n", (long) addr);
 
-    memset (&dci, 0, sizeof (dci));
+  memset (&dci, 0, sizeof (dci));
 
-    if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
+  if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
+    return ret;
+
+  if (u32val != 0xffffffff)
+    {
+      int32_t cie_offset = 0;
+
+      /* In some configurations, an FDE with a 0 length indicates the
+         end of the FDE-table.  */
+      if (u32val == 0)
+        return -UNW_ENOINFO;
+
+      /* the FDE is in the 32-bit DWARF format */
+
+      *addrp = fde_end_addr = addr + u32val;
+      cie_offset_addr = addr;
+
+      if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
         return ret;
 
-    if (u32val != 0xffffffff)
-    {
-        int32_t cie_offset;
+      if (is_cie_id (cie_offset, is_debug_frame))
+        /* ignore CIEs (happens during linear searches) */
+        return 0;
 
-        /* In some configurations, an FDE with a 0 length indicates the
-        end of the FDE-table.  */
-        if (u32val == 0)
-            return -UNW_ENOINFO;
-
-        /* the FDE is in the 32-bit DWARF format */
-
-        *addrp = fde_end_addr = addr + u32val;
-        cie_offset_addr = addr;
-
-        if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
-            return ret;
-
-        if (is_cie_id (cie_offset, base != 0))
-            /* ignore CIEs (happens during linear searches) */
-            return 0;
-
-        if (base != 0)
-            cie_addr = base + cie_offset;
-        else
-            /* DWARF says that the CIE_pointer in the FDE is a
-               .debug_frame-relative offset, but the GCC-generated .eh_frame
-               sections instead store a "pcrelative" offset, which is just
-               as fine as it's self-contained.  */
-            cie_addr = cie_offset_addr - cie_offset;
+      if (is_debug_frame)
+        cie_addr = base + cie_offset;
+      else
+        /* DWARF says that the CIE_pointer in the FDE is a
+           .debug_frame-relative offset, but the GCC-generated .eh_frame
+           sections instead store a "pcrelative" offset, which is just
+           as fine as it's self-contained.  */
+        cie_addr = cie_offset_addr - cie_offset;
     }
-    else
+  else
     {
-        int64_t cie_offset;
+      int64_t cie_offset = 0;
 
-        /* the FDE is in the 64-bit DWARF format */
+      /* the FDE is in the 64-bit DWARF format */
 
-        if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
+        return ret;
 
-        *addrp = fde_end_addr = addr + u64val;
-        cie_offset_addr = addr;
+      *addrp = fde_end_addr = addr + u64val;
+      cie_offset_addr = addr;
 
-        if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
-            return ret;
+      if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
+        return ret;
 
-        if (is_cie_id (cie_offset, base != 0))
-            /* ignore CIEs (happens during linear searches) */
-            return 0;
+      if (is_cie_id (cie_offset, is_debug_frame))
+        /* ignore CIEs (happens during linear searches) */
+        return 0;
 
-        if (base != 0)
-            cie_addr = base + cie_offset;
-        else
-            /* DWARF says that the CIE_pointer in the FDE is a
-               .debug_frame-relative offset, but the GCC-generated .eh_frame
-               sections instead store a "pcrelative" offset, which is just
-               as fine as it's self-contained.  */
-            cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
+      if (is_debug_frame)
+        cie_addr = base + cie_offset;
+      else
+        /* DWARF says that the CIE_pointer in the FDE is a
+           .debug_frame-relative offset, but the GCC-generated .eh_frame
+           sections instead store a "pcrelative" offset, which is just
+           as fine as it's self-contained.  */
+        cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
     }
 
-    Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
+  Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
 
-    if ((ret = parse_cie (as, a, cie_addr, pi, &dci, base, arg)) < 0)
-        return ret;
+  if ((ret = parse_cie (as, a, cie_addr, pi, &dci, is_debug_frame, arg)) < 0)
+    return ret;
 
-    /* IP-range has same encoding as FDE pointers, except that it's
-       always an absolute value: */
-    ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
+  /* IP-range has same encoding as FDE pointers, except that it's
+     always an absolute value: */
+  ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
 
-    if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
-                                           pi, &start_ip, arg)) < 0
-            || (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
-                      pi, &ip_range, arg)) < 0)
-        return ret;
-    pi->start_ip = start_ip;
-    pi->end_ip = start_ip + ip_range;
-    pi->handler = dci.handler;
+  if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
+                                         pi, &start_ip, arg)) < 0
+      || (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
+                                            pi, &ip_range, arg)) < 0)
+    return ret;
+  pi->start_ip = start_ip;
+  pi->end_ip = start_ip + ip_range;
+  pi->handler = dci.handler;
 
-    if (dci.sized_augmentation)
+  if (dci.sized_augmentation)
     {
-        if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
-            return ret;
-        aug_end_addr = addr + aug_size;
+      if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
+        return ret;
+      aug_end_addr = addr + aug_size;
     }
 
-    if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
-                                           pi, &pi->lsda, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
+                                         pi, &pi->lsda, arg)) < 0)
+    return ret;
 
-    Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
-           (long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
+  Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
+         (long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
 
-    if (need_unwind_info)
+  if (need_unwind_info)
     {
-        pi->format = UNW_INFO_FORMAT_TABLE;
-        pi->unwind_info_size = sizeof (dci);
-        pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
-        if (!pi->unwind_info)
-            return -UNW_ENOMEM;
+      pi->format = UNW_INFO_FORMAT_TABLE;
+      pi->unwind_info_size = sizeof (dci);
+      pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
+      if (!pi->unwind_info)
+        return -UNW_ENOMEM;
 
-        if (dci.have_abi_marker)
+      if (dci.have_abi_marker)
         {
-            if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
-                    || (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
-                return ret;
-            Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
-                   dci.abi, dci.tag);
+          if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
+              || (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
+            return ret;
+          Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
+                 dci.abi, dci.tag);
         }
 
-        if (dci.sized_augmentation)
-            dci.fde_instr_start = aug_end_addr;
-        else
-            dci.fde_instr_start = addr;
-        dci.fde_instr_end = fde_end_addr;
+      if (dci.sized_augmentation)
+        dci.fde_instr_start = aug_end_addr;
+      else
+        dci.fde_instr_start = addr;
+      dci.fde_instr_end = fde_end_addr;
 
-        memcpy (pi->unwind_info, &dci, sizeof (dci));
+      memcpy (pi->unwind_info, &dci, sizeof (dci));
     }
-    return 0;
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Gfind_proc_info-lsb.c b/sgx_unwind/libunwind/src/dwarf/Gfind_proc_info-lsb.c
index 77d4b35..c2a5be7 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gfind_proc_info-lsb.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gfind_proc_info-lsb.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -26,9 +26,6 @@
 /* Locate an FDE via the ELF data-structures defined by LSB v1.3
    (http://www.linuxbase.org/spec/).  */
 
-#ifndef UNW_REMOTE_ONLY
-#include <link.h>
-#endif /* !UNW_REMOTE_ONLY */
 #include <stddef.h>
 #include <stdio.h>
 #include <limits.h>
@@ -38,48 +35,61 @@
 #include "libunwind_i.h"
 
 struct table_entry
-{
+  {
     int32_t start_ip_offset;
     int32_t fde_offset;
-};
+  };
 
 #ifndef UNW_REMOTE_ONLY
 
-#ifdef __linux
+#if defined(__linux) && (!HAVE_SGX)
 #include "os-linux.h"
 #endif
 
+#ifndef __clang__
+static ALIAS(dwarf_search_unwind_table) int
+dwarf_search_unwind_table_int (unw_addr_space_t as,
+                               unw_word_t ip,
+                               unw_dyn_info_t *di,
+                               unw_proc_info_t *pi,
+                               int need_unwind_info, void *arg);
+#else
+#define dwarf_search_unwind_table_int dwarf_search_unwind_table
+#endif
+
 static int
 linear_search (unw_addr_space_t as, unw_word_t ip,
                unw_word_t eh_frame_start, unw_word_t eh_frame_end,
                unw_word_t fde_count,
                unw_proc_info_t *pi, int need_unwind_info, void *arg)
 {
-    unw_accessors_t *a = unw_get_accessors (unw_local_addr_space);
-    unw_word_t i = 0, fde_addr, addr = eh_frame_start;
-    int ret;
+  unw_accessors_t *a = unw_get_accessors_int (unw_local_addr_space);
+  unw_word_t i = 0, fde_addr, addr = eh_frame_start;
+  int ret;
 
-    while (i++ < fde_count && addr < eh_frame_end)
+  while (i++ < fde_count && addr < eh_frame_end)
     {
-        fde_addr = addr;
-        if ((ret = dwarf_extract_proc_info_from_fde (as, a, &addr, pi, 0, 0, arg))
-                < 0)
-            return ret;
+      fde_addr = addr;
+      if ((ret = dwarf_extract_proc_info_from_fde (as, a, &addr, pi,
+                                                   eh_frame_start,
+                                                   0, 0, arg)) < 0)
+        return ret;
 
-        if (ip >= pi->start_ip && ip < pi->end_ip)
+      if (ip >= pi->start_ip && ip < pi->end_ip)
         {
-            if (!need_unwind_info)
-                return 1;
-            addr = fde_addr;
-            if ((ret = dwarf_extract_proc_info_from_fde (as, a, &addr, pi,
-                       need_unwind_info, 0,
-                       arg))
-                    < 0)
-                return ret;
+          if (!need_unwind_info)
             return 1;
+          addr = fde_addr;
+          if ((ret = dwarf_extract_proc_info_from_fde (as, a, &addr, pi,
+                                                       eh_frame_start,
+                                                       need_unwind_info, 0,
+                                                       arg))
+              < 0)
+            return ret;
+          return 1;
         }
     }
-    return -UNW_ENOINFO;
+  return -UNW_ENOINFO;
 }
 #endif /* !UNW_REMOTE_ONLY */
 
@@ -87,151 +97,42 @@
 /* Load .debug_frame section from FILE.  Allocates and returns space
    in *BUF, and sets *BUFSIZE to its size.  IS_LOCAL is 1 if using the
    local process, in which case we can search the system debug file
-   directory; 0 for other address spaces, in which case we do not; or
-   -1 for recursive calls following .gnu_debuglink.  Returns 0 on
-   success, 1 on error.  Succeeds even if the file contains no
-   .debug_frame.  */
+   directory; 0 for other address spaces, in which case we do
+   not. Returns 0 on success, 1 on error.  Succeeds even if the file
+   contains no .debug_frame.  */
 /* XXX: Could use mmap; but elf_map_image keeps tons mapped in.  */
 
 static int
 load_debug_frame (const char *file, char **buf, size_t *bufsize, int is_local)
 {
-    FILE *f;
-    Elf_W (Ehdr) ehdr;
-    Elf_W (Half) shstrndx;
-    Elf_W (Shdr) *sec_hdrs = NULL;
-    char *stringtab = NULL;
-    unsigned int i;
-    size_t linksize = 0;
-    char *linkbuf = NULL;
+  struct elf_image ei;
+  Elf_W (Shdr) *shdr;
+  int ret;
 
-    *buf = NULL;
-    *bufsize = 0;
+  ei.image = NULL;
 
-    f = fopen (file, "r");
+  ret = elf_w (load_debuglink) (file, &ei, is_local);
+  if (ret != 0)
+    return ret;
 
-    if (!f)
-        return 1;
-
-    if (fread (&ehdr, sizeof (Elf_W (Ehdr)), 1, f) != 1)
-        goto file_error;
-
-    shstrndx = ehdr.e_shstrndx;
-
-    Debug (4, "opened file '%s'. Section header at offset %d\n",
-           file, (int) ehdr.e_shoff);
-
-    fseek (f, ehdr.e_shoff, SEEK_SET);
-    sec_hdrs = calloc (ehdr.e_shnum, sizeof (Elf_W (Shdr)));
-    if (fread (sec_hdrs, sizeof (Elf_W (Shdr)), ehdr.e_shnum, f) != ehdr.e_shnum)
-        goto file_error;
-
-    Debug (4, "loading string table of size %zd\n",
-           sec_hdrs[shstrndx].sh_size);
-    stringtab = malloc (sec_hdrs[shstrndx].sh_size);
-    fseek (f, sec_hdrs[shstrndx].sh_offset, SEEK_SET);
-    if (fread (stringtab, 1, sec_hdrs[shstrndx].sh_size, f) != sec_hdrs[shstrndx].sh_size)
-        goto file_error;
-
-    for (i = 1; i < ehdr.e_shnum && *buf == NULL; i++)
+  shdr = elf_w (find_section) (&ei, ".debug_frame");
+  if (!shdr ||
+      (shdr->sh_offset + shdr->sh_size > ei.size))
     {
-        char *secname = &stringtab[sec_hdrs[i].sh_name];
-
-        if (strcmp (secname, ".debug_frame") == 0)
-        {
-            *bufsize = sec_hdrs[i].sh_size;
-            *buf = malloc (*bufsize);
-
-            fseek (f, sec_hdrs[i].sh_offset, SEEK_SET);
-            if (fread (*buf, 1, *bufsize, f) != *bufsize)
-                goto file_error;
-
-            Debug (4, "read %zd bytes of .debug_frame from offset %zd\n",
-                   *bufsize, sec_hdrs[i].sh_offset);
-        }
-        else if (strcmp (secname, ".gnu_debuglink") == 0)
-        {
-            linksize = sec_hdrs[i].sh_size;
-            linkbuf = malloc (linksize);
-
-            fseek (f, sec_hdrs[i].sh_offset, SEEK_SET);
-            if (fread (linkbuf, 1, linksize, f) != linksize)
-                goto file_error;
-
-            Debug (4, "read %zd bytes of .gnu_debuglink from offset %zd\n",
-                   linksize, sec_hdrs[i].sh_offset);
-        }
+      munmap(ei.image, ei.size);
+      return 1;
     }
 
-    free (stringtab);
-    free (sec_hdrs);
+  *bufsize = shdr->sh_size;
+  *buf = malloc (*bufsize);
 
-    fclose (f);
+  memcpy(*buf, shdr->sh_offset + ei.image, *bufsize);
 
-    /* Ignore separate debug files which contain a .gnu_debuglink section. */
-    if (linkbuf && is_local == -1)
-    {
-        free (linkbuf);
-        return 1;
-    }
+  Debug (4, "read %zd bytes of .debug_frame from offset %zd\n",
+	 *bufsize, shdr->sh_offset);
 
-    if (*buf == NULL && linkbuf != NULL && memchr (linkbuf, 0, linksize) != NULL)
-    {
-        char *newname, *basedir, *p;
-        static const char *debugdir = "/usr/lib/debug";
-        int ret;
-
-        /* XXX: Don't bother with the checksum; just search for the file.  */
-        basedir = malloc (strlen (file) + 1);
-        newname = malloc (strlen (linkbuf) + strlen (debugdir)
-                          + strlen (file) + 9);
-
-        p = strrchr (file, '/');
-        if (p != NULL)
-        {
-            memcpy (basedir, file, p - file);
-            basedir[p - file] = '\0';
-        }
-        else
-            basedir[0] = 0;
-
-        strcpy (newname, basedir);
-        strcat (newname, "/");
-        strcat (newname, linkbuf);
-        ret = load_debug_frame (newname, buf, bufsize, -1);
-
-        if (ret == 1)
-        {
-            strcpy (newname, basedir);
-            strcat (newname, "/.debug/");
-            strcat (newname, linkbuf);
-            ret = load_debug_frame (newname, buf, bufsize, -1);
-        }
-
-        if (ret == 1 && is_local == 1)
-        {
-            strcpy (newname, debugdir);
-            strcat (newname, basedir);
-            strcat (newname, "/");
-            strcat (newname, linkbuf);
-            ret = load_debug_frame (newname, buf, bufsize, -1);
-        }
-
-        free (basedir);
-        free (newname);
-    }
-    free (linkbuf);
-
-    return 0;
-
-    /* An error reading image file. Release resources and return error code */
-file_error:
-    if (stringtab) free(stringtab);
-    if (sec_hdrs) free(sec_hdrs);
-    if (linkbuf) free(linkbuf);
-    fclose(f);
-
-    return 1;
+  munmap(ei.image, ei.size);
+  return 0;
 }
 
 /* Locate the binary which originated the contents of address ADDR. Return
@@ -242,29 +143,31 @@
 find_binary_for_address (unw_word_t ip, char *name, size_t name_size)
 {
 #if defined(__linux) && (!UNW_REMOTE_ONLY) && (!HAVE_SGX)
-    struct map_iterator mi;
-    int found = 0;
-    int pid = getpid ();
-    unsigned long segbase, mapoff, hi;
+  struct map_iterator mi;
+  int found = 0;
+  int pid = getpid ();
+  unsigned long segbase, mapoff, hi;
 
-    maps_init (&mi, pid);
-    while (maps_next (&mi, &segbase, &hi, &mapoff))
-        if (ip >= segbase && ip < hi)
-        {
-            size_t len = strlen (mi.path);
+  if (maps_init (&mi, pid) != 0)
+    return 1;
 
-            if (len + 1 <= name_size)
-            {
-                memcpy (name, mi.path, len + 1);
-                found = 1;
-            }
-            break;
-        }
-    maps_close (&mi);
-    return !found;
+  while (maps_next (&mi, &segbase, &hi, &mapoff))
+    if (ip >= segbase && ip < hi)
+      {
+        size_t len = strlen (mi.path);
+
+        if (len + 1 <= name_size)
+          {
+            memcpy (name, mi.path, len + 1);
+            found = 1;
+          }
+        break;
+      }
+  maps_close (&mi);
+  return !found;
 #endif
 
-    return 1;
+  return 1;
 }
 
 /* Locate and/or try to load a debug_frame section for address ADDR.  Return
@@ -274,518 +177,593 @@
 locate_debug_info (unw_addr_space_t as, unw_word_t addr, const char *dlname,
                    unw_word_t start, unw_word_t end)
 {
-    struct unw_debug_frame_list *w, *fdesc = 0;
-    char path[PATH_MAX];
-    char *name = path;
-    int err;
-    char *buf;
-    size_t bufsize;
+  struct unw_debug_frame_list *w, *fdesc = 0;
+  char path[PATH_MAX];
+  char *name = path;
+  int err;
+  char *buf;
+  size_t bufsize;
 
-    /* First, see if we loaded this frame already.  */
+  /* First, see if we loaded this frame already.  */
 
-    for (w = as->debug_frames; w; w = w->next)
+  for (w = as->debug_frames; w; w = w->next)
     {
-        Debug (4, "checking %p: %lx-%lx\n", w, (long)w->start, (long)w->end);
-        if (addr >= w->start && addr < w->end)
-            return w;
+      Debug (4, "checking %p: %lx-%lx\n", w, (long)w->start, (long)w->end);
+      if (addr >= w->start && addr < w->end)
+        return w;
     }
 
-    /* If the object name we receive is blank, there's still a chance of locating
-       the file by parsing /proc/self/maps.  */
+  /* If the object name we receive is blank, there's still a chance of locating
+     the file by parsing /proc/self/maps.  */
 
-    if (strcmp (dlname, "") == 0)
+  if (strcmp (dlname, "") == 0)
     {
-        err = find_binary_for_address (addr, name, sizeof(path));
-        if (err)
+      err = find_binary_for_address (addr, name, sizeof(path));
+      if (err)
         {
-            Debug (15, "tried to locate binary for 0x%" PRIx64 ", but no luck\n",
-                   (uint64_t) addr);
-            return 0;
+          Debug (15, "tried to locate binary for 0x%" PRIx64 ", but no luck\n",
+                 (uint64_t) addr);
+          return 0;
         }
     }
-    else
-        name = (char*) dlname;
+  else
+    name = (char*) dlname;
 
-    err = load_debug_frame (name, &buf, &bufsize, as == unw_local_addr_space);
+  err = load_debug_frame (name, &buf, &bufsize, as == unw_local_addr_space);
 
-    if (!err)
+  if (!err)
     {
-        fdesc = malloc (sizeof (struct unw_debug_frame_list));
+      fdesc = malloc (sizeof (struct unw_debug_frame_list));
 
-        fdesc->start = start;
-        fdesc->end = end;
-        fdesc->debug_frame = buf;
-        fdesc->debug_frame_size = bufsize;
-        fdesc->index = NULL;
-        fdesc->next = as->debug_frames;
+      fdesc->start = start;
+      fdesc->end = end;
+      fdesc->debug_frame = buf;
+      fdesc->debug_frame_size = bufsize;
+      fdesc->index = NULL;
+      fdesc->next = as->debug_frames;
 
-        as->debug_frames = fdesc;
+      as->debug_frames = fdesc;
     }
 
-    return fdesc;
+  return fdesc;
 }
 
 struct debug_frame_tab
-{
+  {
     struct table_entry *tab;
     uint32_t length;
     uint32_t size;
-};
-
-static struct debug_frame_tab *
-debug_frame_tab_new (unsigned int base_size)
-{
-    struct debug_frame_tab *tab = malloc (sizeof (struct debug_frame_tab));
-
-    tab->tab = calloc (base_size, sizeof (struct table_entry));
-    tab->length = 0;
-    tab->size = base_size;
-
-    return tab;
-}
+  };
 
 static void
 debug_frame_tab_append (struct debug_frame_tab *tab,
                         unw_word_t fde_offset, unw_word_t start_ip)
 {
-    unsigned int length = tab->length;
+  unsigned int length = tab->length;
 
-    if (length == tab->size)
+  if (length == tab->size)
     {
-        tab->size *= 2;
-        tab->tab = realloc (tab->tab, sizeof (struct table_entry) * tab->size);
+      tab->size *= 2;
+      tab->tab = realloc (tab->tab, sizeof (struct table_entry) * tab->size);
     }
 
-    tab->tab[length].fde_offset = fde_offset;
-    tab->tab[length].start_ip_offset = start_ip;
+  tab->tab[length].fde_offset = fde_offset;
+  tab->tab[length].start_ip_offset = start_ip;
 
-    tab->length = length + 1;
+  tab->length = length + 1;
 }
 
 static void
 debug_frame_tab_shrink (struct debug_frame_tab *tab)
 {
-    if (tab->size > tab->length)
+  if (tab->size > tab->length)
     {
-        tab->tab = realloc (tab->tab, sizeof (struct table_entry) * tab->length);
-        tab->size = tab->length;
+      tab->tab = realloc (tab->tab, sizeof (struct table_entry) * tab->length);
+      tab->size = tab->length;
     }
 }
 
 static int
 debug_frame_tab_compare (const void *a, const void *b)
 {
-    const struct table_entry *fa = a, *fb = b;
+  const struct table_entry *fa = a, *fb = b;
 
-    if (fa->start_ip_offset > fb->start_ip_offset)
-        return 1;
-    else if (fa->start_ip_offset < fb->start_ip_offset)
-        return -1;
-    else
-        return 0;
+  if (fa->start_ip_offset > fb->start_ip_offset)
+    return 1;
+  else if (fa->start_ip_offset < fb->start_ip_offset)
+    return -1;
+  else
+    return 0;
 }
 
-PROTECTED int
+int
 dwarf_find_debug_frame (int found, unw_dyn_info_t *di_debug, unw_word_t ip,
                         unw_word_t segbase, const char* obj_name,
                         unw_word_t start, unw_word_t end)
 {
-    unw_dyn_info_t *di;
-    struct unw_debug_frame_list *fdesc = 0;
-    unw_accessors_t *a;
-    unw_word_t addr;
+  unw_dyn_info_t *di;
+  struct unw_debug_frame_list *fdesc = 0;
+  unw_accessors_t *a;
+  unw_word_t addr;
 
-    Debug (15, "Trying to find .debug_frame for %s\n", obj_name);
-    di = di_debug;
+  Debug (15, "Trying to find .debug_frame for %s\n", obj_name);
+  di = di_debug;
 
-    fdesc = locate_debug_info (unw_local_addr_space, ip, obj_name, start, end);
+  fdesc = locate_debug_info (unw_local_addr_space, ip, obj_name, start, end);
 
-    if (!fdesc)
+  if (!fdesc)
     {
-        Debug (15, "couldn't load .debug_frame\n");
-        return found;
+      Debug (15, "couldn't load .debug_frame\n");
+      return found;
     }
-    else
+  else
     {
-        char *buf;
-        size_t bufsize;
-        unw_word_t item_start, item_end = 0;
-        uint32_t u32val = 0;
-        uint64_t cie_id = 0;
-        struct debug_frame_tab *tab;
+      char *buf;
+      size_t bufsize;
+      unw_word_t item_start, item_end = 0;
+      uint32_t u32val = 0;
+      uint64_t cie_id = 0;
+      struct debug_frame_tab tab;
 
-        Debug (15, "loaded .debug_frame\n");
+      Debug (15, "loaded .debug_frame\n");
 
-        buf = fdesc->debug_frame;
-        bufsize = fdesc->debug_frame_size;
+      buf = fdesc->debug_frame;
+      bufsize = fdesc->debug_frame_size;
 
-        if (bufsize == 0)
-        {
-            Debug (15, "zero-length .debug_frame\n");
-            return found;
-        }
+      if (bufsize == 0)
+       {
+         Debug (15, "zero-length .debug_frame\n");
+         return found;
+       }
 
-        /* Now create a binary-search table, if it does not already exist.  */
-        if (!fdesc->index)
-        {
-            addr = (unw_word_t) (uintptr_t) buf;
+      /* Now create a binary-search table, if it does not already exist.  */
+      if (!fdesc->index)
+       {
+         addr = (unw_word_t) (uintptr_t) buf;
 
-            a = unw_get_accessors (unw_local_addr_space);
+         a = unw_get_accessors_int (unw_local_addr_space);
 
-            /* Find all FDE entries in debug_frame, and make into a sorted
-               index.  */
+         /* Find all FDE entries in debug_frame, and make into a sorted
+            index.  */
 
-            tab = debug_frame_tab_new (16);
+         tab.length = 0;
+         tab.size = 16;
+         tab.tab = calloc (tab.size, sizeof (struct table_entry));
 
-            while (addr < (unw_word_t) (uintptr_t) (buf + bufsize))
-            {
-                uint64_t id_for_cie;
-                item_start = addr;
+         while (addr < (unw_word_t) (uintptr_t) (buf + bufsize))
+           {
+             uint64_t id_for_cie;
+             item_start = addr;
 
-                dwarf_readu32 (unw_local_addr_space, a, &addr, &u32val, NULL);
+             dwarf_readu32 (unw_local_addr_space, a, &addr, &u32val, NULL);
 
-                if (u32val == 0)
-                    break;
-                else if (u32val != 0xffffffff)
-                {
-                    uint32_t cie_id32 = 0;
-                    item_end = addr + u32val;
-                    dwarf_readu32 (unw_local_addr_space, a, &addr, &cie_id32,
-                                   NULL);
-                    cie_id = cie_id32;
-                    id_for_cie = 0xffffffff;
-                }
-                else
-                {
-                    uint64_t u64val = 0;
-                    /* Extended length.  */
-                    dwarf_readu64 (unw_local_addr_space, a, &addr, &u64val, NULL);
-                    item_end = addr + u64val;
-
-                    dwarf_readu64 (unw_local_addr_space, a, &addr, &cie_id, NULL);
-                    id_for_cie = 0xffffffffffffffffull;
-                }
-
-                /*Debug (1, "CIE/FDE id = %.8x\n", (int) cie_id);*/
-
-                if (cie_id == id_for_cie)
-                    ;
-                /*Debug (1, "Found CIE at %.8x.\n", item_start);*/
-                else
-                {
-                    unw_word_t fde_addr = item_start;
-                    unw_proc_info_t this_pi;
-                    int err;
-
-                    /*Debug (1, "Found FDE at %.8x\n", item_start);*/
-
-                    err = dwarf_extract_proc_info_from_fde (unw_local_addr_space,
-                                                            a, &fde_addr,
-                                                            &this_pi, 0,
-                                                            (uintptr_t) buf,
-                                                            NULL);
-                    if (err == 0)
-                    {
-                        Debug (15, "start_ip = %lx, end_ip = %lx\n",
-                               (long) this_pi.start_ip, (long) this_pi.end_ip);
-                        debug_frame_tab_append (tab,
-                                                item_start - (unw_word_t) (uintptr_t) buf,
-                                                this_pi.start_ip);
-                    }
-                    /*else
-                      Debug (1, "FDE parse failed\n");*/
-                }
-
-                addr = item_end;
-            }
-
-            debug_frame_tab_shrink (tab);
-            qsort (tab->tab, tab->length, sizeof (struct table_entry),
-                   debug_frame_tab_compare);
-            /* for (i = 0; i < tab->length; i++)
+             if (u32val == 0)
+               break;
+             else if (u32val != 0xffffffff)
                {
-               fprintf (stderr, "ip %x, fde offset %x\n",
-               (int) tab->tab[i].start_ip_offset,
-               (int) tab->tab[i].fde_offset);
-               }*/
-            fdesc->index = tab->tab;
-            fdesc->index_size = tab->length;
-            free (tab);
-        }
+                 uint32_t cie_id32 = 0;
+                 item_end = addr + u32val;
+                 dwarf_readu32 (unw_local_addr_space, a, &addr, &cie_id32,
+                                NULL);
+                 cie_id = cie_id32;
+                 id_for_cie = 0xffffffff;
+               }
+             else
+               {
+                 uint64_t u64val = 0;
+                 /* Extended length.  */
+                 dwarf_readu64 (unw_local_addr_space, a, &addr, &u64val, NULL);
+                 item_end = addr + u64val;
 
-        di->format = UNW_INFO_FORMAT_TABLE;
-        di->start_ip = fdesc->start;
-        di->end_ip = fdesc->end;
-        di->u.ti.name_ptr = (unw_word_t) (uintptr_t) obj_name;
-        di->u.ti.table_data = (unw_word_t *) fdesc;
-        di->u.ti.table_len = sizeof (*fdesc) / sizeof (unw_word_t);
-        di->u.ti.segbase = segbase;
+                 dwarf_readu64 (unw_local_addr_space, a, &addr, &cie_id, NULL);
+                 id_for_cie = 0xffffffffffffffffull;
+               }
 
-        found = 1;
-        Debug (15, "found debug_frame table `%s': segbase=0x%lx, len=%lu, "
-               "gp=0x%lx, table_data=0x%lx\n",
-               (char *) (uintptr_t) di->u.ti.name_ptr,
-               (long) di->u.ti.segbase, (long) di->u.ti.table_len,
-               (long) di->gp, (long) di->u.ti.table_data);
+             /*Debug (1, "CIE/FDE id = %.8x\n", (int) cie_id);*/
+
+             if (cie_id == id_for_cie)
+               ;
+             /*Debug (1, "Found CIE at %.8x.\n", item_start);*/
+             else
+               {
+                 unw_word_t fde_addr = item_start;
+                 unw_proc_info_t this_pi;
+                 int err;
+
+                 /*Debug (1, "Found FDE at %.8x\n", item_start);*/
+
+                 err = dwarf_extract_proc_info_from_fde (unw_local_addr_space,
+                                                         a, &fde_addr,
+                                                         &this_pi,
+                                                         (uintptr_t) buf, 0, 1,
+                                                         NULL);
+                 if (err == 0)
+                   {
+                     Debug (15, "start_ip = %lx, end_ip = %lx\n",
+                            (long) this_pi.start_ip, (long) this_pi.end_ip);
+                     debug_frame_tab_append (&tab,
+                                             item_start - (unw_word_t) (uintptr_t) buf,
+                                             this_pi.start_ip);
+                   }
+                 /*else
+                   Debug (1, "FDE parse failed\n");*/
+               }
+
+             addr = item_end;
+           }
+
+         debug_frame_tab_shrink (&tab);
+         qsort (tab.tab, tab.length, sizeof (struct table_entry),
+                debug_frame_tab_compare);
+         /* for (i = 0; i < tab.length; i++)
+            {
+            fprintf (stderr, "ip %x, fde offset %x\n",
+            (int) tab.tab[i].start_ip_offset,
+            (int) tab.tab[i].fde_offset);
+            }*/
+         fdesc->index = tab.tab;
+         fdesc->index_size = tab.length;
+       }
+
+      di->format = UNW_INFO_FORMAT_TABLE;
+      di->start_ip = fdesc->start;
+      di->end_ip = fdesc->end;
+      di->u.ti.name_ptr = (unw_word_t) (uintptr_t) obj_name;
+      di->u.ti.table_data = (unw_word_t *) fdesc;
+      di->u.ti.table_len = sizeof (*fdesc) / sizeof (unw_word_t);
+      di->u.ti.segbase = segbase;
+
+      found = 1;
+      Debug (15, "found debug_frame table `%s': segbase=0x%lx, len=%lu, "
+            "gp=0x%lx, table_data=0x%lx\n",
+            (char *) (uintptr_t) di->u.ti.name_ptr,
+            (long) di->u.ti.segbase, (long) di->u.ti.table_len,
+            (long) di->gp, (long) di->u.ti.table_data);
     }
-    return found;
+  return found;
 }
 
 #endif /* CONFIG_DEBUG_FRAME */
 
 #ifndef UNW_REMOTE_ONLY
 
+#ifndef HAVE_SGX
+static Elf_W (Addr)
+dwarf_find_eh_frame_section(struct dl_phdr_info *info)
+{
+  int rc;
+  struct elf_image ei;
+  Elf_W (Addr) eh_frame = 0;
+  Elf_W (Shdr)* shdr;
+  const char *file = info->dlpi_name;
+  char exepath[PATH_MAX];
+
+  if (strlen(file) == 0)
+    {
+      tdep_get_exe_image_path(exepath);
+      file = exepath;
+    }
+
+  Debug (1, "looking for .eh_frame section in %s\n",
+         file);
+
+  rc = elf_map_image (&ei, file);
+  if (rc != 0)
+    return 0;
+
+  shdr = elf_w (find_section) (&ei, ".eh_frame");
+  if (!shdr)
+    goto out;
+
+  eh_frame = shdr->sh_addr + info->dlpi_addr;
+  Debug (4, "found .eh_frame at address %lx\n",
+         eh_frame);
+
+out:
+  FREE_MEMORY (ei.image, ei.size);
+
+  return eh_frame;
+}
+#endif
+
+struct dwarf_callback_data
+  {
+    /* in: */
+    unw_word_t ip;              /* instruction-pointer we're looking for */
+    unw_proc_info_t *pi;        /* proc-info pointer */
+    int need_unwind_info;
+    /* out: */
+    int single_fde;             /* did we find a single FDE? (vs. a table) */
+    unw_dyn_info_t di;          /* table info (if single_fde is false) */
+    unw_dyn_info_t di_debug;    /* additional table info for .debug_frame */
+  };
+
 /* ptr is a pointer to a dwarf_callback_data structure and, on entry,
    member ip contains the instruction-pointer we're looking
    for.  */
 HIDDEN int
 dwarf_callback (struct dl_phdr_info *info, size_t size, void *ptr)
 {
-    struct dwarf_callback_data *cb_data = ptr;
-    unw_dyn_info_t *di = &cb_data->di;
-    const Elf_W(Phdr) *phdr, *p_eh_hdr, *p_dynamic, *p_text;
-    unw_word_t addr, eh_frame_start, eh_frame_end, fde_count, ip;
-    Elf_W(Addr) load_base, max_load_addr = 0;
-    int ret, need_unwind_info = cb_data->need_unwind_info;
-    unw_proc_info_t *pi = cb_data->pi;
-    struct dwarf_eh_frame_hdr *hdr;
-    unw_accessors_t *a;
-    long n;
-    int found = 0;
+  struct dwarf_callback_data *cb_data = ptr;
+  unw_dyn_info_t *di = &cb_data->di;
+  const Elf_W(Phdr) *phdr, *p_eh_hdr, *p_dynamic, *p_text;
+  unw_word_t addr, eh_frame_start, eh_frame_end, fde_count, ip;
+  Elf_W(Addr) load_base, max_load_addr = 0;
+  int ret, need_unwind_info = cb_data->need_unwind_info;
+  unw_proc_info_t *pi = cb_data->pi;
+  struct dwarf_eh_frame_hdr *hdr = NULL;
+  unw_accessors_t *a;
+  long n;
+  int found = 0;
+  struct dwarf_eh_frame_hdr synth_eh_frame_hdr;
 #ifdef CONFIG_DEBUG_FRAME
-    unw_word_t start, end;
+  unw_word_t start, end;
 #endif /* CONFIG_DEBUG_FRAME*/
 
-    ip = cb_data->ip;
+  ip = cb_data->ip;
 
-    /* Make sure struct dl_phdr_info is at least as big as we need.  */
-    if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
-            + sizeof (info->dlpi_phnum))
-        return -1;
+  /* Make sure struct dl_phdr_info is at least as big as we need.  */
+  if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
+             + sizeof (info->dlpi_phnum))
+    return -1;
 
-    Debug (15, "checking %s, base=0x%lx)\n",
-           info->dlpi_name, (long) info->dlpi_addr);
+  Debug (15, "checking %s, base=0x%lx)\n",
+         info->dlpi_name, (long) info->dlpi_addr);
 
-    phdr = info->dlpi_phdr;
-    load_base = info->dlpi_addr;
-    p_text = NULL;
-    p_eh_hdr = NULL;
-    p_dynamic = NULL;
+  phdr = info->dlpi_phdr;
+  load_base = info->dlpi_addr;
+  p_text = NULL;
+  p_eh_hdr = NULL;
+  p_dynamic = NULL;
 
-    /* See if PC falls into one of the loaded segments.  Find the
-       eh-header segment at the same time.  */
-    for (n = info->dlpi_phnum; --n >= 0; phdr++)
+  /* See if PC falls into one of the loaded segments.  Find the
+     eh-header segment at the same time.  */
+  for (n = info->dlpi_phnum; --n >= 0; phdr++)
     {
-        if (phdr->p_type == PT_LOAD)
+      if (phdr->p_type == PT_LOAD)
         {
-            Elf_W(Addr) vaddr = phdr->p_vaddr + load_base;
+          Elf_W(Addr) vaddr = phdr->p_vaddr + load_base;
 
-            if (ip >= vaddr && ip < vaddr + phdr->p_memsz)
-                p_text = phdr;
+          if (ip >= vaddr && ip < vaddr + phdr->p_memsz)
+            p_text = phdr;
 
-            if (vaddr + phdr->p_filesz > max_load_addr)
-                max_load_addr = vaddr + phdr->p_filesz;
+          if (vaddr + phdr->p_filesz > max_load_addr)
+            max_load_addr = vaddr + phdr->p_filesz;
         }
-        else if (phdr->p_type == PT_GNU_EH_FRAME)
-            p_eh_hdr = phdr;
-        else if (phdr->p_type == PT_DYNAMIC)
-            p_dynamic = phdr;
+      else if (phdr->p_type == PT_GNU_EH_FRAME)
+        p_eh_hdr = phdr;
+      else if (phdr->p_type == PT_DYNAMIC)
+        p_dynamic = phdr;
     }
 
-    if (!p_text)
-        return 0;
+  if (!p_text)
+    return 0;
 
-    if (p_eh_hdr)
+  if (p_eh_hdr)
     {
-        if (p_dynamic)
-        {
-            /* For dynamicly linked executables and shared libraries,
-               DT_PLTGOT is the value that data-relative addresses are
-               relative to for that object.  We call this the "gp".  */
-            Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(p_dynamic->p_vaddr + load_base);
-            for (; dyn->d_tag != DT_NULL; ++dyn)
-                if (dyn->d_tag == DT_PLTGOT)
-                {
-                    /* Assume that _DYNAMIC is writable and GLIBC has
-                       relocated it (true for x86 at least).  */
-                    di->gp = dyn->d_un.d_ptr;
-                    break;
-                }
-        }
-        else
-            /* Otherwise this is a static executable with no _DYNAMIC.  Assume
-               that data-relative addresses are relative to 0, i.e.,
-               absolute.  */
-            di->gp = 0;
-        pi->gp = di->gp;
+      hdr = (struct dwarf_eh_frame_hdr *) (p_eh_hdr->p_vaddr + load_base);
+    }
+  else
+    {
+#if HAVE_SGX
+      //SGX Enclave.so will always include the explicit '.eh_frame_hdr' section.
+      //So this function won't be called inside SGX.
+      abort();
+      return -1;
+#else
 
-        hdr = (struct dwarf_eh_frame_hdr *) (p_eh_hdr->p_vaddr + load_base);
-        if (hdr->version != DW_EH_VERSION)
+      Elf_W (Addr) eh_frame;
+      Debug (1, "no .eh_frame_hdr section found\n");
+      eh_frame = dwarf_find_eh_frame_section (info);
+      if (eh_frame)
         {
-            Debug (1, "table `%s' has unexpected version %d\n",
-                   info->dlpi_name, hdr->version);
-            return 0;
+          Debug (1, "using synthetic .eh_frame_hdr section for %s\n",
+                 info->dlpi_name);
+	  synth_eh_frame_hdr.version = DW_EH_VERSION;
+	  synth_eh_frame_hdr.eh_frame_ptr_enc = DW_EH_PE_absptr |
+	    ((sizeof(Elf_W (Addr)) == 4) ? DW_EH_PE_udata4 : DW_EH_PE_udata8);
+          synth_eh_frame_hdr.fde_count_enc = DW_EH_PE_omit;
+          synth_eh_frame_hdr.table_enc = DW_EH_PE_omit;
+	  synth_eh_frame_hdr.eh_frame = eh_frame;
+          hdr = &synth_eh_frame_hdr;
+        }
+#endif
+    }
+
+  if (hdr)
+    {
+      if (p_dynamic)
+        {
+          /* For dynamicly linked executables and shared libraries,
+             DT_PLTGOT is the value that data-relative addresses are
+             relative to for that object.  We call this the "gp".  */
+          Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(p_dynamic->p_vaddr + load_base);
+          for (; dyn->d_tag != DT_NULL; ++dyn)
+            if (dyn->d_tag == DT_PLTGOT)
+              {
+                /* Assume that _DYNAMIC is writable and GLIBC has
+                   relocated it (true for x86 at least).  */
+                di->gp = dyn->d_un.d_ptr;
+                break;
+              }
+        }
+      else
+        /* Otherwise this is a static executable with no _DYNAMIC.  Assume
+           that data-relative addresses are relative to 0, i.e.,
+           absolute.  */
+        di->gp = 0;
+      pi->gp = di->gp;
+
+      if (hdr->version != DW_EH_VERSION)
+        {
+          Debug (1, "table `%s' has unexpected version %d\n",
+                 info->dlpi_name, hdr->version);
+          return 0;
         }
 
-        a = unw_get_accessors (unw_local_addr_space);
-        addr = (unw_word_t) (uintptr_t) (hdr + 1);
+      a = unw_get_accessors_int (unw_local_addr_space);
+      addr = (unw_word_t) (uintptr_t) (&hdr->eh_frame);
 
-        /* (Optionally) read eh_frame_ptr: */
-        if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
-                                               &addr, hdr->eh_frame_ptr_enc, pi,
-                                               &eh_frame_start, NULL)) < 0)
-            return ret;
+      /* (Optionally) read eh_frame_ptr: */
+      if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
+                                             &addr, hdr->eh_frame_ptr_enc, pi,
+                                             &eh_frame_start, NULL)) < 0)
+        return ret;
 
-        /* (Optionally) read fde_count: */
-        if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
-                                               &addr, hdr->fde_count_enc, pi,
-                                               &fde_count, NULL)) < 0)
-            return ret;
+      /* (Optionally) read fde_count: */
+      if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
+                                             &addr, hdr->fde_count_enc, pi,
+                                             &fde_count, NULL)) < 0)
+        return ret;
 
-        if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
+      if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
         {
-            /* If there is no search table or it has an unsupported
-               encoding, fall back on linear search.  */
-            if (hdr->table_enc == DW_EH_PE_omit)
-                Debug (4, "table `%s' lacks search table; doing linear search\n",
-                       info->dlpi_name);
-            else
-                Debug (4, "table `%s' has encoding 0x%x; doing linear search\n",
-                       info->dlpi_name, hdr->table_enc);
+          /* If there is no search table or it has an unsupported
+             encoding, fall back on linear search.  */
+          if (hdr->table_enc == DW_EH_PE_omit)
+            Debug (4, "table `%s' lacks search table; doing linear search\n",
+                   info->dlpi_name);
+          else
+            Debug (4, "table `%s' has encoding 0x%x; doing linear search\n",
+                   info->dlpi_name, hdr->table_enc);
 
-            eh_frame_end = max_load_addr;	/* XXX can we do better? */
+          eh_frame_end = max_load_addr; /* XXX can we do better? */
 
-            if (hdr->fde_count_enc == DW_EH_PE_omit)
-                fde_count = ~0UL;
-            if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
-                abort ();
+          if (hdr->fde_count_enc == DW_EH_PE_omit)
+            fde_count = ~0UL;
+          if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
+            abort ();
 
-            /* XXX we know how to build a local binary search table for
-               .debug_frame, so we could do that here too.  */
-            cb_data->single_fde = 1;
-            found = linear_search (unw_local_addr_space, ip,
-                                   eh_frame_start, eh_frame_end, fde_count,
-                                   pi, need_unwind_info, NULL);
-            if (found != 1)
-                found = 0;
+          Debug (1, "eh_frame_start = %lx eh_frame_end = %lx\n",
+                 eh_frame_start, eh_frame_end);
+
+          /* XXX we know how to build a local binary search table for
+             .debug_frame, so we could do that here too.  */
+          found = linear_search (unw_local_addr_space, ip,
+                                 eh_frame_start, eh_frame_end, fde_count,
+                                 pi, need_unwind_info, NULL);
+          if (found != 1)
+            found = 0;
+	  else
+	    cb_data->single_fde = 1;
         }
-        else
+      else
         {
-            di->format = UNW_INFO_FORMAT_REMOTE_TABLE;
-            di->start_ip = p_text->p_vaddr + load_base;
-            di->end_ip = p_text->p_vaddr + load_base + p_text->p_memsz;
-            di->u.rti.name_ptr = (unw_word_t) (uintptr_t) info->dlpi_name;
-            di->u.rti.table_data = addr;
-            assert (sizeof (struct table_entry) % sizeof (unw_word_t) == 0);
-            di->u.rti.table_len = (fde_count * sizeof (struct table_entry)
-                                   / sizeof (unw_word_t));
-            /* For the binary-search table in the eh_frame_hdr, data-relative
-               means relative to the start of that section... */
-            di->u.rti.segbase = (unw_word_t) (uintptr_t) hdr;
+          di->format = UNW_INFO_FORMAT_REMOTE_TABLE;
+          di->start_ip = p_text->p_vaddr + load_base;
+          di->end_ip = p_text->p_vaddr + load_base + p_text->p_memsz;
+          di->u.rti.name_ptr = (unw_word_t) (uintptr_t) info->dlpi_name;
+          di->u.rti.table_data = addr;
+          assert (sizeof (struct table_entry) % sizeof (unw_word_t) == 0);
+          di->u.rti.table_len = (fde_count * sizeof (struct table_entry)
+                                 / sizeof (unw_word_t));
+          /* For the binary-search table in the eh_frame_hdr, data-relative
+             means relative to the start of that section... */
+          di->u.rti.segbase = (unw_word_t) (uintptr_t) hdr;
 
-            found = 1;
-            Debug (15, "found table `%s': segbase=0x%lx, len=%lu, gp=0x%lx, "
-                   "table_data=0x%lx\n", (char *) (uintptr_t) di->u.rti.name_ptr,
-                   (long) di->u.rti.segbase, (long) di->u.rti.table_len,
-                   (long) di->gp, (long) di->u.rti.table_data);
+          found = 1;
+          Debug (15, "found table `%s': segbase=0x%lx, len=%lu, gp=0x%lx, "
+                 "table_data=0x%lx\n", (char *) (uintptr_t) di->u.rti.name_ptr,
+                 (long) di->u.rti.segbase, (long) di->u.rti.table_len,
+                 (long) di->gp, (long) di->u.rti.table_data);
         }
     }
 
 #ifdef CONFIG_DEBUG_FRAME
-    /* Find the start/end of the described region by parsing the phdr_info
-       structure.  */
-    start = (unw_word_t) -1;
-    end = 0;
+  /* Find the start/end of the described region by parsing the phdr_info
+     structure.  */
+  start = (unw_word_t) -1;
+  end = 0;
 
-    for (n = 0; n < info->dlpi_phnum; n++)
+  for (n = 0; n < info->dlpi_phnum; n++)
     {
-        if (info->dlpi_phdr[n].p_type == PT_LOAD)
+      if (info->dlpi_phdr[n].p_type == PT_LOAD)
         {
-            unw_word_t seg_start = info->dlpi_addr + info->dlpi_phdr[n].p_vaddr;
-            unw_word_t seg_end = seg_start + info->dlpi_phdr[n].p_memsz;
+          unw_word_t seg_start = info->dlpi_addr + info->dlpi_phdr[n].p_vaddr;
+          unw_word_t seg_end = seg_start + info->dlpi_phdr[n].p_memsz;
 
-            if (seg_start < start)
-                start = seg_start;
+          if (seg_start < start)
+            start = seg_start;
 
-            if (seg_end > end)
-                end = seg_end;
+          if (seg_end > end)
+            end = seg_end;
         }
     }
 
-    found = dwarf_find_debug_frame (found, &cb_data->di_debug, ip,
-                                    info->dlpi_addr, info->dlpi_name, start,
-                                    end);
+  found = dwarf_find_debug_frame (found, &cb_data->di_debug, ip,
+                                  info->dlpi_addr, info->dlpi_name, start,
+                                  end);
 #endif  /* CONFIG_DEBUG_FRAME */
 
-    return found;
+  return found;
 }
 
 HIDDEN int
 dwarf_find_proc_info (unw_addr_space_t as, unw_word_t ip,
                       unw_proc_info_t *pi, int need_unwind_info, void *arg)
 {
-    struct dwarf_callback_data cb_data;
-    intrmask_t saved_mask;
-    int ret;
+  struct dwarf_callback_data cb_data;
+  intrmask_t saved_mask;
+  int ret;
 
-    Debug (14, "looking for IP=0x%lx\n", (long) ip);
+  Debug (14, "looking for IP=0x%lx\n", (long) ip);
 
-    memset (&cb_data, 0, sizeof (cb_data));
-    cb_data.ip = ip;
-    cb_data.pi = pi;
-    cb_data.need_unwind_info = need_unwind_info;
-    cb_data.di.format = -1;
-    cb_data.di_debug.format = -1;
+  memset (&cb_data, 0, sizeof (cb_data));
+  cb_data.ip = ip;
+  cb_data.pi = pi;
+  cb_data.need_unwind_info = need_unwind_info;
+  cb_data.di.format = -1;
+  cb_data.di_debug.format = -1;
 
-    SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
-    ret = dl_iterate_phdr (dwarf_callback, &cb_data);
-    SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
+  SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &saved_mask);
+  ret = dl_iterate_phdr (dwarf_callback, &cb_data);
+  SIGPROCMASK (SIG_SETMASK, &saved_mask, NULL);
 
-    if (ret <= 0)
+  if (ret > 0)
     {
-        Debug (14, "IP=0x%lx not found\n", (long) ip);
-        return -UNW_ENOINFO;
+      if (cb_data.single_fde)
+	/* already got the result in *pi */
+	return 0;
+
+      /* search the table: */
+      if (cb_data.di.format != -1)
+	ret = dwarf_search_unwind_table_int (as, ip, &cb_data.di,
+					     pi, need_unwind_info, arg);
+      else
+	ret = -UNW_ENOINFO;
+
+      if (ret == -UNW_ENOINFO && cb_data.di_debug.format != -1)
+	ret = dwarf_search_unwind_table_int (as, ip, &cb_data.di_debug, pi,
+					     need_unwind_info, arg);
     }
+  else
+    ret = -UNW_ENOINFO;
 
-    if (cb_data.single_fde)
-        /* already got the result in *pi */
-        return 0;
-
-    /* search the table: */
-    if (cb_data.di.format != -1)
-        ret = dwarf_search_unwind_table (as, ip, &cb_data.di,
-                                         pi, need_unwind_info, arg);
-    else
-        ret = -UNW_ENOINFO;
-
-    if (ret == -UNW_ENOINFO && cb_data.di_debug.format != -1)
-        ret = dwarf_search_unwind_table (as, ip, &cb_data.di_debug, pi,
-                                         need_unwind_info, arg);
-    return ret;
+  return ret;
 }
 
 static inline const struct table_entry *
 lookup (const struct table_entry *table, size_t table_size, int32_t rel_ip)
 {
-    unsigned long table_len = table_size / sizeof (struct table_entry);
-    const struct table_entry *e = 0;
-    unsigned long lo, hi, mid;
+  unsigned long table_len = table_size / sizeof (struct table_entry);
+  const struct table_entry *e = NULL;
+  unsigned long lo, hi, mid;
 
-    /* do a binary search for right entry: */
-    for (lo = 0, hi = table_len; lo < hi;)
+  /* do a binary search for right entry: */
+  for (lo = 0, hi = table_len; lo < hi;)
     {
-        mid = (lo + hi) / 2;
-        e = table + mid;
-        Debug (15, "e->start_ip_offset = %lx\n", (long) e->start_ip_offset);
-        if (rel_ip < e->start_ip_offset)
-            hi = mid;
-        else
-            lo = mid + 1;
+      mid = (lo + hi) / 2;
+      e = table + mid;
+      Debug (15, "e->start_ip_offset = %lx\n", (long) e->start_ip_offset);
+      if (rel_ip < e->start_ip_offset)
+        hi = mid;
+      else
+        lo = mid + 1;
     }
-    if (hi <= 0)
+  if (hi <= 0)
         return NULL;
-    e = table + hi - 1;
-    return e;
+  e = table + hi - 1;
+  return e;
 }
 
 #endif /* !UNW_REMOTE_ONLY */
@@ -798,145 +776,175 @@
 static int
 remote_lookup (unw_addr_space_t as,
                unw_word_t table, size_t table_size, int32_t rel_ip,
-               struct table_entry *e, void *arg)
+               struct table_entry *e, int32_t *last_ip_offset, void *arg)
 {
-    unsigned long table_len = table_size / sizeof (struct table_entry);
-    unw_accessors_t *a = unw_get_accessors (as);
-    unsigned long lo, hi, mid;
-    unw_word_t e_addr = 0;
-    int32_t start;
-    int ret;
+  unsigned long table_len = table_size / sizeof (struct table_entry);
+  unw_accessors_t *a = unw_get_accessors_int (as);
+  unsigned long lo, hi, mid;
+  unw_word_t e_addr = 0;
+  int32_t start = 0;
+  int ret;
 
-    /* do a binary search for right entry: */
-    for (lo = 0, hi = table_len; lo < hi;)
+  /* do a binary search for right entry: */
+  for (lo = 0, hi = table_len; lo < hi;)
     {
-        mid = (lo + hi) / 2;
-        e_addr = table + mid * sizeof (struct table_entry);
-        if ((ret = dwarf_reads32 (as, a, &e_addr, &start, arg)) < 0)
-            return ret;
-
-        if (rel_ip < start)
-            hi = mid;
-        else
-            lo = mid + 1;
-    }
-    if (hi <= 0)
-        return 0;
-    e_addr = table + (hi - 1) * sizeof (struct table_entry);
-    if ((ret = dwarf_reads32 (as, a, &e_addr, &e->start_ip_offset, arg)) < 0
-            || (ret = dwarf_reads32 (as, a, &e_addr, &e->fde_offset, arg)) < 0)
+      mid = (lo + hi) / 2;
+      e_addr = table + mid * sizeof (struct table_entry);
+      if ((ret = dwarf_reads32 (as, a, &e_addr, &start, arg)) < 0)
         return ret;
-    return 1;
+
+      if (rel_ip < start)
+        hi = mid;
+      else
+        lo = mid + 1;
+    }
+  if (hi <= 0)
+    return 0;
+  e_addr = table + (hi - 1) * sizeof (struct table_entry);
+  if ((ret = dwarf_reads32 (as, a, &e_addr, &e->start_ip_offset, arg)) < 0
+   || (ret = dwarf_reads32 (as, a, &e_addr, &e->fde_offset, arg)) < 0
+   || (hi < table_len &&
+       (ret = dwarf_reads32 (as, a, &e_addr, last_ip_offset, arg)) < 0))
+    return ret;
+  return 1;
 }
 
 #endif /* !UNW_LOCAL_ONLY */
 
-PROTECTED int
+static int is_remote_table(int format)
+{
+  return (format == UNW_INFO_FORMAT_REMOTE_TABLE ||
+          format == UNW_INFO_FORMAT_IP_OFFSET);
+}
+
+int
 dwarf_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
                            unw_dyn_info_t *di, unw_proc_info_t *pi,
                            int need_unwind_info, void *arg)
 {
-    const struct table_entry *e = NULL, *table;
-    unw_word_t segbase = 0, fde_addr;
-    unw_accessors_t *a;
+  const struct table_entry *e = NULL, *table;
+  unw_word_t ip_base = 0, segbase = 0, last_ip, fde_addr;
+  unw_accessors_t *a;
 #ifndef UNW_LOCAL_ONLY
-    struct table_entry ent;
+  struct table_entry ent;
 #endif
-    int ret;
-    unw_word_t debug_frame_base;
-    size_t table_len;
+  int ret;
+  unw_word_t debug_frame_base;
+  size_t table_len;
 
 #ifdef UNW_REMOTE_ONLY
-    assert (di->format == UNW_INFO_FORMAT_REMOTE_TABLE);
+  assert (is_remote_table(di->format));
 #else
-    assert (di->format == UNW_INFO_FORMAT_REMOTE_TABLE
-            || di->format == UNW_INFO_FORMAT_TABLE);
+  assert (is_remote_table(di->format)
+          || di->format == UNW_INFO_FORMAT_TABLE);
 #endif
-    assert (ip >= di->start_ip && ip < di->end_ip);
+  assert (ip >= di->start_ip && ip < di->end_ip);
 
-    if (di->format == UNW_INFO_FORMAT_REMOTE_TABLE)
+  if (is_remote_table(di->format))
     {
-        table = (const struct table_entry *) (uintptr_t) di->u.rti.table_data;
-        table_len = di->u.rti.table_len * sizeof (unw_word_t);
-        debug_frame_base = 0;
+      table = (const struct table_entry *) (uintptr_t) di->u.rti.table_data;
+      table_len = di->u.rti.table_len * sizeof (unw_word_t);
+      debug_frame_base = 0;
     }
-    else
+  else
     {
+      assert(di->format == UNW_INFO_FORMAT_TABLE);
 #ifndef UNW_REMOTE_ONLY
-        struct unw_debug_frame_list *fdesc = (void *) di->u.ti.table_data;
+      struct unw_debug_frame_list *fdesc = (void *) di->u.ti.table_data;
 
-        /* UNW_INFO_FORMAT_TABLE (i.e. .debug_frame) is read from local address
-           space.  Both the index and the unwind tables live in local memory, but
-           the address space to check for properties like the address size and
-           endianness is the target one.  */
-        as = unw_local_addr_space;
-        table = fdesc->index;
-        table_len = fdesc->index_size * sizeof (struct table_entry);
-        debug_frame_base = (uintptr_t) fdesc->debug_frame;
+      /* UNW_INFO_FORMAT_TABLE (i.e. .debug_frame) is read from local address
+         space.  Both the index and the unwind tables live in local memory, but
+         the address space to check for properties like the address size and
+         endianness is the target one.  */
+      as = unw_local_addr_space;
+      table = fdesc->index;
+      table_len = fdesc->index_size * sizeof (struct table_entry);
+      debug_frame_base = (uintptr_t) fdesc->debug_frame;
 #endif
     }
 
-    a = unw_get_accessors (as);
+  a = unw_get_accessors_int (as);
+
+  segbase = di->u.rti.segbase;
+  if (di->format == UNW_INFO_FORMAT_IP_OFFSET) {
+    ip_base = di->start_ip;
+  } else {
+    ip_base = segbase;
+  }
 
 #ifndef UNW_REMOTE_ONLY
-    if (as == unw_local_addr_space)
+  if (as == unw_local_addr_space)
     {
-        segbase = di->u.rti.segbase;
-        e = lookup (table, table_len, ip - segbase);
+      e = lookup (table, table_len, ip - ip_base);
+      if (e && &e[1] < &table[table_len])
+	last_ip = e[1].start_ip_offset + ip_base;
+      else
+	last_ip = di->end_ip;
     }
-    else
+  else
 #endif
     {
 #ifndef UNW_LOCAL_ONLY
-        segbase = di->u.rti.segbase;
-        if ((ret = remote_lookup (as, (uintptr_t) table, table_len,
-                                  ip - segbase, &ent, arg)) < 0)
-            return ret;
-        if (ret)
-            e = &ent;
-        else
-            e = NULL;	/* no info found */
+      int32_t last_ip_offset = di->end_ip - ip_base;
+      segbase = di->u.rti.segbase;
+      if ((ret = remote_lookup (as, (uintptr_t) table, table_len,
+                                ip - ip_base, &ent, &last_ip_offset, arg)) < 0)
+        return ret;
+      if (ret)
+	{
+	  e = &ent;
+	  last_ip = last_ip_offset + ip_base;
+	}
+      else
+        e = NULL;       /* no info found */
 #endif
     }
-    if (!e)
+  if (!e)
     {
-        Debug (1, "IP %lx inside range %lx-%lx, but no explicit unwind info found\n",
-               (long) ip, (long) di->start_ip, (long) di->end_ip);
-        /* IP is inside this table's range, but there is no explicit
-        unwind info.  */
-        return -UNW_ENOINFO;
+      Debug (1, "IP %lx inside range %lx-%lx, but no explicit unwind info found\n",
+             (long) ip, (long) di->start_ip, (long) di->end_ip);
+      /* IP is inside this table's range, but there is no explicit
+         unwind info.  */
+      return -UNW_ENOINFO;
     }
-    Debug (15, "ip=0x%lx, start_ip=0x%lx\n",
-           (long) ip, (long) (e->start_ip_offset));
-    if (debug_frame_base)
-        fde_addr = e->fde_offset + debug_frame_base;
-    else
-        fde_addr = e->fde_offset + segbase;
-    Debug (1, "e->fde_offset = %lx, segbase = %lx, debug_frame_base = %lx, "
-           "fde_addr = %lx\n", (long) e->fde_offset, (long) segbase,
-           (long) debug_frame_base, (long) fde_addr);
-    if ((ret = dwarf_extract_proc_info_from_fde (as, a, &fde_addr, pi,
-               need_unwind_info,
-               debug_frame_base, arg)) < 0)
-        return ret;
+  Debug (15, "ip=0x%lx, start_ip=0x%lx\n",
+         (long) ip, (long) (e->start_ip_offset));
+  if (debug_frame_base)
+    fde_addr = e->fde_offset + debug_frame_base;
+  else
+    fde_addr = e->fde_offset + segbase;
+  Debug (1, "e->fde_offset = %lx, segbase = %lx, debug_frame_base = %lx, "
+            "fde_addr = %lx\n", (long) e->fde_offset, (long) segbase,
+            (long) debug_frame_base, (long) fde_addr);
+  if ((ret = dwarf_extract_proc_info_from_fde (as, a, &fde_addr, pi,
+                                               debug_frame_base ?
+                                               debug_frame_base : segbase,
+                                               need_unwind_info,
+                                               debug_frame_base != 0, arg)) < 0)
+    return ret;
 
-    /* .debug_frame uses an absolute encoding that does not know about any
-       shared library relocation.  */
-    if (di->format == UNW_INFO_FORMAT_TABLE)
+  /* .debug_frame uses an absolute encoding that does not know about any
+     shared library relocation.  */
+  if (di->format == UNW_INFO_FORMAT_TABLE)
     {
-        pi->start_ip += segbase;
-        pi->end_ip += segbase;
-        pi->flags = UNW_PI_FLAG_DEBUG_FRAME;
+      pi->start_ip += segbase;
+      pi->end_ip += segbase;
+      pi->flags = UNW_PI_FLAG_DEBUG_FRAME;
     }
 
-    if (ip < pi->start_ip || ip >= pi->end_ip)
-        return -UNW_ENOINFO;
+#if defined(NEED_LAST_IP)
+  pi->last_ip = last_ip;
+#else
+  (void)last_ip;
+#endif
+  if (ip < pi->start_ip || ip >= pi->end_ip)
+    return -UNW_ENOINFO;
 
-    return 0;
+  return 0;
 }
 
 HIDDEN void
 dwarf_put_unwind_info (unw_addr_space_t as, unw_proc_info_t *pi, void *arg)
 {
-    return;	/* always a nop */
+  return;       /* always a nop */
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Gfind_unwind_table.c b/sgx_unwind/libunwind/src/dwarf/Gfind_unwind_table.c
index 16290a8..6a2ad50 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gfind_unwind_table.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gfind_unwind_table.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -23,7 +23,6 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include <elf.h>
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
@@ -34,195 +33,198 @@
 #include "dwarf-eh.h"
 #include "dwarf_i.h"
 
+#define to_unw_word(p) ((unw_word_t) (uintptr_t) (p))
+
 int
 dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
                          char *path, unw_word_t segbase, unw_word_t mapoff,
                          unw_word_t ip)
 {
-    Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
-    unw_word_t addr, eh_frame_start, fde_count, load_base;
-    unw_word_t max_load_addr = 0;
-    unw_word_t start_ip = (unw_word_t) -1;
-    unw_word_t end_ip = 0;
-    struct dwarf_eh_frame_hdr *hdr;
-    unw_proc_info_t pi;
-    unw_accessors_t *a;
-    Elf_W(Ehdr) *ehdr;
+  Elf_W(Phdr) *phdr, *ptxt = NULL, *peh_hdr = NULL, *pdyn = NULL;
+  unw_word_t addr, eh_frame_start, fde_count, load_base;
+  unw_word_t max_load_addr = 0;
+  unw_word_t start_ip = to_unw_word (-1);
+  unw_word_t end_ip = 0;
+  struct dwarf_eh_frame_hdr *hdr;
+  unw_proc_info_t pi;
+  unw_accessors_t *a;
+  Elf_W(Ehdr) *ehdr;
 #if UNW_TARGET_ARM
-    const Elf_W(Phdr) *parm_exidx = NULL;
+  const Elf_W(Phdr) *parm_exidx = NULL;
 #endif
-    int i, ret, found = 0;
+  int i, ret, found = 0;
 
-    /* XXX: Much of this code is Linux/LSB-specific.  */
+  /* XXX: Much of this code is Linux/LSB-specific.  */
 
-    if (!elf_w(valid_object) (&edi->ei))
-        return -UNW_ENOINFO;
+  if (!elf_w(valid_object) (&edi->ei))
+    return -UNW_ENOINFO;
 
-    ehdr = edi->ei.image;
-    phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
+  ehdr = edi->ei.image;
+  phdr = (Elf_W(Phdr) *) ((char *) edi->ei.image + ehdr->e_phoff);
 
-    for (i = 0; i < ehdr->e_phnum; ++i)
+  for (i = 0; i < ehdr->e_phnum; ++i)
     {
-        switch (phdr[i].p_type)
+      switch (phdr[i].p_type)
         {
         case PT_LOAD:
-            if (phdr[i].p_vaddr < start_ip)
-                start_ip = phdr[i].p_vaddr;
+          if (phdr[i].p_vaddr < start_ip)
+            start_ip = phdr[i].p_vaddr;
 
-            if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
-                end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
+          if (phdr[i].p_vaddr + phdr[i].p_memsz > end_ip)
+            end_ip = phdr[i].p_vaddr + phdr[i].p_memsz;
 
-            if (phdr[i].p_offset == mapoff)
-                ptxt = phdr + i;
-            if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
-                max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
-            break;
+          if (phdr[i].p_offset == mapoff)
+            ptxt = phdr + i;
+          if ((uintptr_t) edi->ei.image + phdr->p_filesz > max_load_addr)
+            max_load_addr = (uintptr_t) edi->ei.image + phdr->p_filesz;
+          break;
 
         case PT_GNU_EH_FRAME:
-            peh_hdr = phdr + i;
-            break;
+          peh_hdr = phdr + i;
+          break;
 
         case PT_DYNAMIC:
-            pdyn = phdr + i;
-            break;
+          pdyn = phdr + i;
+          break;
 
 #if UNW_TARGET_ARM
         case PT_ARM_EXIDX:
-            parm_exidx = phdr + i;
-            break;
+          parm_exidx = phdr + i;
+          break;
 #endif
 
         default:
-            break;
+          break;
         }
     }
 
-    if (!ptxt)
-        return 0;
+  if (!ptxt)
+    return 0;
 
-    load_base = segbase - ptxt->p_vaddr;
-    start_ip += load_base;
-    end_ip += load_base;
+  load_base = segbase - ptxt->p_vaddr;
+  start_ip += load_base;
+  end_ip += load_base;
 
-    if (peh_hdr)
+  if (peh_hdr)
     {
-        if (pdyn)
+      if (pdyn)
         {
-            /* For dynamicly linked executables and shared libraries,
-               DT_PLTGOT is the value that data-relative addresses are
-               relative to for that object.  We call this the "gp".  */
-            Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
-                                             + (char *) edi->ei.image);
-            for (; dyn->d_tag != DT_NULL; ++dyn)
-                if (dyn->d_tag == DT_PLTGOT)
-                {
-                    /* Assume that _DYNAMIC is writable and GLIBC has
-                       relocated it (true for x86 at least).  */
-                    edi->di_cache.gp = dyn->d_un.d_ptr;
-                    break;
-                }
+          /* For dynamicly linked executables and shared libraries,
+             DT_PLTGOT is the value that data-relative addresses are
+             relative to for that object.  We call this the "gp".  */
+                Elf_W(Dyn) *dyn = (Elf_W(Dyn) *)(pdyn->p_offset
+                                                 + (char *) edi->ei.image);
+          for (; dyn->d_tag != DT_NULL; ++dyn)
+            if (dyn->d_tag == DT_PLTGOT)
+              {
+                /* Assume that _DYNAMIC is writable and GLIBC has
+                   relocated it (true for x86 at least).  */
+                edi->di_cache.gp = dyn->d_un.d_ptr;
+                break;
+              }
         }
-        else
-            /* Otherwise this is a static executable with no _DYNAMIC.  Assume
-               that data-relative addresses are relative to 0, i.e.,
-               absolute.  */
-            edi->di_cache.gp = 0;
+      else
+        /* Otherwise this is a static executable with no _DYNAMIC.  Assume
+           that data-relative addresses are relative to 0, i.e.,
+           absolute.  */
+        edi->di_cache.gp = 0;
 
-        hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
-                                             + (char *) edi->ei.image);
-        if (hdr->version != DW_EH_VERSION)
+      hdr = (struct dwarf_eh_frame_hdr *) (peh_hdr->p_offset
+                                           + (char *) edi->ei.image);
+      if (hdr->version != DW_EH_VERSION)
         {
-            Debug (1, "table `%s' has unexpected version %d\n",
-                   path, hdr->version);
-            return -UNW_ENOINFO;
+          Debug (1, "table `%s' has unexpected version %d\n",
+                 path, hdr->version);
+          return -UNW_ENOINFO;
         }
 
-        a = unw_get_accessors (unw_local_addr_space);
-        addr = (unw_word_t) (hdr + 1);
+      a = unw_get_accessors_int (unw_local_addr_space);
+      addr = to_unw_word (&hdr->eh_frame);
 
-        /* Fill in a dummy proc_info structure.  We just need to fill in
-        enough to ensure that dwarf_read_encoded_pointer() can do it's
+      /* Fill in a dummy proc_info structure.  We just need to fill in
+         enough to ensure that dwarf_read_encoded_pointer() can do it's
          job.  Since we don't have a procedure-context at this point, all
          we have to do is fill in the global-pointer.  */
-        memset (&pi, 0, sizeof (pi));
-        pi.gp = edi->di_cache.gp;
+      memset (&pi, 0, sizeof (pi));
+      pi.gp = edi->di_cache.gp;
 
-        /* (Optionally) read eh_frame_ptr: */
-        if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
-                                               &addr, hdr->eh_frame_ptr_enc, &pi,
-                                               &eh_frame_start, NULL)) < 0)
-            return -UNW_ENOINFO;
+      /* (Optionally) read eh_frame_ptr: */
+      if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
+                                             &addr, hdr->eh_frame_ptr_enc, &pi,
+                                             &eh_frame_start, NULL)) < 0)
+        return -UNW_ENOINFO;
 
-        /* (Optionally) read fde_count: */
-        if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
-                                               &addr, hdr->fde_count_enc, &pi,
-                                               &fde_count, NULL)) < 0)
-            return -UNW_ENOINFO;
+      /* (Optionally) read fde_count: */
+      if ((ret = dwarf_read_encoded_pointer (unw_local_addr_space, a,
+                                             &addr, hdr->fde_count_enc, &pi,
+                                             &fde_count, NULL)) < 0)
+        return -UNW_ENOINFO;
 
-        if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
+      if (hdr->table_enc != (DW_EH_PE_datarel | DW_EH_PE_sdata4))
         {
-#if 1
+    #if 1
+          abort ();
+    #else
+          unw_word_t eh_frame_end;
+
+          /* If there is no search table or it has an unsupported
+             encoding, fall back on linear search.  */
+          if (hdr->table_enc == DW_EH_PE_omit)
+            Debug (4, "EH lacks search table; doing linear search\n");
+          else
+            Debug (4, "EH table has encoding 0x%x; doing linear search\n",
+                   hdr->table_enc);
+
+          eh_frame_end = max_load_addr; /* XXX can we do better? */
+
+          if (hdr->fde_count_enc == DW_EH_PE_omit)
+            fde_count = ~0UL;
+          if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
             abort ();
-#else
-            unw_word_t eh_frame_end;
 
-            /* If there is no search table or it has an unsupported
-               encoding, fall back on linear search.  */
-            if (hdr->table_enc == DW_EH_PE_omit)
-                Debug (4, "EH lacks search table; doing linear search\n");
-            else
-                Debug (4, "EH table has encoding 0x%x; doing linear search\n",
-                       hdr->table_enc);
-
-            eh_frame_end = max_load_addr;	/* XXX can we do better? */
-
-            if (hdr->fde_count_enc == DW_EH_PE_omit)
-                fde_count = ~0UL;
-            if (hdr->eh_frame_ptr_enc == DW_EH_PE_omit)
-                abort ();
-
-            return linear_search (unw_local_addr_space, ip,
-                                  eh_frame_start, eh_frame_end, fde_count,
-                                  pi, need_unwind_info, NULL);
-#endif
+          return linear_search (unw_local_addr_space, ip,
+                                eh_frame_start, eh_frame_end, fde_count,
+                                pi, need_unwind_info, NULL);
+    #endif
         }
 
-        edi->di_cache.start_ip = start_ip;
-        edi->di_cache.end_ip = end_ip;
-        edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
-        edi->di_cache.u.rti.name_ptr = 0;
-        /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
-        edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
-        edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
-                                          + (addr - (unw_word_t) edi->ei.image
-                                             - peh_hdr->p_offset));
-
-        /* For the binary-search table in the eh_frame_hdr, data-relative
-        means relative to the start of that section... */
-        edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
-                                       + ((unw_word_t) hdr - (unw_word_t) edi->ei.image
+      edi->di_cache.start_ip = start_ip;
+      edi->di_cache.end_ip = end_ip;
+      edi->di_cache.format = UNW_INFO_FORMAT_REMOTE_TABLE;
+      edi->di_cache.u.rti.name_ptr = 0;
+      /* two 32-bit values (ip_offset/fde_offset) per table-entry: */
+      edi->di_cache.u.rti.table_len = (fde_count * 8) / sizeof (unw_word_t);
+      edi->di_cache.u.rti.table_data = ((load_base + peh_hdr->p_vaddr)
+                                       + (addr - to_unw_word (edi->ei.image)
                                           - peh_hdr->p_offset));
-        found = 1;
+
+      /* For the binary-search table in the eh_frame_hdr, data-relative
+         means relative to the start of that section... */
+      edi->di_cache.u.rti.segbase = ((load_base + peh_hdr->p_vaddr)
+                                    + (to_unw_word (hdr) -
+                                       to_unw_word (edi->ei.image)
+                                       - peh_hdr->p_offset));
+      found = 1;
     }
 
 #if UNW_TARGET_ARM
-    if (parm_exidx)
+  if (parm_exidx)
     {
-        edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
-        edi->di_arm.start_ip = start_ip;
-        edi->di_arm.end_ip = end_ip;
-        edi->di_arm.u.rti.name_ptr = (unw_word_t) path;
-        edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
-        edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
-        found = 1;
+      edi->di_arm.format = UNW_INFO_FORMAT_ARM_EXIDX;
+      edi->di_arm.start_ip = start_ip;
+      edi->di_arm.end_ip = end_ip;
+      edi->di_arm.u.rti.name_ptr = to_unw_word (path);
+      edi->di_arm.u.rti.table_data = load_base + parm_exidx->p_vaddr;
+      edi->di_arm.u.rti.table_len = parm_exidx->p_memsz;
+      found = 1;
     }
 #endif
 
 #ifdef CONFIG_DEBUG_FRAME
-    /* Try .debug_frame. */
-    found = dwarf_find_debug_frame (found, &edi->edi.di_debug, ip, segbase, path,
-                                    start_ip, end_ip);
+  /* Try .debug_frame. */
+  found = dwarf_find_debug_frame (found, &edi->di_debug, ip, load_base, path,
+                                  start_ip, end_ip);
 #endif
 
-    return found;
+  return found;
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Gparser.c b/sgx_unwind/libunwind/src/dwarf/Gparser.c
index 27d3602..ad89352 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gparser.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gparser.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -23,666 +23,715 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include <stddef.h>
 #include "dwarf_i.h"
 #include "libunwind_i.h"
+#include <stddef.h>
+#include <limits.h>
 
-#define alloc_reg_state()	(mempool_alloc (&dwarf_reg_state_pool))
-#define free_reg_state(rs)	(mempool_free (&dwarf_reg_state_pool, rs))
+#define alloc_reg_state()       (mempool_alloc (&dwarf_reg_state_pool))
+#define free_reg_state(rs)      (mempool_free (&dwarf_reg_state_pool, rs))
+
+#define DWARF_UNW_CACHE_SIZE(log_size)   (1 << log_size)
+#define DWARF_UNW_HASH_SIZE(log_size)    (1 << (log_size + 1))
 
 static inline int
 read_regnum (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
              unw_word_t *valp, void *arg)
 {
-    int ret;
+  int ret;
 
-    if ((ret = dwarf_read_uleb128 (as, a, addr, valp, arg)) < 0)
-        return ret;
+  if ((ret = dwarf_read_uleb128 (as, a, addr, valp, arg)) < 0)
+    return ret;
 
-    if (*valp >= DWARF_NUM_PRESERVED_REGS)
+  if (*valp >= DWARF_NUM_PRESERVED_REGS)
     {
-        Debug (1, "Invalid register number %u\n", (unsigned int) *valp);
-        return -UNW_EBADREG;
+      Debug (1, "Invalid register number %u\n", (unsigned int) *valp);
+      return -UNW_EBADREG;
     }
-    return 0;
+  return 0;
 }
 
 static inline void
 set_reg (dwarf_state_record_t *sr, unw_word_t regnum, dwarf_where_t where,
          unw_word_t val)
 {
-    sr->rs_current.reg[regnum].where = where;
-    sr->rs_current.reg[regnum].val = val;
+  sr->rs_current.reg.where[regnum] = where;
+  sr->rs_current.reg.val[regnum] = val;
+}
+
+static inline int
+push_rstate_stack(dwarf_stackable_reg_state_t **rs_stack)
+{
+  dwarf_stackable_reg_state_t *old_rs = *rs_stack;
+  if (NULL == (*rs_stack = alloc_reg_state ()))
+    {
+      *rs_stack = old_rs;
+      return -1;
+    }
+  (*rs_stack)->next = old_rs;
+  return 0;
+}
+
+static inline void
+pop_rstate_stack(dwarf_stackable_reg_state_t **rs_stack)
+{
+  dwarf_stackable_reg_state_t *old_rs = *rs_stack;
+  *rs_stack = old_rs->next;
+  free_reg_state (old_rs);
+}
+
+static inline void
+empty_rstate_stack(dwarf_stackable_reg_state_t **rs_stack)
+{
+  while (*rs_stack)
+    pop_rstate_stack(rs_stack);
 }
 
 /* Run a CFI program to update the register state.  */
 static int
 run_cfi_program (struct dwarf_cursor *c, dwarf_state_record_t *sr,
-                 unw_word_t ip, unw_word_t *addr, unw_word_t end_addr,
+                 unw_word_t *ip, unw_word_t end_ip,
+		 unw_word_t *addr, unw_word_t end_addr,
+		 dwarf_stackable_reg_state_t **rs_stack,
                  struct dwarf_cie_info *dci)
 {
-    unw_word_t curr_ip, operand = 0, regnum, val, len, fde_encoding;
-    dwarf_reg_state_t *rs_stack = NULL, *new_rs, *old_rs;
-    unw_addr_space_t as;
-    unw_accessors_t *a;
-    uint8_t u8, op;
-    uint16_t u16;
-    uint32_t u32;
-    void *arg;
-    int ret;
+  unw_addr_space_t as;
+  void *arg;
 
-    as = c->as;
-    arg = c->as_arg;
-    if (c->pi.flags & UNW_PI_FLAG_DEBUG_FRAME)
+  if (c->pi.flags & UNW_PI_FLAG_DEBUG_FRAME)
     {
-        /* .debug_frame CFI is stored in local address space.  */
-        as = unw_local_addr_space;
-        arg = NULL;
+      /* .debug_frame CFI is stored in local address space.  */
+      as = unw_local_addr_space;
+      arg = NULL;
     }
-    a = unw_get_accessors (as);
-    curr_ip = c->pi.start_ip;
-
-    /* Process everything up to and including the current 'ip',
-       including all the DW_CFA_advance_loc instructions.  See
-       'c->use_prev_instr' use in 'fetch_proc_info' for details. */
-    while (curr_ip <= ip && *addr < end_addr)
+  else
     {
-        if ((ret = dwarf_readu8 (as, a, addr, &op, arg)) < 0)
-            return ret;
+      as = c->as;
+      arg = c->as_arg;
+    }
+  unw_accessors_t *a = unw_get_accessors_int (as);
+  int ret = 0;
 
-        if (op & DWARF_CFA_OPCODE_MASK)
+  while (*ip <= end_ip && *addr < end_addr && ret >= 0)
+    {
+      unw_word_t operand = 0, regnum, val, len;
+      uint8_t u8, op;
+      uint16_t u16;
+      uint32_t u32;
+
+      if ((ret = dwarf_readu8 (as, a, addr, &op, arg)) < 0)
+        break;
+
+      if (op & DWARF_CFA_OPCODE_MASK)
         {
-            operand = op & DWARF_CFA_OPERAND_MASK;
-            op &= ~DWARF_CFA_OPERAND_MASK;
+          operand = op & DWARF_CFA_OPERAND_MASK;
+          op &= ~DWARF_CFA_OPERAND_MASK;
         }
-        switch ((dwarf_cfa_t) op)
+      switch ((dwarf_cfa_t) op)
         {
         case DW_CFA_advance_loc:
-            curr_ip += operand * dci->code_align;
-            Debug (15, "CFA_advance_loc to 0x%lx\n", (long) curr_ip);
-            break;
+          *ip += operand * dci->code_align;
+          Debug (15, "CFA_advance_loc to 0x%lx\n", (long) *ip);
+          break;
 
         case DW_CFA_advance_loc1:
-            if ((ret = dwarf_readu8 (as, a, addr, &u8, arg)) < 0)
-                goto fail;
-            curr_ip += u8 * dci->code_align;
-            Debug (15, "CFA_advance_loc1 to 0x%lx\n", (long) curr_ip);
+          if ((ret = dwarf_readu8 (as, a, addr, &u8, arg)) < 0)
             break;
+          *ip += u8 * dci->code_align;
+          Debug (15, "CFA_advance_loc1 to 0x%lx\n", (long) *ip);
+          break;
 
         case DW_CFA_advance_loc2:
-            if ((ret = dwarf_readu16 (as, a, addr, &u16, arg)) < 0)
-                goto fail;
-            curr_ip += u16 * dci->code_align;
-            Debug (15, "CFA_advance_loc2 to 0x%lx\n", (long) curr_ip);
+          if ((ret = dwarf_readu16 (as, a, addr, &u16, arg)) < 0)
             break;
+          *ip += u16 * dci->code_align;
+          Debug (15, "CFA_advance_loc2 to 0x%lx\n", (long) *ip);
+          break;
 
         case DW_CFA_advance_loc4:
-            if ((ret = dwarf_readu32 (as, a, addr, &u32, arg)) < 0)
-                goto fail;
-            curr_ip += u32 * dci->code_align;
-            Debug (15, "CFA_advance_loc4 to 0x%lx\n", (long) curr_ip);
+          if ((ret = dwarf_readu32 (as, a, addr, &u32, arg)) < 0)
             break;
+          *ip += u32 * dci->code_align;
+          Debug (15, "CFA_advance_loc4 to 0x%lx\n", (long) *ip);
+          break;
 
         case DW_CFA_MIPS_advance_loc8:
 #ifdef UNW_TARGET_MIPS
-        {
-            uint64_t u64;
+          {
+            uint64_t u64 = 0;
 
             if ((ret = dwarf_readu64 (as, a, addr, &u64, arg)) < 0)
-                goto fail;
-            curr_ip += u64 * dci->code_align;
+              break;
+            *ip += u64 * dci->code_align;
             Debug (15, "CFA_MIPS_advance_loc8\n");
             break;
-        }
+          }
 #else
-        Debug (1, "DW_CFA_MIPS_advance_loc8 on non-MIPS target\n");
-        ret = -UNW_EINVAL;
-        goto fail;
+          Debug (1, "DW_CFA_MIPS_advance_loc8 on non-MIPS target\n");
+          ret = -UNW_EINVAL;
+          break;
 #endif
 
         case DW_CFA_offset:
-            regnum = operand;
-            if (regnum >= DWARF_NUM_PRESERVED_REGS)
+          regnum = operand;
+          if (regnum >= DWARF_NUM_PRESERVED_REGS)
             {
-                Debug (1, "Invalid register number %u in DW_cfa_OFFSET\n",
-                       (unsigned int) regnum);
-                ret = -UNW_EBADREG;
-                goto fail;
+              Debug (1, "Invalid register number %u in DW_cfa_OFFSET\n",
+                     (unsigned int) regnum);
+              ret = -UNW_EBADREG;
+              break;
             }
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
-            Debug (15, "CFA_offset r%lu at cfa+0x%lx\n",
-                   (long) regnum, (long) (val * dci->data_align));
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
             break;
+          set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
+          Debug (15, "CFA_offset r%lu at cfa+0x%lx\n",
+                 (long) regnum, (long) (val * dci->data_align));
+          break;
 
         case DW_CFA_offset_extended:
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
-            Debug (15, "CFA_offset_extended r%lu at cf+0x%lx\n",
-                   (long) regnum, (long) (val * dci->data_align));
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
+          Debug (15, "CFA_offset_extended r%lu at cf+0x%lx\n",
+                 (long) regnum, (long) (val * dci->data_align));
+          break;
 
         case DW_CFA_offset_extended_sf:
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
-            Debug (15, "CFA_offset_extended_sf r%lu at cf+0x%lx\n",
-                   (long) regnum, (long) (val * dci->data_align));
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, regnum, DWARF_WHERE_CFAREL, val * dci->data_align);
+          Debug (15, "CFA_offset_extended_sf r%lu at cf+0x%lx\n",
+                 (long) regnum, (long) (val * dci->data_align));
+          break;
 
         case DW_CFA_restore:
-            regnum = operand;
-            if (regnum >= DWARF_NUM_PRESERVED_REGS)
+          regnum = operand;
+          if (regnum >= DWARF_NUM_PRESERVED_REGS)
             {
-                Debug (1, "Invalid register number %u in DW_CFA_restore\n",
-                       (unsigned int) regnum);
-                ret = -UNW_EINVAL;
-                goto fail;
+              Debug (1, "Invalid register number %u in DW_CFA_restore\n",
+                     (unsigned int) regnum);
+              ret = -UNW_EINVAL;
+              break;
             }
-            sr->rs_current.reg[regnum] = sr->rs_initial.reg[regnum];
-            Debug (15, "CFA_restore r%lu\n", (long) regnum);
-            break;
+          sr->rs_current.reg.where[regnum] = sr->rs_initial.reg.where[regnum];
+          sr->rs_current.reg.val[regnum] = sr->rs_initial.reg.val[regnum];
+          Debug (15, "CFA_restore r%lu\n", (long) regnum);
+          break;
 
         case DW_CFA_restore_extended:
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &regnum, arg)) < 0)
-                goto fail;
-            if (regnum >= DWARF_NUM_PRESERVED_REGS)
-            {
-                Debug (1, "Invalid register number %u in "
-                       "DW_CFA_restore_extended\n", (unsigned int) regnum);
-                ret = -UNW_EINVAL;
-                goto fail;
-            }
-            sr->rs_current.reg[regnum] = sr->rs_initial.reg[regnum];
-            Debug (15, "CFA_restore_extended r%lu\n", (long) regnum);
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &regnum, arg)) < 0)
             break;
+          if (regnum >= DWARF_NUM_PRESERVED_REGS)
+            {
+              Debug (1, "Invalid register number %u in "
+                     "DW_CFA_restore_extended\n", (unsigned int) regnum);
+              ret = -UNW_EINVAL;
+              break;
+            }
+          sr->rs_current.reg.where[regnum] = sr->rs_initial.reg.where[regnum];
+          sr->rs_current.reg.val[regnum] = sr->rs_initial.reg.val[regnum];
+          Debug (15, "CFA_restore_extended r%lu\n", (long) regnum);
+          break;
 
         case DW_CFA_nop:
-            break;
+          break;
 
         case DW_CFA_set_loc:
-            fde_encoding = dci->fde_encoding;
-            if ((ret = dwarf_read_encoded_pointer (as, a, addr, fde_encoding,
-                                                   &c->pi, &curr_ip,
-                                                   arg)) < 0)
-                goto fail;
-            Debug (15, "CFA_set_loc to 0x%lx\n", (long) curr_ip);
+          if ((ret = dwarf_read_encoded_pointer (as, a, addr, dci->fde_encoding,
+                                                 &c->pi, ip,
+                                                 arg)) < 0)
             break;
+          Debug (15, "CFA_set_loc to 0x%lx\n", (long) *ip);
+          break;
 
         case DW_CFA_undefined:
-            if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_UNDEF, 0);
-            Debug (15, "CFA_undefined r%lu\n", (long) regnum);
+          if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
             break;
+          set_reg (sr, regnum, DWARF_WHERE_UNDEF, 0);
+          Debug (15, "CFA_undefined r%lu\n", (long) regnum);
+          break;
 
         case DW_CFA_same_value:
-            if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_SAME, 0);
-            Debug (15, "CFA_same_value r%lu\n", (long) regnum);
+          if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
             break;
+          set_reg (sr, regnum, DWARF_WHERE_SAME, 0);
+          Debug (15, "CFA_same_value r%lu\n", (long) regnum);
+          break;
 
         case DW_CFA_register:
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_REG, val);
-            Debug (15, "CFA_register r%lu to r%lu\n", (long) regnum, (long) val);
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, regnum, DWARF_WHERE_REG, val);
+          Debug (15, "CFA_register r%lu to r%lu\n", (long) regnum, (long) val);
+          break;
 
         case DW_CFA_remember_state:
-            new_rs = alloc_reg_state ();
-            if (!new_rs)
-            {
-                Debug (1, "Out of memory in DW_CFA_remember_state\n");
-                ret = -UNW_ENOMEM;
-                goto fail;
-            }
-
-            memcpy (new_rs->reg, sr->rs_current.reg, sizeof (new_rs->reg));
-            new_rs->next = rs_stack;
-            rs_stack = new_rs;
-            Debug (15, "CFA_remember_state\n");
-            break;
+	  if (push_rstate_stack(rs_stack) < 0)
+	    {
+              Debug (1, "Out of memory in DW_CFA_remember_state\n");
+              ret = -UNW_ENOMEM;
+              break;
+	    }
+          (*rs_stack)->state = sr->rs_current;
+          Debug (15, "CFA_remember_state\n");
+          break;
 
         case DW_CFA_restore_state:
-            if (!rs_stack)
+          if (!*rs_stack)
             {
-                Debug (1, "register-state stack underflow\n");
-                ret = -UNW_EINVAL;
-                goto fail;
+              Debug (1, "register-state stack underflow\n");
+              ret = -UNW_EINVAL;
+              break;
             }
-            memcpy (&sr->rs_current.reg, &rs_stack->reg, sizeof (rs_stack->reg));
-            old_rs = rs_stack;
-            rs_stack = rs_stack->next;
-            free_reg_state (old_rs);
-            Debug (15, "CFA_restore_state\n");
-            break;
+          sr->rs_current = (*rs_stack)->state;
+          pop_rstate_stack(rs_stack);
+          Debug (15, "CFA_restore_state\n");
+          break;
 
         case DW_CFA_def_cfa:
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
-            set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val);	/* NOT factored! */
-            Debug (15, "CFA_def_cfa r%lu+0x%lx\n", (long) regnum, (long) val);
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
+          set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val);   /* NOT factored! */
+          Debug (15, "CFA_def_cfa r%lu+0x%lx\n", (long) regnum, (long) val);
+          break;
 
         case DW_CFA_def_cfa_sf:
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
-            set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
-                     val * dci->data_align);		/* factored! */
-            Debug (15, "CFA_def_cfa_sf r%lu+0x%lx\n",
-                   (long) regnum, (long) (val * dci->data_align));
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
+          set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
+                   val * dci->data_align);              /* factored! */
+          Debug (15, "CFA_def_cfa_sf r%lu+0x%lx\n",
+                 (long) regnum, (long) (val * dci->data_align));
+          break;
 
         case DW_CFA_def_cfa_register:
-            if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                goto fail;
-            set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
-            Debug (15, "CFA_def_cfa_register r%lu\n", (long) regnum);
+          if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
             break;
+          set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_REG, regnum);
+          Debug (15, "CFA_def_cfa_register r%lu\n", (long) regnum);
+          break;
 
         case DW_CFA_def_cfa_offset:
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
-                goto fail;
-            set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val);	/* NOT factored! */
-            Debug (15, "CFA_def_cfa_offset 0x%lx\n", (long) val);
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
             break;
+          set_reg (sr, DWARF_CFA_OFF_COLUMN, 0, val);   /* NOT factored! */
+          Debug (15, "CFA_def_cfa_offset 0x%lx\n", (long) val);
+          break;
 
         case DW_CFA_def_cfa_offset_sf:
-            if ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0)
-                goto fail;
-            set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
-                     val * dci->data_align);	/* factored! */
-            Debug (15, "CFA_def_cfa_offset_sf 0x%lx\n",
-                   (long) (val * dci->data_align));
+          if ((ret = dwarf_read_sleb128 (as, a, addr, &val, arg)) < 0)
             break;
+          set_reg (sr, DWARF_CFA_OFF_COLUMN, 0,
+                   val * dci->data_align);      /* factored! */
+          Debug (15, "CFA_def_cfa_offset_sf 0x%lx\n",
+                 (long) (val * dci->data_align));
+          break;
 
         case DW_CFA_def_cfa_expression:
-            /* Save the address of the DW_FORM_block for later evaluation. */
-            set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_EXPR, *addr);
+          /* Save the address of the DW_FORM_block for later evaluation. */
+          set_reg (sr, DWARF_CFA_REG_COLUMN, DWARF_WHERE_EXPR, *addr);
 
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
-                goto fail;
-
-            Debug (15, "CFA_def_cfa_expr @ 0x%lx [%lu bytes]\n",
-                   (long) *addr, (long) len);
-            *addr += len;
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
             break;
 
+          Debug (15, "CFA_def_cfa_expr @ 0x%lx [%lu bytes]\n",
+                 (long) *addr, (long) len);
+          *addr += len;
+          break;
+
         case DW_CFA_expression:
-            if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                goto fail;
-
-            /* Save the address of the DW_FORM_block for later evaluation. */
-            set_reg (sr, regnum, DWARF_WHERE_EXPR, *addr);
-
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
-                goto fail;
-
-            Debug (15, "CFA_expression r%lu @ 0x%lx [%lu bytes]\n",
-                   (long) regnum, (long) addr, (long) len);
-            *addr += len;
+          if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
             break;
 
+          /* Save the address of the DW_FORM_block for later evaluation. */
+          set_reg (sr, regnum, DWARF_WHERE_EXPR, *addr);
+
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
+            break;
+
+          Debug (15, "CFA_expression r%lu @ 0x%lx [%lu bytes]\n",
+                 (long) regnum, (long) addr, (long) len);
+          *addr += len;
+          break;
+
+        case DW_CFA_val_expression:
+          if ((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+            break;
+
+          /* Save the address of the DW_FORM_block for later evaluation. */
+          set_reg (sr, regnum, DWARF_WHERE_VAL_EXPR, *addr);
+
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &len, arg)) < 0)
+            break;
+
+          Debug (15, "CFA_val_expression r%lu @ 0x%lx [%lu bytes]\n",
+                 (long) regnum, (long) addr, (long) len);
+          *addr += len;
+          break;
+
         case DW_CFA_GNU_args_size:
-            if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
-                goto fail;
-            sr->args_size = val;
-            Debug (15, "CFA_GNU_args_size %lu\n", (long) val);
+          if ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0)
             break;
+          sr->args_size = val;
+          Debug (15, "CFA_GNU_args_size %lu\n", (long) val);
+          break;
 
         case DW_CFA_GNU_negative_offset_extended:
-            /* A comment in GCC says that this is obsoleted by
-               DW_CFA_offset_extended_sf, but that it's used by older
-               PowerPC code.  */
-            if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
-                    || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
-                goto fail;
-            set_reg (sr, regnum, DWARF_WHERE_CFAREL, -(val * dci->data_align));
-            Debug (15, "CFA_GNU_negative_offset_extended cfa+0x%lx\n",
-                   (long) -(val * dci->data_align));
+          /* A comment in GCC says that this is obsoleted by
+             DW_CFA_offset_extended_sf, but that it's used by older
+             PowerPC code.  */
+          if (((ret = read_regnum (as, a, addr, &regnum, arg)) < 0)
+              || ((ret = dwarf_read_uleb128 (as, a, addr, &val, arg)) < 0))
             break;
+          set_reg (sr, regnum, DWARF_WHERE_CFAREL, -(val * dci->data_align));
+          Debug (15, "CFA_GNU_negative_offset_extended cfa+0x%lx\n",
+                 (long) -(val * dci->data_align));
+          break;
 
         case DW_CFA_GNU_window_save:
 #ifdef UNW_TARGET_SPARC
-            /* This is a special CFA to handle all 16 windowed registers
-               on SPARC.  */
-            for (regnum = 16; regnum < 32; ++regnum)
-                set_reg (sr, regnum, DWARF_WHERE_CFAREL,
-                         (regnum - 16) * sizeof (unw_word_t));
-            Debug (15, "CFA_GNU_window_save\n");
-            break;
+          /* This is a special CFA to handle all 16 windowed registers
+             on SPARC.  */
+          for (regnum = 16; regnum < 32; ++regnum)
+            set_reg (sr, regnum, DWARF_WHERE_CFAREL,
+                     (regnum - 16) * sizeof (unw_word_t));
+          Debug (15, "CFA_GNU_window_save\n");
+          break;
 #else
-            /* FALL THROUGH */
+          /* FALL THROUGH */
 #endif
         case DW_CFA_lo_user:
         case DW_CFA_hi_user:
-            Debug (1, "Unexpected CFA opcode 0x%x\n", op);
-            ret = -UNW_EINVAL;
-            goto fail;
+          Debug (1, "Unexpected CFA opcode 0x%x\n", op);
+          ret = -UNW_EINVAL;
+          break;
         }
     }
-    ret = 0;
 
-fail:
-    /* Free the register-state stack, if not empty already.  */
-    while (rs_stack)
-    {
-        old_rs = rs_stack;
-        rs_stack = rs_stack->next;
-        free_reg_state (old_rs);
-    }
-    return ret;
+  if (ret > 0)
+    ret = 0;
+  return ret;
 }
 
 static int
-fetch_proc_info (struct dwarf_cursor *c, unw_word_t ip, int need_unwind_info)
+fetch_proc_info (struct dwarf_cursor *c, unw_word_t ip)
 {
-    int ret, dynamic = 1;
+  int ret, dynamic = 1;
 
-    /* The 'ip' can point either to the previous or next instruction
-       depending on what type of frame we have: normal call or a place
-       to resume execution (e.g. after signal frame).
+  /* The 'ip' can point either to the previous or next instruction
+     depending on what type of frame we have: normal call or a place
+     to resume execution (e.g. after signal frame).
 
-       For a normal call frame we need to back up so we point within the
-       call itself; this is important because a) the call might be the
-       very last instruction of the function and the edge of the FDE,
-       and b) so that run_cfi_program() runs locations up to the call
-       but not more.
+     For a normal call frame we need to back up so we point within the
+     call itself; this is important because a) the call might be the
+     very last instruction of the function and the edge of the FDE,
+     and b) so that run_cfi_program() runs locations up to the call
+     but not more.
 
-       For execution resume, we need to do the exact opposite and look
-       up using the current 'ip' value.  That is where execution will
-       continue, and it's important we get this right, as 'ip' could be
-       right at the function entry and hence FDE edge, or at instruction
-       that manipulates CFA (push/pop). */
-    if (c->use_prev_instr)
-        --ip;
+     For signal frame, we need to do the exact opposite and look
+     up using the current 'ip' value.  That is where execution will
+     continue, and it's important we get this right, as 'ip' could be
+     right at the function entry and hence FDE edge, or at instruction
+     that manipulates CFA (push/pop). */
+  if (c->use_prev_instr)
+    --ip;
 
-    if (c->pi_valid && !need_unwind_info)
-        return 0;
+  memset (&c->pi, 0, sizeof (c->pi));
 
-    memset (&c->pi, 0, sizeof (c->pi));
-
-    /* check dynamic info first --- it overrides everything else */
-    ret = unwi_find_dynamic_proc_info (c->as, ip, &c->pi, need_unwind_info,
-                                       c->as_arg);
-    if (ret == -UNW_ENOINFO)
+  /* check dynamic info first --- it overrides everything else */
+  ret = unwi_find_dynamic_proc_info (c->as, ip, &c->pi, 1,
+                                     c->as_arg);
+  if (ret == -UNW_ENOINFO)
     {
-        dynamic = 0;
-        if ((ret = tdep_find_proc_info (c, ip, need_unwind_info)) < 0)
-            return ret;
+      dynamic = 0;
+      if ((ret = tdep_find_proc_info (c, ip, 1)) < 0)
+        return ret;
     }
 
-    if (c->pi.format != UNW_INFO_FORMAT_DYNAMIC
-            && c->pi.format != UNW_INFO_FORMAT_TABLE
-            && c->pi.format != UNW_INFO_FORMAT_REMOTE_TABLE)
-        return -UNW_ENOINFO;
+  if (c->pi.format != UNW_INFO_FORMAT_DYNAMIC
+      && c->pi.format != UNW_INFO_FORMAT_TABLE
+      && c->pi.format != UNW_INFO_FORMAT_REMOTE_TABLE)
+    return -UNW_ENOINFO;
 
-    c->pi_valid = 1;
-    c->pi_is_dynamic = dynamic;
+  c->pi_valid = 1;
+  c->pi_is_dynamic = dynamic;
 
-    /* Let system/machine-dependent code determine frame-specific attributes. */
-    if (ret >= 0)
-        tdep_fetch_frame (c, ip, need_unwind_info);
+  /* Let system/machine-dependent code determine frame-specific attributes. */
+  if (ret >= 0)
+    tdep_fetch_frame (c, ip, 1);
 
-    /* Update use_prev_instr for the next frame. */
-    if (need_unwind_info)
-    {
-        assert(c->pi.unwind_info);
-        struct dwarf_cie_info *dci = c->pi.unwind_info;
-        c->use_prev_instr = ! dci->signal_frame;
-    }
-
-    return ret;
+  return ret;
 }
 
 static int
 parse_dynamic (struct dwarf_cursor *c, unw_word_t ip, dwarf_state_record_t *sr)
 {
-    Debug (1, "Not yet implemented\n");
-#if 0
-    /* Don't forget to set the ret_addr_column!  */
-    c->ret_addr_column = XXX;
-#endif
-    return -UNW_ENOINFO;
+  Debug (1, "Not yet implemented\n");
+  return -UNW_ENOINFO;
 }
 
 static inline void
 put_unwind_info (struct dwarf_cursor *c, unw_proc_info_t *pi)
 {
-    if (!c->pi_valid)
-        return;
-
-    if (c->pi_is_dynamic)
-        unwi_put_dynamic_unwind_info (c->as, pi, c->as_arg);
-    else if (pi->unwind_info)
+  if (c->pi_is_dynamic)
+    unwi_put_dynamic_unwind_info (c->as, pi, c->as_arg);
+  else if (pi->unwind_info && pi->format == UNW_INFO_FORMAT_TABLE)
     {
-        mempool_free (&dwarf_cie_info_pool, pi->unwind_info);
-        pi->unwind_info = NULL;
+      mempool_free (&dwarf_cie_info_pool, pi->unwind_info);
+      pi->unwind_info = NULL;
     }
+  c->pi_valid = 0;
+}
+
+static inline int
+setup_fde (struct dwarf_cursor *c, dwarf_state_record_t *sr)
+{
+  int i, ret;
+
+  assert (c->pi_valid);
+
+  memset (sr, 0, sizeof (*sr));
+  for (i = 0; i < DWARF_NUM_PRESERVED_REGS + 2; ++i)
+    set_reg (sr, i, DWARF_WHERE_SAME, 0);
+
+  struct dwarf_cie_info *dci = c->pi.unwind_info;
+  sr->rs_current.ret_addr_column  = dci->ret_addr_column;
+  unw_word_t addr = dci->cie_instr_start;
+  unw_word_t curr_ip = 0;
+  dwarf_stackable_reg_state_t *rs_stack = NULL;
+  ret = run_cfi_program (c, sr, &curr_ip, ~(unw_word_t) 0, &addr,
+			 dci->cie_instr_end,
+			 &rs_stack, dci);
+  empty_rstate_stack(&rs_stack);
+  if (ret < 0)
+    return ret;
+
+  memcpy (&sr->rs_initial, &sr->rs_current, sizeof (sr->rs_initial));
+  return 0;
 }
 
 static inline int
 parse_fde (struct dwarf_cursor *c, unw_word_t ip, dwarf_state_record_t *sr)
 {
-    struct dwarf_cie_info *dci;
-    unw_word_t addr;
-    int ret;
+  int ret;
+  struct dwarf_cie_info *dci = c->pi.unwind_info;
+  unw_word_t addr = dci->fde_instr_start;
+  unw_word_t curr_ip = c->pi.start_ip;
+  dwarf_stackable_reg_state_t *rs_stack = NULL;
+  /* Process up to current `ip` for signal frame and `ip - 1` for normal call frame
+     See `c->use_prev_instr` use in `fetch_proc_info` for details. */
+  ret = run_cfi_program (c, sr, &curr_ip, ip - c->use_prev_instr, &addr, dci->fde_instr_end,
+			 &rs_stack, dci);
+  empty_rstate_stack(&rs_stack);
+  if (ret < 0)
+    return ret;
 
-    dci = c->pi.unwind_info;
-    c->ret_addr_column = dci->ret_addr_column;
-
-    addr = dci->cie_instr_start;
-    if ((ret = run_cfi_program (c, sr, ~(unw_word_t) 0, &addr,
-                                dci->cie_instr_end, dci)) < 0)
-        return ret;
-
-    memcpy (&sr->rs_initial, &sr->rs_current, sizeof (sr->rs_initial));
-
-    addr = dci->fde_instr_start;
-    if ((ret = run_cfi_program (c, sr, ip, &addr, dci->fde_instr_end, dci)) < 0)
-        return ret;
-
-    return 0;
+  return 0;
 }
 
-static inline void
-flush_rs_cache (struct dwarf_rs_cache *cache)
+HIDDEN int
+dwarf_flush_rs_cache (struct dwarf_rs_cache *cache)
 {
-    int i;
+  int i;
 
-    cache->lru_head = DWARF_UNW_CACHE_SIZE - 1;
-    cache->lru_tail = 0;
+  if (cache->log_size == DWARF_DEFAULT_LOG_UNW_CACHE_SIZE
+      || !cache->hash) {
+    cache->hash = cache->default_hash;
+    cache->buckets = cache->default_buckets;
+    cache->links = cache->default_links;
+    cache->log_size = DWARF_DEFAULT_LOG_UNW_CACHE_SIZE;
+  } else {
+    if (cache->hash && cache->hash != cache->default_hash)
+      FREE_MEMORY(cache->hash, DWARF_UNW_HASH_SIZE(cache->prev_log_size)
+                           * sizeof (cache->hash[0]));
+    if (cache->buckets && cache->buckets != cache->default_buckets)
+      FREE_MEMORY(cache->buckets, DWARF_UNW_CACHE_SIZE(cache->prev_log_size)
+	                      * sizeof (cache->buckets[0]));
+    if (cache->links && cache->links != cache->default_links)
+      FREE_MEMORY(cache->links, DWARF_UNW_CACHE_SIZE(cache->prev_log_size)
+	                      * sizeof (cache->links[0]));
 
-    for (i = 0; i < DWARF_UNW_CACHE_SIZE; ++i)
+    GET_MEMORY(cache->hash, DWARF_UNW_HASH_SIZE(cache->log_size)
+                             * sizeof (cache->hash[0]));
+    GET_MEMORY(cache->buckets, DWARF_UNW_CACHE_SIZE(cache->log_size)
+                                * sizeof (cache->buckets[0]));
+    GET_MEMORY(cache->links, DWARF_UNW_CACHE_SIZE(cache->log_size)
+                                * sizeof (cache->links[0]));
+    if (!cache->hash || !cache->buckets || !cache->links)
+      {
+        Debug (1, "Unable to allocate cache memory");
+        return -UNW_ENOMEM;
+      }
+    cache->prev_log_size = cache->log_size;
+  }
+
+  cache->rr_head = 0;
+
+  for (i = 0; i < DWARF_UNW_CACHE_SIZE(cache->log_size); ++i)
     {
-        if (i > 0)
-            cache->buckets[i].lru_chain = (i - 1);
-        cache->buckets[i].coll_chain = -1;
-        cache->buckets[i].ip = 0;
-        cache->buckets[i].valid = 0;
+      cache->links[i].coll_chain = -1;
+      cache->links[i].ip = 0;
+      cache->links[i].valid = 0;
     }
-    for (i = 0; i<DWARF_UNW_HASH_SIZE; ++i)
-        cache->hash[i] = -1;
+  for (i = 0; i< DWARF_UNW_HASH_SIZE(cache->log_size); ++i)
+    cache->hash[i] = -1;
+
+  return 0;
 }
 
 static inline struct dwarf_rs_cache *
 get_rs_cache (unw_addr_space_t as, intrmask_t *saved_maskp)
 {
-    struct dwarf_rs_cache *cache = &as->global_cache;
-    unw_caching_policy_t caching = as->caching_policy;
+  struct dwarf_rs_cache *cache = &as->global_cache;
+  unw_caching_policy_t caching = as->caching_policy;
 
-    if (caching == UNW_CACHE_NONE)
+  if (caching == UNW_CACHE_NONE)
+    return NULL;
+
+#if defined(HAVE___THREAD) && HAVE___THREAD
+  if (likely (caching == UNW_CACHE_PER_THREAD))
+    {
+      static __thread struct dwarf_rs_cache tls_cache __attribute__((tls_model("initial-exec")));
+      Debug (16, "using TLS cache\n");
+      cache = &tls_cache;
+    }
+  else
+#else
+  if (likely (caching == UNW_CACHE_GLOBAL))
+#endif
+    {
+      Debug (16, "acquiring lock\n");
+      lock_acquire (&cache->lock, *saved_maskp);
+    }
+
+  if ((atomic_read (&as->cache_generation) != atomic_read (&cache->generation))
+       || !cache->hash)
+    {
+      /* cache_size is only set in the global_cache, copy it over before flushing */
+      cache->log_size = as->global_cache.log_size;
+      if (dwarf_flush_rs_cache (cache) < 0)
         return NULL;
-
-    if (likely (caching == UNW_CACHE_GLOBAL))
-    {
-        Debug (16, "%s: acquiring lock\n", __FUNCTION__);
-        lock_acquire (&cache->lock, *saved_maskp);
+      cache->generation = as->cache_generation;
     }
 
-    if (atomic_read (&as->cache_generation) != atomic_read (&cache->generation))
-    {
-        flush_rs_cache (cache);
-        cache->generation = as->cache_generation;
-    }
-
-    return cache;
+  return cache;
 }
 
 static inline void
 put_rs_cache (unw_addr_space_t as, struct dwarf_rs_cache *cache,
-              intrmask_t *saved_maskp)
+                  intrmask_t *saved_maskp)
 {
-    assert (as->caching_policy != UNW_CACHE_NONE);
+  assert (as->caching_policy != UNW_CACHE_NONE);
 
-    Debug (16, "unmasking signals/interrupts and releasing lock\n");
-    if (likely (as->caching_policy == UNW_CACHE_GLOBAL))
-        lock_release (&cache->lock, *saved_maskp);
+  Debug (16, "unmasking signals/interrupts and releasing lock\n");
+  if (likely (as->caching_policy == UNW_CACHE_GLOBAL))
+    lock_release (&cache->lock, *saved_maskp);
 }
 
-static inline unw_hash_index_t
-hash (unw_word_t ip)
+static inline unw_hash_index_t CONST_ATTR
+hash (unw_word_t ip, unsigned short log_size)
 {
-    /* based on (sqrt(5)/2-1)*2^64 */
-# define magic	((unw_word_t) 0x9e3779b97f4a7c16ULL)
+  /* based on (sqrt(5)/2-1)*2^64 */
+# define magic  ((unw_word_t) 0x9e3779b97f4a7c16ULL)
 
-    return ip * magic >> ((sizeof(unw_word_t) * 8) - DWARF_LOG_UNW_HASH_SIZE);
+  return ip * magic >> ((sizeof(unw_word_t) * 8) - (log_size + 1));
 }
 
 static inline long
-cache_match (dwarf_reg_state_t *rs, unw_word_t ip)
+cache_match (struct dwarf_rs_cache *cache, unsigned short index, unw_word_t ip)
 {
-    if (rs->valid && (ip == rs->ip))
-        return 1;
-    return 0;
+  return (cache->links[index].valid && (ip == cache->links[index].ip));
 }
 
 static dwarf_reg_state_t *
 rs_lookup (struct dwarf_rs_cache *cache, struct dwarf_cursor *c)
 {
-    dwarf_reg_state_t *rs = cache->buckets + c->hint;
-    unsigned short index;
-    unw_word_t ip;
+  unsigned short index;
+  unw_word_t ip = c->ip;
 
-    ip = c->ip;
-
-    if (cache_match (rs, ip))
-        return rs;
-
-    index = cache->hash[hash (ip)];
-    if (index >= DWARF_UNW_CACHE_SIZE)
-        return 0;
-
-    rs = cache->buckets + index;
-    while (1)
+  if (c->hint > 0)
     {
-        if (cache_match (rs, ip))
-        {
-            /* update hint; no locking needed: single-word writes are atomic */
-            c->hint = cache->buckets[c->prev_rs].hint =
-                          (rs - cache->buckets);
-            return rs;
-        }
-        if (rs->coll_chain >= DWARF_UNW_HASH_SIZE)
-            return 0;
-        rs = cache->buckets + rs->coll_chain;
+      index = c->hint - 1;
+      if (cache_match (cache, index, ip))
+	return &cache->buckets[index];
     }
+
+  for (index = cache->hash[hash (ip, cache->log_size)];
+       index < DWARF_UNW_CACHE_SIZE(cache->log_size);
+       index = cache->links[index].coll_chain)
+    {
+      if (cache_match (cache, index, ip))
+	return &cache->buckets[index];
+    }
+  return NULL;
 }
 
 static inline dwarf_reg_state_t *
 rs_new (struct dwarf_rs_cache *cache, struct dwarf_cursor * c)
 {
-    dwarf_reg_state_t *rs, *prev, *tmp;
-    unw_hash_index_t index;
-    unsigned short head;
+  unw_hash_index_t index;
+  unsigned short head;
 
-    head = cache->lru_head;
-    rs = cache->buckets + head;
-    cache->lru_head = rs->lru_chain;
+  head = cache->rr_head;
+  cache->rr_head = (head + 1) & (DWARF_UNW_CACHE_SIZE(cache->log_size) - 1);
 
-    /* re-insert rs at the tail of the LRU chain: */
-    cache->buckets[cache->lru_tail].lru_chain = head;
-    cache->lru_tail = head;
-
-    /* remove the old rs from the hash table (if it's there): */
-    if (rs->ip)
+  /* remove the old rs from the hash table (if it's there): */
+  if (cache->links[head].ip)
     {
-        index = hash (rs->ip);
-        tmp = cache->buckets + cache->hash[index];
-        prev = 0;
-        while (1)
-        {
-            if (tmp == rs)
-            {
-                if (prev)
-                    prev->coll_chain = tmp->coll_chain;
-                else
-                    cache->hash[index] = tmp->coll_chain;
-                break;
-            }
-            else
-                prev = tmp;
-            if (tmp->coll_chain >= DWARF_UNW_CACHE_SIZE)
-                /* old rs wasn't in the hash-table */
-                break;
-            tmp = cache->buckets + tmp->coll_chain;
-        }
+      unsigned short *pindex;
+      for (pindex = &cache->hash[hash (cache->links[head].ip, cache->log_size)];
+	   *pindex < DWARF_UNW_CACHE_SIZE(cache->log_size);
+	   pindex = &cache->links[*pindex].coll_chain)
+	{
+	  if (*pindex == head)
+	    {
+	      *pindex = cache->links[*pindex].coll_chain;
+	      break;
+	    }
+	}
     }
 
-    /* enter new rs in the hash table */
-    index = hash (c->ip);
-    rs->coll_chain = cache->hash[index];
-    cache->hash[index] = rs - cache->buckets;
+  /* enter new rs in the hash table */
+  index = hash (c->ip, cache->log_size);
+  cache->links[head].coll_chain = cache->hash[index];
+  cache->hash[index] = head;
 
-    rs->hint = 0;
-    rs->ip = c->ip;
-    rs->valid = 1;
-    rs->ret_addr_column = c->ret_addr_column;
-    rs->signal_frame = 0;
-    tdep_cache_frame (c, rs);
-
-    return rs;
+  cache->links[head].ip = c->ip;
+  cache->links[head].valid = 1;
+  cache->links[head].signal_frame = tdep_cache_frame(c);
+  return cache->buckets + head;
 }
 
 static int
 create_state_record_for (struct dwarf_cursor *c, dwarf_state_record_t *sr,
                          unw_word_t ip)
 {
-    int i, ret;
-
-    assert (c->pi_valid);
-
-    memset (sr, 0, sizeof (*sr));
-    for (i = 0; i < DWARF_NUM_PRESERVED_REGS + 2; ++i)
-        set_reg (sr, i, DWARF_WHERE_SAME, 0);
-
-    switch (c->pi.format)
+  int ret;
+  switch (c->pi.format)
     {
     case UNW_INFO_FORMAT_TABLE:
     case UNW_INFO_FORMAT_REMOTE_TABLE:
-        ret = parse_fde (c, ip, sr);
-        break;
+      if ((ret = setup_fde(c, sr)) < 0)
+	return ret;
+      ret = parse_fde (c, ip, sr);
+      break;
 
     case UNW_INFO_FORMAT_DYNAMIC:
-        ret = parse_dynamic (c, ip, sr);
-        break;
+      ret = parse_dynamic (c, ip, sr);
+      break;
 
     default:
-        Debug (1, "Unexpected unwind-info format %d\n", c->pi.format);
-        ret = -UNW_EINVAL;
+      Debug (1, "Unexpected unwind-info format %d\n", c->pi.format);
+      ret = -UNW_EINVAL;
     }
-    return ret;
+  return ret;
 }
 
 static inline int
@@ -690,219 +739,322 @@
                     unw_accessors_t *a, unw_word_t addr,
                     dwarf_loc_t *locp, void *arg)
 {
-    int ret, is_register;
-    unw_word_t len, val;
+  int ret, is_register;
+  unw_word_t len, val;
 
-    /* read the length of the expression: */
-    if ((ret = dwarf_read_uleb128 (as, a, &addr, &len, arg)) < 0)
-        return ret;
+  /* read the length of the expression: */
+  if ((ret = dwarf_read_uleb128 (as, a, &addr, &len, arg)) < 0)
+    return ret;
 
-    /* evaluate the expression: */
-    if ((ret = dwarf_eval_expr (c, &addr, len, &val, &is_register)) < 0)
-        return ret;
+  /* evaluate the expression: */
+  if ((ret = dwarf_eval_expr (c, &addr, len, &val, &is_register)) < 0)
+    return ret;
 
-    if (is_register)
-        *locp = DWARF_REG_LOC (c, dwarf_to_unw_regnum (val));
-    else
-        *locp = DWARF_MEM_LOC (c, val);
+  if (is_register)
+    *locp = DWARF_REG_LOC (c, dwarf_to_unw_regnum (val));
+  else
+    *locp = DWARF_MEM_LOC (c, val);
 
-    return 0;
+  return 0;
 }
 
 static int
 apply_reg_state (struct dwarf_cursor *c, struct dwarf_reg_state *rs)
 {
-    unw_word_t regnum, addr, cfa, ip;
-    unw_word_t prev_ip, prev_cfa;
-    unw_addr_space_t as;
-    dwarf_loc_t cfa_loc;
-    unw_accessors_t *a;
-    int i, ret;
-    void *arg;
+  unw_word_t regnum, addr, cfa, ip;
+  unw_word_t prev_ip, prev_cfa;
+  unw_addr_space_t as;
+  dwarf_loc_t cfa_loc;
+  unw_accessors_t *a;
+  int i, ret;
+  void *arg;
 
-    prev_ip = c->ip;
-    prev_cfa = c->cfa;
+  prev_ip = c->ip;
+  prev_cfa = c->cfa;
 
-    as = c->as;
-    arg = c->as_arg;
-    a = unw_get_accessors (as);
+  as = c->as;
+  arg = c->as_arg;
+  a = unw_get_accessors_int (as);
 
-    /* Evaluate the CFA first, because it may be referred to by other
-       expressions.  */
+  /* Evaluate the CFA first, because it may be referred to by other
+     expressions.  */
 
-    if (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
+  if (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_REG)
     {
-        /* CFA is equal to [reg] + offset: */
+      /* CFA is equal to [reg] + offset: */
 
-        /* As a special-case, if the stack-pointer is the CFA and the
-        stack-pointer wasn't saved, popping the CFA implicitly pops
+      /* As a special-case, if the stack-pointer is the CFA and the
+         stack-pointer wasn't saved, popping the CFA implicitly pops
          the stack-pointer as well.  */
-        if ((rs->reg[DWARF_CFA_REG_COLUMN].val == UNW_TDEP_SP)
-                && (UNW_TDEP_SP < ARRAY_SIZE(rs->reg))
-                && (rs->reg[UNW_TDEP_SP].where == DWARF_WHERE_SAME))
-            cfa = c->cfa;
-        else
+      if ((rs->reg.val[DWARF_CFA_REG_COLUMN] == UNW_TDEP_SP)
+          && (UNW_TDEP_SP < ARRAY_SIZE(rs->reg.val))
+          && (rs->reg.where[UNW_TDEP_SP] == DWARF_WHERE_SAME))
+          cfa = c->cfa;
+      else
         {
-            regnum = dwarf_to_unw_regnum (rs->reg[DWARF_CFA_REG_COLUMN].val);
-            if ((ret = unw_get_reg ((unw_cursor_t *) c, regnum, &cfa)) < 0)
-                return ret;
-        }
-        cfa += rs->reg[DWARF_CFA_OFF_COLUMN].val;
-    }
-    else
-    {
-        /* CFA is equal to EXPR: */
-
-        assert (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_EXPR);
-
-        addr = rs->reg[DWARF_CFA_REG_COLUMN].val;
-        if ((ret = eval_location_expr (c, as, a, addr, &cfa_loc, arg)) < 0)
+          regnum = dwarf_to_unw_regnum (rs->reg.val[DWARF_CFA_REG_COLUMN]);
+          if ((ret = unw_get_reg ((unw_cursor_t *) c, regnum, &cfa)) < 0)
             return ret;
-        /* the returned location better be a memory location... */
-        if (DWARF_IS_REG_LOC (cfa_loc))
-            return -UNW_EBADFRAME;
-        cfa = DWARF_GET_LOC (cfa_loc);
+        }
+      cfa += rs->reg.val[DWARF_CFA_OFF_COLUMN];
+    }
+  else
+    {
+      /* CFA is equal to EXPR: */
+
+      assert (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_EXPR);
+
+      addr = rs->reg.val[DWARF_CFA_REG_COLUMN];
+      if ((ret = eval_location_expr (c, as, a, addr, &cfa_loc, arg)) < 0)
+        return ret;
+      /* the returned location better be a memory location... */
+      if (DWARF_IS_REG_LOC (cfa_loc))
+        return -UNW_EBADFRAME;
+      cfa = DWARF_GET_LOC (cfa_loc);
     }
 
-    for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
+  dwarf_loc_t new_loc[DWARF_NUM_PRESERVED_REGS];
+  memcpy(new_loc, c->loc, sizeof(new_loc));
+
+  for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
     {
-        switch ((dwarf_where_t) rs->reg[i].where)
+      switch ((dwarf_where_t) rs->reg.where[i])
         {
         case DWARF_WHERE_UNDEF:
-            c->loc[i] = DWARF_NULL_LOC;
-            break;
+          new_loc[i] = DWARF_NULL_LOC;
+          break;
 
         case DWARF_WHERE_SAME:
-            break;
+          break;
 
         case DWARF_WHERE_CFAREL:
-            c->loc[i] = DWARF_MEM_LOC (c, cfa + rs->reg[i].val);
-            break;
+          new_loc[i] = DWARF_MEM_LOC (c, cfa + rs->reg.val[i]);
+          break;
 
         case DWARF_WHERE_REG:
-            c->loc[i] = DWARF_REG_LOC (c, dwarf_to_unw_regnum (rs->reg[i].val));
-            break;
+          new_loc[i] = DWARF_REG_LOC (c, dwarf_to_unw_regnum (rs->reg.val[i]));
+          break;
 
         case DWARF_WHERE_EXPR:
-            addr = rs->reg[i].val;
-            if ((ret = eval_location_expr (c, as, a, addr, c->loc + i, arg)) < 0)
-                return ret;
-            break;
+          addr = rs->reg.val[i];
+          if ((ret = eval_location_expr (c, as, a, addr, new_loc + i, arg)) < 0)
+            return ret;
+          break;
+
+        case DWARF_WHERE_VAL_EXPR:
+          addr = rs->reg.val[i];
+          if ((ret = eval_location_expr (c, as, a, addr, new_loc + i, arg)) < 0)
+            return ret;
+          new_loc[i] = DWARF_VAL_LOC (c, DWARF_GET_LOC (new_loc[i]));
+          break;
         }
     }
 
-    c->cfa = cfa;
-    /* DWARF spec says undefined return address location means end of stack. */
-    if (DWARF_IS_NULL_LOC (c->loc[c->ret_addr_column]))
-        c->ip = 0;
-    else
+  memcpy(c->loc, new_loc, sizeof(new_loc));
+
+  c->cfa = cfa;
+  /* DWARF spec says undefined return address location means end of stack. */
+  if (DWARF_IS_NULL_LOC (c->loc[rs->ret_addr_column]))
     {
-        ret = dwarf_get (c, c->loc[c->ret_addr_column], &ip);
-        if (ret < 0)
-            return ret;
-        c->ip = ip;
+      c->ip = 0;
+      ret = 0;
+    }
+  else
+  {
+    ret = dwarf_get (c, c->loc[rs->ret_addr_column], &ip);
+    if (ret < 0)
+      return ret;
+    c->ip = ip;
+    ret = 1;
+  }
+
+  /* XXX: check for ip to be code_aligned */
+  if (c->ip == prev_ip && c->cfa == prev_cfa)
+    {
+      Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n",
+               __FUNCTION__, (long) c->ip);
+      return -UNW_EBADFRAME;
     }
 
-    /* XXX: check for ip to be code_aligned */
-    if (c->ip == prev_ip && c->cfa == prev_cfa)
-    {
-        Dprintf ("%s: ip and cfa unchanged; stopping here (ip=0x%lx)\n",
-                 __FUNCTION__, (long) c->ip);
-        return -UNW_EBADFRAME;
-    }
+  if (c->stash_frames)
+    tdep_stash_frame (c, rs);
 
-    if (c->stash_frames)
-        tdep_stash_frame (c, rs);
-
-    return 0;
+  return ret;
 }
 
+/* Find the saved locations. */
 static int
-uncached_dwarf_find_save_locs (struct dwarf_cursor *c)
+find_reg_state (struct dwarf_cursor *c, dwarf_state_record_t *sr)
 {
-    dwarf_state_record_t sr;
-    int ret;
+  dwarf_reg_state_t *rs;
+  struct dwarf_rs_cache *cache;
+  int ret = 0;
+  intrmask_t saved_mask;
 
-    if ((ret = fetch_proc_info (c, c->ip, 1)) < 0)
-        return ret;
+  if ((cache = get_rs_cache(c->as, &saved_mask)) &&
+      (rs = rs_lookup(cache, c)))
+    {
+      /* update hint; no locking needed: single-word writes are atomic */
+      unsigned short index = rs - cache->buckets;
+      c->use_prev_instr = ! cache->links[index].signal_frame;
+      memcpy (&sr->rs_current, rs, sizeof (*rs));
+    }
+  else
+    {
+      ret = fetch_proc_info (c, c->ip);
+      int next_use_prev_instr = c->use_prev_instr;
+      if (ret >= 0)
+	{
+	  /* Update use_prev_instr for the next frame. */
+	  assert(c->pi.unwind_info);
+	  struct dwarf_cie_info *dci = c->pi.unwind_info;
+	  next_use_prev_instr = ! dci->signal_frame;
+	  ret = create_state_record_for (c, sr, c->ip);
+	}
+      put_unwind_info (c, &c->pi);
+      c->use_prev_instr = next_use_prev_instr;
 
-    if ((ret = create_state_record_for (c, &sr, c->ip)) < 0)
-        return ret;
+      if (cache && ret >= 0)
+	{
+	  rs = rs_new (cache, c);
+	  cache->links[rs - cache->buckets].hint = 0;
+	  memcpy(rs, &sr->rs_current, sizeof(*rs));
+	}
+    }
 
-    if ((ret = apply_reg_state (c, &sr.rs_current)) < 0)
-        return ret;
-
-    put_unwind_info (c, &c->pi);
-    return 0;
+  unsigned short index = -1;
+  if (cache)
+    {
+      put_rs_cache (c->as, cache, &saved_mask);
+      if (rs)
+	{
+	  index = rs - cache->buckets;
+	  c->hint = cache->links[index].hint;
+	  cache->links[c->prev_rs].hint = index + 1;
+	  c->prev_rs = index;
+	}
+    }
+  if (ret < 0)
+      return ret;
+  if (cache)
+    tdep_reuse_frame (c, cache->links[index].signal_frame);
+  return 0;
 }
 
 /* The function finds the saved locations and applies the register
    state as well. */
 HIDDEN int
-dwarf_find_save_locs (struct dwarf_cursor *c)
+dwarf_step (struct dwarf_cursor *c)
 {
-    dwarf_state_record_t sr;
-    dwarf_reg_state_t *rs, rs_copy;
-    struct dwarf_rs_cache *cache;
-    int ret = 0;
-    intrmask_t saved_mask;
-
-    if (c->as->caching_policy == UNW_CACHE_NONE)
-        return uncached_dwarf_find_save_locs (c);
-
-    cache = get_rs_cache(c->as, &saved_mask);
-    rs = rs_lookup(cache, c);
-
-    if (rs)
-    {
-        c->ret_addr_column = rs->ret_addr_column;
-        c->use_prev_instr = ! rs->signal_frame;
-    }
-    else
-    {
-        if ((ret = fetch_proc_info (c, c->ip, 1)) < 0 ||
-                (ret = create_state_record_for (c, &sr, c->ip)) < 0)
-        {
-            put_rs_cache (c->as, cache, &saved_mask);
-            return ret;
-        }
-
-        rs = rs_new (cache, c);
-        memcpy(rs, &sr.rs_current, offsetof(struct dwarf_reg_state, ip));
-        cache->buckets[c->prev_rs].hint = rs - cache->buckets;
-
-        c->hint = rs->hint;
-        c->prev_rs = rs - cache->buckets;
-
-        put_unwind_info (c, &c->pi);
-    }
-
-    memcpy (&rs_copy, rs, sizeof (rs_copy));
-    put_rs_cache (c->as, cache, &saved_mask);
-
-    tdep_reuse_frame (c, &rs_copy);
-    if ((ret = apply_reg_state (c, &rs_copy)) < 0)
-        return ret;
-
-    return 0;
-}
-
-/* The proc-info must be valid for IP before this routine can be
-   called.  */
-HIDDEN int
-dwarf_create_state_record (struct dwarf_cursor *c, dwarf_state_record_t *sr)
-{
-    return create_state_record_for (c, sr, c->ip);
+  int ret;
+  dwarf_state_record_t sr;
+  if ((ret = find_reg_state (c, &sr)) < 0)
+    return ret;
+  return apply_reg_state (c, &sr.rs_current);
 }
 
 HIDDEN int
 dwarf_make_proc_info (struct dwarf_cursor *c)
 {
 #if 0
-    if (c->as->caching_policy == UNW_CACHE_NONE
-            || get_cached_proc_info (c) < 0)
+  if (c->as->caching_policy == UNW_CACHE_NONE
+      || get_cached_proc_info (c) < 0)
 #endif
-        /* Lookup it up the slow way... */
-        return fetch_proc_info (c, c->ip, 0);
-    return 0;
+  /* Need to check if current frame contains
+     args_size, and set cursor appropriately.  Only
+     needed for unw_resume */
+  dwarf_state_record_t sr;
+  int ret;
+
+  /* Lookup it up the slow way... */
+  ret = fetch_proc_info (c, c->ip);
+  if (ret >= 0)
+      ret = create_state_record_for (c, &sr, c->ip);
+  put_unwind_info (c, &c->pi);
+  if (ret < 0)
+    return ret;
+  c->args_size = sr.args_size;
+
+  return 0;
+}
+
+static int
+dwarf_reg_states_dynamic_iterate(struct dwarf_cursor *c,
+				 unw_reg_states_callback cb,
+				 void *token)
+{
+  Debug (1, "Not yet implemented\n");
+  return -UNW_ENOINFO;
+}
+
+static int
+dwarf_reg_states_table_iterate(struct dwarf_cursor *c,
+			       unw_reg_states_callback cb,
+			       void *token)
+{
+  dwarf_state_record_t sr;
+  int ret = setup_fde(c, &sr);
+  struct dwarf_cie_info *dci = c->pi.unwind_info;
+  unw_word_t addr = dci->fde_instr_start;
+  unw_word_t curr_ip = c->pi.start_ip;
+  dwarf_stackable_reg_state_t *rs_stack = NULL;
+  while (ret >= 0 && curr_ip < c->pi.end_ip && addr < dci->fde_instr_end)
+    {
+      unw_word_t prev_ip = curr_ip;
+      ret = run_cfi_program (c, &sr, &curr_ip, prev_ip, &addr, dci->fde_instr_end,
+			     &rs_stack, dci);
+      if (ret >= 0 && prev_ip < curr_ip)
+	ret = cb(token, &sr.rs_current, sizeof(sr.rs_current), prev_ip, curr_ip);
+    }
+  empty_rstate_stack(&rs_stack);
+#if defined(NEED_LAST_IP)
+  if (ret >= 0 && curr_ip < c->pi.last_ip)
+    /* report the dead zone after the procedure ends */
+    ret = cb(token, &sr.rs_current, sizeof(sr.rs_current), curr_ip, c->pi.last_ip);
+#else
+  if (ret >= 0 && curr_ip < c->pi.end_ip)
+    /* report for whatever is left before procedure end */
+    ret = cb(token, &sr.rs_current, sizeof(sr.rs_current), curr_ip, c->pi.end_ip);
+#endif
+  return ret;
+}
+
+HIDDEN int
+dwarf_reg_states_iterate(struct dwarf_cursor *c,
+			 unw_reg_states_callback cb,
+			 void *token)
+{
+  int ret = fetch_proc_info (c, c->ip);
+  int next_use_prev_instr = c->use_prev_instr;
+  if (ret >= 0)
+    {
+      /* Update use_prev_instr for the next frame. */
+      assert(c->pi.unwind_info);
+      struct dwarf_cie_info *dci = c->pi.unwind_info;
+      next_use_prev_instr = ! dci->signal_frame;
+      switch (c->pi.format)
+	{
+	case UNW_INFO_FORMAT_TABLE:
+	case UNW_INFO_FORMAT_REMOTE_TABLE:
+	  ret = dwarf_reg_states_table_iterate(c, cb, token);
+	  break;
+
+	case UNW_INFO_FORMAT_DYNAMIC:
+	  ret = dwarf_reg_states_dynamic_iterate (c, cb, token);
+	  break;
+
+	default:
+	  Debug (1, "Unexpected unwind-info format %d\n", c->pi.format);
+	  ret = -UNW_EINVAL;
+	}
+    }
+  put_unwind_info (c, &c->pi);
+  c->use_prev_instr = next_use_prev_instr;
+  return ret;
+}
+
+HIDDEN int
+dwarf_apply_reg_state (struct dwarf_cursor *c, struct dwarf_reg_state *rs)
+{
+  return apply_reg_state(c, rs);
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Gpe.c b/sgx_unwind/libunwind/src/dwarf/Gpe.c
index f42cff2..a0e37ba 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gpe.c
+++ b/sgx_unwind/libunwind/src/dwarf/Gpe.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -34,6 +34,6 @@
                             const unw_proc_info_t *pi,
                             unw_word_t *valp, void *arg)
 {
-    return dwarf_read_encoded_pointer_inlined (as, a, addr, encoding,
-            pi, valp, arg);
+  return dwarf_read_encoded_pointer_inlined (as, a, addr, encoding,
+                                             pi, valp, arg);
 }
diff --git a/sgx_unwind/libunwind/src/dwarf/Lstep.c b/sgx_unwind/libunwind/src/dwarf/Lstep.c
deleted file mode 100644
index c1ac3c7..0000000
--- a/sgx_unwind/libunwind/src/dwarf/Lstep.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#define UNW_LOCAL_ONLY
-#include <libunwind.h>
-#if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
-#include "Gstep.c"
-#endif
diff --git a/sgx_unwind/libunwind/src/dwarf/global.c b/sgx_unwind/libunwind/src/dwarf/global.c
index d28fe52..7098507 100644
--- a/sgx_unwind/libunwind/src/dwarf/global.c
+++ b/sgx_unwind/libunwind/src/dwarf/global.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003-2004 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -31,7 +31,7 @@
 HIDDEN int
 dwarf_init (void)
 {
-    mempool_init (&dwarf_reg_state_pool, sizeof (dwarf_reg_state_t), 0);
-    mempool_init (&dwarf_cie_info_pool, sizeof (struct dwarf_cie_info), 0);
-    return 0;
+  mempool_init (&dwarf_reg_state_pool, sizeof (dwarf_stackable_reg_state_t), 0);
+  mempool_init (&dwarf_cie_info_pool, sizeof (struct dwarf_cie_info), 0);
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/elf32.h b/sgx_unwind/libunwind/src/elf32.h
index 63fef83..2c7bca4 100644
--- a/sgx_unwind/libunwind/src/elf32.h
+++ b/sgx_unwind/libunwind/src/elf32.h
@@ -2,7 +2,7 @@
 #define elf32_h
 
 #ifndef ELF_CLASS
-#define ELF_CLASS	ELFCLASS32
+#define ELF_CLASS       ELFCLASS32
 #endif
 #include "elfxx.h"
 
diff --git a/sgx_unwind/libunwind/src/elf64.h b/sgx_unwind/libunwind/src/elf64.h
index fd10ed8..091fba8 100644
--- a/sgx_unwind/libunwind/src/elf64.h
+++ b/sgx_unwind/libunwind/src/elf64.h
@@ -2,7 +2,7 @@
 #define elf64_h
 
 #ifndef ELF_CLASS
-#define ELF_CLASS	ELFCLASS64
+#define ELF_CLASS       ELFCLASS64
 #endif
 #include "elfxx.h"
 
diff --git a/sgx_unwind/libunwind/src/elfxx.c b/sgx_unwind/libunwind/src/elfxx.c
index 9ba3b74..b03dfcb 100644
--- a/sgx_unwind/libunwind/src/elfxx.c
+++ b/sgx_unwind/libunwind/src/elfxx.c
@@ -1,7 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2005 Hewlett-Packard Co
    Copyright (C) 2007 David Mosberger-Tang
-	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
 
 This file is part of libunwind.
 
@@ -24,103 +24,244 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
+#include "libunwind_i.h"
+
 #include <stdio.h>
 #include <sys/param.h>
 
-#include "libunwind_i.h"
+#ifdef HAVE_LZMA
+#include <lzma.h>
+#endif /* HAVE_LZMA */
+
+static Elf_W (Shdr)*
+elf_w (section_table) (struct elf_image *ei)
+{
+  Elf_W (Ehdr) *ehdr = ei->image;
+  Elf_W (Off) soff;
+
+  soff = ehdr->e_shoff;
+  if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
+    {
+      Debug (1, "section table outside of image? (%lu > %lu)\n",
+             (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
+             (unsigned long) ei->size);
+      return NULL;
+    }
+
+  return (Elf_W (Shdr) *) ((char *) ei->image + soff);
+}
+
+static char*
+elf_w (string_table) (struct elf_image *ei, int section)
+{
+  Elf_W (Ehdr) *ehdr = ei->image;
+  Elf_W (Off) soff, str_soff;
+  Elf_W (Shdr) *str_shdr;
+
+  /* this offset is assumed to be OK */
+  soff = ehdr->e_shoff;
+
+  str_soff = soff + (section * ehdr->e_shentsize);
+  if (str_soff + ehdr->e_shentsize > ei->size)
+    {
+      Debug (1, "string shdr table outside of image? (%lu > %lu)\n",
+             (unsigned long) (str_soff + ehdr->e_shentsize),
+             (unsigned long) ei->size);
+      return NULL;
+    }
+  str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
+
+  if (str_shdr->sh_offset + str_shdr->sh_size > ei->size)
+    {
+      Debug (1, "string table outside of image? (%lu > %lu)\n",
+             (unsigned long) (str_shdr->sh_offset + str_shdr->sh_size),
+             (unsigned long) ei->size);
+      return NULL;
+    }
+
+  Debug (16, "strtab=0x%lx\n", (long) str_shdr->sh_offset);
+  return ei->image + str_shdr->sh_offset;
+}
 
 static int
 elf_w (lookup_symbol) (unw_addr_space_t as,
                        unw_word_t ip, struct elf_image *ei,
                        Elf_W (Addr) load_offset,
-                       char *buf, size_t buf_len, unw_word_t *offp)
+                       char *buf, size_t buf_len, Elf_W (Addr) *min_dist)
 {
-    size_t syment_size;
-    Elf_W (Ehdr) *ehdr = ei->image;
-    Elf_W (Sym) *sym, *symtab, *symtab_end;
-    Elf_W (Off) soff, str_soff;
-    Elf_W (Shdr) *shdr, *str_shdr;
-    Elf_W (Addr) val, min_dist = ~(Elf_W (Addr))0;
-    int i, ret = 0;
-    char *strtab;
+  size_t syment_size;
+  Elf_W (Ehdr) *ehdr = ei->image;
+  Elf_W (Sym) *sym, *symtab, *symtab_end;
+  Elf_W (Shdr) *shdr;
+  Elf_W (Addr) val;
+  int i, ret = -UNW_ENOINFO;
+  char *strtab;
 
-    if (!elf_w (valid_object) (ei))
-        return -UNW_ENOINFO;
+  if (!elf_w (valid_object) (ei))
+    return -UNW_ENOINFO;
 
-    soff = ehdr->e_shoff;
-    if (soff + ehdr->e_shnum * ehdr->e_shentsize > ei->size)
+  shdr = elf_w (section_table) (ei);
+  if (!shdr)
+    return -UNW_ENOINFO;
+
+  for (i = 0; i < ehdr->e_shnum; ++i)
     {
-        Debug (1, "section table outside of image? (%lu > %lu)\n",
-               (unsigned long) (soff + ehdr->e_shnum * ehdr->e_shentsize),
-               (unsigned long) ei->size);
-        return -UNW_ENOINFO;
-    }
-
-    shdr = (Elf_W (Shdr) *) ((char *) ei->image + soff);
-
-    for (i = 0; i < ehdr->e_shnum; ++i)
-    {
-        switch (shdr->sh_type)
+      switch (shdr->sh_type)
         {
         case SHT_SYMTAB:
         case SHT_DYNSYM:
-            symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
-            symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
-            syment_size = shdr->sh_entsize;
+          symtab = (Elf_W (Sym) *) ((char *) ei->image + shdr->sh_offset);
+          symtab_end = (Elf_W (Sym) *) ((char *) symtab + shdr->sh_size);
+          syment_size = shdr->sh_entsize;
 
-            str_soff = soff + (shdr->sh_link * ehdr->e_shentsize);
-            if (str_soff + ehdr->e_shentsize >= ei->size)
+          strtab = elf_w (string_table) (ei, shdr->sh_link);
+          if (!strtab)
+            break;
+
+          Debug (16, "symtab=0x%lx[%d]\n",
+                 (long) shdr->sh_offset, shdr->sh_type);
+
+          for (sym = symtab;
+               sym < symtab_end;
+               sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
             {
-                Debug (1, "string table outside of image? (%lu >= %lu)\n",
-                       (unsigned long) (str_soff + ehdr->e_shentsize),
-                       (unsigned long) ei->size);
-                break;
-            }
-            str_shdr = (Elf_W (Shdr) *) ((char *) ei->image + str_soff);
-            strtab = (char *) ei->image + str_shdr->sh_offset;
-
-            Debug (16, "symtab=0x%lx[%d], strtab=0x%lx\n",
-                   (long) shdr->sh_offset, shdr->sh_type,
-                   (long) str_shdr->sh_offset);
-
-            for (sym = symtab;
-                    sym < symtab_end;
-                    sym = (Elf_W (Sym) *) ((char *) sym + syment_size))
-            {
-                if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
-                        && sym->st_shndx != SHN_UNDEF)
+              if (ELF_W (ST_TYPE) (sym->st_info) == STT_FUNC
+                  && sym->st_shndx != SHN_UNDEF)
                 {
-                    if (tdep_get_func_addr (as, sym->st_value, &val) < 0)
-                        continue;
-                    if (sym->st_shndx != SHN_ABS)
-                        val += load_offset;
-                    Debug (16, "0x%016lx info=0x%02x %s\n",
-                           (long) val, sym->st_info, strtab + sym->st_name);
+                  val = sym->st_value;
+                  if (sym->st_shndx != SHN_ABS)
+                    val += load_offset;
+                  if (tdep_get_func_addr (as, val, &val) < 0)
+                    continue;
+                  Debug (16, "0x%016lx info=0x%02x %s\n",
+                         (long) val, sym->st_info, strtab + sym->st_name);
 
-                    if ((Elf_W (Addr)) (ip - val) < min_dist)
+                  if ((Elf_W (Addr)) (ip - val) < *min_dist)
                     {
-                        min_dist = (Elf_W (Addr)) (ip - val);
-                        strncpy (buf, strtab + sym->st_name, buf_len);
-                        buf[buf_len - 1] = '\0';
-                        if (strlen (strtab + sym->st_name) >= buf_len)
-                            ret = -UNW_ENOMEM;
+                      *min_dist = (Elf_W (Addr)) (ip - val);
+                      strncpy (buf, strtab + sym->st_name, buf_len);
+                      buf[buf_len - 1] = '\0';
+                      ret = (strlen (strtab + sym->st_name) >= buf_len
+                             ? -UNW_ENOMEM : 0);
                     }
                 }
             }
-            break;
+          break;
 
         default:
-            break;
+          break;
         }
-        shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
+      shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
     }
-    if (min_dist >= ei->size)
-        return -UNW_ENOINFO;		/* not found */
-    if (offp)
-        *offp = min_dist;
-    return ret;
+  return ret;
 }
 
+static Elf_W (Addr)
+elf_w (get_load_offset) (struct elf_image *ei, unsigned long segbase,
+                         unsigned long mapoff)
+{
+  Elf_W (Addr) offset = 0;
+  Elf_W (Ehdr) *ehdr;
+  Elf_W (Phdr) *phdr;
+  int i;
+
+  ehdr = ei->image;
+  phdr = (Elf_W (Phdr) *) ((char *) ei->image + ehdr->e_phoff);
+
+  for (i = 0; i < ehdr->e_phnum; ++i)
+    if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
+      {
+        offset = segbase - phdr[i].p_vaddr;
+        break;
+      }
+
+  return offset;
+}
+
+#if HAVE_LZMA
+static size_t
+xz_uncompressed_size (uint8_t *compressed, size_t length)
+{
+  uint64_t memlimit = UINT64_MAX;
+  size_t ret = 0, pos = 0;
+  lzma_stream_flags options;
+  lzma_index *index;
+
+  if (length < LZMA_STREAM_HEADER_SIZE)
+    return 0;
+
+  uint8_t *footer = compressed + length - LZMA_STREAM_HEADER_SIZE;
+  if (lzma_stream_footer_decode (&options, footer) != LZMA_OK)
+    return 0;
+
+  if (length < LZMA_STREAM_HEADER_SIZE + options.backward_size)
+    return 0;
+
+  uint8_t *indexdata = footer - options.backward_size;
+  if (lzma_index_buffer_decode (&index, &memlimit, NULL, indexdata,
+                                &pos, options.backward_size) != LZMA_OK)
+    return 0;
+
+  if (lzma_index_size (index) == options.backward_size)
+    {
+      ret = lzma_index_uncompressed_size (index);
+    }
+
+  lzma_index_end (index, NULL);
+  return ret;
+}
+
+static int
+elf_w (extract_minidebuginfo) (struct elf_image *ei, struct elf_image *mdi)
+{
+  Elf_W (Shdr) *shdr;
+  uint8_t *compressed = NULL;
+  uint64_t memlimit = UINT64_MAX; /* no memory limit */
+  size_t compressed_len, uncompressed_len;
+
+  shdr = elf_w (find_section) (ei, ".gnu_debugdata");
+  if (!shdr)
+    return 0;
+
+  compressed = ((uint8_t *) ei->image) + shdr->sh_offset;
+  compressed_len = shdr->sh_size;
+
+  uncompressed_len = xz_uncompressed_size (compressed, compressed_len);
+  if (uncompressed_len == 0)
+    {
+      Debug (1, "invalid .gnu_debugdata contents\n");
+      return 0;
+    }
+
+  mdi->size = uncompressed_len;
+  mdi->image = mmap (NULL, uncompressed_len, PROT_READ|PROT_WRITE,
+                     MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+  if (mdi->image == MAP_FAILED)
+    return 0;
+
+  size_t in_pos = 0, out_pos = 0;
+  lzma_ret lret;
+  lret = lzma_stream_buffer_decode (&memlimit, 0, NULL,
+                                    compressed, &in_pos, compressed_len,
+                                    mdi->image, &out_pos, mdi->size);
+  if (lret != LZMA_OK)
+    {
+      Debug (1, "LZMA decompression failed: %d\n", lret);
+      munmap (mdi->image, mdi->size);
+      return 0;
+    }
+
+  return 1;
+}
+#else
+static int
+elf_w (extract_minidebuginfo) (struct elf_image *ei, struct elf_image *mdi)
+{
+  return 0;
+}
+#endif /* !HAVE_LZMA */
+
 /* Find the ELF image that contains IP and return the "closest"
    procedure name, if there is one.  With some caching, this could be
    sped up greatly, but until an application materializes that's
@@ -128,47 +269,213 @@
 
 HIDDEN int
 elf_w (get_proc_name_in_image) (unw_addr_space_t as, struct elf_image *ei,
-                                unsigned long segbase,
-                                unsigned long mapoff,
-                                unw_word_t ip,
-                                char *buf, size_t buf_len, unw_word_t *offp)
+                       unsigned long segbase,
+                       unsigned long mapoff,
+                       unw_word_t ip,
+                       char *buf, size_t buf_len, unw_word_t *offp)
 {
-    Elf_W (Addr) load_offset = 0;
-    Elf_W (Ehdr) *ehdr;
-    Elf_W (Phdr) *phdr;
-    int i, ret;
+  Elf_W (Addr) load_offset;
+  Elf_W (Addr) min_dist = ~(Elf_W (Addr))0;
+  int ret;
 
-    ehdr = ei->image;
-    phdr = (Elf_W (Phdr) *) ((char *) ei->image + ehdr->e_phoff);
+  load_offset = elf_w (get_load_offset) (ei, segbase, mapoff);
+  ret = elf_w (lookup_symbol) (as, ip, ei, load_offset, buf, buf_len, &min_dist);
 
-    for (i = 0; i < ehdr->e_phnum; ++i)
-        if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset == mapoff)
+  /* If the ELF image has MiniDebugInfo embedded in it, look up the symbol in
+     there as well and replace the previously found if it is closer. */
+  struct elf_image mdi;
+  if (elf_w (extract_minidebuginfo) (ei, &mdi))
+    {
+      int ret_mdi = elf_w (lookup_symbol) (as, ip, &mdi, load_offset, buf,
+                                           buf_len, &min_dist);
+
+      /* Closer symbol was found (possibly truncated). */
+      if (ret_mdi == 0 || ret_mdi == -UNW_ENOMEM)
         {
-            load_offset = segbase - phdr[i].p_vaddr;
-            break;
+          ret = ret_mdi;
         }
 
-    ret = elf_w (lookup_symbol) (as, ip, ei, load_offset, buf, buf_len, offp);
+      munmap (mdi.image, mdi.size);
+    }
 
-    return ret;
+  if (min_dist >= ei->size)
+    return -UNW_ENOINFO;                /* not found */
+  if (offp)
+    *offp = min_dist;
+  return ret;
 }
 
 HIDDEN int
 elf_w (get_proc_name) (unw_addr_space_t as, pid_t pid, unw_word_t ip,
                        char *buf, size_t buf_len, unw_word_t *offp)
 {
-    unsigned long segbase, mapoff;
-    struct elf_image ei;
-    int ret;
+  unsigned long segbase, mapoff;
+  struct elf_image ei;
+  int ret;
+  char file[PATH_MAX];
 
-    ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff, NULL, 0);
-    if (ret < 0)
-        return ret;
-
-    ret = elf_w (get_proc_name_in_image) (as, &ei, segbase, mapoff, ip, buf, buf_len, offp);
-
-    munmap (ei.image, ei.size);
-    ei.image = NULL;
-
+  ret = tdep_get_elf_image (&ei, pid, ip, &segbase, &mapoff, file, PATH_MAX);
+  if (ret < 0)
     return ret;
+
+  ret = elf_w (load_debuglink) (file, &ei, 1);
+  if (ret < 0)
+    return ret;
+
+  ret = elf_w (get_proc_name_in_image) (as, &ei, segbase, mapoff, ip, buf, buf_len, offp);
+
+  munmap (ei.image, ei.size);
+  ei.image = NULL;
+
+  return ret;
+}
+
+HIDDEN Elf_W (Shdr)*
+elf_w (find_section) (struct elf_image *ei, const char* secname)
+{
+  Elf_W (Ehdr) *ehdr = ei->image;
+  Elf_W (Shdr) *shdr;
+  char *strtab;
+  int i;
+
+  if (!elf_w (valid_object) (ei))
+    return 0;
+
+  shdr = elf_w (section_table) (ei);
+  if (!shdr)
+    return 0;
+
+  strtab = elf_w (string_table) (ei, ehdr->e_shstrndx);
+  if (!strtab)
+    return 0;
+
+  for (i = 0; i < ehdr->e_shnum; ++i)
+    {
+      if (strcmp (strtab + shdr->sh_name, secname) == 0)
+        {
+          if (shdr->sh_offset + shdr->sh_size > ei->size)
+            {
+              Debug (1, "section \"%s\" outside image? (0x%lu > 0x%lu)\n",
+                     secname,
+                     (unsigned long) shdr->sh_offset + shdr->sh_size,
+                     (unsigned long) ei->size);
+              return 0;
+            }
+
+          Debug (16, "found section \"%s\" at 0x%lx\n",
+                 secname, (unsigned long) shdr->sh_offset);
+          return shdr;
+        }
+
+      shdr = (Elf_W (Shdr) *) (((char *) shdr) + ehdr->e_shentsize);
+    }
+
+  /* section not found */
+  return 0;
+}
+
+/* Load a debug section, following .gnu_debuglink if appropriate
+ * Loads ei from file if not already mapped.
+ * If is_local, will also search sys directories /usr/local/dbg
+ *
+ * Returns 0 on success, failure otherwise.
+ * ei will be mapped to file or the located .gnu_debuglink from file
+ */
+HIDDEN int
+elf_w (load_debuglink) (const char* file, struct elf_image *ei, int is_local)
+{
+  int ret;
+  Elf_W (Shdr) *shdr;
+  Elf_W (Ehdr) *prev_image;
+  off_t prev_size;
+
+  if (!ei->image)
+    {
+      ret = elf_map_image(ei, file);
+      if (ret)
+	return ret;
+    }
+
+  prev_image = ei->image;
+  prev_size = ei->size;
+
+  /* Ignore separate debug files which contain a .gnu_debuglink section. */
+  if (is_local == -1) {
+    return 0;
+  }
+
+  shdr = elf_w (find_section) (ei, ".gnu_debuglink");
+  if (shdr) {
+    if (shdr->sh_size >= PATH_MAX ||
+	(shdr->sh_offset + shdr->sh_size > ei->size))
+      {
+	return 0;
+      }
+
+    {
+      char linkbuf[shdr->sh_size];
+      char *link = ((char *) ei->image) + shdr->sh_offset;
+      char *p;
+      static const char *debugdir = "/usr/lib/debug";
+      char basedir[strlen(file) + 1];
+      char newname[shdr->sh_size + strlen (debugdir) + strlen (file) + 9];
+
+      memcpy(linkbuf, link, shdr->sh_size);
+
+      if (memchr (linkbuf, 0, shdr->sh_size) == NULL)
+	return 0;
+
+      ei->image = NULL;
+
+      Debug(1, "Found debuglink section, following %s\n", linkbuf);
+
+      p = strrchr (file, '/');
+      if (p != NULL)
+	{
+	  memcpy (basedir, file, p - file);
+	  basedir[p - file] = '\0';
+	}
+      else
+	basedir[0] = 0;
+
+      strcpy (newname, basedir);
+      strcat (newname, "/");
+      strcat (newname, linkbuf);
+      ret = elf_w (load_debuglink) (newname, ei, -1);
+
+      if (ret == -1)
+	{
+	  strcpy (newname, basedir);
+	  strcat (newname, "/.debug/");
+	  strcat (newname, linkbuf);
+	  ret = elf_w (load_debuglink) (newname, ei, -1);
+	}
+
+      if (ret == -1 && is_local == 1)
+	{
+	  strcpy (newname, debugdir);
+	  strcat (newname, basedir);
+	  strcat (newname, "/");
+	  strcat (newname, linkbuf);
+	  ret = elf_w (load_debuglink) (newname, ei, -1);
+	}
+
+      if (ret == -1)
+        {
+          /* No debuglink file found even though .gnu_debuglink existed */
+          ei->image = prev_image;
+          ei->size = prev_size;
+
+          return 0;
+        }
+      else
+        {
+          munmap (prev_image, prev_size);
+        }
+
+      return ret;
+    }
+  }
+
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/elfxx.h b/sgx_unwind/libunwind/src/elfxx.h
index 2bbc9d0..40efc9a 100644
--- a/sgx_unwind/libunwind/src/elfxx.h
+++ b/sgx_unwind/libunwind/src/elfxx.h
@@ -1,7 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003, 2005 Hewlett-Packard Co
    Copyright (C) 2007 David Mosberger-Tang
-	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
 
 This file is part of libunwind.
 
@@ -24,85 +24,80 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include <elf.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #include <sys/mman.h>
 #include <sys/stat.h>
 
-#if ELF_CLASS == ELFCLASS32
-# define ELF_W(x)	ELF32_##x
-# define Elf_W(x)	Elf32_##x
-# define elf_w(x)	_Uelf32_##x
-#else
-# define ELF_W(x)	ELF64_##x
-# define Elf_W(x)	Elf64_##x
-# define elf_w(x)	_Uelf64_##x
-#endif
-
 #include "libunwind_i.h"
 
+#if ELF_CLASS == ELFCLASS32
+# define ELF_W(x)       ELF32_##x
+# define Elf_W(x)       Elf32_##x
+# define elf_w(x)       _Uelf32_##x
+#else
+# define ELF_W(x)       ELF64_##x
+# define Elf_W(x)       Elf64_##x
+# define elf_w(x)       _Uelf64_##x
+#endif
+
 extern int elf_w (get_proc_name) (unw_addr_space_t as,
                                   pid_t pid, unw_word_t ip,
                                   char *buf, size_t len,
                                   unw_word_t *offp);
 
 extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as,
-        struct elf_image *ei,
-        unsigned long segbase,
-        unsigned long mapoff,
-        unw_word_t ip,
-        char *buf, size_t buf_len, unw_word_t *offp);
+                                           struct elf_image *ei,
+                                           unsigned long segbase,
+                                           unsigned long mapoff,
+                                           unw_word_t ip,
+                                           char *buf, size_t buf_len, unw_word_t *offp);
 
-extern int elf_w (get_proc_name) (unw_addr_space_t as,
-                                  pid_t pid, unw_word_t ip,
-                                  char *buf, size_t len,
-                                  unw_word_t *offp);
+extern Elf_W (Shdr)* elf_w (find_section) (struct elf_image *ei, const char* secname);
+extern int elf_w (load_debuglink) (const char* file, struct elf_image *ei, int is_local);
 
+#ifndef HAVE_SGX
 static inline int
 elf_w (valid_object) (struct elf_image *ei)
 {
-    if (ei->size <= EI_VERSION)
-        return 0;
+  if (ei->size <= EI_VERSION)
+    return 0;
 
-    return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
-            && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS
-            && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
-            && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
+  return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
+          && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS
+          && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
+          && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
 }
 
 static inline int
 elf_map_image (struct elf_image *ei, const char *path)
 {
-#if HAVE_SGX
+  struct stat stat;
+  int fd;
+
+  fd = open (path, O_RDONLY);
+  if (fd < 0)
     return -1;
-#else
-    struct stat stat;
-    int fd;
 
-    fd = open (path, O_RDONLY);
-    if (fd < 0)
-        return -1;
-
-    if (fstat (fd, &stat) < 0)
+  if (fstat (fd, &stat) < 0)
     {
-        close (fd);
-        return -1;
+      close (fd);
+      return -1;
     }
 
-    ei->size = stat.st_size;
-    ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
-    close (fd);
-    if (ei->image == MAP_FAILED)
-        return -1;
+  ei->size = stat.st_size;
+  ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
+  close (fd);
+  if (ei->image == MAP_FAILED)
+    return -1;
 
-    if (!elf_w (valid_object) (ei))
-    {
-        munmap(ei->image, ei->size);
-        return -1;
-    }
+  if (!elf_w (valid_object) (ei))
+  {
+    munmap(ei->image, ei->size);
+    return -1;
+  }
 
-    return 0;
-#endif
+  return 0;
 }
+#endif
diff --git a/sgx_unwind/libunwind/src/libunwind-generic.pc.in b/sgx_unwind/libunwind/src/libunwind-generic.pc.in
new file mode 100644
index 0000000..1f3baff
--- /dev/null
+++ b/sgx_unwind/libunwind/src/libunwind-generic.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libunwind-generic
+Description: libunwind generic library
+Version: @VERSION@
+Requires: libunwind
+Libs: -L${libdir} -lunwind-generic
+Cflags: -I${includedir}
diff --git a/sgx_unwind/libunwind/src/mi/Gdestroy_addr_space.c b/sgx_unwind/libunwind/src/mi/Gdestroy_addr_space.c
index 22276c7..504558e 100644
--- a/sgx_unwind/libunwind/src/mi/Gdestroy_addr_space.c
+++ b/sgx_unwind/libunwind/src/mi/Gdestroy_addr_space.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,13 +25,13 @@
 
 #include "libunwind_i.h"
 
-PROTECTED void
+void
 unw_destroy_addr_space (unw_addr_space_t as)
 {
 #ifndef UNW_LOCAL_ONLY
 # if UNW_DEBUG
-    memset (as, 0, sizeof (*as));
+  memset (as, 0, sizeof (*as));
 # endif
-    free (as);
+  free (as);
 #endif
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gdyn-extract.c b/sgx_unwind/libunwind/src/mi/Gdyn-extract.c
index 490cb01..5f7682e 100644
--- a/sgx_unwind/libunwind/src/mi/Gdyn-extract.c
+++ b/sgx_unwind/libunwind/src/mi/Gdyn-extract.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -30,33 +30,35 @@
                                 unw_proc_info_t *pi, unw_dyn_info_t *di,
                                 int need_unwind_info, void *arg)
 {
-    pi->start_ip = di->start_ip;
-    pi->end_ip = di->end_ip;
-    pi->gp = di->gp;
-    pi->format = di->format;
-    switch (di->format)
+  pi->start_ip = di->start_ip;
+  pi->end_ip = di->end_ip;
+  pi->gp = di->gp;
+  pi->format = di->format;
+  switch (di->format)
     {
     case UNW_INFO_FORMAT_DYNAMIC:
-        pi->handler = di->u.pi.handler;
-        pi->lsda = 0;
-        pi->flags = di->u.pi.flags;
-        pi->unwind_info_size = 0;
-        if (need_unwind_info)
-            pi->unwind_info = di;
-        else
-            pi->unwind_info = NULL;
-        return 0;
+      pi->handler = di->u.pi.handler;
+      pi->lsda = 0;
+      pi->flags = di->u.pi.flags;
+      pi->unwind_info_size = 0;
+      if (need_unwind_info)
+        pi->unwind_info = di;
+      else
+        pi->unwind_info = NULL;
+      return 0;
 
     case UNW_INFO_FORMAT_TABLE:
     case UNW_INFO_FORMAT_REMOTE_TABLE:
+    case UNW_INFO_FORMAT_ARM_EXIDX:
+    case UNW_INFO_FORMAT_IP_OFFSET:
 #ifdef tdep_search_unwind_table
-        /* call platform-specific search routine: */
-        return tdep_search_unwind_table (as, ip, di, pi, need_unwind_info, arg);
+      /* call platform-specific search routine: */
+      return tdep_search_unwind_table (as, ip, di, pi, need_unwind_info, arg);
 #else
-        /* fall through */
+      /* fall through */
 #endif
     default:
-        break;
+      break;
     }
-    return -UNW_EINVAL;
+  return -UNW_EINVAL;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gdyn-remote.c b/sgx_unwind/libunwind/src/mi/Gdyn-remote.c
index a3ccb98..40a5ad8 100644
--- a/sgx_unwind/libunwind/src/mi/Gdyn-remote.c
+++ b/sgx_unwind/libunwind/src/mi/Gdyn-remote.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -31,69 +31,69 @@
 static void
 free_regions (unw_dyn_region_info_t *region)
 {
-    if (region->next)
-        free_regions (region->next);
-    free (region);
+  if (region->next)
+    free_regions (region->next);
+  free (region);
 }
 
 static int
 intern_op (unw_addr_space_t as, unw_accessors_t *a, unw_word_t *addr,
            unw_dyn_op_t *op, void *arg)
 {
-    int ret;
+  int ret;
 
-    if ((ret = fetch8 (as, a, addr, &op->tag, arg)) < 0
-            || (ret = fetch8 (as, a, addr, &op->qp, arg)) < 0
-            || (ret = fetch16 (as, a, addr, &op->reg, arg)) < 0
-            || (ret = fetch32 (as, a, addr, &op->when, arg)) < 0
-            || (ret = fetchw  (as, a, addr, &op->val, arg)) < 0)
-        return ret;
-    return 0;
+  if ((ret = fetch8 (as, a, addr, &op->tag, arg)) < 0
+      || (ret = fetch8 (as, a, addr, &op->qp, arg)) < 0
+      || (ret = fetch16 (as, a, addr, &op->reg, arg)) < 0
+      || (ret = fetch32 (as, a, addr, &op->when, arg)) < 0
+      || (ret = fetchw  (as, a, addr, &op->val, arg)) < 0)
+    return ret;
+  return 0;
 }
 
 static int
 intern_regions (unw_addr_space_t as, unw_accessors_t *a,
                 unw_word_t *addr, unw_dyn_region_info_t **regionp, void *arg)
 {
-    uint32_t insn_count, op_count, i;
-    unw_dyn_region_info_t *region;
-    unw_word_t next_addr;
-    int ret;
+  uint32_t insn_count, op_count, i;
+  unw_dyn_region_info_t *region;
+  unw_word_t next_addr;
+  int ret;
 
-    *regionp = NULL;
+  *regionp = NULL;
 
-    if (!*addr)
-        return 0;	/* NULL region-list */
+  if (!*addr)
+    return 0;   /* NULL region-list */
 
-    if ((ret = fetchw (as, a, addr, &next_addr, arg)) < 0
-            || (ret = fetch32 (as, a, addr, (int32_t *) &insn_count, arg)) < 0
-            || (ret = fetch32 (as, a, addr, (int32_t *) &op_count, arg)) < 0)
-        return ret;
+  if ((ret = fetchw (as, a, addr, &next_addr, arg)) < 0
+      || (ret = fetch32 (as, a, addr, (int32_t *) &insn_count, arg)) < 0
+      || (ret = fetch32 (as, a, addr, (int32_t *) &op_count, arg)) < 0)
+    return ret;
 
-    region = calloc (1, _U_dyn_region_info_size (op_count));
-    if (!region)
+  region = calloc (1, _U_dyn_region_info_size (op_count));
+  if (!region)
     {
-        ret = -UNW_ENOMEM;
-        goto out;
+      ret = -UNW_ENOMEM;
+      goto out;
     }
 
-    region->insn_count = insn_count;
-    region->op_count = op_count;
-    for (i = 0; i < op_count; ++i)
-        if ((ret = intern_op (as, a, addr, region->op + i, arg)) < 0)
-            goto out;
+  region->insn_count = insn_count;
+  region->op_count = op_count;
+  for (i = 0; i < op_count; ++i)
+    if ((ret = intern_op (as, a, addr, region->op + i, arg)) < 0)
+      goto out;
 
-    if (next_addr)
-        if ((ret = intern_regions (as, a, &next_addr, &region->next, arg)) < 0)
-            goto out;
+  if (next_addr)
+    if ((ret = intern_regions (as, a, &next_addr, &region->next, arg)) < 0)
+      goto out;
 
-    *regionp = region;
-    return 0;
+  *regionp = region;
+  return 0;
 
-out:
-    if (region)
-        free_regions (region);
-    return ret;
+ out:
+  if (region)
+    free_regions (region);
+  return ret;
 }
 
 static int
@@ -101,52 +101,52 @@
               unw_word_t *addr, unw_word_t table_len, unw_word_t **table_data,
               void *arg)
 {
-    unw_word_t i, *data = calloc (table_len, WSIZE);
-    int ret = 0;
+  unw_word_t i, *data = calloc (table_len, WSIZE);
+  int ret = 0;
 
-    if (!data)
+  if (!data)
     {
-        ret = -UNW_ENOMEM;
-        goto out;
+      ret = -UNW_ENOMEM;
+      goto out;
     }
 
-    for (i = 0; i < table_len; ++i)
-        if (fetchw (as, a, addr, data + i, arg) < 0)
-            goto out;
+  for (i = 0; i < table_len; ++i)
+    if (fetchw (as, a, addr, data + i, arg) < 0)
+      goto out;
 
-    *table_data = data;
-    return 0;
+  *table_data = data;
+  return 0;
 
-out:
-    if (data)
-        free (data);
-    return ret;
+ out:
+  if (data)
+    free (data);
+  return ret;
 }
 
 static void
 free_dyn_info (unw_dyn_info_t *di)
 {
-    switch (di->format)
+  switch (di->format)
     {
     case UNW_INFO_FORMAT_DYNAMIC:
-        if (di->u.pi.regions)
+      if (di->u.pi.regions)
         {
-            free_regions (di->u.pi.regions);
-            di->u.pi.regions = NULL;
+          free_regions (di->u.pi.regions);
+          di->u.pi.regions = NULL;
         }
-        break;
+      break;
 
     case UNW_INFO_FORMAT_TABLE:
-        if (di->u.ti.table_data)
+      if (di->u.ti.table_data)
         {
-            free (di->u.ti.table_data);
-            di->u.ti.table_data = NULL;
+          free (di->u.ti.table_data);
+          di->u.ti.table_data = NULL;
         }
-        break;
+      break;
 
     case UNW_INFO_FORMAT_REMOTE_TABLE:
     default:
-        break;
+      break;
     }
 }
 
@@ -154,50 +154,50 @@
 intern_dyn_info (unw_addr_space_t as, unw_accessors_t *a,
                  unw_word_t *addr, unw_dyn_info_t *di, void *arg)
 {
-    unw_word_t first_region;
-    int ret;
+  unw_word_t first_region;
+  int ret;
 
-    switch (di->format)
+  switch (di->format)
     {
     case UNW_INFO_FORMAT_DYNAMIC:
-        if ((ret = fetchw (as, a, addr, &di->u.pi.name_ptr, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.pi.handler, arg)) < 0
-                || (ret = fetch32 (as, a, addr,
-                                   (int32_t *) &di->u.pi.flags, arg)) < 0)
-            goto out;
-        *addr += 4;	/* skip over pad0 */
-        if ((ret = fetchw (as, a, addr, &first_region, arg)) < 0
-                || (ret = intern_regions (as, a, &first_region, &di->u.pi.regions,
-                                          arg)) < 0)
-            goto out;
-        break;
+      if ((ret = fetchw (as, a, addr, &di->u.pi.name_ptr, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.pi.handler, arg)) < 0
+          || (ret = fetch32 (as, a, addr,
+                             (int32_t *) &di->u.pi.flags, arg)) < 0)
+        goto out;
+      *addr += 4;       /* skip over pad0 */
+      if ((ret = fetchw (as, a, addr, &first_region, arg)) < 0
+          || (ret = intern_regions (as, a, &first_region, &di->u.pi.regions,
+                                    arg)) < 0)
+        goto out;
+      break;
 
     case UNW_INFO_FORMAT_TABLE:
-        if ((ret = fetchw (as, a, addr, &di->u.ti.name_ptr, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.ti.segbase, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.ti.table_len, arg)) < 0
-                || (ret = intern_array (as, a, addr, di->u.ti.table_len,
-                                        &di->u.ti.table_data, arg)) < 0)
-            goto out;
-        break;
+      if ((ret = fetchw (as, a, addr, &di->u.ti.name_ptr, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.ti.segbase, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.ti.table_len, arg)) < 0
+          || (ret = intern_array (as, a, addr, di->u.ti.table_len,
+                                  &di->u.ti.table_data, arg)) < 0)
+        goto out;
+      break;
 
     case UNW_INFO_FORMAT_REMOTE_TABLE:
-        if ((ret = fetchw (as, a, addr, &di->u.rti.name_ptr, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.rti.segbase, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.rti.table_len, arg)) < 0
-                || (ret = fetchw (as, a, addr, &di->u.rti.table_data, arg)) < 0)
-            goto out;
-        break;
+      if ((ret = fetchw (as, a, addr, &di->u.rti.name_ptr, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.rti.segbase, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.rti.table_len, arg)) < 0
+          || (ret = fetchw (as, a, addr, &di->u.rti.table_data, arg)) < 0)
+        goto out;
+      break;
 
     default:
-        ret = -UNW_ENOINFO;
-        goto out;
+      ret = -UNW_ENOINFO;
+      goto out;
     }
-    return 0;
+  return 0;
 
-out:
-    free_dyn_info (di);
-    return ret;
+ out:
+  free_dyn_info (di);
+  return ret;
 }
 
 HIDDEN int
@@ -205,100 +205,96 @@
                                 unw_proc_info_t *pi,
                                 int need_unwind_info, void *arg)
 {
-    unw_accessors_t *a = unw_get_accessors (as);
-    unw_word_t dyn_list_addr, addr, next_addr, gen1, gen2, start_ip, end_ip;
-    unw_dyn_info_t *di = NULL;
-    int ret;
+  unw_accessors_t *a = unw_get_accessors_int (as);
+  unw_word_t dyn_list_addr, addr, next_addr, gen1, gen2, start_ip, end_ip;
+  unw_dyn_info_t *di = NULL;
+  int ret;
 
-    if (as->dyn_info_list_addr)
-        dyn_list_addr = as->dyn_info_list_addr;
-    else
+  if (as->dyn_info_list_addr)
+    dyn_list_addr = as->dyn_info_list_addr;
+  else
     {
-        if ((*a->get_dyn_info_list_addr) (as, &dyn_list_addr, arg) < 0)
-            return -UNW_ENOINFO;
-        if (as->caching_policy != UNW_CACHE_NONE)
-            as->dyn_info_list_addr = dyn_list_addr;
+      if ((*a->get_dyn_info_list_addr) (as, &dyn_list_addr, arg) < 0)
+        return -UNW_ENOINFO;
+      if (as->caching_policy != UNW_CACHE_NONE)
+        as->dyn_info_list_addr = dyn_list_addr;
     }
 
-    do
+  do
     {
-        addr = dyn_list_addr;
+      addr = dyn_list_addr;
 
-        ret = -UNW_ENOINFO;
+      ret = -UNW_ENOINFO;
 
-        if (fetchw (as, a, &addr, &gen1, arg) < 0
-                || fetchw (as, a, &addr, &next_addr, arg) < 0)
-            break;
+      if (fetchw (as, a, &addr, &gen1, arg) < 0
+          || fetchw (as, a, &addr, &next_addr, arg) < 0)
+        return ret;
 
-        for (addr = next_addr; addr != 0; addr = next_addr)
+      for (addr = next_addr; addr != 0; addr = next_addr)
         {
-            if (fetchw (as, a, &addr, &next_addr, arg) < 0)
-                goto recheck;	/* only fail if generation # didn't change */
+          if (fetchw (as, a, &addr, &next_addr, arg) < 0)
+            goto recheck;       /* only fail if generation # didn't change */
 
-            addr += WSIZE;	/* skip over prev_addr */
+          addr += WSIZE;        /* skip over prev_addr */
 
-            if (fetchw (as, a, &addr, &start_ip, arg) < 0
-                    || fetchw (as, a, &addr, &end_ip, arg) < 0)
-                goto recheck;	/* only fail if generation # didn't change */
+          if (fetchw (as, a, &addr, &start_ip, arg) < 0
+              || fetchw (as, a, &addr, &end_ip, arg) < 0)
+            goto recheck;       /* only fail if generation # didn't change */
 
-            if (ip >= start_ip && ip < end_ip)
+          if (ip >= start_ip && ip < end_ip)
             {
-                if (!di)
+              if (!di)
+                di = calloc (1, sizeof (*di));
+
+              di->start_ip = start_ip;
+              di->end_ip = end_ip;
+
+              if (fetchw (as, a, &addr, &di->gp, arg) < 0
+                  || fetch32 (as, a, &addr, &di->format, arg) < 0)
+                goto recheck;   /* only fail if generation # didn't change */
+
+              addr += 4;        /* skip over padding */
+
+              if (need_unwind_info
+                  && intern_dyn_info (as, a, &addr, di, arg) < 0)
+                goto recheck;   /* only fail if generation # didn't change */
+
+              if (unwi_extract_dynamic_proc_info (as, ip, pi, di,
+                                                  need_unwind_info, arg) < 0)
                 {
-                    di = calloc (1, sizeof (*di));
-                    if (!di)
-                        return -UNW_ENOMEM;
+                  free_dyn_info (di);
+                  goto recheck; /* only fail if generation # didn't change */
                 }
-
-                di->start_ip = start_ip;
-                di->end_ip = end_ip;
-
-                if (fetchw (as, a, &addr, &di->gp, arg) < 0
-                        || fetch32 (as, a, &addr, &di->format, arg) < 0)
-                    goto recheck;	/* only fail if generation # didn't change */
-
-                addr += 4;	/* skip over padding */
-
-                if (need_unwind_info
-                        && intern_dyn_info (as, a, &addr, di, arg) < 0)
-                    goto recheck;	/* only fail if generation # didn't change */
-
-                if (unwi_extract_dynamic_proc_info (as, ip, pi, di,
-                                                    need_unwind_info, arg) < 0)
-                {
-                    free_dyn_info (di);
-                    goto recheck;	/* only fail if generation # didn't change */
-                }
-                ret = 0;	/* OK, found it */
-                break;
+              ret = 0;  /* OK, found it */
+              break;
             }
         }
 
-        /* Re-check generation number to ensure the data we have is
-        consistent.  */
-recheck:
-        addr = dyn_list_addr;
-        if (fetchw (as, a, &addr, &gen2, arg) < 0)
-            break;
+      /* Re-check generation number to ensure the data we have is
+         consistent.  */
+    recheck:
+      addr = dyn_list_addr;
+      if (fetchw (as, a, &addr, &gen2, arg) < 0)
+        return ret;
     }
-    while (gen1 != gen2);
+  while (gen1 != gen2);
 
-    if (ret < 0 && di)
-        free (di);
+  if (ret < 0 && di)
+    free (di);
 
-    return ret;
+  return ret;
 }
 
 HIDDEN void
 unwi_dyn_remote_put_unwind_info (unw_addr_space_t as, unw_proc_info_t *pi,
                                  void *arg)
 {
-    if (!pi->unwind_info)
-        return;
+  if (!pi->unwind_info)
+    return;
 
-    free_dyn_info (pi->unwind_info);
-    free (pi->unwind_info);
-    pi->unwind_info = NULL;
+  free_dyn_info (pi->unwind_info);
+  free (pi->unwind_info);
+  pi->unwind_info = NULL;
 }
 
 /* Returns 1 if the cache is up-to-date or -1 if the cache contained
@@ -307,24 +303,24 @@
 HIDDEN int
 unwi_dyn_validate_cache (unw_addr_space_t as, void *arg)
 {
-    unw_word_t addr, gen;
-    unw_accessors_t *a;
+  unw_word_t addr, gen;
+  unw_accessors_t *a;
 
-    if (!as->dyn_info_list_addr)
-        /* If we don't have the dyn_info_list_addr, we don't have anything
-           in the cache.  */
-        return 0;
+  if (!as->dyn_info_list_addr)
+    /* If we don't have the dyn_info_list_addr, we don't have anything
+       in the cache.  */
+    return 0;
 
-    a = unw_get_accessors (as);
-    addr = as->dyn_info_list_addr;
+  a = unw_get_accessors_int (as);
+  addr = as->dyn_info_list_addr;
 
-    if (fetchw (as, a, &addr, &gen, arg) < 0)
-        return 1;
+  if (fetchw (as, a, &addr, &gen, arg) < 0)
+    return 1;
 
-    if (gen == as->dyn_generation)
-        return 1;
+  if (gen == as->dyn_generation)
+    return 1;
 
-    unw_flush_cache (as, 0, 0);
-    as->dyn_generation = gen;
-    return -1;
+  unw_flush_cache (as, 0, 0);
+  as->dyn_generation = gen;
+  return -1;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gfind_dynamic_proc_info.c b/sgx_unwind/libunwind/src/mi/Gfind_dynamic_proc_info.c
index 6c4fc85..2e7c62e 100644
--- a/sgx_unwind/libunwind/src/mi/Gfind_dynamic_proc_info.c
+++ b/sgx_unwind/libunwind/src/mi/Gfind_dynamic_proc_info.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -31,7 +31,7 @@
 local_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
                       int need_unwind_info, void *arg)
 {
-    return -UNW_ENOINFO;
+  return -UNW_ENOINFO;
 }
 
 #else /* !UNW_REMOTE_ONLY */
@@ -40,21 +40,22 @@
 local_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
                       int need_unwind_info, void *arg)
 {
-    unw_dyn_info_list_t *list;
-    unw_dyn_info_t *di;
+  unw_dyn_info_list_t *list;
+  unw_dyn_info_t *di;
 
 #ifndef UNW_LOCAL_ONLY
 # pragma weak _U_dyn_info_list_addr
-    if (!_U_dyn_info_list_addr)
-        return -UNW_ENOINFO;
+  if (!_U_dyn_info_list_addr)
+    return -UNW_ENOINFO;
 #endif
 
-    list = (unw_dyn_info_list_t *) (uintptr_t) _U_dyn_info_list_addr ();
-    for (di = list->first; di; di = di->next)
-        if (ip >= di->start_ip && ip < di->end_ip)
-            return unwi_extract_dynamic_proc_info (as, ip, pi, di, need_unwind_info,
-                                                   arg);
-    return -UNW_ENOINFO;
+  // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
+  list = (unw_dyn_info_list_t *) (uintptr_t) _U_dyn_info_list_addr ();
+  for (di = list->first; di; di = di->next)
+    if (ip >= di->start_ip && ip < di->end_ip)
+      return unwi_extract_dynamic_proc_info (as, ip, pi, di, need_unwind_info,
+                                             arg);
+  return -UNW_ENOINFO;
 }
 
 #endif /* !UNW_REMOTE_ONLY */
@@ -65,7 +66,7 @@
 remote_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
                        int need_unwind_info, void *arg)
 {
-    return -UNW_ENOINFO;
+  return -UNW_ENOINFO;
 }
 
 #else /* !UNW_LOCAL_ONLY */
@@ -74,7 +75,7 @@
 remote_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
                        int need_unwind_info, void *arg)
 {
-    return unwi_dyn_remote_find_proc_info (as, ip, pi, need_unwind_info, arg);
+  return unwi_dyn_remote_find_proc_info (as, ip, pi, need_unwind_info, arg);
 }
 
 #endif /* !UNW_LOCAL_ONLY */
@@ -84,8 +85,8 @@
                              unw_proc_info_t *pi, int need_unwind_info,
                              void *arg)
 {
-    if (as == unw_local_addr_space)
-        return local_find_proc_info (as, ip, pi, need_unwind_info, arg);
-    else
-        return remote_find_proc_info (as, ip, pi, need_unwind_info, arg);
+  if (as == unw_local_addr_space)
+    return local_find_proc_info (as, ip, pi, need_unwind_info, arg);
+  else
+    return remote_find_proc_info (as, ip, pi, need_unwind_info, arg);
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gget_accessors.c b/sgx_unwind/libunwind/src/mi/Gget_accessors.c
index aea6db0..31a6fba 100644
--- a/sgx_unwind/libunwind/src/mi/Gget_accessors.c
+++ b/sgx_unwind/libunwind/src/mi/Gget_accessors.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2004-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,10 +25,13 @@
 
 #include "libunwind_i.h"
 
-PROTECTED unw_accessors_t *
+HIDDEN ALIAS(unw_get_accessors) unw_accessors_t *
+unw_get_accessors_int (unw_addr_space_t as);
+
+unw_accessors_t *
 unw_get_accessors (unw_addr_space_t as)
 {
-    if (tdep_needs_initialization)
-        tdep_init ();
-    return &as->acc;
+  if (!tdep_init_done)
+    tdep_init ();
+  return &as->acc;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gget_fpreg.c b/sgx_unwind/libunwind/src/mi/Gget_fpreg.c
index 634b3b9..f32b128 100644
--- a/sgx_unwind/libunwind/src/mi/Gget_fpreg.c
+++ b/sgx_unwind/libunwind/src/mi/Gget_fpreg.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,10 +25,10 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_get_fpreg (unw_cursor_t *cursor, int regnum, unw_fpreg_t *valp)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    return tdep_access_fpreg (c, regnum, valp, 0);
+  return tdep_access_fpreg (c, regnum, valp, 0);
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gget_proc_info_by_ip.c b/sgx_unwind/libunwind/src/mi/Gget_proc_info_by_ip.c
index 2bba0ce..2697ff8 100644
--- a/sgx_unwind/libunwind/src/mi/Gget_proc_info_by_ip.c
+++ b/sgx_unwind/libunwind/src/mi/Gget_proc_info_by_ip.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,15 +25,15 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_get_proc_info_by_ip (unw_addr_space_t as, unw_word_t ip,
                          unw_proc_info_t *pi, void *as_arg)
 {
-    unw_accessors_t *a = unw_get_accessors (as);
-    int ret;
+  unw_accessors_t *a = unw_get_accessors_int (as);
+  int ret;
 
-    ret = unwi_find_dynamic_proc_info (as, ip, pi, 0, as_arg);
-    if (ret == -UNW_ENOINFO)
-        ret = (*a->find_proc_info) (as, ip, pi, 0, as_arg);
-    return ret;
+  ret = unwi_find_dynamic_proc_info (as, ip, pi, 0, as_arg);
+  if (ret == -UNW_ENOINFO)
+    ret = (*a->find_proc_info) (as, ip, pi, 0, as_arg);
+  return ret;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gget_proc_name.c b/sgx_unwind/libunwind/src/mi/Gget_proc_name.c
index ec2d9d1..840d900 100644
--- a/sgx_unwind/libunwind/src/mi/Gget_proc_name.c
+++ b/sgx_unwind/libunwind/src/mi/Gget_proc_name.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -30,77 +30,89 @@
 intern_string (unw_addr_space_t as, unw_accessors_t *a,
                unw_word_t addr, char *buf, size_t buf_len, void *arg)
 {
-    size_t i;
-    int ret;
+  size_t i;
+  int ret;
 
-    for (i = 0; i < buf_len; ++i)
+  for (i = 0; i < buf_len; ++i)
     {
-        if ((ret = fetch8 (as, a, &addr, (int8_t *) buf + i, arg)) < 0)
-            return ret;
+      if ((ret = fetch8 (as, a, &addr, (int8_t *) buf + i, arg)) < 0)
+        return ret;
 
-        if (buf[i] == '\0')
-            return 0;		/* copied full string; return success */
+      if (buf[i] == '\0')
+        return 0;               /* copied full string; return success */
     }
-    buf[buf_len - 1] = '\0';	/* ensure string is NUL terminated */
-    return -UNW_ENOMEM;
+  buf[buf_len - 1] = '\0';      /* ensure string is NUL terminated */
+  return -UNW_ENOMEM;
 }
 
 static inline int
 get_proc_name (unw_addr_space_t as, unw_word_t ip,
                char *buf, size_t buf_len, unw_word_t *offp, void *arg)
 {
-    unw_accessors_t *a = unw_get_accessors (as);
-    unw_proc_info_t pi;
-    int ret;
+  unw_accessors_t *a = unw_get_accessors_int (as);
+  unw_proc_info_t pi;
+  int ret;
 
-    buf[0] = '\0';	/* always return a valid string, even if it's empty */
+  buf[0] = '\0';        /* always return a valid string, even if it's empty */
 
-    ret = unwi_find_dynamic_proc_info (as, ip, &pi, 1, arg);
-    if (ret == 0)
+  ret = unwi_find_dynamic_proc_info (as, ip, &pi, 1, arg);
+  if (ret == 0)
     {
-        unw_dyn_info_t *di = pi.unwind_info;
+      unw_dyn_info_t *di = pi.unwind_info;
 
-        if (offp)
-            *offp = ip - pi.start_ip;
+      if (offp)
+        *offp = ip - pi.start_ip;
 
-        switch (di->format)
+      switch (di->format)
         {
         case UNW_INFO_FORMAT_DYNAMIC:
-            ret = intern_string (as, a, di->u.pi.name_ptr, buf, buf_len, arg);
-            break;
+          ret = intern_string (as, a, di->u.pi.name_ptr, buf, buf_len, arg);
+          break;
 
         case UNW_INFO_FORMAT_TABLE:
         case UNW_INFO_FORMAT_REMOTE_TABLE:
-            /* XXX should we create a fake name, e.g.: "tablenameN",
-               where N is the index of the function in the table??? */
-            ret = -UNW_ENOINFO;
-            break;
+          /* XXX should we create a fake name, e.g.: "tablenameN",
+             where N is the index of the function in the table??? */
+          ret = -UNW_ENOINFO;
+          break;
 
         default:
-            ret = -UNW_EINVAL;
-            break;
+          ret = -UNW_EINVAL;
+          break;
         }
-        unwi_put_dynamic_unwind_info (as, &pi, arg);
-        return ret;
+      unwi_put_dynamic_unwind_info (as, &pi, arg);
+      return ret;
     }
 
-    if (ret != -UNW_ENOINFO)
-        return ret;
+  if (ret != -UNW_ENOINFO)
+    return ret;
 
-    /* not a dynamic procedure, try to lookup static procedure name: */
+  /* not a dynamic procedure, try to lookup static procedure name: */
 
-    if (a->get_proc_name)
-        return (*a->get_proc_name) (as, ip, buf, buf_len, offp, arg);
+  if (a->get_proc_name)
+    return (*a->get_proc_name) (as, ip, buf, buf_len, offp, arg);
 
-    return -UNW_ENOINFO;
+  return -UNW_ENOINFO;
 }
 
-PROTECTED int
+int
 unw_get_proc_name (unw_cursor_t *cursor, char *buf, size_t buf_len,
                    unw_word_t *offp)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
+  unw_word_t ip;
+  int error;
 
-    return get_proc_name (tdep_get_as (c), tdep_get_ip (c), buf, buf_len, offp,
-                          tdep_get_as_arg (c));
+  ip = tdep_get_ip (c);
+#if !defined(__ia64__)
+  if (c->dwarf.use_prev_instr)
+    --ip;
+#endif
+  error = get_proc_name (tdep_get_as (c), ip, buf, buf_len, offp,
+                         tdep_get_as_arg (c));
+#if !defined(__ia64__)
+  if (c->dwarf.use_prev_instr && offp != NULL && error == 0)
+    *offp += 1;
+#endif
+  return error;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gget_reg.c b/sgx_unwind/libunwind/src/mi/Gget_reg.c
index 94c3583..9fc725c 100644
--- a/sgx_unwind/libunwind/src/mi/Gget_reg.c
+++ b/sgx_unwind/libunwind/src/mi/Gget_reg.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,17 +25,17 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_get_reg (unw_cursor_t *cursor, int regnum, unw_word_t *valp)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    // We can get the IP value directly without needing a lookup.
-    if (regnum == UNW_REG_IP)
+  // We can get the IP value directly without needing a lookup.
+  if (regnum == UNW_REG_IP)
     {
-        *valp = tdep_get_ip (c);
-        return 0;
+      *valp = tdep_get_ip (c);
+      return 0;
     }
 
-    return tdep_access_reg (c, regnum, valp, 0);
+  return tdep_access_reg (c, regnum, valp, 0);
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gput_dynamic_unwind_info.c b/sgx_unwind/libunwind/src/mi/Gput_dynamic_unwind_info.c
index 107e2d4..ca377c9 100644
--- a/sgx_unwind/libunwind/src/mi/Gput_dynamic_unwind_info.c
+++ b/sgx_unwind/libunwind/src/mi/Gput_dynamic_unwind_info.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -29,27 +29,27 @@
 unwi_put_dynamic_unwind_info (unw_addr_space_t as, unw_proc_info_t *pi,
                               void *arg)
 {
-    switch (pi->format)
+  switch (pi->format)
     {
     case UNW_INFO_FORMAT_DYNAMIC:
 #ifndef UNW_LOCAL_ONLY
 # ifdef UNW_REMOTE_ONLY
-        unwi_dyn_remote_put_unwind_info (as, pi, arg);
+      unwi_dyn_remote_put_unwind_info (as, pi, arg);
 # else
-        if (as != unw_local_addr_space)
-            unwi_dyn_remote_put_unwind_info (as, pi, arg);
+      if (as != unw_local_addr_space)
+        unwi_dyn_remote_put_unwind_info (as, pi, arg);
 # endif
 #endif
-        break;
+      break;
 
     case UNW_INFO_FORMAT_TABLE:
     case UNW_INFO_FORMAT_REMOTE_TABLE:
 #ifdef tdep_put_unwind_info
-        tdep_put_unwind_info (as, pi, arg);
-        break;
+      tdep_put_unwind_info (as, pi, arg);
+      break;
 #endif
-    /* fall through */
+      /* fall through */
     default:
-        break;
+      break;
     }
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gset_cache_size.c b/sgx_unwind/libunwind/src/mi/Gset_cache_size.c
new file mode 100644
index 0000000..07b282e
--- /dev/null
+++ b/sgx_unwind/libunwind/src/mi/Gset_cache_size.c
@@ -0,0 +1,72 @@
+/* libunwind - a platform-independent unwind library
+   Copyright (C) 2014
+        Contributed by Milian Wolff <address@hidden>
+                   and Dave Watson <dade.watson@gmail.com>
+
+This file is part of libunwind.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
+
+#include "libunwind_i.h"
+
+int
+unw_set_cache_size (unw_addr_space_t as, size_t size, int flag)
+{
+  size_t power = 1;
+  unsigned short log_size = 0;
+
+  if (!tdep_init_done)
+    tdep_init ();
+
+  if (flag != 0)
+    return -1;
+
+  /* Currently not supported for per-thread cache due to memory leak */
+  /* A pthread-key destructor would work, but is not signal safe */
+#if defined(HAVE___THREAD) && HAVE___THREAD
+  return -1;
+#endif
+
+  /* Round up to next power of two, slowly but portably */
+  while(power < size)
+    {
+      power *= 2;
+      log_size++;
+      /* Largest size currently supported by rs_cache */
+      if (log_size >= 15)
+        break;
+    }
+
+#if !defined(__ia64__)
+  if (log_size == as->global_cache.log_size)
+    return 0;   /* no change */
+
+  as->global_cache.log_size = log_size;
+#endif
+
+  /* Ensure caches are empty (and initialized).  */
+  unw_flush_cache (as, 0, 0);
+#ifdef __ia64__
+  return 0;
+#else
+  /* Synchronously purge cache, to ensure memory is allocated */
+  return dwarf_flush_rs_cache(&as->global_cache);
+#endif
+}
diff --git a/sgx_unwind/libunwind/src/mi/Gset_caching_policy.c b/sgx_unwind/libunwind/src/mi/Gset_caching_policy.c
index 3646730..aa3d237 100644
--- a/sgx_unwind/libunwind/src/mi/Gset_caching_policy.c
+++ b/sgx_unwind/libunwind/src/mi/Gset_caching_policy.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,22 +25,22 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_set_caching_policy (unw_addr_space_t as, unw_caching_policy_t policy)
 {
-    if (tdep_needs_initialization)
-        tdep_init ();
+  if (!tdep_init_done)
+    tdep_init ();
 
-#ifndef HAVE___THREAD
-    if (policy == UNW_CACHE_PER_THREAD)
-        policy = UNW_CACHE_GLOBAL;
+#if !(defined(HAVE___THREAD) && HAVE___THREAD)
+  if (policy == UNW_CACHE_PER_THREAD)
+    policy = UNW_CACHE_GLOBAL;
 #endif
 
-    if (policy == as->caching_policy)
-        return 0;	/* no change */
+  if (policy == as->caching_policy)
+    return 0;   /* no change */
 
-    as->caching_policy = policy;
-    /* Ensure caches are empty (and initialized).  */
-    unw_flush_cache (as, 0, 0);
-    return 0;
+  as->caching_policy = policy;
+  /* Ensure caches are empty (and initialized).  */
+  unw_flush_cache (as, 0, 0);
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gset_fpreg.c b/sgx_unwind/libunwind/src/mi/Gset_fpreg.c
index 450f06b..8c37afd 100644
--- a/sgx_unwind/libunwind/src/mi/Gset_fpreg.c
+++ b/sgx_unwind/libunwind/src/mi/Gset_fpreg.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,10 +25,10 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_set_fpreg (unw_cursor_t *cursor, int regnum, unw_fpreg_t val)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    return tdep_access_fpreg (c, regnum, &val, 1);
+  return tdep_access_fpreg (c, regnum, &val, 1);
 }
diff --git a/sgx_unwind/libunwind/src/mi/Gset_reg.c b/sgx_unwind/libunwind/src/mi/Gset_reg.c
index 1e3264b..b1b1770 100644
--- a/sgx_unwind/libunwind/src/mi/Gset_reg.c
+++ b/sgx_unwind/libunwind/src/mi/Gset_reg.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,10 +25,10 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_set_reg (unw_cursor_t *cursor, int regnum, unw_word_t valp)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    return tdep_access_reg (c, regnum, &valp, 1);
+  return tdep_access_reg (c, regnum, &valp, 1);
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c b/sgx_unwind/libunwind/src/mi/Lset_cache_size.c
similarity index 78%
rename from sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
rename to sgx_unwind/libunwind/src/mi/Lset_cache_size.c
index b9a7c4f..670f64d 100644
--- a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
+++ b/sgx_unwind/libunwind/src/mi/Lset_cache_size.c
@@ -1,5 +1,5 @@
 #define UNW_LOCAL_ONLY
 #include <libunwind.h>
 #if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
-#include "Gis_signal_frame.c"
+#include "Gset_cache_size.c"
 #endif
diff --git a/sgx_unwind/libunwind/src/mi/_ReadSLEB.c b/sgx_unwind/libunwind/src/mi/_ReadSLEB.c
index 996e80c..c041e37 100644
--- a/sgx_unwind/libunwind/src/mi/_ReadSLEB.c
+++ b/sgx_unwind/libunwind/src/mi/_ReadSLEB.c
@@ -3,23 +3,23 @@
 unw_word_t
 _ReadSLEB (unsigned char **dpp)
 {
-    unsigned shift = 0;
-    unw_word_t byte, result = 0;
-    unsigned char *bp = *dpp;
+  unsigned shift = 0;
+  unw_word_t byte, result = 0;
+  unsigned char *bp = *dpp;
 
-    while (1)
+  while (1)
     {
-        byte = *bp++;
-        result |= (byte & 0x7f) << shift;
-        shift += 7;
-        if ((byte & 0x80) == 0)
-            break;
+      byte = *bp++;
+      result |= (byte & 0x7f) << shift;
+      shift += 7;
+      if ((byte & 0x80) == 0)
+        break;
     }
 
-    if (shift < 8 * sizeof (unw_word_t) && (byte & 0x40) != 0)
-        /* sign-extend negative value */
-        result |= ((unw_word_t) -1) << shift;
+  if (shift < 8 * sizeof (unw_word_t) && (byte & 0x40) != 0)
+    /* sign-extend negative value */
+    result |= ((unw_word_t) -1) << shift;
 
-    *dpp = bp;
-    return result;
+  *dpp = bp;
+  return result;
 }
diff --git a/sgx_unwind/libunwind/src/mi/_ReadULEB.c b/sgx_unwind/libunwind/src/mi/_ReadULEB.c
index 6eb8ab3..116f3e1 100644
--- a/sgx_unwind/libunwind/src/mi/_ReadULEB.c
+++ b/sgx_unwind/libunwind/src/mi/_ReadULEB.c
@@ -3,18 +3,18 @@
 unw_word_t
 _ReadULEB (unsigned char **dpp)
 {
-    unsigned shift = 0;
-    unw_word_t byte, result = 0;
-    unsigned char *bp = *dpp;
+  unsigned shift = 0;
+  unw_word_t byte, result = 0;
+  unsigned char *bp = *dpp;
 
-    while (1)
+  while (1)
     {
-        byte = *bp++;
-        result |= (byte & 0x7f) << shift;
-        if ((byte & 0x80) == 0)
-            break;
-        shift += 7;
+      byte = *bp++;
+      result |= (byte & 0x7f) << shift;
+      if ((byte & 0x80) == 0)
+        break;
+      shift += 7;
     }
-    *dpp = bp;
-    return result;
+  *dpp = bp;
+  return result;
 }
diff --git a/sgx_unwind/libunwind/src/mi/backtrace.c b/sgx_unwind/libunwind/src/mi/backtrace.c
index 363dc44..c7aa2bd 100644
--- a/sgx_unwind/libunwind/src/mi/backtrace.c
+++ b/sgx_unwind/libunwind/src/mi/backtrace.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -35,47 +35,47 @@
 static ALWAYS_INLINE int
 slow_backtrace (void **buffer, int size, unw_context_t *uc)
 {
-    unw_cursor_t cursor;
-    unw_word_t ip;
-    int n = 0;
+  unw_cursor_t cursor;
+  unw_word_t ip;
+  int n = 0;
 
-    if (unlikely (unw_init_local (&cursor, uc) < 0))
-        return 0;
+  if (unlikely (unw_init_local (&cursor, uc) < 0))
+    return 0;
 
-    while (unw_step (&cursor) > 0)
+  while (unw_step (&cursor) > 0)
     {
-        if (n >= size)
-            return n;
+      if (n >= size)
+        return n;
 
-        if (unw_get_reg (&cursor, UNW_REG_IP, &ip) < 0)
-            return n;
-        buffer[n++] = (void *) (uintptr_t) ip;
+      if (unw_get_reg (&cursor, UNW_REG_IP, &ip) < 0)
+        return n;
+      buffer[n++] = (void *) (uintptr_t) ip;
     }
-    return n;
+  return n;
 }
 
 int
 unw_backtrace (void **buffer, int size)
 {
-    unw_cursor_t cursor;
-    unw_context_t uc;
-    int n = size;
+  unw_cursor_t cursor;
+  unw_context_t uc;
+  int n = size;
 
-    tdep_getcontext_trace (&uc);
+  tdep_getcontext_trace (&uc);
 
-    if (unlikely (unw_init_local (&cursor, &uc) < 0))
-        return 0;
+  if (unlikely (unw_init_local (&cursor, &uc) < 0))
+    return 0;
 
-    if (unlikely (tdep_trace (&cursor, buffer, &n) < 0))
+  if (unlikely (tdep_trace (&cursor, buffer, &n) < 0))
     {
-        unw_getcontext (&uc);
-        return slow_backtrace (buffer, size, &uc);
+      unw_getcontext (&uc);
+      return slow_backtrace (buffer, size, &uc);
     }
 
-    return n;
+  return n;
 }
 
 extern int backtrace (void **buffer, int size)
-__attribute__((weak, alias("unw_backtrace")));
+  WEAK ALIAS(unw_backtrace);
 
 #endif /* !UNW_REMOTE_ONLY */
diff --git a/sgx_unwind/libunwind/src/mi/dyn-cancel.c b/sgx_unwind/libunwind/src/mi/dyn-cancel.c
index a59e22c..9d7472d 100644
--- a/sgx_unwind/libunwind/src/mi/dyn-cancel.c
+++ b/sgx_unwind/libunwind/src/mi/dyn-cancel.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -28,19 +28,19 @@
 void
 _U_dyn_cancel (unw_dyn_info_t *di)
 {
-    mutex_lock (&_U_dyn_info_list_lock);
-    {
-        ++_U_dyn_info_list.generation;
+  mutex_lock (&_U_dyn_info_list_lock);
+  {
+    ++_U_dyn_info_list.generation;
 
-        if (di->prev)
-            di->prev->next = di->next;
-        else
-            _U_dyn_info_list.first = di->next;
+    if (di->prev)
+      di->prev->next = di->next;
+    else
+      _U_dyn_info_list.first = di->next;
 
-        if (di->next)
-            di->next->prev = di->prev;
-    }
-    mutex_unlock (&_U_dyn_info_list_lock);
+    if (di->next)
+      di->next->prev = di->prev;
+  }
+  mutex_unlock (&_U_dyn_info_list_lock);
 
-    di->next = di->prev = NULL;
+  di->next = di->prev = NULL;
 }
diff --git a/sgx_unwind/libunwind/src/mi/dyn-info-list.c b/sgx_unwind/libunwind/src/mi/dyn-info-list.c
index ceb1ebd..1c7c550 100644
--- a/sgx_unwind/libunwind/src/mi/dyn-info-list.c
+++ b/sgx_unwind/libunwind/src/mi/dyn-info-list.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -27,8 +27,8 @@
 
 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
 
-PROTECTED unw_word_t
+unw_word_t
 _U_dyn_info_list_addr (void)
 {
-    return (unw_word_t) (uintptr_t) &_U_dyn_info_list;
+  return (unw_word_t) (uintptr_t) &_U_dyn_info_list;
 }
diff --git a/sgx_unwind/libunwind/src/mi/dyn-register.c b/sgx_unwind/libunwind/src/mi/dyn-register.c
index 95375aa..efdad3d 100644
--- a/sgx_unwind/libunwind/src/mi/dyn-register.c
+++ b/sgx_unwind/libunwind/src/mi/dyn-register.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2001-2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,20 +25,20 @@
 
 #include "libunwind_i.h"
 
-HIDDEN pthread_mutex_t _U_dyn_info_list_lock = PTHREAD_MUTEX_INITIALIZER;
+HIDDEN define_lock (_U_dyn_info_list_lock);
 
 void
 _U_dyn_register (unw_dyn_info_t *di)
 {
-    mutex_lock (&_U_dyn_info_list_lock);
-    {
-        ++_U_dyn_info_list.generation;
+  mutex_lock (&_U_dyn_info_list_lock);
+  {
+    ++_U_dyn_info_list.generation;
 
-        di->next = _U_dyn_info_list.first;
-        di->prev = NULL;
-        if (di->next)
+    di->next = _U_dyn_info_list.first;
+    di->prev = NULL;
+    if (di->next)
             di->next->prev = di;
-        _U_dyn_info_list.first = di;
-    }
-    mutex_unlock (&_U_dyn_info_list_lock);
+    _U_dyn_info_list.first = di;
+  }
+  mutex_unlock (&_U_dyn_info_list_lock);
 }
diff --git a/sgx_unwind/libunwind/src/mi/flush_cache.c b/sgx_unwind/libunwind/src/mi/flush_cache.c
index 68a6489..cbd93e1 100644
--- a/sgx_unwind/libunwind/src/mi/flush_cache.c
+++ b/sgx_unwind/libunwind/src/mi/flush_cache.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,35 +25,35 @@
 
 #include "libunwind_i.h"
 
-PROTECTED void
+void
 unw_flush_cache (unw_addr_space_t as, unw_word_t lo, unw_word_t hi)
 {
 #if !UNW_TARGET_IA64
-    struct unw_debug_frame_list *w = as->debug_frames;
+  struct unw_debug_frame_list *w = as->debug_frames;
 #endif
 
-    /* clear dyn_info_list_addr cache: */
-    as->dyn_info_list_addr = 0;
+  /* clear dyn_info_list_addr cache: */
+  as->dyn_info_list_addr = 0;
 
 #if !UNW_TARGET_IA64
-    for (; w; w = w->next)
+  for (; w; w = w->next)
     {
-        if (w->index)
-            free (w->index);
-        free (w->debug_frame);
+      if (w->index)
+        free (w->index);
+      free (w->debug_frame);
     }
-    as->debug_frames = NULL;
+  as->debug_frames = NULL;
 #endif
 
-    /* This lets us flush caches lazily.  The implementation currently
-       ignores the flush range arguments (lo-hi).  This is OK because
-       unw_flush_cache() is allowed to flush more than the requested
-       range. */
+  /* This lets us flush caches lazily.  The implementation currently
+     ignores the flush range arguments (lo-hi).  This is OK because
+     unw_flush_cache() is allowed to flush more than the requested
+     range. */
 
-#ifdef HAVE_FETCH_AND_ADD1
-    fetch_and_add1 (&as->cache_generation);
+#ifdef HAVE_FETCH_AND_ADD
+  fetch_and_add1 (&as->cache_generation);
 #else
 # warning unw_flush_cache(): need a way to atomically increment an integer.
-    ++as->cache_generation;
+  ++as->cache_generation;
 #endif
 }
diff --git a/sgx_unwind/libunwind/src/mi/init.c b/sgx_unwind/libunwind/src/mi/init.c
index fdaa6c0..60a48c5 100644
--- a/sgx_unwind/libunwind/src/mi/init.c
+++ b/sgx_unwind/libunwind/src/mi/init.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -28,11 +28,11 @@
 HIDDEN intrmask_t unwi_full_mask;
 
 static const char rcsid[] UNUSED =
-    "$Id: " PACKAGE_STRING " --- report bugs to " PACKAGE_BUGREPORT " $";
+  "$Id: " PACKAGE_STRING " --- report bugs to " PACKAGE_BUGREPORT " $";
 
 #if UNW_DEBUG
 
-/* Must not be declared HIDDEN/PROTECTED because libunwind.so and
+/* Must not be declared HIDDEN because libunwind.so and
    libunwind-PLATFORM.so will both define their own copies of this
    variable and we want to use only one or the other when both
    libraries are loaded.  */
@@ -43,20 +43,18 @@
 HIDDEN void
 mi_init (void)
 {
-#if !HAVE_SGX
 #if UNW_DEBUG
-    const char *str = getenv ("UNW_DEBUG_LEVEL");
+  const char *str = getenv ("UNW_DEBUG_LEVEL");
 
-    if (str)
-        unwi_debug_level = atoi (str);
+  if (str)
+    unwi_debug_level = atoi (str);
 
-    if (unwi_debug_level > 0)
+  if (unwi_debug_level > 0)
     {
-        setbuf (stdout, NULL);
-        setbuf (stderr, NULL);
+      setbuf (stdout, NULL);
+      setbuf (stderr, NULL);
     }
 #endif
-#endif
 
-    assert (sizeof (struct cursor) <= sizeof (unw_cursor_t));
+  assert (sizeof (struct cursor) <= sizeof (unw_cursor_t));
 }
diff --git a/sgx_unwind/libunwind/src/mi/mempool.c b/sgx_unwind/libunwind/src/mi/mempool.c
index d8a644f..536b64e 100644
--- a/sgx_unwind/libunwind/src/mi/mempool.c
+++ b/sgx_unwind/libunwind/src/mi/mempool.c
@@ -1,6 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2003, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
 
 This file is part of libunwind.
 
@@ -25,51 +26,58 @@
 
 #include "libunwind_i.h"
 
-#define MAX_ALIGN	(sizeof (long double))
+/* From GCC docs: ``Gcc also provides a target specific macro
+ * __BIGGEST_ALIGNMENT__, which is the largest alignment ever used for any data
+ * type on the target machine you are compiling for.'' */
+#ifdef __BIGGEST_ALIGNMENT__
+# define MAX_ALIGN      __BIGGEST_ALIGNMENT__
+#else
+/* Crude hack to check that MAX_ALIGN is power-of-two.
+ * sizeof(long double) = 12 on i386. */
+# define MAX_ALIGN_(n)  (n < 8 ? 8 : \
+                         n < 16 ? 16 : n)
+# define MAX_ALIGN      MAX_ALIGN_(sizeof (long double))
+#endif
 
-static char sos_memory[SOS_MEMORY_SIZE];
-static char *sos_memp;
+static char sos_memory[SOS_MEMORY_SIZE] ALIGNED(MAX_ALIGN);
+static size_t sos_memory_freepos;
 static size_t pg_size;
 
 HIDDEN void *
 sos_alloc (size_t size)
 {
-    char *mem;
+  size_t pos;
 
-#ifdef HAVE_CMPXCHG
-    char *old_mem;
+  size = UNW_ALIGN(size, MAX_ALIGN);
 
-    size = (size + MAX_ALIGN - 1) & -MAX_ALIGN;
-    if (!sos_memp)
-        cmpxchg_ptr (&sos_memp, 0, sos_memory);
-    do
-    {
-        old_mem = sos_memp;
+#if defined(__GNUC__) && defined(HAVE_FETCH_AND_ADD)
+  /* Assume `sos_memory' is suitably aligned. */
+  assert(((uintptr_t) &sos_memory[0] & (MAX_ALIGN-1)) == 0);
 
-        mem = (char *) (((unsigned long) old_mem + MAX_ALIGN - 1) & -MAX_ALIGN);
-        mem += size;
-        assert (mem < sos_memory + sizeof (sos_memory));
-    }
-    while (!cmpxchg_ptr (&sos_memp, old_mem, mem));
+  pos = fetch_and_add (&sos_memory_freepos, size);
 #else
-    static define_lock (sos_lock);
-    intrmask_t saved_mask;
+  static define_lock (sos_lock);
+  intrmask_t saved_mask;
 
-    size = (size + MAX_ALIGN - 1) & -MAX_ALIGN;
-
-    lock_acquire (&sos_lock, saved_mask);
-    {
-        if (!sos_memp)
-            sos_memp = sos_memory;
-
-        mem = (char *) (((unsigned long) sos_memp + MAX_ALIGN - 1) & -MAX_ALIGN);
-        mem += size;
-        assert (mem < sos_memory + sizeof (sos_memory));
-        sos_memp = mem;
-    }
-    lock_release (&sos_lock, saved_mask);
+  lock_acquire (&sos_lock, saved_mask);
+  {
+    /* No assumptions about `sos_memory' alignment. */
+    if (sos_memory_freepos == 0)
+      {
+        unsigned align = UNW_ALIGN((uintptr_t) &sos_memory[0], MAX_ALIGN)
+                                - (uintptr_t) &sos_memory[0];
+        sos_memory_freepos = align;
+      }
+    pos = sos_memory_freepos;
+    sos_memory_freepos += size;
+  }
+  lock_release (&sos_lock, saved_mask);
 #endif
-    return mem;
+
+  assert (((uintptr_t) &sos_memory[pos] & (MAX_ALIGN-1)) == 0);
+  assert ((pos+size) <= SOS_MEMORY_SIZE);
+
+  return &sos_memory[pos];
 }
 
 /* Must be called while holding the mempool lock. */
@@ -77,100 +85,100 @@
 static void
 free_object (struct mempool *pool, void *object)
 {
-    struct object *obj = object;
+  struct object *obj = object;
 
-    obj->next = pool->free_list;
-    pool->free_list = obj;
-    ++pool->num_free;
+  obj->next = pool->free_list;
+  pool->free_list = obj;
+  ++pool->num_free;
 }
 
 static void
 add_memory (struct mempool *pool, char *mem, size_t size, size_t obj_size)
 {
-    char *obj;
+  char *obj;
 
-    for (obj = mem; obj <= mem + size - obj_size; obj += obj_size)
-        free_object (pool, obj);
+  for (obj = mem; obj <= mem + size - obj_size; obj += obj_size)
+    free_object (pool, obj);
 }
 
 static void
 expand (struct mempool *pool)
 {
-    size_t size;
-    char *mem;
+  size_t size;
+  char *mem;
 
-    size = pool->chunk_size;
-    GET_MEMORY (mem, size);
-    if (!mem)
+  size = pool->chunk_size;
+  GET_MEMORY (mem, size);
+  if (!mem)
     {
-        size = (pool->obj_size + pg_size - 1) & -pg_size;
-        GET_MEMORY (mem, size);
-        if (!mem)
+      size = UNW_ALIGN(pool->obj_size, pg_size);
+      GET_MEMORY (mem, size);
+      if (!mem)
         {
-            /* last chance: try to allocate one object from the SOS memory */
-            size = pool->obj_size;
-            mem = sos_alloc (size);
+          /* last chance: try to allocate one object from the SOS memory */
+          size = pool->obj_size;
+          mem = sos_alloc (size);
         }
     }
-    add_memory (pool, mem, size, pool->obj_size);
+  add_memory (pool, mem, size, pool->obj_size);
 }
 
 HIDDEN void
 mempool_init (struct mempool *pool, size_t obj_size, size_t reserve)
 {
-    if (pg_size == 0)
-        pg_size = getpagesize ();
+  if (pg_size == 0)
+    pg_size = getpagesize ();
 
-    memset (pool, 0, sizeof (*pool));
+  memset (pool, 0, sizeof (*pool));
 
-    lock_init (&pool->lock);
+  lock_init (&pool->lock);
 
-    /* round object-size up to integer multiple of MAX_ALIGN */
-    obj_size = (obj_size + MAX_ALIGN - 1) & -MAX_ALIGN;
+  /* round object-size up to integer multiple of MAX_ALIGN */
+  obj_size = UNW_ALIGN(obj_size, MAX_ALIGN);
 
-    if (!reserve)
+  if (!reserve)
     {
-        reserve = pg_size / obj_size / 4;
-        if (!reserve)
-            reserve = 16;
+      reserve = pg_size / obj_size / 4;
+      if (!reserve)
+        reserve = 16;
     }
 
-    pool->obj_size = obj_size;
-    pool->reserve = reserve;
-    pool->chunk_size = (2*reserve*obj_size + pg_size - 1) & -pg_size;
+  pool->obj_size = obj_size;
+  pool->reserve = reserve;
+  pool->chunk_size = UNW_ALIGN(2*reserve*obj_size, pg_size);
 
-    expand (pool);
+  expand (pool);
 }
 
 HIDDEN void *
 mempool_alloc (struct mempool *pool)
 {
-    intrmask_t saved_mask;
-    struct object *obj;
+  intrmask_t saved_mask;
+  struct object *obj;
 
-    lock_acquire (&pool->lock, saved_mask);
-    {
-        if (pool->num_free <= pool->reserve)
-            expand (pool);
+  lock_acquire (&pool->lock, saved_mask);
+  {
+    if (pool->num_free <= pool->reserve)
+      expand (pool);
 
-        assert (pool->num_free > 0);
+    assert (pool->num_free > 0);
 
-        --pool->num_free;
-        obj = pool->free_list;
-        pool->free_list = obj->next;
-    }
-    lock_release (&pool->lock, saved_mask);
-    return obj;
+    --pool->num_free;
+    obj = pool->free_list;
+    pool->free_list = obj->next;
+  }
+  lock_release (&pool->lock, saved_mask);
+  return obj;
 }
 
 HIDDEN void
 mempool_free (struct mempool *pool, void *object)
 {
-    intrmask_t saved_mask;
+  intrmask_t saved_mask;
 
-    lock_acquire (&pool->lock, saved_mask);
-    {
-        free_object (pool, object);
-    }
-    lock_release (&pool->lock, saved_mask);
+  lock_acquire (&pool->lock, saved_mask);
+  {
+    free_object (pool, object);
+  }
+  lock_release (&pool->lock, saved_mask);
 }
diff --git a/sgx_unwind/libunwind/src/mi/strerror.c b/sgx_unwind/libunwind/src/mi/strerror.c
index f55210d..2cec73d 100644
--- a/sgx_unwind/libunwind/src/mi/strerror.c
+++ b/sgx_unwind/libunwind/src/mi/strerror.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004 BEA Systems
-	Contributed by Thomas Hallgren <thallgre@bea.com>
+        Contributed by Thomas Hallgren <thallgre@bea.com>
 
 This file is part of libunwind.
 
@@ -30,45 +30,22 @@
 const char *
 unw_strerror (int err_code)
 {
-    const char *cp;
-    unw_error_t error = (unw_error_t)-err_code;
-    switch (error)
+  const char *cp;
+  unw_error_t error = (unw_error_t)-err_code;
+  switch (error)
     {
-    case UNW_ESUCCESS:
-        cp = "no error";
-        break;
-    case UNW_EUNSPEC:
-        cp = "unspecified (general) error";
-        break;
-    case UNW_ENOMEM:
-        cp = "out of memory";
-        break;
-    case UNW_EBADREG:
-        cp = "bad register number";
-        break;
-    case UNW_EREADONLYREG:
-        cp = "attempt to write read-only register";
-        break;
-    case UNW_ESTOPUNWIND:
-        cp = "stop unwinding";
-        break;
-    case UNW_EINVALIDIP:
-        cp = "invalid IP";
-        break;
-    case UNW_EBADFRAME:
-        cp = "bad frame";
-        break;
-    case UNW_EINVAL:
-        cp = "unsupported operation or bad value";
-        break;
-    case UNW_EBADVERSION:
-        cp = "unwind info has unsupported version";
-        break;
-    case UNW_ENOINFO:
-        cp = "no unwind info found";
-        break;
-    default:
-        cp = "invalid error code";
+    case UNW_ESUCCESS:     cp = "no error"; break;
+    case UNW_EUNSPEC:      cp = "unspecified (general) error"; break;
+    case UNW_ENOMEM:       cp = "out of memory"; break;
+    case UNW_EBADREG:      cp = "bad register number"; break;
+    case UNW_EREADONLYREG: cp = "attempt to write read-only register"; break;
+    case UNW_ESTOPUNWIND:  cp = "stop unwinding"; break;
+    case UNW_EINVALIDIP:   cp = "invalid IP"; break;
+    case UNW_EBADFRAME:    cp = "bad frame"; break;
+    case UNW_EINVAL:       cp = "unsupported operation or bad value"; break;
+    case UNW_EBADVERSION:  cp = "unwind info has unsupported version"; break;
+    case UNW_ENOINFO:      cp = "no unwind info found"; break;
+    default:               cp = "invalid error code";
     }
-    return cp;
+  return cp;
 }
diff --git a/sgx_unwind/libunwind/src/os-linux.c b/sgx_unwind/libunwind/src/os-linux.c
index b2aa627..50ba3c8 100644
--- a/sgx_unwind/libunwind/src/os-linux.c
+++ b/sgx_unwind/libunwind/src/os-linux.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -27,41 +27,53 @@
 #include <stdio.h>
 
 #include "libunwind_i.h"
+#ifndef HAVE_SGX
 #include "os-linux.h"
 
-PROTECTED int
+int
 tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
                     unsigned long *segbase, unsigned long *mapoff,
                     char *path, size_t pathlen)
 {
-#if HAVE_SGX
+  struct map_iterator mi;
+  int found = 0, rc;
+  unsigned long hi;
+
+  if (maps_init (&mi, pid) < 0)
     return -1;
+
+  while (maps_next (&mi, segbase, &hi, mapoff))
+    if (ip >= *segbase && ip < hi)
+      {
+        found = 1;
+        break;
+      }
+
+  if (!found)
+    {
+      maps_close (&mi);
+      return -1;
+    }
+  if (path)
+    {
+      strncpy(path, mi.path, pathlen);
+    }
+  rc = elf_map_image (ei, mi.path);
+  maps_close (&mi);
+  return rc;
+}
+#endif
+
+#ifndef UNW_REMOTE_ONLY
+
+void
+tdep_get_exe_image_path (char *path)
+{
+#ifndef HAVE_SGX
+  strcpy(path, "/proc/self/exe");
 #else
-    struct map_iterator mi;
-    int found = 0, rc;
-    unsigned long hi;
-
-    if (maps_init (&mi, pid) < 0)
-        return -1;
-
-    while (maps_next (&mi, segbase, &hi, mapoff))
-        if (ip >= *segbase && ip < hi)
-        {
-            found = 1;
-            break;
-        }
-
-    if (!found)
-    {
-        maps_close (&mi);
-        return -1;
-    }
-    if (path)
-    {
-        strncpy(path, mi.path, pathlen);
-    }
-    rc = elf_map_image (ei, mi.path);
-    maps_close (&mi);
-    return rc;
+  strncpy(path, "/proc/self/exe", strlen("/proc/self/exe"));
 #endif
 }
+
+#endif /* !UNW_REMOTE_ONLY */
diff --git a/sgx_unwind/libunwind/src/os-linux.h b/sgx_unwind/libunwind/src/os-linux.h
index 56c697a..3976b38 100644
--- a/sgx_unwind/libunwind/src/os-linux.h
+++ b/sgx_unwind/libunwind/src/os-linux.h
@@ -1,7 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
    Copyright (C) 2007 David Mosberger-Tang
-	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
 
 This file is part of libunwind.
 
@@ -28,156 +28,153 @@
 #define os_linux_h
 
 struct map_iterator
-{
+  {
     off_t offset;
     int fd;
     size_t buf_size;
     char *buf;
     char *buf_end;
     char *path;
-};
+  };
 
 static inline char *
 ltoa (char *buf, long val)
 {
-    char *cp = buf, tmp;
-    ssize_t i, len;
+  char *cp = buf, tmp;
+  ssize_t i, len;
 
-    do
+  do
     {
-        *cp++ = '0' + (val % 10);
-        val /= 10;
+      *cp++ = '0' + (val % 10);
+      val /= 10;
     }
-    while (val);
+  while (val);
 
-    /* reverse the order of the digits: */
-    len = cp - buf;
-    --cp;
-    for (i = 0; i < len / 2; ++i)
+  /* reverse the order of the digits: */
+  len = cp - buf;
+  --cp;
+  for (i = 0; i < len / 2; ++i)
     {
-        tmp = buf[i];
-        buf[i] = cp[-i];
-        cp[-i] = tmp;
+      tmp = buf[i];
+      buf[i] = cp[-i];
+      cp[-i] = tmp;
     }
-    return buf + len;
+  return buf + len;
 }
 
 static inline int
 maps_init (struct map_iterator *mi, pid_t pid)
 {
-#if !HAVE_SGX
-    char path[sizeof ("/proc/0123456789/maps")], *cp;
+  char path[sizeof ("/proc/0123456789/maps")], *cp;
 
-    memcpy (path, "/proc/", 6);
-    cp = ltoa (path + 6, pid);
-    assert (cp + 6 < path + sizeof (path));
-    memcpy (cp, "/maps", 6);
+  memcpy (path, "/proc/", 6);
+  cp = ltoa (path + 6, pid);
+  assert (cp + 6 < path + sizeof (path));
+  memcpy (cp, "/maps", 6);
 
-    mi->fd = open (path, O_RDONLY);
-    if (mi->fd >= 0)
+  mi->fd = open (path, O_RDONLY);
+  if (mi->fd >= 0)
     {
-        /* Try to allocate a page-sized buffer.  */
-        mi->buf_size = getpagesize ();
-        cp = mmap (0, mi->buf_size, PROT_READ | PROT_WRITE,
-                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-        if (cp == MAP_FAILED)
+      /* Try to allocate a page-sized buffer.  */
+      mi->buf_size = getpagesize ();
+      cp = mmap (NULL, mi->buf_size, PROT_READ | PROT_WRITE,
+                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+      if (cp == MAP_FAILED)
         {
-            close(mi->fd);
-            mi->fd = -1;
-            return -1;
+          close(mi->fd);
+          mi->fd = -1;
+          return -1;
         }
-        else
+      else
         {
-            mi->offset = 0;
-            mi->buf = mi->buf_end = cp + mi->buf_size;
-            return 0;
+          mi->offset = 0;
+          mi->buf = mi->buf_end = cp + mi->buf_size;
+          return 0;
         }
     }
-#else
-    return -1;
-#endif
+  return -1;
 }
 
 static inline char *
 skip_whitespace (char *cp)
 {
-    if (!cp)
-        return NULL;
+  if (!cp)
+    return NULL;
 
-    while (*cp == ' ' || *cp == '\t')
-        ++cp;
-    return cp;
+  while (*cp == ' ' || *cp == '\t')
+    ++cp;
+  return cp;
 }
 
 static inline char *
 scan_hex (char *cp, unsigned long *valp)
 {
-    unsigned long num_digits = 0, digit, val = 0;
+  unsigned long num_digits = 0, digit, val = 0;
 
-    cp = skip_whitespace (cp);
-    if (!cp)
-        return NULL;
+  cp = skip_whitespace (cp);
+  if (!cp)
+    return NULL;
 
-    while (1)
+  while (1)
     {
-        digit = *cp;
-        if ((digit - '0') <= 9)
-            digit -= '0';
-        else if ((digit - 'a') < 6)
-            digit -= 'a' - 10;
-        else if ((digit - 'A') < 6)
-            digit -= 'A' - 10;
-        else
-            break;
-        val = (val << 4) | digit;
-        ++num_digits;
-        ++cp;
+      digit = *cp;
+      if ((digit - '0') <= 9)
+        digit -= '0';
+      else if ((digit - 'a') < 6)
+        digit -= 'a' - 10;
+      else if ((digit - 'A') < 6)
+        digit -= 'A' - 10;
+      else
+        break;
+      val = (val << 4) | digit;
+      ++num_digits;
+      ++cp;
     }
-    if (!num_digits)
-        return NULL;
-    *valp = val;
-    return cp;
+  if (!num_digits)
+    return NULL;
+  *valp = val;
+  return cp;
 }
 
 static inline char *
 scan_dec (char *cp, unsigned long *valp)
 {
-    unsigned long num_digits = 0, digit, val = 0;
+  unsigned long num_digits = 0, digit, val = 0;
 
-    if (!(cp = skip_whitespace (cp)))
-        return NULL;
+  if (!(cp = skip_whitespace (cp)))
+    return NULL;
 
-    while (1)
+  while (1)
     {
-        digit = *cp;
-        if ((digit - '0') <= 9)
+      digit = *cp;
+      if ((digit - '0') <= 9)
         {
-            digit -= '0';
-            ++cp;
+          digit -= '0';
+          ++cp;
         }
-        else
-            break;
-        val = (10 * val) + digit;
-        ++num_digits;
+      else
+        break;
+      val = (10 * val) + digit;
+      ++num_digits;
     }
-    if (!num_digits)
-        return NULL;
-    *valp = val;
-    return cp;
+  if (!num_digits)
+    return NULL;
+  *valp = val;
+  return cp;
 }
 
 static inline char *
 scan_char (char *cp, char *valp)
 {
-    if (!cp)
-        return NULL;
+  if (!cp)
+    return NULL;
 
-    *valp = *cp;
+  *valp = *cp;
 
-    /* don't step over NUL terminator */
-    if (*cp)
-        ++cp;
-    return cp;
+  /* don't step over NUL terminator */
+  if (*cp)
+    ++cp;
+  return cp;
 }
 
 /* Scan a string delimited by white-space.  Fails on empty string or
@@ -185,118 +182,116 @@
 static inline char *
 scan_string (char *cp, char *valp, size_t buf_size)
 {
-    size_t i = 0;
+  size_t i = 0;
 
-    if (!(cp = skip_whitespace (cp)))
-        return NULL;
+  if (!(cp = skip_whitespace (cp)))
+    return NULL;
 
-    while (*cp != ' ' && *cp != '\t' && *cp != '\0')
+  while (*cp != ' ' && *cp != '\t' && *cp != '\0')
     {
-        if ((valp != NULL) && (i < buf_size - 1))
-            valp[i++] = *cp;
-        ++cp;
+      if ((valp != NULL) && (i < buf_size - 1))
+        valp[i++] = *cp;
+      ++cp;
     }
-    if (i == 0 || i >= buf_size)
-        return NULL;
-    valp[i] = '\0';
-    return cp;
+  if (i == 0 || i >= buf_size)
+    return NULL;
+  valp[i] = '\0';
+  return cp;
 }
 
 static inline int
 maps_next (struct map_iterator *mi,
            unsigned long *low, unsigned long *high, unsigned long *offset)
 {
-    char perm[16], dash = 0, colon = 0, *cp;
-    unsigned long major, minor, inum;
-    ssize_t i, nread;
+  char perm[16], dash = 0, colon = 0, *cp;
+  unsigned long major, minor, inum;
+  ssize_t i, nread;
 
-    if (mi->fd < 0)
-        return 0;
+  if (mi->fd < 0)
+    return 0;
 
-    while (1)
+  while (1)
     {
-        ssize_t bytes_left = mi->buf_end - mi->buf;
-        char *eol = NULL;
+      ssize_t bytes_left = mi->buf_end - mi->buf;
+      char *eol = NULL;
 
-        for (i = 0; i < bytes_left; ++i)
+      for (i = 0; i < bytes_left; ++i)
         {
-            if (mi->buf[i] == '\n')
+          if (mi->buf[i] == '\n')
             {
+              eol = mi->buf + i;
+              break;
+            }
+          else if (mi->buf[i] == '\0')
+            break;
+        }
+      if (!eol)
+        {
+          /* copy down the remaining bytes, if any */
+          if (bytes_left > 0)
+            memmove (mi->buf_end - mi->buf_size, mi->buf, bytes_left);
+
+          mi->buf = mi->buf_end - mi->buf_size;
+          nread = read (mi->fd, mi->buf + bytes_left,
+                        mi->buf_size - bytes_left);
+          if (nread <= 0)
+            return 0;
+          else if ((size_t) (nread + bytes_left) < mi->buf_size)
+            {
+              /* Move contents to the end of the buffer so we
+                 maintain the invariant that all bytes between
+                 mi->buf and mi->buf_end are valid.  */
+              memmove (mi->buf_end - nread - bytes_left, mi->buf,
+                       nread + bytes_left);
+              mi->buf = mi->buf_end - nread - bytes_left;
+            }
+
+          eol = mi->buf + bytes_left + nread - 1;
+
+          for (i = bytes_left; i < bytes_left + nread; ++i)
+            if (mi->buf[i] == '\n')
+              {
                 eol = mi->buf + i;
                 break;
-            }
-            else if (mi->buf[i] == '\0')
-                break;
+              }
         }
-        if (!eol)
-        {
-            /* copy down the remaining bytes, if any */
-            if (bytes_left > 0)
-                memmove (mi->buf_end - mi->buf_size, mi->buf, bytes_left);
+      cp = mi->buf;
+      mi->buf = eol + 1;
+      *eol = '\0';
 
-            mi->buf = mi->buf_end - mi->buf_size;
-            nread = read (mi->fd, mi->buf + bytes_left,
-                          mi->buf_size - bytes_left);
-            if (nread <= 0)
-                return 0;
-            else if ((size_t) (nread + bytes_left) < mi->buf_size)
-            {
-                /* Move contents to the end of the buffer so we
-                maintain the invariant that all bytes between
-                 mi->buf and mi->buf_end are valid.  */
-                memmove (mi->buf_end - nread - bytes_left, mi->buf,
-                         nread + bytes_left);
-                mi->buf = mi->buf_end - nread - bytes_left;
-            }
-
-            eol = mi->buf + bytes_left + nread - 1;
-
-            for (i = bytes_left; i < bytes_left + nread; ++i)
-                if (mi->buf[i] == '\n')
-                {
-                    eol = mi->buf + i;
-                    break;
-                }
-        }
-        cp = mi->buf;
-        mi->buf = eol + 1;
-        *eol = '\0';
-
-        /* scan: "LOW-HIGH PERM OFFSET MAJOR:MINOR INUM PATH" */
-        cp = scan_hex (cp, low);
-        cp = scan_char (cp, &dash);
-        cp = scan_hex (cp, high);
-        cp = scan_string (cp, perm, sizeof (perm));
-        cp = scan_hex (cp, offset);
-        cp = scan_hex (cp, &major);
-        cp = scan_char (cp, &colon);
-        cp = scan_hex (cp, &minor);
-        cp = scan_dec (cp, &inum);
-        cp = mi->path = skip_whitespace (cp);
-        if (!cp)
-            continue;
-        cp = scan_string (cp, NULL, 0);
-        if (dash != '-' || colon != ':')
-            continue;	/* skip line with unknown or bad format */
-        return 1;
+      /* scan: "LOW-HIGH PERM OFFSET MAJOR:MINOR INUM PATH" */
+      cp = scan_hex (cp, low);
+      cp = scan_char (cp, &dash);
+      cp = scan_hex (cp, high);
+      cp = scan_string (cp, perm, sizeof (perm));
+      cp = scan_hex (cp, offset);
+      cp = scan_hex (cp, &major);
+      cp = scan_char (cp, &colon);
+      cp = scan_hex (cp, &minor);
+      cp = scan_dec (cp, &inum);
+      cp = mi->path = skip_whitespace (cp);
+      if (!cp)
+        continue;
+      cp = scan_string (cp, NULL, 0);
+      if (dash != '-' || colon != ':')
+        continue;       /* skip line with unknown or bad format */
+      return 1;
     }
-    return 0;
+  return 0;
 }
 
 static inline void
 maps_close (struct map_iterator *mi)
 {
-#if !HAVE_SGX
-    if (mi->fd < 0)
-        return;
-    close (mi->fd);
-    mi->fd = -1;
-    if (mi->buf)
+  if (mi->fd < 0)
+    return;
+  close (mi->fd);
+  mi->fd = -1;
+  if (mi->buf)
     {
-        munmap (mi->buf_end - mi->buf_size, mi->buf_size);
-        mi->buf = mi->buf_end = 0;
+      munmap (mi->buf_end - mi->buf_size, mi->buf_size);
+      mi->buf = mi->buf_end = NULL;
     }
-#endif
 }
 
 #endif /* os_linux_h */
diff --git a/sgx_unwind/libunwind/src/se-iterate-phdr.c b/sgx_unwind/libunwind/src/se-iterate-phdr.c
index cb31083..7af9ee4 100644
--- a/sgx_unwind/libunwind/src/se-iterate-phdr.c
+++ b/sgx_unwind/libunwind/src/se-iterate-phdr.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2019 Intel Corporation. All rights reserved.
+ * Copyright (C) 2011-2020 Intel Corporation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -37,7 +37,7 @@
 #endif
 #include <link.h>
 
-extern uint8_t __ImageBase;
+extern uint8_t  __ImageBase;
 
 /**
  * This function is commonly provided by glibc for application to walk
@@ -45,9 +45,9 @@
  * the libunwind code can work correctly.
  */
 int dl_iterate_phdr(
-    int (*callback) (struct dl_phdr_info *info,
-                     size_t size, void *data),
-    void *data)
+        int (*callback) (struct dl_phdr_info *info,
+                         size_t size, void *data),
+        void *data)
 {
     struct dl_phdr_info info;
     ElfW(Ehdr)         *ehdr;
diff --git a/sgx_unwind/libunwind/src/se-libc-stubs.c b/sgx_unwind/libunwind/src/se-libc-stubs.c
index edd0d51..bb94ece 100644
--- a/sgx_unwind/libunwind/src/se-libc-stubs.c
+++ b/sgx_unwind/libunwind/src/se-libc-stubs.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011-2019 Intel Corporation. All rights reserved.
+ * Copyright (C) 2011-2020 Intel Corporation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -51,13 +51,6 @@
     return SE_PAGE_SIZE;
 }
 
-int mincore(void *addr, size_t length, unsigned char *vec)
-{
-    assert(sgx_is_within_enclave(addr, length));
-
-    return 0;
-}
-
 char *strdup(const char *s)
 {
     size_t len = strlen(s) + 1;
@@ -88,7 +81,7 @@
  * we need to build it with SE tlibc headers.
  */
 void __assert_fail (const char *__assertion, const char *__file,
-                    unsigned int __line, __const char *__function)
+        unsigned int __line, __const char *__function)
 {
     abort();
 }
diff --git a/sgx_unwind/libunwind/src/setjmp/libunwind-setjmp.pc.in b/sgx_unwind/libunwind/src/setjmp/libunwind-setjmp.pc.in
new file mode 100644
index 0000000..7b71126
--- /dev/null
+++ b/sgx_unwind/libunwind/src/setjmp/libunwind-setjmp.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libunwind-setjmp
+Description: libunwind setjmp library
+Version: @VERSION@
+Requires: libunwind
+Libs: -L${libdir} -lunwind-setjmp
+Cflags: -I${includedir}
diff --git a/sgx_unwind/libunwind/src/setjmp/longjmp.c b/sgx_unwind/libunwind/src/setjmp/longjmp.c
index eba7cd6..8295a9b 100644
--- a/sgx_unwind/libunwind/src/setjmp/longjmp.c
+++ b/sgx_unwind/libunwind/src/setjmp/longjmp.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -57,59 +57,59 @@
 void
 _longjmp (jmp_buf env, int val)
 {
-    extern int _UI_longjmp_cont;
-    unw_context_t uc;
-    unw_cursor_t c;
-    unw_word_t sp;
-    unw_word_t *wp = (unw_word_t *) env;
+  extern int _UI_longjmp_cont;
+  unw_context_t uc;
+  unw_cursor_t c;
+  unw_word_t sp;
+  unw_word_t *wp = (unw_word_t *) env;
 
-    if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
-        abort ();
-
-    do
-    {
-        if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
-            abort ();
-#ifdef __FreeBSD__
-        if (sp != wp[JB_SP] + sizeof(unw_word_t))
-#else
-        if (sp != wp[JB_SP])
-#endif
-            continue;
-
-        if (!bsp_match (&c, wp))
-            continue;
-
-        /* found the right frame: */
-
-        assert (UNW_NUM_EH_REGS >= 2);
-
-        if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
-                || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
-                || unw_set_reg (&c, UNW_REG_IP,
-                                (unw_word_t) (uintptr_t) &_UI_longjmp_cont))
-            abort ();
-
-        unw_resume (&c);
-
-        abort ();
-    }
-    while (unw_step (&c) > 0);
-
+  if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
     abort ();
+
+  do
+    {
+      if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
+        abort ();
+#ifdef __FreeBSD__
+      if (sp != wp[JB_SP] + sizeof(unw_word_t))
+#else
+      if (sp != wp[JB_SP])
+#endif
+        continue;
+
+      if (!bsp_match (&c, wp))
+        continue;
+
+      /* found the right frame: */
+
+      assert (UNW_NUM_EH_REGS >= 2);
+
+      if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
+          || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
+          || unw_set_reg (&c, UNW_REG_IP,
+                          (unw_word_t) (uintptr_t) &_UI_longjmp_cont))
+        abort ();
+
+      unw_resume (&c);
+
+      abort ();
+    }
+  while (unw_step (&c) > 0);
+
+  abort ();
 }
 
 #ifdef __GNUC__
 #define STRINGIFY1(x) #x
 #define STRINGIFY(x) STRINGIFY1(x)
-void longjmp (jmp_buf env, int val)
-__attribute__ ((alias (STRINGIFY(_longjmp))));
+void longjmp (jmp_buf env, int val) 
+  __attribute__ ((alias (STRINGIFY(_longjmp))));
 #else
 
 void
 longjmp (jmp_buf env, int val)
 {
-    _longjmp (env, val);
+  _longjmp (env, val);
 }
 
 #endif /* __GNUC__ */
diff --git a/sgx_unwind/libunwind/src/setjmp/setjmp.c b/sgx_unwind/libunwind/src/setjmp/setjmp.c
index 7f614f2..bec9fc7 100644
--- a/sgx_unwind/libunwind/src/setjmp/setjmp.c
+++ b/sgx_unwind/libunwind/src/setjmp/setjmp.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -37,13 +37,13 @@
 
 int
 setjmp (env)
-jmp_buf env;
+     jmp_buf env;
 {
-    void **wp = (void **) env;
+  void **wp = (void **) env;
 
-    /* this should work on most platforms, but may not be
-       performance-optimal; check the code! */
-    wp[JB_SP] = __builtin_frame_address (0);
-    wp[JB_RP] = (void *) __builtin_return_address (0);
-    return 0;
+  /* this should work on most platforms, but may not be
+     performance-optimal; check the code! */
+  wp[JB_SP] = __builtin_frame_address (0);
+  wp[JB_RP] = (void *) __builtin_return_address (0);
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/setjmp/setjmp_i.h b/sgx_unwind/libunwind/src/setjmp/setjmp_i.h
index 7afeb71..4d91396 100644
--- a/sgx_unwind/libunwind/src/setjmp/setjmp_i.h
+++ b/sgx_unwind/libunwind/src/setjmp/setjmp_i.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -31,41 +31,41 @@
 static inline int
 bsp_match (unw_cursor_t *c, unw_word_t *wp)
 {
-    unw_word_t bsp, pfs, sol;
+  unw_word_t bsp, pfs, sol;
 
-    if (unw_get_reg (c, UNW_IA64_BSP, &bsp) < 0
-            || unw_get_reg (c, UNW_IA64_AR_PFS, &pfs) < 0)
-        abort ();
+  if (unw_get_reg (c, UNW_IA64_BSP, &bsp) < 0
+      || unw_get_reg (c, UNW_IA64_AR_PFS, &pfs) < 0)
+    abort ();
 
-    /* simulate the effect of "br.call sigsetjmp" on ar.bsp: */
-    sol = (pfs >> 7) & 0x7f;
-    bsp = rse_skip_regs (bsp, sol);
+  /* simulate the effect of "br.call sigsetjmp" on ar.bsp: */
+  sol = (pfs >> 7) & 0x7f;
+  bsp = rse_skip_regs (bsp, sol);
 
-    if (bsp != wp[JB_BSP])
-        return 0;
+  if (bsp != wp[JB_BSP])
+    return 0;
 
-    if (unlikely (sol == 0))
+  if (unlikely (sol == 0))
     {
-        unw_word_t sp, prev_sp;
-        unw_cursor_t tmp = *c;
+      unw_word_t sp, prev_sp;
+      unw_cursor_t tmp = *c;
 
-        /* The caller of {sig,}setjmp() cannot have a NULL-frame.  If we
-        see a NULL-frame, we haven't reached the right target yet.
+      /* The caller of {sig,}setjmp() cannot have a NULL-frame.  If we
+         see a NULL-frame, we haven't reached the right target yet.
          To have a NULL-frame, the number of locals must be zero and
          the stack-frame must also be empty.  */
 
-        if (unw_step (&tmp) < 0)
-            abort ();
+      if (unw_step (&tmp) < 0)
+        abort ();
 
-        if (unw_get_reg (&tmp, UNW_REG_SP, &sp) < 0
-                || unw_get_reg (&tmp, UNW_REG_SP, &prev_sp) < 0)
-            abort ();
+      if (unw_get_reg (&tmp, UNW_REG_SP, &sp) < 0
+          || unw_get_reg (&tmp, UNW_REG_SP, &prev_sp) < 0)
+        abort ();
 
-        if (sp == prev_sp)
-            /* got a NULL-frame; keep looking... */
-            return 0;
+      if (sp == prev_sp)
+        /* got a NULL-frame; keep looking... */
+        return 0;
     }
-    return 1;
+  return 1;
 }
 
 /* On ia64 we cannot always call sigprocmask() at
@@ -78,26 +78,26 @@
 static inline int
 resume_restores_sigmask (unw_cursor_t *c, unw_word_t *wp)
 {
-    unw_word_t sc_addr = ((struct cursor *) c)->sigcontext_addr;
-    struct sigcontext *sc = (struct sigcontext *) sc_addr;
-    sigset_t current_mask;
-    void *mp;
+  unw_word_t sc_addr = ((struct cursor *) c)->sigcontext_addr;
+  struct sigcontext *sc = (struct sigcontext *) sc_addr;
+  sigset_t current_mask;
+  void *mp;
 
-    if (!sc_addr)
-        return 0;
+  if (!sc_addr)
+    return 0;
 
-    /* let unw_resume() install the desired signal mask */
+  /* let unw_resume() install the desired signal mask */
 
-    if (wp[JB_MASK_SAVED])
-        mp = &wp[JB_MASK];
-    else
+  if (wp[JB_MASK_SAVED])
+    mp = &wp[JB_MASK];
+  else
     {
-        if (sigprocmask (SIG_BLOCK, NULL, &current_mask) < 0)
-            abort ();
-        mp = &current_mask;
+      if (sigprocmask (SIG_BLOCK, NULL, &current_mask) < 0)
+        abort ();
+      mp = &current_mask;
     }
-    memcpy (&sc->sc_mask, mp, sizeof (sc->sc_mask));
-    return 1;
+  memcpy (&sc->sc_mask, mp, sizeof (sc->sc_mask));
+  return 1;
 }
 
 #else /* !UNW_TARGET_IA64 */
@@ -105,14 +105,14 @@
 static inline int
 bsp_match (unw_cursor_t *c, unw_word_t *wp)
 {
-    return 1;
+  return 1;
 }
 
 static inline int
 resume_restores_sigmask (unw_cursor_t *c, unw_word_t *wp)
 {
-    /* We may want to do this analogously as for ia64... */
-    return 0;
+  /* We may want to do this analogously as for ia64... */
+  return 0;
 }
 
 #endif /* !UNW_TARGET_IA64 */
diff --git a/sgx_unwind/libunwind/src/setjmp/siglongjmp.c b/sgx_unwind/libunwind/src/setjmp/siglongjmp.c
index e1857af..0e286f6 100644
--- a/sgx_unwind/libunwind/src/setjmp/siglongjmp.c
+++ b/sgx_unwind/libunwind/src/setjmp/siglongjmp.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -26,7 +26,6 @@
 #define UNW_LOCAL_ONLY
 
 #include <setjmp.h>
-#include <signal.h>
 
 #include "libunwind_i.h"
 #include "jmpbuf.h"
@@ -50,78 +49,79 @@
    so we simply defer to glibc siglongjmp here.  */
 
 #define siglongjmp __nonworking_siglongjmp
-static void siglongjmp (sigjmp_buf env, int val);
+static void siglongjmp (sigjmp_buf env, int val) UNUSED;
 #endif
 #endif /* __GLIBC_PREREQ */
 
 void
 siglongjmp (sigjmp_buf env, int val)
 {
-    unw_word_t *wp = (unw_word_t *) env;
-    extern int _UI_siglongjmp_cont;
-    extern int _UI_longjmp_cont;
-    unw_context_t uc;
-    unw_cursor_t c;
-    unw_word_t sp;
-    int *cont;
+  unw_word_t *wp = (unw_word_t *) env;
+  extern int _UI_siglongjmp_cont;
+  extern int _UI_longjmp_cont;
+  unw_context_t uc;
+  unw_cursor_t c;
+  unw_word_t sp;
+  int *cont;
 
-    if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
-        abort ();
+  if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0)
+    abort ();
 
-    do
+  do
     {
-        if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
-            abort ();
+      if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0)
+        abort ();
 #ifdef __FreeBSD__
-        if (sp != wp[JB_SP] + sizeof(unw_word_t))
+      if (sp != wp[JB_SP] + sizeof(unw_word_t))
 #else
-        if (sp != wp[JB_SP])
+      if (sp != wp[JB_SP])
 #endif
-            continue;
+        continue;
 
-        if (!bsp_match (&c, wp))
-            continue;
+      if (!bsp_match (&c, wp))
+        continue;
 
-        /* found the right frame: */
+      /* found the right frame: */
 
-        /* default to resuming without restoring signal-mask */
-        cont = &_UI_longjmp_cont;
+      /* default to resuming without restoring signal-mask */
+      cont = &_UI_longjmp_cont;
 
-        /* Order of evaluation is important here: if unw_resume()
-        restores signal mask, we must set it up appropriately, even
+      /* Order of evaluation is important here: if unw_resume()
+         restores signal mask, we must set it up appropriately, even
          if wp[JB_MASK_SAVED] is FALSE.  */
-        if (!resume_restores_sigmask (&c, wp) && wp[JB_MASK_SAVED])
+      if (!resume_restores_sigmask (&c, wp) && wp[JB_MASK_SAVED])
         {
-            /* sigmask was saved */
+          /* sigmask was saved */
 #if defined(__linux__)
-            if (UNW_NUM_EH_REGS < 4 || _NSIG > 16 * sizeof (unw_word_t))
-                /* signal mask doesn't fit into EH arguments and we can't
-                   put it on the stack without overwriting something
-                   else... */
-                abort ();
-            else if (unw_set_reg (&c, UNW_REG_EH + 2, wp[JB_MASK]) < 0
-                     || (_NSIG > 8 * sizeof (unw_word_t)
-                         && unw_set_reg (&c, UNW_REG_EH + 3, wp[JB_MASK + 1]) < 0))
-                abort ();
+          if (UNW_NUM_EH_REGS < 4 || _NSIG > 16 * sizeof (unw_word_t))
+            /* signal mask doesn't fit into EH arguments and we can't
+               put it on the stack without overwriting something
+               else... */
+            abort ();
+          else
+            if (unw_set_reg (&c, UNW_REG_EH + 2, wp[JB_MASK]) < 0
+                || (_NSIG > 8 * sizeof (unw_word_t)
+                    && unw_set_reg (&c, UNW_REG_EH + 3, wp[JB_MASK + 1]) < 0))
+              abort ();
 #elif defined(__FreeBSD__)
-            if (unw_set_reg (&c, UNW_REG_EH + 2, &wp[JB_MASK]) < 0)
-                abort();
+          if (unw_set_reg (&c, UNW_REG_EH + 2, &wp[JB_MASK]) < 0)
+              abort();
 #else
 #error Port me
 #endif
-            cont = &_UI_siglongjmp_cont;
+          cont = &_UI_siglongjmp_cont;
         }
 
-        if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
-                || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
-                || unw_set_reg (&c, UNW_REG_IP, (unw_word_t) (uintptr_t) cont))
-            abort ();
-
-        unw_resume (&c);
-
+      if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0
+          || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0
+          || unw_set_reg (&c, UNW_REG_IP, (unw_word_t) (uintptr_t) cont))
         abort ();
-    }
-    while (unw_step (&c) > 0);
 
-    abort ();
+      unw_resume (&c);
+
+      abort ();
+    }
+  while (unw_step (&c) > 0);
+
+  abort ();
 }
diff --git a/sgx_unwind/libunwind/src/setjmp/sigsetjmp.c b/sgx_unwind/libunwind/src/setjmp/sigsetjmp.c
index ff98da6..f84935d 100644
--- a/sgx_unwind/libunwind/src/setjmp/sigsetjmp.c
+++ b/sgx_unwind/libunwind/src/setjmp/sigsetjmp.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -32,19 +32,19 @@
 int
 sigsetjmp (sigjmp_buf env, int savemask)
 {
-    unw_word_t *wp = (unw_word_t *) env;
+  unw_word_t *wp = (unw_word_t *) env;
 
-    /* This should work on most platforms, but may not be
-       performance-optimal; check the code! */
+  /* This should work on most platforms, but may not be
+     performance-optimal; check the code! */
 
-    wp[JB_SP] = (unw_word_t) __builtin_frame_address (0);
-    wp[JB_RP] = (unw_word_t) __builtin_return_address (0);
-    wp[JB_MASK_SAVED] = savemask;
+  wp[JB_SP] = (unw_word_t) __builtin_frame_address (0);
+  wp[JB_RP] = (unw_word_t) __builtin_return_address (0);
+  wp[JB_MASK_SAVED] = savemask;
 
-    /* Note: we assume here that "wp" has same or better alignment as
-       sigset_t.  */
-    if (savemask
-            && sigprocmask (SIG_BLOCK, NULL, (sigset_t *) (wp + JB_MASK)) < 0)
-        abort ();
-    return 0;
+  /* Note: we assume here that "wp" has same or better alignment as
+     sigset_t.  */
+  if (savemask
+      && sigprocmask (SIG_BLOCK, NULL, (sigset_t *) (wp + JB_MASK)) < 0)
+    abort ();
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/unwind/Backtrace.c b/sgx_unwind/libunwind/src/unwind/Backtrace.c
index 3d4738d..0b14df4 100644
--- a/sgx_unwind/libunwind/src/unwind/Backtrace.c
+++ b/sgx_unwind/libunwind/src/unwind/Backtrace.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,32 +25,32 @@
 
 #include "unwind-internal.h"
 
-PROTECTED _Unwind_Reason_Code
+_Unwind_Reason_Code
 _Unwind_Backtrace (_Unwind_Trace_Fn trace, void *trace_parameter)
 {
-    struct _Unwind_Context context;
-    unw_context_t uc;
-    int ret;
+  struct _Unwind_Context context;
+  unw_context_t uc;
+  int ret;
 
-    if (_Unwind_InitContext (&context, &uc) < 0)
-        return _URC_FATAL_PHASE1_ERROR;
+  if (_Unwind_InitContext (&context, &uc) < 0)
+    return _URC_FATAL_PHASE1_ERROR;
 
-    /* Phase 1 (search phase) */
+  /* Phase 1 (search phase) */
 
-    while (1)
+  while (1)
     {
-        if ((ret = unw_step (&context.cursor)) <= 0)
+      if ((ret = unw_step (&context.cursor)) <= 0)
         {
-            if (ret == 0)
-                return _URC_END_OF_STACK;
-            else
-                return _URC_FATAL_PHASE1_ERROR;
+          if (ret == 0)
+            return _URC_END_OF_STACK;
+          else
+            return _URC_FATAL_PHASE1_ERROR;
         }
 
-        if ((*trace) (&context, trace_parameter) != _URC_NO_REASON)
-            return _URC_FATAL_PHASE1_ERROR;
+      if ((*trace) (&context, trace_parameter) != _URC_NO_REASON)
+        return _URC_FATAL_PHASE1_ERROR;
     }
 }
 
 _Unwind_Reason_Code __libunwind_Unwind_Backtrace (_Unwind_Trace_Fn, void *)
-ALIAS (_Unwind_Backtrace);
+     ALIAS (_Unwind_Backtrace);
diff --git a/sgx_unwind/libunwind/src/unwind/DeleteException.c b/sgx_unwind/libunwind/src/unwind/DeleteException.c
index 64fea4e..ad38eaf 100644
--- a/sgx_unwind/libunwind/src/unwind/DeleteException.c
+++ b/sgx_unwind/libunwind/src/unwind/DeleteException.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,14 +25,14 @@
 
 #include "unwind-internal.h"
 
-PROTECTED void
+void
 _Unwind_DeleteException (struct _Unwind_Exception *exception_object)
 {
-    _Unwind_Exception_Cleanup_Fn cleanup = exception_object->exception_cleanup;
+  _Unwind_Exception_Cleanup_Fn cleanup = exception_object->exception_cleanup;
 
-    if (cleanup)
-        (*cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exception_object);
+  if (cleanup)
+    (*cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exception_object);
 }
 
 void __libunwind_Unwind_DeleteException (struct _Unwind_Exception *)
-ALIAS (_Unwind_DeleteException);
+     ALIAS (_Unwind_DeleteException);
diff --git a/sgx_unwind/libunwind/src/unwind/FindEnclosingFunction.c b/sgx_unwind/libunwind/src/unwind/FindEnclosingFunction.c
index 643dbf9..4f10666 100644
--- a/sgx_unwind/libunwind/src/unwind/FindEnclosingFunction.c
+++ b/sgx_unwind/libunwind/src/unwind/FindEnclosingFunction.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,18 +25,18 @@
 
 #include "unwind-internal.h"
 
-PROTECTED void *
+void *
 _Unwind_FindEnclosingFunction (void *ip)
 {
-    unw_proc_info_t pi;
+  unw_proc_info_t pi;
 
-    if (unw_get_proc_info_by_ip (unw_local_addr_space,
-                                 (unw_word_t) (uintptr_t) ip, &pi, 0)
-            < 0)
-        return NULL;
+  if (unw_get_proc_info_by_ip (unw_local_addr_space,
+                               (unw_word_t) (uintptr_t) ip, &pi, 0)
+      < 0)
+    return NULL;
 
-    return (void *) (uintptr_t) pi.start_ip;
+  return (void *) (uintptr_t) pi.start_ip;
 }
 
 void *__libunwind_Unwind_FindEnclosingFunction (void *)
-ALIAS (_Unwind_FindEnclosingFunction);
+     ALIAS (_Unwind_FindEnclosingFunction);
diff --git a/sgx_unwind/libunwind/src/unwind/ForcedUnwind.c b/sgx_unwind/libunwind/src/unwind/ForcedUnwind.c
index c83a5bc..905b31c 100644
--- a/sgx_unwind/libunwind/src/unwind/ForcedUnwind.c
+++ b/sgx_unwind/libunwind/src/unwind/ForcedUnwind.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,28 +25,28 @@
 
 #include "unwind-internal.h"
 
-PROTECTED _Unwind_Reason_Code
+_Unwind_Reason_Code
 _Unwind_ForcedUnwind (struct _Unwind_Exception *exception_object,
                       _Unwind_Stop_Fn stop, void *stop_parameter)
 {
-    struct _Unwind_Context context;
-    unw_context_t uc;
+  struct _Unwind_Context context;
+  unw_context_t uc;
 
-    /* We check "stop" here to tell the compiler's inliner that
-       exception_object->private_1 isn't NULL when calling
-       _Unwind_Phase2().  */
-    if (!stop)
-        return _URC_FATAL_PHASE2_ERROR;
+  /* We check "stop" here to tell the compiler's inliner that
+     exception_object->private_1 isn't NULL when calling
+     _Unwind_Phase2().  */
+  if (!stop)
+    return _URC_FATAL_PHASE2_ERROR;
 
-    if (_Unwind_InitContext (&context, &uc) < 0)
-        return _URC_FATAL_PHASE2_ERROR;
+  if (_Unwind_InitContext (&context, &uc) < 0)
+    return _URC_FATAL_PHASE2_ERROR;
 
-    exception_object->private_1 = (unsigned long) stop;
-    exception_object->private_2 = (unsigned long) stop_parameter;
+  exception_object->private_1 = (unsigned long) stop;
+  exception_object->private_2 = (unsigned long) stop_parameter;
 
-    return _Unwind_Phase2 (exception_object, &context);
+  return _Unwind_Phase2 (exception_object, &context);
 }
 
 _Unwind_Reason_Code __libunwind_Unwind_ForcedUnwind (struct _Unwind_Exception*,
-        _Unwind_Stop_Fn, void *)
-ALIAS (_Unwind_ForcedUnwind);
+                                                     _Unwind_Stop_Fn, void *)
+     ALIAS (_Unwind_ForcedUnwind);
diff --git a/sgx_unwind/libunwind/src/unwind/GetBSP.c b/sgx_unwind/libunwind/src/unwind/GetBSP.c
index 726e081..d1bc84e 100644
--- a/sgx_unwind/libunwind/src/unwind/GetBSP.c
+++ b/sgx_unwind/libunwind/src/unwind/GetBSP.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,18 +25,18 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetBSP (struct _Unwind_Context *context)
 {
 #ifdef UNW_TARGET_IA64
-    unw_word_t val;
+  unw_word_t val;
 
-    unw_get_reg (&context->cursor, UNW_IA64_BSP, &val);
-    return val;
+  unw_get_reg (&context->cursor, UNW_IA64_BSP, &val);
+  return val;
 #else
-    return 0;
+  return 0;
 #endif
 }
 
 unsigned long __libunwind_Unwind_GetBSP (struct _Unwind_Context *)
-ALIAS (_Unwind_GetBSP);
+     ALIAS (_Unwind_GetBSP);
diff --git a/sgx_unwind/libunwind/src/unwind/GetCFA.c b/sgx_unwind/libunwind/src/unwind/GetCFA.c
index e07bace..5ca6390 100644
--- a/sgx_unwind/libunwind/src/unwind/GetCFA.c
+++ b/sgx_unwind/libunwind/src/unwind/GetCFA.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,14 +25,14 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetCFA (struct _Unwind_Context *context)
 {
-    unw_word_t val;
+  unw_word_t val;
 
-    unw_get_reg (&context->cursor, UNW_REG_SP, &val);
-    return val;
+  unw_get_reg (&context->cursor, UNW_REG_SP, &val);
+  return val;
 }
 
 unsigned long __libunwind_Unwind_GetCFA (struct _Unwind_Context *)
-ALIAS (_Unwind_GetCFA);
+     ALIAS (_Unwind_GetCFA);
diff --git a/sgx_unwind/libunwind/src/unwind/GetDataRelBase.c b/sgx_unwind/libunwind/src/unwind/GetDataRelBase.c
index 5b66dd1..8e6914f 100644
--- a/sgx_unwind/libunwind/src/unwind/GetDataRelBase.c
+++ b/sgx_unwind/libunwind/src/unwind/GetDataRelBase.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,15 +25,15 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
 {
-    unw_proc_info_t pi;
+  unw_proc_info_t pi;
 
-    pi.gp = 0;
-    unw_get_proc_info (&context->cursor, &pi);
-    return pi.gp;
+  pi.gp = 0;
+  unw_get_proc_info (&context->cursor, &pi);
+  return pi.gp;
 }
 
 unsigned long __libunwind_Unwind_GetDataRelBase (struct _Unwind_Context *)
-ALIAS (_Unwind_GetDataRelBase);
+     ALIAS (_Unwind_GetDataRelBase);
diff --git a/sgx_unwind/libunwind/src/unwind/GetGR.c b/sgx_unwind/libunwind/src/unwind/GetGR.c
index a65d679..fa70943 100644
--- a/sgx_unwind/libunwind/src/unwind/GetGR.c
+++ b/sgx_unwind/libunwind/src/unwind/GetGR.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,19 +25,19 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetGR (struct _Unwind_Context *context, int index)
 {
-    unw_word_t val;
+  unw_word_t val;
 
-    if (index == UNW_REG_SP && context->end_of_stack)
-        /* _Unwind_ForcedUnwind() requires us to return a NULL
-           stack-pointer after reaching the end of the stack.  */
-        return 0;
+  if (index == UNW_REG_SP && context->end_of_stack)
+    /* _Unwind_ForcedUnwind() requires us to return a NULL
+       stack-pointer after reaching the end of the stack.  */
+    return 0;
 
-    unw_get_reg (&context->cursor, index, &val);
-    return val;
+  unw_get_reg (&context->cursor, index, &val);
+  return val;
 }
 
 unsigned long __libunwind_Unwind_GetGR (struct _Unwind_Context *, int)
-ALIAS (_Unwind_GetGR);
+     ALIAS (_Unwind_GetGR);
diff --git a/sgx_unwind/libunwind/src/unwind/GetIP.c b/sgx_unwind/libunwind/src/unwind/GetIP.c
index 9c8053f..e9fc494 100644
--- a/sgx_unwind/libunwind/src/unwind/GetIP.c
+++ b/sgx_unwind/libunwind/src/unwind/GetIP.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,14 +25,14 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetIP (struct _Unwind_Context *context)
 {
-    unw_word_t val;
+  unw_word_t val;
 
-    unw_get_reg (&context->cursor, UNW_REG_IP, &val);
-    return val;
+  unw_get_reg (&context->cursor, UNW_REG_IP, &val);
+  return val;
 }
 
 unsigned long __libunwind_Unwind_GetIP (struct _Unwind_Context *)
-ALIAS (_Unwind_GetIP);
+     ALIAS (_Unwind_GetIP);
diff --git a/sgx_unwind/libunwind/src/unwind/GetIPInfo.c b/sgx_unwind/libunwind/src/unwind/GetIPInfo.c
index b00daa3..e8ee7fd 100644
--- a/sgx_unwind/libunwind/src/unwind/GetIPInfo.c
+++ b/sgx_unwind/libunwind/src/unwind/GetIPInfo.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2009 Red Hat
-	Contributed by Jan Kratochvil <jan.kratochvil@redhat.com>
+        Contributed by Jan Kratochvil <jan.kratochvil@redhat.com>
 
 This file is part of libunwind.
 
@@ -28,15 +28,15 @@
 /* gcc/unwind-dw2.c: Retrieve the return address and flag whether that IP is
    before or after first not yet fully executed instruction.  */
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
 {
-    unw_word_t val;
+  unw_word_t val;
 
-    unw_get_reg (&context->cursor, UNW_REG_IP, &val);
-    *ip_before_insn = unw_is_signal_frame (&context->cursor);
-    return val;
+  unw_get_reg (&context->cursor, UNW_REG_IP, &val);
+  *ip_before_insn = unw_is_signal_frame (&context->cursor);
+  return val;
 }
 
 unsigned long __libunwind_Unwind_GetIPInfo (struct _Unwind_Context *, int *)
-ALIAS (_Unwind_GetIPInfo);
+     ALIAS (_Unwind_GetIPInfo);
diff --git a/sgx_unwind/libunwind/src/unwind/GetLanguageSpecificData.c b/sgx_unwind/libunwind/src/unwind/GetLanguageSpecificData.c
index c65710d..e7ca9b4 100644
--- a/sgx_unwind/libunwind/src/unwind/GetLanguageSpecificData.c
+++ b/sgx_unwind/libunwind/src/unwind/GetLanguageSpecificData.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,16 +25,16 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
 {
-    unw_proc_info_t pi;
+  unw_proc_info_t pi;
 
-    pi.lsda = 0;
-    unw_get_proc_info (&context->cursor, &pi);
-    return pi.lsda;
+  pi.lsda = 0;
+  unw_get_proc_info (&context->cursor, &pi);
+  return pi.lsda;
 }
 
 unsigned long
 __libunwind_Unwind_GetLanguageSpecificData (struct _Unwind_Context *)
-ALIAS (_Unwind_GetLanguageSpecificData);
+     ALIAS (_Unwind_GetLanguageSpecificData);
diff --git a/sgx_unwind/libunwind/src/unwind/GetRegionStart.c b/sgx_unwind/libunwind/src/unwind/GetRegionStart.c
index f46967e..f499581 100644
--- a/sgx_unwind/libunwind/src/unwind/GetRegionStart.c
+++ b/sgx_unwind/libunwind/src/unwind/GetRegionStart.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,15 +25,15 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetRegionStart (struct _Unwind_Context *context)
 {
-    unw_proc_info_t pi;
+  unw_proc_info_t pi;
 
-    pi.start_ip = 0;
-    unw_get_proc_info (&context->cursor, &pi);
-    return pi.start_ip;
+  pi.start_ip = 0;
+  unw_get_proc_info (&context->cursor, &pi);
+  return pi.start_ip;
 }
 
 unsigned long __libunwind_Unwind_GetRegionStart (struct _Unwind_Context *)
-ALIAS (_Unwind_GetRegionStart);
+     ALIAS (_Unwind_GetRegionStart);
diff --git a/sgx_unwind/libunwind/src/unwind/GetTextRelBase.c b/sgx_unwind/libunwind/src/unwind/GetTextRelBase.c
index 451be8e..ce65ae9 100644
--- a/sgx_unwind/libunwind/src/unwind/GetTextRelBase.c
+++ b/sgx_unwind/libunwind/src/unwind/GetTextRelBase.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,11 +25,11 @@
 
 #include "unwind-internal.h"
 
-PROTECTED unsigned long
+unsigned long
 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
 {
-    return 0;
+  return 0;
 }
 
 unsigned long __libunwind_Unwind_GetTextRelBase (struct _Unwind_Context *)
-ALIAS (_Unwind_GetTextRelBase);
+     ALIAS (_Unwind_GetTextRelBase);
diff --git a/sgx_unwind/libunwind/src/unwind/RaiseException.c b/sgx_unwind/libunwind/src/unwind/RaiseException.c
index dfd62ff..3c3ca19 100644
--- a/sgx_unwind/libunwind/src/unwind/RaiseException.c
+++ b/sgx_unwind/libunwind/src/unwind/RaiseException.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,79 +25,79 @@
 
 #include "unwind-internal.h"
 
-PROTECTED _Unwind_Reason_Code
+_Unwind_Reason_Code
 _Unwind_RaiseException (struct _Unwind_Exception *exception_object)
 {
-    uint64_t exception_class = exception_object->exception_class;
-    _Unwind_Personality_Fn personality;
-    struct _Unwind_Context context;
-    _Unwind_Reason_Code reason;
-    unw_proc_info_t pi;
-    unw_context_t uc;
-    unw_word_t ip;
-    int ret;
+  uint64_t exception_class = exception_object->exception_class;
+  _Unwind_Personality_Fn personality;
+  struct _Unwind_Context context;
+  _Unwind_Reason_Code reason;
+  unw_proc_info_t pi;
+  unw_context_t uc;
+  unw_word_t ip;
+  int ret;
 
-    Debug (1, "(exception_object=%p)\n", exception_object);
+  Debug (1, "(exception_object=%p)\n", exception_object);
 
-    if (_Unwind_InitContext (&context, &uc) < 0)
-        return _URC_FATAL_PHASE1_ERROR;
+  if (_Unwind_InitContext (&context, &uc) < 0)
+    return _URC_FATAL_PHASE1_ERROR;
 
-    /* Phase 1 (search phase) */
+  /* Phase 1 (search phase) */
 
-    while (1)
+  while (1)
     {
-        if ((ret = unw_step (&context.cursor)) <= 0)
+      if ((ret = unw_step (&context.cursor)) <= 0)
         {
-            if (ret == 0)
+          if (ret == 0)
             {
-                Debug (1, "no handler found\n");
-                return _URC_END_OF_STACK;
+              Debug (1, "no handler found\n");
+              return _URC_END_OF_STACK;
             }
-            else
-                return _URC_FATAL_PHASE1_ERROR;
+          else
+            return _URC_FATAL_PHASE1_ERROR;
         }
 
-        if (unw_get_proc_info (&context.cursor, &pi) < 0)
-            return _URC_FATAL_PHASE1_ERROR;
+      if (unw_get_proc_info (&context.cursor, &pi) < 0)
+        return _URC_FATAL_PHASE1_ERROR;
 
-        personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
-        if (personality)
+      personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
+      if (personality)
         {
-            reason = (*personality) (_U_VERSION, _UA_SEARCH_PHASE,
-                                     exception_class, exception_object,
-                                     &context);
-            if (reason != _URC_CONTINUE_UNWIND)
+          reason = (*personality) (_U_VERSION, _UA_SEARCH_PHASE,
+                                   exception_class, exception_object,
+                                   &context);
+          if (reason != _URC_CONTINUE_UNWIND)
             {
-                if (reason == _URC_HANDLER_FOUND)
-                    break;
-                else
+              if (reason == _URC_HANDLER_FOUND)
+                break;
+              else
                 {
-                    Debug (1, "personality returned %d\n", reason);
-                    return _URC_FATAL_PHASE1_ERROR;
+                  Debug (1, "personality returned %d\n", reason);
+                  return _URC_FATAL_PHASE1_ERROR;
                 }
             }
         }
     }
 
-    /* Exceptions are associated with IP-ranges.  If a given exception
-       is handled at a particular IP, it will _always_ be handled at
-       that IP.  If this weren't true, we'd have to track the tuple
-       (IP,SP,BSP) to uniquely identify the stack frame that's handling
-       the exception.  */
-    if (unw_get_reg (&context.cursor, UNW_REG_IP, &ip) < 0)
-        return _URC_FATAL_PHASE1_ERROR;
-    exception_object->private_1 = 0;	/* clear "stop" pointer */
-    exception_object->private_2 = ip;	/* save frame marker */
+  /* Exceptions are associated with IP-ranges.  If a given exception
+     is handled at a particular IP, it will _always_ be handled at
+     that IP.  If this weren't true, we'd have to track the tuple
+     (IP,SP,BSP) to uniquely identify the stack frame that's handling
+     the exception.  */
+  if (unw_get_reg (&context.cursor, UNW_REG_IP, &ip) < 0)
+    return _URC_FATAL_PHASE1_ERROR;
+  exception_object->private_1 = 0;      /* clear "stop" pointer */
+  exception_object->private_2 = ip;     /* save frame marker */
 
-    Debug (1, "found handler for IP=%lx; entering cleanup phase\n", (long) ip);
+  Debug (1, "found handler for IP=%lx; entering cleanup phase\n", (long) ip);
 
-    /* Reset the cursor to the first frame: */
-    if (unw_init_local (&context.cursor, &uc) < 0)
-        return _URC_FATAL_PHASE1_ERROR;
+  /* Reset the cursor to the first frame: */
+  if (unw_init_local (&context.cursor, &uc) < 0)
+    return _URC_FATAL_PHASE1_ERROR;
 
-    return _Unwind_Phase2 (exception_object, &context);
+  return _Unwind_Phase2 (exception_object, &context);
 }
 
 _Unwind_Reason_Code
 __libunwind_Unwind_RaiseException (struct _Unwind_Exception *)
-ALIAS (_Unwind_RaiseException);
+     ALIAS (_Unwind_RaiseException);
diff --git a/sgx_unwind/libunwind/src/unwind/Resume.c b/sgx_unwind/libunwind/src/unwind/Resume.c
index 273141c..e23d6be 100644
--- a/sgx_unwind/libunwind/src/unwind/Resume.c
+++ b/sgx_unwind/libunwind/src/unwind/Resume.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,18 +25,18 @@
 
 #include "unwind-internal.h"
 
-PROTECTED void
+void
 _Unwind_Resume (struct _Unwind_Exception *exception_object)
 {
-    struct _Unwind_Context context;
-    unw_context_t uc;
+  struct _Unwind_Context context;
+  unw_context_t uc;
 
-    if (_Unwind_InitContext (&context, &uc) < 0)
-        abort ();
-
-    _Unwind_Phase2 (exception_object, &context);
+  if (_Unwind_InitContext (&context, &uc) < 0)
     abort ();
+
+  _Unwind_Phase2 (exception_object, &context);
+  abort ();
 }
 
 void __libunwind_Unwind_Resume (struct _Unwind_Exception *)
-ALIAS (_Unwind_Resume);
+     ALIAS (_Unwind_Resume);
diff --git a/sgx_unwind/libunwind/src/unwind/Resume_or_Rethrow.c b/sgx_unwind/libunwind/src/unwind/Resume_or_Rethrow.c
index 2be559e..9c76443 100644
--- a/sgx_unwind/libunwind/src/unwind/Resume_or_Rethrow.c
+++ b/sgx_unwind/libunwind/src/unwind/Resume_or_Rethrow.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,23 +25,23 @@
 
 #include "unwind-internal.h"
 
-PROTECTED _Unwind_Reason_Code
+_Unwind_Reason_Code
 _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exception_object)
 {
-    struct _Unwind_Context context;
-    unw_context_t uc;
+  struct _Unwind_Context context;
+  unw_context_t uc;
 
-    if (exception_object->private_1)
+  if (exception_object->private_1)
     {
-        if (_Unwind_InitContext (&context, &uc) < 0)
-            return _URC_FATAL_PHASE2_ERROR;
+      if (_Unwind_InitContext (&context, &uc) < 0)
+        return _URC_FATAL_PHASE2_ERROR;
 
-        return _Unwind_Phase2 (exception_object, &context);
+      return _Unwind_Phase2 (exception_object, &context);
     }
-    else
-        return _Unwind_RaiseException (exception_object);
+  else
+    return _Unwind_RaiseException (exception_object);
 }
 
 _Unwind_Reason_Code
 __libunwind_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *)
-ALIAS (_Unwind_Resume_or_Rethrow);
+     ALIAS (_Unwind_Resume_or_Rethrow);
diff --git a/sgx_unwind/libunwind/src/unwind/SetGR.c b/sgx_unwind/libunwind/src/unwind/SetGR.c
index b798de8..ae77a8e 100644
--- a/sgx_unwind/libunwind/src/unwind/SetGR.c
+++ b/sgx_unwind/libunwind/src/unwind/SetGR.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -28,20 +28,20 @@
 #include "dwarf_i.h"
 #endif
 
-PROTECTED void
+void
 _Unwind_SetGR (struct _Unwind_Context *context, int index,
                unsigned long new_value)
 {
 #ifdef UNW_TARGET_X86
-    index = dwarf_to_unw_regnum(index);
+  index = dwarf_to_unw_regnum(index);
 #endif
-    unw_set_reg (&context->cursor, index, new_value);
+  unw_set_reg (&context->cursor, index, new_value);
 #ifdef UNW_TARGET_IA64
-    if (index >= UNW_IA64_GR && index <= UNW_IA64_GR + 127)
-        /* Clear the NaT bit. */
-        unw_set_reg (&context->cursor, UNW_IA64_NAT + (index - UNW_IA64_GR), 0);
+  if (index >= UNW_IA64_GR && index <= UNW_IA64_GR + 127)
+    /* Clear the NaT bit. */
+    unw_set_reg (&context->cursor, UNW_IA64_NAT + (index - UNW_IA64_GR), 0);
 #endif
 }
 
 void __libunwind_Unwind_SetGR (struct _Unwind_Context *, int, unsigned long)
-ALIAS (_Unwind_SetGR);
+     ALIAS (_Unwind_SetGR);
diff --git a/sgx_unwind/libunwind/src/unwind/SetIP.c b/sgx_unwind/libunwind/src/unwind/SetIP.c
index d0d10fc..fccc2f0 100644
--- a/sgx_unwind/libunwind/src/unwind/SetIP.c
+++ b/sgx_unwind/libunwind/src/unwind/SetIP.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -25,11 +25,11 @@
 
 #include "unwind-internal.h"
 
-PROTECTED void
+void
 _Unwind_SetIP (struct _Unwind_Context *context, unsigned long new_value)
 {
-    unw_set_reg (&context->cursor, UNW_REG_IP, new_value);
+  unw_set_reg (&context->cursor, UNW_REG_IP, new_value);
 }
 
 void __libunwind_Unwind_SetIP (struct _Unwind_Context *, unsigned long)
-ALIAS (_Unwind_SetIP);
+     ALIAS (_Unwind_SetIP);
diff --git a/sgx_unwind/libunwind/src/unwind/libunwind.pc.in b/sgx_unwind/libunwind/src/unwind/libunwind.pc.in
new file mode 100644
index 0000000..1505c5d
--- /dev/null
+++ b/sgx_unwind/libunwind/src/unwind/libunwind.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libunwind
+Description: libunwind base library
+Version: @VERSION@
+Libs: -L${libdir} -lunwind
+Libs.private: @LIBLZMA@
+Cflags: -I${includedir}
diff --git a/sgx_unwind/libunwind/src/unwind/unwind-internal.h b/sgx_unwind/libunwind/src/unwind/unwind-internal.h
index 1c377cc..c68fc3c 100644
--- a/sgx_unwind/libunwind/src/unwind/unwind-internal.h
+++ b/sgx_unwind/libunwind/src/unwind/unwind-internal.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
 This file is part of libunwind.
 
@@ -35,23 +35,22 @@
 #include "libunwind_i.h"
 
 /* The version of the _Unwind_*() interface implemented by this code.  */
-#define _U_VERSION	1
+#define _U_VERSION      1
 
 typedef _Unwind_Reason_Code (*_Unwind_Personality_Fn)
-(int, _Unwind_Action, uint64_t, struct _Unwind_Exception *,
- struct _Unwind_Context *);
+        (int, _Unwind_Action, uint64_t, struct _Unwind_Exception *,
+         struct _Unwind_Context *);
 
-struct _Unwind_Context
-{
-    unw_cursor_t cursor;
-    int end_of_stack;	/* set to 1 if the end of stack was reached */
+struct _Unwind_Context {
+  unw_cursor_t cursor;
+  int end_of_stack;     /* set to 1 if the end of stack was reached */
 };
 
 /* This must be a macro because unw_getcontext() must be invoked from
    the callee, even if optimization (and hence inlining) is turned
    off.  The macro arguments MUST NOT have any side-effects. */
-#define _Unwind_InitContext(context, uc)				     \
-  ((context)->end_of_stack = 0,						     \
+#define _Unwind_InitContext(context, uc)                                     \
+  ((context)->end_of_stack = 0,                                              \
    ((unw_getcontext (uc) < 0 || unw_init_local (&(context)->cursor, uc) < 0) \
     ? -1 : 0))
 
@@ -59,83 +58,83 @@
 _Unwind_Phase2 (struct _Unwind_Exception *exception_object,
                 struct _Unwind_Context *context)
 {
-    _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
-    uint64_t exception_class = exception_object->exception_class;
-    void *stop_parameter = (void *) exception_object->private_2;
-    _Unwind_Personality_Fn personality;
-    _Unwind_Reason_Code reason;
-    _Unwind_Action actions;
-    unw_proc_info_t pi;
-    unw_word_t ip;
-    int ret;
+  _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) exception_object->private_1;
+  uint64_t exception_class = exception_object->exception_class;
+  void *stop_parameter = (void *) exception_object->private_2;
+  _Unwind_Personality_Fn personality;
+  _Unwind_Reason_Code reason;
+  _Unwind_Action actions;
+  unw_proc_info_t pi;
+  unw_word_t ip;
+  int ret;
 
-    actions = _UA_CLEANUP_PHASE;
-    if (stop)
-        actions |= _UA_FORCE_UNWIND;
+  actions = _UA_CLEANUP_PHASE;
+  if (stop)
+    actions |= _UA_FORCE_UNWIND;
 
-    while (1)
+  while (1)
     {
-        ret = unw_step (&context->cursor);
-        if (ret <= 0)
+      ret = unw_step (&context->cursor);
+      if (ret <= 0)
         {
-            if (ret == 0)
+          if (ret == 0)
             {
-                actions |= _UA_END_OF_STACK;
-                context->end_of_stack = 1;
+              actions |= _UA_END_OF_STACK;
+              context->end_of_stack = 1;
             }
-            else
-                return _URC_FATAL_PHASE2_ERROR;
-        }
-
-        if (stop)
-        {
-            reason = (*stop) (_U_VERSION, actions, exception_class,
-                              exception_object, context, stop_parameter);
-            if (reason != _URC_NO_REASON)
-                /* Stop function may return _URC_FATAL_PHASE2_ERROR if
-                   it's unable to handle end-of-stack condition or
-                   _URC_FATAL_PHASE2_ERROR if something is wrong.  Not
-                   that it matters: the resulting state is indeterminate
-                   anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
-                return _URC_FATAL_PHASE2_ERROR;
-        }
-
-        if (context->end_of_stack
-                || unw_get_proc_info (&context->cursor, &pi) < 0)
+          else
             return _URC_FATAL_PHASE2_ERROR;
+        }
 
-        personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
-        if (personality)
+      if (stop)
         {
-            if (!stop)
-            {
-                if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
-                    return _URC_FATAL_PHASE2_ERROR;
+          reason = (*stop) (_U_VERSION, actions, exception_class,
+                            exception_object, context, stop_parameter);
+          if (reason != _URC_NO_REASON)
+            /* Stop function may return _URC_FATAL_PHASE2_ERROR if
+               it's unable to handle end-of-stack condition or
+               _URC_FATAL_PHASE2_ERROR if something is wrong.  Not
+               that it matters: the resulting state is indeterminate
+               anyhow so we must return _URC_FATAL_PHASE2_ERROR... */
+            return _URC_FATAL_PHASE2_ERROR;
+        }
 
-                if ((unsigned long) stop_parameter == ip)
-                    actions |= _UA_HANDLER_FRAME;
+      if (context->end_of_stack
+          || unw_get_proc_info (&context->cursor, &pi) < 0)
+        return _URC_FATAL_PHASE2_ERROR;
+
+      personality = (_Unwind_Personality_Fn) (uintptr_t) pi.handler;
+      if (personality)
+        {
+          if (!stop)
+            {
+              if (unw_get_reg (&context->cursor, UNW_REG_IP, &ip) < 0)
+                return _URC_FATAL_PHASE2_ERROR;
+
+              if ((unsigned long) stop_parameter == ip)
+                actions |= _UA_HANDLER_FRAME;
             }
 
-            reason = (*personality) (_U_VERSION, actions, exception_class,
-                                     exception_object, context);
-            if (reason != _URC_CONTINUE_UNWIND)
+          reason = (*personality) (_U_VERSION, actions, exception_class,
+                                   exception_object, context);
+          if (reason != _URC_CONTINUE_UNWIND)
             {
-                if (reason == _URC_INSTALL_CONTEXT)
+              if (reason == _URC_INSTALL_CONTEXT)
                 {
-                    /* we may regain control via _Unwind_Resume() */
-                    unw_resume (&context->cursor);
-                    abort ();
+                  /* we may regain control via _Unwind_Resume() */
+                  unw_resume (&context->cursor);
+                  abort ();
                 }
-                else
-                    return _URC_FATAL_PHASE2_ERROR;
+              else
+                return _URC_FATAL_PHASE2_ERROR;
             }
-            if (actions & _UA_HANDLER_FRAME)
-                /* The personality routine for the handler-frame changed
-                   it's mind; that's a no-no... */
-                abort ();
+          if (actions & _UA_HANDLER_FRAME)
+            /* The personality routine for the handler-frame changed
+               it's mind; that's a no-no... */
+            abort ();
         }
     }
-    return _URC_FATAL_PHASE2_ERROR;	/* shouldn't be reached */
+  return _URC_FATAL_PHASE2_ERROR;       /* shouldn't be reached */
 }
 
 #endif /* unwind_internal_h */
diff --git a/sgx_unwind/libunwind/src/dwarf/Gstep.c b/sgx_unwind/libunwind/src/x86_64/Gapply_reg_state.c
similarity index 72%
rename from sgx_unwind/libunwind/src/dwarf/Gstep.c
rename to sgx_unwind/libunwind/src/x86_64/Gapply_reg_state.c
index 3dcc72b..82f056d 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gstep.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gapply_reg_state.c
@@ -1,6 +1,8 @@
 /* libunwind - a platform-independent unwind library
-   Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+   Copyright (c) 2002-2003 Hewlett-Packard Development Company, L.P.
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+
+   Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
 This file is part of libunwind.
 
@@ -23,20 +25,13 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include "dwarf.h"
-#include "libunwind_i.h"
+#include "unwind_i.h"
 
-HIDDEN int
-dwarf_step (struct dwarf_cursor *c)
+int
+unw_apply_reg_state (unw_cursor_t *cursor,
+		     void *reg_states_data)
 {
-    int ret;
+  struct cursor *c = (struct cursor *) cursor;
 
-    if ((ret = dwarf_find_save_locs (c)) >= 0)
-    {
-        c->pi_valid = 0;
-        ret = 1;
-    }
-
-    Debug (15, "returning %d\n", ret);
-    return ret;
+  return dwarf_apply_reg_state (&c->dwarf, (dwarf_reg_state_t *)reg_states_data);
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gcreate_addr_space.c b/sgx_unwind/libunwind/src/x86_64/Gcreate_addr_space.c
index b7ca8f6..9b2db98 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gcreate_addr_space.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gcreate_addr_space.c
@@ -1,8 +1,9 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2003 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
+   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
 
 This file is part of libunwind.
 
@@ -33,27 +34,28 @@
 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
 #endif
 
-PROTECTED unw_addr_space_t
+unw_addr_space_t
 unw_create_addr_space (unw_accessors_t *a, int byte_order)
 {
 #ifdef UNW_LOCAL_ONLY
-    return NULL;
+  return NULL;
 #else
-    unw_addr_space_t as;
+  unw_addr_space_t as;
 
-    /*
-     * x86_64 supports only little-endian.
-     */
-    if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
-        return NULL;
+  /*
+   * x86_64 supports only little-endian.
+   */
+  if (byte_order != 0 && byte_order != __LITTLE_ENDIAN)
+    return NULL;
 
-    as = malloc (sizeof (*as));
-    if (!as)
-        return NULL;
+  as = malloc (sizeof (*as));
+  if (!as)
+    return NULL;
 
-    memset (as, 0, sizeof (*as));
-    as->acc = *a;
+  memset (as, 0, sizeof (*as));
 
-    return as;
+  as->acc = *a;
+
+  return as;
 #endif
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gget_proc_info.c b/sgx_unwind/libunwind/src/x86_64/Gget_proc_info.c
index 5999187..50de1e4 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gget_proc_info.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gget_proc_info.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2002-2003 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -27,22 +27,22 @@
 
 #include "unwind_i.h"
 
-PROTECTED int
+int
 unw_get_proc_info (unw_cursor_t *cursor, unw_proc_info_t *pi)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    if (dwarf_make_proc_info (&c->dwarf) < 0)
+  if (dwarf_make_proc_info (&c->dwarf) < 0)
     {
-        /* On x86-64, some key routines such as _start() and _dl_start()
-        are missing DWARF unwind info.  We don't want to fail in that
+      /* On x86-64, some key routines such as _start() and _dl_start()
+         are missing DWARF unwind info.  We don't want to fail in that
          case, because those frames are uninteresting and just mark
          the end of the frame-chain anyhow.  */
-        memset (pi, 0, sizeof (*pi));
-        pi->start_ip = c->dwarf.ip;
-        pi->end_ip = c->dwarf.ip + 1;
-        return 0;
+      memset (pi, 0, sizeof (*pi));
+      pi->start_ip = c->dwarf.ip;
+      pi->end_ip = c->dwarf.ip + 1;
+      return 0;
     }
-    *pi = c->dwarf.pi;
-    return 0;
+  *pi = c->dwarf.pi;
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gget_save_loc.c b/sgx_unwind/libunwind/src/x86_64/Gget_save_loc.c
index fbd4f57..0057c62 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gget_save_loc.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gget_save_loc.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -27,61 +27,47 @@
 
 #include "unwind_i.h"
 
-PROTECTED int
+int
 unw_get_save_loc (unw_cursor_t *cursor, int reg, unw_save_loc_t *sloc)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    dwarf_loc_t loc;
+  struct cursor *c = (struct cursor *) cursor;
+  dwarf_loc_t loc;
 
-    loc = DWARF_NULL_LOC;		/* default to "not saved" */
+  loc = DWARF_NULL_LOC;         /* default to "not saved" */
 
-    switch (reg)
+  switch (reg)
     {
-    case UNW_X86_64_RBX:
-        loc = c->dwarf.loc[RBX];
-        break;
-    case UNW_X86_64_RSP:
-        loc = c->dwarf.loc[RSP];
-        break;
-    case UNW_X86_64_RBP:
-        loc = c->dwarf.loc[RBP];
-        break;
-    case UNW_X86_64_R12:
-        loc = c->dwarf.loc[R12];
-        break;
-    case UNW_X86_64_R13:
-        loc = c->dwarf.loc[R13];
-        break;
-    case UNW_X86_64_R14:
-        loc = c->dwarf.loc[R14];
-        break;
-    case UNW_X86_64_R15:
-        loc = c->dwarf.loc[R15];
-        break;
+    case UNW_X86_64_RBX: loc = c->dwarf.loc[RBX]; break;
+    case UNW_X86_64_RSP: loc = c->dwarf.loc[RSP]; break;
+    case UNW_X86_64_RBP: loc = c->dwarf.loc[RBP]; break;
+    case UNW_X86_64_R12: loc = c->dwarf.loc[R12]; break;
+    case UNW_X86_64_R13: loc = c->dwarf.loc[R13]; break;
+    case UNW_X86_64_R14: loc = c->dwarf.loc[R14]; break;
+    case UNW_X86_64_R15: loc = c->dwarf.loc[R15]; break;
 
     default:
-        break;
+      break;
     }
 
-    memset (sloc, 0, sizeof (*sloc));
+  memset (sloc, 0, sizeof (*sloc));
 
-    if (DWARF_IS_NULL_LOC (loc))
+  if (DWARF_IS_NULL_LOC (loc))
     {
-        sloc->type = UNW_SLT_NONE;
-        return 0;
+      sloc->type = UNW_SLT_NONE;
+      return 0;
     }
 
 #if !defined(UNW_LOCAL_ONLY)
-    if (DWARF_IS_REG_LOC (loc))
+  if (DWARF_IS_REG_LOC (loc))
     {
-        sloc->type = UNW_SLT_REG;
-        sloc->u.regnum = DWARF_GET_LOC (loc);
+      sloc->type = UNW_SLT_REG;
+      sloc->u.regnum = DWARF_GET_LOC (loc);
     }
-    else
+  else
 #endif
     {
-        sloc->type = UNW_SLT_MEMORY;
-        sloc->u.addr = DWARF_GET_LOC (loc);
+      sloc->type = UNW_SLT_MEMORY;
+      sloc->u.addr = DWARF_GET_LOC (loc);
     }
-    return 0;
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gglobal.c b/sgx_unwind/libunwind/src/x86_64/Gglobal.c
index 0e5ec72..225d602 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gglobal.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gglobal.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003, 2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -25,16 +25,17 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
+#include "config.h"
 #include "unwind_i.h"
 #include "dwarf_i.h"
 
-HIDDEN pthread_mutex_t x86_64_lock = PTHREAD_MUTEX_INITIALIZER;
-HIDDEN int tdep_needs_initialization = 1;
+HIDDEN define_lock (x86_64_lock);
+HIDDEN int tdep_init_done;
 
 /* See comments for svr4_dbx_register_map[] in gcc/config/i386/i386.c.  */
 
-HIDDEN uint8_t dwarf_to_unw_regnum_map[DWARF_NUM_PRESERVED_REGS] =
-{
+HIDDEN const uint8_t dwarf_to_unw_regnum_map[DWARF_NUM_PRESERVED_REGS] =
+  {
     UNW_X86_64_RAX,
     UNW_X86_64_RDX,
     UNW_X86_64_RCX,
@@ -70,34 +71,34 @@
     UNW_X86_64_XMM14,
     UNW_X86_64_XMM15
 #endif
-};
+  };
 
 HIDDEN void
 tdep_init (void)
 {
-    intrmask_t saved_mask;
+  intrmask_t saved_mask;
 
 #if (!HAVE_SGX)
-    sigfillset (&unwi_full_mask);
+  sigfillset (&unwi_full_mask);
 #endif
 
-    lock_acquire (&x86_64_lock, saved_mask);
-    {
-        if (!tdep_needs_initialization)
-            /* another thread else beat us to it... */
-            goto out;
+  lock_acquire (&x86_64_lock, saved_mask);
+  {
+    if (tdep_init_done)
+      /* another thread else beat us to it... */
+      goto out;
 
-        mi_init ();
+    mi_init ();
 
-        dwarf_init ();
+    dwarf_init ();
 
-        tdep_init_mem_validate ();
+    tdep_init_mem_validate ();
 
 #ifndef UNW_REMOTE_ONLY
-        x86_64_local_addr_space_init ();
+    x86_64_local_addr_space_init ();
 #endif
-        tdep_needs_initialization = 0;	/* signal that we're initialized... */
-    }
-out:
-    lock_release (&x86_64_lock, saved_mask);
+    tdep_init_done = 1; /* signal that we're initialized... */
+  }
+ out:
+  lock_release (&x86_64_lock, saved_mask);
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Ginit.c b/sgx_unwind/libunwind/src/x86_64/Ginit.c
index 47b14a8..cf65579 100644
--- a/sgx_unwind/libunwind/src/x86_64/Ginit.c
+++ b/sgx_unwind/libunwind/src/x86_64/Ginit.c
@@ -1,7 +1,7 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002 Hewlett-Packard Co
    Copyright (C) 2007 David Mosberger-Tang
-	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
+        Contributed by David Mosberger-Tang <dmosberger@gmail.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -30,27 +30,29 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
 
 #include "unwind_i.h"
 
 #ifdef UNW_REMOTE_ONLY
 
 /* unw_local_addr_space is a NULL pointer in this case.  */
-PROTECTED unw_addr_space_t unw_local_addr_space;
+unw_addr_space_t unw_local_addr_space;
 
 #else /* !UNW_REMOTE_ONLY */
 
 static struct unw_addr_space local_addr_space;
 
-PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
+unw_addr_space_t unw_local_addr_space = &local_addr_space;
 
 static void
 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
 {
-    /* it's a no-op */
+  /* it's a no-op */
 }
 
 static int
@@ -59,28 +61,112 @@
 {
 #ifndef UNW_LOCAL_ONLY
 # pragma weak _U_dyn_info_list_addr
-    if (!_U_dyn_info_list_addr)
-        return -UNW_ENOINFO;
+  if (!_U_dyn_info_list_addr)
+    return -UNW_ENOINFO;
 #endif
-    // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
-    *dyn_info_list_addr = _U_dyn_info_list_addr ();
-    return 0;
+  // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
+  *dyn_info_list_addr = _U_dyn_info_list_addr ();
+  return 0;
 }
 
 #define PAGE_SIZE 4096
-#define PAGE_START(a)	((a) & ~(PAGE_SIZE-1))
+#define PAGE_START(a)   ((a) & ~(PAGE_SIZE-1))
+
+static int mem_validate_pipe[2] = {-1, -1};
+
+static inline void
+open_pipe (void)
+{
+  #ifndef HAVE_SGX
+  /* ignore errors for closing invalid fd's */
+  close (mem_validate_pipe[0]);
+  close (mem_validate_pipe[1]);
+
+  pipe2 (mem_validate_pipe, O_CLOEXEC | O_NONBLOCK);
+  #endif
+}
+
+ALWAYS_INLINE
+static int
+write_validate (void *addr)
+{
+  #ifndef HAVE_SGX
+  int ret = -1;
+  ssize_t bytes = 0;
+
+  do
+    {
+      char buf;
+      bytes = read (mem_validate_pipe[0], &buf, 1);
+    }
+  while ( errno == EINTR );
+
+  int valid_read = (bytes > 0 || errno == EAGAIN || errno == EWOULDBLOCK);
+  if (!valid_read)
+    {
+      // re-open closed pipe
+      open_pipe ();
+    }
+
+  do
+    {
+      /* use syscall insteadof write() so that ASAN does not complain */
+      ret = syscall (SYS_write, mem_validate_pipe[1], addr, 1);
+    }
+  while ( errno == EINTR );
+
+  return ret;
+#else
+  /*
+   * This function's purpose:
+   * https://github.com/libunwind/libunwind/commit/836c91c43d7a996028aa7e8d1f53630a6b8e7cbe#diff-98fa8f303410f32876910fb84c203cd6
+   *
+   * The calls to mincore() or msync() are not checking for actual accessibility this could lead to SIGSEGV if the address from a mapped page with the
+   * PROT_NONE property occurs on the stack. Hence an attempt to write one byte from the checked address to a pipe will fail if the address is not readable.
+   *
+   *
+   * But we think this function isn't needed inside SGX Enclave.
+  */
+  return 0;
+#endif
+}
 
 static int (*mem_validate_func) (void *addr, size_t len);
 static int msync_validate (void *addr, size_t len)
 {
-    return msync (addr, len, MS_ASYNC);
+  // #ifdef HAVE_SGX
+  // extern int sgx_is_within_enclave(const void *addr, size_t size);
+  // if(!sgx_is_within_enclave(addr, len))
+  //   abort();
+  // #endif
+
+  if (msync (addr, len, MS_ASYNC) != 0)
+    {
+      return -1;
+    }
+
+  return write_validate (addr);
 }
 
 #ifdef HAVE_MINCORE
 static int mincore_validate (void *addr, size_t len)
 {
-    unsigned char mvec[2]; /* Unaligned access may cross page boundary */
-    return mincore (addr, len, mvec);
+  unsigned char mvec[2]; /* Unaligned access may cross page boundary */
+  size_t i;
+
+  /* mincore could fail with EAGAIN but we conservatively return -1
+     instead of looping. */
+  if (mincore (addr, len, mvec) != 0)
+    {
+      return -1;
+    }
+
+  for (i = 0; i < (len + PAGE_SIZE - 1) / PAGE_SIZE; i++)
+    {
+      if (!(mvec[i] & 1)) return -1;
+    }
+  
+  return write_validate (addr);
 }
 #endif
 
@@ -91,18 +177,25 @@
 HIDDEN void
 tdep_init_mem_validate (void)
 {
+  open_pipe ();
+
 #ifdef HAVE_MINCORE
-    unsigned char present = 1;
-    if (mincore (&present, 1, &present) == 0)
+  unsigned char present = 1;
+  unw_word_t addr = PAGE_START((unw_word_t)&present);
+  unsigned char mvec[1];
+  int ret;
+  while ((ret = mincore ((void*)addr, PAGE_SIZE, mvec)) == -1 &&
+         errno == EAGAIN) {}
+  if (ret == 0 && (mvec[0] & 1))
     {
-        Debug(1, "using mincore to validate memory\n");
-        mem_validate_func = mincore_validate;
+      Debug(1, "using mincore to validate memory\n");
+      mem_validate_func = mincore_validate;
     }
-    else
+  else
 #endif
     {
-        Debug(1, "using msync to validate memory\n");
-        mem_validate_func = msync_validate;
+      Debug(1, "using msync to validate memory\n");
+      mem_validate_func = msync_validate;
     }
 }
 
@@ -114,130 +207,130 @@
 static int
 validate_mem (unw_word_t addr)
 {
-    int i, victim;
-    size_t len;
+  int i, victim;
+  size_t len;
 
-    if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
-        len = PAGE_SIZE;
-    else
-        len = PAGE_SIZE * 2;
+  if (PAGE_START(addr + sizeof (unw_word_t) - 1) == PAGE_START(addr))
+    len = PAGE_SIZE;
+  else
+    len = PAGE_SIZE * 2;
 
-    addr = PAGE_START(addr);
+  addr = PAGE_START(addr);
 
-    if (addr == 0)
-        return -1;
+  if (addr == 0)
+    return -1;
 
-    for (i = 0; i < NLGA; i++)
+  for (i = 0; i < NLGA; i++)
     {
-        if (last_good_addr[i] && (addr == last_good_addr[i]))
-            return 0;
+      if (last_good_addr[i] && (addr == last_good_addr[i]))
+        return 0;
     }
 
-    if (mem_validate_func ((void *) addr, len) == -1)
-        return -1;
+  if (mem_validate_func ((void *) addr, len) == -1)
+    return -1;
 
-    victim = lga_victim;
-    for (i = 0; i < NLGA; i++)
-    {
-        if (!last_good_addr[victim])
-        {
-            last_good_addr[victim] = addr;
-            return 0;
-        }
-        victim = (victim + 1) % NLGA;
+  victim = lga_victim;
+  for (i = 0; i < NLGA; i++) {
+    if (!last_good_addr[victim]) {
+      last_good_addr[victim++] = addr;
+      return 0;
     }
-
-    /* All slots full. Evict the victim. */
-    last_good_addr[victim] = addr;
     victim = (victim + 1) % NLGA;
-    lga_victim = victim;
+  }
 
-    return 0;
+  /* All slots full. Evict the victim. */
+  last_good_addr[victim] = addr;
+  victim = (victim + 1) % NLGA;
+  lga_victim = victim;
+
+  return 0;
 }
 
 static int
 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
             void *arg)
 {
-    if (unlikely (write))
+  if (unlikely (write))
     {
-        Debug (16, "mem[%016lx] <- %lx\n", addr, *val);
-        *(unw_word_t *) addr = *val;
+      Debug (16, "mem[%016lx] <- %lx\n", addr, *val);
+      *(unw_word_t *) addr = *val;
     }
-    else
+  else
     {
-        /* validate address */
-        const struct cursor *c = (const struct cursor *)arg;
-        if (likely (c != 0) && unlikely (c->validate)
-                && unlikely (validate_mem (addr)))
-            return -1;
-        *val = *(unw_word_t *) addr;
-        Debug (16, "mem[%016lx] -> %lx\n", addr, *val);
+      /* validate address */
+      const struct cursor *c = (const struct cursor *)arg;
+      if (likely (c != NULL) && unlikely (c->validate)
+          && unlikely (validate_mem (addr))) {
+        Debug (16, "mem[%016lx] -> invalid\n", addr);
+        return -1;
+      }
+      *val = *(unw_word_t *) addr;
+      Debug (16, "mem[%016lx] -> %lx\n", addr, *val);
     }
-    return 0;
+  return 0;
 }
 
 static int
 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
             void *arg)
 {
-    unw_word_t *addr;
-    ucontext_t *uc = ((struct cursor *)arg)->uc;
+  unw_word_t *addr;
+  ucontext_t *uc = ((struct cursor *)arg)->uc;
 
-    if (unw_is_fpreg (reg))
-        goto badreg;
+  if (unw_is_fpreg (reg))
+    goto badreg;
 
-    if (!(addr = x86_64_r_uc_addr (uc, reg)))
-        goto badreg;
+  if (!(addr = x86_64_r_uc_addr (uc, reg)))
+    goto badreg;
 
-    if (write)
+  if (write)
     {
-        *(unw_word_t *) addr = *val;
-        Debug (12, "%s <- 0x%016lx\n", unw_regname (reg), *val);
+      *(unw_word_t *) addr = *val;
+      Debug (12, "%s <- 0x%016lx\n", unw_regname (reg), *val);
     }
-    else
+  else
     {
-        *val = *(unw_word_t *) addr;
-        Debug (12, "%s -> 0x%016lx\n", unw_regname (reg), *val);
+      *val = *(unw_word_t *) addr;
+      Debug (12, "%s -> 0x%016lx\n", unw_regname (reg), *val);
     }
-    return 0;
+  return 0;
 
-badreg:
-    Debug (1, "bad register number %u\n", reg);
-    return -UNW_EBADREG;
+ badreg:
+  Debug (1, "bad register number %u\n", reg);
+  return -UNW_EBADREG;
 }
 
 static int
 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
               int write, void *arg)
 {
-    ucontext_t *uc = ((struct cursor *)arg)->uc;
-    unw_fpreg_t *addr;
+  ucontext_t *uc = ((struct cursor *)arg)->uc;
+  unw_fpreg_t *addr;
 
-    if (!unw_is_fpreg (reg))
-        goto badreg;
+  if (!unw_is_fpreg (reg))
+    goto badreg;
 
-    if (!(addr = x86_64_r_uc_addr (uc, reg)))
-        goto badreg;
+  if (!(addr = x86_64_r_uc_addr (uc, reg)))
+    goto badreg;
 
-    if (write)
+  if (write)
     {
-        Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
-               ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
-        *(unw_fpreg_t *) addr = *val;
+      Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
+             ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
+      *(unw_fpreg_t *) addr = *val;
     }
-    else
+  else
     {
-        *val = *(unw_fpreg_t *) addr;
-        Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
-               ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
+      *val = *(unw_fpreg_t *) addr;
+      Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
+             ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
     }
-    return 0;
+  return 0;
 
-badreg:
-    Debug (1, "bad register number %u\n", reg);
-    /* attempt to access a non-preserved register */
-    return -UNW_EBADREG;
+ badreg:
+  Debug (1, "bad register number %u\n", reg);
+  /* attempt to access a non-preserved register */
+  return -UNW_EBADREG;
 }
 
 static int
@@ -245,30 +338,30 @@
                       char *buf, size_t buf_len, unw_word_t *offp,
                       void *arg)
 {
-#if HAVE_SGX
-    return -1;
+#ifdef HAVE_SGX
+  return -1;
 #else
-    return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
+  return _Uelf64_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
 #endif
 }
 
 HIDDEN void
 x86_64_local_addr_space_init (void)
 {
-    memset (&local_addr_space, 0, sizeof (local_addr_space));
-    local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
-    local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
-    local_addr_space.acc.put_unwind_info = put_unwind_info;
-    local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
-    local_addr_space.acc.access_mem = access_mem;
-    local_addr_space.acc.access_reg = access_reg;
-    local_addr_space.acc.access_fpreg = access_fpreg;
-    local_addr_space.acc.resume = x86_64_local_resume;
-    local_addr_space.acc.get_proc_name = get_static_proc_name;
-    unw_flush_cache (&local_addr_space, 0, 0);
+  memset (&local_addr_space, 0, sizeof (local_addr_space));
+  local_addr_space.caching_policy = UNWI_DEFAULT_CACHING_POLICY;
+  local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
+  local_addr_space.acc.put_unwind_info = put_unwind_info;
+  local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
+  local_addr_space.acc.access_mem = access_mem;
+  local_addr_space.acc.access_reg = access_reg;
+  local_addr_space.acc.access_fpreg = access_fpreg;
+  local_addr_space.acc.resume = x86_64_local_resume;
+  local_addr_space.acc.get_proc_name = get_static_proc_name;
+  unw_flush_cache (&local_addr_space, 0, 0);
 
-    memset (last_good_addr, 0, sizeof (unw_word_t) * NLGA);
-    lga_victim = 0;
+  memset (last_good_addr, 0, sizeof (unw_word_t) * NLGA);
+  lga_victim = 0;
 }
 
 #endif /* !UNW_REMOTE_ONLY */
diff --git a/sgx_unwind/libunwind/src/x86_64/Ginit_local.c b/sgx_unwind/libunwind/src/x86_64/Ginit_local.c
index b608db8..5eaead0 100644
--- a/sgx_unwind/libunwind/src/x86_64/Ginit_local.c
+++ b/sgx_unwind/libunwind/src/x86_64/Ginit_local.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2002-2003 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -30,29 +30,52 @@
 
 #ifdef UNW_REMOTE_ONLY
 
-PROTECTED int
+int
 unw_init_local (unw_cursor_t *cursor, ucontext_t *uc)
 {
-    return -UNW_EINVAL;
+  return -UNW_EINVAL;
 }
 
 #else /* !UNW_REMOTE_ONLY */
 
-PROTECTED int
+static int
+unw_init_local_common (unw_cursor_t *cursor, ucontext_t *uc, unsigned use_prev_instr)
+{
+  struct cursor *c = (struct cursor *) cursor;
+
+  if (unlikely (!tdep_init_done))
+    tdep_init ();
+
+  Debug (1, "(cursor=%p)\n", c);
+
+  c->dwarf.as = unw_local_addr_space;
+  c->dwarf.as_arg = c;
+  c->uc = uc;
+  c->validate = 0;
+  return common_init (c, use_prev_instr);
+}
+
+int
 unw_init_local (unw_cursor_t *cursor, ucontext_t *uc)
 {
-    struct cursor *c = (struct cursor *) cursor;
+  return unw_init_local_common(cursor, uc, 1);
+}
 
-    if (unlikely (tdep_needs_initialization))
-        tdep_init ();
-
-    Debug (1, "(cursor=%p)\n", c);
-
-    c->dwarf.as = unw_local_addr_space;
-    c->dwarf.as_arg = c;
-    c->uc = uc;
-    c->validate = 0;
-    return common_init (c, 1);
+int
+unw_init_local2 (unw_cursor_t *cursor, ucontext_t *uc, int flag)
+{
+  if (!flag)
+    {
+      return unw_init_local_common(cursor, uc, 1);
+    }
+  else if (flag == UNW_INIT_SIGNAL_FRAME)
+    {
+      return unw_init_local_common(cursor, uc, 0);
+    }
+  else
+    {
+      return -UNW_EINVAL;
+    }
 }
 
 #endif /* !UNW_REMOTE_ONLY */
diff --git a/sgx_unwind/libunwind/src/x86_64/Ginit_remote.c b/sgx_unwind/libunwind/src/x86_64/Ginit_remote.c
index 27cf717..efd61d6 100644
--- a/sgx_unwind/libunwind/src/x86_64/Ginit_remote.c
+++ b/sgx_unwind/libunwind/src/x86_64/Ginit_remote.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -28,30 +28,30 @@
 #include "init.h"
 #include "unwind_i.h"
 
-PROTECTED int
+int
 unw_init_remote (unw_cursor_t *cursor, unw_addr_space_t as, void *as_arg)
 {
 #ifdef UNW_LOCAL_ONLY
-    return -UNW_EINVAL;
+  return -UNW_EINVAL;
 #else /* !UNW_LOCAL_ONLY */
-    struct cursor *c = (struct cursor *) cursor;
+  struct cursor *c = (struct cursor *) cursor;
 
-    if (tdep_needs_initialization)
-        tdep_init ();
+  if (!tdep_init_done)
+    tdep_init ();
 
-    Debug (1, "(cursor=%p)\n", c);
+  Debug (1, "(cursor=%p)\n", c);
 
-    c->dwarf.as = as;
-    if (as == unw_local_addr_space)
+  c->dwarf.as = as;
+  if (as == unw_local_addr_space)
     {
-        c->dwarf.as_arg = c;
-        c->uc = as_arg;
+      c->dwarf.as_arg = c;
+      c->uc = as_arg;
     }
-    else
+  else
     {
-        c->dwarf.as_arg = as_arg;
-        c->uc = 0;
+      c->dwarf.as_arg = as_arg;
+      c->uc = NULL;
     }
-    return common_init (c, 0);
+  return common_init (c, 0);
 #endif /* !UNW_LOCAL_ONLY */
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gos-freebsd.c b/sgx_unwind/libunwind/src/x86_64/Gos-freebsd.c
index d6fe1c5..883025c 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gos-freebsd.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gos-freebsd.c
@@ -33,121 +33,122 @@
 #include "unwind_i.h"
 #include "ucontext_i.h"
 
-PROTECTED int
+int
 unw_is_signal_frame (unw_cursor_t *cursor)
 {
-    /* XXXKIB */
-    struct cursor *c = (struct cursor *) cursor;
-    unw_word_t w0, w1, w2, b0, ip;
-    unw_addr_space_t as;
-    unw_accessors_t *a;
-    void *arg;
-    int ret;
+  /* XXXKIB */
+  struct cursor *c = (struct cursor *) cursor;
+  unw_word_t w0, w1, w2, b0, ip;
+  unw_addr_space_t as;
+  unw_accessors_t *a;
+  void *arg;
+  int ret;
 
-    as = c->dwarf.as;
-    a = unw_get_accessors (as);
-    arg = c->dwarf.as_arg;
+  as = c->dwarf.as;
+  a = unw_get_accessors_int (as);
+  arg = c->dwarf.as_arg;
 
-    /* Check if RIP points at sigreturn sequence.
-    48 8d 7c 24 10		lea	SIGF_UC(%rsp),%rdi
-    6a 00			pushq	$0
-    48 c7 c0 a1 01 00 00	movq	$SYS_sigreturn,%rax
-    0f 05			syscall
-    f4		0:	hlt
-    eb fd			jmp	0b
-    */
+  /* Check if RIP points at sigreturn sequence.
+48 8d 7c 24 10          lea     SIGF_UC(%rsp),%rdi
+6a 00                   pushq   $0
+48 c7 c0 a1 01 00 00    movq    $SYS_sigreturn,%rax
+0f 05                   syscall
+f4              0:      hlt
+eb fd                   jmp     0b
+  */
 
-    ip = c->dwarf.ip;
-    c->sigcontext_format = X86_64_SCF_NONE;
-    if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0
-            || (ret = (*a->access_mem) (as, ip + 8, &w1, 0, arg)) < 0
-            || (ret = (*a->access_mem) (as, ip + 16, &w2, 0, arg)) < 0)
-        return 0;
-    w2 &= 0xffffff;
-    if (w0 == 0x48006a10247c8d48 &&
-            w1 == 0x050f000001a1c0c7 &&
-            w2 == 0x0000000000fdebf4)
-    {
-        c->sigcontext_format = X86_64_SCF_FREEBSD_SIGFRAME;
-        return (c->sigcontext_format);
-    }
-    /* Check if RIP points at standard syscall sequence.
-    49 89 ca	mov    %rcx,%r10
-    0f 05		syscall
-    */
-    if ((ret = (*a->access_mem) (as, ip - 5, &b0, 0, arg)) < 0)
-        return (0);
-    Debug (12, "b0 0x%lx\n", b0);
-    if ((b0 & 0xffffffffffffff) == 0x050fca89490000 ||
-            (b0 & 0xffffffffff) == 0x050fca8949)
-    {
-        c->sigcontext_format = X86_64_SCF_FREEBSD_SYSCALL;
-        return (c->sigcontext_format);
-    }
-    return (X86_64_SCF_NONE);
+  ip = c->dwarf.ip;
+  c->sigcontext_format = X86_64_SCF_NONE;
+  if ((ret = (*a->access_mem) (as, ip, &w0, 0, arg)) < 0
+      || (ret = (*a->access_mem) (as, ip + 8, &w1, 0, arg)) < 0
+      || (ret = (*a->access_mem) (as, ip + 16, &w2, 0, arg)) < 0)
+    return 0;
+  w2 &= 0xffffff;
+  if (w0 == 0x48006a10247c8d48 &&
+      w1 == 0x050f000001a1c0c7 &&
+      w2 == 0x0000000000fdebf4)
+   {
+     c->sigcontext_format = X86_64_SCF_FREEBSD_SIGFRAME;
+     return (c->sigcontext_format);
+   }
+  /* Check if RIP points at standard syscall sequence.
+49 89 ca        mov    %rcx,%r10
+0f 05           syscall
+  */
+  if ((ret = (*a->access_mem) (as, ip - 5, &b0, 0, arg)) < 0)
+    return (0);
+  Debug (12, "b0 0x%lx\n", b0);
+  if ((b0 & 0xffffffffffffff) == 0x050fca89490000 ||
+      (b0 & 0xffffffffff) == 0x050fca8949)
+   {
+    c->sigcontext_format = X86_64_SCF_FREEBSD_SYSCALL;
+    return (c->sigcontext_format);
+   }
+  return (X86_64_SCF_NONE);
 }
 
-PROTECTED int
-unw_handle_signal_frame (unw_cursor_t *cursor)
+HIDDEN int
+x86_64_handle_signal_frame (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    unw_word_t ucontext;
-    int ret;
+  struct cursor *c = (struct cursor *) cursor;
+  unw_word_t ucontext;
+  int ret;
 
-    if (c->sigcontext_format == X86_64_SCF_FREEBSD_SIGFRAME)
-    {
-        ucontext = c->dwarf.cfa + offsetof(struct sigframe, sf_uc);
-        c->sigcontext_addr = c->dwarf.cfa;
-        Debug(1, "signal frame, skip over trampoline\n");
+  if (c->sigcontext_format == X86_64_SCF_FREEBSD_SIGFRAME)
+   {
+    ucontext = c->dwarf.cfa + offsetof(struct sigframe, sf_uc);
+    c->sigcontext_addr = c->dwarf.cfa;
+    Debug(1, "signal frame, skip over trampoline\n");
 
-        struct dwarf_loc rsp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
-        ret = dwarf_get (&c->dwarf, rsp_loc, &c->dwarf.cfa);
-        if (ret < 0)
-        {
-            Debug (2, "returning %d\n", ret);
-            return ret;
-        }
+    struct dwarf_loc rsp_loc = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
+    ret = dwarf_get (&c->dwarf, rsp_loc, &c->dwarf.cfa);
+    if (ret < 0)
+     {
+       Debug (2, "returning %d\n", ret);
+       return ret;
+     }
 
-        c->dwarf.loc[RAX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RAX, 0);
-        c->dwarf.loc[RDX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDX, 0);
-        c->dwarf.loc[RCX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RCX, 0);
-        c->dwarf.loc[RBX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBX, 0);
-        c->dwarf.loc[RSI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSI, 0);
-        c->dwarf.loc[RDI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDI, 0);
-        c->dwarf.loc[RBP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBP, 0);
-        c->dwarf.loc[RSP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
-        c->dwarf.loc[ R8] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
-        c->dwarf.loc[ R9] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
-        c->dwarf.loc[R10] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
-        c->dwarf.loc[R11] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
-        c->dwarf.loc[R12] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
-        c->dwarf.loc[R13] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
-        c->dwarf.loc[R14] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
-        c->dwarf.loc[R15] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
-        c->dwarf.loc[RIP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RIP, 0);
+    c->dwarf.loc[RAX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RAX, 0);
+    c->dwarf.loc[RDX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDX, 0);
+    c->dwarf.loc[RCX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RCX, 0);
+    c->dwarf.loc[RBX] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBX, 0);
+    c->dwarf.loc[RSI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSI, 0);
+    c->dwarf.loc[RDI] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RDI, 0);
+    c->dwarf.loc[RBP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RBP, 0);
+    c->dwarf.loc[RSP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RSP, 0);
+    c->dwarf.loc[ R8] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R8, 0);
+    c->dwarf.loc[ R9] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R9, 0);
+    c->dwarf.loc[R10] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R10, 0);
+    c->dwarf.loc[R11] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R11, 0);
+    c->dwarf.loc[R12] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R12, 0);
+    c->dwarf.loc[R13] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R13, 0);
+    c->dwarf.loc[R14] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R14, 0);
+    c->dwarf.loc[R15] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_R15, 0);
+    c->dwarf.loc[RIP] = DWARF_LOC (ucontext + UC_MCONTEXT_GREGS_RIP, 0);
 
-        return 0;
-    }
-    else if (c->sigcontext_format == X86_64_SCF_FREEBSD_SYSCALL)
-    {
-        c->dwarf.loc[RCX] = c->dwarf.loc[R10];
-        /*  rsp_loc = DWARF_LOC(c->dwarf.cfa - 8, 0);	*/
-        /*	rbp_loc = c->dwarf.loc[RBP];			*/
-        c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
-        ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
-        Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
-               (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
-               (unsigned long long) c->dwarf.ip);
-        if (ret < 0)
-        {
-            Debug (2, "returning %d\n", ret);
-            return ret;
-        }
-        c->dwarf.cfa += 8;
-        return 1;
-    }
-    else
-        return -UNW_EBADFRAME;
+    return 0;
+   }
+  else if (c->sigcontext_format == X86_64_SCF_FREEBSD_SYSCALL)
+   {
+    c->dwarf.loc[RCX] = c->dwarf.loc[R10];
+    /*  rsp_loc = DWARF_LOC(c->dwarf.cfa - 8, 0);       */
+    /*  rbp_loc = c->dwarf.loc[RBP];                    */
+    c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
+    ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
+    Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
+           (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
+           (unsigned long long) c->dwarf.ip);
+    if (ret < 0)
+     {
+       Debug (2, "returning %d\n", ret);
+       return ret;
+     }
+    c->dwarf.cfa += 8;
+    c->dwarf.use_prev_instr = 1;
+    return 1;
+   }
+  else
+    return -UNW_EBADFRAME;
 
 }
 
@@ -155,79 +156,63 @@
 HIDDEN void *
 x86_64_r_uc_addr (ucontext_t *uc, int reg)
 {
-    /* NOTE: common_init() in init.h inlines these for fast path access. */
-    void *addr;
+  /* NOTE: common_init() in init.h inlines these for fast path access. */
+  void *addr;
 
-    switch (reg)
+  switch (reg)
     {
-    case UNW_X86_64_R8:
-        addr = &uc->uc_mcontext.mc_r8;
-        break;
-    case UNW_X86_64_R9:
-        addr = &uc->uc_mcontext.mc_r9;
-        break;
-    case UNW_X86_64_R10:
-        addr = &uc->uc_mcontext.mc_r10;
-        break;
-    case UNW_X86_64_R11:
-        addr = &uc->uc_mcontext.mc_r11;
-        break;
-    case UNW_X86_64_R12:
-        addr = &uc->uc_mcontext.mc_r12;
-        break;
-    case UNW_X86_64_R13:
-        addr = &uc->uc_mcontext.mc_r13;
-        break;
-    case UNW_X86_64_R14:
-        addr = &uc->uc_mcontext.mc_r14;
-        break;
-    case UNW_X86_64_R15:
-        addr = &uc->uc_mcontext.mc_r15;
-        break;
-    case UNW_X86_64_RDI:
-        addr = &uc->uc_mcontext.mc_rdi;
-        break;
-    case UNW_X86_64_RSI:
-        addr = &uc->uc_mcontext.mc_rsi;
-        break;
-    case UNW_X86_64_RBP:
-        addr = &uc->uc_mcontext.mc_rbp;
-        break;
-    case UNW_X86_64_RBX:
-        addr = &uc->uc_mcontext.mc_rbx;
-        break;
-    case UNW_X86_64_RDX:
-        addr = &uc->uc_mcontext.mc_rdx;
-        break;
-    case UNW_X86_64_RAX:
-        addr = &uc->uc_mcontext.mc_rax;
-        break;
-    case UNW_X86_64_RCX:
-        addr = &uc->uc_mcontext.mc_rcx;
-        break;
-    case UNW_X86_64_RSP:
-        addr = &uc->uc_mcontext.mc_rsp;
-        break;
-    case UNW_X86_64_RIP:
-        addr = &uc->uc_mcontext.mc_rip;
-        break;
+    case UNW_X86_64_R8: addr = &uc->uc_mcontext.mc_r8; break;
+    case UNW_X86_64_R9: addr = &uc->uc_mcontext.mc_r9; break;
+    case UNW_X86_64_R10: addr = &uc->uc_mcontext.mc_r10; break;
+    case UNW_X86_64_R11: addr = &uc->uc_mcontext.mc_r11; break;
+    case UNW_X86_64_R12: addr = &uc->uc_mcontext.mc_r12; break;
+    case UNW_X86_64_R13: addr = &uc->uc_mcontext.mc_r13; break;
+    case UNW_X86_64_R14: addr = &uc->uc_mcontext.mc_r14; break;
+    case UNW_X86_64_R15: addr = &uc->uc_mcontext.mc_r15; break;
+    case UNW_X86_64_RDI: addr = &uc->uc_mcontext.mc_rdi; break;
+    case UNW_X86_64_RSI: addr = &uc->uc_mcontext.mc_rsi; break;
+    case UNW_X86_64_RBP: addr = &uc->uc_mcontext.mc_rbp; break;
+    case UNW_X86_64_RBX: addr = &uc->uc_mcontext.mc_rbx; break;
+    case UNW_X86_64_RDX: addr = &uc->uc_mcontext.mc_rdx; break;
+    case UNW_X86_64_RAX: addr = &uc->uc_mcontext.mc_rax; break;
+    case UNW_X86_64_RCX: addr = &uc->uc_mcontext.mc_rcx; break;
+    case UNW_X86_64_RSP: addr = &uc->uc_mcontext.mc_rsp; break;
+    case UNW_X86_64_RIP: addr = &uc->uc_mcontext.mc_rip; break;
 
     default:
-        addr = NULL;
+      addr = NULL;
     }
-    return addr;
+  return addr;
 }
 
 HIDDEN NORETURN void
 x86_64_sigreturn (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    ucontext_t *uc = (ucontext_t *)(c->sigcontext_addr +
-                                    offsetof(struct sigframe, sf_uc));
+  struct cursor *c = (struct cursor *) cursor;
+  ucontext_t *uc = (ucontext_t *)(c->sigcontext_addr +
+    offsetof(struct sigframe, sf_uc));
 
-    Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
-           (unsigned long long) c->dwarf.ip, uc);
-    sigreturn(uc);
-    abort();
+  uc->uc_mcontext.mc_r8 = c->uc->uc_mcontext.mc_r8;
+  uc->uc_mcontext.mc_r9 = c->uc->uc_mcontext.mc_r9;
+  uc->uc_mcontext.mc_r10 = c->uc->uc_mcontext.mc_r10;
+  uc->uc_mcontext.mc_r11 = c->uc->uc_mcontext.mc_r11;
+  uc->uc_mcontext.mc_r12 = c->uc->uc_mcontext.mc_r12;
+  uc->uc_mcontext.mc_r13 = c->uc->uc_mcontext.mc_r13;
+  uc->uc_mcontext.mc_r14 = c->uc->uc_mcontext.mc_r14;
+  uc->uc_mcontext.mc_r15 = c->uc->uc_mcontext.mc_r15;
+  uc->uc_mcontext.mc_rdi = c->uc->uc_mcontext.mc_rdi;
+  uc->uc_mcontext.mc_rsi = c->uc->uc_mcontext.mc_rsi;
+  uc->uc_mcontext.mc_rbp = c->uc->uc_mcontext.mc_rbp;
+  uc->uc_mcontext.mc_rbx = c->uc->uc_mcontext.mc_rbx;
+  uc->uc_mcontext.mc_rdx = c->uc->uc_mcontext.mc_rdx;
+  uc->uc_mcontext.mc_rax = c->uc->uc_mcontext.mc_rax;
+  uc->uc_mcontext.mc_rcx = c->uc->uc_mcontext.mc_rcx;
+  uc->uc_mcontext.mc_rsp = c->uc->uc_mcontext.mc_rsp;
+  uc->uc_mcontext.mc_rip = c->uc->uc_mcontext.mc_rip;
+
+  Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
+             (unsigned long long) c->dwarf.ip, uc);
+  sigreturn(uc);
+  abort();
 }
 #endif
diff --git a/sgx_unwind/libunwind/src/x86_64/Gos-linux.c b/sgx_unwind/libunwind/src/x86_64/Gos-linux.c
index 8d71fd6..bd14234 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gos-linux.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gos-linux.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2003 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -33,156 +33,124 @@
 HIDDEN void
 tdep_fetch_frame (struct dwarf_cursor *dw, unw_word_t ip, int need_unwind_info)
 {
-    struct cursor *c = (struct cursor *) dw;
-    assert(! need_unwind_info || dw->pi_valid);
-    assert(! need_unwind_info || dw->pi.unwind_info);
-    if (dw->pi_valid
-            && dw->pi.unwind_info
-            && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
-        c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
-    else
-        c->sigcontext_format = X86_64_SCF_NONE;
+  struct cursor *c = (struct cursor *) dw;
+  assert(! need_unwind_info || dw->pi_valid);
+  assert(! need_unwind_info || dw->pi.unwind_info);
+  if (dw->pi_valid
+      && dw->pi.unwind_info
+      && ((struct dwarf_cie_info *) dw->pi.unwind_info)->signal_frame)
+    c->sigcontext_format = X86_64_SCF_LINUX_RT_SIGFRAME;
+  else
+    c->sigcontext_format = X86_64_SCF_NONE;
 
-    Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
-          dw->ip, dw->cfa, c->sigcontext_format);
+  Debug(5, "fetch frame ip=0x%lx cfa=0x%lx format=%d\n",
+        dw->ip, dw->cfa, c->sigcontext_format);
+}
+
+HIDDEN int
+tdep_cache_frame (struct dwarf_cursor *dw)
+{
+  struct cursor *c = (struct cursor *) dw;
+
+  Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
+        dw->ip, dw->cfa, c->sigcontext_format);
+  return c->sigcontext_format;
 }
 
 HIDDEN void
-tdep_cache_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
+tdep_reuse_frame (struct dwarf_cursor *dw, int frame)
 {
-    struct cursor *c = (struct cursor *) dw;
-    rs->signal_frame = c->sigcontext_format;
+  struct cursor *c = (struct cursor *) dw;
+  c->sigcontext_format = frame;
+  if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
+  {
+    c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
+    /* Offset from cfa to ucontext_t in signal frame.  */
+    c->frame_info.cfa_reg_offset = 0;
+    c->sigcontext_addr = dw->cfa;
+  }
 
-    Debug(5, "cache frame ip=0x%lx cfa=0x%lx format=%d\n",
-          dw->ip, dw->cfa, c->sigcontext_format);
+  Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
+        dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
+        (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
+         ? c->frame_info.cfa_reg_offset : 0));
 }
 
-HIDDEN void
-tdep_reuse_frame (struct dwarf_cursor *dw, struct dwarf_reg_state *rs)
-{
-    struct cursor *c = (struct cursor *) dw;
-    c->sigcontext_format = rs->signal_frame;
-    if (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME)
-    {
-        c->frame_info.frame_type = UNW_X86_64_FRAME_SIGRETURN;
-        /* Offset from cfa to ucontext_t in signal frame.  */
-        c->frame_info.cfa_reg_offset = 0;
-        c->sigcontext_addr = dw->cfa;
-    }
-    else
-        c->sigcontext_addr = 0;
-
-    Debug(5, "reuse frame ip=0x%lx cfa=0x%lx format=%d addr=0x%lx offset=%+d\n",
-          dw->ip, dw->cfa, c->sigcontext_format, c->sigcontext_addr,
-          (c->sigcontext_format == X86_64_SCF_LINUX_RT_SIGFRAME
-           ? c->frame_info.cfa_reg_offset : 0));
-}
-
-PROTECTED int
+int
 unw_is_signal_frame (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    return c->sigcontext_format != X86_64_SCF_NONE;
+  struct cursor *c = (struct cursor *) cursor;
+  return c->sigcontext_format != X86_64_SCF_NONE;
 }
 
-PROTECTED int
-unw_handle_signal_frame (unw_cursor_t *cursor)
+HIDDEN int
+x86_64_handle_signal_frame (unw_cursor_t *cursor)
 {
 #if UNW_DEBUG /* To silence compiler warnings */
-    /* Should not get here because we now use kernel-provided dwarf
-       information for the signal trampoline and dwarf_step() works.
-       Hence unw_step() should never call this function. Maybe
-       restore old non-dwarf signal handling here, but then the
-       gating on unw_is_signal_frame() needs to be removed. */
-    struct cursor *c = (struct cursor *) cursor;
-    Debug(1, "old format signal frame? format=%d addr=0x%lx cfa=0x%lx\n",
-          c->sigcontext_format, c->sigcontext_addr, c->dwarf.cfa);
+  /* Should not get here because we now use kernel-provided dwarf
+     information for the signal trampoline and dwarf_step() works.
+     Hence unw_step() should never call this function. Maybe
+     restore old non-dwarf signal handling here, but then the
+     gating on unw_is_signal_frame() needs to be removed. */
+  struct cursor *c = (struct cursor *) cursor;
+  Debug(1, "old format signal frame? format=%d addr=0x%lx cfa=0x%lx\n",
+        c->sigcontext_format, c->sigcontext_addr, c->dwarf.cfa);
 #endif
-    return -UNW_EBADFRAME;
+  return -UNW_EBADFRAME;
 }
 
 #ifndef UNW_REMOTE_ONLY
 HIDDEN void *
 x86_64_r_uc_addr (ucontext_t *uc, int reg)
 {
-    /* NOTE: common_init() in init.h inlines these for fast path access. */
-    void *addr;
+  /* NOTE: common_init() in init.h inlines these for fast path access. */
+  void *addr;
 
-    switch (reg)
+  switch (reg)
     {
-    case UNW_X86_64_R8:
-        addr = &uc->uc_mcontext.gregs[REG_R8];
-        break;
-    case UNW_X86_64_R9:
-        addr = &uc->uc_mcontext.gregs[REG_R9];
-        break;
-    case UNW_X86_64_R10:
-        addr = &uc->uc_mcontext.gregs[REG_R10];
-        break;
-    case UNW_X86_64_R11:
-        addr = &uc->uc_mcontext.gregs[REG_R11];
-        break;
-    case UNW_X86_64_R12:
-        addr = &uc->uc_mcontext.gregs[REG_R12];
-        break;
-    case UNW_X86_64_R13:
-        addr = &uc->uc_mcontext.gregs[REG_R13];
-        break;
-    case UNW_X86_64_R14:
-        addr = &uc->uc_mcontext.gregs[REG_R14];
-        break;
-    case UNW_X86_64_R15:
-        addr = &uc->uc_mcontext.gregs[REG_R15];
-        break;
-    case UNW_X86_64_RDI:
-        addr = &uc->uc_mcontext.gregs[REG_RDI];
-        break;
-    case UNW_X86_64_RSI:
-        addr = &uc->uc_mcontext.gregs[REG_RSI];
-        break;
-    case UNW_X86_64_RBP:
-        addr = &uc->uc_mcontext.gregs[REG_RBP];
-        break;
-    case UNW_X86_64_RBX:
-        addr = &uc->uc_mcontext.gregs[REG_RBX];
-        break;
-    case UNW_X86_64_RDX:
-        addr = &uc->uc_mcontext.gregs[REG_RDX];
-        break;
-    case UNW_X86_64_RAX:
-        addr = &uc->uc_mcontext.gregs[REG_RAX];
-        break;
-    case UNW_X86_64_RCX:
-        addr = &uc->uc_mcontext.gregs[REG_RCX];
-        break;
-    case UNW_X86_64_RSP:
-        addr = &uc->uc_mcontext.gregs[REG_RSP];
-        break;
-    case UNW_X86_64_RIP:
-        addr = &uc->uc_mcontext.gregs[REG_RIP];
-        break;
+    case UNW_X86_64_R8: addr = &uc->uc_mcontext.gregs[REG_R8]; break;
+    case UNW_X86_64_R9: addr = &uc->uc_mcontext.gregs[REG_R9]; break;
+    case UNW_X86_64_R10: addr = &uc->uc_mcontext.gregs[REG_R10]; break;
+    case UNW_X86_64_R11: addr = &uc->uc_mcontext.gregs[REG_R11]; break;
+    case UNW_X86_64_R12: addr = &uc->uc_mcontext.gregs[REG_R12]; break;
+    case UNW_X86_64_R13: addr = &uc->uc_mcontext.gregs[REG_R13]; break;
+    case UNW_X86_64_R14: addr = &uc->uc_mcontext.gregs[REG_R14]; break;
+    case UNW_X86_64_R15: addr = &uc->uc_mcontext.gregs[REG_R15]; break;
+    case UNW_X86_64_RDI: addr = &uc->uc_mcontext.gregs[REG_RDI]; break;
+    case UNW_X86_64_RSI: addr = &uc->uc_mcontext.gregs[REG_RSI]; break;
+    case UNW_X86_64_RBP: addr = &uc->uc_mcontext.gregs[REG_RBP]; break;
+    case UNW_X86_64_RBX: addr = &uc->uc_mcontext.gregs[REG_RBX]; break;
+    case UNW_X86_64_RDX: addr = &uc->uc_mcontext.gregs[REG_RDX]; break;
+    case UNW_X86_64_RAX: addr = &uc->uc_mcontext.gregs[REG_RAX]; break;
+    case UNW_X86_64_RCX: addr = &uc->uc_mcontext.gregs[REG_RCX]; break;
+    case UNW_X86_64_RSP: addr = &uc->uc_mcontext.gregs[REG_RSP]; break;
+    case UNW_X86_64_RIP: addr = &uc->uc_mcontext.gregs[REG_RIP]; break;
 
     default:
-        addr = NULL;
+      addr = NULL;
     }
-    return addr;
+  return addr;
 }
 
 /* sigreturn() is a no-op on x86_64 glibc.  */
 HIDDEN NORETURN void
 x86_64_sigreturn (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
+  struct cursor *c = (struct cursor *) cursor;
+  struct sigcontext *sc = (struct sigcontext *) c->sigcontext_addr;
+  mcontext_t *sc_mcontext = &((ucontext_t*)sc)->uc_mcontext;
+  /* Copy in saved uc - all preserved regs are at the start of sigcontext */
+  memcpy(sc_mcontext, &c->uc->uc_mcontext,
+         DWARF_NUM_PRESERVED_REGS * sizeof(unw_word_t));
 
-    Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
-           (unsigned long long) c->dwarf.ip, sc);
-    __asm__ __volatile__ ("mov %0, %%rsp;"
-                          "mov %1, %%rax;"
-                          "syscall"
-                          :: "r"(sc), "i"(SYS_rt_sigreturn)
-                          : "memory");
-    abort();
+  Debug (8, "resuming at ip=%llx via sigreturn(%p)\n",
+             (unsigned long long) c->dwarf.ip, sc);
+  __asm__ __volatile__ ("mov %0, %%rsp;"
+                        "mov %1, %%rax;"
+                        "syscall"
+                        :: "r"(sc), "i"(SYS_rt_sigreturn)
+                        : "memory");
+  abort();
 }
 
 #endif
diff --git a/sgx_unwind/libunwind/src/dwarf/Gstep.c b/sgx_unwind/libunwind/src/x86_64/Greg_states_iterate.c
similarity index 72%
copy from sgx_unwind/libunwind/src/dwarf/Gstep.c
copy to sgx_unwind/libunwind/src/x86_64/Greg_states_iterate.c
index 3dcc72b..a17dc1b 100644
--- a/sgx_unwind/libunwind/src/dwarf/Gstep.c
+++ b/sgx_unwind/libunwind/src/x86_64/Greg_states_iterate.c
@@ -1,6 +1,8 @@
 /* libunwind - a platform-independent unwind library
-   Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+   Copyright (c) 2002-2003 Hewlett-Packard Development Company, L.P.
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+
+   Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
 This file is part of libunwind.
 
@@ -23,20 +25,13 @@
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
-#include "dwarf.h"
-#include "libunwind_i.h"
+#include "unwind_i.h"
 
-HIDDEN int
-dwarf_step (struct dwarf_cursor *c)
+int
+unw_reg_states_iterate (unw_cursor_t *cursor,
+			unw_reg_states_callback cb, void *token)
 {
-    int ret;
+  struct cursor *c = (struct cursor *) cursor;
 
-    if ((ret = dwarf_find_save_locs (c)) >= 0)
-    {
-        c->pi_valid = 0;
-        ret = 1;
-    }
-
-    Debug (15, "returning %d\n", ret);
-    return ret;
+  return dwarf_reg_states_iterate (&c->dwarf, cb, token);
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gregs.c b/sgx_unwind/libunwind/src/x86_64/Gregs.c
index 1712fa8..baf8a24 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gregs.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gregs.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2002-2004 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -31,33 +31,33 @@
 static inline dwarf_loc_t
 linux_scratch_loc (struct cursor *c, unw_regnum_t reg)
 {
-    unw_word_t addr = c->sigcontext_addr;
+  unw_word_t addr = c->sigcontext_addr;
 
-    switch (c->sigcontext_format)
+  switch (c->sigcontext_format)
     {
     case X86_64_SCF_NONE:
-        return DWARF_REG_LOC (&c->dwarf, reg);
+      return DWARF_REG_LOC (&c->dwarf, reg);
 
     case X86_64_SCF_LINUX_RT_SIGFRAME:
-        addr += LINUX_UC_MCONTEXT_OFF;
-        break;
+      addr += LINUX_UC_MCONTEXT_OFF;
+      break;
 
     case X86_64_SCF_FREEBSD_SIGFRAME:
-        addr += FREEBSD_UC_MCONTEXT_OFF;
-        break;
+      addr += FREEBSD_UC_MCONTEXT_OFF;
+      break;
     }
 
-    return DWARF_REG_LOC (&c->dwarf, reg);
+  return DWARF_REG_LOC (&c->dwarf, reg);
 
 }
 
 HIDDEN dwarf_loc_t
 x86_64_scratch_loc (struct cursor *c, unw_regnum_t reg)
 {
-    if (c->sigcontext_addr)
-        return linux_scratch_loc (c, reg);
-    else
-        return DWARF_REG_LOC (&c->dwarf, reg);
+  if (c->sigcontext_addr)
+    return linux_scratch_loc (c, reg);
+  else
+    return DWARF_REG_LOC (&c->dwarf, reg);
 }
 #endif
 
@@ -65,100 +65,74 @@
 tdep_access_reg (struct cursor *c, unw_regnum_t reg, unw_word_t *valp,
                  int write)
 {
-    dwarf_loc_t loc = DWARF_NULL_LOC;
-    unsigned int mask;
-    int arg_num;
+  dwarf_loc_t loc = DWARF_NULL_LOC;
+  unsigned int mask;
+  int arg_num;
 
-    switch (reg)
+  switch (reg)
     {
 
     case UNW_X86_64_RIP:
-        if (write)
-            c->dwarf.ip = *valp;		/* also update the RIP cache */
-        loc = c->dwarf.loc[RIP];
-        break;
+      if (write)
+        c->dwarf.ip = *valp;            /* also update the RIP cache */
+      loc = c->dwarf.loc[RIP];
+      break;
 
     case UNW_X86_64_CFA:
     case UNW_X86_64_RSP:
-        if (write)
-            return -UNW_EREADONLYREG;
-        *valp = c->dwarf.cfa;
-        return 0;
+      if (write)
+        return -UNW_EREADONLYREG;
+      *valp = c->dwarf.cfa;
+      return 0;
 
     case UNW_X86_64_RAX:
     case UNW_X86_64_RDX:
-        arg_num = reg - UNW_X86_64_RAX;
-        mask = (1 << arg_num);
-        if (write)
+      arg_num = reg - UNW_X86_64_RAX;
+      mask = (1 << arg_num);
+      if (write)
         {
-            c->dwarf.eh_args[arg_num] = *valp;
-            c->dwarf.eh_valid_mask |= mask;
-            return 0;
+          c->dwarf.eh_args[arg_num] = *valp;
+          c->dwarf.eh_valid_mask |= mask;
+          return 0;
         }
-        else if ((c->dwarf.eh_valid_mask & mask) != 0)
+      else if ((c->dwarf.eh_valid_mask & mask) != 0)
         {
-            *valp = c->dwarf.eh_args[arg_num];
-            return 0;
+          *valp = c->dwarf.eh_args[arg_num];
+          return 0;
         }
-        else
-            loc = c->dwarf.loc[(reg == UNW_X86_64_RAX) ? RAX : RDX];
-        break;
+      else
+        loc = c->dwarf.loc[(reg == UNW_X86_64_RAX) ? RAX : RDX];
+      break;
 
-    case UNW_X86_64_RCX:
-        loc = c->dwarf.loc[RCX];
-        break;
-    case UNW_X86_64_RBX:
-        loc = c->dwarf.loc[RBX];
-        break;
+    case UNW_X86_64_RCX: loc = c->dwarf.loc[RCX]; break;
+    case UNW_X86_64_RBX: loc = c->dwarf.loc[RBX]; break;
 
-    case UNW_X86_64_RBP:
-        loc = c->dwarf.loc[RBP];
-        break;
-    case UNW_X86_64_RSI:
-        loc = c->dwarf.loc[RSI];
-        break;
-    case UNW_X86_64_RDI:
-        loc = c->dwarf.loc[RDI];
-        break;
-    case UNW_X86_64_R8:
-        loc = c->dwarf.loc[R8];
-        break;
-    case UNW_X86_64_R9:
-        loc = c->dwarf.loc[R9];
-        break;
-    case UNW_X86_64_R10:
-        loc = c->dwarf.loc[R10];
-        break;
-    case UNW_X86_64_R11:
-        loc = c->dwarf.loc[R11];
-        break;
-    case UNW_X86_64_R12:
-        loc = c->dwarf.loc[R12];
-        break;
-    case UNW_X86_64_R13:
-        loc = c->dwarf.loc[R13];
-        break;
-    case UNW_X86_64_R14:
-        loc = c->dwarf.loc[R14];
-        break;
-    case UNW_X86_64_R15:
-        loc = c->dwarf.loc[R15];
-        break;
+    case UNW_X86_64_RBP: loc = c->dwarf.loc[RBP]; break;
+    case UNW_X86_64_RSI: loc = c->dwarf.loc[RSI]; break;
+    case UNW_X86_64_RDI: loc = c->dwarf.loc[RDI]; break;
+    case UNW_X86_64_R8: loc = c->dwarf.loc[R8]; break;
+    case UNW_X86_64_R9: loc = c->dwarf.loc[R9]; break;
+    case UNW_X86_64_R10: loc = c->dwarf.loc[R10]; break;
+    case UNW_X86_64_R11: loc = c->dwarf.loc[R11]; break;
+    case UNW_X86_64_R12: loc = c->dwarf.loc[R12]; break;
+    case UNW_X86_64_R13: loc = c->dwarf.loc[R13]; break;
+    case UNW_X86_64_R14: loc = c->dwarf.loc[R14]; break;
+    case UNW_X86_64_R15: loc = c->dwarf.loc[R15]; break;
 
     default:
-        Debug (1, "bad register number %u\n", reg);
-        return -UNW_EBADREG;
+      Debug (1, "bad register number %u\n", reg);
+      return -UNW_EBADREG;
     }
 
-    if (write)
-        return dwarf_put (&c->dwarf, loc, *valp);
-    else
-        return dwarf_get (&c->dwarf, loc, valp);
+  if (write)
+    return dwarf_put (&c->dwarf, loc, *valp);
+  else
+    return dwarf_get (&c->dwarf, loc, valp);
 }
 
 HIDDEN int
 tdep_access_fpreg (struct cursor *c, unw_regnum_t reg, unw_fpreg_t *valp,
                    int write)
 {
-    return -UNW_EBADREG;
+      return -UNW_EBADREG;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gresume.c b/sgx_unwind/libunwind/src/x86_64/Gresume.c
index d588d29..944cdaa 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gresume.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gresume.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2002-2004 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -35,27 +35,27 @@
 HIDDEN inline int
 x86_64_local_resume (unw_addr_space_t as, unw_cursor_t *cursor, void *arg)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    ucontext_t *uc = c->uc;
+  struct cursor *c = (struct cursor *) cursor;
+  ucontext_t *uc = c->uc;
 
-    /* Ensure c->pi is up-to-date.  On x86-64, it's relatively common to
-       be missing DWARF unwind info.  We don't want to fail in that
-       case, because the frame-chain still would let us do a backtrace
-       at least.  */
-    dwarf_make_proc_info (&c->dwarf);
+  /* Ensure c->pi is up-to-date.  On x86-64, it's relatively common to
+     be missing DWARF unwind info.  We don't want to fail in that
+     case, because the frame-chain still would let us do a backtrace
+     at least.  */
+  dwarf_make_proc_info (&c->dwarf);
 
-    if (unlikely (c->sigcontext_format != X86_64_SCF_NONE))
+  if (unlikely (c->sigcontext_addr != X86_64_SCF_NONE))
     {
-        x86_64_sigreturn(cursor);
-        abort();
+      x86_64_sigreturn(cursor);
+      abort();
     }
-    else
+  else
     {
-        Debug (8, "resuming at ip=%llx via setcontext()\n",
-               (unsigned long long) c->dwarf.ip);
-        setcontext (uc);
+      Debug (8, "resuming at ip=%llx via setcontext()\n",
+             (unsigned long long) c->dwarf.ip);
+      setcontext (uc);
     }
-    return -UNW_EINVAL;
+  return -UNW_EINVAL;
 }
 
 #endif /* !UNW_REMOTE_ONLY */
@@ -66,49 +66,58 @@
 static inline int
 establish_machine_state (struct cursor *c)
 {
-    int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
+  int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *,
+                     int write, void *);
+  int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
                        int write, void *);
-    int (*access_fpreg) (unw_addr_space_t, unw_regnum_t, unw_fpreg_t *,
-                         int write, void *);
-    unw_addr_space_t as = c->dwarf.as;
-    void *arg = c->dwarf.as_arg;
-    unw_fpreg_t fpval;
-    unw_word_t val;
-    int reg;
+  unw_addr_space_t as = c->dwarf.as;
+  void *arg = c->dwarf.as_arg;
+  unw_fpreg_t fpval;
+  unw_word_t val;
+  int reg;
 
-    access_reg = as->acc.access_reg;
-    access_fpreg = as->acc.access_fpreg;
+  access_reg = as->acc.access_reg;
+  access_fpreg = as->acc.access_fpreg;
 
-    Debug (8, "copying out cursor state\n");
+  Debug (8, "copying out cursor state\n");
 
-    for (reg = 0; reg <= UNW_REG_LAST; ++reg)
+  for (reg = 0; reg <= UNW_REG_LAST; ++reg)
     {
-        Debug (16, "copying %s %d\n", unw_regname (reg), reg);
-        if (unw_is_fpreg (reg))
+      Debug (16, "copying %s %d\n", unw_regname (reg), reg);
+      if (unw_is_fpreg (reg))
         {
-            if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
-                (*access_fpreg) (as, reg, &fpval, 1, arg);
+          if (tdep_access_fpreg (c, reg, &fpval, 0) >= 0)
+            (*access_fpreg) (as, reg, &fpval, 1, arg);
         }
-        else
+      else
         {
-            if (tdep_access_reg (c, reg, &val, 0) >= 0)
-                (*access_reg) (as, reg, &val, 1, arg);
+          if (tdep_access_reg (c, reg, &val, 0) >= 0)
+            (*access_reg) (as, reg, &val, 1, arg);
         }
     }
-    return 0;
+
+  if (c->dwarf.args_size)
+    {
+      if (tdep_access_reg (c, UNW_X86_64_RSP, &val, 0) >= 0)
+        {
+          val += c->dwarf.args_size;
+          (*access_reg) (as, UNW_X86_64_RSP, &val, 1, arg);
+        }
+    }
+  return 0;
 }
 
-PROTECTED int
+int
 unw_resume (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    int ret;
+  struct cursor *c = (struct cursor *) cursor;
+  int ret;
 
-    Debug (1, "(cursor=%p)\n", c);
+  Debug (1, "(cursor=%p)\n", c);
 
-    if ((ret = establish_machine_state (c)) < 0)
-        return ret;
+  if ((ret = establish_machine_state (c)) < 0)
+    return ret;
 
-    return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
-                                       c->dwarf.as_arg);
+  return (*c->dwarf.as->acc.resume) (c->dwarf.as, (unw_cursor_t *) c,
+                                     c->dwarf.as_arg);
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gstash_frame.c b/sgx_unwind/libunwind/src/x86_64/Gstash_frame.c
index f8b131a..2c7bc31 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gstash_frame.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gstash_frame.c
@@ -28,71 +28,92 @@
 HIDDEN void
 tdep_stash_frame (struct dwarf_cursor *d, struct dwarf_reg_state *rs)
 {
-    struct cursor *c = (struct cursor *) dwarf_to_cursor (d);
-    unw_tdep_frame_t *f = &c->frame_info;
+  struct cursor *c = (struct cursor *) dwarf_to_cursor (d);
+  unw_tdep_frame_t *f = &c->frame_info;
 
-    Debug (4, "ip=0x%lx cfa=0x%lx type %d cfa [where=%d val=%ld] cfaoff=%ld"
-           " ra=0x%lx rbp [where=%d val=%ld @0x%lx] rsp [where=%d val=%ld @0x%lx]\n",
-           d->ip, d->cfa, f->frame_type,
-           rs->reg[DWARF_CFA_REG_COLUMN].where,
-           rs->reg[DWARF_CFA_REG_COLUMN].val,
-           rs->reg[DWARF_CFA_OFF_COLUMN].val,
-           DWARF_GET_LOC(d->loc[d->ret_addr_column]),
-           rs->reg[RBP].where, rs->reg[RBP].val, DWARF_GET_LOC(d->loc[RBP]),
-           rs->reg[RSP].where, rs->reg[RSP].val, DWARF_GET_LOC(d->loc[RSP]));
+  Debug (4, "ip=0x%lx cfa=0x%lx type %d cfa [where=%d val=%ld] cfaoff=%ld"
+         " ra=0x%lx rbp [where=%d val=%ld @0x%lx] rsp [where=%d val=%ld @0x%lx]\n",
+         d->ip, d->cfa, f->frame_type,
+         rs->reg.where[DWARF_CFA_REG_COLUMN],
+         rs->reg.val[DWARF_CFA_REG_COLUMN],
+         rs->reg.val[DWARF_CFA_OFF_COLUMN],
+         DWARF_GET_LOC(d->loc[rs->ret_addr_column]),
+         rs->reg.where[RBP], rs->reg.val[RBP], DWARF_GET_LOC(d->loc[RBP]),
+         rs->reg.where[RSP], rs->reg.val[RSP], DWARF_GET_LOC(d->loc[RSP]));
 
-    /* A standard frame is defined as:
-        - CFA is register-relative offset off RBP or RSP;
-        - Return address is saved at CFA-8;
-        - RBP is unsaved or saved at CFA+offset, offset != -1;
-        - RSP is unsaved or saved at CFA+offset, offset != -1.  */
-    if (f->frame_type == UNW_X86_64_FRAME_OTHER
-            && (rs->reg[DWARF_CFA_REG_COLUMN].where == DWARF_WHERE_REG)
-            && (rs->reg[DWARF_CFA_REG_COLUMN].val == RBP
-                || rs->reg[DWARF_CFA_REG_COLUMN].val == RSP)
-            && labs(rs->reg[DWARF_CFA_OFF_COLUMN].val) < (1 << 29)
-            && DWARF_GET_LOC(d->loc[d->ret_addr_column]) == d->cfa-8
-            && (rs->reg[RBP].where == DWARF_WHERE_UNDEF
-                || rs->reg[RBP].where == DWARF_WHERE_SAME
-                || (rs->reg[RBP].where == DWARF_WHERE_CFAREL
-                    && labs(rs->reg[RBP].val) < (1 << 14)
-                    && rs->reg[RBP].val+1 != 0))
-            && (rs->reg[RSP].where == DWARF_WHERE_UNDEF
-                || rs->reg[RSP].where == DWARF_WHERE_SAME
-                || (rs->reg[RSP].where == DWARF_WHERE_CFAREL
-                    && labs(rs->reg[RSP].val) < (1 << 14)
-                    && rs->reg[RSP].val+1 != 0)))
-    {
-        /* Save information for a standard frame. */
-        f->frame_type = UNW_X86_64_FRAME_STANDARD;
-        f->cfa_reg_rsp = (rs->reg[DWARF_CFA_REG_COLUMN].val == RSP);
-        f->cfa_reg_offset = rs->reg[DWARF_CFA_OFF_COLUMN].val;
-        if (rs->reg[RBP].where == DWARF_WHERE_CFAREL)
-            f->rbp_cfa_offset = rs->reg[RBP].val;
-        if (rs->reg[RSP].where == DWARF_WHERE_CFAREL)
-            f->rsp_cfa_offset = rs->reg[RSP].val;
-        Debug (4, " standard frame\n");
+  if (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_EXPR &&
+    rs->reg.where[RBP] == DWARF_WHERE_EXPR) {
+    /* Check for GCC generated alignment frame for rsp.  A simple
+     * def_cfa_expr that loads a constant offset from rbp, where the
+     * addres of the rip was pushed on the stack */
+    unw_word_t cfa_addr = rs->reg.val[DWARF_CFA_REG_COLUMN];
+    unw_word_t rbp_addr = rs->reg.val[RBP];
+    unw_word_t cfa_offset;
+
+    int ret = dwarf_stack_aligned(d, cfa_addr, rbp_addr, &cfa_offset);
+    if (ret) {
+      f->frame_type = UNW_X86_64_FRAME_ALIGNED;
+      f->cfa_reg_offset = cfa_offset;
+      f->cfa_reg_rsp = 0;
     }
+  }
 
-    /* Signal frame was detected via augmentation in tdep_fetch_frame()  */
-    else if (f->frame_type == UNW_X86_64_FRAME_SIGRETURN)
-    {
-        /* Later we are going to fish out {RBP,RSP,RIP} from sigcontext via
-           their ucontext_t offsets.  Confirm DWARF info agrees with the
-           offsets we expect.  */
+  /* A standard frame is defined as:
+      - CFA is register-relative offset off RBP or RSP;
+      - Return address is saved at CFA-8;
+      - RBP is unsaved or saved at CFA+offset, offset != -1;
+      - RSP is unsaved or saved at CFA+offset, offset != -1.  */
+  if (f->frame_type == UNW_X86_64_FRAME_OTHER
+      && (rs->reg.where[DWARF_CFA_REG_COLUMN] == DWARF_WHERE_REG)
+      && (rs->reg.val[DWARF_CFA_REG_COLUMN] == RBP
+          || rs->reg.val[DWARF_CFA_REG_COLUMN] == RSP)
+      && labs((long) rs->reg.val[DWARF_CFA_OFF_COLUMN]) < (1 << 28)
+      && DWARF_GET_LOC(d->loc[rs->ret_addr_column]) == d->cfa-8
+      && (rs->reg.where[RBP] == DWARF_WHERE_UNDEF
+          || rs->reg.where[RBP] == DWARF_WHERE_SAME
+          || (rs->reg.where[RBP] == DWARF_WHERE_CFAREL
+              && labs((long) rs->reg.val[RBP]) < (1 << 14)
+              && rs->reg.val[RBP]+1 != 0))
+      && (rs->reg.where[RSP] == DWARF_WHERE_UNDEF
+          || rs->reg.where[RSP] == DWARF_WHERE_SAME
+          || (rs->reg.where[RSP] == DWARF_WHERE_CFAREL
+              && labs((long) rs->reg.val[RSP]) < (1 << 14)
+              && rs->reg.val[RSP]+1 != 0)))
+  {
+    /* Save information for a standard frame. */
+    f->frame_type = UNW_X86_64_FRAME_STANDARD;
+    f->cfa_reg_rsp = (rs->reg.val[DWARF_CFA_REG_COLUMN] == RSP);
+    f->cfa_reg_offset = rs->reg.val[DWARF_CFA_OFF_COLUMN];
+    if (rs->reg.where[RBP] == DWARF_WHERE_CFAREL)
+      f->rbp_cfa_offset = rs->reg.val[RBP];
+    if (rs->reg.where[RSP] == DWARF_WHERE_CFAREL)
+      f->rsp_cfa_offset = rs->reg.val[RSP];
+    Debug (4, " standard frame\n");
+  }
+
+  /* Signal frame was detected via augmentation in tdep_fetch_frame()  */
+  else if (f->frame_type == UNW_X86_64_FRAME_SIGRETURN)
+  {
+    /* Later we are going to fish out {RBP,RSP,RIP} from sigcontext via
+       their ucontext_t offsets.  Confirm DWARF info agrees with the
+       offsets we expect.  */
 
 #ifndef NDEBUG
-        const unw_word_t uc = c->sigcontext_addr;
+    const unw_word_t uc = c->sigcontext_addr;
 
-        assert (DWARF_GET_LOC(d->loc[RIP]) - uc == UC_MCONTEXT_GREGS_RIP);
-        assert (DWARF_GET_LOC(d->loc[RBP]) - uc == UC_MCONTEXT_GREGS_RBP);
-        assert (DWARF_GET_LOC(d->loc[RSP]) - uc == UC_MCONTEXT_GREGS_RSP);
+    assert (DWARF_GET_LOC(d->loc[RIP]) - uc == UC_MCONTEXT_GREGS_RIP);
+    assert (DWARF_GET_LOC(d->loc[RBP]) - uc == UC_MCONTEXT_GREGS_RBP);
+    assert (DWARF_GET_LOC(d->loc[RSP]) - uc == UC_MCONTEXT_GREGS_RSP);
 #endif
 
-        Debug (4, " sigreturn frame\n");
-    }
+    Debug (4, " sigreturn frame\n");
+  }
 
-    /* PLT and guessed RBP-walked frames are handled in unw_step(). */
-    else
-        Debug (4, " unusual frame\n");
+  else if (f->frame_type == UNW_X86_64_FRAME_ALIGNED) {
+    Debug (4, " aligned frame, offset %li\n", f->cfa_reg_offset);
+  }
+
+  /* PLT and guessed RBP-walked frames are handled in unw_step(). */
+  else
+    Debug (4, " unusual frame\n");
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gstep.c b/sgx_unwind/libunwind/src/x86_64/Gstep.c
index 507a3bc..1049817 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gstep.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gstep.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002-2004 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -35,65 +35,65 @@
 static int
 is_plt_entry (struct dwarf_cursor *c)
 {
-    unw_word_t w0, w1;
-    unw_accessors_t *a;
-    int ret;
+  unw_word_t w0, w1;
+  unw_accessors_t *a;
+  int ret;
 
-    a = unw_get_accessors (c->as);
-    if ((ret = (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg)) < 0
-            || (ret = (*a->access_mem) (c->as, c->ip + 8, &w1, 0, c->as_arg)) < 0)
-        return 0;
+  a = unw_get_accessors_int (c->as);
+  if ((ret = (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg)) < 0
+      || (ret = (*a->access_mem) (c->as, c->ip + 8, &w1, 0, c->as_arg)) < 0)
+    return 0;
 
-    ret = (((w0 & 0xffff) == 0x25ff)
-           && (((w0 >> 48) & 0xff) == 0x68)
-           && (((w1 >> 24) & 0xff) == 0xe9));
+  ret = (((w0 & 0xffff) == 0x25ff)
+         && (((w0 >> 48) & 0xff) == 0x68)
+         && (((w1 >> 24) & 0xff) == 0xe9));
 
-    Debug (14, "ip=0x%lx => 0x%016lx 0x%016lx, ret = %d\n", c->ip, w0, w1, ret);
-    return ret;
+  Debug (14, "ip=0x%lx => 0x%016lx 0x%016lx, ret = %d\n", c->ip, w0, w1, ret);
+  return ret;
 }
 
-PROTECTED int
+int
 unw_step (unw_cursor_t *cursor)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    int ret, i;
+  struct cursor *c = (struct cursor *) cursor;
+  int ret, i;
 
 #if CONSERVATIVE_CHECKS
-    int val = c->validate;
-    c->validate = 1;
+  int val = c->validate;
+  c->validate = 1;
 #endif
 
-    Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
-           c, c->dwarf.ip, c->dwarf.cfa);
+  Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
+         c, c->dwarf.ip, c->dwarf.cfa);
 
-    /* Try DWARF-based unwinding... */
-    c->sigcontext_format = X86_64_SCF_NONE;
-    ret = dwarf_step (&c->dwarf);
+  /* Try DWARF-based unwinding... */
+  c->sigcontext_format = X86_64_SCF_NONE;
+  ret = dwarf_step (&c->dwarf);
 
 #if CONSERVATIVE_CHECKS
-    c->validate = val;
+  c->validate = val;
 #endif
 
-    if (ret < 0 && ret != -UNW_ENOINFO)
+  if (ret < 0 && ret != -UNW_ENOINFO)
     {
-        Debug (2, "returning %d\n", ret);
-        return ret;
+      Debug (2, "returning %d\n", ret);
+      return ret;
     }
 
-    if (likely (ret >= 0))
+  if (likely (ret >= 0))
     {
-        /* x86_64 ABI specifies that end of call-chain is marked with a
-        NULL RBP.  */
-        if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
-        {
+      /* x86_64 ABI specifies that end of call-chain is marked with a
+         NULL RBP or undefined return address  */
+      if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
+          {
             c->dwarf.ip = 0;
             ret = 0;
-        }
+          }
     }
-    else
+  else
     {
-        /* DWARF failed.  There isn't much of a usable frame-chain on x86-64,
-        but we do need to handle two special-cases:
+      /* DWARF failed.  There isn't much of a usable frame-chain on x86-64,
+         but we do need to handle two special-cases:
 
           (i) signal trampoline: Old kernels and older libcs don't
               export the vDSO needed to get proper unwind info for the
@@ -104,119 +104,124 @@
               via CALLQ.  Try this for all non-signal trampoline
               code.  */
 
-        unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
-        struct dwarf_loc rbp_loc, rsp_loc, rip_loc;
+      unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
+      struct dwarf_loc rbp_loc, rsp_loc, rip_loc;
 
-        /* We could get here because of missing/bad unwind information.
-           Validate all addresses before dereferencing. */
-        c->validate = 1;
+      /* We could get here because of missing/bad unwind information.
+         Validate all addresses before dereferencing. */
+      c->validate = 1;
 
-        Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
+      Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
 
-        if (unw_is_signal_frame (cursor))
+      if (unw_is_signal_frame (cursor) > 0)
         {
-            ret = unw_handle_signal_frame(cursor);
-            if (ret < 0)
+          ret = x86_64_handle_signal_frame(cursor);
+          if (ret < 0)
             {
-                Debug (2, "returning 0\n");
-                return 0;
+              Debug (2, "returning 0\n");
+              return 0;
             }
         }
-        else if (is_plt_entry (&c->dwarf))
+      else if (is_plt_entry (&c->dwarf))
         {
-            /* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
-            Debug (2, "found plt entry\n");
-            c->frame_info.cfa_reg_offset = 8;
-            c->frame_info.cfa_reg_rsp = -1;
-            c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
-            c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
-            c->dwarf.cfa += 8;
+          /* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
+          Debug (2, "found plt entry\n");
+          c->frame_info.cfa_reg_offset = 8;
+          c->frame_info.cfa_reg_rsp = -1;
+          c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
+          c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
+          c->dwarf.cfa += 8;
         }
-        else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
+      else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
         {
-            for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
-                c->dwarf.loc[i] = DWARF_NULL_LOC;
+          for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
+            c->dwarf.loc[i] = DWARF_NULL_LOC;
         }
-        else
+      else
         {
-            unw_word_t rbp;
+          unw_word_t rbp;
 
-            ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
-            if (ret < 0)
+          ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
+          if (ret < 0)
             {
-                Debug (2, "returning %d [RBP=0x%lx]\n", ret,
-                       DWARF_GET_LOC (c->dwarf.loc[RBP]));
-                return ret;
+              Debug (2, "returning %d [RBP=0x%lx]\n", ret,
+                     DWARF_GET_LOC (c->dwarf.loc[RBP]));
+              return ret;
             }
 
-            if (!rbp)
+          if (!rbp)
             {
-                /* Looks like we may have reached the end of the call-chain.  */
-                rbp_loc = DWARF_NULL_LOC;
-                rsp_loc = DWARF_NULL_LOC;
-                rip_loc = DWARF_NULL_LOC;
+              /* Looks like we may have reached the end of the call-chain.  */
+              rbp_loc = DWARF_NULL_LOC;
+              rsp_loc = DWARF_NULL_LOC;
+              rip_loc = DWARF_NULL_LOC;
             }
-            else
+          else
             {
-                unw_word_t rbp1 = 0;
-                rbp_loc = DWARF_LOC(rbp, 0);
-                rsp_loc = DWARF_NULL_LOC;
-                rip_loc = DWARF_LOC (rbp + 8, 0);
-                ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
-                Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
-                       (unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
-                       rbp, c->dwarf.cfa, rbp1);
+              unw_word_t rbp1 = 0;
+              rbp_loc = DWARF_LOC(rbp, 0);
+              rsp_loc = DWARF_NULL_LOC;
+              rip_loc = DWARF_LOC (rbp + 8, 0);
+              ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
+              Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
+                     (unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
+                     rbp, c->dwarf.cfa, rbp1);
 
-                /* Heuristic to determine incorrect guess.  For RBP to be a
-                   valid frame it needs to be above current CFA, but don't
-                let it go more than a little.  Note that we can't deduce
+              /* Heuristic to determine incorrect guess.  For RBP to be a
+                 valid frame it needs to be above current CFA, but don't
+                 let it go more than a little.  Note that we can't deduce
                  anything about new RBP (rbp1) since it may not be a frame
                  pointer in the frame above.  Just check we get the value. */
-                if (ret < 0
-                        || rbp <= c->dwarf.cfa
-                        || (rbp - c->dwarf.cfa) > 0x4000)
+              if (ret < 0
+                  || rbp < c->dwarf.cfa
+                  || (rbp - c->dwarf.cfa) > 0x4000)
                 {
-                    rip_loc = DWARF_NULL_LOC;
-                    rbp_loc = DWARF_NULL_LOC;
+                  rip_loc = DWARF_NULL_LOC;
+                  rbp_loc = DWARF_NULL_LOC;
                 }
 
-                c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
-                c->frame_info.cfa_reg_rsp = 0;
-                c->frame_info.cfa_reg_offset = 16;
-                c->frame_info.rbp_cfa_offset = -16;
-                c->dwarf.cfa += 16;
+              c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
+              c->frame_info.cfa_reg_rsp = 0;
+              c->frame_info.cfa_reg_offset = 16;
+              c->frame_info.rbp_cfa_offset = -16;
+              c->dwarf.cfa += 16;
             }
 
-            /* Mark all registers unsaved */
-            for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
-                c->dwarf.loc[i] = DWARF_NULL_LOC;
+          /* Mark all registers unsaved */
+          for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
+            c->dwarf.loc[i] = DWARF_NULL_LOC;
 
-            c->dwarf.loc[RBP] = rbp_loc;
-            c->dwarf.loc[RSP] = rsp_loc;
-            c->dwarf.loc[RIP] = rip_loc;
+          c->dwarf.loc[RBP] = rbp_loc;
+          c->dwarf.loc[RSP] = rsp_loc;
+          c->dwarf.loc[RIP] = rip_loc;
+          c->dwarf.use_prev_instr = 1;
         }
 
-        c->dwarf.ret_addr_column = RIP;
-
-        if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
+      if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
         {
-            ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
-            Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
-                   (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
-                   (unsigned long long) c->dwarf.ip);
-            if (ret < 0)
-            {
-                Debug (2, "returning %d\n", ret);
-                return ret;
-            }
-            ret = 1;
+          ret = 0;
+          Debug (2, "NULL %%rbp loc, returning %d\n", ret);
+          return ret;
         }
-        else
-            c->dwarf.ip = 0;
+      if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
+        {
+          ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
+          Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
+                     (unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
+                     (unsigned long long) c->dwarf.ip);
+          if (ret < 0)
+            {
+              Debug (2, "returning %d\n", ret);
+              return ret;
+            }
+          ret = 1;
+        }
+      else
+        c->dwarf.ip = 0;
 
-        if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
-            return -UNW_EBADFRAME;
+      if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
+        return -UNW_EBADFRAME;
     }
-    Debug (2, "returning %d\n", ret);
-    return ret;
+  Debug (2, "returning %d\n", ret);
+  return ret;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Gtrace.c b/sgx_unwind/libunwind/src/x86_64/Gtrace.c
index 95abed7..7412271 100644
--- a/sgx_unwind/libunwind/src/x86_64/Gtrace.c
+++ b/sgx_unwind/libunwind/src/x86_64/Gtrace.c
@@ -37,15 +37,15 @@
 
 typedef struct
 {
-    unw_tdep_frame_t *frames;
-    size_t log_size;
-    size_t used;
-    size_t dtor_count;  /* Counts how many times our destructor has already
-			 been called. */
+  unw_tdep_frame_t *frames;
+  size_t log_size;
+  size_t used;
+  size_t dtor_count;  /* Counts how many times our destructor has already
+                         been called. */
 } unw_trace_cache_t;
 
 static const unw_tdep_frame_t empty_frame = { 0, UNW_X86_64_FRAME_OTHER, -1, -1, 0, -1, -1 };
-static pthread_mutex_t trace_init_lock = PTHREAD_MUTEX_INITIALIZER;
+static define_lock (trace_init_lock);
 static pthread_once_t trace_cache_once = PTHREAD_ONCE_INIT;
 static sig_atomic_t trace_cache_once_happen;
 static pthread_key_t trace_cache_key;
@@ -57,43 +57,43 @@
 static void
 trace_cache_free (void *arg)
 {
-    unw_trace_cache_t *cache = arg;
-    if (++cache->dtor_count < PTHREAD_DESTRUCTOR_ITERATIONS)
-    {
-        /* Not yet our turn to get destroyed. Re-install ourselves into the key. */
-        pthread_setspecific(trace_cache_key, cache);
-        Debug(5, "delayed freeing cache %p (%zx to go)\n", cache,
-              PTHREAD_DESTRUCTOR_ITERATIONS - cache->dtor_count);
-        return;
-    }
-    tls_cache_destroyed = 1;
-    tls_cache = NULL;
-    munmap (cache->frames, (1u << cache->log_size) * sizeof(unw_tdep_frame_t));
-    mempool_free (&trace_cache_pool, cache);
-    Debug(5, "freed cache %p\n", cache);
+  unw_trace_cache_t *cache = arg;
+  if (++cache->dtor_count < PTHREAD_DESTRUCTOR_ITERATIONS)
+  {
+    /* Not yet our turn to get destroyed. Re-install ourselves into the key. */
+    pthread_setspecific(trace_cache_key, cache);
+    Debug(5, "delayed freeing cache %p (%zx to go)\n", cache,
+          PTHREAD_DESTRUCTOR_ITERATIONS - cache->dtor_count);
+    return;
+  }
+  tls_cache_destroyed = 1;
+  tls_cache = NULL;
+  munmap (cache->frames, (1u << cache->log_size) * sizeof(unw_tdep_frame_t));
+  mempool_free (&trace_cache_pool, cache);
+  Debug(5, "freed cache %p\n", cache);
 }
 
 /* Initialise frame tracing for threaded use. */
 static void
 trace_cache_init_once (void)
 {
-    pthread_key_create (&trace_cache_key, &trace_cache_free);
-    mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
-    trace_cache_once_happen = 1;
+  pthread_key_create (&trace_cache_key, &trace_cache_free);
+  mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
+  trace_cache_once_happen = 1;
 }
 
 static unw_tdep_frame_t *
 trace_cache_buckets (size_t n)
 {
-    unw_tdep_frame_t *frames;
-    size_t i;
+  unw_tdep_frame_t *frames;
+  size_t i;
 
-    GET_MEMORY(frames, n * sizeof (unw_tdep_frame_t));
-    if (likely(frames != 0))
-        for (i = 0; i < n; ++i)
-            frames[i] = empty_frame;
+  GET_MEMORY(frames, n * sizeof (unw_tdep_frame_t));
+  if (likely(frames != NULL))
+    for (i = 0; i < n; ++i)
+      frames[i] = empty_frame;
 
-    return frames;
+  return frames;
 }
 
 /* Allocate and initialise hash table for frame cache lookups.
@@ -102,36 +102,36 @@
 static unw_trace_cache_t *
 trace_cache_create (void)
 {
-    unw_trace_cache_t *cache;
+  unw_trace_cache_t *cache;
 
-    if (tls_cache_destroyed)
-    {
-        /* The current thread is in the process of exiting. Don't recreate
-           cache, as we wouldn't have another chance to free it. */
-        Debug(5, "refusing to reallocate cache: "
-              "thread-locals are being deallocated\n");
-        return NULL;
-    }
+  if (tls_cache_destroyed)
+  {
+    /* The current thread is in the process of exiting. Don't recreate
+       cache, as we wouldn't have another chance to free it. */
+    Debug(5, "refusing to reallocate cache: "
+             "thread-locals are being deallocated\n");
+    return NULL;
+  }
 
-    if (! (cache = mempool_alloc(&trace_cache_pool)))
-    {
-        Debug(5, "failed to allocate cache\n");
-        return NULL;
-    }
+  if (! (cache = mempool_alloc(&trace_cache_pool)))
+  {
+    Debug(5, "failed to allocate cache\n");
+    return NULL;
+  }
 
-    if (! (cache->frames = trace_cache_buckets(1u << HASH_MIN_BITS)))
-    {
-        Debug(5, "failed to allocate buckets\n");
-        mempool_free(&trace_cache_pool, cache);
-        return NULL;
-    }
+  if (! (cache->frames = trace_cache_buckets(1u << HASH_MIN_BITS)))
+  {
+    Debug(5, "failed to allocate buckets\n");
+    mempool_free(&trace_cache_pool, cache);
+    return NULL;
+  }
 
-    cache->log_size = HASH_MIN_BITS;
-    cache->used = 0;
-    cache->dtor_count = 0;
-    tls_cache_destroyed = 0;  /* Paranoia: should already be 0. */
-    Debug(5, "allocated cache %p\n", cache);
-    return cache;
+  cache->log_size = HASH_MIN_BITS;
+  cache->used = 0;
+  cache->dtor_count = 0;
+  tls_cache_destroyed = 0;  /* Paranoia: should already be 0. */
+  Debug(5, "allocated cache %p\n", cache);
+  return cache;
 }
 
 /* Expand the hash table in the frame cache if possible. This always
@@ -139,67 +139,67 @@
 static int
 trace_cache_expand (unw_trace_cache_t *cache)
 {
-    size_t old_size = (1u << cache->log_size);
-    size_t new_log_size = cache->log_size + 2;
-    unw_tdep_frame_t *new_frames = trace_cache_buckets (1u << new_log_size);
+  size_t old_size = (1u << cache->log_size);
+  size_t new_log_size = cache->log_size + 2;
+  unw_tdep_frame_t *new_frames = trace_cache_buckets (1u << new_log_size);
 
-    if (unlikely(! new_frames))
-    {
-        Debug(5, "failed to expand cache to 2^%lu buckets\n", new_log_size);
-        return -UNW_ENOMEM;
-    }
+  if (unlikely(! new_frames))
+  {
+    Debug(5, "failed to expand cache to 2^%lu buckets\n", new_log_size);
+    return -UNW_ENOMEM;
+  }
 
-    Debug(5, "expanded cache from 2^%lu to 2^%lu buckets\n", cache->log_size, new_log_size);
-    munmap(cache->frames, old_size * sizeof(unw_tdep_frame_t));
-    cache->frames = new_frames;
-    cache->log_size = new_log_size;
-    cache->used = 0;
-    return 0;
+  Debug(5, "expanded cache from 2^%lu to 2^%lu buckets\n", cache->log_size, new_log_size);
+  munmap(cache->frames, old_size * sizeof(unw_tdep_frame_t));
+  cache->frames = new_frames;
+  cache->log_size = new_log_size;
+  cache->used = 0;
+  return 0;
 }
 
 static unw_trace_cache_t *
 trace_cache_get_unthreaded (void)
 {
-    unw_trace_cache_t *cache;
-    intrmask_t saved_mask;
-    static unw_trace_cache_t *global_cache = 0;
-    lock_acquire (&trace_init_lock, saved_mask);
-    if (! global_cache)
-    {
-        mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
-        global_cache = trace_cache_create ();
-    }
-    cache = global_cache;
-    lock_release (&trace_init_lock, saved_mask);
-    Debug(5, "using cache %p\n", cache);
-    return cache;
+  unw_trace_cache_t *cache;
+  intrmask_t saved_mask;
+  static unw_trace_cache_t *global_cache = NULL;
+  lock_acquire (&trace_init_lock, saved_mask);
+  if (! global_cache)
+  {
+    mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
+    global_cache = trace_cache_create ();
+  }
+  cache = global_cache;
+  lock_release (&trace_init_lock, saved_mask);
+  Debug(5, "using cache %p\n", cache);
+  return cache;
 }
 
 /* Get the frame cache for the current thread. Create it if there is none. */
 static unw_trace_cache_t *
 trace_cache_get (void)
 {
-    unw_trace_cache_t *cache;
-    if (likely (pthread_once != 0))
+  unw_trace_cache_t *cache;
+  if (likely (pthread_once != NULL))
+  {
+    pthread_once(&trace_cache_once, &trace_cache_init_once);
+    if (!trace_cache_once_happen)
     {
-        pthread_once(&trace_cache_once, &trace_cache_init_once);
-        if (!trace_cache_once_happen)
-        {
-            return trace_cache_get_unthreaded();
-        }
-        if (! (cache = tls_cache))
-        {
-            cache = trace_cache_create();
-            pthread_setspecific(trace_cache_key, cache);
-            tls_cache = cache;
-        }
-        Debug(5, "using cache %p\n", cache);
-        return cache;
+      return trace_cache_get_unthreaded();
     }
-    else
+    if (! (cache = tls_cache))
     {
-        return trace_cache_get_unthreaded();
+      cache = trace_cache_create();
+      pthread_setspecific(trace_cache_key, cache);
+      tls_cache = cache;
     }
+    Debug(5, "using cache %p\n", cache);
+    return cache;
+  }
+  else
+  {
+    return trace_cache_get_unthreaded();
+  }
 }
 
 /* Initialise frame properties for address cache slot F at address
@@ -218,51 +218,51 @@
                  unw_word_t rbp,
                  unw_word_t rsp)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    struct dwarf_cursor *d = &c->dwarf;
-    int ret = -UNW_EINVAL;
+  struct cursor *c = (struct cursor *) cursor;
+  struct dwarf_cursor *d = &c->dwarf;
+  int ret = -UNW_EINVAL;
 
-    /* Initialise frame properties: unknown, not last. */
-    f->virtual_address = rip;
-    f->frame_type = UNW_X86_64_FRAME_OTHER;
-    f->last_frame = 0;
-    f->cfa_reg_rsp = -1;
-    f->cfa_reg_offset = 0;
-    f->rbp_cfa_offset = -1;
-    f->rsp_cfa_offset = -1;
+  /* Initialise frame properties: unknown, not last. */
+  f->virtual_address = rip;
+  f->frame_type = UNW_X86_64_FRAME_OTHER;
+  f->last_frame = 0;
+  f->cfa_reg_rsp = -1;
+  f->cfa_reg_offset = 0;
+  f->rbp_cfa_offset = -1;
+  f->rsp_cfa_offset = -1;
 
-    /* Reinitialise cursor to this instruction - but undo next/prev RIP
-       adjustment because unw_step will redo it - and force RIP, RBP
-       RSP into register locations (=~ ucontext we keep), then set
-       their desired values. Then perform the step. */
-    d->ip = rip + d->use_prev_instr;
-    d->cfa = cfa;
-    d->loc[UNW_X86_64_RIP] = DWARF_REG_LOC (d, UNW_X86_64_RIP);
-    d->loc[UNW_X86_64_RBP] = DWARF_REG_LOC (d, UNW_X86_64_RBP);
-    d->loc[UNW_X86_64_RSP] = DWARF_REG_LOC (d, UNW_X86_64_RSP);
-    c->frame_info = *f;
+  /* Reinitialise cursor to this instruction - but undo next/prev RIP
+     adjustment because unw_step will redo it - and force RIP, RBP
+     RSP into register locations (=~ ucontext we keep), then set
+     their desired values. Then perform the step. */
+  d->ip = rip + d->use_prev_instr;
+  d->cfa = cfa;
+  d->loc[UNW_X86_64_RIP] = DWARF_REG_LOC (d, UNW_X86_64_RIP);
+  d->loc[UNW_X86_64_RBP] = DWARF_REG_LOC (d, UNW_X86_64_RBP);
+  d->loc[UNW_X86_64_RSP] = DWARF_REG_LOC (d, UNW_X86_64_RSP);
+  c->frame_info = *f;
 
-    if (likely(dwarf_put (d, d->loc[UNW_X86_64_RIP], rip) >= 0)
-            && likely(dwarf_put (d, d->loc[UNW_X86_64_RBP], rbp) >= 0)
-            && likely(dwarf_put (d, d->loc[UNW_X86_64_RSP], rsp) >= 0)
-            && likely((ret = unw_step (cursor)) >= 0))
-        *f = c->frame_info;
+  if (likely(dwarf_put (d, d->loc[UNW_X86_64_RIP], rip) >= 0)
+      && likely(dwarf_put (d, d->loc[UNW_X86_64_RBP], rbp) >= 0)
+      && likely(dwarf_put (d, d->loc[UNW_X86_64_RSP], rsp) >= 0)
+      && likely((ret = unw_step (cursor)) >= 0))
+    *f = c->frame_info;
 
-    /* If unw_step() stopped voluntarily, remember that, even if it
-       otherwise could not determine anything useful.  This avoids
-       failing trace if we hit frames without unwind info, which is
-       common for the outermost frame (CRT stuff) on many systems.
-       This avoids failing trace in very common circumstances; failing
-       to unw_step() loop wouldn't produce any better result. */
-    if (ret == 0)
-        f->last_frame = -1;
+  /* If unw_step() stopped voluntarily, remember that, even if it
+     otherwise could not determine anything useful.  This avoids
+     failing trace if we hit frames without unwind info, which is
+     common for the outermost frame (CRT stuff) on many systems.
+     This avoids failing trace in very common circumstances; failing
+     to unw_step() loop wouldn't produce any better result. */
+  if (ret == 0)
+    f->last_frame = -1;
 
-    Debug (3, "frame va %lx type %d last %d cfa %s+%d rbp @ cfa%+d rsp @ cfa%+d\n",
-           f->virtual_address, f->frame_type, f->last_frame,
-           f->cfa_reg_rsp ? "rsp" : "rbp", f->cfa_reg_offset,
-           f->rbp_cfa_offset, f->rsp_cfa_offset);
+  Debug (3, "frame va %lx type %d last %d cfa %s+%d rbp @ cfa%+d rsp @ cfa%+d\n",
+         f->virtual_address, f->frame_type, f->last_frame,
+         f->cfa_reg_rsp ? "rsp" : "rbp", f->cfa_reg_offset,
+         f->rbp_cfa_offset, f->rsp_cfa_offset);
 
-    return f;
+  return f;
 }
 
 /* Look up and if necessary fill in frame attributes for address RIP
@@ -277,57 +277,57 @@
               unw_word_t rbp,
               unw_word_t rsp)
 {
-    /* First look up for previously cached information using cache as
-       linear probing hash table with probe step of 1.  Majority of
-       lookups should be completed within few steps, but it is very
-       important the hash table does not fill up, or performance falls
-       off the cliff. */
-    uint64_t i, addr;
-    uint64_t cache_size = 1u << cache->log_size;
-    uint64_t slot = ((rip * 0x9e3779b97f4a7c16) >> 43) & (cache_size-1);
-    unw_tdep_frame_t *frame;
+  /* First look up for previously cached information using cache as
+     linear probing hash table with probe step of 1.  Majority of
+     lookups should be completed within few steps, but it is very
+     important the hash table does not fill up, or performance falls
+     off the cliff. */
+  uint64_t i, addr;
+  uint64_t cache_size = 1u << cache->log_size;
+  uint64_t slot = ((rip * 0x9e3779b97f4a7c16) >> 43) & (cache_size-1);
+  unw_tdep_frame_t *frame;
 
-    for (i = 0; i < 16; ++i)
+  for (i = 0; i < 16; ++i)
+  {
+    frame = &cache->frames[slot];
+    addr = frame->virtual_address;
+
+    /* Return if we found the address. */
+    if (likely(addr == rip))
     {
-        frame = &cache->frames[slot];
-        addr = frame->virtual_address;
-
-        /* Return if we found the address. */
-        if (likely(addr == rip))
-        {
-            Debug (4, "found address after %ld steps\n", i);
-            return frame;
-        }
-
-        /* If slot is empty, reuse it. */
-        if (likely(! addr))
-            break;
-
-        /* Linear probe to next slot candidate, step = 1. */
-        if (++slot >= cache_size)
-            slot -= cache_size;
+      Debug (4, "found address after %ld steps\n", i);
+      return frame;
     }
 
-    /* If we collided after 16 steps, or if the hash is more than half
-       full, force the hash to expand. Fill the selected slot, whether
-       it's free or collides. Note that hash expansion drops previous
-       contents; further lookups will refill the hash. */
-    Debug (4, "updating slot %lu after %ld steps, replacing 0x%lx\n", slot, i, addr);
-    if (unlikely(addr || cache->used >= cache_size / 2))
-    {
-        if (unlikely(trace_cache_expand (cache) < 0))
-            return NULL;
+    /* If slot is empty, reuse it. */
+    if (likely(! addr))
+      break;
 
-        cache_size = 1u << cache->log_size;
-        slot = ((rip * 0x9e3779b97f4a7c16) >> 43) & (cache_size-1);
-        frame = &cache->frames[slot];
-        addr = frame->virtual_address;
-    }
+    /* Linear probe to next slot candidate, step = 1. */
+    if (++slot >= cache_size)
+      slot -= cache_size;
+  }
 
-    if (! addr)
-        ++cache->used;
+  /* If we collided after 16 steps, or if the hash is more than half
+     full, force the hash to expand. Fill the selected slot, whether
+     it's free or collides. Note that hash expansion drops previous
+     contents; further lookups will refill the hash. */
+  Debug (4, "updating slot %lu after %ld steps, replacing 0x%lx\n", slot, i, addr);
+  if (unlikely(addr || cache->used >= cache_size / 2))
+  {
+    if (unlikely(trace_cache_expand (cache) < 0))
+      return NULL;
 
-    return trace_init_addr (frame, cursor, cfa, rip, rbp, rsp);
+    cache_size = 1u << cache->log_size;
+    slot = ((rip * 0x9e3779b97f4a7c16) >> 43) & (cache_size-1);
+    frame = &cache->frames[slot];
+    addr = frame->virtual_address;
+  }
+
+  if (! addr)
+    ++cache->used;
+
+  return trace_init_addr (frame, cursor, cfa, rip, rbp, rsp);
 }
 
 /* Fast stack backtrace for x86-64.
@@ -396,138 +396,156 @@
 HIDDEN int
 tdep_trace (unw_cursor_t *cursor, void **buffer, int *size)
 {
-    struct cursor *c = (struct cursor *) cursor;
-    struct dwarf_cursor *d = &c->dwarf;
-    unw_trace_cache_t *cache;
-    unw_word_t rbp, rsp, rip, cfa;
-    int maxdepth = 0;
-    int depth = 0;
-    int ret;
+  struct cursor *c = (struct cursor *) cursor;
+  struct dwarf_cursor *d = &c->dwarf;
+  unw_trace_cache_t *cache;
+  unw_word_t rbp, rsp, rip, cfa;
+  int maxdepth = 0;
+  int depth = 0;
+  int ret;
 
-    /* Check input parametres. */
-    if (unlikely(! cursor || ! buffer || ! size || (maxdepth = *size) <= 0))
-        return -UNW_EINVAL;
+  /* Check input parametres. */
+  if (unlikely(! cursor || ! buffer || ! size || (maxdepth = *size) <= 0))
+    return -UNW_EINVAL;
 
-    Debug (1, "begin ip 0x%lx cfa 0x%lx\n", d->ip, d->cfa);
+  Debug (1, "begin ip 0x%lx cfa 0x%lx\n", d->ip, d->cfa);
 
-    /* Tell core dwarf routines to call back to us. */
-    d->stash_frames = 1;
+  /* Tell core dwarf routines to call back to us. */
+  d->stash_frames = 1;
 
-    /* Determine initial register values. These are direct access safe
-       because we know they come from the initial machine context. */
-    rip = d->ip;
-    rsp = cfa = d->cfa;
-    ACCESS_MEM_FAST(ret, 0, d, DWARF_GET_LOC(d->loc[UNW_X86_64_RBP]), rbp);
-    assert(ret == 0);
+  /* Determine initial register values. These are direct access safe
+     because we know they come from the initial machine context. */
+  rip = d->ip;
+  rsp = cfa = d->cfa;
+  ACCESS_MEM_FAST(ret, 0, d, DWARF_GET_LOC(d->loc[UNW_X86_64_RBP]), rbp);
+  assert(ret == 0);
 
-    /* Get frame cache. */
-    if (unlikely(! (cache = trace_cache_get())))
+  /* Get frame cache. */
+  if (unlikely(! (cache = trace_cache_get())))
+  {
+    Debug (1, "returning %d, cannot get trace cache\n", -UNW_ENOMEM);
+    *size = 0;
+    d->stash_frames = 0;
+    return -UNW_ENOMEM;
+  }
+
+  /* Trace the stack upwards, starting from current RIP.  Adjust
+     the RIP address for previous/next instruction as the main
+     unwinding logic would also do.  We undo this before calling
+     back into unw_step(). */
+  while (depth < maxdepth)
+  {
+    rip -= d->use_prev_instr;
+    Debug (2, "depth %d cfa 0x%lx rip 0x%lx rsp 0x%lx rbp 0x%lx\n",
+           depth, cfa, rip, rsp, rbp);
+
+    /* See if we have this address cached.  If not, evaluate enough of
+       the dwarf unwind information to fill the cache line data, or to
+       decide this frame cannot be handled in fast trace mode.  We
+       cache negative results too to prevent unnecessary dwarf parsing
+       for common failures. */
+    unw_tdep_frame_t *f = trace_lookup (cursor, cache, cfa, rip, rbp, rsp);
+
+    /* If we don't have information for this frame, give up. */
+    if (unlikely(! f))
     {
-        Debug (1, "returning %d, cannot get trace cache\n", -UNW_ENOMEM);
-        *size = 0;
-        d->stash_frames = 0;
-        return -UNW_ENOMEM;
+      ret = -UNW_ENOINFO;
+      break;
     }
 
-    /* Trace the stack upwards, starting from current RIP.  Adjust
-       the RIP address for previous/next instruction as the main
-       unwinding logic would also do.  We undo this before calling
-       back into unw_step(). */
-    while (depth < maxdepth)
+    Debug (3, "frame va %lx type %d last %d cfa %s+%d rbp @ cfa%+d rsp @ cfa%+d\n",
+           f->virtual_address, f->frame_type, f->last_frame,
+           f->cfa_reg_rsp ? "rsp" : "rbp", f->cfa_reg_offset,
+           f->rbp_cfa_offset, f->rsp_cfa_offset);
+
+    assert (f->virtual_address == rip);
+
+    /* Stop if this was the last frame.  In particular don't evaluate
+       new register values as it may not be safe - we don't normally
+       run with full validation on, and do not want to - and there's
+       enough bad unwind info floating around that we need to trust
+       what unw_step() previously said, in potentially bogus frames. */
+    if (f->last_frame)
+      break;
+
+    /* Evaluate CFA and registers for the next frame. */
+    switch (f->frame_type)
     {
-        rip -= d->use_prev_instr;
-        Debug (2, "depth %d cfa 0x%lx rip 0x%lx rsp 0x%lx rbp 0x%lx\n",
-               depth, cfa, rip, rsp, rbp);
+    case UNW_X86_64_FRAME_GUESSED:
+      /* Fall thru to standard processing after forcing validation. */
+      c->validate = 1;
 
-        /* See if we have this address cached.  If not, evaluate enough of
-           the dwarf unwind information to fill the cache line data, or to
-           decide this frame cannot be handled in fast trace mode.  We
-           cache negative results too to prevent unnecessary dwarf parsing
-           for common failures. */
-        unw_tdep_frame_t *f = trace_lookup (cursor, cache, cfa, rip, rbp, rsp);
+    case UNW_X86_64_FRAME_STANDARD:
+      /* Advance standard traceable frame. */
+      cfa = (f->cfa_reg_rsp ? rsp : rbp) + f->cfa_reg_offset;
+      ACCESS_MEM_FAST(ret, c->validate, d, cfa - 8, rip);
+      if (likely(ret >= 0) && likely(f->rbp_cfa_offset != -1))
+        ACCESS_MEM_FAST(ret, c->validate, d, cfa + f->rbp_cfa_offset, rbp);
 
-        /* If we don't have information for this frame, give up. */
-        if (unlikely(! f))
-        {
-            ret = -UNW_ENOINFO;
-            break;
-        }
+      /* Don't bother reading RSP from DWARF, CFA becomes new RSP. */
+      rsp = cfa;
 
-        Debug (3, "frame va %lx type %d last %d cfa %s+%d rbp @ cfa%+d rsp @ cfa%+d\n",
-               f->virtual_address, f->frame_type, f->last_frame,
-               f->cfa_reg_rsp ? "rsp" : "rbp", f->cfa_reg_offset,
-               f->rbp_cfa_offset, f->rsp_cfa_offset);
+      /* Next frame needs to back up for unwind info lookup. */
+      d->use_prev_instr = 1;
+      break;
 
-        assert (f->virtual_address == rip);
+    case UNW_X86_64_FRAME_SIGRETURN:
+      cfa = cfa + f->cfa_reg_offset; /* cfa now points to ucontext_t.  */
 
-        /* Stop if this was the last frame.  In particular don't evaluate
-           new register values as it may not be safe - we don't normally
-           run with full validation on, and do not want to - and there's
-           enough bad unwind info floating around that we need to trust
-           what unw_step() previously said, in potentially bogus frames. */
-        if (f->last_frame)
-            break;
+      ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RIP, rip);
+      if (likely(ret >= 0))
+        ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RBP, rbp);
+      if (likely(ret >= 0))
+        ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RSP, rsp);
 
-        /* Evaluate CFA and registers for the next frame. */
-        switch (f->frame_type)
-        {
-        case UNW_X86_64_FRAME_GUESSED:
-            /* Fall thru to standard processing after forcing validation. */
-            c->validate = 1;
+      /* Resume stack at signal restoration point. The stack is not
+         necessarily continuous here, especially with sigaltstack(). */
+      cfa = rsp;
 
-        case UNW_X86_64_FRAME_STANDARD:
-            /* Advance standard traceable frame. */
-            cfa = (f->cfa_reg_rsp ? rsp : rbp) + f->cfa_reg_offset;
-            ACCESS_MEM_FAST(ret, c->validate, d, cfa - 8, rip);
-            if (likely(ret >= 0) && likely(f->rbp_cfa_offset != -1))
-                ACCESS_MEM_FAST(ret, c->validate, d, cfa + f->rbp_cfa_offset, rbp);
+      /* Next frame should not back up. */
+      d->use_prev_instr = 0;
+      break;
 
-            /* Don't bother reading RSP from DWARF, CFA becomes new RSP. */
-            rsp = cfa;
+    case UNW_X86_64_FRAME_ALIGNED:
+      /* Address of RIP was pushed on the stack via a simple
+       * def_cfa_expr - result stack offset stored in cfa_reg_offset */
+      cfa = (f->cfa_reg_rsp ? rsp : rbp) + f->cfa_reg_offset;
+      ACCESS_MEM_FAST(ret, c->validate, d, cfa, cfa);
+      if (likely(ret >= 0))
+        ACCESS_MEM_FAST(ret, c->validate, d, cfa - 8, rip);
+      if (likely(ret >= 0))
+        ACCESS_MEM_FAST(ret, c->validate, d, rbp, rbp);
 
-            /* Next frame needs to back up for unwind info lookup. */
-            d->use_prev_instr = 1;
-            break;
+      /* Don't bother reading RSP from DWARF, CFA becomes new RSP. */
+      rsp = cfa;
 
-        case UNW_X86_64_FRAME_SIGRETURN:
-            cfa = cfa + f->cfa_reg_offset; /* cfa now points to ucontext_t.  */
+      /* Next frame needs to back up for unwind info lookup. */
+      d->use_prev_instr = 1;
 
-            ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RIP, rip);
-            if (likely(ret >= 0))
-                ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RBP, rbp);
-            if (likely(ret >= 0))
-                ACCESS_MEM_FAST(ret, c->validate, d, cfa + UC_MCONTEXT_GREGS_RSP, rsp);
+      break;
 
-            /* Resume stack at signal restoration point. The stack is not
-               necessarily continuous here, especially with sigaltstack(). */
-            cfa = rsp;
-
-            /* Next frame should not back up. */
-            d->use_prev_instr = 0;
-            break;
-
-        default:
-            /* We cannot trace through this frame, give up and tell the
-            caller we had to stop.  Data collected so far may still be
-             useful to the caller, so let it know how far we got.  */
-            ret = -UNW_ESTOPUNWIND;
-            break;
-        }
-
-        Debug (4, "new cfa 0x%lx rip 0x%lx rsp 0x%lx rbp 0x%lx\n",
-               cfa, rip, rsp, rbp);
-
-        /* If we failed or ended up somewhere bogus, stop. */
-        if (unlikely(ret < 0 || rip < 0x4000))
-            break;
-
-        /* Record this address in stack trace. We skipped the first address. */
-        buffer[depth++] = (void *) (rip - d->use_prev_instr);
+    default:
+      /* We cannot trace through this frame, give up and tell the
+         caller we had to stop.  Data collected so far may still be
+         useful to the caller, so let it know how far we got.  */
+      ret = -UNW_ESTOPUNWIND;
+      break;
     }
 
+    Debug (4, "new cfa 0x%lx rip 0x%lx rsp 0x%lx rbp 0x%lx\n",
+           cfa, rip, rsp, rbp);
+
+    /* If we failed or ended up somewhere bogus, stop. */
+    if (unlikely(ret < 0 || rip < 0x4000))
+      break;
+
+    /* Record this address in stack trace. We skipped the first address. */
+    buffer[depth++] = (void *) (rip - d->use_prev_instr);
+  }
+
 #if UNW_DEBUG
-    Debug (1, "returning %d, depth %d\n", ret, depth);
+  Debug (1, "returning %d, depth %d\n", ret, depth);
 #endif
-    *size = depth;
-    return ret;
+  *size = depth;
+  return ret;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c b/sgx_unwind/libunwind/src/x86_64/Lapply_reg_state.c
similarity index 78%
copy from sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
copy to sgx_unwind/libunwind/src/x86_64/Lapply_reg_state.c
index b9a7c4f..7ebada4 100644
--- a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
+++ b/sgx_unwind/libunwind/src/x86_64/Lapply_reg_state.c
@@ -1,5 +1,5 @@
 #define UNW_LOCAL_ONLY
 #include <libunwind.h>
 #if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
-#include "Gis_signal_frame.c"
+#include "Gapply_reg_state.c"
 #endif
diff --git a/sgx_unwind/libunwind/src/x86_64/Lglobal.c b/sgx_unwind/libunwind/src/x86_64/Lglobal.c
index 6d7b489..8c43a67 100644
--- a/sgx_unwind/libunwind/src/x86_64/Lglobal.c
+++ b/sgx_unwind/libunwind/src/x86_64/Lglobal.c
@@ -1,4 +1,5 @@
 #define UNW_LOCAL_ONLY
+#include "config.h"
 #include <libunwind.h>
 #if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
 #include "Gglobal.c"
diff --git a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c b/sgx_unwind/libunwind/src/x86_64/Lreg_states_iterate.c
similarity index 76%
copy from sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
copy to sgx_unwind/libunwind/src/x86_64/Lreg_states_iterate.c
index b9a7c4f..f1eb1e7 100644
--- a/sgx_unwind/libunwind/src/x86_64/Lis_signal_frame.c
+++ b/sgx_unwind/libunwind/src/x86_64/Lreg_states_iterate.c
@@ -1,5 +1,5 @@
 #define UNW_LOCAL_ONLY
 #include <libunwind.h>
 #if defined(UNW_LOCAL_ONLY) && !defined(UNW_REMOTE_ONLY)
-#include "Gis_signal_frame.c"
+#include "Greg_states_iterate.c"
 #endif
diff --git a/sgx_unwind/libunwind/src/x86_64/init.h b/sgx_unwind/libunwind/src/x86_64/init.h
index c8abba1..a7a996f 100644
--- a/sgx_unwind/libunwind/src/x86_64/init.h
+++ b/sgx_unwind/libunwind/src/x86_64/init.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -44,46 +44,46 @@
 static inline int
 common_init (struct cursor *c, unsigned use_prev_instr)
 {
-    int ret;
+  int ret;
 
-    c->dwarf.loc[RAX] = REG_INIT_LOC(c, rax, RAX);
-    c->dwarf.loc[RDX] = REG_INIT_LOC(c, rdx, RDX);
-    c->dwarf.loc[RCX] = REG_INIT_LOC(c, rcx, RCX);
-    c->dwarf.loc[RBX] = REG_INIT_LOC(c, rbx, RBX);
-    c->dwarf.loc[RSI] = REG_INIT_LOC(c, rsi, RSI);
-    c->dwarf.loc[RDI] = REG_INIT_LOC(c, rdi, RDI);
-    c->dwarf.loc[RBP] = REG_INIT_LOC(c, rbp, RBP);
-    c->dwarf.loc[RSP] = REG_INIT_LOC(c, rsp, RSP);
-    c->dwarf.loc[R8]  = REG_INIT_LOC(c, r8,  R8);
-    c->dwarf.loc[R9]  = REG_INIT_LOC(c, r9,  R9);
-    c->dwarf.loc[R10] = REG_INIT_LOC(c, r10, R10);
-    c->dwarf.loc[R11] = REG_INIT_LOC(c, r11, R11);
-    c->dwarf.loc[R12] = REG_INIT_LOC(c, r12, R12);
-    c->dwarf.loc[R13] = REG_INIT_LOC(c, r13, R13);
-    c->dwarf.loc[R14] = REG_INIT_LOC(c, r14, R14);
-    c->dwarf.loc[R15] = REG_INIT_LOC(c, r15, R15);
-    c->dwarf.loc[RIP] = REG_INIT_LOC(c, rip, RIP);
+  c->dwarf.loc[RAX] = REG_INIT_LOC(c, rax, RAX);
+  c->dwarf.loc[RDX] = REG_INIT_LOC(c, rdx, RDX);
+  c->dwarf.loc[RCX] = REG_INIT_LOC(c, rcx, RCX);
+  c->dwarf.loc[RBX] = REG_INIT_LOC(c, rbx, RBX);
+  c->dwarf.loc[RSI] = REG_INIT_LOC(c, rsi, RSI);
+  c->dwarf.loc[RDI] = REG_INIT_LOC(c, rdi, RDI);
+  c->dwarf.loc[RBP] = REG_INIT_LOC(c, rbp, RBP);
+  c->dwarf.loc[RSP] = REG_INIT_LOC(c, rsp, RSP);
+  c->dwarf.loc[R8]  = REG_INIT_LOC(c, r8,  R8);
+  c->dwarf.loc[R9]  = REG_INIT_LOC(c, r9,  R9);
+  c->dwarf.loc[R10] = REG_INIT_LOC(c, r10, R10);
+  c->dwarf.loc[R11] = REG_INIT_LOC(c, r11, R11);
+  c->dwarf.loc[R12] = REG_INIT_LOC(c, r12, R12);
+  c->dwarf.loc[R13] = REG_INIT_LOC(c, r13, R13);
+  c->dwarf.loc[R14] = REG_INIT_LOC(c, r14, R14);
+  c->dwarf.loc[R15] = REG_INIT_LOC(c, r15, R15);
+  c->dwarf.loc[RIP] = REG_INIT_LOC(c, rip, RIP);
 
-    ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
-    if (ret < 0)
-        return ret;
+  ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
+  if (ret < 0)
+    return ret;
 
-    ret = dwarf_get (&c->dwarf, DWARF_REG_LOC (&c->dwarf, UNW_X86_64_RSP),
-                     &c->dwarf.cfa);
-    if (ret < 0)
-        return ret;
+  ret = dwarf_get (&c->dwarf, DWARF_REG_LOC (&c->dwarf, UNW_X86_64_RSP),
+                   &c->dwarf.cfa);
+  if (ret < 0)
+    return ret;
 
-    c->sigcontext_format = X86_64_SCF_NONE;
-    c->sigcontext_addr = 0;
+  c->sigcontext_format = X86_64_SCF_NONE;
+  c->sigcontext_addr = 0;
 
-    c->dwarf.args_size = 0;
-    c->dwarf.ret_addr_column = RIP;
-    c->dwarf.stash_frames = 0;
-    c->dwarf.use_prev_instr = use_prev_instr;
-    c->dwarf.pi_valid = 0;
-    c->dwarf.pi_is_dynamic = 0;
-    c->dwarf.hint = 0;
-    c->dwarf.prev_rs = 0;
+  c->dwarf.args_size = 0;
+  c->dwarf.stash_frames = 0;
+  c->dwarf.use_prev_instr = use_prev_instr;
+  c->dwarf.pi_valid = 0;
+  c->dwarf.pi_is_dynamic = 0;
+  c->dwarf.hint = 0;
+  c->dwarf.prev_rs = 0;
+  c->dwarf.eh_valid_mask = 0;
 
-    return 0;
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/is_fpreg.c b/sgx_unwind/libunwind/src/x86_64/is_fpreg.c
index e99b18d..5c03613 100644
--- a/sgx_unwind/libunwind/src/x86_64/is_fpreg.c
+++ b/sgx_unwind/libunwind/src/x86_64/is_fpreg.c
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (c) 2004-2005 Hewlett-Packard Development Company, L.P.
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -27,12 +27,12 @@
 
 #include "libunwind_i.h"
 
-PROTECTED int
+int
 unw_is_fpreg (int regnum)
 {
 #if 0
-    return ((regnum >= UNW_X86_ST0 && regnum <= UNW_X86_ST7)
-            || (regnum >= UNW_X86_XMM0_lo && regnum <= UNW_X86_XMM7_hi));
+  return ((regnum >= UNW_X86_ST0 && regnum <= UNW_X86_ST7)
+          || (regnum >= UNW_X86_XMM0_lo && regnum <= UNW_X86_XMM7_hi));
 #endif
-    return 0;
+  return 0;
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/offsets.h b/sgx_unwind/libunwind/src/x86_64/offsets.h
index 80cfe36..0807960 100644
--- a/sgx_unwind/libunwind/src/x86_64/offsets.h
+++ b/sgx_unwind/libunwind/src/x86_64/offsets.h
@@ -1,3 +1,3 @@
 /* FreeBSD specific definitions */
 
-#define FREEBSD_UC_MCONTEXT_OFF		0x10
+#define FREEBSD_UC_MCONTEXT_OFF         0x10
diff --git a/sgx_unwind/libunwind/src/x86_64/regname.c b/sgx_unwind/libunwind/src/x86_64/regname.c
index 7522935..77660af 100644
--- a/sgx_unwind/libunwind/src/x86_64/regname.c
+++ b/sgx_unwind/libunwind/src/x86_64/regname.c
@@ -26,7 +26,7 @@
 #include "unwind_i.h"
 
 static const char *regname[] =
-{
+  {
     "RAX",
     "RDX",
     "RCX",
@@ -44,13 +44,13 @@
     "R14",
     "R15",
     "RIP",
-};
+   };
 
-PROTECTED const char *
+const char *
 unw_regname (unw_regnum_t reg)
 {
-    if (reg < (unw_regnum_t) ARRAY_SIZE (regname))
-        return regname[reg];
-    else
-        return "???";
+  if (reg < (unw_regnum_t) ARRAY_SIZE (regname))
+    return regname[reg];
+  else
+    return "???";
 }
diff --git a/sgx_unwind/libunwind/src/x86_64/ucontext_i.h b/sgx_unwind/libunwind/src/x86_64/ucontext_i.h
index c1249bf..aded941 100644
--- a/sgx_unwind/libunwind/src/x86_64/ucontext_i.h
+++ b/sgx_unwind/libunwind/src/x86_64/ucontext_i.h
@@ -23,60 +23,60 @@
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
 
 #if defined __linux__
-#define UC_MCONTEXT_GREGS_R8	0x28
-#define UC_MCONTEXT_GREGS_R9	0x30
-#define UC_MCONTEXT_GREGS_R10	0x38
-#define UC_MCONTEXT_GREGS_R11	0x40
-#define UC_MCONTEXT_GREGS_R12	0x48
-#define UC_MCONTEXT_GREGS_R13	0x50
-#define UC_MCONTEXT_GREGS_R14	0x58
-#define UC_MCONTEXT_GREGS_R15	0x60
-#define UC_MCONTEXT_GREGS_RDI	0x68
-#define UC_MCONTEXT_GREGS_RSI	0x70
-#define UC_MCONTEXT_GREGS_RBP	0x78
-#define UC_MCONTEXT_GREGS_RBX	0x80
-#define UC_MCONTEXT_GREGS_RDX	0x88
-#define UC_MCONTEXT_GREGS_RAX	0x90
-#define UC_MCONTEXT_GREGS_RCX	0x98
-#define UC_MCONTEXT_GREGS_RSP	0xa0
-#define UC_MCONTEXT_GREGS_RIP	0xa8
+#define UC_MCONTEXT_GREGS_R8    0x28
+#define UC_MCONTEXT_GREGS_R9    0x30
+#define UC_MCONTEXT_GREGS_R10   0x38
+#define UC_MCONTEXT_GREGS_R11   0x40
+#define UC_MCONTEXT_GREGS_R12   0x48
+#define UC_MCONTEXT_GREGS_R13   0x50
+#define UC_MCONTEXT_GREGS_R14   0x58
+#define UC_MCONTEXT_GREGS_R15   0x60
+#define UC_MCONTEXT_GREGS_RDI   0x68
+#define UC_MCONTEXT_GREGS_RSI   0x70
+#define UC_MCONTEXT_GREGS_RBP   0x78
+#define UC_MCONTEXT_GREGS_RBX   0x80
+#define UC_MCONTEXT_GREGS_RDX   0x88
+#define UC_MCONTEXT_GREGS_RAX   0x90
+#define UC_MCONTEXT_GREGS_RCX   0x98
+#define UC_MCONTEXT_GREGS_RSP   0xa0
+#define UC_MCONTEXT_GREGS_RIP   0xa8
 #define UC_MCONTEXT_FPREGS_PTR  0x1a8
-#define UC_MCONTEXT_FPREGS_MEM	0xe0
-#define UC_SIGMASK		0x128
-#define FPREGS_OFFSET_MXCSR	0x18
+#define UC_MCONTEXT_FPREGS_MEM  0xe0
+#define UC_SIGMASK              0x128
+#define FPREGS_OFFSET_MXCSR     0x18
 #elif defined __FreeBSD__
-#define	UC_SIGMASK		0x0
-#define UC_MCONTEXT_GREGS_RDI	0x18
-#define UC_MCONTEXT_GREGS_RSI	0x20
-#define UC_MCONTEXT_GREGS_RDX	0x28
-#define UC_MCONTEXT_GREGS_RCX	0x30
-#define UC_MCONTEXT_GREGS_R8	0x38
-#define UC_MCONTEXT_GREGS_R9	0x40
-#define UC_MCONTEXT_GREGS_RAX	0x48
-#define UC_MCONTEXT_GREGS_RBX	0x50
-#define UC_MCONTEXT_GREGS_RBP	0x58
-#define UC_MCONTEXT_GREGS_R10	0x60
-#define UC_MCONTEXT_GREGS_R11	0x68
-#define UC_MCONTEXT_GREGS_R12	0x70
-#define UC_MCONTEXT_GREGS_R13	0x78
-#define UC_MCONTEXT_GREGS_R14	0x80
-#define UC_MCONTEXT_GREGS_R15	0x88
-#define UC_MCONTEXT_FS		0x94
-#define UC_MCONTEXT_GS		0x96
-#define UC_MCONTEXT_FLAGS	0xa0
-#define UC_MCONTEXT_ES		0xa4
-#define UC_MCONTEXT_DS		0xa6
-#define UC_MCONTEXT_GREGS_RIP	0xb0
-#define UC_MCONTEXT_CS		0xb8
-#define UC_MCONTEXT_RFLAGS	0xc0
-#define UC_MCONTEXT_GREGS_RSP	0xc8
-#define UC_MCONTEXT_SS		0xd0
-#define UC_MCONTEXT_MC_LEN	0xd8
-#define UC_MCONTEXT_FPFORMAT	0xe0
-#define UC_MCONTEXT_OWNEDFP	0xe8
-#define UC_MCONTEXT_FPSTATE	0xf0
-#define UC_MCONTEXT_FPOWNED_FPU	0x20001
-#define UC_MCONTEXT_FPFMT_XMM	0x10002
-#define UC_MCONTEXT_MC_LEN_VAL	0x320
+#define UC_SIGMASK              0x0
+#define UC_MCONTEXT_GREGS_RDI   0x18
+#define UC_MCONTEXT_GREGS_RSI   0x20
+#define UC_MCONTEXT_GREGS_RDX   0x28
+#define UC_MCONTEXT_GREGS_RCX   0x30
+#define UC_MCONTEXT_GREGS_R8    0x38
+#define UC_MCONTEXT_GREGS_R9    0x40
+#define UC_MCONTEXT_GREGS_RAX   0x48
+#define UC_MCONTEXT_GREGS_RBX   0x50
+#define UC_MCONTEXT_GREGS_RBP   0x58
+#define UC_MCONTEXT_GREGS_R10   0x60
+#define UC_MCONTEXT_GREGS_R11   0x68
+#define UC_MCONTEXT_GREGS_R12   0x70
+#define UC_MCONTEXT_GREGS_R13   0x78
+#define UC_MCONTEXT_GREGS_R14   0x80
+#define UC_MCONTEXT_GREGS_R15   0x88
+#define UC_MCONTEXT_FS          0x94
+#define UC_MCONTEXT_GS          0x96
+#define UC_MCONTEXT_FLAGS       0xa0
+#define UC_MCONTEXT_ES          0xa4
+#define UC_MCONTEXT_DS          0xa6
+#define UC_MCONTEXT_GREGS_RIP   0xb0
+#define UC_MCONTEXT_CS          0xb8
+#define UC_MCONTEXT_RFLAGS      0xc0
+#define UC_MCONTEXT_GREGS_RSP   0xc8
+#define UC_MCONTEXT_SS          0xd0
+#define UC_MCONTEXT_MC_LEN      0xd8
+#define UC_MCONTEXT_FPFORMAT    0xe0
+#define UC_MCONTEXT_OWNEDFP     0xe8
+#define UC_MCONTEXT_FPSTATE     0xf0
+#define UC_MCONTEXT_FPOWNED_FPU 0x20001
+#define UC_MCONTEXT_FPFMT_XMM   0x10002
+#define UC_MCONTEXT_MC_LEN_VAL  0x320
 
 #endif
diff --git a/sgx_unwind/libunwind/src/x86_64/unwind_i.h b/sgx_unwind/libunwind/src/x86_64/unwind_i.h
index b559749..e95a60f 100644
--- a/sgx_unwind/libunwind/src/x86_64/unwind_i.h
+++ b/sgx_unwind/libunwind/src/x86_64/unwind_i.h
@@ -1,6 +1,6 @@
 /* libunwind - a platform-independent unwind library
    Copyright (C) 2002, 2005 Hewlett-Packard Co
-	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
+        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
 
    Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
 
@@ -28,7 +28,6 @@
 #ifndef unwind_i_h
 #define unwind_i_h
 
-#include <memory.h>
 #include <stdint.h>
 
 #include <libunwind-x86_64.h>
@@ -37,33 +36,33 @@
 #include <sys/ucontext.h>
 
 /* DWARF column numbers for x86_64: */
-#define RAX	0
-#define RDX	1
-#define RCX	2
-#define RBX	3
-#define RSI	4
-#define RDI	5
-#define RBP	6
-#define RSP	7
-#define R8	8
-#define R9	9
-#define R10	10
-#define R11	11
-#define R12	12
-#define R13	13
-#define R14	14
-#define R15	15
-#define RIP	16
+#define RAX     0
+#define RDX     1
+#define RCX     2
+#define RBX     3
+#define RSI     4
+#define RDI     5
+#define RBP     6
+#define RSP     7
+#define R8      8
+#define R9      9
+#define R10     10
+#define R11     11
+#define R12     12
+#define R13     13
+#define R14     14
+#define R15     15
+#define RIP     16
 
-#define x86_64_lock			UNW_OBJ(lock)
-#define x86_64_local_resume		UNW_OBJ(local_resume)
-#define x86_64_local_addr_space_init	UNW_OBJ(local_addr_space_init)
-#define setcontext			UNW_ARCH_OBJ (setcontext)
+#define x86_64_lock                     UNW_OBJ(lock)
+#define x86_64_local_resume             UNW_OBJ(local_resume)
+#define x86_64_local_addr_space_init    UNW_OBJ(local_addr_space_init)
+#define setcontext                      UNW_ARCH_OBJ (setcontext)
 #if 0
-#define x86_64_scratch_loc		UNW_OBJ(scratch_loc)
+#define x86_64_scratch_loc              UNW_OBJ(scratch_loc)
 #endif
-#define x86_64_r_uc_addr		UNW_OBJ(r_uc_addr)
-#define x86_64_sigreturn		UNW_OBJ(sigreturn)
+#define x86_64_r_uc_addr                UNW_OBJ(r_uc_addr)
+#define x86_64_sigreturn                UNW_OBJ(sigreturn)
 
 /* By-pass calls to access_mem() when known to be safe. */
 #ifdef UNW_LOCAL_ONLY
@@ -79,7 +78,7 @@
 
 extern void x86_64_local_addr_space_init (void);
 extern int x86_64_local_resume (unw_addr_space_t as, unw_cursor_t *cursor,
-                                void *arg);
+                             void *arg);
 extern int setcontext (const ucontext_t *ucp);
 
 #if 0
@@ -88,5 +87,7 @@
 
 extern void *x86_64_r_uc_addr (ucontext_t *uc, int reg);
 extern NORETURN void x86_64_sigreturn (unw_cursor_t *cursor);
+#define x86_64_handle_signal_frame UNW_OBJ(handle_signal_frame)
+extern int x86_64_handle_signal_frame(unw_cursor_t *cursor);
 
 #endif /* unwind_i_h */
diff --git a/sgx_unwind/lib.rs b/sgx_unwind/src/lib.rs
similarity index 85%
rename from sgx_unwind/lib.rs
rename to sgx_unwind/src/lib.rs
index 9a87305..e8526ea 100644
--- a/sgx_unwind/lib.rs
+++ b/sgx_unwind/src/lib.rs
@@ -16,18 +16,18 @@
 // under the License..
 
 #![no_std]
+#![cfg_attr(
+    all(target_env = "sgx", target_vendor = "mesalock"),
+    feature(rustc_private)
+)]
 #![unstable(feature = "panic_unwind", issue = "32837")]
-#![deny(warnings)]
-
+#![feature(c_unwind)]
 #![feature(nll)]
 #![feature(staged_api)]
-#![feature(unwind_attributes)]
-
-#![cfg_attr(target_env = "sgx", feature(rustc_private))]
+#![allow(clippy::upper_case_acronyms)]
 
 #[macro_use]
 mod macros;
 
 mod libunwind;
 pub use libunwind::*;
-
diff --git a/sgx_unwind/libunwind.rs b/sgx_unwind/src/libunwind.rs
similarity index 89%
rename from sgx_unwind/libunwind.rs
rename to sgx_unwind/src/libunwind.rs
index 493e5fc..f8dc375 100644
--- a/sgx_unwind/libunwind.rs
+++ b/sgx_unwind/src/libunwind.rs
@@ -22,7 +22,7 @@
 type uintptr_t = usize;
 
 #[repr(C)]
-#[derive(Debug, Copy, Clone, PartialEq)]
+#[derive(Clone, Copy, Debug, PartialEq)]
 pub enum _Unwind_Reason_Code {
     _URC_NO_REASON = 0,
     _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
@@ -61,9 +61,10 @@
 pub type _Unwind_Exception_Cleanup_Fn =
     extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
 
-extern "C" {
-    #[unwind(allowed)]
+extern "C-unwind" {
     pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
+}
+extern "C" {
     pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
     pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
     pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
@@ -72,7 +73,7 @@
 }
 
 #[repr(C)]
-#[derive(Copy, Clone, PartialEq)]
+#[derive(Clone, Copy, PartialEq)]
 pub enum _Unwind_Action {
     _UA_SEARCH_PHASE = 1,
     _UA_CLEANUP_PHASE = 2,
@@ -87,12 +88,18 @@
     pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
     pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
     pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
-    pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int) -> _Unwind_Word;
+    pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
+        -> _Unwind_Word;
     pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
+    pub fn _Unwind_GetCFA(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
 }
 
-extern "C" {
-    #[unwind(allowed)]
+extern "C-unwind" {
     pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
-    pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn, trace_argument: *mut c_void) -> _Unwind_Reason_Code;
+}
+extern "C" {
+    pub fn _Unwind_Backtrace(
+        trace: _Unwind_Trace_Fn,
+        trace_argument: *mut c_void,
+    ) -> _Unwind_Reason_Code;
 }
diff --git a/sgx_unwind/macros.rs b/sgx_unwind/src/macros.rs
similarity index 99%
rename from sgx_unwind/macros.rs
rename to sgx_unwind/src/macros.rs
index cf550ac..a1d26d6 100644
--- a/sgx_unwind/macros.rs
+++ b/sgx_unwind/src/macros.rs
@@ -72,4 +72,3 @@
         $(#[$m] $it)*
     };
 }
-
diff --git a/sgx_urts/Cargo.toml b/sgx_urts/Cargo.toml
index c8a20cc..00c5b2b 100644
--- a/sgx_urts/Cargo.toml
+++ b/sgx_urts/Cargo.toml
@@ -1,10 +1,10 @@
 [package]
 name = "sgx_urts"
-version = "1.1.3"
+version = "1.1.4"
 authors = ["The Teaclave Authors"]
 repository = "https://github.com/apache/teaclave-sgx-sdk"
 license-file = "LICENSE"
-documentation = "https://dingelish.github.io/"
+documentation = "https://teaclave.apache.org/sgx-sdk-docs/"
 description = "Rust SGX SDK provides the ability to write Intel SGX applications in Rust Programming Language."
 edition = "2018"
 
@@ -16,9 +16,7 @@
 default = []
 global_init = ["global_exit"]
 global_exit = ["global_init"]
-signal = []
-
 
 [dependencies]
 sgx_types = { path = "../sgx_types" }
-libc = "0.2.40"
+libc = "0.2"
diff --git a/sgx_urts/src/enclave.rs b/sgx_urts/src/enclave.rs
index 704ceb6..1af6e26 100644
--- a/sgx_urts/src/enclave.rs
+++ b/sgx_urts/src/enclave.rs
@@ -435,9 +435,11 @@
     num_uworkers: u32,
     num_tworkers: u32,
 ) -> SgxResult<sgx_enclave_id_t> {
-    let mut us_config = sgx_uswitchless_config_t::default();
-    us_config.num_tworkers = num_tworkers;
-    us_config.num_uworkers = num_uworkers;
+    let us_config = sgx_uswitchless_config_t {
+        num_tworkers,
+        num_uworkers,
+        ..Default::default()
+    };
     let mut enclave_ex_p: [*const c_void; 32] = [ptr::null(); 32];
     enclave_ex_p[SGX_CREATE_ENCLAVE_EX_SWITCHLESS_BIT_IDX] =
         &us_config as *const sgx_uswitchless_config_t as *const c_void;
@@ -671,10 +673,21 @@
         // before this function returns.
     }
 
+    #[inline]
     pub fn geteid(&self) -> sgx_enclave_id_t {
         self.id
     }
 
+    #[inline]
+    pub fn is_debug(&self) -> bool {
+        self.debug != 0
+    }
+
+    #[inline]
+    pub fn path(&self) -> &Path {
+        self.path.as_path()
+    }
+
     pub fn get_target_info(&self) -> SgxResult<sgx_target_info_t> {
         rsgx_get_target_info(self.id)
     }
diff --git a/sgx_urts/src/event.rs b/sgx_urts/src/event.rs
index ac28d26..ff357c7 100644
--- a/sgx_urts/src/event.rs
+++ b/sgx_urts/src/event.rs
@@ -16,12 +16,17 @@
     event: AtomicI32,
 }
 
-impl SeEvent {
-    pub fn new() -> SeEvent {
-        SeEvent {
+impl Default for SeEvent {
+    fn default() -> Self {
+        Self {
             event: AtomicI32::new(0),
         }
     }
+}
+impl SeEvent {
+    pub fn new() -> SeEvent {
+        SeEvent::default()
+    }
 
     pub fn wait_timeout(&self, timeout: &timespec) -> i32 {
         if self.event.fetch_add(-1, Ordering::SeqCst) == 0 {
@@ -85,13 +90,13 @@
 
     pub fn get_event(&self, tcs: usize) -> &SeEvent {
         let v = &mut *self.cache.lock().unwrap();
-        let op = v.as_slice().iter().position(|ref x| x.tcs == tcs);
+        let op = v.as_slice().iter().position(|x| x.tcs == tcs);
         match op {
             Some(i) => v[i].se_event,
             None => {
                 let event: &SeEvent = unsafe { &*Box::into_raw(Box::new(SeEvent::new())) };
                 v.push(SgxTcsInfo {
-                    tcs: tcs,
+                    tcs,
                     se_event: event,
                 });
                 let len = v.len();
diff --git a/sgx_urts/src/file.rs b/sgx_urts/src/file.rs
index 85edd82..1666232 100644
--- a/sgx_urts/src/file.rs
+++ b/sgx_urts/src/file.rs
@@ -338,6 +338,28 @@
 }
 
 #[no_mangle]
+pub extern "C" fn u_linkat_ocall(
+    error: *mut c_int,
+    olddirfd: c_int,
+    oldpath: *const c_char,
+    newdirfd: c_int,
+    newpath: *const c_char,
+    flags: c_int,
+) -> c_int {
+    let mut errno = 0;
+    let ret = unsafe { libc::linkat(olddirfd, oldpath, newdirfd, newpath, flags) };
+    if ret < 0 {
+        errno = Error::last_os_error().raw_os_error().unwrap_or(0);
+    }
+    if !error.is_null() {
+        unsafe {
+            *error = errno;
+        }
+    }
+    ret
+}
+
+#[no_mangle]
 pub extern "C" fn u_rename_ocall(
     error: *mut c_int,
     oldpath: *const c_char,
diff --git a/sgx_urts/src/lib.rs b/sgx_urts/src/lib.rs
index 08616ec..aa8d446 100644
--- a/sgx_urts/src/lib.rs
+++ b/sgx_urts/src/lib.rs
@@ -35,5 +35,6 @@
 pub mod sys;
 pub mod thread;
 pub mod time;
+
 mod enclave;
 pub use enclave::*;
diff --git a/sgx_urts/src/signal.rs b/sgx_urts/src/signal.rs
index 96dd8fe..1535124 100644
--- a/sgx_urts/src/signal.rs
+++ b/sgx_urts/src/signal.rs
@@ -21,6 +21,7 @@
 use std::collections::HashMap;
 use std::io::Error;
 use std::mem;
+use std::ptr;
 use std::sync::{Mutex, Once};
 
 static DISPATCHER_INIT: Once = Once::new();
@@ -53,6 +54,7 @@
         }
     }
 
+    /// # Safety
     pub unsafe fn from_raw_uncheck(signo: i32) -> SigNum {
         SigNum(signo)
     }
@@ -65,14 +67,20 @@
 #[derive(Copy, Clone)]
 pub struct SigSet(sigset_t);
 
-impl SigSet {
-    pub fn new() -> SigSet {
+impl Default for SigSet {
+    fn default() -> Self {
         let set = unsafe {
             let mut set: sigset_t = mem::zeroed();
             libc::sigemptyset(&mut set as *mut sigset_t);
             set
         };
-        SigSet(set)
+        Self(set)
+    }
+}
+
+impl SigSet {
+    pub fn new() -> SigSet {
+        SigSet::default()
     }
 
     pub fn fill(&mut self) {
@@ -81,6 +89,7 @@
         }
     }
 
+    /// # Safety
     pub unsafe fn from_raw(set: sigset_t) -> SigSet {
         SigSet(set)
     }
@@ -139,7 +148,7 @@
         }
         let old = self.signal_set.lock().unwrap().insert(signo, enclave_id);
         unsafe {
-            libc::sigprocmask(SIG_SETMASK, &oldmask.raw(), 0 as *mut sigset_t);
+            libc::sigprocmask(SIG_SETMASK, &oldmask.raw(), ptr::null_mut::<sigset_t>());
         }
         old
     }
@@ -169,7 +178,7 @@
             v != eid
         });
         unsafe {
-            libc::sigprocmask(SIG_SETMASK, &oldmask.raw(), 0 as *mut sigset_t);
+            libc::sigprocmask(SIG_SETMASK, &oldmask.raw(), ptr::null_mut::<sigset_t>());
         }
     }
 
@@ -185,11 +194,8 @@
             None => return -1,
         };
 
-        let result = t_signal_handler_ecall(
-            eid,
-            &mut retval as *mut c_int,
-            info as *const siginfo_t,
-        );
+        let result =
+            t_signal_handler_ecall(eid, &mut retval as *mut c_int, info as *const siginfo_t);
         if result != sgx_status_t::SGX_SUCCESS {
             return -1;
         }
@@ -230,9 +236,8 @@
             .signal_dispatcher
             .register_signal(signo.unwrap(), enclave_id);
 
-        type FnSaSigaction = extern "C" fn(c_int, *const siginfo_t, *const c_void);
         let new_act = sigaction {
-            sa_sigaction: unsafe { mem::transmute::<FnSaSigaction, usize>(handle_signal_entry) },
+            sa_sigaction: handle_signal_entry as usize,
             // Set the flag so that sa_sigaction is registered as the signal handler
             // instead of sa_handler.
             sa_flags: e_act.sa_flags | SA_SIGINFO,
diff --git a/sgx_urts/src/socket.rs b/sgx_urts/src/socket.rs
index d606a1c..8696c48 100644
--- a/sgx_urts/src/socket.rs
+++ b/sgx_urts/src/socket.rs
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License..
 
-use libc::{self, c_int, c_void, msghdr, size_t, sockaddr, socklen_t, ssize_t};
+use libc::{self, c_int, c_void, iovec, msghdr, size_t, sockaddr, socklen_t, ssize_t};
 use std::io::Error;
 
 #[no_mangle]
@@ -209,14 +209,47 @@
 pub extern "C" fn u_recvmsg_ocall(
     error: *mut c_int,
     sockfd: c_int,
-    msg: *mut msghdr,
+    msg_name: *mut c_void,
+    msg_namelen: socklen_t,
+    msg_namelen_out: *mut socklen_t,
+    msg_iov: *mut iovec,
+    msg_iovlen: usize,
+    msg_control: *mut c_void,
+    msg_controllen: usize,
+    msg_controllen_out: *mut usize,
+    msg_flags: *mut c_int,
     flags: c_int,
 ) -> ssize_t {
+    if msg_namelen_out.is_null() || msg_controllen_out.is_null() || msg_flags.is_null() {
+        if !error.is_null() {
+            unsafe {
+                *error = libc::EINVAL;
+            }
+        }
+        return -1;
+    }
+
     let mut errno = 0;
-    let ret = unsafe { libc::recvmsg(sockfd, msg, flags) };
+    let mut msg = msghdr {
+        msg_name,
+        msg_namelen,
+        msg_iov,
+        msg_iovlen,
+        msg_control,
+        msg_controllen,
+        msg_flags: 0,
+    };
+    let ret = unsafe { libc::recvmsg(sockfd, &mut msg, flags) };
     if ret < 0 {
         errno = Error::last_os_error().raw_os_error().unwrap_or(0);
+    } else {
+        unsafe {
+            *msg_namelen_out = msg.msg_namelen;
+            *msg_controllen_out = msg.msg_controllen;
+            *msg_flags = msg.msg_flags;
+        }
     }
+
     if !error.is_null() {
         unsafe {
             *error = errno;
@@ -273,11 +306,25 @@
 pub extern "C" fn u_sendmsg_ocall(
     error: *mut c_int,
     sockfd: c_int,
-    msg: *const msghdr,
+    msg_name: *mut c_void,
+    msg_namelen: socklen_t,
+    msg_iov: *mut iovec,
+    msg_iovlen: usize,
+    msg_control: *mut c_void,
+    msg_controllen: usize,
     flags: c_int,
 ) -> ssize_t {
     let mut errno = 0;
-    let ret = unsafe { libc::sendmsg(sockfd, msg, flags) };
+    let msg = msghdr {
+        msg_name,
+        msg_namelen,
+        msg_iov,
+        msg_iovlen,
+        msg_control,
+        msg_controllen,
+        msg_flags: 0,
+    };
+    let ret = unsafe { libc::sendmsg(sockfd, &msg, flags) };
     if ret < 0 {
         errno = Error::last_os_error().raw_os_error().unwrap_or(0);
     }
diff --git a/sgx_ustdc/file.c b/sgx_ustdc/file.c
index 7e3525f..3303015 100644
--- a/sgx_ustdc/file.c
+++ b/sgx_ustdc/file.c
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <stdio.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -199,6 +200,15 @@
     return ret;
 }
 
+int u_linkat_ocall(int *error, int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
+{
+    int ret = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
+    if (error) {
+        *error = ret == -1 ? errno : 0;
+    }
+    return ret;
+}
+
 int u_rename_ocall(int *error, const char *oldpath, const char *newpath)
 {
     int ret = rename(oldpath, newpath);
diff --git a/xargo/sgx_tstd/build.rs b/xargo/sgx_tstd/build.rs
index a3ef7e2..3b90fc9 100644
--- a/xargo/sgx_tstd/build.rs
+++ b/xargo/sgx_tstd/build.rs
@@ -17,7 +17,6 @@
 
 use std::env;
 use std::path::Path;
-use std::process::Command;
 
 fn main() {
     if cfg!(feature = "backtrace") {
@@ -39,74 +38,4 @@
         println!("cargo:rustc-link-search=native={}/lib64", sdk_dir);
         println!("cargo:rustc-link-lib=static=sgx_pthread");
     }
-
-    // since nightly-2020-11-26 (rustc 2020-11-25), auto_traits replaced
-    // optin_builtin_traits
-    // see https://github.com/rust-lang/rust/commit/810324d1f31eb8d75e8f0044df720652986ef133
-    if let Some(true) = is_min_date("2020-11-25") {
-        println!("cargo:rustc-cfg=enable_auto_traits");
-    }
-
-    // nightly-2021-02-08 (rustc 2021-02-07)
-    // https://github.com/rust-lang/rust/commit/dbdbd30bf2cb0d48c8bbce83c2458592664dbb18
-    if let Some(true) = is_min_date("2021-02-07") {
-        println!("cargo:rustc-cfg=derive_macros");
-    }
-
-    // nightly-2021-03-11 (rustc 2021-03-10)
-    // https://github.com/rust-lang/rust/commit/1ab9fe5d44860050232438967bbbf9bdc35dbde1
-    if let Some(true) = is_min_date("2021-03-10") {
-        println!("cargo:rustc-cfg=enable_prelude_version");
-    }
-}
-
-// code below copied from crate version_check
-// we want to remove the build dependencies to make the dependency tree
-// as clean as possible. the following codes credit to SergioBenitez
-#[derive(Debug, PartialEq, Eq, Copy, Clone, PartialOrd, Ord)]
-struct Date(u32);
-
-impl Date {
-    fn read() -> Option<Date> {
-        get_version_and_date()
-            .and_then(|(_, date)| date)
-            .and_then(|date| Date::parse(&date))
-    }
-
-    fn parse(date: &str) -> Option<Date> {
-        let ymd: Vec<u32> = date.split("-")
-            .filter_map(|s| s.parse::<u32>().ok())
-            .collect();
-    
-        if ymd.len() != 3 {
-            return None
-        }
-    
-        let (y, m, d) = (ymd[0], ymd[1], ymd[2]);
-        Some(Date((y << 9) | ((m & 0xF) << 5) | (d & 0x1F)))
-    }
-}
-
-fn get_version_and_date() -> Option<(Option<String>, Option<String>)> {
-    env::var("RUSTC").ok()
-        .and_then(|rustc| Command::new(rustc).arg("--version").output().ok())
-        .or_else(|| Command::new("rustc").arg("--version").output().ok())
-        .and_then(|output| String::from_utf8(output.stdout).ok())
-        .map(|s| version_and_date_from_rustc_version(&s))
-}
-
-fn version_and_date_from_rustc_version(s: &str) -> (Option<String>, Option<String>) {
-    let last_line = s.lines().last().unwrap_or(s);
-    let mut components = last_line.trim().split(" ");
-    let version = components.nth(1);
-    let date = components.filter(|c| c.ends_with(')')).next()
-        .map(|s| s.trim_end().trim_end_matches(")").trim_start().trim_start_matches('('));
-    (version.map(|s| s.to_string()), date.map(|s| s.to_string()))
-}
-
-fn is_min_date(min_date: &str) -> Option<bool> {
-    match (Date::read(), Date::parse(min_date)) {
-        (Some(rustc_date), Some(min_date)) => Some(rustc_date >= min_date),
-        _ => None
-    }
 }