ASR_BASE

Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/tools/mklibs/patches/001-compile.patch b/tools/mklibs/patches/001-compile.patch
new file mode 100644
index 0000000..09b831a
--- /dev/null
+++ b/tools/mklibs/patches/001-compile.patch
@@ -0,0 +1,8 @@
+--- a/configure.ac
++++ b/configure.ac
+@@ -1,4 +1,4 @@
+-AC_INIT([mklibs],m4_esyscmd(dpkg-parsechangelog | perl -ne 'print $1 if m/^Version: (.*)$/;'))
++AC_INIT([mklibs],m4_esyscmd([head -n1 debian/changelog | awk -F'[\\\\(\\\\)]' '{ print $2 }' | xargs -I{} echo -n {}]))
+ AM_INIT_AUTOMAKE([foreign no-define])
+ AC_CONFIG_HEADERS([config.h])
+ AM_MAINTAINER_MODE
diff --git a/tools/mklibs/patches/002-disable_symbol_checks.patch b/tools/mklibs/patches/002-disable_symbol_checks.patch
new file mode 100644
index 0000000..96ca7a5
--- /dev/null
+++ b/tools/mklibs/patches/002-disable_symbol_checks.patch
@@ -0,0 +1,20 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -533,7 +533,7 @@ while 1:
+         # No progress in last pass. Verify all remaining symbols are weak.
+         for name in unresolved:
+             if not needed_symbols[name].weak:
+-                raise Exception("Unresolvable symbol %s" % name)
++                print("WARNING: Unresolvable symbol %s" % name)
+         break
+ 
+     previous_pass_unresolved = unresolved
+@@ -568,7 +568,7 @@ while 1:
+     for name in needed_symbols:
+         if not name in symbol_provider:
+             if not needed_symbols[name].weak:
+-                raise Exception("No library provides non-weak %s" % name)
++                print("WARNING: Unresolvable symbol %s" % name)
+         else:
+             lib = symbol_provider[name]
+             library_symbols_used[lib].add(library_symbols[lib][name])
diff --git a/tools/mklibs/patches/003-no_copy.patch b/tools/mklibs/patches/003-no_copy.patch
new file mode 100644
index 0000000..bc1ae8d
--- /dev/null
+++ b/tools/mklibs/patches/003-no_copy.patch
@@ -0,0 +1,50 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -472,7 +472,7 @@ while 1:
+     passnr = passnr + 1
+     # Gather all already reduced libraries and treat them as objects as well
+     small_libs = []
+-    for lib in regexpfilter(os.listdir(dest_path), "(.*-so-stripped)$"):
++    for lib in regexpfilter(os.listdir(dest_path), "(.*-so)$"):
+         obj = dest_path + "/" + lib
+         small_libs.append(obj)
+         inode = os.stat(obj)[ST_INO]
+@@ -588,12 +588,7 @@ while 1:
+         if not so_file:
+             sys.exit("File not found:" + library)
+         pic_file = find_pic(library)
+-        if not pic_file:
+-            # No pic file, so we have to use the .so file, no reduction
+-            debug(DEBUG_VERBOSE, "No pic file found for", so_file, "; copying")
+-            command(target + "objcopy", "--strip-unneeded -R .note -R .comment",
+-                    so_file, dest_path + "/" + so_file_name + "-so-stripped")
+-        else:
++        if pic_file:
+             # we have a pic file, recompile
+             debug(DEBUG_SPAM, "extracting from:", pic_file, "so_file:", so_file)
+             soname = extract_soname(so_file)
+@@ -636,22 +631,14 @@ while 1:
+             cmd.append(library_depends_gcc_libnames(so_file))
+             command(target + "gcc", *cmd)
+ 
+-            # strip result
+-            command(target + "objcopy", "--strip-unneeded -R .note -R .comment",
+-                      dest_path + "/" + so_file_name + "-so",
+-                      dest_path + "/" + so_file_name + "-so-stripped")
+             ## DEBUG
+             debug(DEBUG_VERBOSE, so_file, "\t", str(os.stat(so_file)[ST_SIZE]))
+             debug(DEBUG_VERBOSE, dest_path + "/" + so_file_name + "-so", "\t",
+                   str(os.stat(dest_path + "/" + so_file_name + "-so")[ST_SIZE]))
+-            debug(DEBUG_VERBOSE, dest_path + "/" + so_file_name + "-so-stripped",
+-                  "\t", str(os.stat(dest_path + "/" + so_file_name + "-so-stripped")[ST_SIZE]))
+ 
+ # Finalising libs and cleaning up
+-for lib in regexpfilter(os.listdir(dest_path), "(.*)-so-stripped$"):
+-    os.rename(dest_path + "/" + lib + "-so-stripped", dest_path + "/" + lib)
+-for lib in regexpfilter(os.listdir(dest_path), "(.*-so)$"):
+-    os.remove(dest_path + "/" + lib)
++for lib in regexpfilter(os.listdir(dest_path), "(.*)-so$"):
++    os.rename(dest_path + "/" + lib + "-so", dest_path + "/" + lib)
+ 
+ # Canonicalize library names.
+ for lib in sorted(regexpfilter(os.listdir(dest_path), "(.*so[.\d]*)$")):
diff --git a/tools/mklibs/patches/004-libpthread_link.patch b/tools/mklibs/patches/004-libpthread_link.patch
new file mode 100644
index 0000000..3a630ea
--- /dev/null
+++ b/tools/mklibs/patches/004-libpthread_link.patch
@@ -0,0 +1,28 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -106,14 +106,14 @@ def library_depends(obj):
+ 
+ # Return a list of libraries the passed objects depend on. The
+ # libraries are in "-lfoo" format suitable for passing to gcc.
+-def library_depends_gcc_libnames(obj):
++def library_depends_gcc_libnames(obj, soname):
+     if not os.access(obj, os.F_OK):
+         raise Exception("Cannot find lib: " + obj)
+     libs = library_depends(obj)
+     ret = []
+     for i in libs:
+         match = re.match("^(((?P<ld>ld\S*)|(lib(?P<lib>\S+))))\.so.*$", i)
+-        if match:
++        if match and not soname in ("libpthread.so.0"):
+             if match.group('ld'):
+                 ret.append(find_lib(match.group(0)))
+             elif match.group('lib'):
+@@ -628,7 +628,7 @@ while 1:
+             cmd.extend(extra_flags)
+             cmd.append("-lgcc")
+             cmd.extend(["-L%s" % a for a in [dest_path] + [sysroot + b for b in lib_path if sysroot == "" or b not in ("/" + libdir + "/", "/usr/" + libdir + "/")]])
+-            cmd.append(library_depends_gcc_libnames(so_file))
++            cmd.append(library_depends_gcc_libnames(so_file, soname))
+             command(target + "gcc", *cmd)
+ 
+             ## DEBUG
diff --git a/tools/mklibs/patches/005-duplicate_syms.patch b/tools/mklibs/patches/005-duplicate_syms.patch
new file mode 100644
index 0000000..2627203
--- /dev/null
+++ b/tools/mklibs/patches/005-duplicate_syms.patch
@@ -0,0 +1,35 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -540,7 +540,6 @@ while 1:
+ 
+     library_symbols = {}
+     library_symbols_used = {}
+-    symbol_provider = {}
+ 
+     # WORKAROUND: Always add libgcc on old-abi arm
+     header = elf_header(find_lib(libraries.copy().pop()))
+@@ -558,20 +557,13 @@ while 1:
+         library_symbols_used[library] = set()
+         for symbol in symbols:
+             for name in symbol.base_names():
+-                if name in symbol_provider:
+-                    debug(DEBUG_SPAM, "duplicate symbol %s in %s and %s" % (symbol, symbol_provider[name], library))
+-                else:
+-                    library_symbols[library][name] = symbol
+-                    symbol_provider[name] = library
++                library_symbols[library][name] = symbol
+ 
+     # which symbols are actually used from each lib
+     for name in needed_symbols:
+-        if not name in symbol_provider:
+-            if not needed_symbols[name].weak:
+-                print("WARNING: Unresolvable symbol %s" % name)
+-        else:
+-            lib = symbol_provider[name]
+-            library_symbols_used[lib].add(library_symbols[lib][name])
++        for lib in libraries:
++            if name in library_symbols[lib]:
++                library_symbols_used[lib].add(library_symbols[lib][name])
+ 
+     # reduce libraries
+     for library in sorted(libraries):
diff --git a/tools/mklibs/patches/007-gc_sections.patch b/tools/mklibs/patches/007-gc_sections.patch
new file mode 100644
index 0000000..3d098ae
--- /dev/null
+++ b/tools/mklibs/patches/007-gc_sections.patch
@@ -0,0 +1,11 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -611,7 +611,7 @@ while 1:
+             # compile in only used symbols
+             cmd = []
+             cmd.extend(gcc_options)
+-            cmd.append("-nostdlib -nostartfiles -shared -Wl,-soname=" + soname)
++            cmd.append("-nostdlib -nostartfiles -shared -Wl,--gc-sections -Wl,-soname=" + soname)
+             cmd.extend(["-u%s" % a.linker_name() for a in symbols])
+             cmd.extend(["-o", dest_path + "/" + so_file_name + "-so"])
+             cmd.extend(extra_pre_obj)
diff --git a/tools/mklibs/patches/008-uclibc_libgcc_link.patch b/tools/mklibs/patches/008-uclibc_libgcc_link.patch
new file mode 100644
index 0000000..a221278
--- /dev/null
+++ b/tools/mklibs/patches/008-uclibc_libgcc_link.patch
@@ -0,0 +1,37 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -113,11 +113,8 @@ def library_depends_gcc_libnames(obj, so
+     ret = []
+     for i in libs:
+         match = re.match("^(((?P<ld>ld\S*)|(lib(?P<lib>\S+))))\.so.*$", i)
+-        if match and not soname in ("libpthread.so.0"):
+-            if match.group('ld'):
+-                ret.append(find_lib(match.group(0)))
+-            elif match.group('lib'):
+-                ret.append('-l%s' % match.group('lib'))
++        if match:
++            ret.append(find_lib(match.group(0)))
+     return ' '.join(ret)
+ 
+ class Symbol(object):
+@@ -593,6 +590,7 @@ while 1:
+             extra_flags = []
+             extra_pre_obj = []
+             extra_post_obj = []
++            libgcc_link = find_lib("libgcc_s.so.1")
+ 
+             symbols.update(library_symbols_used[library])
+ 
+@@ -618,9 +616,10 @@ while 1:
+             cmd.append(pic_file)
+             cmd.extend(extra_post_obj)
+             cmd.extend(extra_flags)
+-            cmd.append("-lgcc")
+             cmd.extend(["-L%s" % a for a in [dest_path] + [sysroot + b for b in lib_path if sysroot == "" or b not in ("/" + libdir + "/", "/usr/" + libdir + "/")]])
+-            cmd.append(library_depends_gcc_libnames(so_file, soname))
++            if soname != "libgcc_s.so.1":
++                cmd.append(library_depends_gcc_libnames(so_file, soname))
++                cmd.append(libgcc_link)
+             command(target + "gcc", *cmd)
+ 
+             ## DEBUG
diff --git a/tools/mklibs/patches/010-remove_STT_GNU_IFUNC.patch b/tools/mklibs/patches/010-remove_STT_GNU_IFUNC.patch
new file mode 100644
index 0000000..6bae8c7
--- /dev/null
+++ b/tools/mklibs/patches/010-remove_STT_GNU_IFUNC.patch
@@ -0,0 +1,20 @@
+--- a/src/mklibs-readelf/main.cpp
++++ b/src/mklibs-readelf/main.cpp
+@@ -84,7 +84,7 @@ static void process_symbols_provided (co
+       continue;
+     if (shndx == SHN_UNDEF || shndx == SHN_ABS)
+       continue;
+-    if (type != STT_NOTYPE && type != STT_OBJECT && type != STT_FUNC && type != STT_GNU_IFUNC && type != STT_COMMON && type != STT_TLS)
++    if (type != STT_NOTYPE && type != STT_OBJECT && type != STT_FUNC && type != STT_COMMON && type != STT_TLS)
+       continue;
+     if (!name.size())
+       continue;
+@@ -115,7 +115,7 @@ static void process_symbols_undefined (c
+       continue;
+     if (shndx != SHN_UNDEF)
+       continue;
+-    if (type != STT_NOTYPE && type != STT_OBJECT && type != STT_FUNC && type != STT_GNU_IFUNC && type != STT_COMMON && type != STT_TLS)
++    if (type != STT_NOTYPE && type != STT_OBJECT && type != STT_FUNC && type != STT_COMMON && type != STT_TLS)
+       continue;
+     if (!name.size())
+       continue;
diff --git a/tools/mklibs/patches/011-remove_multiarch.patch b/tools/mklibs/patches/011-remove_multiarch.patch
new file mode 100644
index 0000000..8c0bfdf
--- /dev/null
+++ b/tools/mklibs/patches/011-remove_multiarch.patch
@@ -0,0 +1,10 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -269,6 +269,7 @@ def extract_soname(so_file):
+     return ""
+ 
+ def multiarch(paths):
++    return paths
+     devnull = open('/dev/null', 'w')
+     dpkg_architecture = subprocess.Popen(
+         ['dpkg-architecture', '-qDEB_HOST_MULTIARCH'],
diff --git a/tools/mklibs/patches/100-apply-2to3.patch b/tools/mklibs/patches/100-apply-2to3.patch
new file mode 100644
index 0000000..dcacbab
--- /dev/null
+++ b/tools/mklibs/patches/100-apply-2to3.patch
@@ -0,0 +1,154 @@
+--- a/src/mklibs
++++ b/src/mklibs
+@@ -57,17 +57,17 @@ debuglevel = DEBUG_NORMAL
+ 
+ def debug(level, *msg):
+     if debuglevel >= level:
+-        print(string.join(msg))
++        print(' '.join(msg))
+ 
+ # return a list of lines of output of the command
+ def command(command, *args):
+-    debug(DEBUG_SPAM, "calling", command, string.join(args))
++    debug(DEBUG_SPAM, "calling", command, ' '.join(args))
+     pipe = os.popen(command + ' ' + ' '.join(args), 'r')
+     output = pipe.read().strip()
+     status = pipe.close() 
+     if status is not None and os.WEXITSTATUS(status) != 0:
+         print("Command failed with status", os.WEXITSTATUS(status),  ":", \
+-               command, string.join(args))
++               command, ' '.join(args))
+         print("With output:", output)
+         sys.exit(1)
+     return [i for i in output.split('\n') if i]
+@@ -296,7 +296,7 @@ def usage(was_err):
+     print("Make a set of minimal libraries for FILE(s, file=outfd) in DEST.", file=outfd)
+     print("" , file=outfd)
+     print("  -d, --dest-dir DIRECTORY     create libraries in DIRECTORY", file=outfd)
+-    print("  -D, --no-default-lib         omit default libpath (", ':'.join(default_lib_path, file=outfd), ", file=outfd)", file=outfd)
++    print("  -D, --no-default-lib         omit default libpath (", ':'.join(default_lib_path), ")", file=outfd)
+     print("  -L DIRECTORY[:DIRECTORY]...  add DIRECTORY(s, file=outfd) to the library search path", file=outfd)
+     print("  -l LIBRARY                   add LIBRARY always", file=outfd)
+     print("      --ldlib LDLIB            use LDLIB for the dynamic linker", file=outfd)
+@@ -372,7 +372,7 @@ for opt, arg in optlist:
+         if debuglevel < DEBUG_SPAM:
+             debuglevel = debuglevel + 1
+     elif opt == "-L":
+-        lib_path.extend(string.split(arg, ":"))
++        lib_path.extend(arg.split(":"))
+     elif opt in ("-d", "--dest-dir"):
+         dest_path = arg
+     elif opt in ("-D", "--no-default-lib"):
+@@ -391,7 +391,7 @@ for opt, arg in optlist:
+     elif opt in ("-l",):
+         force_libs.append(arg)
+     elif opt == "--gcc-options":
+-        gcc_options.extend(string.split(arg, " "))
++        gcc_options.extend(arg.split(" "))
+     elif opt == "--libdir":
+         libdir = arg
+     elif opt in ("--help", "-h"):
+@@ -419,17 +419,17 @@ if ldlib == "LDLIB":
+ objects = {}  # map from inode to filename
+ for prog in proglist:
+     inode = os.stat(prog)[ST_INO]
+-    if objects.has_key(inode):
++    if inode in objects:
+         debug(DEBUG_SPAM, prog, "is a hardlink to", objects[inode])
+     elif so_pattern.match(prog):
+         debug(DEBUG_SPAM, prog, "is a library")
+-    elif script_pattern.match(open(prog).read(256)):
++    elif script_pattern.match(open(prog, 'r', encoding='iso-8859-1').read(256)):
+         debug(DEBUG_SPAM, prog, "is a script")
+     else:
+         objects[inode] = prog
+ 
+ if not ldlib:
+-    for obj in objects.values():
++    for obj in list(objects.values()):
+         output = command("mklibs-readelf", "--print-interp", obj)
+         if output:
+             ldlib = output.pop()
+@@ -462,9 +462,9 @@ previous_pass_unresolved = set()
+ while 1:
+     debug(DEBUG_NORMAL, "I: library reduction pass", str(passnr))
+     if debuglevel >= DEBUG_VERBOSE:
+-        print("Objects:",)
+-        for obj in sorted([x[string.rfind(x, '/') + 1:] for x in objects.values()]):
+-            print(obj,)
++        print("Objects:", end=' ')
++        for obj in sorted([x[x.rfind('/') + 1:] for x in list(objects.values())]):
++            print(obj, end=' ')
+         print()
+ 
+     passnr = passnr + 1
+@@ -474,7 +474,7 @@ while 1:
+         obj = dest_path + "/" + lib
+         small_libs.append(obj)
+         inode = os.stat(obj)[ST_INO]
+-        if objects.has_key(inode):
++        if inode in objects:
+             debug(DEBUG_SPAM, obj, "is hardlink to", objects[inode])
+         else:
+             objects[inode] = obj
+@@ -504,7 +504,7 @@ while 1:
+     present_symbols = {}
+     checked_libs = small_libs
+     checked_libs.extend(available_libs)
+-    checked_libs.append(ldlib)
++    checked_libs.append(sysroot + "/" + ldlib)
+     for lib in checked_libs:
+         for symbol in provided_symbols(lib):
+             debug(DEBUG_SPAM, "present_symbols adding %s" % symbol)
+--- a/src/mklibs-copy
++++ b/src/mklibs-copy
+@@ -159,7 +159,7 @@ if include_default_lib_path:
+ objects = {}  # map from inode to filename
+ for prog in proglist:
+     inode = os.stat(prog)[ST_INO]
+-    if objects.has_key(inode):
++    if inode in objects:
+         logger.debug("%s is a hardlink to %s", prog, objects[inode])
+     elif so_pattern.match(prog):
+         logger.debug("%s is a library", prog)
+@@ -169,7 +169,7 @@ for prog in proglist:
+         logger.debug("%s is no ELF", prog)
+ 
+ if not ldlib:
+-    for obj in objects.values():
++    for obj in list(objects.values()):
+         output = command("mklibs-readelf", "-i", obj)
+         for x in output:
+                 ldlib = x
+@@ -182,7 +182,7 @@ if not ldlib:
+ logger.info('Using %s as dynamic linker', ldlib)
+ 
+ # Check for rpaths
+-for obj in objects.values():
++for obj in list(objects.values()):
+     rpath_val = rpath(obj)
+     if rpath_val:
+         if root:
+@@ -208,18 +208,18 @@ while 1:
+         obj = dest_path + "/" + lib
+         small_libs.append(obj)
+         inode = os.stat(obj)[ST_INO]
+-        if objects.has_key(inode):
++        if inode in objects:
+             logger.debug("%s is hardlink to %s", obj, objects[inode])
+         else:
+             objects[inode] = obj
+ 
+-    for obj in objects.values():
++    for obj in list(objects.values()):
+         small_libs.append(obj)
+ 
+-    logger.verbose('Objects: %r', ' '.join([i[i.rfind('/') + 1:] for i in objects.itervalues()]))
++    logger.verbose('Objects: %r', ' '.join([i[i.rfind('/') + 1:] for i in objects.values()]))
+ 
+     libraries = set()
+-    for obj in objects.values():
++    for obj in list(objects.values()):
+         libraries.update(library_depends(obj))
+ 
+     if libraries == previous_pass_libraries: