1 // gold.cc -- main linker functions
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));
55 // Handle an unreachable case.
58 do_gold_unreachable(const char* filename, int lineno, const char* function)
60 fprintf(stderr, "%s: internal error in %s, at %s:%d\n",
61 program_name, function, filename, lineno);
65 // This class arranges to run the functions done in the middle of the
66 // link. It is just a closure.
68 class Middle_runner : public Task_function_runner
71 Middle_runner(const General_options& options,
72 const Input_objects* input_objects,
75 : options_(options), input_objects_(input_objects), symtab_(symtab),
83 const General_options& options_;
84 const Input_objects* input_objects_;
85 Symbol_table* symtab_;
90 Middle_runner::run(Workqueue* workqueue)
92 queue_middle_tasks(this->options_, this->input_objects_, this->symtab_,
93 this->layout_, workqueue);
96 // Queue up the initial set of tasks for this link job.
99 queue_initial_tasks(const General_options& options,
100 const Dirsearch& search_path,
101 const Command_line& cmdline,
102 Workqueue* workqueue, Input_objects* input_objects,
103 Symbol_table* symtab, Layout* layout)
105 if (cmdline.begin() == cmdline.end())
106 gold_fatal(_("no input files"), false);
108 // Read the input files. We have to add the symbols to the symbol
109 // table in order. We do this by creating a separate blocker for
110 // each input file. We associate the blocker with the following
111 // input file, to give us a convenient place to delete it.
112 Task_token* this_blocker = NULL;
113 for (Command_line::const_iterator p = cmdline.begin();
117 Task_token* next_blocker = new Task_token();
118 next_blocker->add_blocker();
119 workqueue->queue(new Read_symbols(options, input_objects, symtab, layout,
120 search_path, &*p, NULL, this_blocker,
122 this_blocker = next_blocker;
125 workqueue->queue(new Task_function(new Middle_runner(options,
132 // Queue up the middle set of tasks. These are the tasks which run
133 // after all the input objects have been found and all the symbols
134 // have been read, but before we lay out the output file.
137 queue_middle_tasks(const General_options& options,
138 const Input_objects* input_objects,
139 Symbol_table* symtab,
141 Workqueue* workqueue)
143 // Define some sections and symbols needed for a dynamic link. This
144 // handles some cases we want to see before we read the relocs.
145 layout->create_initial_dynamic_sections(input_objects, symtab);
147 // Predefine standard symbols. This should be fast, so we don't
148 // bother to create a task for it.
149 define_standard_symbols(symtab, layout, input_objects->target());
151 // Define __start and __stop symbols for output sections where
153 layout->define_section_symbols(symtab, input_objects->target());
155 // Read the relocations of the input files. We do this to find
156 // which symbols are used by relocations which require a GOT and/or
157 // a PLT entry, or a COPY reloc. When we implement garbage
158 // collection we will do it here by reading the relocations in a
159 // breadth first search by references.
161 // We could also read the relocations during the first pass, and
162 // mark symbols at that time. That is how the old GNU linker works.
163 // Doing that is more complex, since we may later decide to discard
164 // some of the sections, and thus change our minds about the types
165 // of references made to the symbols.
166 Task_token* blocker = new Task_token();
167 Task_token* symtab_lock = new Task_token();
168 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
169 p != input_objects->relobj_end();
172 // We can read and process the relocations in any order. But we
173 // only want one task to write to the symbol table at a time.
174 // So we queue up a task for each object to read the
175 // relocations. That task will in turn queue a task to wait
176 // until it can write to the symbol table.
177 blocker->add_blocker();
178 workqueue->queue(new Read_relocs(options, symtab, layout, *p,
179 symtab_lock, blocker));
182 // Allocate common symbols. This requires write access to the
183 // symbol table, but is independent of the relocation processing.
184 blocker->add_blocker();
185 workqueue->queue(new Allocate_commons_task(options, symtab, layout,
186 symtab_lock, blocker));
188 // When all those tasks are complete, we can start laying out the
190 workqueue->queue(new Task_function(new Layout_task_runner(options,
197 // Queue up the final set of tasks. This is called at the end of
201 queue_final_tasks(const General_options& options,
202 const Input_objects* input_objects,
203 const Symbol_table* symtab,
204 const Layout* layout,
205 Workqueue* workqueue,
208 // Use a blocker to block the final cleanup task.
209 Task_token* final_blocker = new Task_token();
211 // Queue a task for each input object to relocate the sections and
212 // write out the local symbols.
213 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
214 p != input_objects->relobj_end();
217 final_blocker->add_blocker();
218 workqueue->queue(new Relocate_task(options, symtab, layout, *p, of,
222 // Queue a task to write out the symbol table.
223 final_blocker->add_blocker();
224 workqueue->queue(new Write_symbols_task(symtab,
225 input_objects->target(),
231 // Queue a task to write out everything else.
232 final_blocker->add_blocker();
233 workqueue->queue(new Write_data_task(layout, symtab,
234 input_objects->target(),
237 // Queue a task to close the output file. This will be blocked by
239 workqueue->queue(new Task_function(new Close_task_runner(of),
243 } // End namespace gold.