1 // ld.c -- linker main function
11 #include "workqueue.h"
12 #include "dirsearch.h"
24 const char* program_name
;
27 gold_exit(bool status
)
29 exit(status
? EXIT_SUCCESS
: EXIT_FAILURE
);
33 gold_fatal(const char* msg
, bool perrno
)
35 fprintf(stderr
, "%s: ", program_name
);
39 fprintf(stderr
, "%s\n", msg
);
46 // We are out of memory, so try hard to print a reasonable message.
47 // Note that we don't try to translate this message, since the
48 // translation process itself will require memory.
49 write(2, program_name
, strlen(program_name
));
50 const char* const s
= ": out of memory\n";
51 write(2, s
, strlen(s
));
61 // This class arranges to run the functions done in the middle of the
62 // link. It is just a closure.
64 class Middle_runner
: public Task_function_runner
67 Middle_runner(const General_options
& options
,
68 const Input_objects
* input_objects
,
71 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
79 const General_options
& options_
;
80 const Input_objects
* input_objects_
;
81 Symbol_table
* symtab_
;
86 Middle_runner::run(Workqueue
* workqueue
)
88 queue_middle_tasks(this->options_
, this->input_objects_
, this->symtab_
,
89 this->layout_
, workqueue
);
92 // Queue up the initial set of tasks for this link job.
95 queue_initial_tasks(const General_options
& options
,
96 const Dirsearch
& search_path
,
97 const Command_line
& cmdline
,
98 Workqueue
* workqueue
, Input_objects
* input_objects
,
99 Symbol_table
* symtab
, Layout
* layout
)
101 if (cmdline
.begin() == cmdline
.end())
102 gold_fatal(_("no input files"), false);
104 // Read the input files. We have to add the symbols to the symbol
105 // table in order. We do this by creating a separate blocker for
106 // each input file. We associate the blocker with the following
107 // input file, to give us a convenient place to delete it.
108 Task_token
* this_blocker
= NULL
;
109 for (Command_line::const_iterator p
= cmdline
.begin();
113 Task_token
* next_blocker
= new Task_token();
114 next_blocker
->add_blocker();
115 workqueue
->queue(new Read_symbols(options
, input_objects
, symtab
, layout
,
116 search_path
, *p
, NULL
, this_blocker
,
118 this_blocker
= next_blocker
;
121 workqueue
->queue(new Task_function(new Middle_runner(options
,
128 // Queue up the middle set of tasks. These are the tasks which run
129 // after all the input objects have been found and all the symbols
130 // have been read, but before we lay out the output file.
133 queue_middle_tasks(const General_options
& options
,
134 const Input_objects
* input_objects
,
135 Symbol_table
* symtab
,
137 Workqueue
* workqueue
)
139 // Predefine standard symbols. This should be fast, so we don't
140 // bother to create a task for it.
141 define_standard_symbols(symtab
, layout
, input_objects
->target());
143 // Read the relocations of the input files. We do this to find
144 // which symbols are used by relocations which require a GOT and/or
145 // a PLT entry, or a COPY reloc. When we implement garbage
146 // collection we will do it here by reading the relocations in a
147 // breadth first search by references.
149 // We could also read the relocations during the first pass, and
150 // mark symbols at that time. That is how the old GNU linker works.
151 // Doing that is more complex, since we may later decide to discard
152 // some of the sections, and thus change our minds about the types
153 // of references made to the symbols.
154 Task_token
* blocker
= new Task_token();
155 Task_token
* symtab_lock
= new Task_token();
156 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
157 p
!= input_objects
->relobj_end();
160 // We can read and process the relocations in any order. But we
161 // only want one task to write to the symbol table at a time.
162 // So we queue up a task for each object to read the
163 // relocations. That task will in turn queue a task to wait
164 // until it can write to the symbol table.
165 blocker
->add_blocker();
166 workqueue
->queue(new Read_relocs(options
, symtab
, layout
, *p
,
167 symtab_lock
, blocker
));
170 // Allocate common symbols. This requires write access to the
171 // symbol table, but is independent of the relocation processing.
172 blocker
->add_blocker();
173 workqueue
->queue(new Allocate_commons_task(options
, symtab
, layout
,
174 symtab_lock
, blocker
));
176 // When all those tasks are complete, we can start laying out the
178 workqueue
->queue(new Task_function(new Layout_task_runner(options
,
185 // Queue up the final set of tasks. This is called at the end of
189 queue_final_tasks(const General_options
& options
,
190 const Input_objects
* input_objects
,
191 const Symbol_table
* symtab
,
192 const Layout
* layout
,
193 Workqueue
* workqueue
,
196 // Use a blocker to block the final cleanup task.
197 Task_token
* final_blocker
= new Task_token();
199 // Queue a task for each input object to relocate the sections and
200 // write out the local symbols.
201 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
202 p
!= input_objects
->relobj_end();
205 final_blocker
->add_blocker();
206 workqueue
->queue(new Relocate_task(options
, symtab
, layout
, *p
, of
,
210 // Queue a task to write out the symbol table.
211 final_blocker
->add_blocker();
212 workqueue
->queue(new Write_symbols_task(symtab
, input_objects
->target(),
213 layout
->sympool(), of
,
216 // Queue a task to write out everything else.
217 final_blocker
->add_blocker();
218 workqueue
->queue(new Write_data_task(layout
, of
, final_blocker
));
220 // Queue a task to close the output file. This will be blocked by
222 workqueue
->queue(new Task_function(new Close_task_runner(of
),
226 } // End namespace gold.
228 using namespace gold
;
231 main(int argc
, char** argv
)
233 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
234 setlocale (LC_MESSAGES
, "");
236 #if defined (HAVE_SETLOCALE)
237 setlocale (LC_CTYPE
, "");
239 bindtextdomain (PACKAGE
, LOCALEDIR
);
240 textdomain (PACKAGE
);
242 gold::program_name
= argv
[0];
244 // Handle the command line options.
245 gold::Command_line command_line
;
246 command_line
.process(argc
- 1, argv
+ 1);
249 gold::Workqueue
workqueue(command_line
.options());
251 // The list of input objects.
252 Input_objects input_objects
;
257 // The layout object.
258 Layout
layout(command_line
.options());
260 // Get the search path from the -L options.
261 Dirsearch search_path
;
262 search_path
.add(&workqueue
, command_line
.options().search_path());
264 // Queue up the first set of tasks.
265 queue_initial_tasks(command_line
.options(), search_path
,
266 command_line
, &workqueue
, &input_objects
,
269 // Run the main task processing loop.
272 gold::gold_exit(true);