Snapshot. Now able to produce a minimal executable which actually
[platform/upstream/binutils.git] / gold / gold.cc
1 // ld.c -- linker main function
2
3 #include "gold.h"
4
5 #include <cstdlib>
6 #include <cstdio>
7 #include <cstring>
8 #include <unistd.h>
9
10 #include "options.h"
11 #include "workqueue.h"
12 #include "dirsearch.h"
13 #include "readsyms.h"
14 #include "symtab.h"
15 #include "object.h"
16 #include "layout.h"
17 #include "reloc.h"
18
19 namespace gold
20 {
21
22 const char* program_name;
23
24 void
25 gold_exit(bool status)
26 {
27   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
28 }
29
30 void
31 gold_fatal(const char* msg, bool perrno)
32 {
33   fprintf(stderr, "%s: ", program_name);
34   if (perrno)
35     perror(msg);
36   else
37     fprintf(stderr, "%s\n", msg);
38   gold_exit(false);
39 }
40
41 void
42 gold_nomem()
43 {
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));
50   gold_exit(false);
51 }
52
53 void
54 gold_unreachable()
55 {
56   abort();
57 }
58
59 } // End namespace gold.
60
61 namespace
62 {
63
64 using namespace gold;
65
66 // Queue up the initial set of tasks for this link job.
67
68 void
69 queue_initial_tasks(const General_options& options,
70                     const Dirsearch& search_path,
71                     const Command_line::Input_argument_list& inputs,
72                     Workqueue* workqueue, Input_objects* input_objects,
73                     Symbol_table* symtab)
74 {
75   if (inputs.empty())
76     gold_fatal(_("no input files"), false);
77
78   // Read the input files.  We have to add the symbols to the symbol
79   // table in order.  We do this by creating a separate blocker for
80   // each input file.  We associate the blocker with the following
81   // input file, to give us a convenient place to delete it.
82   Task_token* this_blocker = NULL;
83   for (Command_line::Input_argument_list::const_iterator p = inputs.begin();
84        p != inputs.end();
85        ++p)
86     {
87       Task_token* next_blocker = new Task_token();
88       next_blocker->add_blocker();
89       workqueue->queue(new Read_symbols(options, input_objects, symtab,
90                                         search_path, *p, this_blocker,
91                                         next_blocker));
92       this_blocker = next_blocker;
93     }
94
95   workqueue->queue(new Layout_task(options, input_objects, symtab,
96                                    this_blocker));
97 }
98
99 } // end anonymous namespace.
100
101 namespace gold
102 {
103
104 // Queue up the final set of tasks.  This is called at the end of
105 // Layout_task.
106
107 void
108 queue_final_tasks(const General_options& options,
109                   const Input_objects* input_objects,
110                   const Symbol_table* symtab,
111                   const Layout* layout,
112                   Workqueue* workqueue,
113                   Output_file* of)
114 {
115   // Use a blocker to block the final cleanup task.
116   Task_token* final_blocker = new Task_token();
117
118   // Queue a task for each input object to relocate the sections and
119   // write out the local symbols.
120   for (Input_objects::Object_list::const_iterator p = input_objects->begin();
121        p != input_objects->end();
122        ++p)
123     {
124       final_blocker->add_blocker();
125       workqueue->queue(new Relocate_task(options, symtab, layout->sympool(),
126                                          *p, of, final_blocker));
127     }
128
129   // Queue a task to write out the symbol table.
130   final_blocker->add_blocker();
131   workqueue->queue(new Write_symbols_task(symtab, input_objects->target(),
132                                           layout->sympool(), of,
133                                           final_blocker));
134
135   // Queue a task to write out everything else.
136   final_blocker->add_blocker();
137   workqueue->queue(new Write_data_task(layout, of, final_blocker));
138
139   // Queue a task to close the output file.  This will be blocked by
140   // FINAL_BLOCKER.
141   workqueue->queue(new Close_task(of, final_blocker));
142 }
143
144 } // End namespace gold.
145
146 int
147 main(int argc, char** argv)
148 {
149 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
150   setlocale (LC_MESSAGES, "");
151 #endif
152 #if defined (HAVE_SETLOCALE)
153   setlocale (LC_CTYPE, "");
154 #endif
155   bindtextdomain (PACKAGE, LOCALEDIR);
156   textdomain (PACKAGE);
157
158   gold::program_name = argv[0];
159
160   // Handle the command line options.
161   gold::Command_line command_line;
162   command_line.process(argc - 1, argv + 1);
163
164   // The work queue.
165   gold::Workqueue workqueue(command_line.options());
166
167   // The list of input objects.
168   Input_objects input_objects;
169
170   // The symbol table.
171   Symbol_table symtab;
172
173   // Get the search path from the -L options.
174   Dirsearch search_path;
175   search_path.add(&workqueue, command_line.options().search_path());
176
177   // Queue up the first set of tasks.
178   queue_initial_tasks(command_line.options(), search_path,
179                       command_line.inputs(), &workqueue, &input_objects,
180                       &symtab);
181
182   // Run the main task processing loop.
183   workqueue.process();
184
185   gold::gold_exit(true);
186 }