Merge changes Id24bf8e3,Ic9d9f2f3

* changes:
  Don't use \r to go to the start of the line.
  Add --build-report.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56e84dc..9f2a22c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,7 @@
 ---
 
  * Updated Clang to build 4393122 based on ???
+ * AArch64 now uses gold by default, matching the other architectures.
 
 Known Issues
 ------------
diff --git a/build/cmake/android.toolchain.cmake b/build/cmake/android.toolchain.cmake
index 3c86f9d..971bb94 100644
--- a/build/cmake/android.toolchain.cmake
+++ b/build/cmake/android.toolchain.cmake
@@ -165,6 +165,8 @@
   set(ANDROID_ABI armeabi-v7a)
 endif()
 if(ANDROID_PLATFORM MATCHES "^android-([0-9]|1[0-3])$")
+  message(WARNING "${ANDROID_PLATFORM} is unsupported. Using minimum supported "
+                  "version android-14")
   set(ANDROID_PLATFORM android-14)
 elseif(ANDROID_PLATFORM STREQUAL android-20)
   set(ANDROID_PLATFORM android-19)
@@ -291,15 +293,19 @@
 list(APPEND ANDROID_LINKER_FLAGS -Wl,--exclude-libs,libatomic.a)
 
 # STL.
+set(USE_NOSTDLIBXX TRUE)
 set(ANDROID_STL_STATIC_LIBRARIES)
 set(ANDROID_STL_SHARED_LIBRARIES)
 if(ANDROID_STL STREQUAL system)
+  set(USE_NOSTDLIBXX FALSE)
   if(NOT "x${ANDROID_CPP_FEATURES}" STREQUAL "x")
     set(ANDROID_STL_STATIC_LIBRARIES supc++)
   endif()
 elseif(ANDROID_STL STREQUAL stlport_static)
+  set(USE_NOSTDLIBXX FALSE)
   set(ANDROID_STL_STATIC_LIBRARIES stlport_static)
 elseif(ANDROID_STL STREQUAL stlport_shared)
+  set(USE_NOSTDLIBXX FALSE)
   set(ANDROID_STL_SHARED_LIBRARIES stlport_shared)
 elseif(ANDROID_STL STREQUAL gnustl_static)
   set(ANDROID_STL_STATIC_LIBRARIES gnustl_static)
@@ -315,6 +321,10 @@
   message(FATAL_ERROR "Invalid Android STL: ${ANDROID_STL}.")
 endif()
 
+if(USE_NOSTDLIBXX AND ANDROID_TOOLCHAIN STREQUAL clang)
+  list(APPEND ANDROID_LINKER_FLAGS "-nostdlib++")
+endif()
+
 # Behavior of CMAKE_SYSTEM_LIBRARY_PATH and CMAKE_LIBRARY_PATH are really weird
 # when CMAKE_SYSROOT is set. The library path is appended to the sysroot even if
 # the library path is an abspath. Using a relative path from the sysroot doesn't
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index 8506410..bbd6b1f 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -178,6 +178,12 @@
 
 LOCAL_LDFLAGS += -Wl,--build-id
 
+ifeq ($(NDK_TOOLCHAIN_VERSION),clang)
+    ifeq ($(filter system stlport_shared stlport_static,$(NDK_APP_STL)),)
+        LOCAL_LDFLAGS += -nostdlib++
+    endif
+endif
+
 #
 # If LOCAL_ALLOW_UNDEFINED_SYMBOLS is not true, the linker will allow the generation
 # of a binary that uses undefined symbols.
diff --git a/checkbuild.py b/checkbuild.py
index 1c83b7f..f9fc55d 100755
--- a/checkbuild.py
+++ b/checkbuild.py
@@ -393,6 +393,7 @@
     def install_arch(self, out_dir, host, arch):
         version = '4.9'
         toolchain = build_support.arch_to_toolchain(arch)
+        triple = build_support.arch_to_triple(arch)
         host_tag = build_support.host_to_tag(host)
 
         install_path = self.get_install_path(out_dir, host, arch)
@@ -403,6 +404,21 @@
 
         ndk.builds.install_directory(toolchain_path, install_path)
 
+        # Replace ld with ld.gold for aarch64. We should get a new binutils
+        # build that has this set by default, but this work until we get a new
+        # binutils build.
+        if arch == 'arm64':
+            exe = '.exe' if host.startswith('windows') else ''
+            ld_bin = os.path.join(install_path, 'bin', triple + '-ld' + exe)
+            gold_bin = os.path.join(
+                install_path, 'bin', triple + '-ld.gold' + exe)
+            os.remove(ld_bin)
+            shutil.copy2(gold_bin, ld_bin)
+
+            ld_arch = os.path.join(install_path, triple, 'bin/ld' + exe)
+            gold_arch = os.path.join(install_path, triple, 'bin/ld.gold' + exe)
+            shutil.copy2(gold_arch, ld_arch)
+
         # Copy the LLVMgold plugin into the binutils plugin directory so ar can
         # use it.
         if host == 'linux':
@@ -1430,8 +1446,11 @@
         install_dir = ndk.paths.get_install_path(out_dir)
         path = os.path.join(install_dir, self.path)
         with open(path, 'w') as source_properties:
+            build = args.build_number
+            if build == 'dev':
+                build = '0'
             version = '{}.{}.{}'.format(
-                ndk.config.major, ndk.config.hotfix, args.build_number)
+                ndk.config.major, ndk.config.hotfix, build)
             if ndk.config.beta > 0:
                 version += '-beta{}'.format(ndk.config.beta)
             source_properties.writelines([
@@ -1681,7 +1700,9 @@
 
     required_package_modules = set(get_all_module_names())
     have_required_modules = required_package_modules <= set(module_names)
-    do_package = have_required_modules if args.package else args.force_package
+    do_package = have_required_modules if args.package else False
+    if args.force_package:
+        do_package = True
 
     # TODO(danalbert): wine?
     # We're building the Windows packages from Linux, so we can't actually run
diff --git a/parse_elfnote.py b/parse_elfnote.py
old mode 100644
new mode 100755
index 0c73b22..b5f557e
--- a/parse_elfnote.py
+++ b/parse_elfnote.py
@@ -15,16 +15,25 @@
 # limitations under the License.
 #
 
+#
+# Dump the contents of the .note.android.ident section, a NOTE section
+# embedded into Android binaries. See here:
+#  - master: ndk/sources/crt/crtbrand.S
+#  - master: bionic/libc/arch-common/bionic/crtbrand.S
+#  - NDK before r14: development/ndk/platforms/common/src/crtbrand.c
+#
+# Note sections can also be dumped with `readelf -n`.
+#
+
+from __future__ import division, print_function
 import argparse
 import logging
+import struct
 import subprocess
 import sys
-from ctypes import c_char
-from ctypes import c_int
-from ctypes import Structure
+from StringIO import StringIO
 
 SEC_NAME = '.note.android.ident'
-ABI_VENDOR = 'Android'
 NDK_RESERVED_SIZE = 64
 
 
@@ -33,21 +42,71 @@
     return logging.getLogger(__name__)
 
 
-class AbiTag(Structure):
-    _fields_ = [('namesz', c_int),
-                ('descsz', c_int),
-                ('type', c_int),
-                ('name', c_char * len(ABI_VENDOR)),
-                ('android_api', c_int),
-                ('ndk_version', c_char * NDK_RESERVED_SIZE),
-                ('ndk_build_number', c_char * NDK_RESERVED_SIZE)]
+def round_up_to_nearest(val, step):
+    """Round an integer, val, to the next multiple of a positive integer,
+    step."""
+    return (val + (step - 1)) // step * step
+
+
+class StructParser(object):
+    def __init__(self, buf):
+        self._sio = StringIO(buf)
+
+    @property
+    def _remaining(self):
+        return self._sio.len - self._sio.pos
+
+    @property
+    def empty(self):
+        return self._remaining == 0
+
+    def read_struct(self, fmt, kind):
+        fmt = struct.Struct(fmt)
+        if self._remaining < fmt.size:
+            sys.exit('error: {} was truncated'.format(kind))
+        return fmt.unpack(self._sio.read(fmt.size))
+
+
+def iterate_notes(sec_data):
+    sec_data = StructParser(sec_data)
+    while not sec_data.empty:
+        (namesz, descsz, kind) = sec_data.read_struct('<III', 'note header')
+        (name, desc) = sec_data.read_struct(
+            '{}s{}s'.format(
+                round_up_to_nearest(namesz, 4),
+                round_up_to_nearest(descsz, 4)),
+            'note body')
+        name = name[:namesz]
+        if len(name) > 0:
+            if name[-1:] == '\0':
+                name = name[:-1]
+            else:
+                logger().warning('note name %s isn\'t NUL-terminated', name)
+        yield name, kind, desc[:descsz]
+
+
+def dump_android_ident_note(note):
+    note = StructParser(note)
+    (android_api,) = note.read_struct('<I', 'note descriptor')
+    print('ABI_ANDROID_API: {}'.format(android_api))
+    if note.empty:
+        return
+    # Binaries generated by NDK r14 and later have these extra fields. Platform
+    # binaries and binaries generated by older NDKs don't.
+    (ndk_version, ndk_build_number) = note.read_struct(
+        '{}s{}s'.format(NDK_RESERVED_SIZE, NDK_RESERVED_SIZE),
+        'note descriptor')
+    print('ABI_NDK_VERSION: {}'.format(ndk_version.rstrip('\0')))
+    print('ABI_NDK_BUILD_NUMBER: {}'.format(ndk_build_number.rstrip('\0')))
+    if not note.empty:
+        logger().warning('excess data at end of descriptor')
 
 
 # Get the offset to a section from the output of readelf
 def get_section_pos(sec_name, file_path):
     cmd = ['readelf', '--sections', '-W', file_path]
     output = subprocess.check_output(cmd)
-    lines = output.split('\n')
+    lines = output.splitlines()
     for line in lines:
         logger().debug('Checking line for "%s": %s', sec_name, line)
         # Looking for a line like the following (all whitespace of unknown
@@ -58,29 +117,17 @@
         # The only column that might have internal whitespace is the first one.
         # Since we don't care about it, remove the head of the string until the
         # closing bracket, then split.
-        if sec_name not in line:
-            continue
         if ']' not in line:
             continue
-        line = line[line.index(']') + 1:].strip()
+        line = line[line.index(']') + 1:]
 
         sections = line.split()
-        if len(sections) < 5 or sec_name not in sections[0]:
-            logger().debug('Did not find "%s" in %s', sec_name, sections[0])
-            sys.exit('Failed to get offset of {}'.format(sec_name))
-        addr = int(sections[2], 16)
+        if len(sections) < 5 or sec_name != sections[0]:
+            continue
         off = int(sections[3], 16)
-        return off
-    sys.exit('Failed to find section: {}'.format(sec_name))
-
-
-def print_info(tag):
-    print '----------ABI INFO----------'
-    print 'ABI_NOTETYPE: {}'.format(tag.type)
-    print 'ABI_VENDOR: {}'.format(tag.name)
-    print 'ABI_ANDROID_API: {}'.format(tag.android_api)
-    print 'ABI_NDK_VERSION: {}'.format(tag.ndk_version)
-    print 'ABI_NDK_BUILD_NUMBER: {}'.format(tag.ndk_build_number)
+        size = int(sections[4], 16)
+        return (off, size)
+    sys.exit('error: failed to find section: {}'.format(sec_name))
 
 
 def parse_args():
@@ -106,11 +153,22 @@
     file_path = args.file_path
 
     with open(file_path, "rb") as obj_file:
-        pos = get_section_pos(SEC_NAME, file_path)
-        obj_file.seek(pos)
-        tag = AbiTag()
-        obj_file.readinto(tag)
-        print_info(tag)
+        (sec_off, sec_size) = get_section_pos(SEC_NAME, file_path)
+
+        obj_file.seek(sec_off)
+        sec_data = obj_file.read(sec_size)
+        if len(sec_data) != sec_size:
+            sys.exit('error: could not read {} section'.format(SEC_NAME))
+
+        print('----------ABI INFO----------')
+        if len(sec_data) == 0:
+            logger().warning('%s section is empty', SEC_NAME)
+        for (name, kind, desc) in iterate_notes(sec_data):
+            if (name, kind) == ('Android', 1):
+                dump_android_ident_note(desc)
+            else:
+                logger().warning('unrecognized note (name %s, type %d)',
+                    repr(name), kind)
 
 
 if __name__ == '__main__':
diff --git a/tests/build/flto/CMakeLists.txt b/tests/build/flto/CMakeLists.txt
index 7fe955f..eb6575e 100644
--- a/tests/build/flto/CMakeLists.txt
+++ b/tests/build/flto/CMakeLists.txt
@@ -3,10 +3,6 @@
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
 
-if(NOT MIPS AND NOT MIPS64)
-  SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
-endif()
-
 add_library(bar STATIC jni/bar.c)
 
 add_executable(flto jni/flto.c)
diff --git a/tests/build/flto/jni/Android.mk b/tests/build/flto/jni/Android.mk
index cc65614..f629581 100644
--- a/tests/build/flto/jni/Android.mk
+++ b/tests/build/flto/jni/Android.mk
@@ -13,11 +13,4 @@
 LOCAL_CFLAGS += -flto
 LOCAL_LDFLAGS += -flto
 
-# Clang LTO is only supported with gold. ARM64 still uses bfd by default, so
-# make sure this test uses gold when we're using clang.
-ifneq ($(filter clang%,$(NDK_TOOLCHAIN_VERSION)),)
-LOCAL_CFLAGS += -fuse-ld=gold
-LOCAL_LDFLAGS += -fuse-ld=gold
-endif
-
 include $(BUILD_EXECUTABLE)
diff --git a/tests/build/flto/test_config.py b/tests/build/flto/test_config.py
deleted file mode 100644
index a2d8363..0000000
--- a/tests/build/flto/test_config.py
+++ /dev/null
@@ -1,20 +0,0 @@
-# Shut up a warning about us not being a real package.
-from __future__ import absolute_import
-import platform
-
-
-def build_unsupported(abi, _platform, toolchain):
-    # Clang does LTO via gold plugin, but gold doesn't support MIPS yet.
-    if toolchain == 'clang' and abi.startswith('mips'):
-        return '{} {}'.format(toolchain, abi)
-
-    return None
-
-
-def build_broken(_abi, _platform, toolchain):
-    # We don't support LTO on Windows.
-    if platform.system() == 'Windows' and toolchain == 'clang':
-        bug = 'https://github.com/android-ndk/ndk/issues/251'
-        return '{} {}'.format(platform.system(), toolchain), bug
-
-    return None, None