Finished 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, symtab,
95                                    this_blocker));
96 }
97
98 } // end anonymous namespace.
99
100 int
101 main(int argc, char** argv)
102 {
103 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
104   setlocale (LC_MESSAGES, "");
105 #endif
106 #if defined (HAVE_SETLOCALE)
107   setlocale (LC_CTYPE, "");
108 #endif
109   bindtextdomain (PACKAGE, LOCALEDIR);
110   textdomain (PACKAGE);
111
112   gold::program_name = argv[0];
113
114   // Handle the command line options.
115   gold::Command_line command_line;
116   command_line.process(argc - 1, argv + 1);
117
118   // The work queue.
119   gold::Workqueue workqueue(command_line.options());
120
121   // The list of input objects.
122   Input_objects input_objects;
123
124   // The symbol table.
125   Symbol_table symtab;
126
127   // Get the search path from the -L options.
128   Dirsearch search_path;
129   search_path.add(&workqueue, command_line.options().search_path());
130
131   // Queue up the first set of tasks.
132   queue_initial_tasks(command_line.options(), search_path,
133                       command_line.inputs(), &workqueue, &input_objects,
134                       &symtab);
135
136   // Run the main task processing loop.
137   workqueue.process();
138
139   gold::gold_exit(true);
140 }