1 // readsyms.cc -- read input file symbols for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "dirsearch.h"
39 // Class read_symbols.
41 Read_symbols::~Read_symbols()
43 // The this_blocker_ and next_blocker_ pointers are passed on to the
47 // Return whether a Read_symbols task is runnable. We can read an
48 // ordinary input file immediately. For an archive specified using
49 // -l, we have to wait until the search path is complete.
51 Task::Is_runnable_type
52 Read_symbols::is_runnable(Workqueue
*)
54 if (this->input_argument_
->is_file()
55 && this->input_argument_
->file().is_lib()
56 && this->dirpath_
.token().is_blocked())
62 // Return a Task_locker for a Read_symbols task. We don't need any
66 Read_symbols::locks(Workqueue
*)
71 // Run a Read_symbols task. This is where we actually read the
72 // symbols and relocations.
75 Read_symbols::run(Workqueue
* workqueue
)
77 if (this->input_argument_
->is_group())
79 gold_assert(this->input_group_
== NULL
);
80 this->do_group(workqueue
);
84 Input_file
* input_file
= new Input_file(&this->input_argument_
->file());
85 input_file
->open(this->options_
, this->dirpath_
);
87 // Read enough of the file to pick up the entire ELF header.
89 int ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
91 const unsigned char* p
= input_file
->file().get_view(0, ehdr_size
, &bytes
);
94 static unsigned char elfmagic
[4] =
96 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
97 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
99 if (memcmp(p
, elfmagic
, 4) == 0)
101 // This is an ELF object.
103 Object
* obj
= make_elf_object(input_file
->filename(),
104 input_file
, 0, p
, bytes
);
106 // We don't have a way to record a non-archive in an input
107 // group. If this is an ordinary object file, we can't
108 // include it more than once anyhow. If this is a dynamic
109 // object, then including it a second time changes nothing.
110 if (this->input_group_
!= NULL
&& !obj
->is_dynamic())
113 _("%s: %s: ordinary object found in input group\n"),
114 program_name
, input_file
->name());
118 Read_symbols_data
* sd
= new Read_symbols_data
;
119 obj
->read_symbols(sd
);
120 workqueue
->queue_front(new Add_symbols(this->input_objects_
,
121 this->symtab_
, this->layout_
,
124 this->next_blocker_
));
126 // Opening the file locked it, so now we need to unlock it.
127 input_file
->file().unlock();
133 if (bytes
>= Archive::sarmag
)
135 if (memcmp(p
, Archive::armag
, Archive::sarmag
) == 0)
137 // This is an archive.
138 Archive
* arch
= new Archive(this->input_argument_
->file().name(),
141 workqueue
->queue(new Add_archive_symbols(this->symtab_
,
143 this->input_objects_
,
147 this->next_blocker_
));
154 fprintf(stderr
, _("%s: %s: file is empty\n"),
155 program_name
, input_file
->file().filename().c_str());
159 // Try to parse this file as a script.
160 if (read_input_script(workqueue
, this->options_
, this->symtab_
,
161 this->layout_
, this->dirpath_
, this->input_objects_
,
162 this->input_group_
, this->input_argument_
, input_file
,
163 p
, bytes
, this->this_blocker_
, this->next_blocker_
))
166 // Here we have to handle any other input file types we need.
167 fprintf(stderr
, _("%s: %s: not an object or archive\n"),
168 program_name
, input_file
->file().filename().c_str());
172 // Handle a group. We need to walk through the arguments over and
173 // over until we don't see any new undefined symbols. We do this by
174 // setting off Read_symbols Tasks as usual, but recording the archive
175 // entries instead of deleting them. We also start a Finish_group
176 // Task which runs after we've read all the symbols. In that task we
177 // process the archives in a loop until we are done.
180 Read_symbols::do_group(Workqueue
* workqueue
)
182 Input_group
* input_group
= new Input_group();
184 const Input_file_group
* group
= this->input_argument_
->group();
185 Task_token
* this_blocker
= this->this_blocker_
;
186 for (Input_file_group::const_iterator p
= group
->begin();
190 const Input_argument
* arg
= &*p
;
191 gold_assert(arg
->is_file());
193 Task_token
* next_blocker
= new Task_token();
194 next_blocker
->add_blocker();
195 workqueue
->queue(new Read_symbols(this->options_
, this->input_objects_
,
196 this->symtab_
, this->layout_
,
197 this->dirpath_
, arg
, input_group
,
198 this_blocker
, next_blocker
));
199 this_blocker
= next_blocker
;
202 const int saw_undefined
= this->symtab_
->saw_undefined();
203 workqueue
->queue(new Finish_group(this->input_objects_
,
209 this->next_blocker_
));
212 // Class Add_symbols.
214 Add_symbols::~Add_symbols()
216 if (this->this_blocker_
!= NULL
)
217 delete this->this_blocker_
;
218 // next_blocker_ is deleted by the task associated with the next
222 // We are blocked by this_blocker_. We block next_blocker_. We also
225 Task::Is_runnable_type
226 Add_symbols::is_runnable(Workqueue
*)
228 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
230 if (this->object_
->is_locked())
235 class Add_symbols::Add_symbols_locker
: public Task_locker
238 Add_symbols_locker(Task_token
& token
, Workqueue
* workqueue
,
240 : blocker_(token
, workqueue
), objlock_(*object
)
244 Task_locker_block blocker_
;
245 Task_locker_obj
<Object
> objlock_
;
249 Add_symbols::locks(Workqueue
* workqueue
)
251 return new Add_symbols_locker(*this->next_blocker_
, workqueue
,
255 // Add the symbols in the object to the symbol table.
258 Add_symbols::run(Workqueue
*)
260 if (!this->input_objects_
->add_object(this->object_
))
262 // FIXME: We need to close the descriptor here.
263 delete this->object_
;
267 this->object_
->layout(this->symtab_
, this->layout_
, this->sd_
);
268 this->object_
->add_symbols(this->symtab_
, this->sd_
);
274 // Class Finish_group.
276 Finish_group::~Finish_group()
278 if (this->this_blocker_
!= NULL
)
279 delete this->this_blocker_
;
280 // next_blocker_ is deleted by the task associated with the next
281 // input file following the group.
284 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
286 Task::Is_runnable_type
287 Finish_group::is_runnable(Workqueue
*)
289 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
295 Finish_group::locks(Workqueue
* workqueue
)
297 return new Task_locker_block(*this->next_blocker_
, workqueue
);
300 // Loop over the archives until there are no new undefined symbols.
303 Finish_group::run(Workqueue
*)
305 int saw_undefined
= this->saw_undefined_
;
306 while (saw_undefined
!= this->symtab_
->saw_undefined())
308 saw_undefined
= this->symtab_
->saw_undefined();
310 for (Input_group::const_iterator p
= this->input_group_
->begin();
311 p
!= this->input_group_
->end();
314 Task_lock_obj
<Archive
> tl(**p
);
316 (*p
)->add_symbols(this->symtab_
, this->layout_
,
317 this->input_objects_
);
321 // Delete all the archives now that we no longer need them.
322 for (Input_group::const_iterator p
= this->input_group_
->begin();
323 p
!= this->input_group_
->end();
326 delete this->input_group_
;
329 } // End namespace gold.