am cb9ba3c8: Reconcile with jb-release

* commit 'cb9ba3c88a97f34784559a55317ff5970d4c4dba':
  Print the corrupted address passed to free().
diff --git a/libc/Android.mk b/libc/Android.mk
index 6c535dc..0dbc0be 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -559,13 +559,13 @@
     # This flag must be added for x86 targets, but not for ARM
     libc_crt_target_so_cflags += -fPIC
 endif
-GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_so.o
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
 $(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_so.S
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
 ALL_GENERATED_SOURCES += $(GEN)
 
-GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_so.o
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
 $(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend_so.S
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
@@ -573,13 +573,13 @@
 endif # TARGET_ARCH == x86 || TARGET_ARCH == arm
 
 
-GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_static.o
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static.o
 $(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_static.S
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
 ALL_GENERATED_SOURCES += $(GEN)
 
-GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtbegin_dynamic.o
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic.o
 $(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_dynamic.S
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
@@ -588,7 +588,7 @@
 
 # We rename crtend.o to crtend_android.o to avoid a
 # name clash between gcc and bionic.
-GEN := $(TARGET_OUT_STATIC_LIBRARIES)/crtend_android.o
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_android.o
 $(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend.S
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
@@ -678,12 +678,6 @@
 # see libc/bionic/pthread_debug.c for details
 
 LOCAL_CFLAGS := $(libc_common_cflags) -DPTHREAD_DEBUG -DPTHREAD_DEBUG_ENABLED=0
-
-ifeq ($(TARGET_ARCH),arm)
-# TODO: At some point, we need to remove this custom linker script.
-LOCAL_LDFLAGS := -Wl,-T,$(BUILD_SYSTEM)/armelf.xsc
-endif
-
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 
 LOCAL_SRC_FILES := \
diff --git a/linker/linker.c b/linker/linker.c
index eb9cc3e..739d3e4 100644
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -640,10 +640,6 @@
     return -1;
 }
 
-/* temporary space for holding the first page of the shared lib
- * which contains the elf header (with the pht). */
-static unsigned char __header[PAGE_SIZE];
-
 typedef struct {
     long mmap_addr;
     char tag[4]; /* 'P', 'R', 'E', ' ' */
@@ -1086,29 +1082,34 @@
     unsigned ext_sz;
     unsigned req_base;
     const char *bname;
+    struct stat sb;
     soinfo *si = NULL;
-    Elf32_Ehdr *hdr;
+    Elf32_Ehdr *hdr = MAP_FAILED;
 
-    if(fd == -1) {
+    if (fd == -1) {
         DL_ERR("Library '%s' not found", name);
         return NULL;
     }
 
-    /* We have to read the ELF header to figure out what to do with this image
+    /* We have to read the ELF header to figure out what to do with this image.
+     * Map entire file for this.  There won't be much difference in physical
+     * memory usage or performance.
      */
-    if (lseek(fd, 0, SEEK_SET) < 0) {
-        DL_ERR("lseek() failed!");
+    if (fstat(fd, &sb) < 0) {
+        DL_ERR("%5d fstat() failed! (%s)", pid, strerror(errno));
         goto fail;
     }
 
-    if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
-        DL_ERR("read() failed!");
+    hdr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+    if (hdr == MAP_FAILED) {
+        DL_ERR("%5d failed to mmap() header of '%s' (%s)",
+               pid, name, strerror(errno));
         goto fail;
     }
 
     /* Parse the ELF header and get the size of the memory footprint for
      * the library */
-    req_base = get_lib_extents(fd, name, &__header[0], &ext_sz);
+    req_base = get_lib_extents(fd, name, hdr, &ext_sz);
     if (req_base == (unsigned)-1)
         goto fail;
     TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name,
@@ -1138,22 +1139,20 @@
           pid, name, (void *)si->base, (unsigned) ext_sz);
 
     /* Now actually load the library's segments into right places in memory */
-    if (load_segments(fd, &__header[0], si) < 0) {
+    if (load_segments(fd, hdr, si) < 0) {
         goto fail;
     }
 
-    /* this might not be right. Technically, we don't even need this info
-     * once we go through 'load_segments'. */
-    hdr = (Elf32_Ehdr *)si->base;
     si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff);
     si->phnum = hdr->e_phnum;
-    /**/
 
+    munmap(hdr, sb.st_size);
     close(fd);
     return si;
 
 fail:
     if (si) free_info(si);
+    if (hdr != MAP_FAILED) munmap(hdr, sb.st_size);
     close(fd);
     return NULL;
 }
@@ -1678,7 +1677,7 @@
 
     if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
         /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for
-         * linkage info if this is the executable or the linker itself. 
+         * linkage info if this is the executable or the linker itself.
          * If this was a dynamic lib, that would have been done at load time.
          *
          * TODO: It's unfortunate that small pieces of this are