linker: add more context to link failure error.

This change makes it easier to diagnose mistakes in linker
configuration that result in a library being accidentally loaded in
multiple namespaces without its dependencies available everywhere.

Test: manually tested the error message
Test: bionic-unit-tests
Change-Id: I03a20507f8fc902c2445a7fbbf59767ffffd5ebf
diff --git a/linker/linker.cpp b/linker/linker.cpp
index eedce70..a2ac7c8 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1503,7 +1503,16 @@
   // Open the file.
   int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
   if (fd == -1) {
-    DL_OPEN_ERR("library \"%s\" not found", name);
+    if (task->is_dt_needed()) {
+      if (needed_by->is_main_executable()) {
+        DL_OPEN_ERR("library \"%s\" not found: needed by main executable", name);
+      } else {
+        DL_OPEN_ERR("library \"%s\" not found: needed by %s in namespace %s", name,
+                    needed_by->get_realpath(), task->get_start_from()->get_name());
+      }
+    } else {
+      DL_OPEN_ERR("library \"%s\" not found", name);
+    }
     return false;
   }