Merge "Fix symbol lookup order during relocation"
diff --git a/libc/include/limits.h b/libc/include/limits.h
index 69df082..6ae629b 100644
--- a/libc/include/limits.h
+++ b/libc/include/limits.h
@@ -97,4 +97,5 @@
 /* POSIX says these belong in <unistd.h> but BSD has some in <limits.h>. */
 #include <machine/posix_limits.h>
 
+#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
 #endif /* !_LIMITS_H_ */
diff --git a/libc/include/machine/posix_limits.h b/libc/include/machine/posix_limits.h
index f906e3b..f092731 100644
--- a/libc/include/machine/posix_limits.h
+++ b/libc/include/machine/posix_limits.h
@@ -37,6 +37,7 @@
 #define _POSIX_CHOWN_RESTRICTED     1  /* yes, chown requires appropriate privileges */
 #define _POSIX_FSYNC                1  /* fdatasync() supported */
 #define _POSIX_JOB_CONTROL          1  /* job control is a Linux feature */
+#define _POSIX_HOST_NAME_MAX        255
 #define _POSIX_LINK_MAX             8
 #define _POSIX_LOGIN_NAME_MAX       9  /* includes trailing NUL */
 #define _POSIX_MAPPED_FILES         1  /* mmap-ed files supported */
diff --git a/libc/upstream-freebsd/android/include/libc_private.h b/libc/upstream-freebsd/android/include/libc_private.h
deleted file mode 100644
index c6a6433..0000000
--- a/libc/upstream-freebsd/android/include/libc_private.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-#ifndef _BIONIC_FREEBSD_LIBC_PRIVATE_H_included
-#define _BIONIC_FREEBSD_LIBC_PRIVATE_H_included
-
-#define STDIO_THREAD_LOCK()   /* TODO: until we have the FreeBSD findfp.c, this is useless. */
-#define STDIO_THREAD_UNLOCK() /* TODO: until we have the FreeBSD findfp.c, this is useless. */
-
-#define ORIENT(fp, o) /* Only needed for wide-character stream support. */
-
-#endif
diff --git a/linker/linker.cpp b/linker/linker.cpp
index ab0fc07..008833f 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -107,6 +107,8 @@
 #define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
 #define LDPRELOAD_MAX 8
 
+#define MAX_PATH_LEN 512
+
 static char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
 static const char* g_ld_library_paths[LDPATH_MAX + 1];
 
@@ -742,7 +744,7 @@
 }
 
 static int open_library_on_path(const char* name, const char* const paths[]) {
-  char buf[512];
+  char buf[MAX_PATH_LEN];
   for (size_t i = 0; paths[i] != nullptr; ++i) {
     int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
     if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
@@ -1121,8 +1123,18 @@
       return nullptr;
     }
   }
+
+  size_t name_len = strlen(name);
+  if (name_len >= MAX_PATH_LEN) {
+    DL_ERR("library name \"%s\" is too long", name);
+    return nullptr;
+  }
+
+  char local_name[name_len+1];
+  strlcpy(local_name, name, name_len+1);
+
   protect_data(PROT_READ | PROT_WRITE);
-  soinfo* si = find_library(name, flags, extinfo);
+  soinfo* si = find_library(local_name, flags, extinfo);
   if (si != nullptr) {
     si->CallConstructors();
   }