merge from gcc
[binutils.git] / gold / gold.cc
blob02e7366a40efe41d0b838968dbc3e02fe2c83e29
1 // ld.c -- linker main function
3 #include "gold.h"
5 #include <cstdlib>
6 #include <cstdio>
7 #include <cstring>
8 #include <unistd.h>
10 #include "options.h"
11 #include "workqueue.h"
12 #include "dirsearch.h"
13 #include "readsyms.h"
14 #include "symtab.h"
15 #include "layout.h"
17 namespace gold
20 const char* program_name;
22 void
23 gold_exit(bool status)
25 exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
28 void
29 gold_fatal(const char* msg, bool perrno)
31 fprintf(stderr, "%s: ", program_name);
32 if (perrno)
33 perror(msg);
34 else
35 fprintf(stderr, "%s\n", msg);
36 gold_exit(false);
39 void
40 gold_nomem()
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);
51 void
52 gold_unreachable()
54 abort();
57 } // End namespace gold.
59 namespace
62 using namespace gold;
64 // Queue up the initial set of tasks for this link job.
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)
73 if (inputs.empty())
74 gold_fatal(_("no input files"), false);
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)
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;
93 workqueue->queue(new Layout_task(options, input_objects, this_blocker));
96 } // end anonymous namespace.
98 int
99 main(int argc, char** argv)
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);
110 gold::program_name = argv[0];
112 // Handle the command line options.
113 gold::Command_line command_line;
114 command_line.process(argc - 1, argv + 1);
116 // The work queue.
117 gold::Workqueue workqueue(command_line.options());
119 // The list of input objects.
120 Object_list input_objects;
122 // The symbol table.
123 Symbol_table symtab;
125 // Get the search path from the -L options.
126 Dirsearch search_path;
127 search_path.add(&workqueue, command_line.options().search_path());
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);
134 // Run the main task processing loop.
135 workqueue.process();
137 gold::gold_exit(true);