Rework swapping code.
[deliverable/binutils-gdb.git] / gold / readsyms.cc
CommitLineData
bae7f79e
ILT
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"
a2fb1b05 10#include "object.h"
61ba1cf9
ILT
11#include "archive.h"
12#include "readsyms.h"
bae7f79e
ILT
13
14namespace gold
15{
16
17// Class read_symbols.
18
19Read_symbols::~Read_symbols()
20{
21 // The this_blocker_ and next_blocker_ pointers are passed on to the
22 // Add_symbols task.
23}
24
ead1e424
ILT
25// Return whether a Read_symbols task is runnable. We can read an
26// ordinary input file immediately. For an archive specified using
27// -l, we have to wait until the search path is complete.
bae7f79e
ILT
28
29Task::Is_runnable_type
30Read_symbols::is_runnable(Workqueue*)
31{
ead1e424
ILT
32 if (this->input_.is_file()
33 && this->input_.file().is_lib()
34 && this->dirpath_.token().is_blocked())
bae7f79e
ILT
35 return IS_BLOCKED;
36
37 return IS_RUNNABLE;
38}
39
40// Return a Task_locker for a Read_symbols task. We don't need any
41// locks here.
42
43Task_locker*
44Read_symbols::locks(Workqueue*)
45{
46 return NULL;
47}
48
49// Run a Read_symbols task. This is where we actually read the
50// symbols and relocations.
51
52void
53Read_symbols::run(Workqueue* workqueue)
54{
ead1e424
ILT
55 if (this->input_.is_group())
56 {
57 assert(this->input_group_ == NULL);
58 this->do_group(workqueue);
59 return;
60 }
61
62 Input_file* input_file = new Input_file(this->input_.file());
bae7f79e
ILT
63 input_file->open(this->options_, this->dirpath_);
64
65 // Read enough of the file to pick up the entire ELF header.
66
67 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
68 off_t bytes;
69 const unsigned char* p = input_file->file().get_view(0, ehdr_size, &bytes);
70 if (bytes >= 4)
71 {
72 static unsigned char elfmagic[4] =
73 {
74 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
75 elfcpp::ELFMAG2, elfcpp::ELFMAG3
76 };
77 if (memcmp(p, elfmagic, 4) == 0)
78 {
79 // This is an ELF object.
a2fb1b05 80
ead1e424
ILT
81 if (this->input_group_ != NULL)
82 {
83 fprintf(stderr,
84 _("%s: %s: ordinary object found in input group\n"),
85 program_name, input_file->name());
86 gold_exit(false);
87 }
88
89 Object* obj = make_elf_object(this->input_.file().name(),
90 input_file, 0, p, bytes);
a2fb1b05 91
12e14209
ILT
92 Read_symbols_data* sd = new Read_symbols_data;
93 obj->read_symbols(sd);
ead1e424
ILT
94 workqueue->queue_front(new Add_symbols(this->input_objects_,
95 this->symtab_, this->layout_,
92e059d8
ILT
96 obj, sd,
97 this->this_blocker_,
98 this->next_blocker_));
bae7f79e
ILT
99
100 // Opening the file locked it, so now we need to unlock it.
101 input_file->file().unlock();
102
103 return;
104 }
105 }
106
61ba1cf9
ILT
107 if (bytes >= Archive::sarmag)
108 {
109 if (memcmp(p, Archive::armag, Archive::sarmag) == 0)
110 {
111 // This is an archive.
ead1e424 112 Archive* arch = new Archive(this->input_.file().name(), input_file);
61ba1cf9
ILT
113 arch->setup();
114 workqueue->queue(new Add_archive_symbols(this->symtab_,
12e14209 115 this->layout_,
61ba1cf9
ILT
116 this->input_objects_,
117 arch,
ead1e424 118 this->input_group_,
61ba1cf9
ILT
119 this->this_blocker_,
120 this->next_blocker_));
121 return;
122 }
123 }
124
92e059d8 125 // Here we have to handle any other input file types we need.
61ba1cf9
ILT
126 fprintf(stderr, _("%s: %s: not an object or archive\n"),
127 program_name, input_file->file().filename().c_str());
128 gold_exit(false);
bae7f79e
ILT
129}
130
ead1e424
ILT
131// Handle a group. We need to walk through the arguments over and
132// over until we don't see any new undefined symbols. We do this by
133// setting off Read_symbols Tasks as usual, but recording the archive
134// entries instead of deleting them. We also start a Finish_group
135// Task which runs after we've read all the symbols. In that task we
136// process the archives in a loop until we are done.
137
138void
139Read_symbols::do_group(Workqueue* workqueue)
140{
141 Input_group* input_group = new Input_group();
142
143 const Input_file_group* group = this->input_.group();
144 Task_token* this_blocker = this->this_blocker_;
145 for (Input_file_group::const_iterator p = group->begin();
146 p != group->end();
147 ++p)
148 {
149 const Input_argument& arg(*p);
150 assert(arg.is_file());
151
152 Task_token* next_blocker = new Task_token();
153 next_blocker->add_blocker();
154 workqueue->queue(new Read_symbols(this->options_, this->input_objects_,
155 this->symtab_, this->layout_,
156 this->dirpath_, arg, input_group,
157 this_blocker, next_blocker));
158 this_blocker = next_blocker;
159 }
160
161 const int saw_undefined = this->symtab_->saw_undefined();
162 workqueue->queue(new Finish_group(this->input_objects_,
163 this->symtab_,
164 this->layout_,
165 input_group,
166 saw_undefined,
167 this_blocker,
168 this->next_blocker_));
169}
170
bae7f79e
ILT
171// Class Add_symbols.
172
173Add_symbols::~Add_symbols()
174{
175 if (this->this_blocker_ != NULL)
176 delete this->this_blocker_;
177 // next_blocker_ is deleted by the task associated with the next
178 // input file.
179}
180
a2fb1b05
ILT
181// We are blocked by this_blocker_. We block next_blocker_. We also
182// lock the file.
bae7f79e
ILT
183
184Task::Is_runnable_type
185Add_symbols::is_runnable(Workqueue*)
186{
187 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
188 return IS_BLOCKED;
a2fb1b05
ILT
189 if (this->object_->is_locked())
190 return IS_LOCKED;
bae7f79e
ILT
191 return IS_RUNNABLE;
192}
193
a2fb1b05
ILT
194class Add_symbols::Add_symbols_locker : public Task_locker
195{
196 public:
197 Add_symbols_locker(Task_token& token, Workqueue* workqueue,
198 Object* object)
199 : blocker_(token, workqueue), objlock_(*object)
200 { }
201
202 private:
203 Task_locker_block blocker_;
204 Task_locker_obj<Object> objlock_;
205};
206
bae7f79e
ILT
207Task_locker*
208Add_symbols::locks(Workqueue* workqueue)
209{
a2fb1b05
ILT
210 return new Add_symbols_locker(*this->next_blocker_, workqueue,
211 this->object_);
bae7f79e
ILT
212}
213
ead1e424
ILT
214// Add the symbols in the object to the symbol table.
215
bae7f79e
ILT
216void
217Add_symbols::run(Workqueue*)
218{
ead1e424 219 this->input_objects_->add_object(this->object_);
12e14209 220 this->object_->layout(this->layout_, this->sd_);
14bfc3f5 221 this->object_->add_symbols(this->symtab_, this->sd_);
12e14209
ILT
222 delete this->sd_;
223 this->sd_ = NULL;
bae7f79e
ILT
224}
225
ead1e424
ILT
226// Class Finish_group.
227
228Finish_group::~Finish_group()
229{
230 if (this->this_blocker_ != NULL)
231 delete this->this_blocker_;
232 // next_blocker_ is deleted by the task associated with the next
233 // input file following the group.
234}
235
236// We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
237
238Task::Is_runnable_type
239Finish_group::is_runnable(Workqueue*)
240{
241 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
242 return IS_BLOCKED;
243 return IS_RUNNABLE;
244}
245
246Task_locker*
247Finish_group::locks(Workqueue* workqueue)
248{
249 return new Task_locker_block(*this->next_blocker_, workqueue);
250}
251
252// Loop over the archives until there are no new undefined symbols.
253
254void
255Finish_group::run(Workqueue*)
256{
257 int saw_undefined = this->saw_undefined_;
258 while (saw_undefined != this->symtab_->saw_undefined())
259 {
260 saw_undefined = this->symtab_->saw_undefined();
261
262 for (Input_group::const_iterator p = this->input_group_->begin();
263 p != this->input_group_->end();
264 ++p)
265 {
266 Task_lock_obj<Archive> tl(**p);
267
268 (*p)->add_symbols(this->symtab_, this->layout_,
269 this->input_objects_);
270 }
271 }
272
273 // Delete all the archives now that we no longer need them.
274 for (Input_group::const_iterator p = this->input_group_->begin();
275 p != this->input_group_->end();
276 ++p)
277 delete *p;
278 delete this->input_group_;
279}
280
bae7f79e 281} // End namespace gold.
This page took 0.039836 seconds and 4 git commands to generate.