Merge "libdl.cpp doesn't need <stdbool.h> like libdl.c did."
diff --git a/docs/status.md b/docs/status.md
index 7d2f3b6..d9bd0af 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -9,6 +9,7 @@
 New libc functions in P:
   * `__freading`/`__fwriting` (completing <stdio_ext.h>)
   * `endhostent`/endnetent`/`endprotoent`/`getnetent`/`getprotoent`/`sethostent`/`setnetent`/`setprotoent` (completing <netdb.h>)
+  * `fexecve`
   * `getentropy`/`getrandom` (adding <sys/random.h>)
   * `getlogin_r`
   * `glob`/`globfree` (adding <glob.h>)
@@ -16,6 +17,7 @@
   * `iconv`/`iconv_close`/`iconv_open` (adding <iconv.h>)
   * `pthread_setschedprio`
   * <spawn.h>
+  * `swab`
   * `syncfs`
 
 New libc functions in O:
@@ -71,7 +73,6 @@
 aio_return
 aio_suspend
 aio_write
-fexecve
 lio_listio
 pthread_attr_getinheritsched
 pthread_attr_setinheritsched
@@ -89,7 +90,6 @@
 pthread_setcanceltype
 pthread_testcancel
 sockatmark
-swab
 wordexp
 wordfree
 ```
diff --git a/libc/Android.bp b/libc/Android.bp
index 81135ea..2e5ec00 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1534,6 +1534,7 @@
         "bionic/strings_l.cpp",
         "bionic/strsignal.cpp",
         "bionic/strtold.cpp",
+        "bionic/swab.cpp",
         "bionic/symlink.cpp",
         "bionic/sync_file_range.cpp",
         "bionic/sys_msg.cpp",
diff --git a/libc/bionic/swab.cpp b/libc/bionic/swab.cpp
new file mode 100644
index 0000000..bc53ba4
--- /dev/null
+++ b/libc/bionic/swab.cpp
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * 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.
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+
+void swab(const void* void_src, void* void_dst, ssize_t byte_count) {
+  const uint8_t* src = static_cast<const uint8_t*>(void_src);
+  uint8_t* dst = static_cast<uint8_t*>(void_dst);
+  while (byte_count > 1) {
+    uint8_t x = *src++;
+    uint8_t y = *src++;
+    *dst++ = y;
+    *dst++ = x;
+    byte_count -= 2;
+  }
+}
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index fe98c10..c60cf80 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -247,6 +247,8 @@
 int getdomainname(char* __buf, size_t __buf_size) __INTRODUCED_IN(26);
 int setdomainname(const char* __name, size_t __n) __INTRODUCED_IN(26);
 
+void swab(const void* __src, void* __dst, ssize_t __byte_count) __INTRODUCED_IN_FUTURE;
+
 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
 #include <bits/fortify/unistd.h>
 #endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index af4efb9..6e1015a 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1367,6 +1367,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1287,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 33ecbed..4fc6535 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1392,6 +1392,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 579491a..360af09 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1351,6 +1351,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1287,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 7d1d3ef..04ff514 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1349,6 +1349,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 5c7f726..b88ecd0 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1287,6 +1287,7 @@
     sethostent;
     setnetent;
     setprotoent;
+    swab;
     syncfs;
 } LIBC_O;
 
diff --git a/linker/linker.cpp b/linker/linker.cpp
index f6ca430..ec92c92 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1621,11 +1621,13 @@
   }
 
   // Step 4-3: Add the new global group members to all the linked namespaces
-  for (auto si : new_global_group_members) {
+  if (namespaces != nullptr) {
     for (auto linked_ns : *namespaces) {
-      if (si->get_primary_namespace() != linked_ns) {
-        linked_ns->add_soinfo(si);
-        si->add_secondary_namespace(linked_ns);
+      for (auto si : new_global_group_members) {
+        if (si->get_primary_namespace() != linked_ns) {
+          linked_ns->add_soinfo(si);
+          si->add_secondary_namespace(linked_ns);
+        }
       }
     }
   }
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index adc5ee4..697b84a 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -1496,4 +1496,9 @@
   ASSERT_SUBSTR(expected_dlerror.c_str(), dlerror());
 }
 
+TEST(dlfcn, dlopen_df_1_global) {
+  void* handle = dlopen("libtest_dlopen_df_1_global.so", RTLD_NOW);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+}
+
 #endif
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index 858f2b1..ba0b1aa 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -377,6 +377,26 @@
 }
 
 // -----------------------------------------------------------------------------
+// Library with DF_1_GLOBAL which will be dlopened
+// (note: libdl_test_df_1_global above will be included in DT_NEEDED)
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libtest_dlopen_df_1_global",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["dl_df_1_global_dummy.cpp"],
+    ldflags: ["-Wl,-z,global"],
+
+    target: {
+        host: {
+            // TODO (dimitry): host ld.gold does not yet support -z global
+            // remove this line once it is updated.
+            ldflags: ["-fuse-ld=bfd"],
+        },
+    },
+}
+
+
+// -----------------------------------------------------------------------------
 // Library with weak function
 // -----------------------------------------------------------------------------
 cc_test_library {
diff --git a/tests/libs/dl_df_1_global_dummy.cpp b/tests/libs/dl_df_1_global_dummy.cpp
new file mode 100644
index 0000000..423247c
--- /dev/null
+++ b/tests/libs/dl_df_1_global_dummy.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed 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.
+ */
+
+extern "C" int foo() {
+  return 1;
+}
diff --git a/tests/linux_swab_test.cpp b/tests/linux_swab_test.cpp
index 6b964dc..ffd4072 100644
--- a/tests/linux_swab_test.cpp
+++ b/tests/linux_swab_test.cpp
@@ -21,7 +21,7 @@
 // This test makes sure that references to all of the kernel swab
 // macros/inline functions that are exported work properly.
 // Verifies that any kernel header updates do not break these macros.
-TEST(swab, fswa) {
+TEST(linux_swab, smoke) {
   EXPECT_EQ(0x3412U, __swab16(0x1234));
   EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
   EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index c79ed59..3143c50 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -1443,3 +1443,46 @@
   EXPECT_EQ(0, getlogin_r(buf, sizeof(buf)));
   EXPECT_STREQ(getlogin(), buf);
 }
+
+TEST(UNISTD_TEST, swab) {
+  // POSIX: "The swab() function shall copy nbytes bytes, which are pointed to by src,
+  // to the object pointed to by dest, exchanging adjacent bytes."
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("ehll oowlr\0d", buf, 12);
+  ASSERT_STREQ("hello world", buf);
+}
+
+TEST(UNISTD_TEST, swab_odd_byte_count) {
+  // POSIX: "If nbytes is odd, swab() copies and exchanges nbytes-1 bytes and the disposition
+  // of the last byte is unspecified."
+  // ...but it seems unreasonable to not just leave the last byte alone.
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("012345", buf, 3);
+  ASSERT_EQ('1', buf[0]);
+  ASSERT_EQ('0', buf[1]);
+  ASSERT_EQ('x', buf[2]);
+}
+
+TEST(UNISTD_TEST, swab_overlap) {
+  // POSIX: "If copying takes place between objects that overlap, the behavior is undefined."
+  // ...but it seems unreasonable to not just do the right thing.
+  char buf[] = "012345";
+  swab(buf, buf, 4);
+  ASSERT_EQ('1', buf[0]);
+  ASSERT_EQ('0', buf[1]);
+  ASSERT_EQ('3', buf[2]);
+  ASSERT_EQ('2', buf[3]);
+  ASSERT_EQ('4', buf[4]);
+  ASSERT_EQ('5', buf[5]);
+  ASSERT_EQ(0, buf[6]);
+}
+
+TEST(UNISTD_TEST, swab_negative_byte_count) {
+  // POSIX: "If nbytes is negative, swab() does nothing."
+  char buf[BUFSIZ];
+  memset(buf, 'x', sizeof(buf));
+  swab("hello", buf, -1);
+  ASSERT_EQ('x', buf[0]);
+}