main.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // main.cc -- gold main function.
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstdio>
  19. #include <cstring>
  20. #ifdef HAVE_MALLINFO
  21. #include <malloc.h>
  22. #endif
  23. #include "libiberty.h"
  24. #include "script.h"
  25. #include "options.h"
  26. #include "target-select.h"
  27. #include "parameters.h"
  28. #include "errors.h"
  29. #include "mapfile.h"
  30. #include "dirsearch.h"
  31. #include "workqueue.h"
  32. #include "object.h"
  33. #include "archive.h"
  34. #include "symtab.h"
  35. #include "layout.h"
  36. #include "plugin.h"
  37. #include "gc.h"
  38. #include "icf.h"
  39. #include "incremental.h"
  40. #include "gdb-index.h"
  41. #include "timer.h"
  42. using namespace gold;
  43. // This function emits the commandline to a hard-coded file in temp.
  44. // This is useful for debugging since ld is typically invoked by gcc,
  45. // so its commandline is not always easy to extract. You should be
  46. // able to run 'gcc -B... foo.o -o foo' to invoke this linker the
  47. // first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
  48. // runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
  49. // (or whatever value the environment variable GDB is set to), for
  50. // even easier debugging. Since this is a debugging-only tool, and
  51. // creates files, it is only turned on when the user explicitly asks
  52. // for it, by compiling with -DDEBUG. Do not do this for release
  53. // versions of the linker!
  54. #ifdef DEBUG
  55. #include <stdio.h>
  56. #include <sys/stat.h> // for chmod()
  57. static std::string
  58. collect_argv(int argc, char** argv)
  59. {
  60. // This is used by write_debug_script(), which wants the unedited argv.
  61. std::string args;
  62. for (int i = 0; i < argc; ++i)
  63. {
  64. args.append(" '");
  65. // Now append argv[i], but with all single-quotes escaped
  66. const char* argpos = argv[i];
  67. while (1)
  68. {
  69. const int len = strcspn(argpos, "'");
  70. args.append(argpos, len);
  71. if (argpos[len] == '\0')
  72. break;
  73. args.append("'\"'\"'");
  74. argpos += len + 1;
  75. }
  76. args.append("'");
  77. }
  78. return args;
  79. }
  80. static void
  81. write_debug_script(std::string filename_str,
  82. const char* argv_0, const char* args)
  83. {
  84. size_t slash = filename_str.rfind('/');
  85. if (slash != std::string::npos)
  86. filename_str = filename_str.c_str() + slash + 1;
  87. filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
  88. const char* filename = filename_str.c_str();
  89. FILE* fp = fopen(filename, "w");
  90. if (fp)
  91. {
  92. fprintf(fp, "[ \"$1\" = debug ]"
  93. " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
  94. " && shift\n",
  95. argv_0);
  96. fprintf(fp, "$PREFIX%s $*\n", args);
  97. fclose(fp);
  98. chmod(filename, 0755);
  99. }
  100. else
  101. filename = "[none]";
  102. fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
  103. fflush(stderr);
  104. }
  105. #else // !defined(DEBUG)
  106. static inline std::string
  107. collect_argv(int, char**)
  108. {
  109. return "";
  110. }
  111. static inline void
  112. write_debug_script(std::string, const char*, const char*)
  113. {
  114. }
  115. #endif // !defined(DEBUG)
  116. int
  117. main(int argc, char** argv)
  118. {
  119. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  120. setlocale(LC_MESSAGES, "");
  121. #endif
  122. #if defined (HAVE_SETLOCALE)
  123. setlocale(LC_CTYPE, "");
  124. #endif
  125. bindtextdomain(PACKAGE, LOCALEDIR);
  126. textdomain(PACKAGE);
  127. program_name = argv[0];
  128. // In libiberty; expands @filename to the args in "filename".
  129. expandargv(&argc, &argv);
  130. // This is used by write_debug_script(), which wants the unedited argv.
  131. std::string args = collect_argv(argc, argv);
  132. Errors errors(program_name);
  133. // Initialize the global parameters, to let random code get to the
  134. // errors object.
  135. set_parameters_errors(&errors);
  136. // Handle the command line options.
  137. Command_line command_line;
  138. command_line.process(argc - 1, const_cast<const char**>(argv + 1));
  139. Timer timer;
  140. if (command_line.options().stats())
  141. {
  142. timer.start();
  143. set_parameters_timer(&timer);
  144. }
  145. // Store some options in the globally accessible parameters.
  146. set_parameters_options(&command_line.options());
  147. // Do this as early as possible (since it prints a welcome message).
  148. write_debug_script(command_line.options().output_file_name(),
  149. program_name, args.c_str());
  150. // If the user asked for a map file, open it.
  151. Mapfile* mapfile = NULL;
  152. if (command_line.options().user_set_Map())
  153. {
  154. mapfile = new Mapfile();
  155. if (!mapfile->open(command_line.options().Map()))
  156. {
  157. delete mapfile;
  158. mapfile = NULL;
  159. }
  160. }
  161. // The GNU linker ignores version scripts when generating
  162. // relocatable output. If we are not compatible, then we break the
  163. // Linux kernel build, which uses a linker script with -r which must
  164. // not force symbols to be local. It would actually be useful to
  165. // permit symbols to be forced local with -r, though, as it would
  166. // permit some linker optimizations. Perhaps we need yet another
  167. // option to control this. FIXME.
  168. if (parameters->options().relocatable())
  169. command_line.script_options().version_script_info()->clear();
  170. // The work queue.
  171. Workqueue workqueue(command_line.options());
  172. // The list of input objects.
  173. Input_objects input_objects;
  174. // The Garbage Collection (GC, --gc-sections) Object.
  175. Garbage_collection gc;
  176. // The Identical Code Folding (ICF, --icf) Object.
  177. Icf icf;
  178. // The symbol table. We're going to guess here how many symbols
  179. // we're going to see based on the number of input files. Even when
  180. // this is off, it means at worst we don't quite optimize hashtable
  181. // resizing as well as we could have (perhaps using more memory).
  182. Symbol_table symtab(command_line.number_of_input_files() * 1024,
  183. command_line.version_script());
  184. if (parameters->options().gc_sections())
  185. symtab.set_gc(&gc);
  186. if (parameters->options().icf_enabled())
  187. symtab.set_icf(&icf);
  188. // The layout object.
  189. Layout layout(command_line.number_of_input_files(),
  190. &command_line.script_options());
  191. if (layout.incremental_inputs() != NULL)
  192. layout.incremental_inputs()->report_command_line(argc, argv);
  193. if (parameters->options().section_ordering_file())
  194. layout.read_layout_from_file();
  195. // Load plugin libraries.
  196. if (command_line.options().has_plugins())
  197. command_line.options().plugins()->load_plugins(&layout);
  198. // Get the search path from the -L options.
  199. Dirsearch search_path;
  200. search_path.initialize(&workqueue, &command_line.options().library_path());
  201. // Queue up the first set of tasks.
  202. queue_initial_tasks(command_line.options(), search_path,
  203. command_line, &workqueue, &input_objects,
  204. &symtab, &layout, mapfile);
  205. // Run the main task processing loop.
  206. workqueue.process(0);
  207. if (command_line.options().print_output_format())
  208. print_output_format();
  209. if (command_line.options().stats())
  210. {
  211. timer.stamp(2);
  212. Timer::TimeStats elapsed = timer.get_pass_time(0);
  213. fprintf(stderr,
  214. _("%s: initial tasks run time: " \
  215. "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
  216. program_name,
  217. elapsed.user / 1000, (elapsed.user % 1000) * 1000,
  218. elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
  219. elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
  220. elapsed = timer.get_pass_time(1);
  221. fprintf(stderr,
  222. _("%s: middle tasks run time: " \
  223. "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
  224. program_name,
  225. elapsed.user / 1000, (elapsed.user % 1000) * 1000,
  226. elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
  227. elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
  228. elapsed = timer.get_pass_time(2);
  229. fprintf(stderr,
  230. _("%s: final tasks run time: " \
  231. "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
  232. program_name,
  233. elapsed.user / 1000, (elapsed.user % 1000) * 1000,
  234. elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
  235. elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
  236. elapsed = timer.get_elapsed_time();
  237. fprintf(stderr,
  238. _("%s: total run time: " \
  239. "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
  240. program_name,
  241. elapsed.user / 1000, (elapsed.user % 1000) * 1000,
  242. elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
  243. elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
  244. #ifdef HAVE_MALLINFO
  245. struct mallinfo m = mallinfo();
  246. fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
  247. program_name, m.arena);
  248. #endif
  249. File_read::print_stats();
  250. Archive::print_stats();
  251. Lib_group::print_stats();
  252. fprintf(stderr, _("%s: output file size: %lld bytes\n"),
  253. program_name, static_cast<long long>(layout.output_file_size()));
  254. symtab.print_stats();
  255. layout.print_stats();
  256. Gdb_index::print_stats();
  257. Free_list::print_stats();
  258. }
  259. // Issue defined symbol report.
  260. if (command_line.options().user_set_print_symbol_counts())
  261. input_objects.print_symbol_counts(&symtab);
  262. // Output cross reference table.
  263. if (command_line.options().cref())
  264. input_objects.print_cref(&symtab,
  265. mapfile == NULL ? stdout : mapfile->file());
  266. if (mapfile != NULL)
  267. mapfile->close();
  268. if (parameters->options().fatal_warnings()
  269. && errors.warning_count() > 0
  270. && errors.error_count() == 0)
  271. gold_error("treating warnings as errors");
  272. // If the user used --noinhibit-exec, we force the exit status to be
  273. // successful. This is compatible with GNU ld.
  274. gold_exit((errors.error_count() == 0
  275. || parameters->options().noinhibit_exec())
  276. ? GOLD_OK
  277. : GOLD_ERR);
  278. }