1 // readsyms.cc -- read input file symbols for gold
18 // Class read_symbols.
20 Read_symbols::~Read_symbols()
22 // The this_blocker_ and next_blocker_ pointers are passed on to the
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.
30 Task::Is_runnable_type
31 Read_symbols::is_runnable(Workqueue
*)
33 if (this->input_
.is_file()
34 && this->input_
.file().is_lib()
35 && this->dirpath_
.token().is_blocked())
41 // Return a Task_locker for a Read_symbols task. We don't need any
45 Read_symbols::locks(Workqueue
*)
50 // Run a Read_symbols task. This is where we actually read the
51 // symbols and relocations.
54 Read_symbols::run(Workqueue
* workqueue
)
56 if (this->input_
.is_group())
58 assert(this->input_group_
== NULL
);
59 this->do_group(workqueue
);
63 Input_file
* input_file
= new Input_file(this->input_
.file());
64 input_file
->open(this->options_
, this->dirpath_
);
66 // Read enough of the file to pick up the entire ELF header.
68 int ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
70 const unsigned char* p
= input_file
->file().get_view(0, ehdr_size
, &bytes
);
73 static unsigned char elfmagic
[4] =
75 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
76 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
78 if (memcmp(p
, elfmagic
, 4) == 0)
80 // This is an ELF object.
82 if (this->input_group_
!= NULL
)
85 _("%s: %s: ordinary object found in input group\n"),
86 program_name
, input_file
->name());
90 Object
* obj
= make_elf_object(this->input_
.file().name(),
91 input_file
, 0, p
, bytes
);
93 Read_symbols_data
* sd
= new Read_symbols_data
;
94 obj
->read_symbols(sd
);
95 workqueue
->queue_front(new Add_symbols(this->options_
,
97 this->symtab_
, this->layout_
,
100 this->next_blocker_
));
102 // Opening the file locked it, so now we need to unlock it.
103 input_file
->file().unlock();
109 if (bytes
>= Archive::sarmag
)
111 if (memcmp(p
, Archive::armag
, Archive::sarmag
) == 0)
113 // This is an archive.
114 Archive
* arch
= new Archive(this->input_
.file().name(), input_file
);
116 workqueue
->queue(new Add_archive_symbols(this->options_
,
119 this->input_objects_
,
123 this->next_blocker_
));
128 // Here we have to handle any other input file types we need.
129 fprintf(stderr
, _("%s: %s: not an object or archive\n"),
130 program_name
, input_file
->file().filename().c_str());
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.
142 Read_symbols::do_group(Workqueue
* workqueue
)
144 Input_group
* input_group
= new Input_group();
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();
152 const Input_argument
& arg(*p
);
153 assert(arg
.is_file());
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
;
164 const int saw_undefined
= this->symtab_
->saw_undefined();
165 workqueue
->queue(new Finish_group(this->options_
,
166 this->input_objects_
,
172 this->next_blocker_
));
175 // Class Add_symbols.
177 Add_symbols::~Add_symbols()
179 if (this->this_blocker_
!= NULL
)
180 delete this->this_blocker_
;
181 // next_blocker_ is deleted by the task associated with the next
185 // We are blocked by this_blocker_. We block next_blocker_. We also
188 Task::Is_runnable_type
189 Add_symbols::is_runnable(Workqueue
*)
191 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
193 if (this->object_
->is_locked())
198 class Add_symbols::Add_symbols_locker
: public Task_locker
201 Add_symbols_locker(Task_token
& token
, Workqueue
* workqueue
,
203 : blocker_(token
, workqueue
), objlock_(*object
)
207 Task_locker_block blocker_
;
208 Task_locker_obj
<Object
> objlock_
;
212 Add_symbols::locks(Workqueue
* workqueue
)
214 return new Add_symbols_locker(*this->next_blocker_
, workqueue
,
218 // Add the symbols in the object to the symbol table.
221 Add_symbols::run(Workqueue
*)
223 this->input_objects_
->add_object(this->object_
);
224 this->object_
->layout(this->options_
, this->symtab_
, this->layout_
,
226 this->object_
->add_symbols(this->symtab_
, this->sd_
);
231 // Class Finish_group.
233 Finish_group::~Finish_group()
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.
241 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
243 Task::Is_runnable_type
244 Finish_group::is_runnable(Workqueue
*)
246 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
252 Finish_group::locks(Workqueue
* workqueue
)
254 return new Task_locker_block(*this->next_blocker_
, workqueue
);
257 // Loop over the archives until there are no new undefined symbols.
260 Finish_group::run(Workqueue
*)
262 int saw_undefined
= this->saw_undefined_
;
263 while (saw_undefined
!= this->symtab_
->saw_undefined())
265 saw_undefined
= this->symtab_
->saw_undefined();
267 for (Input_group::const_iterator p
= this->input_group_
->begin();
268 p
!= this->input_group_
->end();
271 Task_lock_obj
<Archive
> tl(**p
);
273 (*p
)->add_symbols(this->options_
, this->symtab_
, this->layout_
,
274 this->input_objects_
);
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();
283 delete this->input_group_
;
286 } // End namespace gold.