1 // ld.c -- linker main function
11 #include "workqueue.h"
12 #include "dirsearch.h"
22 const char* program_name
;
25 gold_exit(bool status
)
27 exit(status
? EXIT_SUCCESS
: EXIT_FAILURE
);
31 gold_fatal(const char* msg
, bool perrno
)
33 fprintf(stderr
, "%s: ", program_name
);
37 fprintf(stderr
, "%s\n", msg
);
44 // We are out of memory, so try hard to print a reasonable message.
45 // Note that we don't try to translate this message, since the
46 // translation process itself will require memory.
47 write(2, program_name
, strlen(program_name
));
48 const char* const s
= ": out of memory\n";
49 write(2, s
, strlen(s
));
59 // This class arranges to run the functions done in the middle of the
60 // link. It is just a closure.
62 class Middle_runner
: public Task_function_runner
65 Middle_runner(const General_options
& options
,
66 const Input_objects
* input_objects
,
69 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
77 const General_options
& options_
;
78 const Input_objects
* input_objects_
;
79 Symbol_table
* symtab_
;
84 Middle_runner::run(Workqueue
* workqueue
)
86 queue_middle_tasks(this->options_
, this->input_objects_
, this->symtab_
,
87 this->layout_
, workqueue
);
90 // Queue up the initial set of tasks for this link job.
93 queue_initial_tasks(const General_options
& options
,
94 const Dirsearch
& search_path
,
95 const Input_argument_list
& inputs
,
96 Workqueue
* workqueue
, Input_objects
* input_objects
,
97 Symbol_table
* symtab
, Layout
* layout
)
100 gold_fatal(_("no input files"), false);
102 // Read the input files. We have to add the symbols to the symbol
103 // table in order. We do this by creating a separate blocker for
104 // each input file. We associate the blocker with the following
105 // input file, to give us a convenient place to delete it.
106 Task_token
* this_blocker
= NULL
;
107 for (Input_argument_list::const_iterator p
= inputs
.begin();
111 Task_token
* next_blocker
= new Task_token();
112 next_blocker
->add_blocker();
113 workqueue
->queue(new Read_symbols(options
, input_objects
, symtab
, layout
,
114 search_path
, *p
, this_blocker
,
116 this_blocker
= next_blocker
;
119 workqueue
->queue(new Task_function(new Middle_runner(options
,
126 // Queue up the middle set of tasks. These are the tasks which run
127 // after all the input objects have been found and all the symbols
128 // have been read, but before we lay out the output file.
131 queue_middle_tasks(const General_options
& options
,
132 const Input_objects
* input_objects
,
133 Symbol_table
* symtab
,
135 Workqueue
* workqueue
)
137 // Read the relocations of the input files. We do this to find
138 // which symbols are used by relocations which require a GOT and/or
139 // a PLT entry, or a COPY reloc. When we implement garbage
140 // collection we will do it here by reading the relocations in a
141 // breadth first search by references.
143 // We could also read the relocations during the first pass, and
144 // mark symbols at that time. That is how the old GNU linker works.
145 // Doing that is more complex, since we may later decide to discard
146 // some of the sections, and thus change our minds about the types
147 // of references made to the symbols.
148 Task_token
* blocker
= new Task_token();
149 Task_token
* symtab_lock
= new Task_token();
150 for (Input_objects::Object_list::const_iterator p
= input_objects
->begin();
151 p
!= input_objects
->end();
154 // We can read and process the relocations in any order. But we
155 // only want one task to write to the symbol table at a time.
156 // So we queue up a task for each object to read the
157 // relocations. That task will in turn queue a task to wait
158 // until it can write to the symbol table.
159 blocker
->add_blocker();
160 workqueue
->queue(new Read_relocs(options
, symtab
, *p
, symtab_lock
,
164 // Allocate common symbols. This requires write access to the
165 // symbol table, but is independent of the relocation processing.
166 // blocker->add_blocker();
167 // workqueue->queue(new Allocate_commons_task(options, symtab, layout,
168 // symtab_lock, blocker));
170 // When all those tasks are complete, we can start laying out the
172 workqueue
->queue(new Task_function(new Layout_task_runner(options
,
179 // Queue up the final set of tasks. This is called at the end of
183 queue_final_tasks(const General_options
& options
,
184 const Input_objects
* input_objects
,
185 const Symbol_table
* symtab
,
186 const Layout
* layout
,
187 Workqueue
* workqueue
,
190 // Use a blocker to block the final cleanup task.
191 Task_token
* final_blocker
= new Task_token();
193 // Queue a task for each input object to relocate the sections and
194 // write out the local symbols.
195 for (Input_objects::Object_list::const_iterator p
= input_objects
->begin();
196 p
!= input_objects
->end();
199 final_blocker
->add_blocker();
200 workqueue
->queue(new Relocate_task(options
, symtab
, layout
, *p
, of
,
204 // Queue a task to write out the symbol table.
205 final_blocker
->add_blocker();
206 workqueue
->queue(new Write_symbols_task(symtab
, input_objects
->target(),
207 layout
->sympool(), of
,
210 // Queue a task to write out everything else.
211 final_blocker
->add_blocker();
212 workqueue
->queue(new Write_data_task(layout
, of
, final_blocker
));
214 // Queue a task to close the output file. This will be blocked by
216 workqueue
->queue(new Task_function(new Close_task_runner(of
),
220 } // End namespace gold.
222 using namespace gold
;
225 main(int argc
, char** argv
)
227 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
228 setlocale (LC_MESSAGES
, "");
230 #if defined (HAVE_SETLOCALE)
231 setlocale (LC_CTYPE
, "");
233 bindtextdomain (PACKAGE
, LOCALEDIR
);
234 textdomain (PACKAGE
);
236 gold::program_name
= argv
[0];
238 // Handle the command line options.
239 gold::Command_line command_line
;
240 command_line
.process(argc
- 1, argv
+ 1);
243 gold::Workqueue
workqueue(command_line
.options());
245 // The list of input objects.
246 Input_objects input_objects
;
251 // The layout object.
252 Layout
layout(command_line
.options());
254 // Get the search path from the -L options.
255 Dirsearch search_path
;
256 search_path
.add(&workqueue
, command_line
.options().search_path());
258 // Queue up the first set of tasks.
259 queue_initial_tasks(command_line
.options(), search_path
,
260 command_line
.inputs(), &workqueue
, &input_objects
,
263 // Run the main task processing loop.
266 gold::gold_exit(true);