Merge "init: use a property instead of file to communicate cold boot done"
diff --git a/adb/daemon/include/adbd/usb.h b/adb/daemon/include/adbd/usb.h
index fca3c58..85a5711 100644
--- a/adb/daemon/include/adbd/usb.h
+++ b/adb/daemon/include/adbd/usb.h
@@ -16,6 +16,8 @@
  * limitations under the License.
  */
 
+#include <linux/usb/functionfs.h>
+
 #include <atomic>
 #include <condition_variable>
 #include <mutex>
@@ -62,5 +64,9 @@
 };
 
 usb_handle *create_usb_handle(unsigned num_bufs, unsigned io_size);
+
+struct usb_functionfs_event;
+const char* ffs_event_to_string(enum usb_functionfs_event_type type);
+bool read_functionfs_setup(android::base::borrowed_fd fd, usb_functionfs_event* event);
 bool open_functionfs(android::base::unique_fd* control, android::base::unique_fd* bulk_out,
                      android::base::unique_fd* bulk_in);
diff --git a/adb/daemon/usb.cpp b/adb/daemon/usb.cpp
index 1abae87..48fa771 100644
--- a/adb/daemon/usb.cpp
+++ b/adb/daemon/usb.cpp
@@ -66,25 +66,6 @@
 static constexpr size_t kUsbWriteQueueDepth = 8;
 static constexpr size_t kUsbWriteSize = 4 * PAGE_SIZE;
 
-static const char* to_string(enum usb_functionfs_event_type type) {
-    switch (type) {
-        case FUNCTIONFS_BIND:
-            return "FUNCTIONFS_BIND";
-        case FUNCTIONFS_UNBIND:
-            return "FUNCTIONFS_UNBIND";
-        case FUNCTIONFS_ENABLE:
-            return "FUNCTIONFS_ENABLE";
-        case FUNCTIONFS_DISABLE:
-            return "FUNCTIONFS_DISABLE";
-        case FUNCTIONFS_SETUP:
-            return "FUNCTIONFS_SETUP";
-        case FUNCTIONFS_SUSPEND:
-            return "FUNCTIONFS_SUSPEND";
-        case FUNCTIONFS_RESUME:
-            return "FUNCTIONFS_RESUME";
-    }
-}
-
 enum class TransferDirection : uint64_t {
     READ = 0,
     WRITE = 1,
@@ -169,12 +150,12 @@
 };
 
 struct UsbFfsConnection : public Connection {
-    UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write,
+    UsbFfsConnection(unique_fd* control, unique_fd read, unique_fd write,
                      std::promise<void> destruction_notifier)
         : worker_started_(false),
           stopped_(false),
           destruction_notifier_(std::move(destruction_notifier)),
-          control_fd_(std::move(control)),
+          control_fd_(control),
           read_fd_(std::move(read)),
           write_fd_(std::move(write)) {
         LOG(INFO) << "UsbFfsConnection constructed";
@@ -183,11 +164,6 @@
             PLOG(FATAL) << "failed to create eventfd";
         }
 
-        monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
-        if (monitor_event_fd_ == -1) {
-            PLOG(FATAL) << "failed to create eventfd";
-        }
-
         aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth);
     }
 
@@ -199,7 +175,6 @@
         // We need to explicitly close our file descriptors before we notify our destruction,
         // because the thread listening on the future will immediately try to reopen the endpoint.
         aio_context_.reset();
-        control_fd_.reset();
         read_fd_.reset();
         write_fd_.reset();
 
@@ -246,13 +221,6 @@
             PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection";
         }
         CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
-
-        rc = adb_write(monitor_event_fd_.get(), &notify, sizeof(notify));
-        if (rc < 0) {
-            PLOG(FATAL) << "failed to notify monitor eventfd to stop UsbFfsConnection";
-        }
-
-        CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
     }
 
   private:
@@ -271,33 +239,24 @@
         monitor_thread_ = std::thread([this]() {
             adb_thread_setname("UsbFfs-monitor");
 
-            bool bound = false;
             bool enabled = false;
             bool running = true;
             while (running) {
                 adb_pollfd pfd[2] = {
-                  { .fd = control_fd_.get(), .events = POLLIN, .revents = 0 },
-                  { .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 },
+                        {.fd = control_fd_->get(), .events = POLLIN, .revents = 0},
                 };
 
-                // If we don't see our first bind within a second, try again.
-                int timeout_ms = bound ? -1 : 1000;
-
-                int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, timeout_ms));
+                int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, -1));
                 if (rc == -1) {
                     PLOG(FATAL) << "poll on USB control fd failed";
-                } else if (rc == 0) {
-                    LOG(WARNING) << "timed out while waiting for FUNCTIONFS_BIND, trying again";
-                    break;
                 }
 
                 if (pfd[1].revents) {
-                    // We were told to die.
-                    break;
+                    // We were told to die, continue reading until FUNCTIONFS_UNBIND.
                 }
 
                 struct usb_functionfs_event event;
-                rc = TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event)));
+                rc = TEMP_FAILURE_RETRY(adb_read(control_fd_->get(), &event, sizeof(event)));
                 if (rc == -1) {
                     PLOG(FATAL) << "failed to read functionfs event";
                 } else if (rc == 0) {
@@ -309,32 +268,15 @@
                 }
 
                 LOG(INFO) << "USB event: "
-                          << to_string(static_cast<usb_functionfs_event_type>(event.type));
+                          << ffs_event_to_string(
+                                     static_cast<usb_functionfs_event_type>(event.type));
 
                 switch (event.type) {
                     case FUNCTIONFS_BIND:
-                        if (bound) {
-                            LOG(WARNING) << "received FUNCTIONFS_BIND while already bound?";
-                            running = false;
-                            break;
-                        }
-
-                        if (enabled) {
-                            LOG(WARNING) << "received FUNCTIONFS_BIND while already enabled?";
-                            running = false;
-                            break;
-                        }
-
-                        bound = true;
+                        LOG(FATAL) << "received FUNCTIONFS_BIND after already opened?";
                         break;
 
                     case FUNCTIONFS_ENABLE:
-                        if (!bound) {
-                            LOG(WARNING) << "received FUNCTIONFS_ENABLE while not bound?";
-                            running = false;
-                            break;
-                        }
-
                         if (enabled) {
                             LOG(WARNING) << "received FUNCTIONFS_ENABLE while already enabled?";
                             running = false;
@@ -346,10 +288,6 @@
                         break;
 
                     case FUNCTIONFS_DISABLE:
-                        if (!bound) {
-                            LOG(WARNING) << "received FUNCTIONFS_DISABLE while not bound?";
-                        }
-
                         if (!enabled) {
                             LOG(WARNING) << "received FUNCTIONFS_DISABLE while not enabled?";
                         }
@@ -363,44 +301,12 @@
                             LOG(WARNING) << "received FUNCTIONFS_UNBIND while still enabled?";
                         }
 
-                        if (!bound) {
-                            LOG(WARNING) << "received FUNCTIONFS_UNBIND when not bound?";
-                        }
-
-                        bound = false;
                         running = false;
                         break;
 
                     case FUNCTIONFS_SETUP: {
-                        LOG(INFO) << "received FUNCTIONFS_SETUP control transfer: bRequestType = "
-                                  << static_cast<int>(event.u.setup.bRequestType)
-                                  << ", bRequest = " << static_cast<int>(event.u.setup.bRequest)
-                                  << ", wValue = " << static_cast<int>(event.u.setup.wValue)
-                                  << ", wIndex = " << static_cast<int>(event.u.setup.wIndex)
-                                  << ", wLength = " << static_cast<int>(event.u.setup.wLength);
-
-                        if ((event.u.setup.bRequestType & USB_DIR_IN)) {
-                            LOG(INFO) << "acking device-to-host control transfer";
-                            ssize_t rc = adb_write(control_fd_.get(), "", 0);
-                            if (rc != 0) {
-                                PLOG(ERROR) << "failed to write empty packet to host";
-                                break;
-                            }
-                        } else {
-                            std::string buf;
-                            buf.resize(event.u.setup.wLength + 1);
-
-                            ssize_t rc = adb_read(control_fd_.get(), buf.data(), buf.size());
-                            if (rc != event.u.setup.wLength) {
-                                LOG(ERROR)
-                                        << "read " << rc
-                                        << " bytes when trying to read control request, expected "
-                                        << event.u.setup.wLength;
-                            }
-
-                            LOG(INFO) << "control request contents: " << buf;
-                            break;
-                        }
+                        read_functionfs_setup(*control_fd_, &event);
+                        break;
                     }
                 }
             }
@@ -426,6 +332,12 @@
                 uint64_t dummy;
                 ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy));
                 if (rc == -1) {
+                    if (errno == EINTR) {
+                        // We were interrupted either to stop, or because of a backtrace.
+                        // Check stopped_ again to see if we need to exit.
+                        continue;
+                    }
+
                     PLOG(FATAL) << "failed to read from eventfd";
                 } else if (rc == 0) {
                     LOG(FATAL) << "hit EOF on eventfd";
@@ -462,6 +374,7 @@
         }
 
         worker_thread_.join();
+        worker_started_ = false;
     }
 
     void PrepareReadBlock(IoBlock* block, uint64_t id) {
@@ -509,14 +422,16 @@
             }
 
             if (id.direction == TransferDirection::READ) {
-                HandleRead(id, event.res);
+                if (!HandleRead(id, event.res)) {
+                    return;
+                }
             } else {
                 HandleWrite(id);
             }
         }
     }
 
-    void HandleRead(TransferId id, int64_t size) {
+    bool HandleRead(TransferId id, int64_t size) {
         uint64_t read_idx = id.id % kUsbReadQueueDepth;
         IoBlock* block = &read_requests_[read_idx];
         block->pending = false;
@@ -526,7 +441,7 @@
         if (block->id().id != needed_read_id_) {
             LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
                          << needed_read_id_;
-            return;
+            return true;
         }
 
         for (uint64_t id = needed_read_id_;; ++id) {
@@ -535,15 +450,22 @@
             if (current_block->pending) {
                 break;
             }
-            ProcessRead(current_block);
+            if (!ProcessRead(current_block)) {
+                return false;
+            }
             ++needed_read_id_;
         }
+
+        return true;
     }
 
-    void ProcessRead(IoBlock* block) {
+    bool ProcessRead(IoBlock* block) {
         if (!block->payload->empty()) {
             if (!incoming_header_.has_value()) {
-                CHECK_EQ(sizeof(amessage), block->payload->size());
+                if (block->payload->size() != sizeof(amessage)) {
+                    HandleError("received packet of unexpected length while reading header");
+                    return false;
+                }
                 amessage msg;
                 memcpy(&msg, block->payload->data(), sizeof(amessage));
                 LOG(DEBUG) << "USB read:" << dump_header(&msg);
@@ -551,7 +473,10 @@
             } else {
                 size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
                 Block payload = std::move(*block->payload);
-                CHECK_LE(payload.size(), bytes_left);
+                if (block->payload->size() > bytes_left) {
+                    HandleError("received too many bytes while waiting for payload");
+                    return false;
+                }
                 incoming_payload_.append(std::make_unique<Block>(std::move(payload)));
             }
 
@@ -570,6 +495,7 @@
 
         PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
         SubmitRead(block);
+        return true;
     }
 
     bool SubmitRead(IoBlock* block) {
@@ -679,10 +605,13 @@
     std::once_flag error_flag_;
 
     unique_fd worker_event_fd_;
-    unique_fd monitor_event_fd_;
 
     ScopedAioContext aio_context_;
-    unique_fd control_fd_;
+
+    // We keep a pointer to the control fd, so that we can reuse it to avoid USB reconfiguration,
+    // and still be able to reset it to force a reopen after FUNCTIONFS_UNBIND or running into an
+    // unexpected situation.
+    unique_fd* control_fd_;
     unique_fd read_fd_;
     unique_fd write_fd_;
 
@@ -711,15 +640,16 @@
 static void usb_ffs_open_thread() {
     adb_thread_setname("usb ffs open");
 
+    unique_fd control;
+    unique_fd bulk_out;
+    unique_fd bulk_in;
+
     while (true) {
         if (gFfsAioSupported.has_value() && !gFfsAioSupported.value()) {
             LOG(INFO) << "failed to use nonblocking ffs, falling back to legacy";
             return usb_init_legacy();
         }
 
-        unique_fd control;
-        unique_fd bulk_out;
-        unique_fd bulk_in;
         if (!open_functionfs(&control, &bulk_out, &bulk_in)) {
             std::this_thread::sleep_for(1s);
             continue;
@@ -730,7 +660,7 @@
         std::promise<void> destruction_notifier;
         std::future<void> future = destruction_notifier.get_future();
         transport->SetConnection(std::make_unique<UsbFfsConnection>(
-                std::move(control), std::move(bulk_out), std::move(bulk_in),
+                &control, std::move(bulk_out), std::move(bulk_in),
                 std::move(destruction_notifier)));
         register_transport(transport);
         future.wait();
diff --git a/adb/daemon/usb_ffs.cpp b/adb/daemon/usb_ffs.cpp
index a64ce40..5fbca3b 100644
--- a/adb/daemon/usb_ffs.cpp
+++ b/adb/daemon/usb_ffs.cpp
@@ -248,6 +248,56 @@
 };
 // clang-format on
 
+const char* ffs_event_to_string(enum usb_functionfs_event_type type) {
+    switch (type) {
+        case FUNCTIONFS_BIND:
+            return "FUNCTIONFS_BIND";
+        case FUNCTIONFS_UNBIND:
+            return "FUNCTIONFS_UNBIND";
+        case FUNCTIONFS_ENABLE:
+            return "FUNCTIONFS_ENABLE";
+        case FUNCTIONFS_DISABLE:
+            return "FUNCTIONFS_DISABLE";
+        case FUNCTIONFS_SETUP:
+            return "FUNCTIONFS_SETUP";
+        case FUNCTIONFS_SUSPEND:
+            return "FUNCTIONFS_SUSPEND";
+        case FUNCTIONFS_RESUME:
+            return "FUNCTIONFS_RESUME";
+    }
+}
+
+bool read_functionfs_setup(borrowed_fd fd, usb_functionfs_event* event) {
+    LOG(INFO) << "received FUNCTIONFS_SETUP control transfer: bRequestType = "
+              << static_cast<int>(event->u.setup.bRequestType)
+              << ", bRequest = " << static_cast<int>(event->u.setup.bRequest)
+              << ", wValue = " << static_cast<int>(event->u.setup.wValue)
+              << ", wIndex = " << static_cast<int>(event->u.setup.wIndex)
+              << ", wLength = " << static_cast<int>(event->u.setup.wLength);
+
+    if ((event->u.setup.bRequestType & USB_DIR_IN)) {
+        LOG(INFO) << "acking device-to-host control transfer";
+        ssize_t rc = adb_write(fd.get(), "", 0);
+        if (rc != 0) {
+            PLOG(ERROR) << "failed to write empty packet to host";
+            return false;
+        }
+    } else {
+        std::string buf;
+        buf.resize(event->u.setup.wLength + 1);
+
+        ssize_t rc = adb_read(fd.get(), buf.data(), buf.size());
+        if (rc != event->u.setup.wLength) {
+            LOG(ERROR) << "read " << rc << " bytes when trying to read control request, expected "
+                       << event->u.setup.wLength;
+        }
+
+        LOG(INFO) << "control request contents: " << buf;
+    }
+
+    return true;
+}
+
 bool open_functionfs(android::base::unique_fd* out_control, android::base::unique_fd* out_bulk_out,
                      android::base::unique_fd* out_bulk_in) {
     unique_fd control, bulk_out, bulk_in;
@@ -297,11 +347,37 @@
             PLOG(ERROR) << "failed to write USB strings";
             return false;
         }
-        // Signal only when writing the descriptors to ffs
+
+        // Signal init after we've written our descriptors.
         android::base::SetProperty("sys.usb.ffs.ready", "1");
         *out_control = std::move(control);
     }
 
+    // Read until we get FUNCTIONFS_BIND from the control endpoint.
+    while (true) {
+        struct usb_functionfs_event event;
+        ssize_t rc = TEMP_FAILURE_RETRY(adb_read(*out_control, &event, sizeof(event)));
+
+        if (rc == -1) {
+            PLOG(FATAL) << "failed to read from FFS control fd";
+        } else if (rc == 0) {
+            LOG(WARNING) << "hit EOF on functionfs control fd during initialization";
+        } else if (rc != sizeof(event)) {
+            LOG(FATAL) << "read functionfs event of unexpected size, expected " << sizeof(event)
+                       << ", got " << rc;
+        }
+
+        LOG(INFO) << "USB event: "
+                  << ffs_event_to_string(static_cast<usb_functionfs_event_type>(event.type));
+        if (event.type == FUNCTIONFS_BIND) {
+            break;
+        } else if (event.type == FUNCTIONFS_SETUP) {
+            read_functionfs_setup(*out_control, &event);
+        } else {
+            continue;
+        }
+    }
+
     bulk_out.reset(adb_open(USB_FFS_ADB_OUT, O_RDONLY));
     if (bulk_out < 0) {
         PLOG(ERROR) << "cannot open bulk-out endpoint " << USB_FFS_ADB_OUT;
diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp
index 886ded4..f86cd03 100644
--- a/adb/sysdeps_win32.cpp
+++ b/adb/sysdeps_win32.cpp
@@ -2606,7 +2606,9 @@
 extern "C" int wmain(int argc, wchar_t **argv) {
     // Convert args from UTF-16 to UTF-8 and pass that to main().
     NarrowArgs narrow_args(argc, argv);
-    return main(argc, narrow_args.data());
+
+    // Avoid destructing NarrowArgs: argv might have been mutated to point to string literals.
+    _exit(main(argc, narrow_args.data()));
 }
 
 // Shadow UTF-8 environment variable name/value pairs that are created from
diff --git a/base/Android.bp b/base/Android.bp
index 25a9f68..58b6fb5 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -111,6 +111,9 @@
         "libbase_headers",
     ],
     export_header_lib_headers: ["libbase_headers"],
+    static_libs: ["fmtlib"],
+    whole_static_libs: ["fmtlib"],
+    export_static_lib_headers: ["fmtlib"],
 }
 
 cc_library_static {
@@ -119,6 +122,9 @@
     sdk_version: "current",
     stl: "c++_static",
     export_include_dirs: ["include"],
+    static_libs: ["fmtlib_ndk"],
+    whole_static_libs: ["fmtlib_ndk"],
+    export_static_lib_headers: ["fmtlib_ndk"],
 }
 
 // Tests
@@ -176,3 +182,21 @@
     },
     test_suites: ["device-tests"],
 }
+
+cc_benchmark {
+    name: "libbase_benchmark",
+    defaults: ["libbase_cflags_defaults"],
+
+    srcs: ["format_benchmark.cpp"],
+    shared_libs: ["libbase"],
+
+    compile_multilib: "both",
+    multilib: {
+        lib32: {
+            suffix: "32",
+        },
+        lib64: {
+            suffix: "64",
+        },
+    },
+}
diff --git a/base/format_benchmark.cpp b/base/format_benchmark.cpp
new file mode 100644
index 0000000..9590b23
--- /dev/null
+++ b/base/format_benchmark.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "android-base/format.h"
+
+#include <limits>
+
+#include <benchmark/benchmark.h>
+
+#include "android-base/stringprintf.h"
+
+using android::base::StringPrintf;
+
+static void BenchmarkFormatInt(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(fmt::format("{} {} {}", 42, std::numeric_limits<int>::min(),
+                                         std::numeric_limits<int>::max()));
+  }
+}
+
+BENCHMARK(BenchmarkFormatInt);
+
+static void BenchmarkStringPrintfInt(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(StringPrintf("%d %d %d", 42, std::numeric_limits<int>::min(),
+                                          std::numeric_limits<int>::max()));
+  }
+}
+
+BENCHMARK(BenchmarkStringPrintfInt);
+
+static void BenchmarkFormatFloat(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(fmt::format("{} {} {}", 42.42, std::numeric_limits<float>::min(),
+                                         std::numeric_limits<float>::max()));
+  }
+}
+
+BENCHMARK(BenchmarkFormatFloat);
+
+static void BenchmarkStringPrintfFloat(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(StringPrintf("%f %f %f", 42.42, std::numeric_limits<float>::min(),
+                                          std::numeric_limits<float>::max()));
+  }
+}
+
+BENCHMARK(BenchmarkStringPrintfFloat);
+
+static void BenchmarkFormatStrings(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(fmt::format("{} hello there {}", "hi,", "!!"));
+  }
+}
+
+BENCHMARK(BenchmarkFormatStrings);
+
+static void BenchmarkStringPrintfStrings(benchmark::State& state) {
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(StringPrintf("%s hello there %s", "hi,", "!!"));
+  }
+}
+
+BENCHMARK(BenchmarkStringPrintfStrings);
+
+// Run the benchmark
+BENCHMARK_MAIN();
diff --git a/base/include/android-base/expected.h b/base/include/android-base/expected.h
index 08c9fb5..957a8a0 100644
--- a/base/include/android-base/expected.h
+++ b/base/include/android-base/expected.h
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#pragma once
+
 #include <algorithm>
 #include <initializer_list>
 #include <type_traits>
diff --git a/mkbootimg/mkbootimg_dummy.cpp b/base/include/android-base/format.h
similarity index 62%
copy from mkbootimg/mkbootimg_dummy.cpp
copy to base/include/android-base/format.h
index 410d379..6799c1f 100644
--- a/mkbootimg/mkbootimg_dummy.cpp
+++ b/base/include/android-base/format.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 The Android Open Source Project
+ * Copyright (C) 2019 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,11 +14,12 @@
  * limitations under the License.
  */
 
-#include <abi_check/mkbootimg_abi_check.h>
+#pragma once
 
-void mkbootimg_dummy(boot_img_hdr* hdr) {
-    // TODO: Hack to trigger abi checks, remove this.
-    if (hdr) {
-        hdr--;
-    }
-}
+// We include fmtlib here as an alias, since libbase will have fmtlib statically linked already.
+// It is accessed through its normal fmt:: namespace.
+#include <fmt/core.h>
+#include <fmt/format.h>
+#include <fmt/ostream.h>
+#include <fmt/printf.h>
+#include <fmt/time.h>
diff --git a/base/include/android-base/result.h b/base/include/android-base/result.h
index 897c48f..1b763af 100644
--- a/base/include/android-base/result.h
+++ b/base/include/android-base/result.h
@@ -42,10 +42,15 @@
 // to the end of the failure string to aid in interacting with C APIs.  Alternatively, an errno
 // value can be directly specified via the Error() constructor.
 //
-// ResultError can be used in the ostream when using Error to construct a Result<T>.  In this case,
-// the string that the ResultError takes is passed through the stream normally, but the errno is
-// passed to the Result<T>.  This can be used to pass errno from a failing C function up multiple
-// callers.
+// Errorf and ErrnoErrorf accept the format string syntax of the fmblib (https://fmt.dev).
+// Errorf("{} errors", num) is equivalent to Error() << num << " errors".
+//
+// ResultError can be used in the ostream and when using Error/Errorf to construct a Result<T>.
+// In this case, the string that the ResultError takes is passed through the stream normally, but
+// the errno is passed to the Result<T>. This can be used to pass errno from a failing C function up
+// multiple callers. Note that when the outer Result<T> is created with ErrnoError/ErrnoErrorf then
+// the errno from the inner ResultError is not passed. Also when multiple ResultError objects are
+// used, the errno of the last one is respected.
 //
 // ResultError can also directly construct a Result<T>.  This is particularly useful if you have a
 // function that return Result<T> but you have a Result<U> and want to return its error.  In this
@@ -55,10 +60,10 @@
 // Result<U> CalculateResult(const T& input) {
 //   U output;
 //   if (!SomeOtherCppFunction(input, &output)) {
-//     return Error() << "SomeOtherCppFunction(" << input << ") failed";
+//     return Errorf("SomeOtherCppFunction {} failed", input);
 //   }
 //   if (!c_api_function(output)) {
-//     return ErrnoError() << "c_api_function(" << output << ") failed";
+//     return ErrnoErrorf("c_api_function {} failed", output);
 //   }
 //   return output;
 // }
@@ -75,14 +80,14 @@
 #include <string>
 
 #include "android-base/expected.h"
+#include "android-base/format.h"
 
 namespace android {
 namespace base {
 
 struct ResultError {
   template <typename T>
-  ResultError(T&& message, int code)
-      : message_(std::forward<T>(message)), code_(code) {}
+  ResultError(T&& message, int code) : message_(std::forward<T>(message)), code_(code) {}
 
   template <typename T>
   operator android::base::expected<T, ResultError>() {
@@ -122,18 +127,16 @@
 
   template <typename T>
   Error& operator<<(T&& t) {
+    if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, ResultError>) {
+      errno_ = t.code();
+      return (*this) << t.message();
+    }
     int saved = errno;
     ss_ << t;
     errno = saved;
     return *this;
   }
 
-  Error& operator<<(const ResultError& result_error) {
-    (*this) << result_error.message();
-    errno_ = result_error.code();
-    return *this;
-  }
-
   const std::string str() const {
     std::string str = ss_.str();
     if (append_errno_) {
@@ -150,16 +153,51 @@
   Error& operator=(const Error&) = delete;
   Error& operator=(Error&&) = delete;
 
+  template <typename... Args>
+  friend Error Errorf(const char* fmt, const Args&... args);
+
+  template <typename... Args>
+  friend Error ErrnoErrorf(const char* fmt, const Args&... args);
+
  private:
+  Error(bool append_errno, int errno_to_append, const std::string& message)
+      : errno_(errno_to_append), append_errno_(append_errno) {
+    (*this) << message;
+  }
+
   std::stringstream ss_;
   int errno_;
-  bool append_errno_;
+  const bool append_errno_;
 };
 
 inline Error ErrnoError() {
   return Error(errno);
 }
 
+inline int ErrorCode(int code) {
+  return code;
+}
+
+// Return the error code of the last ResultError object, if any.
+// Otherwise, return `code` as it is.
+template <typename T, typename... Args>
+inline int ErrorCode(int code, T&& t, const Args&... args) {
+  if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, ResultError>) {
+    return ErrorCode(t.code(), args...);
+  }
+  return ErrorCode(code, args...);
+}
+
+template <typename... Args>
+inline Error Errorf(const char* fmt, const Args&... args) {
+  return Error(false, ErrorCode(0, args...), fmt::format(fmt, args...));
+}
+
+template <typename... Args>
+inline Error ErrnoErrorf(const char* fmt, const Args&... args) {
+  return Error(true, errno, fmt::format(fmt, args...));
+}
+
 template <typename T>
 using Result = android::base::expected<T, ResultError>;
 
diff --git a/base/result_test.cpp b/base/result_test.cpp
index 72f97f4..2ee4057 100644
--- a/base/result_test.cpp
+++ b/base/result_test.cpp
@@ -143,8 +143,8 @@
   ASSERT_FALSE(result2);
   ASSERT_FALSE(result2.has_value());
 
-  EXPECT_EQ(0, result.error().code());
-  EXPECT_EQ(error_text, result.error().message());
+  EXPECT_EQ(0, result2.error().code());
+  EXPECT_EQ(error_text, result2.error().message());
 }
 
 TEST(result, result_error_through_ostream) {
@@ -159,8 +159,8 @@
   ASSERT_FALSE(result2);
   ASSERT_FALSE(result2.has_value());
 
-  EXPECT_EQ(0, result.error().code());
-  EXPECT_EQ(error_text, result.error().message());
+  EXPECT_EQ(0, result2.error().code());
+  EXPECT_EQ(error_text, result2.error().message());
 }
 
 TEST(result, result_errno_error_through_ostream) {
@@ -179,8 +179,8 @@
   ASSERT_FALSE(result2);
   ASSERT_FALSE(result2.has_value());
 
-  EXPECT_EQ(test_errno, result.error().code());
-  EXPECT_EQ(error_text + ": " + strerror(test_errno), result.error().message());
+  EXPECT_EQ(test_errno, result2.error().code());
+  EXPECT_EQ(error_text + ": " + strerror(test_errno), result2.error().message());
 }
 
 TEST(result, constructor_forwarding) {
@@ -355,5 +355,68 @@
   EXPECT_EQ(old_errno, result2.error().code());
 }
 
+TEST(result, error_with_fmt) {
+  Result<int> result = Errorf("{} {}!", "hello", "world");
+  EXPECT_EQ("hello world!", result.error().message());
+
+  result = Errorf("{} {}!", std::string("hello"), std::string("world"));
+  EXPECT_EQ("hello world!", result.error().message());
+
+  result = Errorf("{h} {w}!", fmt::arg("w", "world"), fmt::arg("h", "hello"));
+  EXPECT_EQ("hello world!", result.error().message());
+
+  result = Errorf("hello world!");
+  EXPECT_EQ("hello world!", result.error().message());
+
+  Result<int> result2 = Errorf("error occurred with {}", result.error());
+  EXPECT_EQ("error occurred with hello world!", result2.error().message());
+
+  constexpr int test_errno = 6;
+  errno = test_errno;
+  result = ErrnoErrorf("{} {}!", "hello", "world");
+  EXPECT_EQ(test_errno, result.error().code());
+  EXPECT_EQ("hello world!: "s + strerror(test_errno), result.error().message());
+}
+
+TEST(result, error_with_fmt_carries_errno) {
+  constexpr int inner_errno = 6;
+  errno = inner_errno;
+  Result<int> inner_result = ErrnoErrorf("inner failure");
+  errno = 0;
+  EXPECT_EQ(inner_errno, inner_result.error().code());
+
+  // outer_result is created with Errorf, but its error code is got from inner_result.
+  Result<int> outer_result = Errorf("outer failure caused by {}", inner_result.error());
+  EXPECT_EQ(inner_errno, outer_result.error().code());
+  EXPECT_EQ("outer failure caused by inner failure: "s + strerror(inner_errno),
+            outer_result.error().message());
+
+  // now both result objects are created with ErrnoErrorf. errno from the inner_result
+  // is not passed to outer_result.
+  constexpr int outer_errno = 10;
+  errno = outer_errno;
+  outer_result = ErrnoErrorf("outer failure caused by {}", inner_result.error());
+  EXPECT_EQ(outer_errno, outer_result.error().code());
+  EXPECT_EQ("outer failure caused by inner failure: "s + strerror(inner_errno) + ": "s +
+                strerror(outer_errno),
+            outer_result.error().message());
+}
+
+TEST(result, errno_chaining_multiple) {
+  constexpr int errno1 = 6;
+  errno = errno1;
+  Result<int> inner1 = ErrnoErrorf("error1");
+
+  constexpr int errno2 = 10;
+  errno = errno2;
+  Result<int> inner2 = ErrnoErrorf("error2");
+
+  // takes the error code of inner2 since its the last one.
+  Result<int> outer = Errorf("two errors: {}, {}", inner1.error(), inner2.error());
+  EXPECT_EQ(errno2, outer.error().code());
+  EXPECT_EQ("two errors: error1: "s + strerror(errno1) + ", error2: "s + strerror(errno2),
+            outer.error().message());
+}
+
 }  // namespace base
 }  // namespace android
diff --git a/fastboot/device/commands.cpp b/fastboot/device/commands.cpp
index 1b09f79..409ef70 100644
--- a/fastboot/device/commands.cpp
+++ b/fastboot/device/commands.cpp
@@ -39,6 +39,7 @@
 #include "flashing.h"
 #include "utility.h"
 
+using android::fs_mgr::MetadataBuilder;
 using ::android::hardware::hidl_string;
 using ::android::hardware::boot::V1_0::BoolResult;
 using ::android::hardware::boot::V1_0::CommandResult;
@@ -46,8 +47,6 @@
 using ::android::hardware::fastboot::V1_0::Result;
 using ::android::hardware::fastboot::V1_0::Status;
 
-using namespace android::fs_mgr;
-
 struct VariableHandlers {
     // Callback to retrieve the value of a single variable.
     std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get;
@@ -340,7 +339,7 @@
 PartitionBuilder::PartitionBuilder(FastbootDevice* device, const std::string& partition_name)
     : device_(device) {
     std::string slot_suffix = GetSuperSlotSuffix(device, partition_name);
-    slot_number_ = SlotNumberForSlotSuffix(slot_suffix);
+    slot_number_ = android::fs_mgr::SlotNumberForSlotSuffix(slot_suffix);
     auto super_device = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number_));
     if (!super_device) {
         return;
@@ -350,7 +349,7 @@
 }
 
 bool PartitionBuilder::Write() {
-    std::unique_ptr<LpMetadata> metadata = builder_->Export();
+    auto metadata = builder_->Export();
     if (!metadata) {
         return false;
     }
@@ -381,7 +380,7 @@
         return device->WriteFail("Partition already exists");
     }
 
-    Partition* partition = builder->AddPartition(partition_name, 0);
+    auto partition = builder->AddPartition(partition_name, 0);
     if (!partition) {
         return device->WriteFail("Failed to add partition");
     }
@@ -437,7 +436,7 @@
         return device->WriteFail("Could not open super partition");
     }
 
-    Partition* partition = builder->FindPartition(partition_name);
+    auto partition = builder->FindPartition(partition_name);
     if (!partition) {
         return device->WriteFail("Partition does not exist");
     }
@@ -466,7 +465,7 @@
 class AutoMountMetadata {
   public:
     AutoMountMetadata() {
-        Fstab proc_mounts;
+        android::fs_mgr::Fstab proc_mounts;
         if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) {
             LOG(ERROR) << "Could not read /proc/mounts";
             return;
@@ -494,7 +493,7 @@
     explicit operator bool() const { return mounted_; }
 
   private:
-    Fstab fstab_;
+    android::fs_mgr::Fstab fstab_;
     bool mounted_ = false;
     bool should_unmount_ = false;
 };
diff --git a/fs_mgr/Android.bp b/fs_mgr/Android.bp
index ffde114..3d3503c 100644
--- a/fs_mgr/Android.bp
+++ b/fs_mgr/Android.bp
@@ -35,6 +35,7 @@
     export_include_dirs: ["include"],
     include_dirs: ["system/vold"],
     srcs: [
+        "file_wait.cpp",
         "fs_mgr.cpp",
         "fs_mgr_format.cpp",
         "fs_mgr_verity.cpp",
diff --git a/fs_mgr/file_wait.cpp b/fs_mgr/file_wait.cpp
new file mode 100644
index 0000000..cbf6845
--- /dev/null
+++ b/fs_mgr/file_wait.cpp
@@ -0,0 +1,235 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <fs_mgr/file_wait.h>
+
+#include <limits.h>
+#if defined(__linux__)
+#include <poll.h>
+#include <sys/inotify.h>
+#endif
+#if defined(WIN32)
+#include <io.h>
+#else
+#include <unistd.h>
+#endif
+
+#include <functional>
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+namespace android {
+namespace fs_mgr {
+
+using namespace std::literals;
+using android::base::unique_fd;
+
+bool PollForFile(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+    auto start_time = std::chrono::steady_clock::now();
+
+    while (true) {
+        if (!access(path.c_str(), F_OK) || errno != ENOENT) return true;
+
+        std::this_thread::sleep_for(50ms);
+
+        auto now = std::chrono::steady_clock::now();
+        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+        if (time_elapsed > relative_timeout) return false;
+    }
+}
+
+bool PollForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+    auto start_time = std::chrono::steady_clock::now();
+
+    while (true) {
+        if (access(path.c_str(), F_OK) && errno == ENOENT) return true;
+
+        std::this_thread::sleep_for(50ms);
+
+        auto now = std::chrono::steady_clock::now();
+        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
+        if (time_elapsed > relative_timeout) return false;
+    }
+}
+
+#if defined(__linux__)
+class OneShotInotify {
+  public:
+    OneShotInotify(const std::string& path, uint32_t mask,
+                   const std::chrono::milliseconds relative_timeout);
+
+    bool Wait();
+
+  private:
+    bool CheckCompleted();
+    int64_t RemainingMs() const;
+    bool ConsumeEvents();
+
+    enum class Result { Success, Timeout, Error };
+    Result WaitImpl();
+
+    unique_fd inotify_fd_;
+    std::string path_;
+    uint32_t mask_;
+    std::chrono::time_point<std::chrono::steady_clock> start_time_;
+    std::chrono::milliseconds relative_timeout_;
+    bool finished_;
+};
+
+OneShotInotify::OneShotInotify(const std::string& path, uint32_t mask,
+                               const std::chrono::milliseconds relative_timeout)
+    : path_(path),
+      mask_(mask),
+      start_time_(std::chrono::steady_clock::now()),
+      relative_timeout_(relative_timeout),
+      finished_(false) {
+    // If the condition is already met, don't bother creating an inotify.
+    if (CheckCompleted()) return;
+
+    unique_fd inotify_fd(inotify_init1(IN_CLOEXEC | IN_NONBLOCK));
+    if (inotify_fd < 0) {
+        PLOG(ERROR) << "inotify_init1 failed";
+        return;
+    }
+
+    std::string watch_path;
+    if (mask == IN_CREATE) {
+        watch_path = android::base::Dirname(path);
+    } else {
+        watch_path = path;
+    }
+    if (inotify_add_watch(inotify_fd, watch_path.c_str(), mask) < 0) {
+        PLOG(ERROR) << "inotify_add_watch failed";
+        return;
+    }
+
+    // It's possible the condition was met before the add_watch. Check for
+    // this and abort early if so.
+    if (CheckCompleted()) return;
+
+    inotify_fd_ = std::move(inotify_fd);
+}
+
+bool OneShotInotify::Wait() {
+    Result result = WaitImpl();
+    if (result == Result::Success) return true;
+    if (result == Result::Timeout) return false;
+
+    // Some kind of error with inotify occurred, so fallback to a poll.
+    std::chrono::milliseconds timeout(RemainingMs());
+    if (mask_ == IN_CREATE) {
+        return PollForFile(path_, timeout);
+    } else if (mask_ == IN_DELETE_SELF) {
+        return PollForFileDeleted(path_, timeout);
+    } else {
+        LOG(ERROR) << "Unknown inotify mask: " << mask_;
+        return false;
+    }
+}
+
+OneShotInotify::Result OneShotInotify::WaitImpl() {
+    // If the operation completed super early, we'll never have created an
+    // inotify instance.
+    if (finished_) return Result::Success;
+    if (inotify_fd_ < 0) return Result::Error;
+
+    while (true) {
+        auto remaining_ms = RemainingMs();
+        if (remaining_ms <= 0) return Result::Timeout;
+
+        struct pollfd event = {
+                .fd = inotify_fd_,
+                .events = POLLIN,
+                .revents = 0,
+        };
+        int rv = poll(&event, 1, static_cast<int>(remaining_ms));
+        if (rv <= 0) {
+            if (rv == 0 || errno == EINTR) {
+                continue;
+            }
+            PLOG(ERROR) << "poll for inotify failed";
+            return Result::Error;
+        }
+        if (event.revents & POLLERR) {
+            LOG(ERROR) << "error reading inotify for " << path_;
+            return Result::Error;
+        }
+
+        // Note that we don't bother checking what kind of event it is, since
+        // it's cheap enough to just see if the initial condition is satisified.
+        // If it's not, we consume all the events available and continue.
+        if (CheckCompleted()) return Result::Success;
+        if (!ConsumeEvents()) return Result::Error;
+    }
+}
+
+bool OneShotInotify::CheckCompleted() {
+    if (mask_ == IN_CREATE) {
+        finished_ = !access(path_.c_str(), F_OK) || errno != ENOENT;
+    } else if (mask_ == IN_DELETE_SELF) {
+        finished_ = access(path_.c_str(), F_OK) && errno == ENOENT;
+    } else {
+        LOG(ERROR) << "Unexpected mask: " << mask_;
+    }
+    return finished_;
+}
+
+bool OneShotInotify::ConsumeEvents() {
+    // According to the manpage, this is enough to read at least one event.
+    static constexpr size_t kBufferSize = sizeof(struct inotify_event) + NAME_MAX + 1;
+    char buffer[kBufferSize];
+
+    do {
+        ssize_t rv = TEMP_FAILURE_RETRY(read(inotify_fd_, buffer, sizeof(buffer)));
+        if (rv <= 0) {
+            if (rv == 0 || errno == EAGAIN) {
+                return true;
+            }
+            PLOG(ERROR) << "read inotify failed";
+            return false;
+        }
+    } while (true);
+}
+
+int64_t OneShotInotify::RemainingMs() const {
+    auto remaining = (std::chrono::steady_clock::now() - start_time_);
+    auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(remaining);
+    return (relative_timeout_ - elapsed).count();
+}
+#endif
+
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+#if defined(__linux__)
+    OneShotInotify inotify(path, IN_CREATE, relative_timeout);
+    return inotify.Wait();
+#else
+    return PollForFile(path, relative_timeout);
+#endif
+}
+
+// Wait at most |relative_timeout| milliseconds for |path| to stop existing.
+bool WaitForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout) {
+#if defined(__linux__)
+    OneShotInotify inotify(path, IN_DELETE_SELF, relative_timeout);
+    return inotify.Wait();
+#else
+    return PollForFileDeleted(path, relative_timeout);
+#endif
+}
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/fs_mgr.cpp b/fs_mgr/fs_mgr.cpp
index 56ea92c..2a9a9d0 100644
--- a/fs_mgr/fs_mgr.cpp
+++ b/fs_mgr/fs_mgr.cpp
@@ -56,6 +56,7 @@
 #include <ext4_utils/ext4_utils.h>
 #include <ext4_utils/wipe.h>
 #include <fs_avb/fs_avb.h>
+#include <fs_mgr/file_wait.h>
 #include <fs_mgr_overlayfs.h>
 #include <libdm/dm.h>
 #include <liblp/metadata_format.h>
@@ -116,28 +117,6 @@
     FS_STAT_ENABLE_VERITY_FAILED = 0x80000,
 };
 
-// TODO: switch to inotify()
-bool fs_mgr_wait_for_file(const std::string& filename,
-                          const std::chrono::milliseconds relative_timeout,
-                          FileWaitMode file_wait_mode) {
-    auto start_time = std::chrono::steady_clock::now();
-
-    while (true) {
-        int rv = access(filename.c_str(), F_OK);
-        if (file_wait_mode == FileWaitMode::Exists) {
-            if (!rv || errno != ENOENT) return true;
-        } else if (file_wait_mode == FileWaitMode::DoesNotExist) {
-            if (rv && errno == ENOENT) return true;
-        }
-
-        std::this_thread::sleep_for(50ms);
-
-        auto now = std::chrono::steady_clock::now();
-        auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
-        if (time_elapsed > relative_timeout) return false;
-    }
-}
-
 static void log_fs_stat(const std::string& blk_device, int fs_stat) {
     if ((fs_stat & FS_STAT_IS_EXT4) == 0) return; // only log ext4
     std::string msg =
@@ -245,11 +224,11 @@
             if (should_force_check(*fs_stat)) {
                 ret = android_fork_execvp_ext(
                     ARRAY_SIZE(e2fsck_forced_argv), const_cast<char**>(e2fsck_forced_argv), &status,
-                    true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), NULL, 0);
+                    true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
             } else {
                 ret = android_fork_execvp_ext(
                     ARRAY_SIZE(e2fsck_argv), const_cast<char**>(e2fsck_argv), &status, true,
-                    LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), NULL, 0);
+                    LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
             }
 
             if (ret < 0) {
@@ -263,13 +242,19 @@
         }
     } else if (is_f2fs(fs_type)) {
         const char* f2fs_fsck_argv[] = {F2FS_FSCK_BIN, "-a", blk_device.c_str()};
-        LINFO << "Running " << F2FS_FSCK_BIN << " -a " << realpath(blk_device);
+        const char* f2fs_fsck_forced_argv[] = {F2FS_FSCK_BIN, "-f", blk_device.c_str()};
 
-        ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv),
-                                      const_cast<char **>(f2fs_fsck_argv),
-                                      &status, true, LOG_KLOG | LOG_FILE,
-                                      true, const_cast<char *>(FSCK_LOG_FILE),
-                                      NULL, 0);
+        if (should_force_check(*fs_stat)) {
+            LINFO << "Running " << F2FS_FSCK_BIN << " -f " << realpath(blk_device);
+            ret = android_fork_execvp_ext(
+                ARRAY_SIZE(f2fs_fsck_forced_argv), const_cast<char**>(f2fs_fsck_forced_argv), &status,
+                true, LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+        } else {
+            LINFO << "Running " << F2FS_FSCK_BIN << " -a " << realpath(blk_device);
+            ret = android_fork_execvp_ext(
+                ARRAY_SIZE(f2fs_fsck_argv), const_cast<char**>(f2fs_fsck_argv), &status, true,
+                LOG_KLOG | LOG_FILE, true, const_cast<char*>(FSCK_LOG_FILE), nullptr, 0);
+        }
         if (ret < 0) {
             /* No need to check for error in fork, we can't really handle it now */
             LERROR << "Failed trying to run " << F2FS_FSCK_BIN;
@@ -1097,8 +1082,7 @@
             continue;
         }
 
-        if (current_entry.fs_mgr_flags.wait &&
-            !fs_mgr_wait_for_file(current_entry.blk_device, 20s)) {
+        if (current_entry.fs_mgr_flags.wait && !WaitForFile(current_entry.blk_device, 20s)) {
             LERROR << "Skipping '" << current_entry.blk_device << "' during mount_all";
             continue;
         }
@@ -1367,7 +1351,7 @@
         }
 
         // First check the filesystem if requested.
-        if (fstab_entry.fs_mgr_flags.wait && !fs_mgr_wait_for_file(n_blk_device, 20s)) {
+        if (fstab_entry.fs_mgr_flags.wait && !WaitForFile(n_blk_device, 20s)) {
             LERROR << "Skipping mounting '" << n_blk_device << "'";
             continue;
         }
@@ -1570,7 +1554,7 @@
             fprintf(zram_fp.get(), "%" PRId64 "\n", entry.zram_size);
         }
 
-        if (entry.fs_mgr_flags.wait && !fs_mgr_wait_for_file(entry.blk_device, 20s)) {
+        if (entry.fs_mgr_flags.wait && !WaitForFile(entry.blk_device, 20s)) {
             LERROR << "Skipping mkswap for '" << entry.blk_device << "'";
             ret = false;
             continue;
diff --git a/fs_mgr/fs_mgr_dm_linear.cpp b/fs_mgr/fs_mgr_dm_linear.cpp
index ee6ffdb..1f21a71 100644
--- a/fs_mgr/fs_mgr_dm_linear.cpp
+++ b/fs_mgr/fs_mgr_dm_linear.cpp
@@ -38,6 +38,7 @@
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
+#include <fs_mgr/file_wait.h>
 #include <liblp/reader.h>
 
 #include "fs_mgr_priv.h"
@@ -128,7 +129,7 @@
         return false;
     }
     if (timeout_ms > std::chrono::milliseconds::zero()) {
-        if (!fs_mgr_wait_for_file(*path, timeout_ms, FileWaitMode::Exists)) {
+        if (!WaitForFile(*path, timeout_ms)) {
             DestroyLogicalPartition(name, {});
             LERROR << "Timed out waiting for device path: " << *path;
             return false;
@@ -202,7 +203,7 @@
     if (!dm.DeleteDevice(name)) {
         return false;
     }
-    if (!path.empty() && !fs_mgr_wait_for_file(path, timeout_ms, FileWaitMode::DoesNotExist)) {
+    if (!path.empty() && !WaitForFileDeleted(path, timeout_ms)) {
         LERROR << "Timed out waiting for device path to unlink: " << path;
         return false;
     }
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index 403f9be..31790b1 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -171,6 +171,18 @@
                 fs_options.append(",");  // appends a comma if not the first
             }
             fs_options.append(flag);
+
+            if (entry->fs_type == "f2fs" && StartsWith(flag, "reserve_root=")) {
+                std::string arg;
+                if (auto equal_sign = flag.find('='); equal_sign != std::string::npos) {
+                    arg = flag.substr(equal_sign + 1);
+                }
+                if (!ParseInt(arg, &entry->reserved_size)) {
+                    LWARNING << "Warning: reserve_root= flag malformed: " << arg;
+                } else {
+                    entry->reserved_size <<= 12;
+                }
+            }
         }
     }
     entry->fs_options = std::move(fs_options);
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index ed8cce6..05ca5fc 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -44,6 +44,7 @@
 #include <android-base/unique_fd.h>
 #include <ext4_utils/ext4_utils.h>
 #include <fs_mgr.h>
+#include <fs_mgr/file_wait.h>
 #include <fs_mgr_dm_linear.h>
 #include <fs_mgr_overlayfs.h>
 #include <fstab/fstab.h>
@@ -867,7 +868,7 @@
             scratch_can_be_mounted = false;
             auto scratch_device = fs_mgr_overlayfs_scratch_device();
             if (fs_mgr_overlayfs_scratch_can_be_mounted(scratch_device) &&
-                fs_mgr_wait_for_file(scratch_device, 10s)) {
+                WaitForFile(scratch_device, 10s)) {
                 const auto mount_type = fs_mgr_overlayfs_scratch_mount_type();
                 if (fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type,
                                                    true /* readonly */)) {
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index c36fd3d..3a33cf3 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -88,12 +88,6 @@
 
 using namespace std::chrono_literals;
 
-enum class FileWaitMode { Exists, DoesNotExist };
-
-bool fs_mgr_wait_for_file(const std::string& filename,
-                          const std::chrono::milliseconds relative_timeout,
-                          FileWaitMode wait_mode = FileWaitMode::Exists);
-
 bool fs_mgr_set_blk_ro(const std::string& blockdev, bool readonly = true);
 bool fs_mgr_update_for_slotselect(android::fs_mgr::Fstab* fstab);
 bool fs_mgr_is_device_unlocked();
diff --git a/fs_mgr/fs_mgr_verity.cpp b/fs_mgr/fs_mgr_verity.cpp
index 1deb1ac..be8077b 100644
--- a/fs_mgr/fs_mgr_verity.cpp
+++ b/fs_mgr/fs_mgr_verity.cpp
@@ -35,6 +35,7 @@
 #include <android-base/unique_fd.h>
 #include <crypto_utils/android_pubkey.h>
 #include <cutils/properties.h>
+#include <fs_mgr/file_wait.h>
 #include <libdm/dm.h>
 #include <logwrap/logwrap.h>
 #include <openssl/obj_mac.h>
@@ -529,7 +530,7 @@
     }
 
     // make sure we've set everything up properly
-    if (wait_for_verity_dev && !fs_mgr_wait_for_file(entry->blk_device, 1s)) {
+    if (wait_for_verity_dev && !WaitForFile(entry->blk_device, 1s)) {
         goto out;
     }
 
diff --git a/fs_mgr/include/fs_mgr/file_wait.h b/fs_mgr/include/fs_mgr/file_wait.h
new file mode 100644
index 0000000..74d160e
--- /dev/null
+++ b/fs_mgr/include/fs_mgr/file_wait.h
@@ -0,0 +1,34 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include <chrono>
+#include <string>
+
+namespace android {
+namespace fs_mgr {
+
+// Wait at most |relative_timeout| milliseconds for |path| to exist. dirname(path)
+// must already exist. For example, to wait on /dev/block/dm-6, /dev/block must
+// be a valid directory.
+bool WaitForFile(const std::string& path, const std::chrono::milliseconds relative_timeout);
+
+// Wait at most |relative_timeout| milliseconds for |path| to stop existing.
+// Note that this only returns true if the inode itself no longer exists, i.e.,
+// all outstanding file descriptors have been closed.
+bool WaitForFileDeleted(const std::string& path, const std::chrono::milliseconds relative_timeout);
+
+}  // namespace fs_mgr
+}  // namespace android
diff --git a/fs_mgr/libdm/Android.bp b/fs_mgr/libdm/Android.bp
index c8c2d83..21255df 100644
--- a/fs_mgr/libdm/Android.bp
+++ b/fs_mgr/libdm/Android.bp
@@ -46,6 +46,7 @@
     static_libs: [
         "libdm",
         "libbase",
+        "libfs_mgr",
         "liblog",
     ],
     srcs: [
diff --git a/fs_mgr/libdm/dm.cpp b/fs_mgr/libdm/dm.cpp
index c2917a4..d54b6ef 100644
--- a/fs_mgr/libdm/dm.cpp
+++ b/fs_mgr/libdm/dm.cpp
@@ -302,6 +302,26 @@
     return true;
 }
 
+bool DeviceMapper::GetDeviceNumber(const std::string& name, dev_t* dev) {
+    struct dm_ioctl io;
+    InitIo(&io, name);
+    if (ioctl(fd_, DM_DEV_STATUS, &io) < 0) {
+        PLOG(WARNING) << "DM_DEV_STATUS failed for " << name;
+        return false;
+    }
+    *dev = io.dev;
+    return true;
+}
+
+bool DeviceMapper::GetDeviceString(const std::string& name, std::string* dev) {
+    dev_t num;
+    if (!GetDeviceNumber(name, &num)) {
+        return false;
+    }
+    *dev = std::to_string(major(num)) + ":" + std::to_string(minor(num));
+    return true;
+}
+
 bool DeviceMapper::GetTableStatus(const std::string& name, std::vector<TargetInfo>* table) {
     return GetTable(name, 0, table);
 }
@@ -368,5 +388,13 @@
     }
 }
 
+std::string DeviceMapper::GetTargetType(const struct dm_target_spec& spec) {
+    if (const void* p = memchr(spec.target_type, '\0', sizeof(spec.target_type))) {
+        ptrdiff_t length = reinterpret_cast<const char*>(p) - spec.target_type;
+        return std::string{spec.target_type, static_cast<size_t>(length)};
+    }
+    return std::string{spec.target_type, sizeof(spec.target_type)};
+}
+
 }  // namespace dm
 }  // namespace android
diff --git a/fs_mgr/libdm/dm_test.cpp b/fs_mgr/libdm/dm_test.cpp
index dc47c33..c5881dd 100644
--- a/fs_mgr/libdm/dm_test.cpp
+++ b/fs_mgr/libdm/dm_test.cpp
@@ -132,8 +132,8 @@
     // Define a 2-sector device, with each sector mapping to the first sector
     // of one of our loop devices.
     DmTable table;
-    ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(0, 1, loop_a.device(), 0)));
-    ASSERT_TRUE(table.AddTarget(make_unique<DmTargetLinear>(1, 1, loop_b.device(), 0)));
+    ASSERT_TRUE(table.Emplace<DmTargetLinear>(0, 1, loop_a.device(), 0));
+    ASSERT_TRUE(table.Emplace<DmTargetLinear>(1, 1, loop_b.device(), 0));
     ASSERT_TRUE(table.valid());
 
     TempDevice dev("libdm-test-dm-linear", table);
@@ -141,6 +141,16 @@
     ASSERT_FALSE(dev.path().empty());
     ASSERT_TRUE(dev.WaitForUdev());
 
+    auto& dm = DeviceMapper::Instance();
+
+    dev_t dev_number;
+    ASSERT_TRUE(dm.GetDeviceNumber(dev.name(), &dev_number));
+    ASSERT_NE(dev_number, 0);
+
+    std::string dev_string;
+    ASSERT_TRUE(dm.GetDeviceString(dev.name(), &dev_string));
+    ASSERT_FALSE(dev_string.empty());
+
     // Note: a scope is needed to ensure that there are no open descriptors
     // when we go to close the device.
     {
@@ -157,7 +167,6 @@
     }
 
     // Test GetTableStatus.
-    DeviceMapper& dm = DeviceMapper::Instance();
     vector<DeviceMapper::TargetInfo> targets;
     ASSERT_TRUE(dm.GetTableStatus(dev.name(), &targets));
     ASSERT_EQ(targets.size(), 2);
@@ -170,6 +179,10 @@
     EXPECT_EQ(targets[1].spec.sector_start, 1);
     EXPECT_EQ(targets[1].spec.length, 1);
 
+    // Test GetTargetType().
+    EXPECT_EQ(DeviceMapper::GetTargetType(targets[0].spec), std::string{"linear"});
+    EXPECT_EQ(DeviceMapper::GetTargetType(targets[1].spec), std::string{"linear"});
+
     // Normally the TestDevice destructor would delete this, but at least one
     // test should ensure that device deletion works.
     ASSERT_TRUE(dev.Destroy());
diff --git a/fs_mgr/libdm/include/libdm/dm.h b/fs_mgr/libdm/include/libdm/dm.h
index d7e8aa9..afcb090 100644
--- a/fs_mgr/libdm/include/libdm/dm.h
+++ b/fs_mgr/libdm/include/libdm/dm.h
@@ -20,6 +20,7 @@
 #include <fcntl.h>
 #include <linux/dm-ioctl.h>
 #include <linux/kdev_t.h>
+#include <linux/types.h>
 #include <stdint.h>
 #include <sys/sysmacros.h>
 #include <unistd.h>
@@ -111,6 +112,13 @@
     // parameter is not set.
     bool GetDmDevicePathByName(const std::string& name, std::string* path);
 
+    // Returns the dev_t for the named device-mapper node.
+    bool GetDeviceNumber(const std::string& name, dev_t* dev);
+
+    // Returns a major:minor string for the named device-mapper node, that can
+    // be used as inputs to DmTargets that take a block device.
+    bool GetDeviceString(const std::string& name, std::string* dev);
+
     // The only way to create a DeviceMapper object.
     static DeviceMapper& Instance();
 
@@ -136,6 +144,8 @@
     // mapper device from the kernel.
     bool GetTableInfo(const std::string& name, std::vector<TargetInfo>* table);
 
+    static std::string GetTargetType(const struct dm_target_spec& spec);
+
   private:
     // Maximum possible device mapper targets registered in the kernel.
     // This is only used to read the list of targets from kernel so we allocate
diff --git a/fs_mgr/libdm/include/libdm/loop_control.h b/fs_mgr/libdm/include/libdm/loop_control.h
index e6e83f4..6b4c2d8 100644
--- a/fs_mgr/libdm/include/libdm/loop_control.h
+++ b/fs_mgr/libdm/include/libdm/loop_control.h
@@ -35,6 +35,9 @@
     // Detach the loop device given by 'loopdev' from the attached backing file.
     bool Detach(const std::string& loopdev) const;
 
+    // Enable Direct I/O on a loop device. This requires kernel 4.9+.
+    static bool EnableDirectIo(int fd);
+
     LoopControl(const LoopControl&) = delete;
     LoopControl& operator=(const LoopControl&) = delete;
     LoopControl& operator=(LoopControl&&) = default;
diff --git a/fs_mgr/libdm/loop_control.cpp b/fs_mgr/libdm/loop_control.cpp
index 0beb1a6..16bf4b0 100644
--- a/fs_mgr/libdm/loop_control.cpp
+++ b/fs_mgr/libdm/loop_control.cpp
@@ -91,6 +91,27 @@
     return true;
 }
 
+bool LoopControl::EnableDirectIo(int fd) {
+#if !defined(LOOP_SET_BLOCK_SIZE)
+    static constexpr int LOOP_SET_BLOCK_SIZE = 0x4C09;
+#endif
+#if !defined(LOOP_SET_DIRECT_IO)
+    static constexpr int LOOP_SET_DIRECT_IO = 0x4C08;
+#endif
+
+    // Note: the block size has to be >= the logical block size of the underlying
+    // block device, *not* the filesystem block size.
+    if (ioctl(fd, LOOP_SET_BLOCK_SIZE, 4096)) {
+        PLOG(ERROR) << "Could not set loop device block size";
+        return false;
+    }
+    if (ioctl(fd, LOOP_SET_DIRECT_IO, 1)) {
+        PLOG(ERROR) << "Could not set loop direct IO";
+        return false;
+    }
+    return true;
+}
+
 LoopDevice::LoopDevice(int fd, bool auto_close) : fd_(fd), owns_fd_(auto_close) {
     Init();
 }
diff --git a/fs_mgr/libfiemap_writer/Android.bp b/fs_mgr/libfiemap_writer/Android.bp
index 32fc3d2..ed209aa 100644
--- a/fs_mgr/libfiemap_writer/Android.bp
+++ b/fs_mgr/libfiemap_writer/Android.bp
@@ -30,6 +30,7 @@
     ],
 
     static_libs: [
+        "libdm",
         "libext4_utils",
     ],
 
diff --git a/fs_mgr/libfiemap_writer/fiemap_writer.cpp b/fs_mgr/libfiemap_writer/fiemap_writer.cpp
index e3803d5..0a3ba6c 100644
--- a/fs_mgr/libfiemap_writer/fiemap_writer.cpp
+++ b/fs_mgr/libfiemap_writer/fiemap_writer.cpp
@@ -37,10 +37,13 @@
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
+#include <libdm/dm.h>
 
 namespace android {
 namespace fiemap_writer {
 
+using namespace android::dm;
+
 // We are expecting no more than 512 extents in a fiemap of the file we create.
 // If we find more, then it is treated as error for now.
 static constexpr const uint32_t kMaxExtents = 512;
@@ -86,15 +89,75 @@
     return true;
 }
 
-static bool DeviceMapperStackPop(const std::string& bdev, std::string* bdev_raw) {
-    // TODO: Stop popping the device mapper stack if dm-linear target is found
-    if (!::android::base::StartsWith(bdev, "dm-")) {
-        // We are at the bottom of the device mapper stack.
-        *bdev_raw = bdev;
+static bool ValidateDmTarget(const DeviceMapper::TargetInfo& target) {
+    const auto& entry = target.spec;
+    if (entry.sector_start != 0) {
+        LOG(INFO) << "Stopping at target with non-zero starting sector";
+        return false;
+    }
+
+    auto target_type = DeviceMapper::GetTargetType(entry);
+    if (target_type == "bow" || target_type == "default-key" || target_type == "crypt") {
+        return true;
+    }
+    if (target_type == "linear") {
+        auto pieces = android::base::Split(target.data, " ");
+        if (pieces[1] != "0") {
+            LOG(INFO) << "Stopping at complex linear target with non-zero starting sector: "
+                      << pieces[1];
+            return false;
+        }
         return true;
     }
 
-    std::string dm_leaf_dir = ::android::base::StringPrintf("/sys/block/%s/slaves", bdev.c_str());
+    LOG(INFO) << "Stopping at complex target type " << target_type;
+    return false;
+}
+
+static bool DeviceMapperStackPop(const std::string& bdev, std::string* bdev_raw) {
+    *bdev_raw = bdev;
+
+    if (!::android::base::StartsWith(bdev, "dm-")) {
+        // We are at the bottom of the device mapper stack.
+        return true;
+    }
+
+    // Get the device name.
+    auto dm_name_file = "/sys/block/" + bdev + "/dm/name";
+    std::string dm_name;
+    if (!android::base::ReadFileToString(dm_name_file, &dm_name)) {
+        PLOG(ERROR) << "Could not read file: " << dm_name_file;
+        return false;
+    }
+    dm_name = android::base::Trim(dm_name);
+
+    auto& dm = DeviceMapper::Instance();
+    std::vector<DeviceMapper::TargetInfo> table;
+    if (!dm.GetTableInfo(dm_name, &table)) {
+        LOG(ERROR) << "Could not read device-mapper table for " << dm_name << " at " << bdev;
+        return false;
+    }
+
+    // The purpose of libfiemap_writer is to provide an extent-based view into
+    // a file. This is difficult if devices are not layered in a 1:1 manner;
+    // we would have to translate and break up extents based on the actual
+    // block mapping. Since this is too complex, we simply stop processing
+    // the device-mapper stack if we encounter a complex case.
+    //
+    // It is up to the caller to decide whether stopping at a virtual block
+    // device is allowable. In most cases it is not, because we want either
+    // "userdata" or an external volume. It is useful for tests however.
+    // Callers can check by comparing the device number to that of userdata,
+    // or by checking whether is a device-mapper node.
+    if (table.size() > 1) {
+        LOG(INFO) << "Stopping at complex table for " << dm_name << " at " << bdev;
+        return true;
+    }
+    if (!ValidateDmTarget(table[0])) {
+        return true;
+    }
+
+    auto dm_leaf_dir = "/sys/block/" + bdev + "/slaves";
     auto d = std::unique_ptr<DIR, decltype(&closedir)>(opendir(dm_leaf_dir.c_str()), closedir);
     if (d == nullptr) {
         PLOG(ERROR) << "Failed to open: " << dm_leaf_dir;
diff --git a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h b/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h
index 9486122..ee79262 100644
--- a/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h
+++ b/fs_mgr/libfiemap_writer/include/libfiemap_writer/fiemap_writer.h
@@ -60,10 +60,17 @@
     // FiemapWriter::Open).
     static bool HasPinnedExtents(const std::string& file_path);
 
-    // Returns the underlying block device of a file. This will look past device-mapper layers.
-    // If an intermediate device-mapper layer would not maintain a 1:1 mapping (i.e. is a non-
-    // trivial dm-linear), then this will fail. If device-mapper nodes are encountered, then
-    // |uses_dm| will be set to true.
+    // Returns the underlying block device of a file. This will look past device-mapper layers
+    // as long as each layer would not change block mappings (i.e., dm-crypt, dm-bow, and dm-
+    // default-key tables are okay; dm-linear is not). If a mapping such as dm-linear is found,
+    // it will be returned in place of any physical block device.
+    //
+    // It is the caller's responsibility to check whether the returned block device is acceptable.
+    // Gsid, for example, will only accept /dev/block/by-name/userdata as the bottom device.
+    // Callers can check the device name (dm- or loop prefix), inspect sysfs, or compare the major
+    // number against a boot device.
+    //
+    // If device-mapper nodes were encountered, then |uses_dm| will be set to true.
     static bool GetBlockDeviceForFile(const std::string& file_path, std::string* bdev_path,
                                       bool* uses_dm = nullptr);
 
diff --git a/fs_mgr/liblp/Android.bp b/fs_mgr/liblp/Android.bp
index 7039994..b504161 100644
--- a/fs_mgr/liblp/Android.bp
+++ b/fs_mgr/liblp/Android.bp
@@ -14,6 +14,16 @@
 // limitations under the License.
 //
 
+liblp_lib_deps = [
+    "libbase",
+    "liblog",
+    "libcrypto",
+    "libcrypto_utils",
+    "libsparse",
+    "libext4_utils",
+    "libz",
+]
+
 cc_library {
     name: "liblp",
     host_supported: true,
@@ -30,15 +40,7 @@
         "utility.cpp",
         "writer.cpp",
     ],
-    shared_libs: [
-        "libbase",
-        "liblog",
-        "libcrypto",
-        "libcrypto_utils",
-        "libsparse",
-        "libext4_utils",
-        "libz",
-    ],
+    shared_libs: liblp_lib_deps,
     target: {
         windows: {
             enabled: true,
@@ -53,24 +55,28 @@
 }
 
 cc_test {
-    name: "liblp_test",
+    name: "liblp_test_static",
     defaults: ["fs_mgr_defaults"],
     cppflags: [
         "-Wno-unused-parameter",
     ],
     static_libs: [
         "libgmock",
-    ],
-    shared_libs: [
-        "liblp",
-        "libbase",
         "libfs_mgr",
-        "libsparse",
-    ],
+        "liblp",
+    ] + liblp_lib_deps,
+    stl: "libc++_static",
     srcs: [
         "builder_test.cpp",
         "io_test.cpp",
         "test_partition_opener.cpp",
         "utility_test.cpp",
     ],
+    target: {
+        android: {
+            static_libs: [
+                "libcutils",
+            ],
+        },
+    },
 }
diff --git a/fs_mgr/liblp/AndroidTest.xml b/fs_mgr/liblp/AndroidTest.xml
index 007a302..fe1002c 100644
--- a/fs_mgr/liblp/AndroidTest.xml
+++ b/fs_mgr/liblp/AndroidTest.xml
@@ -21,8 +21,8 @@
     </target_preparer>
     <test class="com.android.tradefed.testtype.VtsMultiDeviceTest">
       <option name="test-module-name" value="VtsKernelLiblpTest"/>
-        <option name="binary-test-source" value="_32bit::DATA/nativetest/liblp_test/liblp_test" />
-        <option name="binary-test-source" value="_64bit::DATA/nativetest64/liblp_test/liblp_test" />
+        <option name="binary-test-source" value="_32bit::DATA/nativetest/liblp_test_static/liblp_test_static" />
+        <option name="binary-test-source" value="_64bit::DATA/nativetest64/liblp_test_static/liblp_test_static" />
         <option name="binary-test-type" value="gtest"/>
         <option name="test-timeout" value="1m"/>
         <option name="precondition-first-api-level" value="29" />
diff --git a/fs_mgr/liblp/builder.cpp b/fs_mgr/liblp/builder.cpp
index 41c01da..25a042f 100644
--- a/fs_mgr/liblp/builder.cpp
+++ b/fs_mgr/liblp/builder.cpp
@@ -214,7 +214,7 @@
     sABOverrideValue = ab_device;
 }
 
-MetadataBuilder::MetadataBuilder() : auto_slot_suffixing_(false), ignore_slot_suffixing_(false) {
+MetadataBuilder::MetadataBuilder() : auto_slot_suffixing_(false) {
     memset(&geometry_, 0, sizeof(geometry_));
     geometry_.magic = LP_METADATA_GEOMETRY_MAGIC;
     geometry_.struct_size = sizeof(geometry_);
@@ -443,11 +443,6 @@
         LERROR << "Could not find partition group: " << group_name;
         return nullptr;
     }
-    if (IsABDevice() && !auto_slot_suffixing_ && name != "scratch" && !ignore_slot_suffixing_ &&
-        GetPartitionSlotSuffix(name).empty()) {
-        LERROR << "Unsuffixed partition not allowed on A/B device: " << name;
-        return nullptr;
-    }
     partitions_.push_back(std::make_unique<Partition>(name, group_name, attributes));
     return partitions_.back().get();
 }
@@ -1049,10 +1044,6 @@
     auto_slot_suffixing_ = true;
 }
 
-void MetadataBuilder::IgnoreSlotSuffixing() {
-    ignore_slot_suffixing_ = true;
-}
-
 bool MetadataBuilder::IsABDevice() const {
     if (sABOverrideSet) {
         return sABOverrideValue;
diff --git a/fs_mgr/liblp/builder_test.cpp b/fs_mgr/liblp/builder_test.cpp
index 46bfe92..34c68d4 100644
--- a/fs_mgr/liblp/builder_test.cpp
+++ b/fs_mgr/liblp/builder_test.cpp
@@ -106,6 +106,13 @@
     EXPECT_EQ(extent->num_sectors(), 32768 / LP_SECTOR_SIZE);
     EXPECT_EQ(extent->physical_sector(), 32);
 
+    auto exported = builder->Export();
+    ASSERT_NE(exported, nullptr);
+    ASSERT_EQ(FindPartition(*exported.get(), "not found"), nullptr);
+    auto entry = FindPartition(*exported.get(), "system");
+    ASSERT_NE(entry, nullptr);
+    ASSERT_EQ(GetPartitionSize(*exported.get(), *entry), 32768);
+
     // Test shrinking to 0.
     builder->ResizePartition(system, 0);
     EXPECT_EQ(system->size(), 0);
@@ -765,15 +772,6 @@
     EXPECT_FALSE(builder->ImportPartitions(*exported.get(), {"system"}));
 }
 
-TEST_F(BuilderTest, UnsuffixedPartitions) {
-    MetadataBuilder::OverrideABForTesting(true);
-    unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(1024 * 1024, 1024, 2);
-    ASSERT_NE(builder, nullptr);
-
-    ASSERT_EQ(builder->AddPartition("system", 0), nullptr);
-    ASSERT_NE(builder->AddPartition("system_a", 0), nullptr);
-}
-
 TEST_F(BuilderTest, ABExtents) {
     BlockDeviceInfo device_info("super", 10_GiB, 768 * 1024, 0, 4096);
 
diff --git a/fs_mgr/liblp/include/liblp/builder.h b/fs_mgr/liblp/include/liblp/builder.h
index c706f2a..e70c552 100644
--- a/fs_mgr/liblp/include/liblp/builder.h
+++ b/fs_mgr/liblp/include/liblp/builder.h
@@ -345,7 +345,6 @@
     std::vector<std::unique_ptr<PartitionGroup>> groups_;
     std::vector<LpMetadataBlockDevice> block_devices_;
     bool auto_slot_suffixing_;
-    bool ignore_slot_suffixing_;
 };
 
 // Read BlockDeviceInfo for a given block device. This always returns false
diff --git a/fs_mgr/liblp/include/liblp/liblp.h b/fs_mgr/liblp/include/liblp/liblp.h
index d3a7b93..135a1b3 100644
--- a/fs_mgr/liblp/include/liblp/liblp.h
+++ b/fs_mgr/liblp/include/liblp/liblp.h
@@ -107,6 +107,10 @@
 std::string SlotSuffixForSlotNumber(uint32_t slot_number);
 std::string GetPartitionSlotSuffix(const std::string& partition_name);
 
+// Helpers for common functions.
+const LpMetadataPartition* FindPartition(const LpMetadata& metadata, const std::string& name);
+uint64_t GetPartitionSize(const LpMetadata& metadata, const LpMetadataPartition& partition);
+
 }  // namespace fs_mgr
 }  // namespace android
 
diff --git a/fs_mgr/liblp/partition_opener.cpp b/fs_mgr/liblp/partition_opener.cpp
index 3b12213..cc4a882 100644
--- a/fs_mgr/liblp/partition_opener.cpp
+++ b/fs_mgr/liblp/partition_opener.cpp
@@ -64,6 +64,12 @@
         PERROR << __PRETTY_FUNCTION__ << "BLKALIGNOFF failed on " << block_device;
         return false;
     }
+    // The kernel can return -1 here when misaligned devices are stacked (i.e.
+    // device-mapper).
+    if (alignment_offset == -1) {
+        alignment_offset = 0;
+    }
+
     int logical_block_size;
     if (ioctl(fd, BLKSSZGET, &logical_block_size) < 0) {
         PERROR << __PRETTY_FUNCTION__ << "BLKSSZGET failed on " << block_device;
diff --git a/fs_mgr/liblp/utility.cpp b/fs_mgr/liblp/utility.cpp
index 72a3c57..338b525 100644
--- a/fs_mgr/liblp/utility.cpp
+++ b/fs_mgr/liblp/utility.cpp
@@ -135,6 +135,24 @@
     return list;
 }
 
+const LpMetadataPartition* FindPartition(const LpMetadata& metadata, const std::string& name) {
+    for (const auto& partition : metadata.partitions) {
+        if (GetPartitionName(partition) == name) {
+            return &partition;
+        }
+    }
+    return nullptr;
+}
+
+uint64_t GetPartitionSize(const LpMetadata& metadata, const LpMetadataPartition& partition) {
+    uint64_t total_size = 0;
+    for (uint32_t i = 0; i < partition.num_extents; i++) {
+        const auto& extent = metadata.extents[partition.first_extent_index + i];
+        total_size += extent.num_sectors * LP_SECTOR_SIZE;
+    }
+    return total_size;
+}
+
 std::string GetPartitionSlotSuffix(const std::string& partition_name) {
     if (partition_name.size() <= 2) {
         return "";
diff --git a/fs_mgr/tests/Android.bp b/fs_mgr/tests/Android.bp
index eb9f525..83668e9 100644
--- a/fs_mgr/tests/Android.bp
+++ b/fs_mgr/tests/Android.bp
@@ -25,6 +25,7 @@
         "libfstab",
     ],
     srcs: [
+        "file_wait_test.cpp",
         "fs_mgr_test.cpp",
     ],
 
diff --git a/fs_mgr/tests/file_wait_test.cpp b/fs_mgr/tests/file_wait_test.cpp
new file mode 100644
index 0000000..cc8b143
--- /dev/null
+++ b/fs_mgr/tests/file_wait_test.cpp
@@ -0,0 +1,91 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <chrono>
+#include <string>
+#include <thread>
+
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+#include <fs_mgr/file_wait.h>
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+using android::base::unique_fd;
+using android::fs_mgr::WaitForFile;
+using android::fs_mgr::WaitForFileDeleted;
+
+class FileWaitTest : public ::testing::Test {
+  protected:
+    void SetUp() override {
+        const ::testing::TestInfo* tinfo = ::testing::UnitTest::GetInstance()->current_test_info();
+        test_file_ = temp_dir_.path + "/"s + tinfo->name();
+    }
+
+    void TearDown() override { unlink(test_file_.c_str()); }
+
+    TemporaryDir temp_dir_;
+    std::string test_file_;
+};
+
+TEST_F(FileWaitTest, FileExists) {
+    unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    ASSERT_GE(fd, 0);
+
+    ASSERT_TRUE(WaitForFile(test_file_, 500ms));
+    ASSERT_FALSE(WaitForFileDeleted(test_file_, 500ms));
+}
+
+TEST_F(FileWaitTest, FileDoesNotExist) {
+    ASSERT_FALSE(WaitForFile(test_file_, 500ms));
+    ASSERT_TRUE(WaitForFileDeleted(test_file_, 500ms));
+}
+
+TEST_F(FileWaitTest, CreateAsync) {
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    });
+    EXPECT_TRUE(WaitForFile(test_file_, 3s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, CreateOtherAsync) {
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+    });
+    EXPECT_FALSE(WaitForFile(test_file_ + ".wontexist", 2s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, DeleteAsync) {
+    // Note: need to close the file, otherwise inotify considers it not deleted.
+    {
+        unique_fd fd(open(test_file_.c_str(), O_CREAT | O_TRUNC | O_RDWR, 0700));
+        ASSERT_GE(fd, 0);
+    }
+
+    std::thread thread([this] {
+        std::this_thread::sleep_for(std::chrono::seconds(1));
+        unlink(test_file_.c_str());
+    });
+    EXPECT_TRUE(WaitForFileDeleted(test_file_, 3s));
+    thread.join();
+}
+
+TEST_F(FileWaitTest, BadPath) {
+    ASSERT_FALSE(WaitForFile("/this/path/does/not/exist", 5ms));
+    EXPECT_EQ(errno, ENOENT);
+}
diff --git a/init/Android.bp b/init/Android.bp
index fa0a35c..9c1ed15 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -63,6 +63,7 @@
         "libavb",
         "libc++fs",
         "libcgrouprc_format",
+        "libmodprobe",
         "libprotobuf-cpp-lite",
         "libpropertyinfoserializer",
         "libpropertyinfoparser",
diff --git a/init/Android.mk b/init/Android.mk
index 0a3e8c7..b24f757 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -110,6 +110,7 @@
     libdexfile_support \
     libunwindstack \
     libbacktrace \
+    libmodprobe \
 
 LOCAL_SANITIZE := signed-integer-overflow
 # First stage init is weird: it may start without stdout/stderr, and no /proc.
diff --git a/init/README.md b/init/README.md
index 6868378..8179bff 100644
--- a/init/README.md
+++ b/init/README.md
@@ -196,9 +196,9 @@
 
 `interface <interface name> <instance name>`
 > Associates this service with a list of the HIDL services that it provides. The interface name
-  must be a fully-qualified name and not a value name. This is used to allow hwservicemanager to
-  lazily start services. When multiple interfaces are served, this tag should be used multiple
-  times.
+  must be a fully-qualified name and not a value name. For instance, this is used to allow
+  hwservicemanager to lazily start services. When multiple interfaces are served, this tag should
+  be used multiple times.
   For example: interface vendor.foo.bar@1.0::IBaz default
 
 `ioprio <class> <priority>`
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 2a583e8..44cac4b 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -1076,10 +1076,7 @@
 
 static Result<void> do_parse_apex_configs(const BuiltinArguments& args) {
     glob_t glob_result;
-    // @ is added to filter out the later paths, which are bind mounts of the places
-    // where the APEXes are really mounted at. Otherwise, we will parse the
-    // same file twice.
-    static constexpr char glob_pattern[] = "/apex/*@*/etc/*.rc";
+    static constexpr char glob_pattern[] = "/apex/*/etc/*.rc";
     const int ret = glob(glob_pattern, GLOB_MARK, nullptr, &glob_result);
     if (ret != 0 && ret != GLOB_NOMATCH) {
         globfree(&glob_result);
@@ -1088,7 +1085,15 @@
     std::vector<std::string> configs;
     Parser parser = CreateServiceOnlyParser(ServiceList::GetInstance());
     for (size_t i = 0; i < glob_result.gl_pathc; i++) {
-        configs.emplace_back(glob_result.gl_pathv[i]);
+        std::string path = glob_result.gl_pathv[i];
+        // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to
+        // /apex/<name> paths, so unless we filter them out, we will parse the
+        // same file twice.
+        std::vector<std::string> paths = android::base::Split(path, "/");
+        if (paths.size() >= 2 && paths[1].find('@') != std::string::npos) {
+            continue;
+        }
+        configs.push_back(path);
     }
     globfree(&glob_result);
 
diff --git a/init/first_stage_init.cpp b/init/first_stage_init.cpp
index 5d64f41..17387e2 100644
--- a/init/first_stage_init.cpp
+++ b/init/first_stage_init.cpp
@@ -33,6 +33,7 @@
 #include <android-base/chrono_utils.h>
 #include <android-base/file.h>
 #include <android-base/logging.h>
+#include <modprobe/modprobe.h>
 #include <private/android_filesystem_config.h>
 
 #include "debug_ramdisk.h"
@@ -192,6 +193,11 @@
         old_root_dir.reset();
     }
 
+    Modprobe m({"/lib/modules"});
+    if (!m.LoadListedModules()) {
+        LOG(FATAL) << "Failed to load kernel modules";
+    }
+
     if (ForceNormalBoot()) {
         mkdir("/first_stage_ramdisk", 0755);
         // SwitchRoot() must be called with a mount point as the target, so we bind mount the
diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp
index a3baeb1..33373d4 100644
--- a/init/keychords_test.cpp
+++ b/init/keychords_test.cpp
@@ -29,7 +29,6 @@
 #include <vector>
 
 #include <android-base/properties.h>
-#include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <gtest/gtest.h>
 
diff --git a/init/keyword_map.h b/init/keyword_map.h
index c95fc73..7837bb3 100644
--- a/init/keyword_map.h
+++ b/init/keyword_map.h
@@ -14,14 +14,11 @@
  * limitations under the License.
  */
 
-#ifndef _INIT_KEYWORD_MAP_H_
-#define _INIT_KEYWORD_MAP_H_
+#pragma once
 
 #include <map>
 #include <string>
 
-#include <android-base/stringprintf.h>
-
 #include "result.h"
 
 namespace android {
@@ -37,8 +34,6 @@
     }
 
     const Result<Function> FindFunction(const std::vector<std::string>& args) const {
-        using android::base::StringPrintf;
-
         if (args.empty()) return Error() << "Keyword needed, but not provided";
 
         auto& keyword = args[0];
@@ -46,7 +41,7 @@
 
         auto function_info_it = map().find(keyword);
         if (function_info_it == map().end()) {
-            return Error() << StringPrintf("Invalid keyword '%s'", keyword.c_str());
+            return Errorf("Invalid keyword '{}'", keyword);
         }
 
         auto function_info = function_info_it->second;
@@ -54,17 +49,17 @@
         auto min_args = std::get<0>(function_info);
         auto max_args = std::get<1>(function_info);
         if (min_args == max_args && num_args != min_args) {
-            return Error() << StringPrintf("%s requires %zu argument%s", keyword.c_str(), min_args,
-                                           (min_args > 1 || min_args == 0) ? "s" : "");
+            return Errorf("{} requires {} argument{}", keyword, min_args,
+                          (min_args > 1 || min_args == 0) ? "s" : "");
         }
 
         if (num_args < min_args || num_args > max_args) {
             if (max_args == std::numeric_limits<decltype(max_args)>::max()) {
-                return Error() << StringPrintf("%s requires at least %zu argument%s",
-                                               keyword.c_str(), min_args, min_args > 1 ? "s" : "");
+                return Errorf("{} requires at least {} argument{}", keyword, min_args,
+                              min_args > 1 ? "s" : "");
             } else {
-                return Error() << StringPrintf("%s requires between %zu and %zu arguments",
-                                               keyword.c_str(), min_args, max_args);
+                return Errorf("{} requires between {} and {} arguments", keyword, min_args,
+                              max_args);
             }
         }
 
@@ -79,5 +74,3 @@
 
 }  // namespace init
 }  // namespace android
-
-#endif
diff --git a/init/modalias_handler.cpp b/init/modalias_handler.cpp
index a511156..07b05d8 100644
--- a/init/modalias_handler.cpp
+++ b/init/modalias_handler.cpp
@@ -16,147 +16,20 @@
 
 #include "modalias_handler.h"
 
-#include <fnmatch.h>
-#include <sys/syscall.h>
-
-#include <algorithm>
-#include <functional>
 #include <string>
 #include <vector>
 
-#include <android-base/chrono_utils.h>
-#include <android-base/logging.h>
-#include <android-base/unique_fd.h>
-
-#include "parser.h"
+#include <modprobe/modprobe.h>
 
 namespace android {
 namespace init {
 
-Result<void> ModaliasHandler::ParseDepCallback(std::vector<std::string>&& args) {
-    std::vector<std::string> deps;
-
-    // Set first item as our modules path
-    std::string::size_type pos = args[0].find(':');
-    if (pos != std::string::npos) {
-        deps.emplace_back(args[0].substr(0, pos));
-    } else {
-        return Error() << "dependency lines must start with name followed by ':'";
-    }
-
-    // Remaining items are dependencies of our module
-    for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
-        deps.push_back(*arg);
-    }
-
-    // Key is striped module name to match names in alias file
-    std::size_t start = args[0].find_last_of('/');
-    std::size_t end = args[0].find(".ko:");
-    if ((end - start) <= 1) return Error() << "malformed dependency line";
-    auto mod_name = args[0].substr(start + 1, (end - start) - 1);
-    // module names can have '-', but their file names will have '_'
-    std::replace(mod_name.begin(), mod_name.end(), '-', '_');
-    this->module_deps_[mod_name] = deps;
-
-    return {};
-}
-
-Result<void> ModaliasHandler::ParseAliasCallback(std::vector<std::string>&& args) {
-    auto it = args.begin();
-    const std::string& type = *it++;
-
-    if (type != "alias") {
-        return Error() << "we only handle alias lines, got: " << type;
-    }
-
-    if (args.size() != 3) {
-        return Error() << "alias lines must have 3 entries";
-    }
-
-    std::string& alias = *it++;
-    std::string& module_name = *it++;
-    this->module_aliases_.emplace_back(alias, module_name);
-
-    return {};
-}
-
-ModaliasHandler::ModaliasHandler() {
-    using namespace std::placeholders;
-
-    static const std::string base_paths[] = {
-            "/vendor/lib/modules/",
-            "/lib/modules/",
-            "/odm/lib/modules/",
-    };
-
-    Parser alias_parser;
-    auto alias_callback = std::bind(&ModaliasHandler::ParseAliasCallback, this, _1);
-    alias_parser.AddSingleLineParser("alias", alias_callback);
-    for (const auto& base_path : base_paths) alias_parser.ParseConfig(base_path + "modules.alias");
-
-    Parser dep_parser;
-    auto dep_callback = std::bind(&ModaliasHandler::ParseDepCallback, this, _1);
-    dep_parser.AddSingleLineParser("", dep_callback);
-    for (const auto& base_path : base_paths) dep_parser.ParseConfig(base_path + "modules.dep");
-}
-
-Result<void> ModaliasHandler::Insmod(const std::string& path_name, const std::string& args) {
-    base::unique_fd fd(
-            TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
-    if (fd == -1) return ErrnoError() << "Could not open module '" << path_name << "'";
-
-    int ret = syscall(__NR_finit_module, fd.get(), args.c_str(), 0);
-    if (ret != 0) {
-        if (errno == EEXIST) {
-            // Module already loaded
-            return {};
-        }
-        return ErrnoError() << "Failed to insmod '" << path_name << "' with args '" << args << "'";
-    }
-
-    LOG(INFO) << "Loaded kernel module " << path_name;
-    return {};
-}
-
-Result<void> ModaliasHandler::InsmodWithDeps(const std::string& module_name,
-                                             const std::string& args) {
-    if (module_name.empty()) {
-        return Error() << "Need valid module name";
-    }
-
-    auto it = module_deps_.find(module_name);
-    if (it == module_deps_.end()) {
-        return Error() << "Module '" << module_name << "' not in dependency file";
-    }
-    auto& dependencies = it->second;
-
-    // load module dependencies in reverse order
-    for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
-        if (auto result = Insmod(*dep, ""); !result) return result;
-    }
-
-    // load target module itself with args
-    return Insmod(dependencies[0], args);
-}
+ModaliasHandler::ModaliasHandler(const std::vector<std::string>& base_paths)
+    : modprobe_(base_paths) {}
 
 void ModaliasHandler::HandleUevent(const Uevent& uevent) {
     if (uevent.modalias.empty()) return;
-
-    for (const auto& [alias, module] : module_aliases_) {
-        if (fnmatch(alias.c_str(), uevent.modalias.c_str(), 0) != 0) continue;  // Keep looking
-
-        LOG(DEBUG) << "Loading kernel module '" << module << "' for alias '" << uevent.modalias
-                   << "'";
-
-        if (auto result = InsmodWithDeps(module, ""); !result) {
-            LOG(ERROR) << "Cannot load module: " << result.error();
-            // try another one since there may be another match
-            continue;
-        }
-
-        // loading was successful
-        return;
-    }
+    modprobe_.LoadWithAliases(uevent.modalias, true);
 }
 
 }  // namespace init
diff --git a/init/modalias_handler.h b/init/modalias_handler.h
index 7d0afde..ce89a05 100644
--- a/init/modalias_handler.h
+++ b/init/modalias_handler.h
@@ -17,10 +17,10 @@
 #pragma once
 
 #include <string>
-#include <unordered_map>
 #include <vector>
 
-#include "result.h"
+#include <modprobe/modprobe.h>
+
 #include "uevent.h"
 #include "uevent_handler.h"
 
@@ -29,20 +29,13 @@
 
 class ModaliasHandler : public UeventHandler {
   public:
-    ModaliasHandler();
+    ModaliasHandler(const std::vector<std::string>&);
     virtual ~ModaliasHandler() = default;
 
     void HandleUevent(const Uevent& uevent) override;
 
   private:
-    Result<void> InsmodWithDeps(const std::string& module_name, const std::string& args);
-    Result<void> Insmod(const std::string& path_name, const std::string& args);
-
-    Result<void> ParseDepCallback(std::vector<std::string>&& args);
-    Result<void> ParseAliasCallback(std::vector<std::string>&& args);
-
-    std::vector<std::pair<std::string, std::string>> module_aliases_;
-    std::unordered_map<std::string, std::vector<std::string>> module_deps_;
+    Modprobe modprobe_;
 };
 
 }  // namespace init
diff --git a/init/property_service.cpp b/init/property_service.cpp
index ab5dd61..14bb819 100644
--- a/init/property_service.cpp
+++ b/init/property_service.cpp
@@ -645,8 +645,14 @@
                 while (isspace(*key)) key++;
             }
 
-            load_properties_from_file(fn, key, properties);
+            std::string raw_filename(fn);
+            std::string expanded_filename;
+            if (!expand_props(raw_filename, &expanded_filename)) {
+                LOG(ERROR) << "Could not expand filename '" << raw_filename << "'";
+                continue;
+            }
 
+            load_properties_from_file(expanded_filename.c_str(), key, properties);
         } else {
             value = strchr(key, '=');
             if (!value) continue;
diff --git a/init/reboot.cpp b/init/reboot.cpp
index fbc03c2..eaba3cc 100644
--- a/init/reboot.cpp
+++ b/init/reboot.cpp
@@ -41,7 +41,6 @@
 #include <android-base/logging.h>
 #include <android-base/macros.h>
 #include <android-base/properties.h>
-#include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 #include <bootloader_message/bootloader_message.h>
@@ -62,7 +61,6 @@
 
 using android::base::GetBoolProperty;
 using android::base::Split;
-using android::base::StringPrintf;
 using android::base::Timer;
 using android::base::unique_fd;
 using android::base::WriteStringToFile;
diff --git a/init/result.h b/init/result.h
index 8c1f91e..b70dd1b 100644
--- a/init/result.h
+++ b/init/result.h
@@ -22,6 +22,8 @@
 #include <android-base/result.h>
 
 using android::base::ErrnoError;
+using android::base::ErrnoErrorf;
 using android::base::Error;
+using android::base::Errorf;
 using android::base::Result;
 using android::base::ResultError;
diff --git a/init/service.cpp b/init/service.cpp
index b6a7c33..4fe374c 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -332,12 +332,11 @@
         const std::string& arg = args[i];
         int res = LookupCap(arg);
         if (res < 0) {
-            return Error() << StringPrintf("invalid capability '%s'", arg.c_str());
+            return Errorf("invalid capability '{}'", arg);
         }
         unsigned int cap = static_cast<unsigned int>(res);  // |res| is >= 0.
         if (cap > last_valid_cap) {
-            return Error() << StringPrintf("capability '%s' not supported by the kernel",
-                                           arg.c_str());
+            return Errorf("capability '{}' not supported by the kernel", arg);
         }
         (*capabilities_)[cap] = true;
     }
@@ -402,8 +401,8 @@
     if (!ParseInt(args[1], &proc_attr_.priority,
                   static_cast<int>(ANDROID_PRIORITY_HIGHEST),  // highest is negative
                   static_cast<int>(ANDROID_PRIORITY_LOWEST))) {
-        return Error() << StringPrintf("process priority value must be range %d - %d",
-                                       ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST);
+        return Errorf("process priority value must be range {} - {}", ANDROID_PRIORITY_HIGHEST,
+                      ANDROID_PRIORITY_LOWEST);
     }
     return {};
 }
diff --git a/init/ueventd.cpp b/init/ueventd.cpp
index 3e33f2f..3b9de0f 100644
--- a/init/ueventd.cpp
+++ b/init/ueventd.cpp
@@ -251,7 +251,8 @@
             std::move(ueventd_configuration.firmware_directories)));
 
     if (ueventd_configuration.enable_modalias_handling) {
-        uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>());
+        std::vector<std::string> base_paths = {"/odm/lib/modules", "/vendor/lib/modules"};
+        uevent_handlers.emplace_back(std::make_unique<ModaliasHandler>(base_paths));
     }
     UeventListener uevent_listener(ueventd_configuration.uevent_socket_rcvbuf_size);
 
diff --git a/libbacktrace/Android.bp b/libbacktrace/Android.bp
index e171155..9ece847 100644
--- a/libbacktrace/Android.bp
+++ b/libbacktrace/Android.bp
@@ -135,7 +135,6 @@
     defaults: ["libbacktrace_common"],
     host_supported: true,
     srcs: [
-        "backtrace_offline_test.cpp",
         "backtrace_test.cpp",
     ],
 
diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp
index 36640cd..a128623 100644
--- a/libbacktrace/UnwindStack.cpp
+++ b/libbacktrace/UnwindStack.cpp
@@ -129,22 +129,6 @@
   return true;
 }
 
-bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
-                              const backtrace_stackinfo_t& stack,
-                              std::vector<backtrace_frame_data_t>* frames,
-                              BacktraceUnwindError* error) {
-  UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
-  // Create the process memory from the stack data since this will almost
-  // always be different each unwind.
-  if (!offline_map->CreateProcessMemory(stack)) {
-    if (error != nullptr) {
-      error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
-    }
-    return false;
-  }
-  return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
-}
-
 UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
     : BacktraceCurrent(pid, tid, map) {}
 
@@ -171,7 +155,7 @@
 }
 
 UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
-    : BacktracePtrace(pid, tid, map), memory_(pid) {}
+    : BacktracePtrace(pid, tid, map), memory_(unwindstack::Memory::CreateProcessMemory(pid)) {}
 
 std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
   return GetMap()->GetFunctionName(pc, offset);
@@ -189,73 +173,5 @@
 }
 
 size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
-  return memory_.Read(addr, buffer, bytes);
-}
-
-UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
-                                       bool map_shared)
-    : Backtrace(pid, tid, map), arch_(arch) {
-  map_shared_ = map_shared;
-}
-
-bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
-  if (ucontext == nullptr) {
-    return false;
-  }
-
-  unwindstack::ArchEnum arch;
-  switch (arch_) {
-    case ARCH_ARM:
-      arch = unwindstack::ARCH_ARM;
-      break;
-    case ARCH_ARM64:
-      arch = unwindstack::ARCH_ARM64;
-      break;
-    case ARCH_X86:
-      arch = unwindstack::ARCH_X86;
-      break;
-    case ARCH_X86_64:
-      arch = unwindstack::ARCH_X86_64;
-      break;
-    default:
-      return false;
-  }
-
-  std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
-
-  return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
-}
-
-std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
-  return "";
-}
-
-size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
-  return 0;
-}
-
-bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
-  return false;
-}
-
-Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
-                                    const std::vector<backtrace_map_t>& maps,
-                                    const backtrace_stackinfo_t& stack) {
-  std::unique_ptr<UnwindStackOfflineMap> map(
-      reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
-  if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
-    return nullptr;
-  }
-  return new UnwindStackOffline(arch, pid, tid, map.release(), false);
-}
-
-Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
-  if (map == nullptr) {
-    return nullptr;
-  }
-  return new UnwindStackOffline(arch, pid, tid, map, true);
-}
-
-void Backtrace::SetGlobalElfCache(bool enable) {
-  unwindstack::Elf::SetCachingEnabled(enable);
+  return memory_->Read(addr, buffer, bytes);
 }
diff --git a/libbacktrace/UnwindStack.h b/libbacktrace/UnwindStack.h
index 4ec591d..47f6757 100644
--- a/libbacktrace/UnwindStack.h
+++ b/libbacktrace/UnwindStack.h
@@ -19,6 +19,7 @@
 
 #include <stdint.h>
 
+#include <memory>
 #include <string>
 
 #include <backtrace/BacktraceMap.h>
@@ -49,23 +50,7 @@
   size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) override;
 
  private:
-  unwindstack::MemoryRemote memory_;
-};
-
-class UnwindStackOffline : public Backtrace {
- public:
-  UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map, bool map_shared);
-
-  bool Unwind(size_t num_ignore_frames, void* context) override;
-
-  std::string GetFunctionNameRaw(uint64_t pc, uint64_t* offset) override;
-
-  size_t Read(uint64_t addr, uint8_t* buffer, size_t bytes) override;
-
-  bool ReadWord(uint64_t ptr, word_t* out_value) override;
-
- private:
-  ArchEnum arch_;
+  std::shared_ptr<unwindstack::Memory> memory_;
 };
 
 #endif  // _LIBBACKTRACE_UNWIND_STACK_H
diff --git a/libbacktrace/UnwindStackMap.cpp b/libbacktrace/UnwindStackMap.cpp
index 4518891..aa0b17c 100644
--- a/libbacktrace/UnwindStackMap.cpp
+++ b/libbacktrace/UnwindStackMap.cpp
@@ -132,43 +132,6 @@
   return process_memory_;
 }
 
-UnwindStackOfflineMap::UnwindStackOfflineMap(pid_t pid) : UnwindStackMap(pid) {}
-
-bool UnwindStackOfflineMap::Build() {
-  return false;
-}
-
-bool UnwindStackOfflineMap::Build(const std::vector<backtrace_map_t>& backtrace_maps) {
-  for (const backtrace_map_t& map : backtrace_maps) {
-    maps_.push_back(map);
-  }
-
-  std::sort(maps_.begin(), maps_.end(),
-            [](const backtrace_map_t& a, const backtrace_map_t& b) { return a.start < b.start; });
-
-  unwindstack::Maps* maps = new unwindstack::Maps;
-  stack_maps_.reset(maps);
-  for (const backtrace_map_t& map : maps_) {
-    maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias);
-  }
-  return true;
-}
-
-bool UnwindStackOfflineMap::CreateProcessMemory(const backtrace_stackinfo_t& stack) {
-  if (stack.start >= stack.end) {
-    return false;
-  }
-
-  // Create the process memory from the stack data.
-  if (memory_ == nullptr) {
-    memory_ = new unwindstack::MemoryOfflineBuffer(stack.data, stack.start, stack.end);
-    process_memory_.reset(memory_);
-  } else {
-    memory_->Reset(stack.data, stack.start, stack.end);
-  }
-  return true;
-}
-
 //-------------------------------------------------------------------------
 // BacktraceMap create function.
 //-------------------------------------------------------------------------
@@ -189,15 +152,3 @@
   }
   return map;
 }
-
-//-------------------------------------------------------------------------
-// BacktraceMap create offline function.
-//-------------------------------------------------------------------------
-BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps) {
-  UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid);
-  if (!map->Build(maps)) {
-    delete map;
-    return nullptr;
-  }
-  return map;
-}
diff --git a/libbacktrace/UnwindStackMap.h b/libbacktrace/UnwindStackMap.h
index e19b605..f0e7d8b 100644
--- a/libbacktrace/UnwindStackMap.h
+++ b/libbacktrace/UnwindStackMap.h
@@ -33,6 +33,7 @@
 #include <unwindstack/Elf.h>
 #include <unwindstack/JitDebug.h>
 #include <unwindstack/Maps.h>
+#include <unwindstack/Memory.h>
 
 // Forward declarations.
 class UnwindDexFile;
@@ -74,19 +75,4 @@
   unwindstack::ArchEnum arch_ = unwindstack::ARCH_UNKNOWN;
 };
 
-class UnwindStackOfflineMap : public UnwindStackMap {
- public:
-  UnwindStackOfflineMap(pid_t pid);
-  ~UnwindStackOfflineMap() = default;
-
-  bool Build() override;
-
-  bool Build(const std::vector<backtrace_map_t>& maps);
-
-  bool CreateProcessMemory(const backtrace_stackinfo_t& stack);
-
- private:
-  unwindstack::MemoryOfflineBuffer* memory_ = nullptr;
-};
-
 #endif  // _LIBBACKTRACE_UNWINDSTACK_MAP_H
diff --git a/libbacktrace/backtrace_offline_test.cpp b/libbacktrace/backtrace_offline_test.cpp
deleted file mode 100644
index 662fb99..0000000
--- a/libbacktrace/backtrace_offline_test.cpp
+++ /dev/null
@@ -1,397 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <inttypes.h>
-#include <pthread.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <functional>
-#include <memory>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include <android-base/file.h>
-#include <android-base/logging.h>
-#include <android-base/macros.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-#include <android-base/threads.h>
-#include <backtrace/Backtrace.h>
-#include <backtrace/BacktraceMap.h>
-
-#include <gtest/gtest.h>
-
-#include "BacktraceTest.h"
-
-struct FunctionSymbol {
-  std::string name;
-  uint64_t start;
-  uint64_t end;
-};
-
-static std::vector<FunctionSymbol> GetFunctionSymbols() {
-  std::vector<FunctionSymbol> symbols = {
-      {"unknown_start", 0, 0},
-      {"test_level_one", reinterpret_cast<uint64_t>(&BacktraceTest::test_level_one_), 0},
-      {"test_level_two", reinterpret_cast<uint64_t>(&BacktraceTest::test_level_two_), 0},
-      {"test_level_three", reinterpret_cast<uint64_t>(&BacktraceTest::test_level_three_), 0},
-      {"test_level_four", reinterpret_cast<uint64_t>(&BacktraceTest::test_level_four_), 0},
-      {"test_recursive_call", reinterpret_cast<uint64_t>(&BacktraceTest::test_recursive_call_), 0},
-      {"test_get_context_and_wait",
-       reinterpret_cast<uint64_t>(&BacktraceTest::test_get_context_and_wait_), 0},
-      {"unknown_end", static_cast<uint64_t>(-1), static_cast<uint64_t>(-1)},
-  };
-  std::sort(
-      symbols.begin(), symbols.end(),
-      [](const FunctionSymbol& s1, const FunctionSymbol& s2) { return s1.start < s2.start; });
-  for (size_t i = 0; i + 1 < symbols.size(); ++i) {
-    symbols[i].end = symbols[i + 1].start;
-  }
-  return symbols;
-}
-
-static std::string RawDataToHexString(const void* data, size_t size) {
-  const uint8_t* p = static_cast<const uint8_t*>(data);
-  std::string s;
-  for (size_t i = 0; i < size; ++i) {
-    s += android::base::StringPrintf("%02x", p[i]);
-  }
-  return s;
-}
-
-static void HexStringToRawData(const char* s, std::vector<uint8_t>* data, size_t size) {
-  for (size_t i = 0; i < size; ++i) {
-    int value;
-    sscanf(s, "%02x", &value);
-    data->push_back(value);
-    s += 2;
-  }
-}
-
-struct OfflineThreadArg {
-  std::vector<uint8_t> ucontext;
-  pid_t tid;
-  volatile int exit_flag;
-};
-
-static void* OfflineThreadFunc(void* arg) {
-  OfflineThreadArg* fn_arg = reinterpret_cast<OfflineThreadArg*>(arg);
-  fn_arg->tid = android::base::GetThreadId();
-  BacktraceTest::test_get_context_and_wait_(&fn_arg->ucontext, &fn_arg->exit_flag);
-  return nullptr;
-}
-
-std::string GetTestPath(const std::string& arch, const std::string& path) {
-  return android::base::GetExecutableDirectory() + "/testdata/" + arch + '/' + path;
-}
-
-// This test is disable because it is for generating test data.
-TEST_F(BacktraceTest, DISABLED_generate_offline_testdata) {
-  // Create a thread to generate the needed stack and registers information.
-  const size_t stack_size = 16 * 1024;
-  void* stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
-  ASSERT_NE(MAP_FAILED, stack);
-  uint64_t stack_addr = reinterpret_cast<uint64_t>(stack);
-  pthread_attr_t attr;
-  ASSERT_EQ(0, pthread_attr_init(&attr));
-  ASSERT_EQ(0, pthread_attr_setstack(&attr, reinterpret_cast<void*>(stack), stack_size));
-  pthread_t thread;
-  OfflineThreadArg arg;
-  arg.exit_flag = 0;
-  ASSERT_EQ(0, pthread_create(&thread, &attr, OfflineThreadFunc, &arg));
-  // Wait for the offline thread to generate the stack and context information.
-  sleep(1);
-  // Copy the stack information.
-  std::vector<uint8_t> stack_data(reinterpret_cast<uint8_t*>(stack),
-                                  reinterpret_cast<uint8_t*>(stack) + stack_size);
-  arg.exit_flag = 1;
-  ASSERT_EQ(0, pthread_join(thread, nullptr));
-  ASSERT_EQ(0, munmap(stack, stack_size));
-
-  std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid()));
-  ASSERT_TRUE(map != nullptr);
-
-  backtrace_stackinfo_t stack_info;
-  stack_info.start = stack_addr;
-  stack_info.end = stack_addr + stack_size;
-  stack_info.data = stack_data.data();
-
-  // Generate offline testdata.
-  std::string testdata;
-  // 1. Dump pid, tid
-  testdata += android::base::StringPrintf("pid: %d tid: %d\n", getpid(), arg.tid);
-  // 2. Dump maps
-  for (auto it = map->begin(); it != map->end(); ++it) {
-    const backtrace_map_t* entry = *it;
-    testdata +=
-        android::base::StringPrintf("map: start: %" PRIx64 " end: %" PRIx64 " offset: %" PRIx64
-                                    " load_bias: %" PRIx64 " flags: %d name: %s\n",
-                                    entry->start, entry->end, entry->offset, entry->load_bias,
-                                    entry->flags, entry->name.c_str());
-  }
-  // 3. Dump ucontext
-  testdata += android::base::StringPrintf("ucontext: %zu ", arg.ucontext.size());
-  testdata += RawDataToHexString(arg.ucontext.data(), arg.ucontext.size());
-  testdata.push_back('\n');
-
-  // 4. Dump stack
-  testdata += android::base::StringPrintf(
-      "stack: start: %" PRIx64 " end: %" PRIx64 " size: %zu ",
-      stack_info.start, stack_info.end, stack_data.size());
-  testdata += RawDataToHexString(stack_data.data(), stack_data.size());
-  testdata.push_back('\n');
-
-  // 5. Dump function symbols
-  std::vector<FunctionSymbol> function_symbols = GetFunctionSymbols();
-  for (const auto& symbol : function_symbols) {
-    testdata +=
-        android::base::StringPrintf("function: start: %" PRIx64 " end: %" PRIx64 " name: %s\n",
-                                    symbol.start, symbol.end, symbol.name.c_str());
-  }
-
-  ASSERT_TRUE(android::base::WriteStringToFile(testdata, "offline_testdata"));
-}
-
-// Return the name of the function which matches the address. Although we don't know the
-// exact end of each function, it is accurate enough for the tests.
-static std::string FunctionNameForAddress(uint64_t addr,
-                                          const std::vector<FunctionSymbol>& symbols) {
-  for (auto& symbol : symbols) {
-    if (addr >= symbol.start && addr < symbol.end) {
-      return symbol.name;
-    }
-  }
-  return "";
-}
-
-struct OfflineTestData {
-  int pid;
-  int tid;
-  std::vector<backtrace_map_t> maps;
-  std::vector<uint8_t> ucontext;
-  backtrace_stackinfo_t stack_info;
-  std::vector<uint8_t> stack;
-  std::vector<FunctionSymbol> symbols;
-};
-
-bool ReadOfflineTestData(const std::string offline_testdata_path, OfflineTestData* testdata) {
-  std::string s;
-  if (!android::base::ReadFileToString(offline_testdata_path, &s)) {
-    return false;
-  }
-  // Parse offline_testdata.
-  std::vector<std::string> lines = android::base::Split(s, "\n");
-  for (const auto& line : lines) {
-    if (android::base::StartsWith(line, "pid:")) {
-      sscanf(line.c_str(), "pid: %d tid: %d", &testdata->pid, &testdata->tid);
-    } else if (android::base::StartsWith(line, "map:")) {
-      testdata->maps.resize(testdata->maps.size() + 1);
-      backtrace_map_t& map = testdata->maps.back();
-      int pos;
-      sscanf(line.c_str(),
-             "map: start: %" SCNx64 " end: %" SCNx64 " offset: %" SCNx64 " load_bias: %" SCNx64
-             " flags: %d name: %n",
-             &map.start, &map.end, &map.offset, &map.load_bias, &map.flags, &pos);
-      map.name = android::base::Trim(line.substr(pos));
-    } else if (android::base::StartsWith(line, "ucontext:")) {
-      size_t size;
-      int pos;
-      testdata->ucontext.clear();
-      sscanf(line.c_str(), "ucontext: %zu %n", &size, &pos);
-      HexStringToRawData(&line[pos], &testdata->ucontext, size);
-    } else if (android::base::StartsWith(line, "stack:")) {
-      size_t size;
-      int pos;
-      sscanf(line.c_str(),
-             "stack: start: %" SCNx64 " end: %" SCNx64 " size: %zu %n",
-             &testdata->stack_info.start, &testdata->stack_info.end, &size, &pos);
-      CHECK_EQ(testdata->stack_info.end - testdata->stack_info.start, size);
-      testdata->stack.clear();
-      HexStringToRawData(&line[pos], &testdata->stack, size);
-      testdata->stack_info.data = testdata->stack.data();
-    } else if (android::base::StartsWith(line, "function:")) {
-      testdata->symbols.resize(testdata->symbols.size() + 1);
-      FunctionSymbol& symbol = testdata->symbols.back();
-      int pos;
-      sscanf(line.c_str(), "function: start: %" SCNx64 " end: %" SCNx64 " name: %n", &symbol.start,
-             &symbol.end, &pos);
-      symbol.name = line.substr(pos);
-    }
-  }
-  return true;
-}
-
-static void BacktraceOfflineTest(std::string arch_str, const std::string& testlib_name) {
-  const std::string testlib_path(GetTestPath(arch_str, testlib_name));
-  const std::string offline_testdata_path(GetTestPath(arch_str, "offline_testdata"));
-  OfflineTestData testdata;
-  ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata)) << "Failed " << arch_str;
-
-  // Fix path of libbacktrace_testlib.so.
-  for (auto& map : testdata.maps) {
-    if (map.name.find("libbacktrace_test.so") != std::string::npos) {
-      map.name = testlib_path;
-    }
-  }
-
-  Backtrace::ArchEnum arch;
-  if (arch_str == "arm") {
-    arch = Backtrace::ARCH_ARM;
-  } else if (arch_str == "arm64") {
-    arch = Backtrace::ARCH_ARM64;
-  } else if (arch_str == "x86") {
-    arch = Backtrace::ARCH_X86;
-  } else if (arch_str == "x86_64") {
-    arch = Backtrace::ARCH_X86_64;
-  } else {
-    abort();
-  }
-
-  std::unique_ptr<Backtrace> backtrace(Backtrace::CreateOffline(
-      arch, testdata.pid, testdata.tid, testdata.maps, testdata.stack_info));
-  ASSERT_TRUE(backtrace != nullptr) << "Failed " << arch_str;
-
-  ASSERT_TRUE(backtrace->Unwind(0, testdata.ucontext.data())) << "Failed " << arch_str;
-
-  // Collect pc values of the call stack frames.
-  std::vector<uint64_t> pc_values;
-  for (size_t i = 0; i < backtrace->NumFrames(); ++i) {
-    pc_values.push_back(backtrace->GetFrame(i)->pc);
-  }
-
-  size_t test_one_index = 0;
-  for (size_t i = 0; i < pc_values.size(); ++i) {
-    if (FunctionNameForAddress(pc_values[i], testdata.symbols) == "test_level_one") {
-      test_one_index = i;
-      break;
-    }
-  }
-
-  ASSERT_GE(test_one_index, 3u) << "Failed " << arch_str;
-  ASSERT_EQ("test_level_one", FunctionNameForAddress(pc_values[test_one_index], testdata.symbols))
-      << "Failed " << arch_str;
-  ASSERT_EQ("test_level_two", FunctionNameForAddress(pc_values[test_one_index - 1], testdata.symbols))
-      << "Failed " << arch_str;
-  ASSERT_EQ("test_level_three",
-            FunctionNameForAddress(pc_values[test_one_index - 2], testdata.symbols))
-      << "Failed " << arch_str;
-  ASSERT_EQ("test_level_four",
-            FunctionNameForAddress(pc_values[test_one_index - 3], testdata.symbols))
-      << "Failed " << arch_str;
-}
-
-// For now, these tests can only run on the given architectures.
-TEST_F(BacktraceTest, offline_eh_frame) {
-  BacktraceOfflineTest("arm64", "libbacktrace_test_eh_frame.so");
-  BacktraceOfflineTest("x86_64", "libbacktrace_test_eh_frame.so");
-}
-
-TEST_F(BacktraceTest, offline_debug_frame) {
-  BacktraceOfflineTest("arm", "libbacktrace_test_debug_frame.so");
-  BacktraceOfflineTest("x86", "libbacktrace_test_debug_frame.so");
-}
-
-TEST_F(BacktraceTest, offline_gnu_debugdata) {
-  BacktraceOfflineTest("arm", "libbacktrace_test_gnu_debugdata.so");
-  BacktraceOfflineTest("x86", "libbacktrace_test_gnu_debugdata.so");
-}
-
-TEST_F(BacktraceTest, offline_arm_exidx) {
-  BacktraceOfflineTest("arm", "libbacktrace_test_arm_exidx.so");
-}
-
-static void LibUnwindingTest(const std::string& arch_str, const std::string& testdata_name,
-                             const std::string& testlib_name) {
-  const std::string testlib_path(GetTestPath(arch_str, testlib_name));
-  struct stat st;
-  ASSERT_EQ(0, stat(testlib_path.c_str(), &st)) << "can't find testlib " << testlib_path;
-
-  const std::string offline_testdata_path(GetTestPath(arch_str, testdata_name));
-  OfflineTestData testdata;
-  ASSERT_TRUE(ReadOfflineTestData(offline_testdata_path, &testdata));
-
-  // Fix path of the testlib.
-  for (auto& map : testdata.maps) {
-    if (map.name.find(testlib_name) != std::string::npos) {
-      map.name = testlib_path;
-    }
-  }
-
-  Backtrace::ArchEnum arch;
-  if (arch_str == "arm") {
-    arch = Backtrace::ARCH_ARM;
-  } else if (arch_str == "arm64") {
-    arch = Backtrace::ARCH_ARM64;
-  } else if (arch_str == "x86") {
-    arch = Backtrace::ARCH_X86;
-  } else if (arch_str == "x86_64") {
-    arch = Backtrace::ARCH_X86_64;
-  } else {
-    ASSERT_TRUE(false) << "Unsupported arch " << arch_str;
-    abort();
-  }
-
-  // Do offline backtrace.
-  std::unique_ptr<Backtrace> backtrace(Backtrace::CreateOffline(
-      arch, testdata.pid, testdata.tid, testdata.maps, testdata.stack_info));
-  ASSERT_TRUE(backtrace != nullptr);
-
-  ASSERT_TRUE(backtrace->Unwind(0, testdata.ucontext.data()));
-
-  ASSERT_EQ(testdata.symbols.size(), backtrace->NumFrames());
-  for (size_t i = 0; i < backtrace->NumFrames(); ++i) {
-    std::string name = FunctionNameForAddress(backtrace->GetFrame(i)->rel_pc, testdata.symbols);
-    ASSERT_EQ(name, testdata.symbols[i].name);
-  }
-  ASSERT_TRUE(backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED ||
-              backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_MAP_MISSING ||
-              backtrace->GetError().error_code == BACKTRACE_UNWIND_ERROR_REPEATED_FRAME);
-}
-
-// This test tests the situation that ranges of functions covered by .eh_frame and .ARM.exidx
-// overlap with each other, which appears in /system/lib/libart.so.
-TEST_F(BacktraceTest, offline_unwind_mix_eh_frame_and_arm_exidx) {
-  LibUnwindingTest("arm", "offline_testdata_for_libart", "libart.so");
-}
-
-TEST_F(BacktraceTest, offline_debug_frame_with_load_bias) {
-  LibUnwindingTest("arm", "offline_testdata_for_libandroid_runtime", "libandroid_runtime.so");
-}
-
-TEST_F(BacktraceTest, offline_try_armexidx_after_debug_frame) {
-  LibUnwindingTest("arm", "offline_testdata_for_libGLESv2_adreno", "libGLESv2_adreno.so");
-}
-
-TEST_F(BacktraceTest, offline_cie_with_P_augmentation) {
-  // Make sure we can unwind through functions with CIE entry containing P augmentation, which
-  // makes unwinding library reading personality handler from memory. One example is
-  // /system/lib64/libskia.so.
-  LibUnwindingTest("arm64", "offline_testdata_for_libskia", "libskia.so");
-}
-
-TEST_F(BacktraceTest, offline_empty_eh_frame_hdr) {
-  // Make sure we can unwind through libraries with empty .eh_frame_hdr section. One example is
-  // /vendor/lib64/egl/eglSubDriverAndroid.so.
-  LibUnwindingTest("arm64", "offline_testdata_for_eglSubDriverAndroid", "eglSubDriverAndroid.so");
-}
-
-TEST_F(BacktraceTest, offline_max_frames_limit) {
-  // The length of callchain can reach 256 when recording an application.
-  ASSERT_GE(MAX_BACKTRACE_FRAMES, 256);
-}
diff --git a/libbacktrace/include/backtrace/Backtrace.h b/libbacktrace/include/backtrace/Backtrace.h
index 404e7e8..664b531 100644
--- a/libbacktrace/include/backtrace/Backtrace.h
+++ b/libbacktrace/include/backtrace/Backtrace.h
@@ -126,24 +126,6 @@
   // If map is not NULL, the map is still owned by the caller.
   static Backtrace* Create(pid_t pid, pid_t tid, BacktraceMap* map = nullptr);
 
-  // Create an offline Backtrace object that can be used to do an unwind without a process
-  // that is still running. By default, information is only cached in the map
-  // file. If the calling code creates the map, data can be cached between
-  // unwinds. If not, all cached data will be destroyed when the Backtrace
-  // object is destroyed.
-  static Backtrace* CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
-                                  const std::vector<backtrace_map_t>& maps,
-                                  const backtrace_stackinfo_t& stack);
-  static Backtrace* CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map);
-
-  // Create an offline Backtrace object that can be used to do an unwind without a process
-  // that is still running. If cache_file is set to true, then elf information will be cached
-  // for this call. The cached information survives until the calling process ends. This means
-  // that subsequent calls to create offline Backtrace objects will continue to use the same
-  // cache. It also assumes that the elf files used for each offline unwind are the same.
-  static Backtrace* CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map,
-                                  const backtrace_stackinfo_t& stack, bool cache_file = false);
-
   virtual ~Backtrace();
 
   // Get the current stack trace and store in the backtrace_ structure.
@@ -153,11 +135,6 @@
                      std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
                      std::vector<std::string>* skip_names, BacktraceUnwindError* error = nullptr);
 
-  static bool UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
-                            const backtrace_stackinfo_t& stack_info,
-                            std::vector<backtrace_frame_data_t>* frames,
-                            BacktraceUnwindError* error = nullptr);
-
   // Get the function name and offset into the function given the pc.
   // If the string is empty, then no valid function name was found,
   // or the pc is not in any valid map.
diff --git a/libbacktrace/include/backtrace/BacktraceMap.h b/libbacktrace/include/backtrace/BacktraceMap.h
index c564271..f8d5058 100644
--- a/libbacktrace/include/backtrace/BacktraceMap.h
+++ b/libbacktrace/include/backtrace/BacktraceMap.h
@@ -69,8 +69,6 @@
   // is unsupported.
   static BacktraceMap* Create(pid_t pid, bool uncached = false);
 
-  static BacktraceMap* CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps);
-
   virtual ~BacktraceMap();
 
   class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> {
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index 6606030..5b5f2eb 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -46,19 +46,6 @@
 using android::base::EndsWith;
 using android::base::StartsWith;
 
-// My kingdom for <endian.h>
-static inline uint16_t get2LE(const uint8_t* src) {
-    return src[0] | (src[1] << 8);
-}
-
-static inline uint64_t get8LE(const uint8_t* src) {
-    uint32_t low, high;
-
-    low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
-    high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
-    return ((uint64_t)high << 32) | (uint64_t)low;
-}
-
 #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
 
 // Rules for directories.
@@ -333,7 +320,7 @@
 
         while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
             char* prefix;
-            uint16_t host_len = get2LE((const uint8_t*)&header.len);
+            uint16_t host_len = header.len;
             ssize_t len, remainder = host_len - sizeof(header);
             if (remainder <= 0) {
                 ALOGE("%s len is corrupted", conf[which][dir]);
@@ -358,10 +345,10 @@
             if (fs_config_cmp(dir, prefix, len, path, plen)) {
                 free(prefix);
                 close(fd);
-                *uid = get2LE((const uint8_t*)&(header.uid));
-                *gid = get2LE((const uint8_t*)&(header.gid));
-                *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
-                *capabilities = get8LE((const uint8_t*)&(header.capabilities));
+                *uid = header.uid;
+                *gid = header.gid;
+                *mode = (*mode & (~07777)) | header.mode;
+                *capabilities = header.capabilities;
                 return;
             }
             free(prefix);
@@ -379,21 +366,3 @@
     *mode = (*mode & (~07777)) | pc->mode;
     *capabilities = pc->capabilities;
 }
-
-ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
-    struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
-    size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
-
-    if ((length < len) || (len > UINT16_MAX)) {
-        return -ENOSPC;
-    }
-    memset(p, 0, len);
-    uint16_t host_len = len;
-    p->len = get2LE((const uint8_t*)&host_len);
-    p->mode = get2LE((const uint8_t*)&(pc->mode));
-    p->uid = get2LE((const uint8_t*)&(pc->uid));
-    p->gid = get2LE((const uint8_t*)&(pc->gid));
-    p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
-    strcpy(p->prefix, pc->prefix);
-    return len;
-}
diff --git a/libcutils/include/private/fs_config.h b/libcutils/include/private/fs_config.h
index 8926491..603cf1a 100644
--- a/libcutils/include/private/fs_config.h
+++ b/libcutils/include/private/fs_config.h
@@ -74,8 +74,6 @@
 void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
                unsigned* mode, uint64_t* capabilities);
 
-ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc);
-
 __END_DECLS
 
 #endif /* _LIBS_CUTILS_PRIVATE_FS_CONFIG_H */
diff --git a/libmodprobe/Android.bp b/libmodprobe/Android.bp
new file mode 100644
index 0000000..a2824d1
--- /dev/null
+++ b/libmodprobe/Android.bp
@@ -0,0 +1,30 @@
+cc_library_static {
+    name: "libmodprobe",
+    cflags: [
+        "-Werror",
+    ],
+    recovery_available: true,
+    srcs: [
+        "libmodprobe.cpp",
+        "libmodprobe_ext.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+    ],
+    export_include_dirs: ["include/"],
+}
+
+cc_test {
+    name: "libmodprobe_tests",
+    cflags: ["-Werror"],
+    shared_libs: [
+        "libbase",
+    ],
+    local_include_dirs: ["include/"],
+    srcs: [
+        "libmodprobe_test.cpp",
+        "libmodprobe.cpp",
+        "libmodprobe_ext_test.cpp",
+    ],
+    test_suites: ["device-tests"],
+}
diff --git a/libmodprobe/include/modprobe/modprobe.h b/libmodprobe/include/modprobe/modprobe.h
new file mode 100644
index 0000000..0ec766a
--- /dev/null
+++ b/libmodprobe/include/modprobe/modprobe.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+class Modprobe {
+  public:
+    Modprobe(const std::vector<std::string>&);
+
+    bool LoadListedModules();
+    bool LoadWithAliases(const std::string& module_name, bool strict);
+
+  private:
+    std::string MakeCanonical(const std::string& module_path);
+    bool InsmodWithDeps(const std::string& module_name);
+    bool Insmod(const std::string& path_name);
+    std::vector<std::string> GetDependencies(const std::string& module);
+    bool ModuleExists(const std::string& module_name);
+
+    bool ParseDepCallback(const std::string& base_path, const std::vector<std::string>& args);
+    bool ParseAliasCallback(const std::vector<std::string>& args);
+    bool ParseSoftdepCallback(const std::vector<std::string>& args);
+    bool ParseLoadCallback(const std::vector<std::string>& args);
+    bool ParseOptionsCallback(const std::vector<std::string>& args);
+    void ParseCfg(const std::string& cfg, std::function<bool(const std::vector<std::string>&)> f);
+
+    std::vector<std::pair<std::string, std::string>> module_aliases_;
+    std::unordered_map<std::string, std::vector<std::string>> module_deps_;
+    std::vector<std::pair<std::string, std::string>> module_pre_softdep_;
+    std::vector<std::pair<std::string, std::string>> module_post_softdep_;
+    std::vector<std::string> module_load_;
+    std::unordered_map<std::string, std::string> module_options_;
+};
diff --git a/libmodprobe/libmodprobe.cpp b/libmodprobe/libmodprobe.cpp
new file mode 100644
index 0000000..01cf2e3
--- /dev/null
+++ b/libmodprobe/libmodprobe.cpp
@@ -0,0 +1,321 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <modprobe/modprobe.h>
+
+#include <fnmatch.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <algorithm>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <android-base/chrono_utils.h>
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+
+std::string Modprobe::MakeCanonical(const std::string& module_path) {
+    auto start = module_path.find_last_of('/');
+    if (start == std::string::npos) {
+        start = 0;
+    } else {
+        start += 1;
+    }
+    auto end = module_path.size();
+    if (android::base::EndsWith(module_path, ".ko")) {
+        end -= 3;
+    }
+    if ((end - start) <= 1) {
+        LOG(ERROR) << "malformed module name: " << module_path;
+        return "";
+    }
+    std::string module_name = module_path.substr(start, end - start);
+    // module names can have '-', but their file names will have '_'
+    std::replace(module_name.begin(), module_name.end(), '-', '_');
+    return module_name;
+}
+
+bool Modprobe::ParseDepCallback(const std::string& base_path,
+                                const std::vector<std::string>& args) {
+    std::vector<std::string> deps;
+    std::string prefix = "";
+
+    // Set first item as our modules path
+    std::string::size_type pos = args[0].find(':');
+    if (args[0][0] != '/') {
+        prefix = base_path + "/";
+    }
+    if (pos != std::string::npos) {
+        deps.emplace_back(prefix + args[0].substr(0, pos));
+    } else {
+        LOG(ERROR) << "dependency lines must start with name followed by ':'";
+    }
+
+    // Remaining items are dependencies of our module
+    for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
+        if ((*arg)[0] != '/') {
+            prefix = base_path + "/";
+        } else {
+            prefix = "";
+        }
+        deps.push_back(prefix + *arg);
+    }
+
+    std::string canonical_name = MakeCanonical(args[0].substr(0, pos));
+    if (canonical_name.empty()) {
+        return false;
+    }
+    this->module_deps_[canonical_name] = deps;
+
+    return true;
+}
+
+bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+
+    if (type != "alias") {
+        LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type;
+        return false;
+    }
+
+    if (args.size() != 3) {
+        LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size();
+        return false;
+    }
+
+    const std::string& alias = *it++;
+    const std::string& module_name = *it++;
+    this->module_aliases_.emplace_back(alias, module_name);
+
+    return true;
+}
+
+bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+    std::string state = "";
+
+    if (type != "softdep") {
+        LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type;
+        return false;
+    }
+
+    if (args.size() < 4) {
+        LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries";
+        return false;
+    }
+
+    const std::string& module = *it++;
+    while (it != args.end()) {
+        const std::string& token = *it++;
+        if (token == "pre:" || token == "post:") {
+            state = token;
+            continue;
+        }
+        if (state == "") {
+            LOG(ERROR) << "malformed modules.softdep at token " << token;
+            return false;
+        }
+        if (state == "pre:") {
+            this->module_pre_softdep_.emplace_back(module, token);
+        } else {
+            this->module_post_softdep_.emplace_back(module, token);
+        }
+    }
+
+    return true;
+}
+
+bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& module = *it++;
+
+    const std::string& canonical_name = MakeCanonical(module);
+    if (canonical_name.empty()) {
+        return false;
+    }
+    this->module_load_.emplace_back(canonical_name);
+
+    return true;
+}
+
+bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) {
+    auto it = args.begin();
+    const std::string& type = *it++;
+
+    if (type != "options") {
+        LOG(ERROR) << "non-options line encountered in modules.options";
+        return false;
+    }
+
+    if (args.size() < 2) {
+        LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size();
+        return false;
+    }
+
+    const std::string& module = *it++;
+    std::string options = "";
+
+    const std::string& canonical_name = MakeCanonical(module);
+    if (canonical_name.empty()) {
+        return false;
+    }
+
+    while (it != args.end()) {
+        options += *it++;
+        if (it != args.end()) {
+            options += " ";
+        }
+    }
+
+    auto [unused, inserted] = this->module_options_.emplace(canonical_name, options);
+    if (!inserted) {
+        LOG(ERROR) << "multiple options lines present for module " << module;
+        return false;
+    }
+    return true;
+}
+
+void Modprobe::ParseCfg(const std::string& cfg,
+                        std::function<bool(const std::vector<std::string>&)> f) {
+    std::string cfg_contents;
+    if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) {
+        return;
+    }
+
+    std::vector<std::string> lines = android::base::Split(cfg_contents, "\n");
+    for (const std::string line : lines) {
+        if (line.empty() || line[0] == '#') {
+            continue;
+        }
+        const std::vector<std::string> args = android::base::Split(line, " ");
+        if (args.empty()) continue;
+        f(args);
+    }
+    return;
+}
+
+Modprobe::Modprobe(const std::vector<std::string>& base_paths) {
+    using namespace std::placeholders;
+
+    for (const auto& base_path : base_paths) {
+        auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1);
+        ParseCfg(base_path + "/modules.alias", alias_callback);
+
+        auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1);
+        ParseCfg(base_path + "/modules.dep", dep_callback);
+
+        auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1);
+        ParseCfg(base_path + "/modules.softdep", softdep_callback);
+
+        auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1);
+        ParseCfg(base_path + "/modules.load", load_callback);
+
+        auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1);
+        ParseCfg(base_path + "/modules.options", options_callback);
+    }
+}
+
+std::vector<std::string> Modprobe::GetDependencies(const std::string& module) {
+    auto it = module_deps_.find(module);
+    if (it == module_deps_.end()) {
+        return {};
+    }
+    return it->second;
+}
+
+bool Modprobe::InsmodWithDeps(const std::string& module_name) {
+    if (module_name.empty()) {
+        LOG(ERROR) << "Need valid module name, given: " << module_name;
+        return false;
+    }
+
+    auto dependencies = GetDependencies(module_name);
+    if (dependencies.empty()) {
+        LOG(ERROR) << "Module " << module_name << " not in dependency file";
+        return false;
+    }
+
+    // load module dependencies in reverse order
+    for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
+        const std::string& canonical_name = MakeCanonical(*dep);
+        if (canonical_name.empty()) {
+            return false;
+        }
+        if (!LoadWithAliases(canonical_name, true)) {
+            return false;
+        }
+    }
+
+    // try to load soft pre-dependencies
+    for (const auto& [module, softdep] : module_pre_softdep_) {
+        if (module_name == module) {
+            LoadWithAliases(softdep, false);
+        }
+    }
+
+    // load target module itself with args
+    if (!Insmod(dependencies[0])) {
+        return false;
+    }
+
+    // try to load soft post-dependencies
+    for (const auto& [module, softdep] : module_post_softdep_) {
+        if (module_name == module) {
+            LoadWithAliases(softdep, false);
+        }
+    }
+
+    return true;
+}
+
+bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict) {
+    std::set<std::string> modules_to_load = {module_name};
+    bool module_loaded = false;
+
+    // use aliases to expand list of modules to load (multiple modules
+    // may alias themselves to the requested name)
+    for (const auto& [alias, aliased_module] : module_aliases_) {
+        if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue;
+        modules_to_load.emplace(aliased_module);
+    }
+
+    // attempt to load all modules aliased to this name
+    for (const auto& module : modules_to_load) {
+        if (!ModuleExists(module)) continue;
+        if (InsmodWithDeps(module)) module_loaded = true;
+    }
+
+    if (strict && !module_loaded) {
+        LOG(ERROR) << "LoadWithAliases did not find a module for " << module_name;
+        return false;
+    }
+    return true;
+}
+
+bool Modprobe::LoadListedModules() {
+    for (const auto& module : module_load_) {
+        if (!LoadWithAliases(module, true)) {
+            return false;
+        }
+    }
+    return true;
+}
diff --git a/libmodprobe/libmodprobe_ext.cpp b/libmodprobe/libmodprobe_ext.cpp
new file mode 100644
index 0000000..5f3a04d
--- /dev/null
+++ b/libmodprobe/libmodprobe_ext.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+#include <modprobe/modprobe.h>
+
+bool Modprobe::Insmod(const std::string& path_name) {
+    android::base::unique_fd fd(
+            TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
+    if (fd == -1) {
+        LOG(ERROR) << "Could not open module '" << path_name << "'";
+        return false;
+    }
+
+    std::string options = "";
+    auto options_iter = module_options_.find(MakeCanonical(path_name));
+    if (options_iter != module_options_.end()) {
+        options = options_iter->second;
+    }
+
+    LOG(INFO) << "Loading module " << path_name << " with args \"" << options << "\"";
+    int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0);
+    if (ret != 0) {
+        if (errno == EEXIST) {
+            // Module already loaded
+            return true;
+        }
+        LOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'";
+        return false;
+    }
+
+    LOG(INFO) << "Loaded kernel module " << path_name;
+    return true;
+}
+
+bool Modprobe::ModuleExists(const std::string& module_name) {
+    struct stat fileStat;
+    auto deps = GetDependencies(module_name);
+    if (deps.empty()) {
+        // missing deps can happen in the case of an alias
+        return false;
+    }
+    if (stat(deps.front().c_str(), &fileStat)) {
+        return false;
+    }
+    if (!S_ISREG(fileStat.st_mode)) {
+        return false;
+    }
+    return true;
+}
diff --git a/libmodprobe/libmodprobe_ext_test.cpp b/libmodprobe/libmodprobe_ext_test.cpp
new file mode 100644
index 0000000..0f073cb
--- /dev/null
+++ b/libmodprobe/libmodprobe_ext_test.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <string>
+#include <vector>
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+
+#include <modprobe/modprobe.h>
+
+#include "libmodprobe_test.h"
+
+bool Modprobe::Insmod(const std::string& path_name) {
+    auto deps = GetDependencies(MakeCanonical(path_name));
+    if (deps.empty()) {
+        return false;
+    }
+    if (std::find(test_modules.begin(), test_modules.end(), deps.front()) == test_modules.end()) {
+        return false;
+    }
+    for (auto it = modules_loaded.begin(); it != modules_loaded.end(); ++it) {
+        if (android::base::StartsWith(*it, path_name)) {
+            return true;
+        }
+    }
+    std::string options;
+    auto options_iter = module_options_.find(MakeCanonical(path_name));
+    if (options_iter != module_options_.end()) {
+        options = " " + options_iter->second;
+    }
+    modules_loaded.emplace_back(path_name + options);
+    return true;
+}
+
+bool Modprobe::ModuleExists(const std::string& module_name) {
+    auto deps = GetDependencies(module_name);
+    if (deps.empty()) {
+        // missing deps can happen in the case of an alias
+        return false;
+    }
+    return std::find(test_modules.begin(), test_modules.end(), deps.front()) != test_modules.end();
+}
diff --git a/libmodprobe/libmodprobe_test.cpp b/libmodprobe/libmodprobe_test.cpp
new file mode 100644
index 0000000..481658d
--- /dev/null
+++ b/libmodprobe/libmodprobe_test.cpp
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <functional>
+
+#include <android-base/file.h>
+#include <android-base/macros.h>
+#include <android-base/unique_fd.h>
+#include <gtest/gtest.h>
+
+#include <modprobe/modprobe.h>
+
+#include "libmodprobe_test.h"
+
+// Used by libmodprobe_ext_test to check if requested modules are present.
+std::vector<std::string> test_modules;
+
+// Used by libmodprobe_ext_test to report which modules would have been loaded.
+std::vector<std::string> modules_loaded;
+
+TEST(libmodprobe, Test) {
+    test_modules = {
+            "/test1.ko",  "/test2.ko",  "/test3.ko",  "/test4.ko",  "/test5.ko",
+            "/test6.ko",  "/test7.ko",  "/test8.ko",  "/test9.ko",  "/test10.ko",
+            "/test11.ko", "/test12.ko", "/test13.ko", "/test14.ko", "/test15.ko",
+    };
+
+    std::vector<std::string> expected_modules_loaded = {
+            "/test14.ko",
+            "/test15.ko",
+            "/test3.ko",
+            "/test4.ko",
+            "/test1.ko",
+            "/test6.ko",
+            "/test2.ko",
+            "/test5.ko",
+            "/test8.ko",
+            "/test7.ko param1=4",
+            "/test9.ko param_x=1 param_y=2 param_z=3",
+            "/test10.ko",
+            "/test12.ko",
+            "/test11.ko",
+            "/test13.ko",
+    };
+
+    const std::string modules_dep =
+            "test1.ko:\n"
+            "test2.ko:\n"
+            "test3.ko:\n"
+            "test4.ko: test3.ko\n"
+            "test5.ko: test2.ko test6.ko\n"
+            "test6.ko:\n"
+            "test7.ko:\n"
+            "test8.ko:\n"
+            "test9.ko:\n"
+            "test10.ko:\n"
+            "test11.ko:\n"
+            "test12.ko:\n"
+            "test13.ko:\n"
+            "test14.ko:\n"
+            "test15.ko:\n";
+
+    const std::string modules_softdep =
+            "softdep test7 pre: test8\n"
+            "softdep test9 post: test10\n"
+            "softdep test11 pre: test12 post: test13\n"
+            "softdep test3 pre: test141516\n";
+
+    const std::string modules_alias =
+            "# Aliases extracted from modules themselves.\n"
+            "\n"
+            "alias test141516 test14\n"
+            "alias test141516 test15\n"
+            "alias test141516 test16\n";
+
+    const std::string modules_options =
+            "options test7.ko param1=4\n"
+            "options test9.ko param_x=1 param_y=2 param_z=3\n"
+            "options test100.ko param_1=1\n";
+
+    const std::string modules_load =
+            "test4.ko\n"
+            "test1.ko\n"
+            "test3.ko\n"
+            "test5.ko\n"
+            "test7.ko\n"
+            "test9.ko\n"
+            "test11.ko\n";
+
+    TemporaryDir dir;
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_alias, std::string(dir.path) + "/modules.alias", 0600, getuid(), getgid()));
+
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_dep, std::string(dir.path) + "/modules.dep", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_softdep, std::string(dir.path) + "/modules.softdep", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_options, std::string(dir.path) + "/modules.options", 0600, getuid(), getgid()));
+    ASSERT_TRUE(android::base::WriteStringToFile(
+            modules_load, std::string(dir.path) + "/modules.load", 0600, getuid(), getgid()));
+
+    for (auto i = test_modules.begin(); i != test_modules.end(); ++i) {
+        *i = dir.path + *i;
+    }
+
+    Modprobe m({dir.path});
+    EXPECT_TRUE(m.LoadListedModules());
+
+    GTEST_LOG_(INFO) << "Expected modules loaded (in order):";
+    for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) {
+        *i = dir.path + *i;
+        GTEST_LOG_(INFO) << "\"" << *i << "\"";
+    }
+    GTEST_LOG_(INFO) << "Actual modules loaded (in order):";
+    for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) {
+        GTEST_LOG_(INFO) << "\"" << *i << "\"";
+    }
+
+    EXPECT_TRUE(modules_loaded == expected_modules_loaded);
+}
diff --git a/mkbootimg/mkbootimg_dummy.cpp b/libmodprobe/libmodprobe_test.h
similarity index 70%
rename from mkbootimg/mkbootimg_dummy.cpp
rename to libmodprobe/libmodprobe_test.h
index 410d379..a001b69 100644
--- a/mkbootimg/mkbootimg_dummy.cpp
+++ b/libmodprobe/libmodprobe_test.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 The Android Open Source Project
+ * Copyright (C) 2019 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,11 +14,10 @@
  * limitations under the License.
  */
 
-#include <abi_check/mkbootimg_abi_check.h>
+#pragma once
 
-void mkbootimg_dummy(boot_img_hdr* hdr) {
-    // TODO: Hack to trigger abi checks, remove this.
-    if (hdr) {
-        hdr--;
-    }
-}
+#include <string>
+#include <vector>
+
+extern std::vector<std::string> test_modules;
+extern std::vector<std::string> modules_loaded;
diff --git a/libprocessgroup/Android.bp b/libprocessgroup/Android.bp
index 52a297c..618a5c5 100644
--- a/libprocessgroup/Android.bp
+++ b/libprocessgroup/Android.bp
@@ -24,9 +24,9 @@
     ],
     name: "libprocessgroup",
     host_supported: true,
+    native_bridge_supported: true,
     recovery_available: true,
     vendor_available: true,
-    native_bridge_supported: true,
     vndk: {
         enabled: true,
         support_system_process: true,
diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp
index 92fcd1e..9797d76 100644
--- a/libprocessgroup/cgroup_map.cpp
+++ b/libprocessgroup/cgroup_map.cpp
@@ -70,8 +70,8 @@
 bool CgroupController::IsUsable() const {
     if (!HasValue()) return false;
 
-    uint32_t flags = ACgroupController_getFlags(controller_);
-    return (flags & CGROUPRC_CONTROLLER_FLAG_MOUNTED) != 0;
+    static bool enabled = (access(GetProcsFilePath("", 0, 0).c_str(), F_OK) == 0);
+    return enabled;
 }
 
 std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
@@ -160,7 +160,6 @@
         const ACgroupController* controller = ACgroupFile_getController(i);
         LOG(INFO) << "\t" << ACgroupController_getName(controller) << " ver "
                   << ACgroupController_getVersion(controller) << " path "
-                  << ACgroupController_getFlags(controller) << " flags "
                   << ACgroupController_getPath(controller);
     }
 }
diff --git a/libprocessgroup/cgrouprc/cgroup_controller.cpp b/libprocessgroup/cgrouprc/cgroup_controller.cpp
index 5a326e5..d064d31 100644
--- a/libprocessgroup/cgrouprc/cgroup_controller.cpp
+++ b/libprocessgroup/cgrouprc/cgroup_controller.cpp
@@ -27,11 +27,6 @@
     return controller->version();
 }
 
-uint32_t ACgroupController_getFlags(const ACgroupController* controller) {
-    CHECK(controller != nullptr);
-    return controller->flags();
-}
-
 const char* ACgroupController_getName(const ACgroupController* controller) {
     CHECK(controller != nullptr);
     return controller->name();
diff --git a/libprocessgroup/cgrouprc/include/android/cgrouprc.h b/libprocessgroup/cgrouprc/include/android/cgrouprc.h
index ffc9f0b..0f6a9cd 100644
--- a/libprocessgroup/cgrouprc/include/android/cgrouprc.h
+++ b/libprocessgroup/cgrouprc/include/android/cgrouprc.h
@@ -66,18 +66,11 @@
         __INTRODUCED_IN(29);
 
 /**
- * Flag bitmask used in ACgroupController_getFlags
+ * Flag bitmask to be used when ACgroupController_getFlags can be exported
  */
 #define CGROUPRC_CONTROLLER_FLAG_MOUNTED 0x1
 
 /**
- * Returns the flags bitmask of the given controller.
- * If the given controller is null, return 0.
- */
-__attribute__((warn_unused_result)) uint32_t ACgroupController_getFlags(const ACgroupController*)
-        __INTRODUCED_IN(29);
-
-/**
  * Returns the name of the given controller.
  * If the given controller is null, return nullptr.
  */
diff --git a/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt b/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
index ea3df33..91df392 100644
--- a/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
+++ b/libprocessgroup/cgrouprc/libcgrouprc.llndk.txt
@@ -4,7 +4,6 @@
     ACgroupFile_getControllerCount;
     ACgroupFile_getController;
     ACgroupController_getVersion;
-    ACgroupController_getFlags;
     ACgroupController_getName;
     ACgroupController_getPath;
   local:
diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h
index 7e6bf45..f73ec2d 100644
--- a/libprocessgroup/include/processgroup/processgroup.h
+++ b/libprocessgroup/include/processgroup/processgroup.h
@@ -39,6 +39,11 @@
 
 bool UsePerAppMemcg();
 
+// Drop the fd cache of cgroup path. It is used for when resource caching is enabled and a process
+// loses the access to the path, the access checking (See SetCgroupAction::EnableResourceCaching)
+// should be active again. E.g. Zygote specialization for child process.
+void DropTaskProfilesResourceCaching();
+
 // Return 0 and removes the cgroup if there are no longer any processes in it.
 // Returns -1 in the case of an error occurring or if there are processes still running
 // even after retrying for up to 200ms.
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index d3ac26b..7c191be 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -111,6 +111,10 @@
     return memcg_supported;
 }
 
+void DropTaskProfilesResourceCaching() {
+    TaskProfiles::GetInstance().DropResourceCaching();
+}
+
 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
                         bool use_fd_cache) {
     const TaskProfiles& tp = TaskProfiles::GetInstance();
diff --git a/libprocessgroup/task_profiles.cpp b/libprocessgroup/task_profiles.cpp
index 40d8d90..aee5f0c 100644
--- a/libprocessgroup/task_profiles.cpp
+++ b/libprocessgroup/task_profiles.cpp
@@ -150,6 +150,7 @@
 }
 
 void SetCgroupAction::EnableResourceCaching() {
+    std::lock_guard<std::mutex> lock(fd_mutex_);
     if (fd_ != FDS_NOT_CACHED) {
         return;
     }
@@ -172,6 +173,15 @@
     fd_ = std::move(fd);
 }
 
+void SetCgroupAction::DropResourceCaching() {
+    std::lock_guard<std::mutex> lock(fd_mutex_);
+    if (fd_ == FDS_NOT_CACHED) {
+        return;
+    }
+
+    fd_.reset(FDS_NOT_CACHED);
+}
+
 bool SetCgroupAction::AddTidToCgroup(int tid, int fd) {
     if (tid <= 0) {
         return true;
@@ -191,6 +201,7 @@
 }
 
 bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
+    std::lock_guard<std::mutex> lock(fd_mutex_);
     if (IsFdValid()) {
         // fd is cached, reuse it
         if (!AddTidToCgroup(pid, fd_)) {
@@ -221,6 +232,7 @@
 }
 
 bool SetCgroupAction::ExecuteForTask(int tid) const {
+    std::lock_guard<std::mutex> lock(fd_mutex_);
     if (IsFdValid()) {
         // fd is cached, reuse it
         if (!AddTidToCgroup(tid, fd_)) {
@@ -289,6 +301,24 @@
     res_cached_ = true;
 }
 
+void TaskProfile::DropResourceCaching() {
+    if (!res_cached_) {
+        return;
+    }
+
+    for (auto& element : elements_) {
+        element->DropResourceCaching();
+    }
+
+    res_cached_ = false;
+}
+
+void TaskProfiles::DropResourceCaching() const {
+    for (auto& iter : profiles_) {
+        iter.second->DropResourceCaching();
+    }
+}
+
 TaskProfiles& TaskProfiles::GetInstance() {
     // Deliberately leak this object to avoid a race between destruction on
     // process exit and concurrent access from another thread.
diff --git a/libprocessgroup/task_profiles.h b/libprocessgroup/task_profiles.h
index 445647d..891d5b5 100644
--- a/libprocessgroup/task_profiles.h
+++ b/libprocessgroup/task_profiles.h
@@ -19,6 +19,7 @@
 #include <sys/cdefs.h>
 #include <sys/types.h>
 #include <map>
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -50,6 +51,7 @@
     virtual bool ExecuteForTask(int) const { return false; };
 
     virtual void EnableResourceCaching() {}
+    virtual void DropResourceCaching() {}
 };
 
 // Profile actions
@@ -113,6 +115,7 @@
     virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
     virtual bool ExecuteForTask(int tid) const;
     virtual void EnableResourceCaching();
+    virtual void DropResourceCaching();
 
     const CgroupController* controller() const { return &controller_; }
     std::string path() const { return path_; }
@@ -127,6 +130,7 @@
     CgroupController controller_;
     std::string path_;
     android::base::unique_fd fd_;
+    mutable std::mutex fd_mutex_;
 
     static bool IsAppDependentPath(const std::string& path);
     static bool AddTidToCgroup(int tid, int fd);
@@ -143,6 +147,7 @@
     bool ExecuteForProcess(uid_t uid, pid_t pid) const;
     bool ExecuteForTask(int tid) const;
     void EnableResourceCaching();
+    void DropResourceCaching();
 
   private:
     bool res_cached_;
@@ -156,6 +161,7 @@
 
     TaskProfile* GetProfile(const std::string& name) const;
     const ProfileAttribute* GetAttribute(const std::string& name) const;
+    void DropResourceCaching() const;
 
   private:
     std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
diff --git a/libunwindstack/Android.bp b/libunwindstack/Android.bp
index b7650a1..5423de5 100644
--- a/libunwindstack/Android.bp
+++ b/libunwindstack/Android.bp
@@ -241,6 +241,7 @@
         "tests/files/offline/debug_frame_first_x86/*",
         "tests/files/offline/debug_frame_load_bias_arm/*",
         "tests/files/offline/eh_frame_hdr_begin_x86_64/*",
+        "tests/files/offline/invalid_elf_offset_arm/*",
         "tests/files/offline/jit_debug_arm/*",
         "tests/files/offline/jit_debug_x86/*",
         "tests/files/offline/jit_map_arm/*",
diff --git a/libunwindstack/ElfInterface.cpp b/libunwindstack/ElfInterface.cpp
index dee8eb3..f0e4138 100644
--- a/libunwindstack/ElfInterface.cpp
+++ b/libunwindstack/ElfInterface.cpp
@@ -29,12 +29,12 @@
 #include <unwindstack/DwarfSection.h>
 #include <unwindstack/ElfInterface.h>
 #include <unwindstack/Log.h>
-#include <unwindstack/Memory.h>
 #include <unwindstack/Regs.h>
 
 #include "DwarfDebugFrame.h"
 #include "DwarfEhFrame.h"
 #include "DwarfEhFrameWithHdr.h"
+#include "MemoryBuffer.h"
 #include "Symbols.h"
 
 namespace unwindstack {
diff --git a/libunwindstack/JitDebug.cpp b/libunwindstack/JitDebug.cpp
index 20bc4b9..8a85607 100644
--- a/libunwindstack/JitDebug.cpp
+++ b/libunwindstack/JitDebug.cpp
@@ -23,7 +23,8 @@
 #include <unwindstack/Elf.h>
 #include <unwindstack/JitDebug.h>
 #include <unwindstack/Maps.h>
-#include <unwindstack/Memory.h>
+
+#include "MemoryRange.h"
 
 // This implements the JIT Compilation Interface.
 // See https://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html
diff --git a/libunwindstack/MapInfo.cpp b/libunwindstack/MapInfo.cpp
index 03658b4..5b30a4d 100644
--- a/libunwindstack/MapInfo.cpp
+++ b/libunwindstack/MapInfo.cpp
@@ -27,7 +27,9 @@
 #include <unwindstack/Elf.h>
 #include <unwindstack/MapInfo.h>
 #include <unwindstack/Maps.h>
-#include <unwindstack/Memory.h>
+
+#include "MemoryFileAtOffset.h"
+#include "MemoryRange.h"
 
 namespace unwindstack {
 
@@ -231,11 +233,13 @@
     }
   }
 
-  // If there is a read-only map then a read-execute map that represents the
-  // same elf object, make sure the previous map is using the same elf
-  // object if it hasn't already been set.
-  if (prev_map != nullptr && elf_start_offset != offset && prev_map->offset == elf_start_offset &&
-      prev_map->name == name) {
+  if (!elf->valid()) {
+    elf_start_offset = offset;
+  } else if (prev_map != nullptr && elf_start_offset != offset &&
+             prev_map->offset == elf_start_offset && prev_map->name == name) {
+    // If there is a read-only map then a read-execute map that represents the
+    // same elf object, make sure the previous map is using the same elf
+    // object if it hasn't already been set.
     std::lock_guard<std::mutex> guard(prev_map->mutex_);
     if (prev_map->elf.get() == nullptr) {
       prev_map->elf = elf;
@@ -296,7 +300,7 @@
 
 std::string MapInfo::GetBuildID() {
   uintptr_t id = build_id.load();
-  if (build_id != 0) {
+  if (id != 0) {
     return *reinterpret_cast<std::string*>(id);
   }
 
diff --git a/libunwindstack/Memory.cpp b/libunwindstack/Memory.cpp
index 9904fef..a66cd5b 100644
--- a/libunwindstack/Memory.cpp
+++ b/libunwindstack/Memory.cpp
@@ -32,6 +32,14 @@
 #include <unwindstack/Memory.h>
 
 #include "Check.h"
+#include "MemoryBuffer.h"
+#include "MemoryCache.h"
+#include "MemoryFileAtOffset.h"
+#include "MemoryLocal.h"
+#include "MemoryOffline.h"
+#include "MemoryOfflineBuffer.h"
+#include "MemoryRange.h"
+#include "MemoryRemote.h"
 
 namespace unwindstack {
 
@@ -168,6 +176,16 @@
   return false;
 }
 
+std::unique_ptr<Memory> Memory::CreateFileMemory(const std::string& path, uint64_t offset) {
+  auto memory = std::make_unique<MemoryFileAtOffset>();
+
+  if (memory->Init(path, offset)) {
+    return memory;
+  }
+
+  return nullptr;
+}
+
 std::shared_ptr<Memory> Memory::CreateProcessMemory(pid_t pid) {
   if (pid == getpid()) {
     return std::shared_ptr<Memory>(new MemoryLocal());
@@ -182,6 +200,11 @@
   return std::shared_ptr<Memory>(new MemoryCache(new MemoryRemote(pid)));
 }
 
+std::shared_ptr<Memory> Memory::CreateOfflineMemory(const uint8_t* data, uint64_t start,
+                                                    uint64_t end) {
+  return std::shared_ptr<Memory>(new MemoryOfflineBuffer(data, start, end));
+}
+
 size_t MemoryBuffer::Read(uint64_t addr, void* dst, size_t size) {
   if (addr >= raw_.size()) {
     return 0;
diff --git a/libunwindstack/MemoryBuffer.h b/libunwindstack/MemoryBuffer.h
new file mode 100644
index 0000000..3fe4bbb
--- /dev/null
+++ b/libunwindstack/MemoryBuffer.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_BUFFER_H
+#define _LIBUNWINDSTACK_MEMORY_BUFFER_H
+
+#include <stdint.h>
+
+#include <string>
+#include <vector>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryBuffer : public Memory {
+ public:
+  MemoryBuffer() = default;
+  virtual ~MemoryBuffer() = default;
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+  uint8_t* GetPtr(size_t offset);
+
+  void Resize(size_t size) { raw_.resize(size); }
+
+  uint64_t Size() { return raw_.size(); }
+
+ private:
+  std::vector<uint8_t> raw_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_BUFFER_H
diff --git a/libunwindstack/MemoryCache.h b/libunwindstack/MemoryCache.h
new file mode 100644
index 0000000..769d907
--- /dev/null
+++ b/libunwindstack/MemoryCache.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_CACHE_H
+#define _LIBUNWINDSTACK_MEMORY_CACHE_H
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryCache : public Memory {
+ public:
+  MemoryCache(Memory* memory) : impl_(memory) {}
+  virtual ~MemoryCache() = default;
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+  void Clear() override { cache_.clear(); }
+
+ private:
+  constexpr static size_t kCacheBits = 12;
+  constexpr static size_t kCacheMask = (1 << kCacheBits) - 1;
+  constexpr static size_t kCacheSize = 1 << kCacheBits;
+  std::unordered_map<uint64_t, uint8_t[kCacheSize]> cache_;
+
+  std::unique_ptr<Memory> impl_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_CACHE_H
diff --git a/libunwindstack/MemoryFileAtOffset.h b/libunwindstack/MemoryFileAtOffset.h
new file mode 100644
index 0000000..d136eb4
--- /dev/null
+++ b/libunwindstack/MemoryFileAtOffset.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_FILE_AT_OFFSET_H
+#define _LIBUNWINDSTACK_MEMORY_FILE_AT_OFFSET_H
+
+#include <stdint.h>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryFileAtOffset : public Memory {
+ public:
+  MemoryFileAtOffset() = default;
+  virtual ~MemoryFileAtOffset();
+
+  bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX);
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+  size_t Size() { return size_; }
+
+  void Clear() override;
+
+ protected:
+  size_t size_ = 0;
+  size_t offset_ = 0;
+  uint8_t* data_ = nullptr;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_FILE_AT_OFFSET_H
diff --git a/libunwindstack/MemoryLocal.h b/libunwindstack/MemoryLocal.h
new file mode 100644
index 0000000..29aaf12
--- /dev/null
+++ b/libunwindstack/MemoryLocal.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_LOCAL_H
+#define _LIBUNWINDSTACK_MEMORY_LOCAL_H
+
+#include <stdint.h>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryLocal : public Memory {
+ public:
+  MemoryLocal() = default;
+  virtual ~MemoryLocal() = default;
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_LOCAL_H
diff --git a/libunwindstack/MemoryOffline.h b/libunwindstack/MemoryOffline.h
new file mode 100644
index 0000000..789f1a2
--- /dev/null
+++ b/libunwindstack/MemoryOffline.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_OFFLINE_H
+#define _LIBUNWINDSTACK_MEMORY_OFFLINE_H
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <unwindstack/Memory.h>
+
+#include "MemoryRange.h"
+
+namespace unwindstack {
+
+class MemoryOffline : public Memory {
+ public:
+  MemoryOffline() = default;
+  virtual ~MemoryOffline() = default;
+
+  bool Init(const std::string& file, uint64_t offset);
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+ private:
+  std::unique_ptr<MemoryRange> memory_;
+};
+
+class MemoryOfflineParts : public Memory {
+ public:
+  MemoryOfflineParts() = default;
+  virtual ~MemoryOfflineParts();
+
+  void Add(MemoryOffline* memory) { memories_.push_back(memory); }
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+ private:
+  std::vector<MemoryOffline*> memories_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_OFFLINE_H
diff --git a/libunwindstack/MemoryOfflineBuffer.h b/libunwindstack/MemoryOfflineBuffer.h
new file mode 100644
index 0000000..64c49a1
--- /dev/null
+++ b/libunwindstack/MemoryOfflineBuffer.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_OFFLINE_BUFFER_H
+#define _LIBUNWINDSTACK_MEMORY_OFFLINE_BUFFER_H
+
+#include <stdint.h>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryOfflineBuffer : public Memory {
+ public:
+  MemoryOfflineBuffer(const uint8_t* data, uint64_t start, uint64_t end);
+  virtual ~MemoryOfflineBuffer() = default;
+
+  void Reset(const uint8_t* data, uint64_t start, uint64_t end);
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+ private:
+  const uint8_t* data_;
+  uint64_t start_;
+  uint64_t end_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_OFFLINE_BUFFER_H
diff --git a/libunwindstack/MemoryRange.h b/libunwindstack/MemoryRange.h
new file mode 100644
index 0000000..3b4ab5c
--- /dev/null
+++ b/libunwindstack/MemoryRange.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_RANGE_H
+#define _LIBUNWINDSTACK_MEMORY_RANGE_H
+
+#include <stdint.h>
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+// MemoryRange maps one address range onto another.
+// The range [src_begin, src_begin + length) in the underlying Memory is mapped onto offset,
+// such that range.read(offset) is equivalent to underlying.read(src_begin).
+class MemoryRange : public Memory {
+ public:
+  MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t length,
+              uint64_t offset);
+  virtual ~MemoryRange() = default;
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+  uint64_t offset() { return offset_; }
+  uint64_t length() { return length_; }
+
+ private:
+  std::shared_ptr<Memory> memory_;
+  uint64_t begin_;
+  uint64_t length_;
+  uint64_t offset_;
+};
+
+class MemoryRanges : public Memory {
+ public:
+  MemoryRanges() = default;
+  virtual ~MemoryRanges() = default;
+
+  void Insert(MemoryRange* memory);
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+ private:
+  std::map<uint64_t, std::unique_ptr<MemoryRange>> maps_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_RANGE_H
diff --git a/libunwindstack/MemoryRemote.h b/libunwindstack/MemoryRemote.h
new file mode 100644
index 0000000..db367d6
--- /dev/null
+++ b/libunwindstack/MemoryRemote.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBUNWINDSTACK_MEMORY_REMOTE_H
+#define _LIBUNWINDSTACK_MEMORY_REMOTE_H
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include <atomic>
+
+#include <unwindstack/Memory.h>
+
+namespace unwindstack {
+
+class MemoryRemote : public Memory {
+ public:
+  MemoryRemote(pid_t pid) : pid_(pid), read_redirect_func_(0) {}
+  virtual ~MemoryRemote() = default;
+
+  size_t Read(uint64_t addr, void* dst, size_t size) override;
+
+  pid_t pid() { return pid_; }
+
+ private:
+  pid_t pid_;
+  std::atomic_uintptr_t read_redirect_func_;
+};
+
+}  // namespace unwindstack
+
+#endif  // _LIBUNWINDSTACK_MEMORY_REMOTE_H
diff --git a/libunwindstack/include/unwindstack/MapInfo.h b/libunwindstack/include/unwindstack/MapInfo.h
index 025fd98..13ce10f 100644
--- a/libunwindstack/include/unwindstack/MapInfo.h
+++ b/libunwindstack/include/unwindstack/MapInfo.h
@@ -25,10 +25,11 @@
 #include <string>
 
 #include <unwindstack/Elf.h>
-#include <unwindstack/Memory.h>
 
 namespace unwindstack {
 
+class MemoryFileAtOffset;
+
 struct MapInfo {
   MapInfo(MapInfo* map_info, uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
           const char* name)
diff --git a/libunwindstack/include/unwindstack/Memory.h b/libunwindstack/include/unwindstack/Memory.h
index b3beb6e..3106564 100644
--- a/libunwindstack/include/unwindstack/Memory.h
+++ b/libunwindstack/include/unwindstack/Memory.h
@@ -21,12 +21,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <atomic>
-#include <map>
 #include <memory>
 #include <string>
-#include <unordered_map>
-#include <vector>
 
 namespace unwindstack {
 
@@ -37,6 +33,9 @@
 
   static std::shared_ptr<Memory> CreateProcessMemory(pid_t pid);
   static std::shared_ptr<Memory> CreateProcessMemoryCached(pid_t pid);
+  static std::shared_ptr<Memory> CreateOfflineMemory(const uint8_t* data, uint64_t start,
+                                                     uint64_t end);
+  static std::unique_ptr<Memory> CreateFileMemory(const std::string& path, uint64_t offset);
 
   virtual bool ReadString(uint64_t addr, std::string* string, uint64_t max_read = UINT64_MAX);
 
@@ -55,157 +54,6 @@
   }
 };
 
-class MemoryCache : public Memory {
- public:
-  MemoryCache(Memory* memory) : impl_(memory) {}
-  virtual ~MemoryCache() = default;
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
-  void Clear() override { cache_.clear(); }
-
- private:
-  constexpr static size_t kCacheBits = 12;
-  constexpr static size_t kCacheMask = (1 << kCacheBits) - 1;
-  constexpr static size_t kCacheSize = 1 << kCacheBits;
-  std::unordered_map<uint64_t, uint8_t[kCacheSize]> cache_;
-
-  std::unique_ptr<Memory> impl_;
-};
-
-class MemoryBuffer : public Memory {
- public:
-  MemoryBuffer() = default;
-  virtual ~MemoryBuffer() = default;
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
-  uint8_t* GetPtr(size_t offset);
-
-  void Resize(size_t size) { raw_.resize(size); }
-
-  uint64_t Size() { return raw_.size(); }
-
- private:
-  std::vector<uint8_t> raw_;
-};
-
-class MemoryFileAtOffset : public Memory {
- public:
-  MemoryFileAtOffset() = default;
-  virtual ~MemoryFileAtOffset();
-
-  bool Init(const std::string& file, uint64_t offset, uint64_t size = UINT64_MAX);
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
-  size_t Size() { return size_; }
-
-  void Clear() override;
-
- protected:
-  size_t size_ = 0;
-  size_t offset_ = 0;
-  uint8_t* data_ = nullptr;
-};
-
-class MemoryRemote : public Memory {
- public:
-  MemoryRemote(pid_t pid) : pid_(pid), read_redirect_func_(0) {}
-  virtual ~MemoryRemote() = default;
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
-  pid_t pid() { return pid_; }
-
- private:
-  pid_t pid_;
-  std::atomic_uintptr_t read_redirect_func_;
-};
-
-class MemoryLocal : public Memory {
- public:
-  MemoryLocal() = default;
-  virtual ~MemoryLocal() = default;
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-};
-
-// MemoryRange maps one address range onto another.
-// The range [src_begin, src_begin + length) in the underlying Memory is mapped onto offset,
-// such that range.read(offset) is equivalent to underlying.read(src_begin).
-class MemoryRange : public Memory {
- public:
-  MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t length,
-              uint64_t offset);
-  virtual ~MemoryRange() = default;
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
-  uint64_t offset() { return offset_; }
-  uint64_t length() { return length_; }
-
- private:
-  std::shared_ptr<Memory> memory_;
-  uint64_t begin_;
-  uint64_t length_;
-  uint64_t offset_;
-};
-
-class MemoryRanges : public Memory {
- public:
-  MemoryRanges() = default;
-  virtual ~MemoryRanges() = default;
-
-  void Insert(MemoryRange* memory);
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
- private:
-  std::map<uint64_t, std::unique_ptr<MemoryRange>> maps_;
-};
-
-class MemoryOffline : public Memory {
- public:
-  MemoryOffline() = default;
-  virtual ~MemoryOffline() = default;
-
-  bool Init(const std::string& file, uint64_t offset);
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
- private:
-  std::unique_ptr<MemoryRange> memory_;
-};
-
-class MemoryOfflineBuffer : public Memory {
- public:
-  MemoryOfflineBuffer(const uint8_t* data, uint64_t start, uint64_t end);
-  virtual ~MemoryOfflineBuffer() = default;
-
-  void Reset(const uint8_t* data, uint64_t start, uint64_t end);
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
- private:
-  const uint8_t* data_;
-  uint64_t start_;
-  uint64_t end_;
-};
-
-class MemoryOfflineParts : public Memory {
- public:
-  MemoryOfflineParts() = default;
-  virtual ~MemoryOfflineParts();
-
-  void Add(MemoryOffline* memory) { memories_.push_back(memory); }
-
-  size_t Read(uint64_t addr, void* dst, size_t size) override;
-
- private:
-  std::vector<MemoryOffline*> memories_;
-};
-
 }  // namespace unwindstack
 
 #endif  // _LIBUNWINDSTACK_MEMORY_H
diff --git a/libunwindstack/tests/ArmExidxDecodeTest.cpp b/libunwindstack/tests/ArmExidxDecodeTest.cpp
index 5f3d1ea..7b1dd92 100644
--- a/libunwindstack/tests/ArmExidxDecodeTest.cpp
+++ b/libunwindstack/tests/ArmExidxDecodeTest.cpp
@@ -1662,7 +1662,7 @@
   ASSERT_EQ(0x10U, (*exidx_->regs())[15]);
 }
 
-INSTANTIATE_TEST_CASE_P(, ArmExidxDecodeTest,
-                        ::testing::Values("logging", "register_logging", "no_logging"));
+INSTANTIATE_TEST_SUITE_P(, ArmExidxDecodeTest,
+                         ::testing::Values("logging", "register_logging", "no_logging"));
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfCfaLogTest.cpp b/libunwindstack/tests/DwarfCfaLogTest.cpp
index bb2e8f0..9dd0cdd 100644
--- a/libunwindstack/tests/DwarfCfaLogTest.cpp
+++ b/libunwindstack/tests/DwarfCfaLogTest.cpp
@@ -66,7 +66,7 @@
   DwarfCie cie_;
   DwarfFde fde_;
 };
-TYPED_TEST_CASE_P(DwarfCfaLogTest);
+TYPED_TEST_SUITE_P(DwarfCfaLogTest);
 
 // NOTE: All class variable references have to be prefaced with this->.
 
@@ -763,17 +763,17 @@
   ASSERT_EQ("", GetFakeLogBuf());
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
-                           cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
-                           cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
-                           cfa_undefined, cfa_same, cfa_register, cfa_state,
-                           cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
-                           cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
-                           cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
-                           cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
-                           cfa_gnu_negative_offset_extended, cfa_register_override);
+REGISTER_TYPED_TEST_SUITE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
+                            cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
+                            cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
+                            cfa_undefined, cfa_same, cfa_register, cfa_state,
+                            cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
+                            cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
+                            cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
+                            cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
+                            cfa_gnu_negative_offset_extended, cfa_register_override);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaLogTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfCfaLogTest, DwarfCfaLogTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfCfaLogTest, DwarfCfaLogTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfCfaTest.cpp b/libunwindstack/tests/DwarfCfaTest.cpp
index 7395b04..dd71490 100644
--- a/libunwindstack/tests/DwarfCfaTest.cpp
+++ b/libunwindstack/tests/DwarfCfaTest.cpp
@@ -64,7 +64,7 @@
   DwarfCie cie_;
   DwarfFde fde_;
 };
-TYPED_TEST_CASE_P(DwarfCfaTest);
+TYPED_TEST_SUITE_P(DwarfCfaTest);
 
 // NOTE: All test class variables need to be referenced as this->.
 
@@ -952,16 +952,17 @@
   ASSERT_EQ("", GetFakeLogBuf());
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfCfaTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
-                           cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
-                           cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4, cfa_undefined,
-                           cfa_same, cfa_register, cfa_state, cfa_state_cfa_offset_restore,
-                           cfa_def_cfa, cfa_def_cfa_sf, cfa_def_cfa_register, cfa_def_cfa_offset,
-                           cfa_def_cfa_offset_sf, cfa_def_cfa_expression, cfa_expression,
-                           cfa_val_offset, cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
-                           cfa_gnu_negative_offset_extended, cfa_register_override);
+REGISTER_TYPED_TEST_SUITE_P(DwarfCfaTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
+                            cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
+                            cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4, cfa_undefined,
+                            cfa_same, cfa_register, cfa_state, cfa_state_cfa_offset_restore,
+                            cfa_def_cfa, cfa_def_cfa_sf, cfa_def_cfa_register, cfa_def_cfa_offset,
+                            cfa_def_cfa_offset_sf, cfa_def_cfa_expression, cfa_expression,
+                            cfa_val_offset, cfa_val_offset_sf, cfa_val_expression,
+                            cfa_gnu_args_size, cfa_gnu_negative_offset_extended,
+                            cfa_register_override);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfCfaTest, DwarfCfaTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfCfaTest, DwarfCfaTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfDebugFrameTest.cpp b/libunwindstack/tests/DwarfDebugFrameTest.cpp
index 120bd73..2b36f17 100644
--- a/libunwindstack/tests/DwarfDebugFrameTest.cpp
+++ b/libunwindstack/tests/DwarfDebugFrameTest.cpp
@@ -44,7 +44,7 @@
   MemoryFake memory_;
   DwarfDebugFrame<TypeParam>* debug_frame_ = nullptr;
 };
-TYPED_TEST_CASE_P(DwarfDebugFrameTest);
+TYPED_TEST_SUITE_P(DwarfDebugFrameTest);
 
 // NOTE: All test class variables need to be referenced as this->.
 
@@ -812,7 +812,7 @@
   EXPECT_EQ(0xb50U, fde->pc_end);
 }
 
-REGISTER_TYPED_TEST_CASE_P(
+REGISTER_TYPED_TEST_SUITE_P(
     DwarfDebugFrameTest, GetFdes32, GetFdes32_after_GetFdeFromPc, GetFdes32_not_in_section,
     GetFdeFromPc32, GetFdeFromPc32_reverse, GetFdeFromPc32_not_in_section, GetFdes64,
     GetFdes64_after_GetFdeFromPc, GetFdes64_not_in_section, GetFdeFromPc64, GetFdeFromPc64_reverse,
@@ -825,6 +825,6 @@
     GetFdeFromOffset64_lsda_address, GetFdeFromPc_interleaved);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfDebugFrameTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfDebugFrameTest, DwarfDebugFrameTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfEhFrameTest.cpp b/libunwindstack/tests/DwarfEhFrameTest.cpp
index 9cac6e8..4792fb5 100644
--- a/libunwindstack/tests/DwarfEhFrameTest.cpp
+++ b/libunwindstack/tests/DwarfEhFrameTest.cpp
@@ -42,7 +42,7 @@
   MemoryFake memory_;
   DwarfEhFrame<TypeParam>* eh_frame_ = nullptr;
 };
-TYPED_TEST_CASE_P(DwarfEhFrameTest);
+TYPED_TEST_SUITE_P(DwarfEhFrameTest);
 
 // NOTE: All test class variables need to be referenced as this->.
 
@@ -125,9 +125,9 @@
   EXPECT_EQ(1U, cie->return_address_register);
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64);
+REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameTest, GetFdeCieFromOffset32, GetFdeCieFromOffset64);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfEhFrameTest, DwarfEhFrameTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp b/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
index be9e721..78608e3 100644
--- a/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
+++ b/libunwindstack/tests/DwarfEhFrameWithHdrTest.cpp
@@ -73,7 +73,7 @@
   MemoryFake memory_;
   TestDwarfEhFrameWithHdr<TypeParam>* eh_frame_ = nullptr;
 };
-TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest);
+TYPED_TEST_SUITE_P(DwarfEhFrameWithHdrTest);
 
 // NOTE: All test class variables need to be referenced as this->.
 
@@ -446,14 +446,14 @@
   ASSERT_EQ(nullptr, this->eh_frame_->GetFdeFromPc(0x800));
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias, GetFdes,
-                           GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
-                           GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
-                           GetFdeOffsetFromPc_verify, GetFdeOffsetFromPc_index_fail,
-                           GetFdeOffsetFromPc_fail_fde_count, GetFdeOffsetFromPc_search,
-                           GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
+REGISTER_TYPED_TEST_SUITE_P(DwarfEhFrameWithHdrTest, Init, Init_non_zero_load_bias, GetFdes,
+                            GetFdeInfoFromIndex_expect_cache_fail, GetFdeInfoFromIndex_read_pcrel,
+                            GetFdeInfoFromIndex_read_datarel, GetFdeInfoFromIndex_cached,
+                            GetFdeOffsetFromPc_verify, GetFdeOffsetFromPc_index_fail,
+                            GetFdeOffsetFromPc_fail_fde_count, GetFdeOffsetFromPc_search,
+                            GetCieFde32, GetCieFde64, GetFdeFromPc_fde_not_found);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfEhFrameWithHdrTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfEhFrameWithHdrTest, DwarfEhFrameWithHdrTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfOpLogTest.cpp b/libunwindstack/tests/DwarfOpLogTest.cpp
index 3f09dd8..f4ade5d 100644
--- a/libunwindstack/tests/DwarfOpLogTest.cpp
+++ b/libunwindstack/tests/DwarfOpLogTest.cpp
@@ -48,7 +48,7 @@
   std::unique_ptr<DwarfMemory> mem_;
   std::unique_ptr<DwarfOp<TypeParam>> op_;
 };
-TYPED_TEST_CASE_P(DwarfOpLogTest);
+TYPED_TEST_SUITE_P(DwarfOpLogTest);
 
 TYPED_TEST_P(DwarfOpLogTest, multiple_ops) {
   // Multi operation opcodes.
@@ -65,9 +65,9 @@
   ASSERT_EQ(expected, lines);
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfOpLogTest, multiple_ops);
+REGISTER_TYPED_TEST_SUITE_P(DwarfOpLogTest, multiple_ops);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfOpLogTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfOpLogTest, DwarfOpLogTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfOpLogTest, DwarfOpLogTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfOpTest.cpp b/libunwindstack/tests/DwarfOpTest.cpp
index d424d5f..0898ec0 100644
--- a/libunwindstack/tests/DwarfOpTest.cpp
+++ b/libunwindstack/tests/DwarfOpTest.cpp
@@ -48,7 +48,7 @@
   std::unique_ptr<DwarfMemory> mem_;
   std::unique_ptr<DwarfOp<TypeParam>> op_;
 };
-TYPED_TEST_CASE_P(DwarfOpTest);
+TYPED_TEST_SUITE_P(DwarfOpTest);
 
 TYPED_TEST_P(DwarfOpTest, decode) {
   // Memory error.
@@ -1571,15 +1571,16 @@
   EXPECT_FALSE(this->op_->dex_pc_set());
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfOpTest, decode, eval, illegal_opcode, not_implemented, op_addr,
-                           op_deref, op_deref_size, const_unsigned, const_signed, const_uleb,
-                           const_sleb, op_dup, op_drop, op_over, op_pick, op_swap, op_rot, op_abs,
-                           op_and, op_div, op_minus, op_mod, op_mul, op_neg, op_not, op_or, op_plus,
-                           op_plus_uconst, op_shl, op_shr, op_shra, op_xor, op_bra,
-                           compare_opcode_stack_error, compare_opcodes, op_skip, op_lit, op_reg,
-                           op_regx, op_breg, op_breg_invalid_register, op_bregx, op_nop, is_dex_pc);
+REGISTER_TYPED_TEST_SUITE_P(DwarfOpTest, decode, eval, illegal_opcode, not_implemented, op_addr,
+                            op_deref, op_deref_size, const_unsigned, const_signed, const_uleb,
+                            const_sleb, op_dup, op_drop, op_over, op_pick, op_swap, op_rot, op_abs,
+                            op_and, op_div, op_minus, op_mod, op_mul, op_neg, op_not, op_or,
+                            op_plus, op_plus_uconst, op_shl, op_shr, op_shra, op_xor, op_bra,
+                            compare_opcode_stack_error, compare_opcodes, op_skip, op_lit, op_reg,
+                            op_regx, op_breg, op_breg_invalid_register, op_bregx, op_nop,
+                            is_dex_pc);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfOpTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfOpTest, DwarfOpTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfOpTest, DwarfOpTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/DwarfSectionImplTest.cpp b/libunwindstack/tests/DwarfSectionImplTest.cpp
index 46f555a..b386ef4 100644
--- a/libunwindstack/tests/DwarfSectionImplTest.cpp
+++ b/libunwindstack/tests/DwarfSectionImplTest.cpp
@@ -68,7 +68,7 @@
   MemoryFake memory_;
   TestDwarfSectionImpl<TypeParam>* section_ = nullptr;
 };
-TYPED_TEST_CASE_P(DwarfSectionImplTest);
+TYPED_TEST_SUITE_P(DwarfSectionImplTest);
 
 // NOTE: All test class variables need to be referenced as this->.
 
@@ -571,18 +571,18 @@
   ASSERT_EQ("", GetFakeLogBuf());
 }
 
-REGISTER_TYPED_TEST_CASE_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache,
-                           GetFdeFromOffset_fail_should_not_cache, Eval_cfa_expr_eval_fail,
-                           Eval_cfa_expr_no_stack, Eval_cfa_expr_is_register, Eval_cfa_expr,
-                           Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa, Eval_cfa_bad,
-                           Eval_cfa_register_prev, Eval_cfa_register_from_value,
-                           Eval_double_indirection, Eval_register_reference_chain, Eval_dex_pc,
-                           Eval_invalid_register, Eval_different_reg_locations,
-                           Eval_return_address_undefined, Eval_pc_zero, Eval_return_address,
-                           Eval_ignore_large_reg_loc, Eval_reg_expr, Eval_reg_val_expr,
-                           GetCfaLocationInfo_cie_not_cached, GetCfaLocationInfo_cie_cached, Log);
+REGISTER_TYPED_TEST_SUITE_P(DwarfSectionImplTest, GetCieFromOffset_fail_should_not_cache,
+                            GetFdeFromOffset_fail_should_not_cache, Eval_cfa_expr_eval_fail,
+                            Eval_cfa_expr_no_stack, Eval_cfa_expr_is_register, Eval_cfa_expr,
+                            Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa, Eval_cfa_bad,
+                            Eval_cfa_register_prev, Eval_cfa_register_from_value,
+                            Eval_double_indirection, Eval_register_reference_chain, Eval_dex_pc,
+                            Eval_invalid_register, Eval_different_reg_locations,
+                            Eval_return_address_undefined, Eval_pc_zero, Eval_return_address,
+                            Eval_ignore_large_reg_loc, Eval_reg_expr, Eval_reg_val_expr,
+                            GetCfaLocationInfo_cie_not_cached, GetCfaLocationInfo_cie_cached, Log);
 
 typedef ::testing::Types<uint32_t, uint64_t> DwarfSectionImplTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, DwarfSectionImplTest, DwarfSectionImplTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, DwarfSectionImplTest, DwarfSectionImplTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/ElfCacheTest.cpp b/libunwindstack/tests/ElfCacheTest.cpp
index 07fd6f6..5735858 100644
--- a/libunwindstack/tests/ElfCacheTest.cpp
+++ b/libunwindstack/tests/ElfCacheTest.cpp
@@ -31,7 +31,7 @@
 
 class ElfCacheTest : public ::testing::Test {
  protected:
-  static void SetUpTestCase() { memory_.reset(new MemoryFake); }
+  static void SetUpTestSuite() { memory_.reset(new MemoryFake); }
 
   void SetUp() override { Elf::SetCachingEnabled(true); }
 
diff --git a/libunwindstack/tests/MapInfoCreateMemoryTest.cpp b/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
index 6be8bdc..5b4ca7c 100644
--- a/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
+++ b/libunwindstack/tests/MapInfoCreateMemoryTest.cpp
@@ -58,7 +58,7 @@
     ASSERT_TRUE(android::base::WriteFully(fd, buffer.data(), buffer.size()));
   }
 
-  static void SetUpTestCase() {
+  static void SetUpTestSuite() {
     std::vector<uint8_t> buffer(12288, 0);
     memcpy(buffer.data(), ELFMAG, SELFMAG);
     buffer[EI_CLASS] = ELFCLASS32;
diff --git a/libunwindstack/tests/MemoryBufferTest.cpp b/libunwindstack/tests/MemoryBufferTest.cpp
index 28e0e76..a6c12aa 100644
--- a/libunwindstack/tests/MemoryBufferTest.cpp
+++ b/libunwindstack/tests/MemoryBufferTest.cpp
@@ -18,9 +18,8 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
-
 #include "LogFake.h"
+#include "MemoryBuffer.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryCacheTest.cpp b/libunwindstack/tests/MemoryCacheTest.cpp
index a3def20..3bd3e4d 100644
--- a/libunwindstack/tests/MemoryCacheTest.cpp
+++ b/libunwindstack/tests/MemoryCacheTest.cpp
@@ -20,8 +20,7 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
-
+#include "MemoryCache.h"
 #include "MemoryFake.h"
 
 namespace unwindstack {
diff --git a/libunwindstack/tests/MemoryFileTest.cpp b/libunwindstack/tests/MemoryFileTest.cpp
index d7d1ace..4124a49 100644
--- a/libunwindstack/tests/MemoryFileTest.cpp
+++ b/libunwindstack/tests/MemoryFileTest.cpp
@@ -21,7 +21,7 @@
 #include <android-base/file.h>
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
+#include "MemoryFileAtOffset.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryLocalTest.cpp b/libunwindstack/tests/MemoryLocalTest.cpp
index 5a389d0..c9e5dc0 100644
--- a/libunwindstack/tests/MemoryLocalTest.cpp
+++ b/libunwindstack/tests/MemoryLocalTest.cpp
@@ -22,7 +22,7 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
+#include "MemoryLocal.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryOfflineBufferTest.cpp b/libunwindstack/tests/MemoryOfflineBufferTest.cpp
index f022884..9531708 100644
--- a/libunwindstack/tests/MemoryOfflineBufferTest.cpp
+++ b/libunwindstack/tests/MemoryOfflineBufferTest.cpp
@@ -18,9 +18,8 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
-
 #include "LogFake.h"
+#include "MemoryOfflineBuffer.h"
 
 namespace unwindstack {
 
@@ -31,7 +30,7 @@
     memory_.reset(new MemoryOfflineBuffer(buffer_.data(), kStart, kEnd));
   }
 
-  static void SetUpTestCase() {
+  static void SetUpTestSuite() {
     buffer_.resize(kLength);
     for (size_t i = 0; i < kLength; i++) {
       buffer_[i] = i % 189;
diff --git a/libunwindstack/tests/MemoryOfflineTest.cpp b/libunwindstack/tests/MemoryOfflineTest.cpp
index ab9aa9d..d0c441b 100644
--- a/libunwindstack/tests/MemoryOfflineTest.cpp
+++ b/libunwindstack/tests/MemoryOfflineTest.cpp
@@ -19,7 +19,8 @@
 #include <gtest/gtest.h>
 
 #include <android-base/file.h>
-#include <unwindstack/Memory.h>
+
+#include "MemoryOffline.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryRangeTest.cpp b/libunwindstack/tests/MemoryRangeTest.cpp
index 2bac95b..2d4f141 100644
--- a/libunwindstack/tests/MemoryRangeTest.cpp
+++ b/libunwindstack/tests/MemoryRangeTest.cpp
@@ -21,9 +21,8 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
-
 #include "MemoryFake.h"
+#include "MemoryRange.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryRangesTest.cpp b/libunwindstack/tests/MemoryRangesTest.cpp
index d24fcd2..e4e9fc4 100644
--- a/libunwindstack/tests/MemoryRangesTest.cpp
+++ b/libunwindstack/tests/MemoryRangesTest.cpp
@@ -20,9 +20,8 @@
 
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
-
 #include "MemoryFake.h"
+#include "MemoryRange.h"
 
 namespace unwindstack {
 
diff --git a/libunwindstack/tests/MemoryRemoteTest.cpp b/libunwindstack/tests/MemoryRemoteTest.cpp
index fb56e8a..c90dedc 100644
--- a/libunwindstack/tests/MemoryRemoteTest.cpp
+++ b/libunwindstack/tests/MemoryRemoteTest.cpp
@@ -30,7 +30,7 @@
 #include <android-base/file.h>
 #include <gtest/gtest.h>
 
-#include <unwindstack/Memory.h>
+#include "MemoryRemote.h"
 
 #include "MemoryFake.h"
 #include "TestUtils.h"
diff --git a/libunwindstack/tests/RegsIterateTest.cpp b/libunwindstack/tests/RegsIterateTest.cpp
index 9a27dbd..7e36953 100644
--- a/libunwindstack/tests/RegsIterateTest.cpp
+++ b/libunwindstack/tests/RegsIterateTest.cpp
@@ -236,7 +236,7 @@
 }
 
 using RegTypes = ::testing::Types<RegsArm, RegsArm64, RegsX86, RegsX86_64, RegsMips, RegsMips64>;
-TYPED_TEST_CASE(RegsIterateTest, RegTypes);
+TYPED_TEST_SUITE(RegsIterateTest, RegTypes);
 
 TYPED_TEST(RegsIterateTest, iterate) {
   std::vector<Register> expected = ExpectedRegisters<TypeParam>();
diff --git a/libunwindstack/tests/SymbolsTest.cpp b/libunwindstack/tests/SymbolsTest.cpp
index b40a253..ae3c349 100644
--- a/libunwindstack/tests/SymbolsTest.cpp
+++ b/libunwindstack/tests/SymbolsTest.cpp
@@ -55,7 +55,7 @@
 
   MemoryFake memory_;
 };
-TYPED_TEST_CASE_P(SymbolsTest);
+TYPED_TEST_SUITE_P(SymbolsTest);
 
 TYPED_TEST_P(SymbolsTest, function_bounds_check) {
   Symbols symbols(0x1000, sizeof(TypeParam), sizeof(TypeParam), 0x2000, 0x100);
@@ -362,11 +362,11 @@
   EXPECT_EQ(4U, offset);
 }
 
-REGISTER_TYPED_TEST_CASE_P(SymbolsTest, function_bounds_check, no_symbol, multiple_entries,
-                           multiple_entries_nonstandard_size, symtab_value_out_of_bounds,
-                           symtab_read_cached, get_global);
+REGISTER_TYPED_TEST_SUITE_P(SymbolsTest, function_bounds_check, no_symbol, multiple_entries,
+                            multiple_entries_nonstandard_size, symtab_value_out_of_bounds,
+                            symtab_read_cached, get_global);
 
 typedef ::testing::Types<Elf32_Sym, Elf64_Sym> SymbolsTestTypes;
-INSTANTIATE_TYPED_TEST_CASE_P(, SymbolsTest, SymbolsTestTypes);
+INSTANTIATE_TYPED_TEST_SUITE_P(, SymbolsTest, SymbolsTestTypes);
 
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/UnwindOfflineTest.cpp b/libunwindstack/tests/UnwindOfflineTest.cpp
index 6c64c40..baada82 100644
--- a/libunwindstack/tests/UnwindOfflineTest.cpp
+++ b/libunwindstack/tests/UnwindOfflineTest.cpp
@@ -35,7 +35,6 @@
 #include <unwindstack/MachineX86.h>
 #include <unwindstack/MachineX86_64.h>
 #include <unwindstack/Maps.h>
-#include <unwindstack/Memory.h>
 #include <unwindstack/RegsArm.h>
 #include <unwindstack/RegsArm64.h>
 #include <unwindstack/RegsX86.h>
@@ -43,6 +42,7 @@
 #include <unwindstack/Unwinder.h>
 
 #include "ElfTestUtils.h"
+#include "MemoryOffline.h"
 #include "TestUtils.h"
 
 namespace unwindstack {
@@ -62,7 +62,7 @@
     free(cwd_);
   }
 
-  void Init(const char* file_dir, ArchEnum arch) {
+  void Init(const char* file_dir, ArchEnum arch, bool add_stack = true) {
     dir_ = TestGetFileDirectory() + "offline/" + file_dir;
 
     std::string data;
@@ -71,23 +71,25 @@
     maps_.reset(new BufferMaps(data.c_str()));
     ASSERT_TRUE(maps_->Parse());
 
-    std::string stack_name(dir_ + "stack.data");
-    struct stat st;
-    if (stat(stack_name.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
-      std::unique_ptr<MemoryOffline> stack_memory(new MemoryOffline);
-      ASSERT_TRUE(stack_memory->Init((dir_ + "stack.data").c_str(), 0));
-      process_memory_.reset(stack_memory.release());
-    } else {
-      std::unique_ptr<MemoryOfflineParts> stack_memory(new MemoryOfflineParts);
-      for (size_t i = 0;; i++) {
-        stack_name = dir_ + "stack" + std::to_string(i) + ".data";
-        if (stat(stack_name.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) {
-          ASSERT_TRUE(i != 0) << "No stack data files found.";
-          break;
+    if (add_stack) {
+      std::string stack_name(dir_ + "stack.data");
+      struct stat st;
+      if (stat(stack_name.c_str(), &st) == 0 && S_ISREG(st.st_mode)) {
+        std::unique_ptr<MemoryOffline> stack_memory(new MemoryOffline);
+        ASSERT_TRUE(stack_memory->Init((dir_ + "stack.data").c_str(), 0));
+        process_memory_.reset(stack_memory.release());
+      } else {
+        std::unique_ptr<MemoryOfflineParts> stack_memory(new MemoryOfflineParts);
+        for (size_t i = 0;; i++) {
+          stack_name = dir_ + "stack" + std::to_string(i) + ".data";
+          if (stat(stack_name.c_str(), &st) == -1 || !S_ISREG(st.st_mode)) {
+            ASSERT_TRUE(i != 0) << "No stack data files found.";
+            break;
+          }
+          AddMemory(stack_name, stack_memory.get());
         }
-        AddMemory(stack_name, stack_memory.get());
+        process_memory_.reset(stack_memory.release());
       }
-      process_memory_.reset(stack_memory.release());
     }
 
     switch (arch) {
@@ -1442,4 +1444,17 @@
   EXPECT_EQ(0x7be4f07d20ULL, unwinder.frames()[12].sp);
 }
 
+TEST_F(UnwindOfflineTest, invalid_elf_offset_arm) {
+  ASSERT_NO_FATAL_FAILURE(Init("invalid_elf_offset_arm/", ARCH_ARM, false));
+
+  Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_);
+  unwinder.Unwind();
+
+  std::string frame_info(DumpFrames(unwinder));
+  ASSERT_EQ(1U, unwinder.NumFrames()) << "Unwind:\n" << frame_info;
+  EXPECT_EQ("  #00 pc 00aa7508  invalid.apk (offset 0x12e4000)\n", frame_info);
+  EXPECT_EQ(0xc898f508, unwinder.frames()[0].pc);
+  EXPECT_EQ(0xc2044218, unwinder.frames()[0].sp);
+}
+
 }  // namespace unwindstack
diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp
index 4e38015..f76a101 100644
--- a/libunwindstack/tests/UnwindTest.cpp
+++ b/libunwindstack/tests/UnwindTest.cpp
@@ -35,11 +35,11 @@
 #include <android-base/threads.h>
 
 #include <unwindstack/Maps.h>
-#include <unwindstack/Memory.h>
 #include <unwindstack/Regs.h>
 #include <unwindstack/RegsGetLocal.h>
 #include <unwindstack/Unwinder.h>
 
+#include "MemoryRemote.h"
 #include "TestUtils.h"
 
 namespace unwindstack {
diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp
index 1463167..ef1950c 100644
--- a/libunwindstack/tests/UnwinderTest.cpp
+++ b/libunwindstack/tests/UnwinderTest.cpp
@@ -54,7 +54,7 @@
     }
   }
 
-  static void SetUpTestCase() {
+  static void SetUpTestSuite() {
     maps_.reset(new Maps);
 
     ElfFake* elf = new ElfFake(new MemoryFake);
diff --git a/libunwindstack/tests/files/offline/invalid_elf_offset_arm/maps.txt b/libunwindstack/tests/files/offline/invalid_elf_offset_arm/maps.txt
new file mode 100644
index 0000000..022404c
--- /dev/null
+++ b/libunwindstack/tests/files/offline/invalid_elf_offset_arm/maps.txt
@@ -0,0 +1 @@
+c7ee8000-c8c52fff r-xp  12e4000    00:00 0  invalid.apk
diff --git a/libunwindstack/tests/files/offline/invalid_elf_offset_arm/regs.txt b/libunwindstack/tests/files/offline/invalid_elf_offset_arm/regs.txt
new file mode 100644
index 0000000..b7f10ef
--- /dev/null
+++ b/libunwindstack/tests/files/offline/invalid_elf_offset_arm/regs.txt
@@ -0,0 +1,16 @@
+r0: c0434c00
+r1: 2a4c9fbc
+r2: 00000000
+r3: c83ef1f9
+r4: 00000004
+r5: c2044904
+r6: 00000000
+r7: c20443b8
+r8: 000b33ff
+r9: c20444b0
+r10: cac90740
+r11: 00000000
+ip: ed891ca4
+sp: c2044218
+lr: ed807265
+pc: c898f508
diff --git a/libunwindstack/tools/unwind_info.cpp b/libunwindstack/tools/unwind_info.cpp
index 92e5c0a..7a6d8ba 100644
--- a/libunwindstack/tools/unwind_info.cpp
+++ b/libunwindstack/tools/unwind_info.cpp
@@ -31,6 +31,7 @@
 #include <unwindstack/Elf.h>
 #include <unwindstack/ElfInterface.h>
 #include <unwindstack/Log.h>
+#include <unwindstack/Memory.h>
 
 #include "ArmExidx.h"
 #include "ElfInterfaceArm.h"
@@ -105,14 +106,7 @@
   // Send all log messages to stdout.
   log_to_stdout(true);
 
-  MemoryFileAtOffset* memory = new MemoryFileAtOffset;
-  if (!memory->Init(file, offset)) {
-    // Initializatation failed.
-    printf("Failed to init\n");
-    return 1;
-  }
-
-  Elf elf(memory);
+  Elf elf(Memory::CreateFileMemory(file, offset).release());
   if (!elf.Init() || !elf.valid()) {
     printf("%s is not a valid elf file.\n", file);
     return 1;
diff --git a/libunwindstack/tools/unwind_reg_info.cpp b/libunwindstack/tools/unwind_reg_info.cpp
index b77a86b..d0562d9 100644
--- a/libunwindstack/tools/unwind_reg_info.cpp
+++ b/libunwindstack/tools/unwind_reg_info.cpp
@@ -33,6 +33,7 @@
 #include <unwindstack/Elf.h>
 #include <unwindstack/ElfInterface.h>
 #include <unwindstack/Log.h>
+#include <unwindstack/Memory.h>
 
 #include "ArmExidx.h"
 #include "DwarfOp.h"
@@ -165,14 +166,7 @@
 }
 
 int GetInfo(const char* file, uint64_t pc) {
-  MemoryFileAtOffset* memory = new MemoryFileAtOffset;
-  if (!memory->Init(file, 0)) {
-    // Initializatation failed.
-    printf("Failed to init\n");
-    return 1;
-  }
-
-  Elf elf(memory);
+  Elf elf(Memory::CreateFileMemory(file, pc).release());
   if (!elf.Init() || !elf.valid()) {
     printf("%s is not a valid elf file.\n", file);
     return 1;
@@ -205,7 +199,7 @@
   DwarfSection* section = interface->eh_frame();
   if (section != nullptr) {
     printf("\neh_frame:\n");
-    PrintRegInformation(section, memory, pc, elf.class_type());
+    PrintRegInformation(section, elf.memory(), pc, elf.class_type());
   } else {
     printf("\nno eh_frame information\n");
   }
@@ -213,7 +207,7 @@
   section = interface->debug_frame();
   if (section != nullptr) {
     printf("\ndebug_frame:\n");
-    PrintRegInformation(section, memory, pc, elf.class_type());
+    PrintRegInformation(section, elf.memory(), pc, elf.class_type());
     printf("\n");
   } else {
     printf("\nno debug_frame information\n");
diff --git a/libunwindstack/tools/unwind_symbols.cpp b/libunwindstack/tools/unwind_symbols.cpp
index b0a4dd0..8df2284 100644
--- a/libunwindstack/tools/unwind_symbols.cpp
+++ b/libunwindstack/tools/unwind_symbols.cpp
@@ -59,13 +59,7 @@
   // Send all log messages to stdout.
   unwindstack::log_to_stdout(true);
 
-  unwindstack::MemoryFileAtOffset* memory = new unwindstack::MemoryFileAtOffset;
-  if (!memory->Init(argv[1], 0)) {
-    printf("Failed to init\n");
-    return 1;
-  }
-
-  unwindstack::Elf elf(memory);
+  unwindstack::Elf elf(unwindstack::Memory::CreateFileMemory(argv[1], 0).release());
   if (!elf.Init() || !elf.valid()) {
     printf("%s is not a valid elf file.\n", argv[1]);
     return 1;
diff --git a/libziparchive/include/ziparchive/zip_archive.h b/libziparchive/include/ziparchive/zip_archive.h
index a56a4a2..e3ac114 100644
--- a/libziparchive/include/ziparchive/zip_archive.h
+++ b/libziparchive/include/ziparchive/zip_archive.h
@@ -36,30 +36,6 @@
   kCompressDeflated = 8,  // standard deflate
 };
 
-// TODO: remove this when everyone's moved over to std::string.
-struct ZipString {
-  const uint8_t* name;
-  uint16_t name_length;
-
-  ZipString() {}
-
-  explicit ZipString(std::string_view entry_name);
-
-  bool operator==(const ZipString& rhs) const {
-    return name && (name_length == rhs.name_length) && (memcmp(name, rhs.name, name_length) == 0);
-  }
-
-  bool StartsWith(const ZipString& prefix) const {
-    return name && (name_length >= prefix.name_length) &&
-           (memcmp(name, prefix.name, prefix.name_length) == 0);
-  }
-
-  bool EndsWith(const ZipString& suffix) const {
-    return name && (name_length >= suffix.name_length) &&
-           (memcmp(name + name_length - suffix.name_length, suffix.name, suffix.name_length) == 0);
-  }
-};
-
 /*
  * Represents information about a zip entry in a zip file.
  */
@@ -190,8 +166,7 @@
  * archive and lower negative values on failure.
  */
 int32_t Next(void* cookie, ZipEntry* data, std::string* name);
-// TODO: remove this when everyone's moved over to std::string.
-int32_t Next(void* cookie, ZipEntry* data, ZipString* name);
+int32_t Next(void* cookie, ZipEntry* data, std::string_view* name);
 
 /*
  * End iteration over all entries of a zip file and frees the memory allocated
diff --git a/libziparchive/include/ziparchive/zip_writer.h b/libziparchive/include/ziparchive/zip_writer.h
index bd44fdb..a2a0dbf 100644
--- a/libziparchive/include/ziparchive/zip_writer.h
+++ b/libziparchive/include/ziparchive/zip_writer.h
@@ -21,6 +21,7 @@
 
 #include <memory>
 #include <string>
+#include <string_view>
 #include <vector>
 
 #include "android-base/macros.h"
@@ -101,7 +102,7 @@
    * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
    * Returns 0 on success, and an error value < 0 on failure.
    */
-  int32_t StartEntry(const char* path, size_t flags);
+  int32_t StartEntry(std::string_view path, size_t flags);
 
   /**
    * Starts a new zip entry with the given path and flags, where the
@@ -111,17 +112,17 @@
    * Subsequent calls to WriteBytes(const void*, size_t) will add data to this entry.
    * Returns 0 on success, and an error value < 0 on failure.
    */
-  int32_t StartAlignedEntry(const char* path, size_t flags, uint32_t alignment);
+  int32_t StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment);
 
   /**
    * Same as StartEntry(const char*, size_t), but sets a last modified time for the entry.
    */
-  int32_t StartEntryWithTime(const char* path, size_t flags, time_t time);
+  int32_t StartEntryWithTime(std::string_view path, size_t flags, time_t time);
 
   /**
    * Same as StartAlignedEntry(const char*, size_t), but sets a last modified time for the entry.
    */
-  int32_t StartAlignedEntryWithTime(const char* path, size_t flags, time_t time, uint32_t alignment);
+  int32_t StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time, uint32_t alignment);
 
   /**
    * Writes bytes to the zip file for the previously started zip entry.
diff --git a/libziparchive/zip_archive.cc b/libziparchive/zip_archive.cc
index f4b6c74..c95b035 100644
--- a/libziparchive/zip_archive.cc
+++ b/libziparchive/zip_archive.cc
@@ -47,6 +47,7 @@
 #include <android-base/macros.h>  // TEMP_FAILURE_RETRY may or may not be in unistd
 #include <android-base/mapped_file.h>
 #include <android-base/memory.h>
+#include <android-base/strings.h>
 #include <android-base/utf8.h>
 #include <log/log.h>
 #include "zlib.h"
@@ -101,25 +102,8 @@
   return val;
 }
 
-static uint32_t ComputeHash(const ZipString& name) {
-  return static_cast<uint32_t>(std::hash<std::string_view>{}(
-      std::string_view(reinterpret_cast<const char*>(name.name), name.name_length)));
-}
-
-static bool isZipStringEqual(const uint8_t* start, const ZipString& zip_string,
-                             const ZipStringOffset& zip_string_offset) {
-  const ZipString from_offset = zip_string_offset.GetZipString(start);
-  return from_offset == zip_string;
-}
-
-/**
- * Returns offset of ZipString#name from the start of the central directory in the memory map.
- * For valid ZipStrings contained in the zip archive mmap, 0 < offset < 0xffffff.
- */
-static inline uint32_t GetOffset(const uint8_t* name, const uint8_t* start) {
-  CHECK_GT(name, start);
-  CHECK_LT(name, start + 0xffffff);
-  return static_cast<uint32_t>(name - start);
+static uint32_t ComputeHash(std::string_view name) {
+  return static_cast<uint32_t>(std::hash<std::string_view>{}(name));
 }
 
 /*
@@ -127,19 +111,19 @@
  * valid range.
  */
 static int64_t EntryToIndex(const ZipStringOffset* hash_table, const uint32_t hash_table_size,
-                            const ZipString& name, const uint8_t* start) {
+                            std::string_view name, const uint8_t* start) {
   const uint32_t hash = ComputeHash(name);
 
   // NOTE: (hash_table_size - 1) is guaranteed to be non-negative.
   uint32_t ent = hash & (hash_table_size - 1);
   while (hash_table[ent].name_offset != 0) {
-    if (isZipStringEqual(start, name, hash_table[ent])) {
+    if (hash_table[ent].ToStringView(start) == name) {
       return ent;
     }
     ent = (ent + 1) & (hash_table_size - 1);
   }
 
-  ALOGV("Zip: Unable to find entry %.*s", name.name_length, name.name);
+  ALOGV("Zip: Unable to find entry %.*s", static_cast<int>(name.size()), name.data());
   return kEntryNotFound;
 }
 
@@ -147,7 +131,7 @@
  * Add a new entry to the hash table.
  */
 static int32_t AddToHash(ZipStringOffset* hash_table, const uint32_t hash_table_size,
-                         const ZipString& name, const uint8_t* start) {
+                         std::string_view name, const uint8_t* start) {
   const uint64_t hash = ComputeHash(name);
   uint32_t ent = hash & (hash_table_size - 1);
 
@@ -156,15 +140,18 @@
    * Further, we guarantee that the hashtable size is not 0.
    */
   while (hash_table[ent].name_offset != 0) {
-    if (isZipStringEqual(start, name, hash_table[ent])) {
-      // We've found a duplicate entry. We don't accept it
-      ALOGW("Zip: Found duplicate entry %.*s", name.name_length, name.name);
+    if (hash_table[ent].ToStringView(start) == name) {
+      // We've found a duplicate entry. We don't accept duplicates.
+      ALOGW("Zip: Found duplicate entry %.*s", static_cast<int>(name.size()), name.data());
       return kDuplicateEntry;
     }
     ent = (ent + 1) & (hash_table_size - 1);
   }
-  hash_table[ent].name_offset = GetOffset(name.name, start);
-  hash_table[ent].name_length = name.name_length;
+
+  // `name` has already been validated before entry.
+  const char* start_char = reinterpret_cast<const char*>(start);
+  hash_table[ent].name_offset = static_cast<uint32_t>(name.data() - start_char);
+  hash_table[ent].name_length = static_cast<uint16_t>(name.size());
   return 0;
 }
 
@@ -366,7 +353,7 @@
       reinterpret_cast<ZipStringOffset*>(calloc(archive->hash_table_size, sizeof(ZipStringOffset)));
   if (archive->hash_table == nullptr) {
     ALOGW("Zip: unable to allocate the %u-entry hash_table, entry size: %zu",
-          archive->hash_table_size, sizeof(ZipString));
+          archive->hash_table_size, sizeof(ZipStringOffset));
     return -1;
   }
 
@@ -404,21 +391,19 @@
     const uint8_t* file_name = ptr + sizeof(CentralDirectoryRecord);
 
     if (file_name + file_name_length > cd_end) {
-      ALOGW(
-          "Zip: file name boundary exceeds the central directory range, file_name_length: "
-          "%" PRIx16 ", cd_length: %zu",
-          file_name_length, cd_length);
+      ALOGW("Zip: file name for entry %" PRIu16
+            " exceeds the central directory range, file_name_length: %" PRIu16 ", cd_length: %zu",
+            i, file_name_length, cd_length);
       return -1;
     }
-    /* check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters */
+    // Check that file name is valid UTF-8 and doesn't contain NUL (U+0000) characters.
     if (!IsValidEntryName(file_name, file_name_length)) {
+      ALOGW("Zip: invalid file name at entry %" PRIu16, i);
       return -1;
     }
 
-    /* add the CDE filename to the hash table */
-    ZipString entry_name;
-    entry_name.name = file_name;
-    entry_name.name_length = file_name_length;
+    // Add the CDE filename to the hash table.
+    std::string_view entry_name{reinterpret_cast<const char*>(file_name), file_name_length};
     const int add_result = AddToHash(archive->hash_table, archive->hash_table_size, entry_name,
                                      archive->central_directory.GetBasePtr());
     if (add_result != 0) {
@@ -539,15 +524,13 @@
   // Recover the start of the central directory entry from the filename
   // pointer.  The filename is the first entry past the fixed-size data,
   // so we can just subtract back from that.
-  const ZipString from_offset =
-      archive->hash_table[ent].GetZipString(archive->central_directory.GetBasePtr());
-  const uint8_t* ptr = from_offset.name;
+  const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
+  const uint8_t* ptr = base_ptr + archive->hash_table[ent].name_offset;
   ptr -= sizeof(CentralDirectoryRecord);
 
   // This is the base of our mmapped region, we have to sanity check that
   // the name that's in the hash table is a pointer to a location within
   // this mapped region.
-  const uint8_t* base_ptr = archive->central_directory.GetBasePtr();
   if (ptr < base_ptr || ptr > base_ptr + archive->central_directory.GetMapLength()) {
     ALOGW("Zip: Invalid entry pointer");
     return kInvalidOffset;
@@ -639,26 +622,24 @@
 
   // Check that the local file header name matches the declared
   // name in the central directory.
-  if (lfh->file_name_length == nameLen) {
-    const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
-    if (name_offset + lfh->file_name_length > cd_offset) {
-      ALOGW("Zip: Invalid declared length");
-      return kInvalidOffset;
-    }
-
-    std::vector<uint8_t> name_buf(nameLen);
-    if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
-      ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
-      return kIoError;
-    }
-    const ZipString from_offset =
-        archive->hash_table[ent].GetZipString(archive->central_directory.GetBasePtr());
-    if (memcmp(from_offset.name, name_buf.data(), nameLen)) {
-      return kInconsistentInformation;
-    }
-
-  } else {
-    ALOGW("Zip: lfh name did not match central directory.");
+  if (lfh->file_name_length != nameLen) {
+    ALOGW("Zip: lfh name length did not match central directory");
+    return kInconsistentInformation;
+  }
+  const off64_t name_offset = local_header_offset + sizeof(LocalFileHeader);
+  if (name_offset + lfh->file_name_length > cd_offset) {
+    ALOGW("Zip: lfh name has invalid declared length");
+    return kInvalidOffset;
+  }
+  std::vector<uint8_t> name_buf(nameLen);
+  if (!archive->mapped_zip.ReadAtOffset(name_buf.data(), nameLen, name_offset)) {
+    ALOGW("Zip: failed reading lfh name from offset %" PRId64, static_cast<int64_t>(name_offset));
+    return kIoError;
+  }
+  const std::string_view entry_name =
+      archive->hash_table[ent].ToStringView(archive->central_directory.GetBasePtr());
+  if (memcmp(entry_name.data(), name_buf.data(), nameLen) != 0) {
+    ALOGW("Zip: lfh name did not match central directory");
     return kInconsistentInformation;
   }
 
@@ -691,21 +672,13 @@
 struct IterationHandle {
   ZipArchive* archive;
 
-  std::string prefix_holder;
-  ZipString prefix;
-
-  std::string suffix_holder;
-  ZipString suffix;
+  std::string prefix;
+  std::string suffix;
 
   uint32_t position = 0;
 
-  IterationHandle(ZipArchive* archive, const std::string_view in_prefix,
-                  const std::string_view in_suffix)
-      : archive(archive),
-        prefix_holder(in_prefix),
-        prefix(prefix_holder),
-        suffix_holder(in_suffix),
-        suffix(suffix_holder) {}
+  IterationHandle(ZipArchive* archive, std::string_view in_prefix, std::string_view in_suffix)
+      : archive(archive), prefix(in_prefix), suffix(in_suffix) {}
 };
 
 int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
@@ -737,8 +710,8 @@
     return kInvalidEntryName;
   }
 
-  const int64_t ent = EntryToIndex(archive->hash_table, archive->hash_table_size,
-                                   ZipString(entryName), archive->central_directory.GetBasePtr());
+  const int64_t ent = EntryToIndex(archive->hash_table, archive->hash_table_size, entryName,
+                                   archive->central_directory.GetBasePtr());
   if (ent < 0) {
     ALOGV("Zip: Could not find entry %.*s", static_cast<int>(entryName.size()), entryName.data());
     return static_cast<int32_t>(ent);  // kEntryNotFound is safe to truncate.
@@ -748,15 +721,15 @@
 }
 
 int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
-  ZipString zs;
-  int32_t result = Next(cookie, data, &zs);
-  if (result == 0) {
-    *name = std::string(reinterpret_cast<const char*>(zs.name), zs.name_length);
+  std::string_view sv;
+  int32_t result = Next(cookie, data, &sv);
+  if (result == 0 && name) {
+    *name = std::string(sv);
   }
   return result;
 }
 
-int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
+int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
   IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
   if (handle == NULL) {
     ALOGW("Zip: Null ZipArchiveHandle");
@@ -773,16 +746,14 @@
   const uint32_t hash_table_length = archive->hash_table_size;
   const ZipStringOffset* hash_table = archive->hash_table;
   for (uint32_t i = currentOffset; i < hash_table_length; ++i) {
-    const ZipString from_offset =
-        hash_table[i].GetZipString(archive->central_directory.GetBasePtr());
-    if (hash_table[i].name_offset != 0 &&
-        (handle->prefix.name_length == 0 || from_offset.StartsWith(handle->prefix)) &&
-        (handle->suffix.name_length == 0 || from_offset.EndsWith(handle->suffix))) {
+    const std::string_view entry_name =
+        hash_table[i].ToStringView(archive->central_directory.GetBasePtr());
+    if (hash_table[i].name_offset != 0 && (android::base::StartsWith(entry_name, handle->prefix) &&
+                                           android::base::EndsWith(entry_name, handle->suffix))) {
       handle->position = (i + 1);
       const int error = FindEntry(archive, i, data);
-      if (!error) {
-        name->name = from_offset.name;
-        name->name_length = hash_table[i].name_length;
+      if (!error && name) {
+        *name = entry_name;
       }
       return error;
     }
@@ -1150,13 +1121,6 @@
   return archive->mapped_zip.GetFileDescriptor();
 }
 
-ZipString::ZipString(std::string_view entry_name)
-    : name(reinterpret_cast<const uint8_t*>(entry_name.data())) {
-  size_t len = entry_name.size();
-  CHECK_LE(len, static_cast<size_t>(UINT16_MAX));
-  name_length = static_cast<uint16_t>(len);
-}
-
 #if !defined(_WIN32)
 class ProcessWriter : public zip_archive::Writer {
  public:
diff --git a/libziparchive/zip_archive_benchmark.cpp b/libziparchive/zip_archive_benchmark.cpp
index 23ed408..09d3b8a 100644
--- a/libziparchive/zip_archive_benchmark.cpp
+++ b/libziparchive/zip_archive_benchmark.cpp
@@ -58,7 +58,7 @@
   std::string_view name("thisFileNameDoesNotExist");
 
   // Start the benchmark.
-  while (state.KeepRunning()) {
+  for (auto _ : state) {
     OpenArchive(temp_file->path, &handle);
     FindEntry(handle, name, &data);
     CloseArchive(handle);
@@ -73,7 +73,7 @@
   ZipEntry data;
   std::string name;
 
-  while (state.KeepRunning()) {
+  for (auto _ : state) {
     OpenArchive(temp_file->path, &handle);
     StartIteration(handle, &iteration_cookie);
     while (Next(iteration_cookie, &data, &name) == 0) {
@@ -84,4 +84,27 @@
 }
 BENCHMARK(Iterate_all_files);
 
+static void StartAlignedEntry(benchmark::State& state) {
+  TemporaryFile file;
+  FILE* fp = fdopen(file.fd, "w");
+
+  ZipWriter writer(fp);
+
+  auto alignment = uint32_t(state.range(0));
+  std::string name = "name";
+  int counter = 0;
+  for (auto _ : state) {
+    writer.StartAlignedEntry(name + std::to_string(counter++), 0, alignment);
+    state.PauseTiming();
+    writer.WriteBytes("hola", 4);
+    writer.FinishEntry();
+    state.ResumeTiming();
+  }
+
+  writer.Finish();
+  fclose(fp);
+}
+BENCHMARK(StartAlignedEntry)->Arg(2)->Arg(16)->Arg(1024)->Arg(4096);
+
+
 BENCHMARK_MAIN();
diff --git a/libziparchive/zip_archive_private.h b/libziparchive/zip_archive_private.h
index 330a02a..30a1d72 100644
--- a/libziparchive/zip_archive_private.h
+++ b/libziparchive/zip_archive_private.h
@@ -137,22 +137,22 @@
 };
 
 /**
- * More space efficient string representation of strings in an mmaped zipped file than
- * std::string_view or ZipString. Using ZipString as an entry in the ZipArchive hashtable wastes
- * space. ZipString stores a pointer to a string (on 64 bit, 8 bytes) and the length to read from
- * that pointer, 2 bytes. Because of alignment, the structure consumes 16 bytes, wasting 6 bytes.
- * ZipStringOffset stores a 4 byte offset from a fixed location in the memory mapped file instead
- * of the entire address, consuming 8 bytes with alignment.
+ * More space efficient string representation of strings in an mmaped zipped
+ * file than std::string_view. Using std::string_view as an entry in the
+ * ZipArchive hash table wastes space. std::string_view stores a pointer to a
+ * string (on 64 bit, 8 bytes) and the length to read from that pointer,
+ * 2 bytes. Because of alignment, the structure consumes 16 bytes, wasting
+ * 6 bytes.
+ *
+ * ZipStringOffset stores a 4 byte offset from a fixed location in the memory
+ * mapped file instead of the entire address, consuming 8 bytes with alignment.
  */
 struct ZipStringOffset {
   uint32_t name_offset;
   uint16_t name_length;
 
-  const ZipString GetZipString(const uint8_t* start) const {
-    ZipString zip_string;
-    zip_string.name = start + name_offset;
-    zip_string.name_length = name_length;
-    return zip_string;
+  const std::string_view ToStringView(const uint8_t* start) const {
+    return std::string_view{reinterpret_cast<const char*>(start + name_offset), name_length};
   }
 };
 
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc
index b6ca9ec..8781ab7 100644
--- a/libziparchive/zip_archive_test.cc
+++ b/libziparchive/zip_archive_test.cc
@@ -107,6 +107,26 @@
   close(fd);
 }
 
+TEST(ziparchive, Iteration_std_string_view) {
+  ZipArchiveHandle handle;
+  ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
+
+  void* iteration_cookie;
+  ASSERT_EQ(0, StartIteration(handle, &iteration_cookie));
+
+  ZipEntry data;
+  std::vector<std::string_view> names;
+  std::string_view name;
+  while (Next(iteration_cookie, &data, &name) == 0) names.push_back(name);
+
+  // Assert that the names are as expected.
+  std::vector<std::string_view> expected_names{"a.txt", "b.txt", "b/", "b/c.txt", "b/d.txt"};
+  std::sort(names.begin(), names.end());
+  ASSERT_EQ(expected_names, names);
+
+  CloseArchive(handle);
+}
+
 static void AssertIterationOrder(const std::string_view prefix, const std::string_view suffix,
                                  const std::vector<std::string>& expected_names_sorted) {
   ZipArchiveHandle handle;
diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc
index ae9d145..198154b 100644
--- a/libziparchive/zip_writer.cc
+++ b/libziparchive/zip_writer.cc
@@ -130,7 +130,7 @@
   return error_code;
 }
 
-int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
+int32_t ZipWriter::StartEntry(std::string_view path, size_t flags) {
   uint32_t alignment = 0;
   if (flags & kAlign32) {
     flags &= ~kAlign32;
@@ -139,11 +139,11 @@
   return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
 }
 
-int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
+int32_t ZipWriter::StartAlignedEntry(std::string_view path, size_t flags, uint32_t alignment) {
   return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
 }
 
-int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) {
+int32_t ZipWriter::StartEntryWithTime(std::string_view path, size_t flags, time_t time) {
   uint32_t alignment = 0;
   if (flags & kAlign32) {
     flags &= ~kAlign32;
@@ -198,7 +198,7 @@
   dst->extra_field_length = src.padding_length;
 }
 
-int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time,
+int32_t ZipWriter::StartAlignedEntryWithTime(std::string_view path, size_t flags, time_t time,
                                              uint32_t alignment) {
   if (state_ != State::kWritingZip) {
     return kInvalidState;
@@ -247,13 +247,24 @@
   ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date);
 
   off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size();
-  std::vector<char> zero_padding;
+  // prepare a pre-zeroed memory page in case when we need to pad some aligned data.
+  static constexpr auto kPageSize = 4096;
+  static constexpr char kSmallZeroPadding[kPageSize] = {};
+  // use this buffer if our preallocated one is too small
+  std::vector<char> zero_padding_big;
+  const char* zero_padding = nullptr;
+
   if (alignment != 0 && (offset & (alignment - 1))) {
     // Pad the extra field so the data will be aligned.
     uint16_t padding = static_cast<uint16_t>(alignment - (offset % alignment));
     file_entry.padding_length = padding;
     offset += padding;
-    zero_padding.resize(padding, 0);
+    if (padding <= std::size(kSmallZeroPadding)) {
+        zero_padding = kSmallZeroPadding;
+    } else {
+        zero_padding_big.resize(padding, 0);
+        zero_padding = zero_padding_big.data();
+    }
   }
 
   LocalFileHeader header = {};
@@ -265,11 +276,11 @@
     return HandleError(kIoError);
   }
 
-  if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) {
+  if (fwrite(path.data(), 1, path.size(), file_) != path.size()) {
     return HandleError(kIoError);
   }
 
-  if (file_entry.padding_length != 0 && fwrite(zero_padding.data(), 1, file_entry.padding_length,
+  if (file_entry.padding_length != 0 && fwrite(zero_padding, 1, file_entry.padding_length,
                                                file_) != file_entry.padding_length) {
     return HandleError(kIoError);
   }
diff --git a/mkbootimg/Android.bp b/mkbootimg/Android.bp
deleted file mode 100644
index c3cf746..0000000
--- a/mkbootimg/Android.bp
+++ /dev/null
@@ -1,64 +0,0 @@
-// Copyright 2012 The Android Open Source Project
-
-cc_library_headers {
-    name: "libmkbootimg_abi_headers",
-    vendor_available: true,
-    export_include_dirs: ["include"],
-}
-
-cc_library_headers {
-    name: "bootimg_headers",
-    vendor_available: true,
-    recovery_available: true,
-    export_include_dirs: ["include/bootimg"],
-    host_supported: true,
-    target: {
-        windows: {
-            enabled: true,
-        },
-    },
-}
-
-cc_library {
-    name: "libmkbootimg_abi_check",
-    vendor_available: true,
-    vndk: {
-        enabled: true,
-    },
-    srcs: [
-        "mkbootimg_dummy.cpp",
-    ],
-    header_libs: ["libmkbootimg_abi_headers"],
-    export_header_lib_headers: ["libmkbootimg_abi_headers"],
-}
-
-python_defaults {
-    name: "mkbootimg_defaults",
-
-    version: {
-        py2: {
-            enabled: true,
-            embedded_launcher: true,
-        },
-        py3: {
-            enabled: false,
-            embedded_launcher: false,
-        },
-    },
-}
-
-python_binary_host {
-    name: "mkbootimg",
-    defaults: ["mkbootimg_defaults"],
-    srcs: [
-        "mkbootimg.py",
-    ],
-}
-
-python_binary_host {
-    name: "unpack_bootimg",
-    defaults: ["mkbootimg_defaults"],
-    srcs: [
-        "unpack_bootimg.py",
-    ],
-}
diff --git a/mkbootimg/OWNERS b/mkbootimg/OWNERS
deleted file mode 100644
index 3a00860..0000000
--- a/mkbootimg/OWNERS
+++ /dev/null
@@ -1,2 +0,0 @@
-hridya@google.com
-tbao@google.com
diff --git a/mkbootimg/include/abi_check/mkbootimg_abi_check.h b/mkbootimg/include/abi_check/mkbootimg_abi_check.h
deleted file mode 100644
index d478aba..0000000
--- a/mkbootimg/include/abi_check/mkbootimg_abi_check.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2018 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <bootimg/bootimg.h>
-
-// This header has been created for the following reaons:
-//    1) In order for a change in a user defined type to be classified as API /
-//       ABI breaking, it needs to be referenced by an 'exported interface'
-//       (in this case the function mkbootimg_dummy).
-//    2) Since 'mkbootimg_dummy' needs to be exported, we need to have it
-//       exposed through a public header.
-//    3) It is desirable not to pollute bootimg.h with interfaces which are not
-//       'used' in reality by on device binaries. Furthermore, bootimg.h might
-//       be exported by a library in the future, so we must avoid polluting it.
-void mkbootimg_dummy(boot_img_hdr*);
diff --git a/mkbootimg/include/bootimg/bootimg.h b/mkbootimg/include/bootimg/bootimg.h
deleted file mode 100644
index 9ee7869..0000000
--- a/mkbootimg/include/bootimg/bootimg.h
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <stdint.h>
-
-#define BOOT_MAGIC "ANDROID!"
-#define BOOT_MAGIC_SIZE 8
-#define BOOT_NAME_SIZE 16
-#define BOOT_ARGS_SIZE 512
-#define BOOT_EXTRA_ARGS_SIZE 1024
-
-// The bootloader expects the structure of boot_img_hdr with header
-// version 0 to be as follows:
-struct boot_img_hdr_v0 {
-    // Must be BOOT_MAGIC.
-    uint8_t magic[BOOT_MAGIC_SIZE];
-
-    uint32_t kernel_size; /* size in bytes */
-    uint32_t kernel_addr; /* physical load addr */
-
-    uint32_t ramdisk_size; /* size in bytes */
-    uint32_t ramdisk_addr; /* physical load addr */
-
-    uint32_t second_size; /* size in bytes */
-    uint32_t second_addr; /* physical load addr */
-
-    uint32_t tags_addr; /* physical addr for kernel tags */
-    uint32_t page_size; /* flash page size we assume */
-
-    // Version of the boot image header.
-    uint32_t header_version;
-
-    // Operating system version and security patch level.
-    // For version "A.B.C" and patch level "Y-M-D":
-    //   (7 bits for each of A, B, C; 7 bits for (Y-2000), 4 bits for M)
-    //   os_version = A[31:25] B[24:18] C[17:11] (Y-2000)[10:4] M[3:0]
-    uint32_t os_version;
-
-#if __cplusplus
-    void SetOsVersion(unsigned major, unsigned minor, unsigned patch) {
-        os_version &= ((1 << 11) - 1);
-        os_version |= (((major & 0x7f) << 25) | ((minor & 0x7f) << 18) | ((patch & 0x7f) << 11));
-    }
-
-    void SetOsPatchLevel(unsigned year, unsigned month) {
-        os_version &= ~((1 << 11) - 1);
-        os_version |= (((year - 2000) & 0x7f) << 4) | ((month & 0xf) << 0);
-    }
-#endif
-
-    uint8_t name[BOOT_NAME_SIZE]; /* asciiz product name */
-
-    uint8_t cmdline[BOOT_ARGS_SIZE];
-
-    uint32_t id[8]; /* timestamp / checksum / sha1 / etc */
-
-    // Supplemental command line data; kept here to maintain
-    // binary compatibility with older versions of mkbootimg.
-    uint8_t extra_cmdline[BOOT_EXTRA_ARGS_SIZE];
-} __attribute__((packed));
-
-/*
- * It is expected that callers would explicitly specify which version of the
- * boot image header they need to use.
- */
-typedef struct boot_img_hdr_v0 boot_img_hdr;
-
-/* When a boot header is of version 0, the structure of boot image is as
- * follows:
- *
- * +-----------------+
- * | boot header     | 1 page
- * +-----------------+
- * | kernel          | n pages
- * +-----------------+
- * | ramdisk         | m pages
- * +-----------------+
- * | second stage    | o pages
- * +-----------------+
- *
- * n = (kernel_size + page_size - 1) / page_size
- * m = (ramdisk_size + page_size - 1) / page_size
- * o = (second_size + page_size - 1) / page_size
- *
- * 0. all entities are page_size aligned in flash
- * 1. kernel and ramdisk are required (size != 0)
- * 2. second is optional (second_size == 0 -> no second)
- * 3. load each element (kernel, ramdisk, second) at
- *    the specified physical address (kernel_addr, etc)
- * 4. prepare tags at tag_addr.  kernel_args[] is
- *    appended to the kernel commandline in the tags.
- * 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
- * 6. if second_size != 0: jump to second_addr
- *    else: jump to kernel_addr
- */
-
-struct boot_img_hdr_v1 : public boot_img_hdr_v0 {
-    uint32_t recovery_dtbo_size;   /* size in bytes for recovery DTBO/ACPIO image */
-    uint64_t recovery_dtbo_offset; /* offset to recovery dtbo/acpio in boot image */
-    uint32_t header_size;
-} __attribute__((packed));
-
-/* When the boot image header has a version of 2, the structure of the boot
- * image is as follows:
- *
- * +---------------------+
- * | boot header         | 1 page
- * +---------------------+
- * | kernel              | n pages
- * +---------------------+
- * | ramdisk             | m pages
- * +---------------------+
- * | second stage        | o pages
- * +---------------------+
- * | recovery dtbo/acpio | p pages
- * +---------------------+
- * | dtb                 | q pages
- * +---------------------+
-
- * n = (kernel_size + page_size - 1) / page_size
- * m = (ramdisk_size + page_size - 1) / page_size
- * o = (second_size + page_size - 1) / page_size
- * p = (recovery_dtbo_size + page_size - 1) / page_size
- * q = (dtb_size + page_size - 1) / page_size
- *
- * 0. all entities are page_size aligned in flash
- * 1. kernel, ramdisk and DTB are required (size != 0)
- * 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B
- *    devices(recovery_dtbo_size != 0)
- * 3. second is optional (second_size == 0 -> no second)
- * 4. load each element (kernel, ramdisk, second, dtb) at
- *    the specified physical address (kernel_addr, etc)
- * 5. If booting to recovery mode in a non-A/B device, extract recovery
- *    dtbo/acpio and apply the correct set of overlays on the base device tree
- *    depending on the hardware/product revision.
- * 6. prepare tags at tag_addr.  kernel_args[] is
- *    appended to the kernel commandline in the tags.
- * 7. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
- * 8. if second_size != 0: jump to second_addr
- *    else: jump to kernel_addr
- */
-struct boot_img_hdr_v2 : public boot_img_hdr_v1 {
-    uint32_t dtb_size; /* size in bytes for DTB image */
-    uint64_t dtb_addr; /* physical load address for DTB image */
-} __attribute__((packed));
diff --git a/mkbootimg/mkbootimg.py b/mkbootimg/mkbootimg.py
deleted file mode 100644
index 934f28e..0000000
--- a/mkbootimg/mkbootimg.py
+++ /dev/null
@@ -1,236 +0,0 @@
-#!/usr/bin/env python
-# Copyright 2015, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-from __future__ import print_function
-from sys import argv, exit, stderr
-from argparse import ArgumentParser, FileType, Action
-from os import fstat
-from struct import pack
-from hashlib import sha1
-import sys
-import re
-
-def filesize(f):
-    if f is None:
-        return 0
-    try:
-        return fstat(f.fileno()).st_size
-    except OSError:
-        return 0
-
-
-def update_sha(sha, f):
-    if f:
-        sha.update(f.read())
-        f.seek(0)
-        sha.update(pack('I', filesize(f)))
-    else:
-        sha.update(pack('I', 0))
-
-
-def pad_file(f, padding):
-    pad = (padding - (f.tell() & (padding - 1))) & (padding - 1)
-    f.write(pack(str(pad) + 'x'))
-
-
-def get_number_of_pages(image_size, page_size):
-    """calculates the number of pages required for the image"""
-    return (image_size + page_size - 1) / page_size
-
-
-def get_recovery_dtbo_offset(args):
-    """calculates the offset of recovery_dtbo image in the boot image"""
-    num_header_pages = 1 # header occupies a page
-    num_kernel_pages = get_number_of_pages(filesize(args.kernel), args.pagesize)
-    num_ramdisk_pages = get_number_of_pages(filesize(args.ramdisk), args.pagesize)
-    num_second_pages = get_number_of_pages(filesize(args.second), args.pagesize)
-    dtbo_offset = args.pagesize * (num_header_pages + num_kernel_pages +
-                                   num_ramdisk_pages + num_second_pages)
-    return dtbo_offset
-
-
-def write_header(args):
-    BOOT_IMAGE_HEADER_V1_SIZE = 1648
-    BOOT_IMAGE_HEADER_V2_SIZE = 1660
-    BOOT_MAGIC = 'ANDROID!'.encode()
-
-    if (args.header_version > 2):
-        raise ValueError('Boot header version %d not supported' % args.header_version)
-
-    args.output.write(pack('8s', BOOT_MAGIC))
-    args.output.write(pack('10I',
-        filesize(args.kernel),                          # size in bytes
-        args.base + args.kernel_offset,                 # physical load addr
-        filesize(args.ramdisk),                         # size in bytes
-        args.base + args.ramdisk_offset,                # physical load addr
-        filesize(args.second),                          # size in bytes
-        args.base + args.second_offset,                 # physical load addr
-        args.base + args.tags_offset,                   # physical addr for kernel tags
-        args.pagesize,                                  # flash page size we assume
-        args.header_version,                            # version of bootimage header
-        (args.os_version << 11) | args.os_patch_level)) # os version and patch level
-    args.output.write(pack('16s', args.board.encode())) # asciiz product name
-    args.output.write(pack('512s', args.cmdline[:512].encode()))
-
-    sha = sha1()
-    update_sha(sha, args.kernel)
-    update_sha(sha, args.ramdisk)
-    update_sha(sha, args.second)
-
-    if args.header_version > 0:
-        update_sha(sha, args.recovery_dtbo)
-    if args.header_version > 1:
-        update_sha(sha, args.dtb)
-
-    img_id = pack('32s', sha.digest())
-
-    args.output.write(img_id)
-    args.output.write(pack('1024s', args.cmdline[512:].encode()))
-
-    if args.header_version > 0:
-        args.output.write(pack('I', filesize(args.recovery_dtbo)))   # size in bytes
-        if args.recovery_dtbo:
-            args.output.write(pack('Q', get_recovery_dtbo_offset(args))) # recovery dtbo offset
-        else:
-            args.output.write(pack('Q', 0)) # Will be set to 0 for devices without a recovery dtbo
-
-    # Populate boot image header size for header versions 1 and 2.
-    if args.header_version == 1:
-        args.output.write(pack('I', BOOT_IMAGE_HEADER_V1_SIZE))
-    elif args.header_version == 2:
-        args.output.write(pack('I', BOOT_IMAGE_HEADER_V2_SIZE))
-
-    if args.header_version > 1:
-
-        if filesize(args.dtb) == 0:
-            raise ValueError("DTB image must not be empty.")
-
-        args.output.write(pack('I', filesize(args.dtb)))   # size in bytes
-        args.output.write(pack('Q', args.base + args.dtb_offset)) # dtb physical load address
-    pad_file(args.output, args.pagesize)
-    return img_id
-
-
-class ValidateStrLenAction(Action):
-    def __init__(self, option_strings, dest, nargs=None, **kwargs):
-        if 'maxlen' not in kwargs:
-            raise ValueError('maxlen must be set')
-        self.maxlen = int(kwargs['maxlen'])
-        del kwargs['maxlen']
-        super(ValidateStrLenAction, self).__init__(option_strings, dest, **kwargs)
-
-    def __call__(self, parser, namespace, values, option_string=None):
-        if len(values) > self.maxlen:
-            raise ValueError('String argument too long: max {0:d}, got {1:d}'.
-                format(self.maxlen, len(values)))
-        setattr(namespace, self.dest, values)
-
-
-def write_padded_file(f_out, f_in, padding):
-    if f_in is None:
-        return
-    f_out.write(f_in.read())
-    pad_file(f_out, padding)
-
-
-def parse_int(x):
-    return int(x, 0)
-
-def parse_os_version(x):
-    match = re.search(r'^(\d{1,3})(?:\.(\d{1,3})(?:\.(\d{1,3}))?)?', x)
-    if match:
-        a = int(match.group(1))
-        b = c = 0
-        if match.lastindex >= 2:
-            b = int(match.group(2))
-        if match.lastindex == 3:
-            c = int(match.group(3))
-        # 7 bits allocated for each field
-        assert a < 128
-        assert b < 128
-        assert c < 128
-        return (a << 14) | (b << 7) | c
-    return 0
-
-def parse_os_patch_level(x):
-    match = re.search(r'^(\d{4})-(\d{2})-(\d{2})', x)
-    if match:
-        y = int(match.group(1)) - 2000
-        m = int(match.group(2))
-        # 7 bits allocated for the year, 4 bits for the month
-        assert y >= 0 and y < 128
-        assert m > 0 and m <= 12
-        return (y << 4) | m
-    return 0
-
-def parse_cmdline():
-    parser = ArgumentParser()
-    parser.add_argument('--kernel', help='path to the kernel', type=FileType('rb'),
-                        required=True)
-    parser.add_argument('--ramdisk', help='path to the ramdisk', type=FileType('rb'))
-    parser.add_argument('--second', help='path to the 2nd bootloader', type=FileType('rb'))
-    parser.add_argument('--dtb', help='path to dtb', type=FileType('rb'))
-    recovery_dtbo_group = parser.add_mutually_exclusive_group()
-    recovery_dtbo_group.add_argument('--recovery_dtbo', help='path to the recovery DTBO', type=FileType('rb'))
-    recovery_dtbo_group.add_argument('--recovery_acpio', help='path to the recovery ACPIO',
-                                     type=FileType('rb'), metavar='RECOVERY_ACPIO', dest='recovery_dtbo')
-    parser.add_argument('--cmdline', help='extra arguments to be passed on the '
-                        'kernel command line', default='', action=ValidateStrLenAction, maxlen=1536)
-    parser.add_argument('--base', help='base address', type=parse_int, default=0x10000000)
-    parser.add_argument('--kernel_offset', help='kernel offset', type=parse_int, default=0x00008000)
-    parser.add_argument('--ramdisk_offset', help='ramdisk offset', type=parse_int, default=0x01000000)
-    parser.add_argument('--second_offset', help='2nd bootloader offset', type=parse_int,
-                        default=0x00f00000)
-    parser.add_argument('--dtb_offset', help='dtb offset', type=parse_int, default=0x01f00000)
-
-    parser.add_argument('--os_version', help='operating system version', type=parse_os_version,
-                        default=0)
-    parser.add_argument('--os_patch_level', help='operating system patch level',
-                        type=parse_os_patch_level, default=0)
-    parser.add_argument('--tags_offset', help='tags offset', type=parse_int, default=0x00000100)
-    parser.add_argument('--board', help='board name', default='', action=ValidateStrLenAction,
-                        maxlen=16)
-    parser.add_argument('--pagesize', help='page size', type=parse_int,
-                        choices=[2**i for i in range(11,15)], default=2048)
-    parser.add_argument('--id', help='print the image ID on standard output',
-                        action='store_true')
-    parser.add_argument('--header_version', help='boot image header version', type=parse_int, default=0)
-    parser.add_argument('-o', '--output', help='output file name', type=FileType('wb'),
-                        required=True)
-    return parser.parse_args()
-
-
-def write_data(args):
-    write_padded_file(args.output, args.kernel, args.pagesize)
-    write_padded_file(args.output, args.ramdisk, args.pagesize)
-    write_padded_file(args.output, args.second, args.pagesize)
-
-    if args.header_version > 0:
-        write_padded_file(args.output, args.recovery_dtbo, args.pagesize)
-    if args.header_version > 1:
-        write_padded_file(args.output, args.dtb, args.pagesize)
-
-def main():
-    args = parse_cmdline()
-    img_id = write_header(args)
-    write_data(args)
-    if args.id:
-        if isinstance(img_id, str):
-            # Python 2's struct.pack returns a string, but py3 returns bytes.
-            img_id = [ord(x) for x in img_id]
-        print('0x' + ''.join('{:02x}'.format(c) for c in img_id))
-
-if __name__ == '__main__':
-    main()
diff --git a/mkbootimg/unpack_bootimg.py b/mkbootimg/unpack_bootimg.py
deleted file mode 100755
index 789bf5e..0000000
--- a/mkbootimg/unpack_bootimg.py
+++ /dev/null
@@ -1,152 +0,0 @@
-#!/usr/bin/env python
-# Copyright 2018, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""unpacks the bootimage.
-
-Extracts the kernel, ramdisk, second bootloader, dtb and recovery dtbo images.
-"""
-
-from __future__ import print_function
-from argparse import ArgumentParser, FileType
-from struct import unpack
-import os
-
-
-def create_out_dir(dir_path):
-    """creates a directory 'dir_path' if it does not exist"""
-    if not os.path.exists(dir_path):
-        os.makedirs(dir_path)
-
-
-def extract_image(offset, size, bootimage, extracted_image_name):
-    """extracts an image from the bootimage"""
-    bootimage.seek(offset)
-    with open(extracted_image_name, 'wb') as file_out:
-        file_out.write(bootimage.read(size))
-
-
-def get_number_of_pages(image_size, page_size):
-    """calculates the number of pages required for the image"""
-    return (image_size + page_size - 1) / page_size
-
-
-def unpack_bootimage(args):
-    """extracts kernel, ramdisk, second bootloader and recovery dtbo"""
-    boot_magic = unpack('8s', args.boot_img.read(8))
-    print('boot_magic: %s' % boot_magic)
-    kernel_ramdisk_second_info = unpack('10I', args.boot_img.read(10 * 4))
-    print('kernel_size: %s' % kernel_ramdisk_second_info[0])
-    print('kernel load address: %#x' % kernel_ramdisk_second_info[1])
-    print('ramdisk size: %s' % kernel_ramdisk_second_info[2])
-    print('ramdisk load address: %#x' % kernel_ramdisk_second_info[3])
-    print('second bootloader size: %s' % kernel_ramdisk_second_info[4])
-    print('second bootloader load address: %#x' % kernel_ramdisk_second_info[5])
-    print('kernel tags load address: %#x' % kernel_ramdisk_second_info[6])
-    print('page size: %s' % kernel_ramdisk_second_info[7])
-    print('boot image header version: %s' % kernel_ramdisk_second_info[8])
-    print('os version and patch level: %s' % kernel_ramdisk_second_info[9])
-
-    product_name = unpack('16s', args.boot_img.read(16))
-    print('product name: %s' % product_name)
-    cmdline = unpack('512s', args.boot_img.read(512))
-    print('command line args: %s' % cmdline)
-
-    args.boot_img.read(32)  # ignore SHA
-
-    extra_cmdline = unpack('1024s', args.boot_img.read(1024))
-    print('additional command line args: %s' % extra_cmdline)
-
-    kernel_size = kernel_ramdisk_second_info[0]
-    ramdisk_size = kernel_ramdisk_second_info[2]
-    second_size = kernel_ramdisk_second_info[4]
-    page_size = kernel_ramdisk_second_info[7]
-    version = kernel_ramdisk_second_info[8]
-    if version > 0:
-        recovery_dtbo_size = unpack('I', args.boot_img.read(1 * 4))[0]
-        print('recovery dtbo size: %s' % recovery_dtbo_size)
-        recovery_dtbo_offset = unpack('Q', args.boot_img.read(8))[0]
-        print('recovery dtbo offset: %#x' % recovery_dtbo_offset)
-        boot_header_size = unpack('I', args.boot_img.read(4))[0]
-        print('boot header size: %s' % boot_header_size)
-    else:
-        recovery_dtbo_size = 0
-    if version > 1:
-        dtb_size = unpack('I', args.boot_img.read(4))[0]
-        print('dtb size: %s' % dtb_size)
-        dtb_load_address = unpack('Q', args.boot_img.read(8))[0]
-        print('dtb address: %#x' % dtb_load_address)
-    else:
-        dtb_size = 0
-
-
-    # The first page contains the boot header
-    num_header_pages = 1
-
-    num_kernel_pages = get_number_of_pages(kernel_size, page_size)
-    kernel_offset = page_size * num_header_pages  # header occupies a page
-    image_info_list = [(kernel_offset, kernel_size, 'kernel')]
-
-    num_ramdisk_pages = get_number_of_pages(ramdisk_size, page_size)
-    ramdisk_offset = page_size * (num_header_pages + num_kernel_pages
-                                 ) # header + kernel
-    image_info_list.append((ramdisk_offset, ramdisk_size, 'ramdisk'))
-
-    if second_size > 0:
-        second_offset = page_size * (
-                num_header_pages + num_kernel_pages + num_ramdisk_pages
-                )  # header + kernel + ramdisk
-        image_info_list.append((second_offset, second_size, 'second'))
-
-    if recovery_dtbo_size > 0:
-        image_info_list.append((recovery_dtbo_offset, recovery_dtbo_size,
-                                'recovery_dtbo'))
-    if dtb_size > 0:
-        num_second_pages = get_number_of_pages(second_size, page_size)
-        num_recovery_dtbo_pages = get_number_of_pages(recovery_dtbo_size, page_size)
-        dtb_offset = page_size * (
-            num_header_pages + num_kernel_pages + num_ramdisk_pages + num_second_pages +
-            num_recovery_dtbo_pages
-        )
-
-        image_info_list.append((dtb_offset, dtb_size, 'dtb'))
-
-    for image_info in image_info_list:
-        extract_image(image_info[0], image_info[1], args.boot_img,
-                      os.path.join(args.out, image_info[2]))
-
-
-def parse_cmdline():
-    """parse command line arguments"""
-    parser = ArgumentParser(
-        description='Unpacks boot.img/recovery.img, extracts the kernel,'
-        'ramdisk, second bootloader, recovery dtbo and dtb')
-    parser.add_argument(
-        '--boot_img',
-        help='path to boot image',
-        type=FileType('rb'),
-        required=True)
-    parser.add_argument('--out', help='path to out binaries', default='out')
-    return parser.parse_args()
-
-
-def main():
-    """parse arguments and unpack boot image"""
-    args = parse_cmdline()
-    create_out_dir(args.out)
-    unpack_bootimage(args)
-
-
-if __name__ == '__main__':
-    main()
diff --git a/rootdir/etc/ld.config.txt b/rootdir/etc/ld.config.txt
index 3b3f61e..b1616d3 100644
--- a/rootdir/etc/ld.config.txt
+++ b/rootdir/etc/ld.config.txt
@@ -187,6 +187,7 @@
 namespace.media.links = default
 namespace.media.link.default.shared_libs  = %LLNDK_LIBRARIES%
 namespace.media.link.default.shared_libs += libbinder_ndk.so
+namespace.media.link.default.shared_libs += libcgrouprc.so
 namespace.media.link.default.shared_libs += libmediametrics.so
 namespace.media.link.default.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
 
@@ -616,6 +617,7 @@
 
 # TODO(b/122876336): Remove libpac.so once it's migrated to Webview
 namespace.default.link.runtime.shared_libs += libpac.so
+namespace.default.link.runtime.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
 
 namespace.default.link.resolv.shared_libs = libnetd_resolv.so
 
diff --git a/rootdir/etc/ld.config.vndk_lite.txt b/rootdir/etc/ld.config.vndk_lite.txt
index 0880be0..9212408 100644
--- a/rootdir/etc/ld.config.vndk_lite.txt
+++ b/rootdir/etc/ld.config.vndk_lite.txt
@@ -428,6 +428,7 @@
 
 # TODO(b/122876336): Remove libpac.so once it's migrated to Webview
 namespace.default.link.runtime.shared_libs += libpac.so
+namespace.default.link.runtime.shared_libs += %SANITIZER_RUNTIME_LIBRARIES%
 
 namespace.default.link.resolv.shared_libs = libnetd_resolv.so
 
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 7cb0f66..3acf301 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -345,8 +345,11 @@
     trigger early-boot
     trigger boot
 
-on post-fs
+on early-fs
+    # Once metadata has been mounted, we'll need vold to deal with userdata checkpointing
     start vold
+
+on post-fs
     exec - system system -- /system/bin/vdc checkpoint markBootAttempt
 
     # Once everything is setup, no need to modify /.
@@ -442,6 +445,7 @@
     mkdir /data/apex 0750 root system
     mkdir /data/apex/active 0750 root system
     mkdir /data/apex/backup 0700 root system
+    mkdir /data/apex/hashtree 0700 root system
     mkdir /data/apex/sessions 0700 root system
     mkdir /data/app-staging 0750 system system
     start apexd
diff --git a/usbd/usbd.cpp b/usbd/usbd.cpp
index 191fb92..6e24d8e 100644
--- a/usbd/usbd.cpp
+++ b/usbd/usbd.cpp
@@ -24,8 +24,6 @@
 
 #include <hidl/HidlTransportSupport.h>
 
-#define PERSISTENT_USB_CONFIG "persist.sys.usb.config"
-
 using android::base::GetProperty;
 using android::base::SetProperty;
 using android::hardware::configureRpcThreadpool;
@@ -34,14 +32,15 @@
 using android::hardware::Return;
 
 int main(int /*argc*/, char** /*argv*/) {
-    configureRpcThreadpool(1, true /*callerWillJoin*/);
+    if (GetProperty("ro.bootmode", "") == "charger") exit(0);
 
+    configureRpcThreadpool(1, true /*callerWillJoin*/);
     android::sp<IUsbGadget> gadget = IUsbGadget::getService();
     Return<void> ret;
 
     if (gadget != nullptr) {
         LOG(INFO) << "Usb HAL found.";
-        std::string function = GetProperty(PERSISTENT_USB_CONFIG, "");
+        std::string function = GetProperty("persist.sys.usb.config", "");
         if (function == "adb") {
             LOG(INFO) << "peristent prop is adb";
             SetProperty("ctl.start", "adbd");