2006-09-08 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / readsyms.cc
blob1076e6c17ef74fbf6e261a93eae980c063434b7c
1 // readsyms.cc -- read input file symbols for gold
3 #include "gold.h"
5 #include <cstring>
7 #include "elfcpp.h"
8 #include "options.h"
9 #include "dirsearch.h"
10 #include "readsyms.h"
12 namespace gold
15 // Class read_symbols.
17 Read_symbols::~Read_symbols()
19 // The this_blocker_ and next_blocker_ pointers are passed on to the
20 // Add_symbols task.
23 // Return whether a Read_symbols task is runnable. We need write
24 // access to the symbol table. We can read an ordinary input file
25 // immediately. For an archive specified using -l, we have to wait
26 // until the search path is complete.
28 Task::Is_runnable_type
29 Read_symbols::is_runnable(Workqueue*)
31 if (this->input_.is_lib() && this->dirpath_.token().is_blocked())
32 return IS_BLOCKED;
34 return IS_RUNNABLE;
37 // Return a Task_locker for a Read_symbols task. We don't need any
38 // locks here.
40 Task_locker*
41 Read_symbols::locks(Workqueue*)
43 return NULL;
46 // Run a Read_symbols task. This is where we actually read the
47 // symbols and relocations.
49 void
50 Read_symbols::run(Workqueue* workqueue)
52 // We don't keep track of Input_file objects, so this is a memory
53 // leak.
54 Input_file* input_file = new Input_file(this->input_);
55 input_file->open(this->options_, this->dirpath_);
57 // Read enough of the file to pick up the entire ELF header.
59 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
60 off_t bytes;
61 const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
62 if (bytes >= 4)
64 static unsigned char elfmagic[4] =
66 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
67 elfcpp::ELFMAG2, elfcpp::ELFMAG3
69 if (memcmp(p, elfmagic, 4) == 0)
71 // This is an ELF object.
72 Object* obj = make_elf_object(this->input_.name(), input_file, 0,
73 p, bytes);
75 Read_symbols_data sd = obj->read_symbols();
76 workqueue->queue(new Add_symbols(this->symtab_, obj, sd,
77 this->this_blocker_,
78 this->next_blocker_));
80 // Opening the file locked it, so now we need to unlock it.
81 input_file->file().unlock();
83 return;
87 // Here we have to handle archives and any other input file
88 // types we need.
89 gold_fatal("only objects are currently supported", false);
92 // Class Add_symbols.
94 Add_symbols::~Add_symbols()
96 if (this->this_blocker_ != NULL)
97 delete this->this_blocker_;
98 // next_blocker_ is deleted by the task associated with the next
99 // input file.
102 // We are blocked by this_blocker_. We block next_blocker_.
104 Task::Is_runnable_type
105 Add_symbols::is_runnable(Workqueue*)
107 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
108 return IS_BLOCKED;
109 return IS_RUNNABLE;
112 Task_locker*
113 Add_symbols::locks(Workqueue* workqueue)
115 return new Task_locker_block(*this->next_blocker_, workqueue);
118 void
119 Add_symbols::run(Workqueue*)
121 this->object_->add_symbols(this->symtab_, this->sd_);
124 } // End namespace gold.