extract-utils: Enable elf checks on elf targets

This involves extracting shared libraries from the target

Change-Id: I58645db830f61627e713c613562f6921113e9ada
diff --git a/extract_utils.sh b/extract_utils.sh
index cac51b5..d58e6c0 100644
--- a/extract_utils.sh
+++ b/extract_utils.sh
@@ -55,11 +55,13 @@
     fi
 
     export BINARIES_LOCATION="$ANDROID_ROOT"/prebuilts/extract-tools/${HOST}-x86/bin
+    export CLANG_BINUTILS="$ANDROID_ROOT"/prebuilts/clang/host/${HOST}-x86/llvm-binutils-stable
 
     export SIMG2IMG="$BINARIES_LOCATION"/simg2img
     export LPUNPACK="$BINARIES_LOCATION"/lpunpack
     export OTA_EXTRACTOR="$BINARIES_LOCATION"/ota_extractor
     export SIGSCAN="$BINARIES_LOCATION"/SigScan
+    export OBJDUMP="$CLANG_BINUTILS"/llvm-objdump
 
     for version in 0_8 0_9 0_17_2; do
         export PATCHELF_${version}="$BINARIES_LOCATION"/patchelf-"${version}"
@@ -401,6 +403,7 @@
     local SRC=
     local STEM=
     local OVERRIDEPKG=
+    local DISABLE_CHECKELF=
 
     [ "$COMMON" -eq 1 ] && local VENDOR="${VENDOR_COMMON:-$VENDOR}"
 
@@ -421,10 +424,13 @@
 
         # Allow overriding module name
         STEM=
+        DISABLE_CHECKELF=
         for ARG in "${ARGS[@]}"; do
             if [[ "$ARG" =~ "MODULE" ]]; then
                 STEM="$PKGNAME"
                 PKGNAME=${ARG#*=}
+            elif [[ "$ARG" == "DISABLE_CHECKELF" ]]; then
+                DISABLE_CHECKELF="true"
             fi
         done
 
@@ -458,24 +464,38 @@
             if [ "$EXTRA" = "both" ]; then
                 printf '\t\tandroid_arm: {\n'
                 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
+                if [ -z "$DISABLE_CHECKELF" ]; then
+                    printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
+                fi
                 printf '\t\t},\n'
                 printf '\t\tandroid_arm64: {\n'
                 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
+                if [ -z "$DISABLE_CHECKELF" ]; then
+                    printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib64/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
+                fi
                 printf '\t\t},\n'
             elif [ "$EXTRA" = "64" ]; then
                 printf '\t\tandroid_arm64: {\n'
                 printf '\t\t\tsrcs: ["%s/lib64/%s"],\n' "$SRC" "$FILE"
+                if [ -z "$DISABLE_CHECKELF" ]; then
+                    printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib64/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
+                fi
                 printf '\t\t},\n'
             else
                 printf '\t\tandroid_arm: {\n'
                 printf '\t\t\tsrcs: ["%s/lib/%s"],\n' "$SRC" "$FILE"
+                if [ -z "$DISABLE_CHECKELF" ]; then
+                    printf '\t\t\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/lib/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
+                fi
                 printf '\t\t},\n'
             fi
             printf '\t},\n'
             if [ "$EXTRA" != "none" ]; then
                 printf '\tcompile_multilib: "%s",\n' "$EXTRA"
             fi
-            printf '\tcheck_elf_files: false,\n'
+            if [ ! -z "$DISABLE_CHECKELF" ]; then
+                printf '\tcheck_elf_files: false,\n'
+            fi
         elif [ "$CLASS" = "APEX" ]; then
             printf 'prebuilt_apex {\n'
             printf '\tname: "%s",\n' "$PKGNAME"
@@ -526,6 +546,11 @@
             printf '\tsrc: "%s/etc/%s",\n' "$SRC" "$FILE"
             printf '\tfilename_from_src: true,\n'
         elif [ "$CLASS" = "EXECUTABLES" ]; then
+            if ! objdump -a "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" 2>/dev/null |grep -c 'file format elf' > /dev/null; then
+                # This is not an elf file, assume it's a shell script that doesn't have an extension
+                # Setting extension here does not change the target extension, only the module type
+                EXTENSION="sh"
+            fi
             if [ "$EXTENSION" = "sh" ]; then
                 printf 'sh_binary {\n'
             else
@@ -535,7 +560,11 @@
             printf '\towner: "%s",\n' "$VENDOR"
             if [ "$EXTENSION" != "sh" ]; then
                 printf '\tsrcs: ["%s/bin/%s"],\n' "$SRC" "$FILE"
-                printf '\tcheck_elf_files: false,\n'
+                if [ -z "$DISABLE_CHECKELF" ]; then
+                    printf '\tshared_libs: [%s],\n' "$(basename -s .so $(${OBJDUMP} -x "$ANDROID_ROOT"/"$OUTDIR"/"$SRC"/bin/"$FILE" 2>/dev/null |grep NEEDED) 2>/dev/null |grep -v ^NEEDED$ |sed 's/-3.9.1//g' |sed 's/\(.*\)/"\1",/g' |tr '\n' ' ')"
+                else
+                    printf '\tcheck_elf_files: false,\n'
+                fi
                 printf '\tstrip: {\n'
                 printf '\t\tnone: true,\n'
                 printf '\t},\n'
@@ -1116,6 +1145,18 @@
 
     cat << EOF >> "$ANDROIDBP"
 soong_namespace {
+	imports: [
+EOF
+
+    if [ ! -z "$DEVICE_COMMON" -a "$COMMON" -ne 1 ]; then
+        cat << EOF >> "$ANDROIDBP"
+		"vendor/${VENDOR_COMMON:-$VENDOR}/$DEVICE_COMMON",
+EOF
+    fi
+    vendor_imports "$ANDROIDBP"
+
+    cat << EOF >> "$ANDROIDBP"
+	],
 }
 
 EOF
@@ -1217,6 +1258,8 @@
         elif suffix_match_file ".apex" "$(src_file "$SPEC")" || \
              suffix_match_file ".apk" "$(src_file "$SPEC")" || \
              suffix_match_file ".jar" "$(src_file "$SPEC")" || \
+             suffix_match_file ".so" "$(src_file "$SPEC")" || \
+             [[ "$SPEC" == *"bin/"* ]] || \
              [[ "$SPEC" == *"etc/vintf/manifest/"* ]]; then
             PRODUCT_PACKAGES_LIST+=("$SPEC")
             PRODUCT_PACKAGES_HASHES+=("$HASH")
@@ -1531,6 +1574,14 @@
     :
 }
 
+# To be overridden by device-level extract-files.sh
+# Parameters:
+#   $1: Path to vendor Android.bp
+#
+function vendor_imports() {
+    :
+}
+
 #
 # prepare_images:
 #