New drop, with first cut of section layout code.
[external/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 "layout.h"
16
17 namespace gold
18 {
19
20 const char* program_name;
21
22 void
23 gold_exit(bool status)
24 {
25   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
26 }
27
28 void
29 gold_fatal(const char* msg, bool perrno)
30 {
31   fprintf(stderr, "%s: ", program_name);
32   if (perrno)
33     perror(msg);
34   else
35     fprintf(stderr, "%s\n", msg);
36   gold_exit(false);
37 }
38
39 void
40 gold_nomem()
41 {
42   // We are out of memory, so try hard to print a reasonable message.
43   // Note that we don't try to translate this message, since the
44   // translation process itself will require memory.
45   write(2, program_name, strlen(program_name));
46   const char* const s = ": out of memory\n";
47   write(2, s, strlen(s));
48   gold_exit(false);
49 }
50
51 void
52 gold_unreachable()
53 {
54   abort();
55 }
56
57 } // End namespace gold.
58
59 namespace
60 {
61
62 using namespace gold;
63
64 // Queue up the initial set of tasks for this link job.
65
66 void
67 queue_initial_tasks(const General_options& options,
68                     const Dirsearch& search_path,
69                     const Command_line::Input_argument_list& inputs,
70                     Workqueue* workqueue, Object_list* input_objects,
71                     Symbol_table* symtab)
72 {
73   if (inputs.empty())
74     gold_fatal(_("no input files"), false);
75
76   // Read the input files.  We have to add the symbols to the symbol
77   // table in order.  We do this by creating a separate blocker for
78   // each input file.  We associate the blocker with the following
79   // input file, to give us a convenient place to delete it.
80   Task_token* this_blocker = NULL;
81   for (Command_line::Input_argument_list::const_iterator p = inputs.begin();
82        p != inputs.end();
83        ++p)
84     {
85       Task_token* next_blocker = new Task_token();
86       next_blocker->add_blocker();
87       workqueue->queue(new Read_symbols(options, input_objects, symtab,
88                                         search_path, *p, this_blocker,
89                                         next_blocker));
90       this_blocker = next_blocker;
91     }
92
93   workqueue->queue(new Layout_task(options, input_objects, this_blocker));
94 }
95
96 } // end anonymous namespace.
97
98 int
99 main(int argc, char** argv)
100 {
101 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
102   setlocale (LC_MESSAGES, "");
103 #endif
104 #if defined (HAVE_SETLOCALE)
105   setlocale (LC_CTYPE, "");
106 #endif
107   bindtextdomain (PACKAGE, LOCALEDIR);
108   textdomain (PACKAGE);
109
110   gold::program_name = argv[0];
111
112   // Handle the command line options.
113   gold::Command_line command_line;
114   command_line.process(argc - 1, argv + 1);
115
116   // The work queue.
117   gold::Workqueue workqueue(command_line.options());
118
119   // The list of input objects.
120   Object_list input_objects;
121
122   // The symbol table.
123   Symbol_table symtab;
124
125   // Get the search path from the -L options.
126   Dirsearch search_path;
127   search_path.add(&workqueue, command_line.options().search_path());
128
129   // Queue up the first set of tasks.
130   queue_initial_tasks(command_line.options(), search_path,
131                       command_line.inputs(), &workqueue, &input_objects,
132                       &symtab);
133
134   // Run the main task processing loop.
135   workqueue.process();
136
137   gold::gold_exit(true);
138 }