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