Do not add linker the _r_debug.r_map on link_image

Bug: http://b/27533895
Change-Id: Idd0fb06a5d9f400c0608ae532cc4c575211810fa
(cherry picked from commit e97d8ed70e44db619de9975a43ed2d1509653d74)
diff --git a/linker/linker.cpp b/linker/linker.cpp
index f80266d..faf5759 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -274,8 +274,8 @@
 }
 
 static void notify_gdb_of_load(soinfo* info) {
-  if (info->is_main_executable()) {
-    // GDB already knows about the main executable
+  if (info->is_linker() || info->is_main_executable()) {
+    // gdb already knows about the linker and the main executable.
     return;
   }
 
@@ -286,15 +286,13 @@
   map->l_name = const_cast<char*>(info->get_realpath());
   map->l_ld = info->dynamic;
 
+  CHECK(map->l_name != nullptr);
+  CHECK(map->l_name[0] != '\0');
+
   notify_gdb_of_load(map);
 }
 
 static void notify_gdb_of_unload(soinfo* info) {
-  if (info->is_main_executable()) {
-    // GDB already knows about the main executable
-    return;
-  }
-
   notify_gdb_of_unload(&(info->link_map_head));
 }
 
@@ -3284,6 +3282,10 @@
   return (flags_ & FLAG_EXE) != 0;
 }
 
+bool soinfo::is_linker() const {
+  return (flags_ & FLAG_LINKER) != 0;
+}
+
 void soinfo::set_linked() {
   flags_ |= FLAG_LINKED;
 }
@@ -4049,10 +4051,12 @@
   si->set_main_executable();
   link_map* map = &(si->link_map_head);
 
+  // Register the main executable and the linker upfront to have
+  // gdb aware of them before loading the rest of the dependency
+  // tree.
   map->l_addr = 0;
   map->l_name = args.argv[0];
   insert_link_map_into_debug_map(map);
-
   init_linker_info_for_gdb(linker_base);
 
   // Extract information passed from the kernel.