Merge "Switch to inline assembler in crtbegin."
diff --git a/libc/dns/include/resolv_netid.h b/libc/dns/include/resolv_netid.h
index 711792b..3b2f4da 100644
--- a/libc/dns/include/resolv_netid.h
+++ b/libc/dns/include/resolv_netid.h
@@ -86,6 +86,7 @@
  * explore_fqdn() dispatch table method, with the below function only making DNS calls.
  */
 struct hostent *android_gethostbyaddrfornetcontext(const void *, socklen_t, int, const struct android_net_context *) __used_in_netd;
+struct hostent *android_gethostbynamefornetcontext(const char *, int, const struct android_net_context *) __used_in_netd;
 int android_getaddrinfofornetcontext(const char *, const char *, const struct addrinfo *,
     const struct android_net_context *, struct addrinfo **) __used_in_netd;
 
diff --git a/libc/dns/include/resolv_private.h b/libc/dns/include/resolv_private.h
index 284270c..77b03bf 100644
--- a/libc/dns/include/resolv_private.h
+++ b/libc/dns/include/resolv_private.h
@@ -504,8 +504,8 @@
 __LIBC_HIDDEN__ int		res_getservers(res_state,
 				    union res_sockaddr_union *, int);
 
-__LIBC_HIDDEN__ void res_setnetid(res_state, unsigned);
-__LIBC_HIDDEN__ void res_setmark(res_state, unsigned);
+struct android_net_context; /* forward */
+__LIBC_HIDDEN__ void res_setnetcontext(res_state, const struct android_net_context *);
 
 // We use the OpenBSD __res_randomid...
 u_int __res_randomid(void);
diff --git a/libc/dns/net/getaddrinfo.c b/libc/dns/net/getaddrinfo.c
index 4215963..418bf6d 100644
--- a/libc/dns/net/getaddrinfo.c
+++ b/libc/dns/net/getaddrinfo.c
@@ -1981,8 +1981,7 @@
 	 * fully populate the thread private data here, but if we get down there
 	 * and have a cache hit that would be wasted, so we do the rest there on miss
 	 */
-	res_setnetid(res, netcontext->dns_netid);
-	res_setmark(res, netcontext->dns_mark);
+	res_setnetcontext(res, netcontext);
 	if (res_searchN(name, &q, res) < 0) {
 		__res_put_state(res);
 		free(buf);
diff --git a/libc/dns/net/gethnamaddr.c b/libc/dns/net/gethnamaddr.c
index 036ad1d..4e416fd 100644
--- a/libc/dns/net/gethnamaddr.c
+++ b/libc/dns/net/gethnamaddr.c
@@ -160,7 +160,7 @@
 static int _dns_gethtbyname(void *, void *, va_list);
 
 static struct hostent *gethostbyname_internal(const char *, int, res_state,
-    struct hostent *, char *, size_t, int *, unsigned, unsigned);
+    struct hostent *, char *, size_t, int *, const struct android_net_context *);
 static struct hostent* android_gethostbyaddrfornetcontext_proxy_internal(const void*, socklen_t,
     int, struct hostent *, char *, size_t, int *, const struct android_net_context *);
 
@@ -529,15 +529,15 @@
 	_DIAGASSERT(name != NULL);
 
 	if (res->options & RES_USE_INET6) {
-		*result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp, NETID_UNSET,
-		                                 MARK_UNSET);
+		*result = gethostbyname_internal(name, AF_INET6, res, hp, buf, buflen, errorp,
+		                                 &NETCONTEXT_UNSET);
 		if (*result) {
 			__res_put_state(res);
 			return 0;
 		}
 	}
-	*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp, NETID_UNSET,
-	                                 MARK_UNSET);
+	*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp,
+	                                 &NETCONTEXT_UNSET);
 	__res_put_state(res);
 	if (!*result && errno == ENOSPC) {
 	  errno = ERANGE;
@@ -558,8 +558,8 @@
 		*errorp = NETDB_INTERNAL;
 		return -1;
 	}
-	*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp, NETID_UNSET,
-	                                 MARK_UNSET);
+	*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp,
+	                                 &NETCONTEXT_UNSET);
 	__res_put_state(res);
 	if (!*result && errno == ENOSPC) {
 		errno = ERANGE;
@@ -822,17 +822,16 @@
 // very similar in proxy-ness to android_getaddrinfo_proxy
 static struct hostent *
 gethostbyname_internal(const char *name, int af, res_state res, struct hostent *hp, char *hbuf,
-                       size_t hbuflen, int *errorp, unsigned netid, unsigned mark)
+                       size_t hbuflen, int *errorp, const struct android_net_context *netcontext)
 {
 	FILE* proxy = android_open_proxy();
 	if (proxy == NULL) {
 		// Either we're not supposed to be using the proxy or the proxy is unavailable.
-		res_setnetid(res, netid);
-		res_setmark(res, mark);
+		res_setnetcontext(res, netcontext);
 		return gethostbyname_internal_real(name, af, res, hp, hbuf, hbuflen, errorp);
 	}
 
-	netid = __netdClientDispatch.netIdForResolv(netid);
+	unsigned netid = __netdClientDispatch.netIdForResolv(netcontext->app_netid);
 
 	// This is writing to system/netd/server/DnsProxyListener.cpp and changes
 	// here need to be matched there.
@@ -1296,8 +1295,7 @@
 		free(buf);
 		return NS_NOTFOUND;
 	}
-	res_setnetid(res, netcontext->dns_netid);
-	res_setmark(res, netcontext->dns_mark);
+	res_setnetcontext(res, netcontext);
 	n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf));
 	if (n < 0) {
 		free(buf);
@@ -1611,13 +1609,21 @@
 struct hostent *
 android_gethostbynamefornet(const char *name, int af, unsigned netid, unsigned mark)
 {
+	const struct android_net_context netcontext = make_context(netid, mark);
+	return android_gethostbynamefornetcontext(name, af, &netcontext);
+}
+
+struct hostent *
+android_gethostbynamefornetcontext(const char *name, int af,
+	const struct android_net_context *netcontext)
+{
 	struct hostent *hp;
 	res_state res = __res_get_state();
 	if (res == NULL)
 		return NULL;
 	res_static rs = __res_get_static(); /* Use res_static to provide thread-safety. */
 	hp = gethostbyname_internal(name, af, res, &rs->host, rs->hostbuf, sizeof(rs->hostbuf),
-	                            &h_errno, netid, mark);
+	                            &h_errno, netcontext);
 	__res_put_state(res);
 	return hp;
 }
diff --git a/libc/dns/resolv/res_init.c b/libc/dns/resolv/res_init.c
index b9fc131..302a62b 100644
--- a/libc/dns/resolv/res_init.c
+++ b/libc/dns/resolv/res_init.c
@@ -768,17 +768,12 @@
 }
 
 #ifdef ANDROID_CHANGES
-void res_setnetid(res_state statp, unsigned netid)
+void res_setnetcontext(res_state statp, const struct android_net_context *netcontext)
 {
 	if (statp != NULL) {
-		statp->netid = netid;
-	}
-}
-
-void res_setmark(res_state statp, unsigned mark)
-{
-	if (statp != NULL) {
-		statp->_mark = mark;
+		statp->netid = netcontext->dns_netid;
+		statp->_mark = netcontext->dns_mark;
+		statp->qhook = netcontext->qhook;
 	}
 }
 
diff --git a/libc/include/sys/epoll.h b/libc/include/sys/epoll.h
index b7fdd4d..5e92fdc 100644
--- a/libc/include/sys/epoll.h
+++ b/libc/include/sys/epoll.h
@@ -75,6 +75,22 @@
 
 int epoll_create(int);
 int epoll_create1(int) __INTRODUCED_IN(21);
+
+/*
+ * Some third-party code uses the existence of EPOLL_CLOEXEC to detect the
+ * availability of epoll_create1. This is not correct, since having up-to-date
+ * UAPI headers says nothing about the C library, but for the time being we
+ * don't want to harm adoption to the unified headers. We'll undef EPOLL_CLOEXEC
+ * if we don't have epoll_create1 for the time being, and maybe revisit this
+ * later.
+ *
+ * https://github.com/android-ndk/ndk/issues/302
+ * https://github.com/android-ndk/ndk/issues/394
+ */
+#if __ANDROID_API__ < __ANDROID_API_L__ && defined(EPOLL_CLOEXEC)
+#undef EPOLL_CLOEXEC
+#endif
+
 int epoll_ctl(int, int, int, struct epoll_event*);
 int epoll_wait(int, struct epoll_event*, int, int);
 int epoll_pwait(int, struct epoll_event*, int, int, const sigset_t*) __INTRODUCED_IN(21);
diff --git a/libc/include/sys/inotify.h b/libc/include/sys/inotify.h
index 2e99144..98f7198 100644
--- a/libc/include/sys/inotify.h
+++ b/libc/include/sys/inotify.h
@@ -37,8 +37,21 @@
 
 __BEGIN_DECLS
 
+/*
+ * Some third-party code uses the existence of IN_CLOEXEC/IN_NONBLOCK to detect
+ * the availability of inotify_init1. This is not correct, since
+ * `syscall(__NR_inotify_init1, IN_CLOEXEC)` is still valid even if the C
+ * library doesn't have that function, but for the time being we don't want to
+ * harm adoption to the unified headers. We'll avoid defining IN_CLOEXEC and
+ * IN_NONBLOCK if we don't have inotify_init1 for the time being, and maybe
+ * revisit this later.
+ *
+ * https://github.com/android-ndk/ndk/issues/394
+ */
+#if __ANDROID_API__ >= __ANDROID_API_L__
 #define IN_CLOEXEC O_CLOEXEC
 #define IN_NONBLOCK O_NONBLOCK
+#endif
 
 int inotify_init(void);
 int inotify_init1(int) __INTRODUCED_IN(21);
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index a74027a..b32f769 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1496,6 +1496,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     atexit; # arm
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 90929f5..f05e7eb 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1522,6 +1522,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     atexit; # arm
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index aac876d..a472214 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1339,6 +1339,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     bcopy; # arm x86 mips
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 682f2c4..27a597f 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1338,6 +1338,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     arc4random_addrandom; # arm x86 mips
     arc4random_stir; # arm x86 mips
     bcopy; # arm x86 mips
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 34d3429..afcf8bb 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1244,6 +1244,7 @@
     android_gethostbyaddrfornet;
     android_gethostbyaddrfornetcontext;
     android_gethostbynamefornet;
+    android_gethostbynamefornetcontext;
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/tests/libs/Android.build.dlext_testzip.mk b/tests/libs/Android.build.dlext_testzip.mk
index 0220ae2..4b6d099 100644
--- a/tests/libs/Android.build.dlext_testzip.mk
+++ b/tests/libs/Android.build.dlext_testzip.mk
@@ -22,7 +22,7 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_CLASS := NATIVE_TESTS
 LOCAL_MODULE := libdlext_test_zip_zipaligned
 LOCAL_MODULE_SUFFIX := .zip
 LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test_zip
@@ -44,7 +44,7 @@
 
 include $(CLEAR_VARS)
 
-LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_CLASS := NATIVE_TESTS
 LOCAL_MODULE := libdlext_test_runpath_zip_zipaligned
 LOCAL_MODULE_SUFFIX := .zip
 LOCAL_MODULE_PATH := $($(bionic_2nd_arch_prefix)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test_runpath_zip