* NEW: Mention pending breakpoint changes and
[deliverable/binutils-gdb.git] / gold / gold.cc
CommitLineData
5a6f7e2d 1// gold.cc -- main linker functions
bae7f79e 2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
bae7f79e
ILT
23#include "gold.h"
24
25#include <cstdlib>
26#include <cstdio>
27#include <cstring>
28#include <unistd.h>
29
30#include "options.h"
31#include "workqueue.h"
32#include "dirsearch.h"
33#include "readsyms.h"
14bfc3f5 34#include "symtab.h"
ead1e424 35#include "common.h"
54dc6425 36#include "object.h"
a2fb1b05 37#include "layout.h"
61ba1cf9 38#include "reloc.h"
ead1e424 39#include "defstd.h"
bae7f79e
ILT
40
41namespace gold
42{
43
44const char* program_name;
45
46void
47gold_exit(bool status)
48{
49 exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
50}
51
52void
53gold_fatal(const char* msg, bool perrno)
54{
55 fprintf(stderr, "%s: ", program_name);
56 if (perrno)
57 perror(msg);
58 else
59 fprintf(stderr, "%s\n", msg);
60 gold_exit(false);
61}
62
63void
64gold_nomem()
65{
66 // We are out of memory, so try hard to print a reasonable message.
67 // Note that we don't try to translate this message, since the
68 // translation process itself will require memory.
69 write(2, program_name, strlen(program_name));
70 const char* const s = ": out of memory\n";
71 write(2, s, strlen(s));
72 gold_exit(false);
73}
74
a3ad94ed
ILT
75// Handle an unreachable case.
76
bae7f79e 77void
a3ad94ed 78do_gold_unreachable(const char* filename, int lineno, const char* function)
bae7f79e 79{
a3ad94ed
ILT
80 fprintf(stderr, "%s: internal error in %s, at %s:%d\n",
81 program_name, function, filename, lineno);
82 gold_exit(false);
bae7f79e
ILT
83}
84
92e059d8
ILT
85// This class arranges to run the functions done in the middle of the
86// link. It is just a closure.
bae7f79e 87
92e059d8 88class Middle_runner : public Task_function_runner
bae7f79e 89{
92e059d8
ILT
90 public:
91 Middle_runner(const General_options& options,
92 const Input_objects* input_objects,
93 Symbol_table* symtab,
94 Layout* layout)
95 : options_(options), input_objects_(input_objects), symtab_(symtab),
96 layout_(layout)
97 { }
98
99 void
100 run(Workqueue*);
101
102 private:
103 const General_options& options_;
104 const Input_objects* input_objects_;
105 Symbol_table* symtab_;
106 Layout* layout_;
107};
bae7f79e 108
92e059d8
ILT
109void
110Middle_runner::run(Workqueue* workqueue)
111{
112 queue_middle_tasks(this->options_, this->input_objects_, this->symtab_,
113 this->layout_, workqueue);
114}
bae7f79e
ILT
115
116// Queue up the initial set of tasks for this link job.
117
118void
119queue_initial_tasks(const General_options& options,
120 const Dirsearch& search_path,
ead1e424 121 const Command_line& cmdline,
54dc6425 122 Workqueue* workqueue, Input_objects* input_objects,
12e14209 123 Symbol_table* symtab, Layout* layout)
bae7f79e 124{
ead1e424 125 if (cmdline.begin() == cmdline.end())
bae7f79e
ILT
126 gold_fatal(_("no input files"), false);
127
128 // Read the input files. We have to add the symbols to the symbol
129 // table in order. We do this by creating a separate blocker for
130 // each input file. We associate the blocker with the following
131 // input file, to give us a convenient place to delete it.
132 Task_token* this_blocker = NULL;
ead1e424
ILT
133 for (Command_line::const_iterator p = cmdline.begin();
134 p != cmdline.end();
bae7f79e
ILT
135 ++p)
136 {
137 Task_token* next_blocker = new Task_token();
138 next_blocker->add_blocker();
12e14209 139 workqueue->queue(new Read_symbols(options, input_objects, symtab, layout,
dbe717ef 140 search_path, &*p, NULL, this_blocker,
a2fb1b05 141 next_blocker));
bae7f79e
ILT
142 this_blocker = next_blocker;
143 }
144
92e059d8
ILT
145 workqueue->queue(new Task_function(new Middle_runner(options,
146 input_objects,
147 symtab,
148 layout),
149 this_blocker));
bae7f79e
ILT
150}
151
92e059d8
ILT
152// Queue up the middle set of tasks. These are the tasks which run
153// after all the input objects have been found and all the symbols
154// have been read, but before we lay out the output file.
bae7f79e 155
92e059d8
ILT
156void
157queue_middle_tasks(const General_options& options,
158 const Input_objects* input_objects,
159 Symbol_table* symtab,
160 Layout* layout,
161 Workqueue* workqueue)
61ba1cf9 162{
a3ad94ed
ILT
163 // Define some sections and symbols needed for a dynamic link. This
164 // handles some cases we want to see before we read the relocs.
165 layout->create_initial_dynamic_sections(input_objects, symtab);
166
ead1e424
ILT
167 // Predefine standard symbols. This should be fast, so we don't
168 // bother to create a task for it.
169 define_standard_symbols(symtab, layout, input_objects->target());
170
bfd58944
ILT
171 // Define __start and __stop symbols for output sections where
172 // appropriate.
173 layout->define_section_symbols(symtab, input_objects->target());
174
92e059d8
ILT
175 // Read the relocations of the input files. We do this to find
176 // which symbols are used by relocations which require a GOT and/or
177 // a PLT entry, or a COPY reloc. When we implement garbage
178 // collection we will do it here by reading the relocations in a
179 // breadth first search by references.
180 //
181 // We could also read the relocations during the first pass, and
182 // mark symbols at that time. That is how the old GNU linker works.
183 // Doing that is more complex, since we may later decide to discard
184 // some of the sections, and thus change our minds about the types
185 // of references made to the symbols.
186 Task_token* blocker = new Task_token();
187 Task_token* symtab_lock = new Task_token();
f6ce93d6
ILT
188 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
189 p != input_objects->relobj_end();
92e059d8
ILT
190 ++p)
191 {
192 // We can read and process the relocations in any order. But we
193 // only want one task to write to the symbol table at a time.
194 // So we queue up a task for each object to read the
195 // relocations. That task will in turn queue a task to wait
196 // until it can write to the symbol table.
197 blocker->add_blocker();
ead1e424
ILT
198 workqueue->queue(new Read_relocs(options, symtab, layout, *p,
199 symtab_lock, blocker));
92e059d8
ILT
200 }
201
202 // Allocate common symbols. This requires write access to the
203 // symbol table, but is independent of the relocation processing.
ead1e424
ILT
204 blocker->add_blocker();
205 workqueue->queue(new Allocate_commons_task(options, symtab, layout,
206 symtab_lock, blocker));
92e059d8
ILT
207
208 // When all those tasks are complete, we can start laying out the
209 // output file.
210 workqueue->queue(new Task_function(new Layout_task_runner(options,
211 input_objects,
212 symtab,
213 layout),
214 blocker));
215}
61ba1cf9
ILT
216
217// Queue up the final set of tasks. This is called at the end of
218// Layout_task.
219
220void
221queue_final_tasks(const General_options& options,
222 const Input_objects* input_objects,
223 const Symbol_table* symtab,
224 const Layout* layout,
225 Workqueue* workqueue,
226 Output_file* of)
227{
228 // Use a blocker to block the final cleanup task.
229 Task_token* final_blocker = new Task_token();
230
231 // Queue a task for each input object to relocate the sections and
232 // write out the local symbols.
f6ce93d6
ILT
233 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
234 p != input_objects->relobj_end();
61ba1cf9
ILT
235 ++p)
236 {
237 final_blocker->add_blocker();
92e059d8
ILT
238 workqueue->queue(new Relocate_task(options, symtab, layout, *p, of,
239 final_blocker));
61ba1cf9
ILT
240 }
241
242 // Queue a task to write out the symbol table.
243 final_blocker->add_blocker();
16649710
ILT
244 workqueue->queue(new Write_symbols_task(symtab,
245 input_objects->target(),
246 layout->sympool(),
247 layout->dynpool(),
248 of,
61ba1cf9
ILT
249 final_blocker));
250
251 // Queue a task to write out everything else.
252 final_blocker->add_blocker();
9025d29d 253 workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
61ba1cf9
ILT
254
255 // Queue a task to close the output file. This will be blocked by
256 // FINAL_BLOCKER.
92e059d8
ILT
257 workqueue->queue(new Task_function(new Close_task_runner(of),
258 final_blocker));
61ba1cf9
ILT
259}
260
261} // End namespace gold.
This page took 0.072669 seconds and 4 git commands to generate.