Don't allow text relocations on 64-bit.

I've also updated our <sys/exec_elf.h> to match upstream.

Change-Id: I52f9fce3167541811208d273ff23ceaa112f7135
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 166efac..09f3ddf 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -295,6 +295,8 @@
     { .l_addr = 0, .l_name = 0, .l_ld = 0, .l_next = 0, .l_prev = 0, },
     .constructors_called = false,
     .load_bias = 0,
+#if !defined(__LP64__)
     .has_text_relocations = false,
+#endif
     .has_DT_SYMBOLIC = true,
 };
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 5a4f20f..6bf18ac 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1548,25 +1548,32 @@
             si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
             break;
         case DT_TEXTREL:
+#if defined(__LP64__)
+            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
+            return false;
+#else
             si->has_text_relocations = true;
             break;
+#endif
         case DT_SYMBOLIC:
             si->has_DT_SYMBOLIC = true;
             break;
         case DT_NEEDED:
             ++needed_count;
             break;
-#if defined DT_FLAGS
-        // TODO: why is DT_FLAGS not defined?
         case DT_FLAGS:
             if (d->d_un.d_val & DF_TEXTREL) {
+#if defined(__LP64__)
+                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
+                return false;
+#else
                 si->has_text_relocations = true;
+#endif
             }
             if (d->d_un.d_val & DF_SYMBOLIC) {
                 si->has_DT_SYMBOLIC = true;
             }
             break;
-#endif
 #if defined(__mips__)
         case DT_STRSZ:
         case DT_SYMENT:
@@ -1661,12 +1668,10 @@
     }
     *pneeded = NULL;
 
+#if !defined(__LP64__)
     if (si->has_text_relocations) {
-        /* Unprotect the segments, i.e. make them writable, to allow
-         * text relocations to work properly. We will later call
-         * phdr_table_protect_segments() after all of them are applied
-         * and all constructors are run.
-         */
+        // Make segments writable to allow text relocations to work properly. We will later call
+        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
         DL_WARN("%s has text relocations. This is wasting memory and prevents "
                 "security hardening. Please fix.", si->name);
         if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
@@ -1675,6 +1680,7 @@
             return false;
         }
     }
+#endif
 
 #if defined(USE_RELA)
     if (si->plt_rela != NULL) {
@@ -1713,15 +1719,16 @@
     si->flags |= FLAG_LINKED;
     DEBUG("[ finished linking %s ]", si->name);
 
+#if !defined(__LP64__)
     if (si->has_text_relocations) {
-        /* All relocations are done, we can protect our segments back to
-         * read-only. */
+        // All relocations are done, we can protect our segments back to read-only.
         if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
             DL_ERR("can't protect segments for \"%s\": %s",
                    si->name, strerror(errno));
             return false;
         }
     }
+#endif
 
     /* We can also turn on GNU RELRO protection */
     if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
diff --git a/linker/linker.h b/linker/linker.h
index 4ba354d..b9e1237 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -183,7 +183,9 @@
   // value to get the corresponding address in the process' address space.
   Elf_Addr load_bias;
 
+#if !defined(__LP64__)
   bool has_text_relocations;
+#endif
   bool has_DT_SYMBOLIC;
 
   void CallConstructors();
@@ -195,15 +197,16 @@
   void CallFunction(const char* function_name, linker_function_t function);
 };
 
-extern soinfo libdl_info;
+// The possible DT_FLAGS bits are in
+// http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#dynamic_section
+// but not in the upstream NetBSD <sys/exec_elf.h> header file ours is based on.
+#define DF_ORIGIN 0x1
+#define DF_SYMBOLIC 0x2
+#define DF_TEXTREL 0x4
+#define DF_BIND_NOW 0x8
+#define DF_STATIC_TLS 0x10
 
-// These aren't defined in <sys/exec_elf.h>.
-#ifndef DT_PREINIT_ARRAY
-#define DT_PREINIT_ARRAY   32
-#endif
-#ifndef DT_PREINIT_ARRAYSZ
-#define DT_PREINIT_ARRAYSZ 33
-#endif
+extern soinfo libdl_info;
 
 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
 soinfo* do_dlopen(const char* name, int flags);