1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "crazy_linker_library_view.h"
6 
7 #include <dlfcn.h>
8 #include "crazy_linker_debug.h"
9 #include "crazy_linker_globals.h"
10 #include "crazy_linker_shared_library.h"
11 
12 namespace crazy {
13 
~LibraryView()14 LibraryView::~LibraryView() {
15   LOG("%s: Destroying %s\n", __FUNCTION__, name_.c_str());
16   if (type_ == TYPE_SYSTEM) {
17     ::dlclose(system_);
18     system_ = NULL;
19   }
20   if (type_ == TYPE_CRAZY) {
21     delete crazy_;
22     crazy_ = NULL;
23   }
24   type_ = TYPE_NONE;
25 }
26 
LookupSymbol(const char * symbol_name)27 void* LibraryView::LookupSymbol(const char* symbol_name) {
28   if (type_ == TYPE_SYSTEM)
29     return ::dlsym(system_, symbol_name);
30 
31   if (type_ == TYPE_CRAZY) {
32     LibraryList* lib_list = Globals::GetLibraries();
33     return lib_list->FindSymbolFrom(symbol_name, this);
34   }
35 
36   return NULL;
37 }
38 
GetInfo(size_t * load_address,size_t * load_size,size_t * relro_start,size_t * relro_size,Error * error)39 bool LibraryView::GetInfo(size_t* load_address,
40                           size_t* load_size,
41                           size_t* relro_start,
42                           size_t* relro_size,
43                           Error* error) {
44   if (type_ != TYPE_CRAZY) {
45     *error = "No RELRO sharing with system libraries";
46     return false;
47   }
48 
49   crazy_->GetInfo(load_address, load_size, relro_start, relro_size);
50   return true;
51 }
52 
53 }  // namespace crazy
54