More section layout code.
[external/binutils.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6
7 #include "elfcpp.h"
8 #include "options.h"
9 #include "dirsearch.h"
10 #include "readsyms.h"
11 #include "object.h"
12
13 namespace gold
14 {
15
16 // Class read_symbols.
17
18 Read_symbols::~Read_symbols()
19 {
20   // The this_blocker_ and next_blocker_ pointers are passed on to the
21   // Add_symbols task.
22 }
23
24 // Return whether a Read_symbols task is runnable.  We need write
25 // access to the symbol table.  We can read an ordinary input file
26 // immediately.  For an archive specified using -l, we have to wait
27 // until the search path is complete.
28
29 Task::Is_runnable_type
30 Read_symbols::is_runnable(Workqueue*)
31 {
32   if (this->input_.is_lib() && this->dirpath_.token().is_blocked())
33     return IS_BLOCKED;
34
35   return IS_RUNNABLE;
36 }
37
38 // Return a Task_locker for a Read_symbols task.  We don't need any
39 // locks here.
40
41 Task_locker*
42 Read_symbols::locks(Workqueue*)
43 {
44   return NULL;
45 }
46
47 // Run a Read_symbols task.  This is where we actually read the
48 // symbols and relocations.
49
50 void
51 Read_symbols::run(Workqueue* workqueue)
52 {
53   Input_file* input_file = new Input_file(this->input_);
54   input_file->open(this->options_, this->dirpath_);
55
56   // Read enough of the file to pick up the entire ELF header.
57
58   int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
59   off_t bytes;
60   const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
61   if (bytes >= 4)
62     {
63       static unsigned char elfmagic[4] =
64         {
65           elfcpp::ELFMAG0, elfcpp::ELFMAG1,
66           elfcpp::ELFMAG2, elfcpp::ELFMAG3
67         };
68       if (memcmp(p, elfmagic, 4) == 0)
69         {
70           // This is an ELF object.
71           Object* obj = make_elf_object(this->input_.name(), input_file, 0,
72                                         p, bytes);
73
74           this->input_objects_->add_object(obj);
75
76           Read_symbols_data sd = obj->read_symbols();
77           workqueue->queue(new Add_symbols(this->symtab_, obj, sd,
78                                            this->this_blocker_,
79                                            this->next_blocker_));
80
81           // Opening the file locked it, so now we need to unlock it.
82           input_file->file().unlock();
83
84           return;
85         }
86     }
87
88   // Here we have to handle archives and any other input file
89   // types we need.
90   gold_fatal("only objects are currently supported", false);
91 }
92
93 // Class Add_symbols.
94
95 Add_symbols::~Add_symbols()
96 {
97   if (this->this_blocker_ != NULL)
98     delete this->this_blocker_;
99   // next_blocker_ is deleted by the task associated with the next
100   // input file.
101 }
102
103 // We are blocked by this_blocker_.  We block next_blocker_.  We also
104 // lock the file.
105
106 Task::Is_runnable_type
107 Add_symbols::is_runnable(Workqueue*)
108 {
109   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
110     return IS_BLOCKED;
111   if (this->object_->is_locked())
112     return IS_LOCKED;
113   return IS_RUNNABLE;
114 }
115
116 class Add_symbols::Add_symbols_locker : public Task_locker
117 {
118  public:
119   Add_symbols_locker(Task_token& token, Workqueue* workqueue,
120                      Object* object)
121     : blocker_(token, workqueue), objlock_(*object)
122   { }
123
124  private:
125   Task_locker_block blocker_;
126   Task_locker_obj<Object> objlock_;
127 };
128
129 Task_locker*
130 Add_symbols::locks(Workqueue* workqueue)
131 {
132   return new Add_symbols_locker(*this->next_blocker_, workqueue,
133                                 this->object_);
134 }
135
136 void
137 Add_symbols::run(Workqueue*)
138 {
139   this->object_->add_symbols(this->symtab_, this->sd_);
140 }
141
142 } // End namespace gold.