From 4151ea73b75e274d1ff80b42d9d457a783208516 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivanov Date: Thu, 24 Jul 2014 15:33:25 -0700 Subject: [PATCH] Fix global variable initialization for linker Linker now calls init functions for itself. Change-Id: Ibd099812493041ac70f591e3f379ee742b4683b8 --- linker/linker.cpp | 9 +++------ linker/linker_allocator.cpp | 9 ++------- linker/linker_allocator.h | 6 ++---- linker/tests/linker_allocator_test.cpp | 4 ---- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/linker/linker.cpp b/linker/linker.cpp index 04ffe5996..4c765944a 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -2077,12 +2077,6 @@ static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW( ldpreload_env = linker_env_get("LD_PRELOAD"); } - // Linker does not call constructors for its own - // global variables so we need to initialize - // the allocators explicitly. - g_soinfo_allocator.init(); - g_soinfo_links_allocator.init(); - INFO("[ android linker & debugger ]"); soinfo* si = soinfo_alloc(args.argv[0], NULL); @@ -2271,6 +2265,9 @@ extern "C" ElfW(Addr) __linker_init(void* raw_args) { _exit(EXIT_FAILURE); } + // lets properly initialize global variables + linker_so.CallConstructors(); + // We have successfully fixed our own relocations. It's safe to run // the main part of the linker now. args.abort_message_ptr = &g_abort_message; diff --git a/linker/linker_allocator.cpp b/linker/linker_allocator.cpp index c8b97b121..f5d3745b4 100644 --- a/linker/linker_allocator.cpp +++ b/linker/linker_allocator.cpp @@ -28,17 +28,12 @@ struct FreeBlockInfo { size_t num_free_blocks; }; -LinkerBlockAllocator::LinkerBlockAllocator() - : block_size_(0), +LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size) + : block_size_(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size), page_list_(nullptr), free_block_list_(nullptr) {} -void LinkerBlockAllocator::init(size_t block_size) { - block_size_ = block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size; -} - - void* LinkerBlockAllocator::alloc() { if (free_block_list_ == nullptr) { create_new_page(); diff --git a/linker/linker_allocator.h b/linker/linker_allocator.h index fbf58fec3..5d3563fbd 100644 --- a/linker/linker_allocator.h +++ b/linker/linker_allocator.h @@ -32,9 +32,8 @@ struct LinkerAllocatorPage; */ class LinkerBlockAllocator { public: - LinkerBlockAllocator(); + explicit LinkerBlockAllocator(size_t block_size); - void init(size_t block_size); void* alloc(); void free(void* block); void protect_all(int prot); @@ -60,8 +59,7 @@ class LinkerBlockAllocator { template class LinkerAllocator { public: - LinkerAllocator() : block_allocator_() {} - void init() { block_allocator_.init(sizeof(T)); } + LinkerAllocator() : block_allocator_(sizeof(T)) {} T* alloc() { return reinterpret_cast(block_allocator_.alloc()); } void free(T* t) { block_allocator_.free(t); } void protect_all(int prot) { block_allocator_.protect_all(prot); } diff --git a/linker/tests/linker_allocator_test.cpp b/linker/tests/linker_allocator_test.cpp index 0ed82528f..9292a054d 100644 --- a/linker/tests/linker_allocator_test.cpp +++ b/linker/tests/linker_allocator_test.cpp @@ -50,7 +50,6 @@ static size_t kPageSize = sysconf(_SC_PAGE_SIZE); TEST(linker_allocator, test_nominal) { LinkerAllocator allocator; - allocator.init(); test_struct_nominal* ptr1 = allocator.alloc(); ASSERT_TRUE(ptr1 != nullptr); @@ -67,7 +66,6 @@ TEST(linker_allocator, test_nominal) { TEST(linker_allocator, test_small) { LinkerAllocator allocator; - allocator.init(); char* ptr1 = reinterpret_cast(allocator.alloc()); char* ptr2 = reinterpret_cast(allocator.alloc()); @@ -79,7 +77,6 @@ TEST(linker_allocator, test_small) { TEST(linker_allocator, test_larger) { LinkerAllocator allocator; - allocator.init(); test_struct_larger* ptr1 = allocator.alloc(); test_struct_larger* ptr2 = allocator.alloc(); @@ -103,7 +100,6 @@ TEST(linker_allocator, test_larger) { static void protect_all() { LinkerAllocator allocator; - allocator.init(); // number of allocs to reach the end of first page size_t n = kPageSize/sizeof(test_struct_larger) - 1;