loader: fix d-tor call order

In the case when there are multiple dependencies on
the same library in the local_group the unload may
in some situations (covered now by tests) result
calling d-tors for some libraries prematurely.

In order to have correct call order loader checks if this
is last dependency in local group before adding it to BFS
queue.

Bug: http://b/35201832
Test: bionic-unit-tests --gtest_filter=dl*:Dl*
Test: bionic-unit-tests-glibc --gtest_filter=dl*
Change-Id: I4c6955b9032acc7147a51d9f09b61d9e0818700c
diff --git a/linker/linked_list.h b/linker/linked_list.h
index 092e831..b0dd4ce 100644
--- a/linker/linked_list.h
+++ b/linker/linked_list.h
@@ -136,6 +136,10 @@
     tail_ = nullptr;
   }
 
+  bool empty() {
+    return (head_ == nullptr);
+  }
+
   template<typename F>
   void for_each(F action) const {
     visit([&] (T* si) {
@@ -179,6 +183,12 @@
     }
   }
 
+  void remove(T* element) {
+    remove_if([&](T* e) {
+      return e == element;
+    });
+  }
+
   template<typename F>
   T* find_if(F predicate) const {
     for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {