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