* commit '6aac3cd112360631970ff3a7b809d58ed88a10c9': Load libraries in breadth-first order
This commit is contained in:
		
							
								
								
									
										140
									
								
								libc/private/UniquePtr.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								libc/private/UniquePtr.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,140 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2010 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 UNIQUE_PTR_H_included
 | 
			
		||||
#define UNIQUE_PTR_H_included
 | 
			
		||||
 | 
			
		||||
// Default deleter for pointer types.
 | 
			
		||||
template <typename T>
 | 
			
		||||
struct DefaultDelete {
 | 
			
		||||
    enum { type_must_be_complete = sizeof(T) };
 | 
			
		||||
    DefaultDelete() {}
 | 
			
		||||
    void operator()(T* p) const {
 | 
			
		||||
        delete p;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Default deleter for array types.
 | 
			
		||||
template <typename T>
 | 
			
		||||
struct DefaultDelete<T[]> {
 | 
			
		||||
    enum { type_must_be_complete = sizeof(T) };
 | 
			
		||||
    void operator()(T* p) const {
 | 
			
		||||
        delete[] p;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// A smart pointer that deletes the given pointer on destruction.
 | 
			
		||||
// Equivalent to C++0x's std::unique_ptr (a combination of boost::scoped_ptr
 | 
			
		||||
// and boost::scoped_array).
 | 
			
		||||
// Named to be in keeping with Android style but also to avoid
 | 
			
		||||
// collision with any other implementation, until we can switch over
 | 
			
		||||
// to unique_ptr.
 | 
			
		||||
// Use thus:
 | 
			
		||||
//   UniquePtr<C> c(new C);
 | 
			
		||||
template <typename T, typename D = DefaultDelete<T> >
 | 
			
		||||
class UniquePtr {
 | 
			
		||||
public:
 | 
			
		||||
    // Construct a new UniquePtr, taking ownership of the given raw pointer.
 | 
			
		||||
    explicit UniquePtr(T* ptr = nullptr) : mPtr(ptr) { }
 | 
			
		||||
 | 
			
		||||
    UniquePtr(UniquePtr<T, D>&& that) {
 | 
			
		||||
      mPtr = that.mPtr;
 | 
			
		||||
      that.mPtr = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ~UniquePtr() {
 | 
			
		||||
        reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Accessors.
 | 
			
		||||
    T& operator*() const { return *mPtr; }
 | 
			
		||||
    T* operator->() const { return mPtr; }
 | 
			
		||||
    T* get() const { return mPtr; }
 | 
			
		||||
 | 
			
		||||
    // Returns the raw pointer and hands over ownership to the caller.
 | 
			
		||||
    // The pointer will not be deleted by UniquePtr.
 | 
			
		||||
    T* release() __attribute__((warn_unused_result)) {
 | 
			
		||||
        T* result = mPtr;
 | 
			
		||||
        mPtr = nullptr;
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Takes ownership of the given raw pointer.
 | 
			
		||||
    // If this smart pointer previously owned a different raw pointer, that
 | 
			
		||||
    // raw pointer will be freed.
 | 
			
		||||
    void reset(T* ptr = nullptr) {
 | 
			
		||||
        if (ptr != mPtr) {
 | 
			
		||||
            D()(mPtr);
 | 
			
		||||
            mPtr = ptr;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    // The raw pointer.
 | 
			
		||||
    T* mPtr;
 | 
			
		||||
 | 
			
		||||
    // Comparing unique pointers is probably a mistake, since they're unique.
 | 
			
		||||
    template <typename T2> bool operator==(const UniquePtr<T2>& p) const = delete;
 | 
			
		||||
    template <typename T2> bool operator!=(const UniquePtr<T2>& p) const = delete;
 | 
			
		||||
 | 
			
		||||
    // Disallow copy and assignment.
 | 
			
		||||
    UniquePtr(const UniquePtr&) = delete;
 | 
			
		||||
    void operator=(const UniquePtr&) = delete;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Partial specialization for array types. Like std::unique_ptr, this removes
 | 
			
		||||
// operator* and operator-> but adds operator[].
 | 
			
		||||
template <typename T, typename D>
 | 
			
		||||
class UniquePtr<T[], D> {
 | 
			
		||||
public:
 | 
			
		||||
    explicit UniquePtr(T* ptr = NULL) : mPtr(ptr) {
 | 
			
		||||
    }
 | 
			
		||||
    UniquePtr(UniquePtr<T, D>&& that) {
 | 
			
		||||
      mPtr = that.mPtr;
 | 
			
		||||
      that.mPtr = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ~UniquePtr() {
 | 
			
		||||
        reset();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    T& operator[](size_t i) const {
 | 
			
		||||
        return mPtr[i];
 | 
			
		||||
    }
 | 
			
		||||
    T* get() const { return mPtr; }
 | 
			
		||||
 | 
			
		||||
    T* release() __attribute__((warn_unused_result)) {
 | 
			
		||||
        T* result = mPtr;
 | 
			
		||||
        mPtr = NULL;
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void reset(T* ptr = NULL) {
 | 
			
		||||
        if (ptr != mPtr) {
 | 
			
		||||
            D()(mPtr);
 | 
			
		||||
            mPtr = ptr;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    T* mPtr;
 | 
			
		||||
 | 
			
		||||
    // Disallow copy and assignment.
 | 
			
		||||
    UniquePtr(const UniquePtr&) = delete;
 | 
			
		||||
    void operator=(const UniquePtr&) = delete;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif  // UNIQUE_PTR_H_included
 | 
			
		||||
@@ -44,6 +44,8 @@
 | 
			
		||||
#include "private/KernelArgumentBlock.h"
 | 
			
		||||
#include "private/ScopedPthreadMutexLocker.h"
 | 
			
		||||
#include "private/ScopedFd.h"
 | 
			
		||||
#include "private/ScopeGuard.h"
 | 
			
		||||
#include "private/UniquePtr.h"
 | 
			
		||||
 | 
			
		||||
#include "linker.h"
 | 
			
		||||
#include "linker_debug.h"
 | 
			
		||||
@@ -170,7 +172,6 @@ DISALLOW_ALLOCATION(void, free, (void* u __unused));
 | 
			
		||||
DISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
 | 
			
		||||
DISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
 | 
			
		||||
 | 
			
		||||
static char tmp_err_buf[768];
 | 
			
		||||
static char __linker_dl_err_buf[768];
 | 
			
		||||
 | 
			
		||||
char* linker_get_error_buffer() {
 | 
			
		||||
@@ -778,6 +779,22 @@ static void for_each_dt_needed(const soinfo* si, F action) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
 | 
			
		||||
  int fd = -1;
 | 
			
		||||
  ScopedFd file_guard(-1);
 | 
			
		||||
 | 
			
		||||
  if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
 | 
			
		||||
    fd = extinfo->library_fd;
 | 
			
		||||
  } else {
 | 
			
		||||
    // Open the file.
 | 
			
		||||
    fd = open_library(name);
 | 
			
		||||
    if (fd == -1) {
 | 
			
		||||
      DL_ERR("library \"%s\" not found", name);
 | 
			
		||||
      return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
 | 
			
		||||
  int fd = -1;
 | 
			
		||||
  ScopedFd file_guard(-1);
 | 
			
		||||
@@ -795,35 +812,34 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld
 | 
			
		||||
    file_guard.reset(fd);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    ElfReader elf_reader(name, fd);
 | 
			
		||||
  struct stat file_stat;
 | 
			
		||||
  if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
 | 
			
		||||
    DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    struct stat file_stat;
 | 
			
		||||
    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
 | 
			
		||||
      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
 | 
			
		||||
      return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Check for symlink and other situations where
 | 
			
		||||
    // file can have different names.
 | 
			
		||||
    for (soinfo* si = solist; si != nullptr; si = si->next) {
 | 
			
		||||
      if (si->get_st_dev() != 0 &&
 | 
			
		||||
          si->get_st_ino() != 0 &&
 | 
			
		||||
          si->get_st_dev() == file_stat.st_dev &&
 | 
			
		||||
          si->get_st_ino() == file_stat.st_ino) {
 | 
			
		||||
        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
 | 
			
		||||
        return si;
 | 
			
		||||
      }
 | 
			
		||||
  // Check for symlink and other situations where
 | 
			
		||||
  // file can have different names.
 | 
			
		||||
  for (soinfo* si = solist; si != nullptr; si = si->next) {
 | 
			
		||||
    if (si->get_st_dev() != 0 &&
 | 
			
		||||
        si->get_st_ino() != 0 &&
 | 
			
		||||
        si->get_st_dev() == file_stat.st_dev &&
 | 
			
		||||
        si->get_st_ino() == file_stat.st_ino) {
 | 
			
		||||
      TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
 | 
			
		||||
      return si;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if ((rtld_flags & RTLD_NOLOAD) != 0) {
 | 
			
		||||
    DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    // Read the ELF header and load the segments.
 | 
			
		||||
    if (!elf_reader.Load(extinfo)) {
 | 
			
		||||
        return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
  // Read the ELF header and load the segments.
 | 
			
		||||
  ElfReader elf_reader(name, fd);
 | 
			
		||||
  if (!elf_reader.Load(extinfo)) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat, rtld_flags);
 | 
			
		||||
  if (si == nullptr) {
 | 
			
		||||
@@ -835,17 +851,16 @@ static soinfo* load_library(LoadTaskList& load_tasks, const char* name, int rtld
 | 
			
		||||
  si->phnum = elf_reader.phdr_count();
 | 
			
		||||
  si->phdr = elf_reader.loaded_phdr();
 | 
			
		||||
 | 
			
		||||
    // At this point we know that whatever is loaded @ base is a valid ELF
 | 
			
		||||
    // shared library whose segments are properly mapped in.
 | 
			
		||||
    TRACE("[ load_library base=%p size=%zu name='%s' ]",
 | 
			
		||||
          reinterpret_cast<void*>(si->base), si->size, si->name);
 | 
			
		||||
  if (!si->PrelinkImage()) {
 | 
			
		||||
    soinfo_free(si);
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
    if (!si->LinkImage(extinfo)) {
 | 
			
		||||
      soinfo_free(si);
 | 
			
		||||
      return nullptr;
 | 
			
		||||
    }
 | 
			
		||||
  for_each_dt_needed(si, [&] (const char* name) {
 | 
			
		||||
    load_tasks.push_back(LoadTask::create(name, si));
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
    return si;
 | 
			
		||||
  return si;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static soinfo *find_loaded_library_by_name(const char* name) {
 | 
			
		||||
@@ -863,13 +878,23 @@ static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name,
 | 
			
		||||
  soinfo* si = find_loaded_library_by_name(name);
 | 
			
		||||
 | 
			
		||||
  // Library might still be loaded, the accurate detection
 | 
			
		||||
  // of this fact is done by load_library
 | 
			
		||||
  // of this fact is done by load_library.
 | 
			
		||||
  if (si == nullptr) {
 | 
			
		||||
    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
 | 
			
		||||
    si = load_library(load_tasks, name, rtld_flags, extinfo);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (si != nullptr && (si->flags & FLAG_LINKED) == 0) {
 | 
			
		||||
  return si;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void soinfo_unload(soinfo* si);
 | 
			
		||||
 | 
			
		||||
static bool is_recursive(soinfo* si, soinfo* parent) {
 | 
			
		||||
  if (parent == nullptr) {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (si == parent) {
 | 
			
		||||
    DL_ERR("recursive link to \"%s\"", si->name);
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
@@ -945,7 +970,10 @@ static bool find_libraries(const char* const library_names[], size_t library_nam
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return si;
 | 
			
		||||
  // All is well - found_libs and load_tasks are empty at this point
 | 
			
		||||
  // and all libs are successfully linked.
 | 
			
		||||
  failure_guard.disable();
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) {
 | 
			
		||||
@@ -959,6 +987,7 @@ static soinfo* find_library(const char* name, int rtld_flags, const android_dlex
 | 
			
		||||
  if (!find_libraries(&name, 1, &si, nullptr, 0, rtld_flags, extinfo)) {
 | 
			
		||||
    return nullptr;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return si;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -974,20 +1003,17 @@ static void soinfo_unload(soinfo* si) {
 | 
			
		||||
        soinfo_unload(child);
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
 | 
			
		||||
        if (d->d_tag == DT_NEEDED) {
 | 
			
		||||
          const char* library_name = si->strtab + d->d_un.d_val;
 | 
			
		||||
          TRACE("%s needs to unload %s", si->name, library_name);
 | 
			
		||||
          soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
 | 
			
		||||
          if (needed != nullptr) {
 | 
			
		||||
            soinfo_unload(needed);
 | 
			
		||||
          } else {
 | 
			
		||||
            // Not found: for example if symlink was deleted between dlopen and dlclose
 | 
			
		||||
            // Since we cannot really handle errors at this point - print and continue.
 | 
			
		||||
            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
 | 
			
		||||
          }
 | 
			
		||||
      for_each_dt_needed(si, [&] (const char* library_name) {
 | 
			
		||||
        TRACE("deprecated (old format of soinfo): %s needs to unload %s", si->name, library_name);
 | 
			
		||||
        soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
 | 
			
		||||
        if (needed != nullptr) {
 | 
			
		||||
          soinfo_unload(needed);
 | 
			
		||||
        } else {
 | 
			
		||||
          // Not found: for example if symlink was deleted between dlopen and dlclose
 | 
			
		||||
          // Since we cannot really handle errors at this point - print and continue.
 | 
			
		||||
          PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    notify_gdb_of_unload(si);
 | 
			
		||||
@@ -1058,9 +1084,6 @@ static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
 | 
			
		||||
 | 
			
		||||
#if defined(USE_RELA)
 | 
			
		||||
int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
			
		||||
  ElfW(Sym)* s;
 | 
			
		||||
  soinfo* lsi;
 | 
			
		||||
 | 
			
		||||
  for (size_t idx = 0; idx < count; ++idx, ++rela) {
 | 
			
		||||
    unsigned type = ELFW(R_TYPE)(rela->r_info);
 | 
			
		||||
    unsigned sym = ELFW(R_SYM)(rela->r_info);
 | 
			
		||||
@@ -1072,6 +1095,10 @@ int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
			
		||||
    if (type == 0) { // R_*_NONE
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ElfW(Sym)* s = nullptr;
 | 
			
		||||
    soinfo* lsi = nullptr;
 | 
			
		||||
 | 
			
		||||
    if (sym != 0) {
 | 
			
		||||
      sym_name = get_string(symtab[sym].st_name);
 | 
			
		||||
      s = soinfo_do_lookup(this, sym_name, &lsi);
 | 
			
		||||
@@ -1132,8 +1159,6 @@ int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
 | 
			
		||||
        sym_addr = lsi->resolve_symbol_address(s);
 | 
			
		||||
      }
 | 
			
		||||
      count_relocation(kRelocSymbol);
 | 
			
		||||
    } else {
 | 
			
		||||
      s = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch (type) {
 | 
			
		||||
@@ -1669,6 +1694,9 @@ void soinfo::CallConstructors() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void soinfo::CallDestructors() {
 | 
			
		||||
  if (!constructors_called) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  TRACE("\"%s\": calling destructors", name);
 | 
			
		||||
 | 
			
		||||
  // DT_FINI_ARRAY must be parsed in reverse order.
 | 
			
		||||
@@ -1754,7 +1782,7 @@ int soinfo::get_rtld_flags() {
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This is a return on get_children() in case
 | 
			
		||||
// This is a return on get_children()/get_parents() if
 | 
			
		||||
// 'this->flags' does not have FLAG_NEW_SOINFO set.
 | 
			
		||||
static soinfo::soinfo_list_t g_empty_list;
 | 
			
		||||
 | 
			
		||||
@@ -1944,7 +1972,7 @@ bool soinfo::PrelinkImage() {
 | 
			
		||||
        // if the dynamic table is writable
 | 
			
		||||
// FIXME: not working currently for N64
 | 
			
		||||
// The flags for the LOAD and DYNAMIC program headers do not agree.
 | 
			
		||||
// The LOAD section containng the dynamic table has been mapped as
 | 
			
		||||
// The LOAD section containing the dynamic table has been mapped as
 | 
			
		||||
// read-only, but the DYNAMIC header claims it is writable.
 | 
			
		||||
#if !(defined(__mips__) && defined(__LP64__))
 | 
			
		||||
        if ((dynamic_flags & PF_W) != 0) {
 | 
			
		||||
@@ -2123,37 +2151,7 @@ bool soinfo::PrelinkImage() {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
 | 
			
		||||
    if (flags & FLAG_EXE) {
 | 
			
		||||
        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
 | 
			
		||||
        size_t preload_count = 0;
 | 
			
		||||
        for (size_t i = 0; g_ld_preload_names[i] != nullptr; i++) {
 | 
			
		||||
            soinfo* lsi = find_library(g_ld_preload_names[i], 0, nullptr);
 | 
			
		||||
            if (lsi != nullptr) {
 | 
			
		||||
                g_ld_preloads[preload_count++] = lsi;
 | 
			
		||||
            } else {
 | 
			
		||||
                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
 | 
			
		||||
                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
 | 
			
		||||
                        g_ld_preload_names[i], name, linker_get_error_buffer());
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
 | 
			
		||||
        if (d->d_tag == DT_NEEDED) {
 | 
			
		||||
            const char* library_name = strtab + d->d_un.d_val;
 | 
			
		||||
            DEBUG("%s needs %s", name, library_name);
 | 
			
		||||
            soinfo* lsi = find_library(library_name, 0, nullptr);
 | 
			
		||||
            if (lsi == nullptr) {
 | 
			
		||||
                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
 | 
			
		||||
                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
 | 
			
		||||
                       library_name, name, tmp_err_buf);
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            add_child(lsi);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
bool soinfo::LinkImage(const android_dlextinfo* extinfo) {
 | 
			
		||||
 | 
			
		||||
#if !defined(__LP64__)
 | 
			
		||||
  if (has_text_relocations) {
 | 
			
		||||
@@ -2276,6 +2274,7 @@ static void add_vdso(KernelArgumentBlock& args __unused) {
 | 
			
		||||
  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
 | 
			
		||||
  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 | 
			
		||||
 | 
			
		||||
  si->PrelinkImage();
 | 
			
		||||
  si->LinkImage(nullptr);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
@@ -2560,7 +2559,7 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) {
 | 
			
		||||
  linker_so.phnum = elf_hdr->e_phnum;
 | 
			
		||||
  linker_so.flags |= FLAG_LINKER;
 | 
			
		||||
 | 
			
		||||
  if (!linker_so.LinkImage(nullptr)) {
 | 
			
		||||
  if (!(linker_so.PrelinkImage() && linker_so.LinkImage(nullptr))) {
 | 
			
		||||
    // It would be nice to print an error message, but if the linker
 | 
			
		||||
    // can't link itself, there's no guarantee that we'll be able to
 | 
			
		||||
    // call write() (because it involves a GOT reference). We may as
 | 
			
		||||
 
 | 
			
		||||
@@ -206,6 +206,7 @@ struct soinfo {
 | 
			
		||||
  void CallConstructors();
 | 
			
		||||
  void CallDestructors();
 | 
			
		||||
  void CallPreInitConstructors();
 | 
			
		||||
  bool PrelinkImage();
 | 
			
		||||
  bool LinkImage(const android_dlextinfo* extinfo);
 | 
			
		||||
 | 
			
		||||
  void add_child(soinfo* child);
 | 
			
		||||
@@ -223,6 +224,7 @@ struct soinfo {
 | 
			
		||||
  int get_rtld_flags();
 | 
			
		||||
 | 
			
		||||
  soinfo_list_t& get_children();
 | 
			
		||||
  soinfo_list_t& get_parents();
 | 
			
		||||
 | 
			
		||||
  ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -729,8 +729,11 @@ void phdr_table_get_dynamic_section(const ElfW(Phdr)* phdr_table, size_t phdr_co
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  *dynamic = nullptr;
 | 
			
		||||
  if (dynamic_count) {
 | 
			
		||||
    *dynamic_count = 0;
 | 
			
		||||
  for (const ElfW(Phdr)* phdr = phdr_table, *phdr_limit = phdr + phdr_count; phdr < phdr_limit; phdr++) {
 | 
			
		||||
    if (phdr->p_type == PT_DYNAMIC) {
 | 
			
		||||
      *dynamic = reinterpret_cast<ElfW(Dyn)*>(load_bias + phdr->p_vaddr);
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -114,6 +114,7 @@ libBionicStandardTests_src_files := \
 | 
			
		||||
    system_properties_test.cpp \
 | 
			
		||||
    time_test.cpp \
 | 
			
		||||
    uchar_test.cpp \
 | 
			
		||||
    uniqueptr_test.cpp \
 | 
			
		||||
    unistd_test.cpp \
 | 
			
		||||
    wchar_test.cpp \
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -254,6 +254,160 @@ include $(LOCAL_PATH)/Android.build.testlib.mk
 | 
			
		||||
module := libtest_relo_check_dt_needed_order_2
 | 
			
		||||
include $(LOCAL_PATH)/Android.build.testlib.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# Libraries used by dlfcn tests to verify correct load order:
 | 
			
		||||
# libtest_check_order_2_right.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_2_right_src_files := \
 | 
			
		||||
    dlopen_testlib_answer.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_2_right_cflags := -D__ANSWER=42
 | 
			
		||||
module := libtest_check_order_2_right
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order_a.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_a_src_files := \
 | 
			
		||||
    dlopen_testlib_answer.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_a_cflags := -D__ANSWER=1
 | 
			
		||||
module := libtest_check_order_a
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order_b.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_b_src_files := \
 | 
			
		||||
    dlopen_testlib_answer.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_b_cflags := -D__ANSWER=2 -D__ANSWER2=43
 | 
			
		||||
module := libtest_check_order_b
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order_c.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_3_c_src_files := \
 | 
			
		||||
    dlopen_testlib_answer.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_3_c_cflags := -D__ANSWER=3
 | 
			
		||||
module := libtest_check_order_3_c
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order_d.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_d_src_files := \
 | 
			
		||||
   dlopen_testlib_answer.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_d_shared_libraries := libtest_check_order_b
 | 
			
		||||
libtest_check_order_d_cflags := -D__ANSWER=4 -D__ANSWER2=4
 | 
			
		||||
module := libtest_check_order_d
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order_left.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_1_left_src_files := \
 | 
			
		||||
    empty.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_1_left_shared_libraries := libtest_check_order_a libtest_check_order_b
 | 
			
		||||
 | 
			
		||||
module := libtest_check_order_1_left
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_check_order.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_check_order_src_files := \
 | 
			
		||||
    empty.cpp
 | 
			
		||||
 | 
			
		||||
libtest_check_order_shared_libraries := libtest_check_order_1_left \
 | 
			
		||||
  libtest_check_order_2_right libtest_check_order_3_c
 | 
			
		||||
 | 
			
		||||
module := libtest_check_order
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# Library with dependency loop used by dlfcn tests
 | 
			
		||||
#
 | 
			
		||||
# libtest_with_dependency_loop -> a -> b -> c -> a
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_with_dependency_loop_src_files := empty.cpp
 | 
			
		||||
 | 
			
		||||
libtest_with_dependency_loop_shared_libraries := \
 | 
			
		||||
    libtest_with_dependency_loop_a
 | 
			
		||||
 | 
			
		||||
module := libtest_with_dependency_loop
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_with_dependency_loop_a.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_with_dependency_loop_a_src_files := empty.cpp
 | 
			
		||||
 | 
			
		||||
libtest_with_dependency_loop_a_shared_libraries := \
 | 
			
		||||
    libtest_with_dependency_loop_b_tmp
 | 
			
		||||
 | 
			
		||||
module := libtest_with_dependency_loop_a
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_with_dependency_loop_b.so
 | 
			
		||||
#
 | 
			
		||||
# this is temporary placeholder - will be removed
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_with_dependency_loop_b_tmp_src_files := empty.cpp
 | 
			
		||||
libtest_with_dependency_loop_b_tmp_ldflags := -Wl,-soname=libtest_with_dependency_loop_b.so
 | 
			
		||||
 | 
			
		||||
module := libtest_with_dependency_loop_b_tmp
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_with_dependency_loop_b.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_with_dependency_loop_b_src_files := empty.cpp
 | 
			
		||||
libtest_with_dependency_loop_b_shared_libraries := libtest_with_dependency_loop_c
 | 
			
		||||
 | 
			
		||||
module := libtest_with_dependency_loop_b
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_with_dependency_loop_c.so
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
libtest_with_dependency_loop_c_src_files := empty.cpp
 | 
			
		||||
 | 
			
		||||
libtest_with_dependency_loop_c_shared_libraries := \
 | 
			
		||||
    libtest_with_dependency_loop_a
 | 
			
		||||
 | 
			
		||||
module := libtest_with_dependency_loop_c
 | 
			
		||||
build_type := target
 | 
			
		||||
build_target := SHARED_LIBRARY
 | 
			
		||||
include $(TEST_PATH)/Android.build.mk
 | 
			
		||||
 | 
			
		||||
# -----------------------------------------------------------------------------
 | 
			
		||||
# libtest_relo_check_dt_needed_order.so
 | 
			
		||||
# |
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										25
									
								
								tests/libs/dlopen_testlib_answer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								tests/libs/dlopen_testlib_answer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2014 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.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
extern "C" int dlopen_test_get_answer() {
 | 
			
		||||
  return __ANSWER;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef __ANSWER2
 | 
			
		||||
extern "C" int dlopen_test_get_answer2() {
 | 
			
		||||
  return __ANSWER2;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										101
									
								
								tests/uniqueptr_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								tests/uniqueptr_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,101 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2014 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 <gtest/gtest.h>
 | 
			
		||||
 | 
			
		||||
#include <private/UniquePtr.h>
 | 
			
		||||
 | 
			
		||||
static int cCount = 0;
 | 
			
		||||
struct C {
 | 
			
		||||
  C() { ++cCount; }
 | 
			
		||||
  ~C() { --cCount; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static bool freed = false;
 | 
			
		||||
struct Freer {
 | 
			
		||||
  void operator() (int* p) {
 | 
			
		||||
    ASSERT_EQ(123, *p);
 | 
			
		||||
    free(p);
 | 
			
		||||
    freed = true;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
TEST(UniquePtr, smoke) {
 | 
			
		||||
  //
 | 
			
		||||
  // UniquePtr<T> tests...
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  // Can we free a single object?
 | 
			
		||||
  {
 | 
			
		||||
    UniquePtr<C> c(new C);
 | 
			
		||||
    ASSERT_TRUE(cCount == 1);
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 0);
 | 
			
		||||
  // Does release work?
 | 
			
		||||
  C* rawC;
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<C> c(new C);
 | 
			
		||||
      ASSERT_TRUE(cCount == 1);
 | 
			
		||||
      rawC = c.release();
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 1);
 | 
			
		||||
  delete rawC;
 | 
			
		||||
  // Does reset work?
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<C> c(new C);
 | 
			
		||||
      ASSERT_TRUE(cCount == 1);
 | 
			
		||||
      c.reset(new C);
 | 
			
		||||
      ASSERT_TRUE(cCount == 1);
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 0);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // UniquePtr<T[]> tests...
 | 
			
		||||
  //
 | 
			
		||||
 | 
			
		||||
  // Can we free an array?
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<C[]> cs(new C[4]);
 | 
			
		||||
      ASSERT_TRUE(cCount == 4);
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 0);
 | 
			
		||||
  // Does release work?
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<C[]> c(new C[4]);
 | 
			
		||||
      ASSERT_TRUE(cCount == 4);
 | 
			
		||||
      rawC = c.release();
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 4);
 | 
			
		||||
  delete[] rawC;
 | 
			
		||||
  // Does reset work?
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<C[]> c(new C[4]);
 | 
			
		||||
      ASSERT_TRUE(cCount == 4);
 | 
			
		||||
      c.reset(new C[2]);
 | 
			
		||||
      ASSERT_TRUE(cCount == 2);
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(cCount == 0);
 | 
			
		||||
 | 
			
		||||
  //
 | 
			
		||||
  // Custom deleter tests...
 | 
			
		||||
  //
 | 
			
		||||
  ASSERT_TRUE(!freed);
 | 
			
		||||
  {
 | 
			
		||||
      UniquePtr<int, Freer> i(reinterpret_cast<int*>(malloc(sizeof(int))));
 | 
			
		||||
      *i = 123;
 | 
			
		||||
  }
 | 
			
		||||
  ASSERT_TRUE(freed);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user