[zircon] [gn] Build libc

This includes the ldmsg, runtime, and sync libraries, which are used
internally by libc.

Bug: BLD-325
Test: gn gen && ninja
Change-Id: Ia4ebdc2e9a9b5f3d290bebc5fa45a1d6029deebf
diff --git a/zircon/BUILD.gn b/zircon/BUILD.gn
index 74418cc..3cf4320 100644
--- a/zircon/BUILD.gn
+++ b/zircon/BUILD.gn
@@ -92,7 +92,8 @@
     environment_label = "$zx/public/gn/toolchain:user"
     cpu = cpu
     deps = [
-      "$zx/system/ulib/zircon",
+      # TODO: This indirectly reaches everything that builds so far.
+      "$zx/system/ulib/c",
     ]
   }
 }
diff --git a/zircon/public/gn/BUILDCONFIG.gn b/zircon/public/gn/BUILDCONFIG.gn
index 7d362ee..857ca83 100644
--- a/zircon/public/gn/BUILDCONFIG.gn
+++ b/zircon/public/gn/BUILDCONFIG.gn
@@ -117,6 +117,26 @@
                              "target_type",
                              "visibility",
                            ])
+
+    if (!defined(no_implicit_deps) || !no_implicit_deps) {
+      # Apply the toolchain's implicit_deps.  Note the logic here should
+      # match the ${toolchain.configs} handling in the set_defaults()
+      # loop at the end of this file.
+      if (!defined(deps)) {
+        deps = []
+      }
+      foreach(dep, toolchain.implicit_deps) {
+        # Either it's an absolute label string or it's a scope with filters
+        # and mutators.  See environment().
+        if (dep == "$dep") {
+          deps += [ dep ]
+        } else if (!defined(dep.types) || dep.types + [ invoker.target_type ] -
+                                          [ invoker.target_type ] != dep.types) {
+          deps += dep.add
+          deps -= dep.remove
+        }
+      }
+    }
   }
 }
 
@@ -307,6 +327,9 @@
 
   # Not all of these will be referenced in all toolchains.
   not_needed(_library_params)
+  if (shared && defined(invoker.no_implicit_deps)) {
+    not_needed(invoker, [ "no_implicit_deps" ])
+  }
 
   # A specialized toolchain might not support shared libraries.
   shared = shared && defined(toolchain.shlib)
@@ -376,6 +399,7 @@
                                [
                                  "configs",
                                  "data_deps",
+                                 "public_configs",
                                  "public_deps",
                                  "testonly",
                                  "visibility",
@@ -403,13 +427,15 @@
                                  "ldflags",
                                  "libs",
                                  "lib_dirs",
+                                 "no_implicit_deps",
+                                 "public_configs",
                                  "testonly",
                                  "visibility",
                                ])
 
         # Everything that depends on the library gets the headers.
         # It also gets the explicit `public_deps`, which includes
-        # any header dependencies or public config()s.
+        # any header dependencies or other targets with public_configs.
         public_deps = [
           ":${_library_name}.headers",
         ]
@@ -418,7 +444,7 @@
         }
 
         # The library depends on the source_set().  It also depends on the
-        # `deps` from the source_set() so as to get any config()
+        # `deps` from the source_set() so as to get any public_configs from
         # dependencies that affect linking rather than just compilation.
         # Other dependencies are redundant since the source_set() already
         # has them, but they don't hurt.
@@ -473,15 +499,20 @@
   }
 
   group("${_library_name}.headers") {
-    public_configs = [ ":_library.config.$_library_name" ]
-
     # The public_deps here represent header dependencies.
+    # Direct use of public_configs should be rare but is sometimes needed.
     forward_variables_from(invoker,
                            [
+                             "public_configs",
                              "public_deps",
                              "testonly",
                              "visibility",
                            ])
+    if (!defined(public_configs)) {
+      public_configs = []
+    }
+    public_configs += [ ":_library.config.$_library_name" ]
+
     if (defined(visibility)) {
       visibility += [ ":$_library_name" ]
       if (!is_kernel) {
@@ -898,21 +929,23 @@
           visibility = invoker.visibility + extra_visibility
         }
 
-        # Apply the toolchain's implicit_deps.  Note the logic here should
-        # match the ${toolchain.configs} handling in the set_defaults()
-        # loop at the end of this file.
-        if (!defined(deps)) {
-          deps = []
-        }
-        foreach(dep, toolchain.implicit_deps) {
-          # Either it's an absolute label string or it's a scope with filters
-          # and mutators.  See environment().
-          if (dep == "$dep") {
-            deps += [ dep ]
-          } else if (!defined(dep.types) || dep.types + [ target.type ] -
-                                            [ target.type ] != dep.types) {
-            deps += dep.add
-            deps -= dep.remove
+        if (!defined(invoker.no_implicit_deps) || !invoker.no_implicit_deps) {
+          # Apply the toolchain's implicit_deps.  Note the logic here should
+          # match the ${toolchain.configs} handling in the set_defaults()
+          # loop at the end of this file.
+          if (!defined(deps)) {
+            deps = []
+          }
+          foreach(dep, toolchain.implicit_deps) {
+            # Either it's an absolute label string or it's a scope with filters
+            # and mutators.  See environment().
+            if (dep == "$dep") {
+              deps += [ dep ]
+            } else if (!defined(dep.types) || dep.types + [ target.type ] -
+                                              [ target.type ] != dep.types) {
+              deps += dep.add
+              deps -= dep.remove
+            }
           }
         }
       }
diff --git a/zircon/public/gn/config/BUILD.gn b/zircon/public/gn/config/BUILD.gn
index 4416ece..04c859f 100644
--- a/zircon/public/gn/config/BUILD.gn
+++ b/zircon/public/gn/config/BUILD.gn
@@ -491,6 +491,7 @@
 
 config("user") {
   defines = [ "_ALL_SOURCE" ]
+  configs = [ "$zx/third_party/ulib/musl:headers" ]
 }
 
 config("static-libc++") {
diff --git a/zircon/public/gn/config/standard.gni b/zircon/public/gn/config/standard.gni
index d0622f0..f0e67c8 100644
--- a/zircon/public/gn/config/standard.gni
+++ b/zircon/public/gn/config/standard.gni
@@ -101,6 +101,17 @@
       ]
     }
     configs = standard_fuchsia_configs + standard_fuchsia_user_configs
+    implicit_deps = [
+      "$zx/system/ulib/c",
+      {
+        types = [
+          "executable",
+          "host_tool",
+          "test",
+        ]
+        add = [ "$zx/system/ulib/c:crt1" ]
+      },
+    ]
     shlib = true
     strip = "--strip-sections"
   },
diff --git a/zircon/system/ulib/c/BUILD.gn b/zircon/system/ulib/c/BUILD.gn
new file mode 100644
index 0000000..7f120a4
--- /dev/null
+++ b/zircon/system/ulib/c/BUILD.gn
@@ -0,0 +1,32 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+library("c") {
+  sdk = "shared"
+  sdk_headers = []  # TODO
+  shared = true
+  static = false
+
+  # At link time and in DT_SONAME, musl is known as libc.so.  But the
+  # (only) place it needs to be installed at runtime is where the
+  # PT_INTERP strings embedded in executables point, which is ld.so.1.
+  install_path = "lib/${toolchain.libprefix}ld.so.1"
+
+  public_configs = [ "$zx/third_party/ulib/musl:headers" ]
+
+  # The code comes from musl, where a source_set is defined.
+  sources = []
+  deps = [
+    "$zx/third_party/ulib/musl",
+  ]
+
+  # Suppress the ${toolchain.implicit_deps} that points back to here.
+  no_implicit_deps = true
+}
+
+group("crt1") {
+  deps = [
+    "$zx/third_party/ulib/musl:crt1",
+  ]
+}
diff --git a/zircon/system/ulib/ldmsg/BUILD.gn b/zircon/system/ulib/ldmsg/BUILD.gn
new file mode 100644
index 0000000..e750900
--- /dev/null
+++ b/zircon/system/ulib/ldmsg/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+library("ldmsg") {
+  sdk = "static"
+  sdk_headers = [ "ldmsg/ldmsg.h" ]
+  sources = [
+    "ldmsg.c",
+  ]
+  deps = [
+    "$zx/system/ulib/zircon",
+  ]
+
+  if (toolchain.environment == "user") {
+    # This library is used by libc, so suppress the circularity.
+    configs -= [ "$zx/public/gn/config:user" ]
+  }
+
+  # However, it uses <string.h> for memcpy.
+  deps += [ "$zx/system/ulib/c:headers" ]
+}
diff --git a/zircon/system/ulib/runtime/BUILD.gn b/zircon/system/ulib/runtime/BUILD.gn
new file mode 100644
index 0000000..2b9997e
--- /dev/null
+++ b/zircon/system/ulib/runtime/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+library("runtime") {
+  sources = [
+    "message.c",
+    "processargs.c",
+    "thread.c",
+  ]
+
+  deps = [
+    "$zx/system/ulib/zircon",
+  ]
+
+  configs += [
+    "$zx/public/gn/config:visibility_hidden",
+
+    # This code is used in early startup, where safe-stack is not ready yet.
+    "$zx/public/gn/config:no_sanitizers",
+  ]
+}
diff --git a/zircon/system/ulib/runtime/processargs.c b/zircon/system/ulib/runtime/processargs.c
index 17c505f..d2a8376 100644
--- a/zircon/system/ulib/runtime/processargs.c
+++ b/zircon/system/ulib/runtime/processargs.c
@@ -3,9 +3,7 @@
 // found in the LICENSE file.
 
 #include <runtime/processargs.h>
-
 #include <zircon/syscalls.h>
-#include <string.h>
 
 // TODO(mcgrathr): Is there a better error code to use for marshalling
 // protocol violations?
diff --git a/zircon/system/ulib/sync/BUILD.gn b/zircon/system/ulib/sync/BUILD.gn
new file mode 100644
index 0000000..b6fbfe5
--- /dev/null
+++ b/zircon/system/ulib/sync/BUILD.gn
@@ -0,0 +1,29 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+library("sync") {
+  sdk = "static"
+  sdk_headers = [
+    "lib/sync/internal/condition-template.h",
+    "lib/sync/completion.h",
+    "lib/sync/condition.h",
+    "lib/sync/mutex.h",
+  ]
+  sources = [
+    "completion.c",
+    "condition.cpp",
+    "mutex.c",
+  ]
+  configs += [ "$zx/public/gn/config:visibility_hidden" ]
+  deps = [
+    "$zx/system/ulib/zircon",
+    "$zx/system/ulib/zircon-internal",
+  ]
+
+  if (toolchain.environment == "user") {
+    # This code is used in early startup, where safe-stack setup is not
+    # ready yet.
+    configs += [ "$zx/public/gn/config:no_sanitizers" ]
+  }
+}
diff --git a/zircon/system/ulib/zircon/BUILD.gn b/zircon/system/ulib/zircon/BUILD.gn
index 252c441..f176066a 100644
--- a/zircon/system/ulib/zircon/BUILD.gn
+++ b/zircon/system/ulib/zircon/BUILD.gn
@@ -73,14 +73,8 @@
     # Note, "./" makes GN realize this is a file rather than a -l switch.
     libs = [ "./alternates.ld" ]
 
-    if (toolchain.environment == "user") {
-      # This doesn't get normal default deps on libc.
-      configs -= [ "$zx/public/gn/config:user" ]
-      if (is_gcc) {
-        # However, GCC's <x86intrin.h> needs <stdlib.h> indirectly.
-        configs += [ "$zx/system/ulib/c:headers" ]
-      }
-    }
+    # This doesn't get normal default deps on libc.
+    no_implicit_deps = true
   }
 
   # Everything with ":config" in configs needs ":generate" in deps.
diff --git a/zircon/third_party/ulib/jemalloc/BUILD.gn b/zircon/third_party/ulib/jemalloc/BUILD.gn
new file mode 100644
index 0000000..9c9dcc2
--- /dev/null
+++ b/zircon/third_party/ulib/jemalloc/BUILD.gn
@@ -0,0 +1,32 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("jemalloc") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  include_dirs = [ "include" ]
+  sources = [
+    "src/arena.c",
+    "src/base.c",
+    "src/bitmap.c",
+    "src/ckh.c",
+    "src/ctl.c",
+    "src/extent.c",
+    "src/extent_dss.c",
+    "src/extent_mmap.c",
+    "src/jemalloc.c",
+    "src/large.c",
+    "src/mutex.c",
+    "src/nstime.c",
+    "src/pages.c",
+    "src/prof.c",
+    "src/rtree.c",
+    "src/stats.c",
+    "src/tcache.c",
+    "src/tsd.c",
+    "src/util.c",
+    "src/witness.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/BUILD.gn b/zircon/third_party/ulib/musl/BUILD.gn
new file mode 100644
index 0000000..d53a224
--- /dev/null
+++ b/zircon/third_party/ulib/musl/BUILD.gn
@@ -0,0 +1,111 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+config("headers") {
+  visibility = [
+    "./*",
+    "$zx/system/ulib/c:*",
+  ]
+  include_dirs = [ "include" ]
+}
+
+group("musl_internal") {
+  visibility = [
+    "./*",
+    "$zx/third_party/ulib/jemalloc:*",
+  ]
+  public_deps = [
+    "$zx/system/ulib/runtime",
+    "$zx/system/ulib/zircon",
+  ]
+  public_configs = [
+    ":headers",
+    ":musl_internal.config",
+  ]
+}
+
+config("musl_internal.config") {
+  visibility = [ ":musl_internal" ]
+
+  include_dirs = [
+    "src/internal",
+    "arch/${toolchain.cpu}",
+  ]
+
+  defines = [ "_XOPEN_SOURCE=700" ]
+
+  # TODO(kulakowski): Clean up the junkier -Wno flags below.
+  cflags = [
+    "-Wno-sign-compare",
+    "-Werror=incompatible-pointer-types",
+    "-Wno-implicit-fallthrough",
+  ]
+
+  if (!is_gcc) {
+    # Some of this code runs before the TLS area for the canary is set up.
+    # TODO(mcgrathr): Isolate this to exactly what code needs it.
+    cflags += [ "-fno-stack-protector" ]
+  }
+
+  cflags += [ "-ffreestanding" ]
+}
+
+if (toolchain.environment == "user") {
+  group("musl") {
+    visibility = [ "$zx/system/ulib/c:*" ]
+    deps = [
+      "$zx/third_party/ulib/jemalloc",
+      "ldso",
+      "pthread",
+      "sanitizers",
+      "src/complex",
+      "src/conf",
+      "src/ctype",
+      "src/dirent",
+      "src/env",
+      "src/errno",
+      "src/exit",
+      "src/fcntl",
+      "src/fenv",
+      "src/internal",
+      "src/ipc",
+      "src/ldso",
+      "src/legacy",
+      "src/linux",
+      "src/locale",
+      "src/math",
+      "src/misc",
+      "src/mman",
+      "src/multibyte",
+      "src/network",
+      "src/passwd",
+      "src/prng",
+      "src/process",
+      "src/regex",
+      "src/sched",
+      "src/setjmp",
+      "src/signal",
+      "src/stat",
+      "src/stdio",
+      "src/stdlib",
+      "src/string",
+      "src/temp",
+      "src/termios",
+      "src/thread",
+      "src/time",
+      "src/unistd",
+      "stubs",
+      "third_party/smoothsort",
+      "third_party/tre",
+      "zircon",
+    ]
+  }
+
+  source_set("crt1") {
+    visibility = [ "$zx/system/ulib/c:crt1" ]
+    sources = [
+      "arch/${toolchain.cpu}/Scrt1.S",
+    ]
+  }
+}
diff --git a/zircon/third_party/ulib/musl/ldso/BUILD.gn b/zircon/third_party/ulib/musl/ldso/BUILD.gn
new file mode 100644
index 0000000..71b4c4da
--- /dev/null
+++ b/zircon/third_party/ulib/musl/ldso/BUILD.gn
@@ -0,0 +1,16 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("ldso") {
+  deps = [
+    "$zx/system/ulib/ldmsg",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "$zx/third_party/ulib/musl/arch/${toolchain.cpu}/dl-entry.S",
+    "dlstart.c",
+    "dynlink-sancov.S",
+    "dynlink.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/pthread/BUILD.gn b/zircon/third_party/ulib/musl/pthread/BUILD.gn
new file mode 100644
index 0000000..82f2e91
--- /dev/null
+++ b/zircon/third_party/ulib/musl/pthread/BUILD.gn
@@ -0,0 +1,90 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("pthread") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "pthread_atfork.c",
+    "pthread_attr_destroy.c",
+    "pthread_attr_get.c",
+    "pthread_attr_init.c",
+    "pthread_attr_setdetachstate.c",
+    "pthread_attr_setguardsize.c",
+    "pthread_attr_setschedparam.c",
+    "pthread_attr_setstacksize.c",
+    "pthread_barrier_destroy.c",
+    "pthread_barrier_init.c",
+    "pthread_barrier_wait.c",
+    "pthread_barrierattr_destroy.c",
+    "pthread_barrierattr_init.c",
+    "pthread_cancel.c",
+    "pthread_cond_broadcast.c",
+    "pthread_cond_destroy.c",
+    "pthread_cond_init.c",
+    "pthread_cond_signal.c",
+    "pthread_cond_timedwait.c",
+    "pthread_cond_wait.c",
+    "pthread_condattr_destroy.c",
+    "pthread_condattr_init.c",
+    "pthread_condattr_setclock.c",
+    "pthread_create.c",
+    "pthread_detach.c",
+    "pthread_equal.c",
+    "pthread_getattr_np.c",
+    "pthread_getconcurrency.c",
+    "pthread_getcpuclockid.c",
+    "pthread_getspecific.c",
+    "pthread_join.c",
+    "pthread_key.c",
+    "pthread_kill.c",
+    "pthread_mutex_consistent.c",
+    "pthread_mutex_destroy.c",
+    "pthread_mutex_getprioceiling.c",
+    "pthread_mutex_init.c",
+    "pthread_mutex_lock.c",
+    "pthread_mutex_setprioceiling.c",
+    "pthread_mutex_timedlock.c",
+    "pthread_mutex_trylock.c",
+    "pthread_mutex_unlock.c",
+    "pthread_mutexattr_destroy.c",
+    "pthread_mutexattr_init.c",
+    "pthread_mutexattr_setprotocol.c",
+    "pthread_mutexattr_setrobust.c",
+    "pthread_mutexattr_settype.c",
+    "pthread_once.c",
+    "pthread_rwlock_destroy.c",
+    "pthread_rwlock_init.c",
+    "pthread_rwlock_rdlock.c",
+    "pthread_rwlock_timedrdlock.c",
+    "pthread_rwlock_timedwrlock.c",
+    "pthread_rwlock_tryrdlock.c",
+    "pthread_rwlock_trywrlock.c",
+    "pthread_rwlock_unlock.c",
+    "pthread_rwlock_wrlock.c",
+    "pthread_rwlockattr_destroy.c",
+    "pthread_rwlockattr_init.c",
+    "pthread_self.c",
+    "pthread_setcancelstate.c",
+    "pthread_setcanceltype.c",
+    "pthread_setconcurrency.c",
+    "pthread_setspecific.c",
+    "pthread_sigmask.c",
+    "pthread_spin_destroy.c",
+    "pthread_spin_init.c",
+    "pthread_spin_lock.c",
+    "pthread_spin_trylock.c",
+    "pthread_spin_unlock.c",
+    "pthread_testcancel.c",
+    "sem_destroy.c",
+    "sem_getvalue.c",
+    "sem_init.c",
+    "sem_post.c",
+    "sem_timedwait.c",
+    "sem_trywait.c",
+    "sem_unlink.c",
+    "sem_wait.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/sanitizers/BUILD.gn b/zircon/third_party/ulib/musl/sanitizers/BUILD.gn
new file mode 100644
index 0000000..7973bb6
--- /dev/null
+++ b/zircon/third_party/ulib/musl/sanitizers/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("sanitizers") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__asan_early_init.c",
+    "asan-stubs.c",
+    "hooks.c",
+    "log.c",
+  ]
+
+  # There is no '#if __has_feature(coverage)', so this file has to be
+  # excluded from the build entirely when not in use.
+  sancov = [ "$zx/public/gn/config/sanitizers:sancov" ]
+  if (configs + sancov - sancov != configs) {
+    sources += [ "sancov-stubs.S" ]
+  }
+}
diff --git a/zircon/third_party/ulib/musl/src/complex/BUILD.gn b/zircon/third_party/ulib/musl/src/complex/BUILD.gn
new file mode 100644
index 0000000..ccb9a4e
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/complex/BUILD.gn
@@ -0,0 +1,66 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("complex") {
+  include_dirs = [ "$zx/third_party/ulib/musl/third_party/include" ]
+  sources = [
+    "cabs.c",
+    "cabsf.c",
+    "cabsl.c",
+    "cacos.c",
+    "cacosf.c",
+    "cacosh.c",
+    "cacoshf.c",
+    "cacoshl.c",
+    "cacosl.c",
+    "carg.c",
+    "cargf.c",
+    "cargl.c",
+    "casin.c",
+    "casinf.c",
+    "casinh.c",
+    "casinhf.c",
+    "casinhl.c",
+    "casinl.c",
+    "catanh.c",
+    "catanhf.c",
+    "catanhl.c",
+    "ccos.c",
+    "ccosf.c",
+    "ccoshl.c",
+    "ccosl.c",
+    "cexpl.c",
+    "cimag.c",
+    "cimagf.c",
+    "cimagl.c",
+    "clog.c",
+    "clogf.c",
+    "clogl.c",
+    "conj.c",
+    "conjf.c",
+    "conjl.c",
+    "cpow.c",
+    "cpowf.c",
+    "cpowl.c",
+    "cproj.c",
+    "cprojf.c",
+    "cprojl.c",
+    "creal.c",
+    "crealf.c",
+    "creall.c",
+    "csin.c",
+    "csinf.c",
+    "csinhl.c",
+    "csinl.c",
+    "csqrtl.c",
+    "ctan.c",
+    "ctanf.c",
+    "ctanhl.c",
+    "ctanl.c",
+  ]
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+    "$zx/third_party/ulib/musl/third_party/complex",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/conf/BUILD.gn b/zircon/third_party/ulib/musl/src/conf/BUILD.gn
new file mode 100644
index 0000000..463835b
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/conf/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("conf") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "confstr.c",
+    "fpathconf.c",
+    "pathconf.c",
+    "sysconf.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/ctype/BUILD.gn b/zircon/third_party/ulib/musl/src/ctype/BUILD.gn
new file mode 100644
index 0000000..3037175
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/ctype/BUILD.gn
@@ -0,0 +1,48 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("ctype") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__ctype_b_loc.c",
+    "__ctype_get_mb_cur_max.c",
+    "__ctype_tolower_loc.c",
+    "__ctype_toupper_loc.c",
+    "isalnum.c",
+    "isalpha.c",
+    "isascii.c",
+    "isblank.c",
+    "iscntrl.c",
+    "isdigit.c",
+    "isgraph.c",
+    "islower.c",
+    "isprint.c",
+    "ispunct.c",
+    "isspace.c",
+    "isupper.c",
+    "iswalnum.c",
+    "iswalpha.c",
+    "iswblank.c",
+    "iswcntrl.c",
+    "iswctype.c",
+    "iswdigit.c",
+    "iswgraph.c",
+    "iswlower.c",
+    "iswprint.c",
+    "iswpunct.c",
+    "iswspace.c",
+    "iswupper.c",
+    "iswxdigit.c",
+    "isxdigit.c",
+    "toascii.c",
+    "tolower.c",
+    "toupper.c",
+    "towctrans.c",
+    "wcswidth.c",
+    "wctrans.c",
+    "wcwidth.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/dirent/BUILD.gn b/zircon/third_party/ulib/musl/src/dirent/BUILD.gn
new file mode 100644
index 0000000..b567273
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/dirent/BUILD.gn
@@ -0,0 +1,14 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("dirent") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "alphasort.c",
+    "scandir.c",
+    "versionsort.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/env/BUILD.gn b/zircon/third_party/ulib/musl/src/env/BUILD.gn
new file mode 100644
index 0000000..12d31f1
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/env/BUILD.gn
@@ -0,0 +1,19 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("env") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__environ.c",
+    "__libc_start_main.c",
+    "__stack_chk_fail.c",
+    "clearenv.c",
+    "getenv.c",
+    "putenv.c",
+    "setenv.c",
+    "unsetenv.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/errno/BUILD.gn b/zircon/third_party/ulib/musl/src/errno/BUILD.gn
new file mode 100644
index 0000000..10fff77
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/errno/BUILD.gn
@@ -0,0 +1,13 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("errno") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__errno_location.c",
+    "strerror.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/exit/BUILD.gn b/zircon/third_party/ulib/musl/src/exit/BUILD.gn
new file mode 100644
index 0000000..36287da
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/exit/BUILD.gn
@@ -0,0 +1,20 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("exit") {
+  sources = [
+    "_Exit.c",
+    "__cxa_thread_atexit.c",
+    "abort.c",
+    "assert.c",
+    "at_quick_exit.c",
+    "atexit.c",
+    "exit.c",
+    "quick_exit.c",
+  ]
+  deps = [
+    "$zx/system/ulib/zircon",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/exit/at_quick_exit.c b/zircon/third_party/ulib/musl/src/exit/at_quick_exit.c
index c4b2013..0c0c64e 100644
--- a/zircon/third_party/ulib/musl/src/exit/at_quick_exit.c
+++ b/zircon/third_party/ulib/musl/src/exit/at_quick_exit.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include "libc.h"
 #include <stdlib.h>
 #include <threads.h>
diff --git a/zircon/third_party/ulib/musl/src/exit/atexit.c b/zircon/third_party/ulib/musl/src/exit/atexit.c
index 90a11c1..637b3b6 100644
--- a/zircon/third_party/ulib/musl/src/exit/atexit.c
+++ b/zircon/third_party/ulib/musl/src/exit/atexit.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include "libc.h"
 #include <stdint.h>
 #include <stdlib.h>
diff --git a/zircon/third_party/ulib/musl/src/fcntl/BUILD.gn b/zircon/third_party/ulib/musl/src/fcntl/BUILD.gn
new file mode 100644
index 0000000..a70073e
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/fcntl/BUILD.gn
@@ -0,0 +1,12 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("fcntl") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "creat.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/fenv/BUILD.gn b/zircon/third_party/ulib/musl/src/fenv/BUILD.gn
new file mode 100644
index 0000000..69d2e00
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/fenv/BUILD.gn
@@ -0,0 +1,18 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("fenv") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "${toolchain.cpu}/fenv.c",
+    "__flt_rounds.c",
+    "fegetexceptflag.c",
+    "feholdexcept.c",
+    "fesetexceptflag.c",
+    "fesetround.c",
+    "feupdateenv.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/internal/BUILD.gn b/zircon/third_party/ulib/musl/src/internal/BUILD.gn
new file mode 100644
index 0000000..a5930e6
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/internal/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("internal") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "floatscan.c",
+    "intscan.c",
+    "libc.c",
+    "shgetc.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/ipc/BUILD.gn b/zircon/third_party/ulib/musl/src/ipc/BUILD.gn
new file mode 100644
index 0000000..28744a80
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/ipc/BUILD.gn
@@ -0,0 +1,24 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("ipc") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "ftok.c",
+    "msgctl.c",
+    "msgget.c",
+    "msgrcv.c",
+    "msgsnd.c",
+    "semctl.c",
+    "semget.c",
+    "semop.c",
+    "semtimedop.c",
+    "shmat.c",
+    "shmctl.c",
+    "shmdt.c",
+    "shmget.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/ldso/BUILD.gn b/zircon/third_party/ulib/musl/src/ldso/BUILD.gn
new file mode 100644
index 0000000..5e10435
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/ldso/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("ldso") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "${toolchain.cpu}/tlsdesc.S",
+    "dlclose.c",
+    "dlerror.c",
+    "dlinfo.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/legacy/BUILD.gn b/zircon/third_party/ulib/musl/src/legacy/BUILD.gn
new file mode 100644
index 0000000..e4da659
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/legacy/BUILD.gn
@@ -0,0 +1,19 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("legacy") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "err.c",
+    "euidaccess.c",
+    "futimes.c",
+    "getdtablesize.c",
+    "getpagesize.c",
+    "getpass.c",
+    "isastream.c",
+    "lutimes.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/linux/BUILD.gn b/zircon/third_party/ulib/musl/src/linux/BUILD.gn
new file mode 100644
index 0000000..b5c8e669
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/linux/BUILD.gn
@@ -0,0 +1,17 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("linux") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "adjtime.c",
+    "flock.c",
+    "sethostname.c",
+    "settimeofday.c",
+    "stime.c",
+    "utimes.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/locale/BUILD.gn b/zircon/third_party/ulib/musl/src/locale/BUILD.gn
new file mode 100644
index 0000000..356018f
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/locale/BUILD.gn
@@ -0,0 +1,32 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("locale") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__lctrans.c",
+    "__mo_lookup.c",
+    "c_locale.c",
+    "catclose.c",
+    "catgets.c",
+    "catopen.c",
+    "duplocale.c",
+    "freelocale.c",
+    "iconv.c",
+    "langinfo.c",
+    "locale_map.c",
+    "localeconv.c",
+    "newlocale.c",
+    "pleval.c",
+    "setlocale.c",
+    "strcoll.c",
+    "strfmon.c",
+    "strxfrm.c",
+    "uselocale.c",
+    "wcscoll.c",
+    "wcsxfrm.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/locale/setlocale.c b/zircon/third_party/ulib/musl/src/locale/setlocale.c
index beb4817..1ee43b5 100644
--- a/zircon/third_party/ulib/musl/src/locale/setlocale.c
+++ b/zircon/third_party/ulib/musl/src/locale/setlocale.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include "atomic.h"
 #include "libc.h"
 #include "locale_impl.h"
diff --git a/zircon/third_party/ulib/musl/src/math/BUILD.gn b/zircon/third_party/ulib/musl/src/math/BUILD.gn
new file mode 100644
index 0000000..4d04b15
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/math/BUILD.gn
@@ -0,0 +1,187 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("math") {
+  include_dirs = [ "$zx/third_party/ulib/musl/third_party/include" ]
+  sources = [
+    "__expo2.c",
+    "__expo2f.c",
+    "__fpclassify.c",
+    "__fpclassifyf.c",
+    "__fpclassifyl.c",
+    "__invtrigl.c",
+    "__signbit.c",
+    "__signbitf.c",
+    "__signbitl.c",
+    "acosh.c",
+    "acoshf.c",
+    "acoshl.c",
+    "asinh.c",
+    "asinhf.c",
+    "asinhl.c",
+    "atanh.c",
+    "atanhf.c",
+    "atanhl.c",
+    "ceil.c",
+    "ceilf.c",
+    "copysign.c",
+    "copysignf.c",
+    "copysignl.c",
+    "cosh.c",
+    "coshf.c",
+    "coshl.c",
+    "cosl.c",
+    "exp10.c",
+    "exp10f.c",
+    "exp10l.c",
+    "fdim.c",
+    "fdimf.c",
+    "fdiml.c",
+    "finite.c",
+    "finitef.c",
+    "floor.c",
+    "floorf.c",
+    "fmax.c",
+    "fmaxf.c",
+    "fmaxl.c",
+    "fmin.c",
+    "fminf.c",
+    "fminl.c",
+    "fmod.c",
+    "fmodf.c",
+    "frexp.c",
+    "frexpf.c",
+    "frexpl.c",
+    "hypot.c",
+    "hypotf.c",
+    "hypotl.c",
+    "ilogb.c",
+    "ilogbf.c",
+    "ilogbl.c",
+    "ldexp.c",
+    "ldexpf.c",
+    "ldexpl.c",
+    "lgamma.c",
+    "lgammaf.c",
+    "llround.c",
+    "llroundf.c",
+    "llroundl.c",
+    "logb.c",
+    "logbf.c",
+    "logbl.c",
+    "lround.c",
+    "lroundf.c",
+    "lroundl.c",
+    "modf.c",
+    "modff.c",
+    "modfl.c",
+    "nan.c",
+    "nanf.c",
+    "nanl.c",
+    "nearbyint.c",
+    "nearbyintf.c",
+    "nearbyintl.c",
+    "nextafter.c",
+    "nextafterf.c",
+    "nextafterl.c",
+    "nexttoward.c",
+    "nexttowardf.c",
+    "nexttowardl.c",
+    "remainder.c",
+    "remainderf.c",
+    "remquo.c",
+    "remquof.c",
+    "remquol.c",
+    "rint.c",
+    "rintf.c",
+    "round.c",
+    "roundf.c",
+    "roundl.c",
+    "scalbln.c",
+    "scalblnf.c",
+    "scalblnl.c",
+    "scalbn.c",
+    "scalbnf.c",
+    "scalbnl.c",
+    "signgam.c",
+    "significand.c",
+    "significandf.c",
+    "sincosl.c",
+    "sinh.c",
+    "sinhf.c",
+    "sinhl.c",
+    "sinl.c",
+    "tanh.c",
+    "tanhf.c",
+    "tanhl.c",
+    "tanl.c",
+    "tgamma.c",
+    "tgammaf.c",
+    "trunc.c",
+    "truncf.c",
+  ]
+  if (current_cpu == "x64") {
+    sources += [
+      "x86_64/__invtrigl.S",
+      "x86_64/acosl.S",
+      "x86_64/asinl.S",
+      "x86_64/atan2l.S",
+      "x86_64/atanl.S",
+      "x86_64/ceill.S",
+      "x86_64/exp2l.S",
+      "x86_64/expl.S",
+      "x86_64/expm1l.S",
+      "x86_64/fabs.S",
+      "x86_64/fabsf.S",
+      "x86_64/fabsl.S",
+      "x86_64/floorl.S",
+      "x86_64/fmodl.S",
+      "x86_64/llrint.S",
+      "x86_64/llrintf.S",
+      "x86_64/llrintl.S",
+      "x86_64/log10l.S",
+      "x86_64/log1pl.S",
+      "x86_64/log2l.S",
+      "x86_64/logl.S",
+      "x86_64/lrint.S",
+      "x86_64/lrintf.S",
+      "x86_64/lrintl.S",
+      "x86_64/remainderl.S",
+      "x86_64/rintl.S",
+      "x86_64/sqrt.S",
+      "x86_64/sqrtf.S",
+      "x86_64/sqrtl.S",
+      "x86_64/truncl.S",
+    ]
+  } else {
+    sources += [
+      "ceill.c",
+      "fabsl.c",
+      "floorl.c",
+      "fmodl.c",
+      "llrint.c",
+      "llrintf.c",
+      "llrintl.c",
+      "lrint.c",
+      "lrintf.c",
+      "lrintl.c",
+      "remainderl.c",
+      "rintl.c",
+      "sqrtl.c",
+      "truncl.c",
+    ]
+  }
+  if (current_cpu == "arm64") {
+    sources += [
+      "aarch64/fabs.S",
+      "aarch64/fabsf.S",
+      "aarch64/sqrt.S",
+      "aarch64/sqrtf.S",
+    ]
+  }
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+    "$zx/third_party/ulib/musl/third_party/math",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/misc/BUILD.gn b/zircon/third_party/ulib/musl/src/misc/BUILD.gn
new file mode 100644
index 0000000..ebf89d16
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/misc/BUILD.gn
@@ -0,0 +1,33 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("misc") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "a64l.c",
+    "basename.c",
+    "dirname.c",
+    "ffs.c",
+    "ffsl.c",
+    "ffsll.c",
+    "get_current_dir_name.c",
+    "getauxval.c",
+    "getdomainname.c",
+    "gethostid.c",
+    "getopt.c",
+    "getopt_long.c",
+    "getsubopt.c",
+    "initgroups.c",
+    "issetugid.c",
+    "lockf.c",
+    "openpty.c",
+    "ptsname.c",
+    "pty.c",
+    "setdomainname.c",
+    "syslog.c",
+    "wordexp.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/misc/syslog.c b/zircon/third_party/ulib/musl/src/misc/syslog.c
index e10c126..2dd3366 100644
--- a/zircon/third_party/ulib/musl/src/misc/syslog.c
+++ b/zircon/third_party/ulib/musl/src/misc/syslog.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include "libc.h"
 #include <errno.h>
 #include <fcntl.h>
diff --git a/zircon/third_party/ulib/musl/src/mman/BUILD.gn b/zircon/third_party/ulib/musl/src/mman/BUILD.gn
new file mode 100644
index 0000000..fc2752d
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/mman/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("mman") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "madvise.c",
+    "mlock.c",
+    "mlockall.c",
+    "mmap.c",
+    "mprotect.c",
+    "msync.c",
+    "munlock.c",
+    "munlockall.c",
+    "munmap.c",
+    "posix_madvise.c",
+    "shm_open.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/multibyte/BUILD.gn b/zircon/third_party/ulib/musl/src/multibyte/BUILD.gn
new file mode 100644
index 0000000..25dbd1e
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/multibyte/BUILD.gn
@@ -0,0 +1,31 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("multibyte") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "btowc.c",
+    "c16rtomb.c",
+    "c32rtomb.c",
+    "internal.c",
+    "mblen.c",
+    "mbrlen.c",
+    "mbrtoc16.c",
+    "mbrtoc32.c",
+    "mbrtowc.c",
+    "mbsinit.c",
+    "mbsnrtowcs.c",
+    "mbsrtowcs.c",
+    "mbstowcs.c",
+    "mbtowc.c",
+    "wcrtomb.c",
+    "wcsnrtombs.c",
+    "wcsrtombs.c",
+    "wcstombs.c",
+    "wctob.c",
+    "wctomb.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/network/BUILD.gn b/zircon/third_party/ulib/musl/src/network/BUILD.gn
new file mode 100644
index 0000000..b9cf908
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/network/BUILD.gn
@@ -0,0 +1,67 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("network") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "accept.c",
+    "dn_comp.c",
+    "dn_expand.c",
+    "dn_skipname.c",
+    "dns_parse.c",
+    "ent.c",
+    "ether.c",
+    "gai_strerror.c",
+    "gethostbyaddr.c",
+    "gethostbyaddr_r.c",
+    "gethostbyname.c",
+    "gethostbyname2.c",
+    "gethostbyname2_r.c",
+    "gethostbyname_r.c",
+    "getifaddrs.c",
+    "getnameinfo.c",
+    "getservbyname.c",
+    "getservbyname_r.c",
+    "getservbyport.c",
+    "getservbyport_r.c",
+    "h_errno.c",
+    "herror.c",
+    "hstrerror.c",
+    "htonl.c",
+    "htons.c",
+    "if_freenameindex.c",
+    "if_indextoname.c",
+    "if_nameindex.c",
+    "if_nametoindex.c",
+    "in6addr_any.c",
+    "in6addr_loopback.c",
+    "inet_addr.c",
+    "inet_aton.c",
+    "inet_legacy.c",
+    "inet_ntoa.c",
+    "inet_ntop.c",
+    "inet_pton.c",
+    "lookup_ipliteral.c",
+    "lookup_serv.c",
+    "netlink.c",
+    "netname.c",
+    "ns_parse.c",
+    "ntohl.c",
+    "ntohs.c",
+    "proto.c",
+    "recv.c",
+    "res_init.c",
+    "res_mkquery.c",
+    "res_msend.c",
+    "res_query.c",
+    "res_querydomain.c",
+    "res_send.c",
+    "res_state.c",
+    "resolvconf.c",
+    "send.c",
+    "serv.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/passwd/BUILD.gn b/zircon/third_party/ulib/musl/src/passwd/BUILD.gn
new file mode 100644
index 0000000..dc88650
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/passwd/BUILD.gn
@@ -0,0 +1,25 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("passwd") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "fgetgrent.c",
+    "fgetpwent.c",
+    "getgr_a.c",
+    "getgr_r.c",
+    "getgrent.c",
+    "getgrent_a.c",
+    "getgrouplist.c",
+    "getpw_a.c",
+    "getpw_r.c",
+    "getpwent.c",
+    "getpwent_a.c",
+    "nscd_query.c",
+    "putgrent.c",
+    "putpwent.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/prng/BUILD.gn b/zircon/third_party/ulib/musl/src/prng/BUILD.gn
new file mode 100644
index 0000000..ce6cb0a
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/prng/BUILD.gn
@@ -0,0 +1,22 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("prng") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__rand48_step.c",
+    "__seed48.c",
+    "drand48.c",
+    "lcong48.c",
+    "lrand48.c",
+    "mrand48.c",
+    "rand.c",
+    "rand_r.c",
+    "random.c",
+    "seed48.c",
+    "srand48.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/prng/random.c b/zircon/third_party/ulib/musl/src/prng/random.c
index af9f1be..fe59de3 100644
--- a/zircon/third_party/ulib/musl/src/prng/random.c
+++ b/zircon/third_party/ulib/musl/src/prng/random.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include <stdlib.h>
 
 #include <limits.h>
diff --git a/zircon/third_party/ulib/musl/src/process/BUILD.gn b/zircon/third_party/ulib/musl/src/process/BUILD.gn
new file mode 100644
index 0000000..b387dfa
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/process/BUILD.gn
@@ -0,0 +1,41 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("process") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "execl.c",
+    "execle.c",
+    "execlp.c",
+    "execv.c",
+    "execve.c",
+    "execvp.c",
+    "fexecve.c",
+    "fork.c",
+    "posix_spawn.c",
+    "posix_spawn_file_actions_addclose.c",
+    "posix_spawn_file_actions_adddup2.c",
+    "posix_spawn_file_actions_addopen.c",
+    "posix_spawn_file_actions_destroy.c",
+    "posix_spawn_file_actions_init.c",
+    "posix_spawnattr_destroy.c",
+    "posix_spawnattr_getflags.c",
+    "posix_spawnattr_getpgroup.c",
+    "posix_spawnattr_getsigdefault.c",
+    "posix_spawnattr_getsigmask.c",
+    "posix_spawnattr_init.c",
+    "posix_spawnattr_sched.c",
+    "posix_spawnattr_setflags.c",
+    "posix_spawnattr_setpgroup.c",
+    "posix_spawnattr_setsigdefault.c",
+    "posix_spawnattr_setsigmask.c",
+    "posix_spawnp.c",
+    "system.c",
+    "wait.c",
+    "waitid.c",
+    "waitpid.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/regex/BUILD.gn b/zircon/third_party/ulib/musl/src/regex/BUILD.gn
new file mode 100644
index 0000000..98b2b80
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/regex/BUILD.gn
@@ -0,0 +1,13 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("regex") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "fnmatch.c",
+    "glob.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/sched/BUILD.gn b/zircon/third_party/ulib/musl/src/sched/BUILD.gn
new file mode 100644
index 0000000..32d70d6
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/sched/BUILD.gn
@@ -0,0 +1,21 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("sched") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "affinity.c",
+    "sched_cpucount.c",
+    "sched_get_priority_max.c",
+    "sched_getcpu.c",
+    "sched_getparam.c",
+    "sched_getscheduler.c",
+    "sched_rr_get_interval.c",
+    "sched_setparam.c",
+    "sched_setscheduler.c",
+    "sched_yield.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/setjmp/BUILD.gn b/zircon/third_party/ulib/musl/src/setjmp/BUILD.gn
new file mode 100644
index 0000000..23a0152
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/setjmp/BUILD.gn
@@ -0,0 +1,18 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# TODO(mcgrathr): This code was all rewritten from scratch.  Move it out of
+# third_party/ulib/musl into system/ulib/c.
+
+source_set("setjmp") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "${toolchain.cpu}/longjmp.S",
+    "${toolchain.cpu}/setjmp.S",
+    "longjmp.c",
+    "setjmp.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/signal/BUILD.gn b/zircon/third_party/ulib/musl/src/signal/BUILD.gn
new file mode 100644
index 0000000..743b7d3
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/signal/BUILD.gn
@@ -0,0 +1,40 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("signal") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "getitimer.c",
+    "kill.c",
+    "killpg.c",
+    "psiginfo.c",
+    "psignal.c",
+    "raise.c",
+    "setitimer.c",
+    "sigaction.c",
+    "sigaddset.c",
+    "sigaltstack.c",
+    "sigandset.c",
+    "sigdelset.c",
+    "sigemptyset.c",
+    "sigfillset.c",
+    "siginterrupt.c",
+    "sigisemptyset.c",
+    "sigismember.c",
+    "signal.c",
+    "sigorset.c",
+    "sigpause.c",
+    "sigpending.c",
+    "sigprocmask.c",
+    "sigqueue.c",
+    "sigrtmax.c",
+    "sigrtmin.c",
+    "sigsuspend.c",
+    "sigtimedwait.c",
+    "sigwait.c",
+    "sigwaitinfo.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/stat/BUILD.gn b/zircon/third_party/ulib/musl/src/stat/BUILD.gn
new file mode 100644
index 0000000..df135e8
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/stat/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("stat") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "futimesat.c",
+    "lchmod.c",
+    "mkfifoat.c",
+    "mknodat.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/stdio/BUILD.gn b/zircon/third_party/ulib/musl/src/stdio/BUILD.gn
new file mode 100644
index 0000000..dacc7361
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/stdio/BUILD.gn
@@ -0,0 +1,124 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("stdio") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__fclose_ca.c",
+    "__fdopen.c",
+    "__fmodeflags.c",
+    "__fopen_rb_ca.c",
+    "__lockfile.c",
+    "__overflow.c",
+    "__stdio_close.c",
+    "__stdio_exit.c",
+    "__stdio_read.c",
+    "__stdio_seek.c",
+    "__stdio_write.c",
+    "__stdout_write.c",
+    "__string_read.c",
+    "__toread.c",
+    "__towrite.c",
+    "__uflow.c",
+    "asprintf.c",
+    "clearerr.c",
+    "dprintf.c",
+    "fclose.c",
+    "feof.c",
+    "ferror.c",
+    "fflush.c",
+    "fgetc.c",
+    "fgetln.c",
+    "fgetpos.c",
+    "fgets.c",
+    "fgetwc.c",
+    "fgetws.c",
+    "fileno.c",
+    "flockfile.c",
+    "fmemopen.c",
+    "fopen.c",
+    "fprintf.c",
+    "fputc.c",
+    "fputs.c",
+    "fputwc.c",
+    "fputws.c",
+    "fread.c",
+    "freopen.c",
+    "fscanf.c",
+    "fseek.c",
+    "fsetpos.c",
+    "ftell.c",
+    "ftrylockfile.c",
+    "funlockfile.c",
+    "fwide.c",
+    "fwprintf.c",
+    "fwrite.c",
+    "fwscanf.c",
+    "getc.c",
+    "getc_unlocked.c",
+    "getchar.c",
+    "getchar_unlocked.c",
+    "getdelim.c",
+    "getline.c",
+    "gets.c",
+    "getw.c",
+    "getwc.c",
+    "getwchar.c",
+    "ofl.c",
+    "ofl_add.c",
+    "open_memstream.c",
+    "open_wmemstream.c",
+    "pclose.c",
+    "perror.c",
+    "popen.c",
+    "printf.c",
+    "putc.c",
+    "putc_unlocked.c",
+    "putchar.c",
+    "putchar_unlocked.c",
+    "puts.c",
+    "putw.c",
+    "putwc.c",
+    "putwchar.c",
+    "remove.c",
+    "rewind.c",
+    "scanf.c",
+    "setbuf.c",
+    "setbuffer.c",
+    "setlinebuf.c",
+    "setvbuf.c",
+    "snprintf.c",
+    "sprintf.c",
+    "sscanf.c",
+    "stderr.c",
+    "stdin.c",
+    "stdout.c",
+    "swprintf.c",
+    "swscanf.c",
+    "tempnam.c",
+    "tmpfile.c",
+    "tmpnam.c",
+    "ungetc.c",
+    "ungetwc.c",
+    "vasprintf.c",
+    "vdprintf.c",
+    "vfprintf.c",
+    "vfscanf.c",
+    "vfwprintf.c",
+    "vfwscanf.c",
+    "vprintf.c",
+    "vscanf.c",
+    "vsnprintf.c",
+    "vsprintf.c",
+    "vsscanf.c",
+    "vswprintf.c",
+    "vswscanf.c",
+    "vwprintf.c",
+    "vwscanf.c",
+    "wprintf.c",
+    "wscanf.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/stdlib/BUILD.gn b/zircon/third_party/ulib/musl/src/stdlib/BUILD.gn
new file mode 100644
index 0000000..32c036d8
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/stdlib/BUILD.gn
@@ -0,0 +1,31 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("stdlib") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "abs.c",
+    "atof.c",
+    "atoi.c",
+    "atol.c",
+    "atoll.c",
+    "bsearch.c",
+    "div.c",
+    "ecvt.c",
+    "fcvt.c",
+    "gcvt.c",
+    "imaxabs.c",
+    "imaxdiv.c",
+    "labs.c",
+    "ldiv.c",
+    "llabs.c",
+    "lldiv.c",
+    "strtod.c",
+    "strtol.c",
+    "wcstod.c",
+    "wcstol.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/string/BUILD.gn b/zircon/third_party/ulib/musl/src/string/BUILD.gn
new file mode 100644
index 0000000..3eb2b2d
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/string/BUILD.gn
@@ -0,0 +1,225 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+group("string") {
+  deps = [
+    ":bsd",
+    ":extmem",
+    ":extstr",
+    ":stdmem",
+    ":stdstr",
+    ":wcs",
+    ":wmem",
+  ]
+}
+
+# These are the functions the compiler produces implicit calls to.
+source_set("compiler") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  if (current_cpu == "arm64") {
+    # These use '#include "third_party/lib/cortex-strings/src/aarch64/..."'.
+    include_dirs = [ "$zx/" ]
+    sources = [
+      "aarch64/memcpy.S",
+      "aarch64/memset.S",
+    ]
+  } else if (current_cpu == "x64") {
+    sources = [
+      "x86_64/memcpy.S",
+      "x86_64/memset.S",
+    ]
+  } else {
+    sources = [
+      "memcpy.c",
+      "memset.c",
+    ]
+  }
+}
+
+source_set("stdmem") {
+  deps = [
+    ":compiler",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "memrchr.c",
+  ]
+  if (current_cpu == "arm64") {
+    # These use '#include "third_party/lib/cortex-strings/src/aarch64/..."'.
+    include_dirs = [ "$zx/" ]
+    sources += [ "aarch64/memmove.S" ]
+  } else if (current_cpu == "x64") {
+    sources += [ "x86_64/memmove.S" ]
+  } else {
+    sources += [ "memmove.c" ]
+  }
+  if (current_cpu == "arm64") {
+    sources += [
+      "$zx/third_party/lib/cortex-strings/src/aarch64/memchr.S",
+      "$zx/third_party/lib/cortex-strings/src/aarch64/memcmp.S",
+    ]
+  } else {
+    sources += [
+      "memchr.c",
+      "memcmp.c",
+    ]
+  }
+}
+
+source_set("extmem") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "memccpy.c",
+    "memmem.c",
+  ]
+  if (current_cpu == "x64" && !defined(toolchain.sanitizer)) {
+    # Only use the assembly version if x86-64 and not sanitized.
+    sources += [ "x86_64/mempcpy.S" ]
+  } else {
+    sources += [ "mempcpy.c" ]
+  }
+}
+
+source_set("minimal_str") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  if (current_cpu == "arm64") {
+    sources = [
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strlen.S",
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strncmp.S",
+    ]
+  } else {
+    sources = [
+      "strlen.c",
+      "strncmp.c",
+    ]
+  }
+}
+
+source_set("stdstr") {
+  deps = [
+    ":minimal_str",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "strcat.c",
+    "strcspn.c",
+    "strdup.c",
+    "strerror_r.c",
+    "strncat.c",
+    "strncpy.c",
+    "strpbrk.c",
+    "strrchr.c",
+    "strspn.c",
+    "strstr.c",
+    "strtok.c",
+    "strtok_r.c",
+  ]
+  if (current_cpu == "arm64") {
+    sources += [
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strchr.S",
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strcmp.S",
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strcpy.S",
+    ]
+  } else {
+    sources += [
+      "strchr.c",
+      "strcmp.c",
+      "strcpy.c",
+    ]
+  }
+}
+
+source_set("extstr") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "stpcpy.c",
+    "stpncpy.c",
+    "strcasecmp.c",
+    "strcasestr.c",
+    "strlcat.c",
+    "strlcpy.c",
+    "strncasecmp.c",
+    "strndup.c",
+    "strsep.c",
+    "strsignal.c",
+    "strverscmp.c",
+  ]
+  if (current_cpu == "arm64") {
+    # These use '#include "third_party/lib/cortex-strings/src/aarch64/..."'.
+    include_dirs = [ "$zx/" ]
+    sources += [
+      "$zx/third_party/lib/cortex-strings/src/aarch64/strnlen.S",
+      "aarch64/strchrnul.S",
+    ]
+  } else {
+    sources += [
+      "strchrnul.c",
+      "strnlen.c",
+    ]
+  }
+}
+
+source_set("bsd") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "bcmp.c",
+    "bcopy.c",
+    "bzero.c",
+    "index.c",
+    "rindex.c",
+    "swab.c",
+  ]
+}
+
+source_set("wcs") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "wcpcpy.c",
+    "wcpncpy.c",
+    "wcscasecmp.c",
+    "wcscat.c",
+    "wcschr.c",
+    "wcscmp.c",
+    "wcscpy.c",
+    "wcscspn.c",
+    "wcsdup.c",
+    "wcslen.c",
+    "wcsncasecmp.c",
+    "wcsncat.c",
+    "wcsncmp.c",
+    "wcsncpy.c",
+    "wcsnlen.c",
+    "wcspbrk.c",
+    "wcsrchr.c",
+    "wcsspn.c",
+    "wcsstr.c",
+    "wcstok.c",
+    "wcswcs.c",
+  ]
+}
+
+source_set("wmem") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "wmemchr.c",
+    "wmemcmp.c",
+    "wmemcpy.c",
+    "wmemmove.c",
+    "wmemset.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/string/aarch64/memcpy.S b/zircon/third_party/ulib/musl/src/string/aarch64/memcpy.S
index d75abb7..1598c54 100644
--- a/zircon/third_party/ulib/musl/src/string/aarch64/memcpy.S
+++ b/zircon/third_party/ulib/musl/src/string/aarch64/memcpy.S
@@ -9,5 +9,9 @@
 
 #include "third_party/lib/cortex-strings/src/aarch64/memcpy.S"
 
+#ifdef HIDDEN
+    .hidden memcpy
+#else
 ALIAS(memcpy, __unsanitized_memcpy)
 ASAN_WEAK_ALIAS(memcpy)
+#endif
diff --git a/zircon/third_party/ulib/musl/src/string/aarch64/memmove.S b/zircon/third_party/ulib/musl/src/string/aarch64/memmove.S
index f5497bf..dbb9092 100644
--- a/zircon/third_party/ulib/musl/src/string/aarch64/memmove.S
+++ b/zircon/third_party/ulib/musl/src/string/aarch64/memmove.S
@@ -9,5 +9,9 @@
 
 #include "third_party/lib/cortex-strings/src/aarch64/memmove.S"
 
+#ifdef HIDDEN
+    .hidden memmove
+#else
 ALIAS(memmove, __unsanitized_memmove)
 ASAN_WEAK_ALIAS(memmove)
+#endif
diff --git a/zircon/third_party/ulib/musl/src/string/x86_64/memcpy.S b/zircon/third_party/ulib/musl/src/string/x86_64/memcpy.S
index f07b462..97a7f6a 100644
--- a/zircon/third_party/ulib/musl/src/string/x86_64/memcpy.S
+++ b/zircon/third_party/ulib/musl/src/string/x86_64/memcpy.S
@@ -15,7 +15,11 @@
     ret
 END(memcpy)
 
+#ifdef HIDDEN
+    .hidden memcpy
+#else
 ALIAS(memcpy, __unsanitized_memcpy)
 ASAN_WEAK_ALIAS(memcpy)
+#endif
 ALIAS(memcpy, __memcpy_fwd)
 .hidden __memcpy_fwd
diff --git a/zircon/third_party/ulib/musl/src/string/x86_64/memmove.S b/zircon/third_party/ulib/musl/src/string/x86_64/memmove.S
index 52c8d98..79904f6 100644
--- a/zircon/third_party/ulib/musl/src/string/x86_64/memmove.S
+++ b/zircon/third_party/ulib/musl/src/string/x86_64/memmove.S
@@ -16,5 +16,9 @@
 	ret
 END(memmove)
 
+#ifdef HIDDEN
+    .hidden memmove
+#else
 ALIAS(memmove, __unsanitized_memmove)
 ASAN_WEAK_ALIAS(memmove)
+#endif
diff --git a/zircon/third_party/ulib/musl/src/string/x86_64/memset.S b/zircon/third_party/ulib/musl/src/string/x86_64/memset.S
index 2f01840..fbc2ec0 100644
--- a/zircon/third_party/ulib/musl/src/string/x86_64/memset.S
+++ b/zircon/third_party/ulib/musl/src/string/x86_64/memset.S
@@ -17,9 +17,13 @@
     ret
 END(memset)
 
+#ifdef HIDDEN
+    .hidden memset
+#else
 ALIAS(memset, __unsanitized_memset)
 ASAN_WEAK_ALIAS(memset)
 
 // This name is called from inside libc to avoid going through the PLT.
 .hidden __libc_memset
 ALIAS(memset, __libc_memset)
+#endif
diff --git a/zircon/third_party/ulib/musl/src/temp/BUILD.gn b/zircon/third_party/ulib/musl/src/temp/BUILD.gn
new file mode 100644
index 0000000..e2bda1e
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/temp/BUILD.gn
@@ -0,0 +1,18 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("temp") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__randname.c",
+    "mkdtemp.c",
+    "mkostemp.c",
+    "mkostemps.c",
+    "mkstemp.c",
+    "mkstemps.c",
+    "mktemp.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/termios/BUILD.gn b/zircon/third_party/ulib/musl/src/termios/BUILD.gn
new file mode 100644
index 0000000..7e506aa
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/termios/BUILD.gn
@@ -0,0 +1,21 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("termios") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "cfgetospeed.c",
+    "cfmakeraw.c",
+    "cfsetospeed.c",
+    "tcdrain.c",
+    "tcflow.c",
+    "tcflush.c",
+    "tcgetattr.c",
+    "tcgetsid.c",
+    "tcsendbreak.c",
+    "tcsetattr.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/thread/BUILD.gn b/zircon/third_party/ulib/musl/src/thread/BUILD.gn
new file mode 100644
index 0000000..63af5fa
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/thread/BUILD.gn
@@ -0,0 +1,38 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("thread") {
+  deps = [
+    "$zx/system/ulib/sync",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__timedwait.c",
+    "__tls_get_addr.c",
+    "__wait.c",
+    "allocate.c",
+    "call_once.c",
+    "cnd_broadcast.c",
+    "cnd_destroy.c",
+    "cnd_init.c",
+    "cnd_signal.c",
+    "cnd_timedwait.c",
+    "cnd_wait.c",
+    "mtx_destroy.c",
+    "mtx_init.c",
+    "mtx_lock.c",
+    "mtx_timedlock.c",
+    "mtx_trylock.c",
+    "mtx_unlock.c",
+    "safestack.c",
+    "thrd_create.c",
+    "thrd_detach.c",
+    "thrd_exit.c",
+    "thrd_join.c",
+    "thrd_sleep.c",
+    "thrd_yield.c",
+    "tss.c",
+    "tss_set.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/thread/thrd_create.c b/zircon/third_party/ulib/musl/src/thread/thrd_create.c
index 8557ea4..0c7699f 100644
--- a/zircon/third_party/ulib/musl/src/thread/thrd_create.c
+++ b/zircon/third_party/ulib/musl/src/thread/thrd_create.c
@@ -1,4 +1,4 @@
-#define _ALL_SOURCE
+#define _ALL_SOURCE 1
 #include <threads.h>
 
 #include "threads_impl.h"
diff --git a/zircon/third_party/ulib/musl/src/time/BUILD.gn b/zircon/third_party/ulib/musl/src/time/BUILD.gn
new file mode 100644
index 0000000..ad7d547
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/time/BUILD.gn
@@ -0,0 +1,46 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("time") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "__asctime.c",
+    "__map_file.c",
+    "__month_to_secs.c",
+    "__secs_to_tm.c",
+    "__tm_to_secs.c",
+    "__tz.c",
+    "__year_to_secs.c",
+    "asctime.c",
+    "asctime_r.c",
+    "clock.c",
+    "clock_getcpuclockid.c",
+    "clock_getres.c",
+    "clock_gettime.c",
+    "clock_nanosleep.c",
+    "clock_settime.c",
+    "ctime.c",
+    "ctime_r.c",
+    "difftime.c",
+    "ftime.c",
+    "getdate.c",
+    "gettimeofday.c",
+    "gmtime.c",
+    "gmtime_r.c",
+    "localtime.c",
+    "localtime_r.c",
+    "mktime.c",
+    "nanosleep.c",
+    "strftime.c",
+    "strptime.c",
+    "time.c",
+    "timegm.c",
+    "times.c",
+    "timespec_get.c",
+    "utime.c",
+    "wcsftime.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/src/unistd/BUILD.gn b/zircon/third_party/ulib/musl/src/unistd/BUILD.gn
new file mode 100644
index 0000000..90f917f
--- /dev/null
+++ b/zircon/third_party/ulib/musl/src/unistd/BUILD.gn
@@ -0,0 +1,27 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("unistd") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "_exit.c",
+    "acct.c",
+    "alarm.c",
+    "ctermid.c",
+    "gethostname.c",
+    "getlogin.c",
+    "getlogin_r.c",
+    "pause.c",
+    "posix_close.c",
+    "setpgrp.c",
+    "sleep.c",
+    "tcgetpgrp.c",
+    "tcsetpgrp.c",
+    "ttyname.c",
+    "ualarm.c",
+    "usleep.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/stubs/BUILD.gn b/zircon/third_party/ulib/musl/stubs/BUILD.gn
new file mode 100644
index 0000000..3873b8f
--- /dev/null
+++ b/zircon/third_party/ulib/musl/stubs/BUILD.gn
@@ -0,0 +1,14 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("stubs") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "idstubs.c",
+    "iostubs.c",
+    "socketstubs.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/third_party/complex/BUILD.gn b/zircon/third_party/ulib/musl/third_party/complex/BUILD.gn
new file mode 100644
index 0000000..8c858bb
--- /dev/null
+++ b/zircon/third_party/ulib/musl/third_party/complex/BUILD.gn
@@ -0,0 +1,27 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("complex") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  include_dirs = [ "$zx/third_party/ulib/musl/third_party/include" ]
+  sources = [
+    "__cexp.c",
+    "__cexpf.c",
+    "catan.c",
+    "catanf.c",
+    "catanl.c",
+    "ccosh.c",
+    "ccoshf.c",
+    "cexp.c",
+    "cexpf.c",
+    "csinh.c",
+    "csinhf.c",
+    "csqrt.c",
+    "csqrtf.c",
+    "ctanh.c",
+    "ctanhf.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/third_party/math/BUILD.gn b/zircon/third_party/ulib/musl/third_party/math/BUILD.gn
new file mode 100644
index 0000000..e83ed32
--- /dev/null
+++ b/zircon/third_party/ulib/musl/third_party/math/BUILD.gn
@@ -0,0 +1,96 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("math") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  include_dirs = [ "$zx/third_party/ulib/musl/third_party/include" ]
+  sources = [
+    "__cos.c",
+    "__cosdf.c",
+    "__cosl.c",
+    "__polevll.c",
+    "__rem_pio2.c",
+    "__rem_pio2_large.c",
+    "__rem_pio2f.c",
+    "__rem_pio2l.c",
+    "__sin.c",
+    "__sindf.c",
+    "__sinl.c",
+    "__tan.c",
+    "__tandf.c",
+    "__tanl.c",
+    "acos.c",
+    "acosf.c",
+    "asin.c",
+    "asinf.c",
+    "atan.c",
+    "atan2.c",
+    "atan2f.c",
+    "atanf.c",
+    "cbrt.c",
+    "cbrtf.c",
+    "cbrtl.c",
+    "cos.c",
+    "cosf.c",
+    "erf.c",
+    "erff.c",
+    "erfl.c",
+    "exp.c",
+    "exp2.c",
+    "exp2f.c",
+    "expf.c",
+    "expm1.c",
+    "expm1f.c",
+    "fma.c",
+    "fmaf.c",
+    "fmal.c",
+    "j0.c",
+    "j0f.c",
+    "j1.c",
+    "j1f.c",
+    "jn.c",
+    "jnf.c",
+    "lgamma_r.c",
+    "lgammaf_r.c",
+    "lgammal.c",
+    "log.c",
+    "log10.c",
+    "log10f.c",
+    "log1p.c",
+    "log1pf.c",
+    "log2.c",
+    "log2f.c",
+    "logf.c",
+    "pow.c",
+    "powf.c",
+    "powl.c",
+    "scalb.c",
+    "scalbf.c",
+    "sin.c",
+    "sincos.c",
+    "sincosf.c",
+    "sinf.c",
+    "tan.c",
+    "tanf.c",
+    "tgammal.c",
+  ]
+  if (current_cpu != "x64") {
+    # $zx/third_party/ulib/musl/src/math/x86_64 provides these in assembly.
+    sources += [
+      "acosl.c",
+      "asinl.c",
+      "atan2l.c",
+      "atanl.c",
+      "exp2l.c",
+      "expl.c",
+      "expm1l.c",
+      "log10l.c",
+      "log1pl.c",
+      "log2l.c",
+      "logl.c",
+    ]
+  }
+}
diff --git a/zircon/third_party/ulib/musl/third_party/smoothsort/BUILD.gn b/zircon/third_party/ulib/musl/third_party/smoothsort/BUILD.gn
new file mode 100644
index 0000000..fa3a31a
--- /dev/null
+++ b/zircon/third_party/ulib/musl/third_party/smoothsort/BUILD.gn
@@ -0,0 +1,12 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("smoothsort") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "qsort.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/third_party/tre/BUILD.gn b/zircon/third_party/ulib/musl/third_party/tre/BUILD.gn
new file mode 100644
index 0000000..f012ba6
--- /dev/null
+++ b/zircon/third_party/ulib/musl/third_party/tre/BUILD.gn
@@ -0,0 +1,15 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("tre") {
+  deps = [
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+  sources = [
+    "regcomp.c",
+    "regerror.c",
+    "regexec.c",
+    "tre-mem.c",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/zircon/BUILD.gn b/zircon/third_party/ulib/musl/zircon/BUILD.gn
new file mode 100644
index 0000000..2cc6aa9
--- /dev/null
+++ b/zircon/third_party/ulib/musl/zircon/BUILD.gn
@@ -0,0 +1,16 @@
+# Copyright 2019 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("zircon") {
+  sources = [
+    "getentropy.c",
+    "internal.c",
+    "take_startup_handle.c",
+    "thrd_get_zx_handle.c",
+  ]
+  deps = [
+    "$zx/system/ulib/runtime",
+    "$zx/third_party/ulib/musl:musl_internal",
+  ]
+}
diff --git a/zircon/third_party/ulib/musl/zircon/take_startup_handle.c b/zircon/third_party/ulib/musl/zircon/take_startup_handle.c
index 47edece..4e1a2ed 100644
--- a/zircon/third_party/ulib/musl/zircon/take_startup_handle.c
+++ b/zircon/third_party/ulib/musl/zircon/take_startup_handle.c
@@ -2,13 +2,12 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+#define _ALL_SOURCE 1
 #include "libc.h"
+#include <threads.h>
 #include <zircon/process.h>
 #include <zircon/types.h>
 
-#define _ALL_SOURCE  // For MTX_INIT
-#include <threads.h>
-
 static mtx_t startup_handles_lock = MTX_INIT;
 static uint32_t startup_handles_num;
 static zx_handle_t* startup_handles;