simpleperf: link simpleperf_ndk with dynamic libdexfile

This change let simpleperf_ndk always use libdexfile.so on the device
to read symbols from DEX files running on that device. Consequently,
libdexfile in AOSP can drop support for the old CDEX file format.

Also replace the CDEX file in testdata with a DEX file.

Bug: 369208474
Test: run simpleperf_ndk manually on Android S+
Test: run simpleperF_unit_test
Change-Id: I85026f324058a33f41ca0dc6647fc5961a850333
diff --git a/simpleperf/Android.bp b/simpleperf/Android.bp
index 403f143..ac41239 100644
--- a/simpleperf/Android.bp
+++ b/simpleperf/Android.bp
@@ -144,10 +144,8 @@
         "libziparchive",
         "libzstd",
     ],
+    stl: "libc++_static",
     target: {
-        host: {
-            stl: "libc++_static",
-        },
         linux: {
             static_libs: [
                 "libunwindstack",
@@ -310,7 +308,6 @@
 
     target: {
         linux: {
-            // See note for libdexfile_static in simpleperf_ndk.
             static_libs: ["libdexfile_support"],
             runtime_libs: ["libdexfile"], // libdexfile_support dependency
         },
@@ -395,13 +392,8 @@
         "libsimpleperf",
     ],
 
+    min_sdk_version: "31",
     target: {
-        android: {
-            static_executable: true,
-            static_libs: [
-                "libc",
-            ],
-        },
         android_arm: {
             dist: {
                 dir: "simpleperf/android/arm",
@@ -436,11 +428,8 @@
             },
         },
         linux: {
-            // In the NDK we need libdexfile_static which links libdexfile and
-            // its ART dependencies statically. However in other libraries we
-            // must use libdexfile_support, which dlopen's libdexfile.so from
-            // the ART APEX, to avoid getting ART internals in the system image.
-            static_libs: ["libdexfile_static"],
+            static_libs: ["libdexfile_support"],
+            runtime_libs: ["libdexfile"], // libdexfile_support dependency
         },
         linux_glibc_x86: {
             dist: {
@@ -493,13 +482,13 @@
         darwin: {
             enabled: false,
         },
+        linux: {
+            static_libs: ["libdexfile_support"],
+            runtime_libs: ["libdexfile"], // libdexfile_support dependency
+        },
         windows: {
             enabled: false,
         },
-        linux: {
-            // See note for libdexfile_static in simpleperf_ndk.
-            static_libs: ["libdexfile_static"],
-        },
     },
 }
 
@@ -523,7 +512,6 @@
         },
         linux: {
             ldflags: ["-Wl,--exclude-libs,ALL"],
-            // See note for libdexfile_static in simpleperf_ndk.
             static_libs: ["libdexfile_static"],
         },
         darwin: {
diff --git a/simpleperf/JITDebugReader_test.cpp b/simpleperf/JITDebugReader_test.cpp
index 4aaad81..cbfba55 100644
--- a/simpleperf/JITDebugReader_test.cpp
+++ b/simpleperf/JITDebugReader_test.cpp
@@ -56,9 +56,9 @@
 TEST(JITDebugReader, read_dex_file_in_memory) {
   // 1. Create dex file in memory. Use mmap instead of malloc, to avoid the pointer from
   // being modified by memory tag (or pointer authentication?) on ARM64.
-  std::string dex_file = GetTestData("base.vdex");
+  std::string dex_file = GetTestData("base.dex");
   uint64_t file_size = GetFileSize(dex_file);
-  const uint64_t dex_file_offset = 0x28;
+  const uint64_t dex_file_offset = 0;
   ASSERT_GT(file_size, dex_file_offset);
   uint64_t symfile_size = file_size - dex_file_offset;
   void* symfile_addr =
@@ -90,7 +90,7 @@
   ASSERT_EQ(info.dex_file_map->start_addr, reinterpret_cast<uintptr_t>(symfile_addr));
   ASSERT_EQ(info.dex_file_map->len, symfile_size);
   ASSERT_TRUE(android::base::StartsWith(info.dex_file_map->name, kDexFileInMemoryPrefix));
-  ASSERT_EQ(info.symbols.size(), 12435);
+  ASSERT_EQ(info.symbols.size(), 3912);
   // 4. Test if the symbols are sorted.
   uint64_t prev_addr = 0;
   for (const auto& symbol : info.symbols) {
diff --git a/simpleperf/dso_test.cpp b/simpleperf/dso_test.cpp
index 710299b..e68804f 100644
--- a/simpleperf/dso_test.cpp
+++ b/simpleperf/dso_test.cpp
@@ -183,16 +183,16 @@
 TEST(dso, dex_file_dso) {
 #if defined(__linux__)
   for (DsoType dso_type : {DSO_DEX_FILE, DSO_ELF_FILE}) {
-    std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.vdex"));
+    std::unique_ptr<Dso> dso = Dso::CreateDso(dso_type, GetTestData("base.dex"));
     ASSERT_TRUE(dso);
-    dso->AddDexFileOffset(0x28);
+    dso->AddDexFileOffset(0);
     ASSERT_EQ(DSO_DEX_FILE, dso->type());
-    const Symbol* symbol = dso->FindSymbol(0x6c77e);
+    const Symbol* symbol = dso->FindSymbol(0x613ec);
     ASSERT_NE(symbol, nullptr);
-    ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x6c77e));
-    ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x16));
+    ASSERT_EQ(symbol->addr, static_cast<uint64_t>(0x613ec));
+    ASSERT_EQ(symbol->len, static_cast<uint64_t>(0x128));
     ASSERT_STREQ(symbol->DemangledName(),
-                 "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
+                 "com.example.android.displayingbitmaps.ui.ImageDetailActivity.onCreate");
     uint64_t min_vaddr;
     uint64_t file_offset_of_min_vaddr;
     dso->GetMinExecutableVaddr(&min_vaddr, &file_offset_of_min_vaddr);
@@ -384,7 +384,7 @@
   }
   {
     // Don't warn when the file may not be an ELF file.
-    auto dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("base.vdex"));
+    auto dso = Dso::CreateDso(DSO_ELF_FILE, GetTestData("base.dex"));
     CapturedStderr capture;
     dso->LoadSymbols();
     ASSERT_EQ(capture.str().find("failed to read symbols"), std::string::npos);
diff --git a/simpleperf/read_dex_file_test.cpp b/simpleperf/read_dex_file_test.cpp
index a9860f8..74a2aac 100644
--- a/simpleperf/read_dex_file_test.cpp
+++ b/simpleperf/read_dex_file_test.cpp
@@ -33,12 +33,12 @@
   auto symbol_callback = [&](DexFileSymbol* symbol) {
     symbols.emplace_back(symbol->name, symbol->addr, symbol->size);
   };
-  ASSERT_TRUE(ReadSymbolsFromDexFile(GetTestData("base.vdex"), {0x28}, symbol_callback));
-  ASSERT_EQ(12435u, symbols.size());
+  ASSERT_TRUE(ReadSymbolsFromDexFile(GetTestData("base.dex"), {0}, symbol_callback));
+  ASSERT_EQ(3912u, symbols.size());
   auto it = std::find_if(symbols.begin(), symbols.end(),
-                         [](const Symbol& symbol) { return symbol.addr == 0x6c77e; });
+                         [](const Symbol& symbol) { return symbol.addr == 0x613ec; });
   ASSERT_NE(it, symbols.end());
-  ASSERT_EQ(it->addr, 0x6c77e);
-  ASSERT_EQ(it->len, 0x16);
-  ASSERT_STREQ(it->Name(), "com.example.simpleperf.simpleperfexamplewithnative.MixActivity$1.run");
+  ASSERT_EQ(it->addr, 0x613ec);
+  ASSERT_EQ(it->len, 0x128);
+  ASSERT_STREQ(it->Name(), "com.example.android.displayingbitmaps.ui.ImageDetailActivity.onCreate");
 }
diff --git a/simpleperf/testdata/base.dex b/simpleperf/testdata/base.dex
new file mode 100644
index 0000000..1864c29
--- /dev/null
+++ b/simpleperf/testdata/base.dex
Binary files differ
diff --git a/simpleperf/testdata/base.vdex b/simpleperf/testdata/base.vdex
deleted file mode 100644
index b0ea018..0000000
--- a/simpleperf/testdata/base.vdex
+++ /dev/null
Binary files differ