gold.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. // gold.cc -- main linker functions
  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 <cstdlib>
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <unistd.h>
  22. #include <algorithm>
  23. #include "libiberty.h"
  24. #include "options.h"
  25. #include "target-select.h"
  26. #include "debug.h"
  27. #include "workqueue.h"
  28. #include "dirsearch.h"
  29. #include "readsyms.h"
  30. #include "symtab.h"
  31. #include "common.h"
  32. #include "object.h"
  33. #include "layout.h"
  34. #include "reloc.h"
  35. #include "defstd.h"
  36. #include "plugin.h"
  37. #include "gc.h"
  38. #include "icf.h"
  39. #include "incremental.h"
  40. #include "timer.h"
  41. namespace gold
  42. {
  43. class Object;
  44. const char* program_name;
  45. static Task*
  46. process_incremental_input(Incremental_binary*, unsigned int, Input_objects*,
  47. Symbol_table*, Layout*, Dirsearch*, Mapfile*,
  48. Task_token*, Task_token*);
  49. void
  50. gold_exit(Exit_status status)
  51. {
  52. if (parameters != NULL
  53. && parameters->options_valid()
  54. && parameters->options().has_plugins())
  55. parameters->options().plugins()->cleanup();
  56. if (status != GOLD_OK && parameters != NULL && parameters->options_valid())
  57. unlink_if_ordinary(parameters->options().output_file_name());
  58. exit(status);
  59. }
  60. void
  61. gold_nomem()
  62. {
  63. // We are out of memory, so try hard to print a reasonable message.
  64. // Note that we don't try to translate this message, since the
  65. // translation process itself will require memory.
  66. // LEN only exists to avoid a pointless warning when write is
  67. // declared with warn_use_result, as when compiling with
  68. // -D_USE_FORTIFY on GNU/Linux. Casting to void does not appear to
  69. // work, at least not with gcc 4.3.0.
  70. ssize_t len = write(2, program_name, strlen(program_name));
  71. if (len >= 0)
  72. {
  73. const char* const s = ": out of memory\n";
  74. len = write(2, s, strlen(s));
  75. }
  76. gold_exit(GOLD_ERR);
  77. }
  78. // Handle an unreachable case.
  79. void
  80. do_gold_unreachable(const char* filename, int lineno, const char* function)
  81. {
  82. fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
  83. program_name, function, filename, lineno);
  84. gold_exit(GOLD_ERR);
  85. }
  86. // This class arranges to run the functions done in the middle of the
  87. // link. It is just a closure.
  88. class Middle_runner : public Task_function_runner
  89. {
  90. public:
  91. Middle_runner(const General_options& options,
  92. const Input_objects* input_objects,
  93. Symbol_table* symtab,
  94. Layout* layout, Mapfile* mapfile)
  95. : options_(options), input_objects_(input_objects), symtab_(symtab),
  96. layout_(layout), mapfile_(mapfile)
  97. { }
  98. void
  99. run(Workqueue*, const Task*);
  100. private:
  101. const General_options& options_;
  102. const Input_objects* input_objects_;
  103. Symbol_table* symtab_;
  104. Layout* layout_;
  105. Mapfile* mapfile_;
  106. };
  107. void
  108. Middle_runner::run(Workqueue* workqueue, const Task* task)
  109. {
  110. queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
  111. this->layout_, workqueue, this->mapfile_);
  112. }
  113. // This class arranges the tasks to process the relocs for garbage collection.
  114. class Gc_runner : public Task_function_runner
  115. {
  116. public:
  117. Gc_runner(const General_options& options,
  118. const Input_objects* input_objects,
  119. Symbol_table* symtab,
  120. Layout* layout, Mapfile* mapfile)
  121. : options_(options), input_objects_(input_objects), symtab_(symtab),
  122. layout_(layout), mapfile_(mapfile)
  123. { }
  124. void
  125. run(Workqueue*, const Task*);
  126. private:
  127. const General_options& options_;
  128. const Input_objects* input_objects_;
  129. Symbol_table* symtab_;
  130. Layout* layout_;
  131. Mapfile* mapfile_;
  132. };
  133. void
  134. Gc_runner::run(Workqueue* workqueue, const Task* task)
  135. {
  136. queue_middle_gc_tasks(this->options_, task, this->input_objects_,
  137. this->symtab_, this->layout_, workqueue,
  138. this->mapfile_);
  139. }
  140. // Queue up the initial set of tasks for this link job.
  141. void
  142. queue_initial_tasks(const General_options& options,
  143. Dirsearch& search_path,
  144. const Command_line& cmdline,
  145. Workqueue* workqueue, Input_objects* input_objects,
  146. Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
  147. {
  148. if (cmdline.begin() == cmdline.end())
  149. {
  150. bool is_ok = false;
  151. if (options.printed_version())
  152. is_ok = true;
  153. if (options.print_output_format())
  154. {
  155. print_output_format();
  156. is_ok = true;
  157. }
  158. if (is_ok)
  159. gold_exit(GOLD_OK);
  160. gold_fatal(_("no input files"));
  161. }
  162. int thread_count = options.thread_count_initial();
  163. if (thread_count == 0)
  164. thread_count = cmdline.number_of_input_files();
  165. workqueue->set_thread_count(thread_count);
  166. // For incremental links, the base output file.
  167. Incremental_binary* ibase = NULL;
  168. if (parameters->incremental_update())
  169. {
  170. Output_file* of = new Output_file(options.output_file_name());
  171. if (of->open_base_file(options.incremental_base(), true))
  172. {
  173. ibase = open_incremental_binary(of);
  174. if (ibase != NULL
  175. && ibase->check_inputs(cmdline, layout->incremental_inputs()))
  176. ibase->init_layout(layout);
  177. else
  178. {
  179. delete ibase;
  180. ibase = NULL;
  181. of->close();
  182. }
  183. }
  184. if (ibase == NULL)
  185. {
  186. if (set_parameters_incremental_full())
  187. gold_info(_("linking with --incremental-full"));
  188. else
  189. gold_fallback(_("restart link with --incremental-full"));
  190. }
  191. }
  192. // Read the input files. We have to add the symbols to the symbol
  193. // table in order. We do this by creating a separate blocker for
  194. // each input file. We associate the blocker with the following
  195. // input file, to give us a convenient place to delete it.
  196. Task_token* this_blocker = NULL;
  197. if (ibase == NULL)
  198. {
  199. // Normal link. Queue a Read_symbols task for each input file
  200. // on the command line.
  201. for (Command_line::const_iterator p = cmdline.begin();
  202. p != cmdline.end();
  203. ++p)
  204. {
  205. Task_token* next_blocker = new Task_token(true);
  206. next_blocker->add_blocker();
  207. workqueue->queue(new Read_symbols(input_objects, symtab, layout,
  208. &search_path, 0, mapfile, &*p, NULL,
  209. NULL, this_blocker, next_blocker));
  210. this_blocker = next_blocker;
  211. }
  212. }
  213. else
  214. {
  215. // Incremental update link. Process the list of input files
  216. // stored in the base file, and queue a task for each file:
  217. // a Read_symbols task for a changed file, and an Add_symbols task
  218. // for an unchanged file. We need to mark all the space used by
  219. // unchanged files before we can start any tasks running.
  220. unsigned int input_file_count = ibase->input_file_count();
  221. std::vector<Task*> tasks;
  222. tasks.reserve(input_file_count);
  223. for (unsigned int i = 0; i < input_file_count; ++i)
  224. {
  225. Task_token* next_blocker = new Task_token(true);
  226. next_blocker->add_blocker();
  227. Task* t = process_incremental_input(ibase, i, input_objects, symtab,
  228. layout, &search_path, mapfile,
  229. this_blocker, next_blocker);
  230. tasks.push_back(t);
  231. this_blocker = next_blocker;
  232. }
  233. // Now we can queue the tasks.
  234. for (unsigned int i = 0; i < tasks.size(); i++)
  235. workqueue->queue(tasks[i]);
  236. }
  237. if (options.has_plugins())
  238. {
  239. Task_token* next_blocker = new Task_token(true);
  240. next_blocker->add_blocker();
  241. workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
  242. &search_path, mapfile, this_blocker,
  243. next_blocker));
  244. this_blocker = next_blocker;
  245. }
  246. if (options.relocatable()
  247. && (options.gc_sections() || options.icf_enabled()))
  248. gold_error(_("cannot mix -r with --gc-sections or --icf"));
  249. if (options.gc_sections() || options.icf_enabled())
  250. {
  251. workqueue->queue(new Task_function(new Gc_runner(options,
  252. input_objects,
  253. symtab,
  254. layout,
  255. mapfile),
  256. this_blocker,
  257. "Task_function Gc_runner"));
  258. }
  259. else
  260. {
  261. workqueue->queue(new Task_function(new Middle_runner(options,
  262. input_objects,
  263. symtab,
  264. layout,
  265. mapfile),
  266. this_blocker,
  267. "Task_function Middle_runner"));
  268. }
  269. }
  270. // Process an incremental input file: if it is unchanged from the previous
  271. // link, return a task to add its symbols from the base file's incremental
  272. // info; if it has changed, return a normal Read_symbols task. We create a
  273. // task for every input file, if only to report the file for rebuilding the
  274. // incremental info.
  275. static Task*
  276. process_incremental_input(Incremental_binary* ibase,
  277. unsigned int input_file_index,
  278. Input_objects* input_objects,
  279. Symbol_table* symtab,
  280. Layout* layout,
  281. Dirsearch* search_path,
  282. Mapfile* mapfile,
  283. Task_token* this_blocker,
  284. Task_token* next_blocker)
  285. {
  286. const Incremental_binary::Input_reader* input_reader =
  287. ibase->get_input_reader(input_file_index);
  288. Incremental_input_type input_type = input_reader->type();
  289. // Get the input argument corresponding to this input file, matching on
  290. // the argument serial number. If the input file cannot be matched
  291. // to an existing input argument, synthesize a new one.
  292. const Input_argument* input_argument =
  293. ibase->get_input_argument(input_file_index);
  294. if (input_argument == NULL)
  295. {
  296. Input_file_argument file(input_reader->filename(),
  297. Input_file_argument::INPUT_FILE_TYPE_FILE,
  298. "", false, parameters->options());
  299. Input_argument* arg = new Input_argument(file);
  300. arg->set_script_info(ibase->get_script_info(input_file_index));
  301. input_argument = arg;
  302. }
  303. gold_debug(DEBUG_INCREMENTAL, "Incremental object: %s, type %d",
  304. input_reader->filename(), input_type);
  305. if (input_type == INCREMENTAL_INPUT_SCRIPT)
  306. {
  307. // Incremental_binary::check_inputs should have cancelled the
  308. // incremental update if the script has changed.
  309. gold_assert(!ibase->file_has_changed(input_file_index));
  310. return new Check_script(layout, ibase, input_file_index, input_reader,
  311. this_blocker, next_blocker);
  312. }
  313. if (input_type == INCREMENTAL_INPUT_ARCHIVE)
  314. {
  315. Incremental_library* lib = ibase->get_library(input_file_index);
  316. gold_assert(lib != NULL);
  317. if (lib->filename() == "/group/"
  318. || !ibase->file_has_changed(input_file_index))
  319. {
  320. // Queue a task to check that no references have been added to any
  321. // of the library's unused symbols.
  322. return new Check_library(symtab, layout, ibase, input_file_index,
  323. input_reader, this_blocker, next_blocker);
  324. }
  325. else
  326. {
  327. // Queue a Read_symbols task to process the archive normally.
  328. return new Read_symbols(input_objects, symtab, layout, search_path,
  329. 0, mapfile, input_argument, NULL, NULL,
  330. this_blocker, next_blocker);
  331. }
  332. }
  333. if (input_type == INCREMENTAL_INPUT_ARCHIVE_MEMBER)
  334. {
  335. // For archive members, check the timestamp of the containing archive.
  336. Incremental_library* lib = ibase->get_library(input_file_index);
  337. gold_assert(lib != NULL);
  338. // Process members of a --start-lib/--end-lib group as normal objects.
  339. if (lib->filename() != "/group/")
  340. {
  341. if (ibase->file_has_changed(lib->input_file_index()))
  342. {
  343. return new Read_member(input_objects, symtab, layout, mapfile,
  344. input_reader, this_blocker, next_blocker);
  345. }
  346. else
  347. {
  348. // The previous contributions from this file will be kept.
  349. // Mark the pieces of output sections contributed by this
  350. // object.
  351. ibase->reserve_layout(input_file_index);
  352. Object* obj = make_sized_incremental_object(ibase,
  353. input_file_index,
  354. input_type,
  355. input_reader);
  356. return new Add_symbols(input_objects, symtab, layout,
  357. search_path, 0, mapfile, input_argument,
  358. obj, lib, NULL, this_blocker,
  359. next_blocker);
  360. }
  361. }
  362. }
  363. // Normal object file or shared library. Check if the file has changed
  364. // since the last incremental link.
  365. if (ibase->file_has_changed(input_file_index))
  366. {
  367. return new Read_symbols(input_objects, symtab, layout, search_path, 0,
  368. mapfile, input_argument, NULL, NULL,
  369. this_blocker, next_blocker);
  370. }
  371. else
  372. {
  373. // The previous contributions from this file will be kept.
  374. // Mark the pieces of output sections contributed by this object.
  375. ibase->reserve_layout(input_file_index);
  376. Object* obj = make_sized_incremental_object(ibase,
  377. input_file_index,
  378. input_type,
  379. input_reader);
  380. return new Add_symbols(input_objects, symtab, layout, search_path, 0,
  381. mapfile, input_argument, obj, NULL, NULL,
  382. this_blocker, next_blocker);
  383. }
  384. }
  385. // Queue up a set of tasks to be done before queueing the middle set
  386. // of tasks. This is only necessary when garbage collection
  387. // (--gc-sections) of unused sections is desired. The relocs are read
  388. // and processed here early to determine the garbage sections before the
  389. // relocs can be scanned in later tasks.
  390. void
  391. queue_middle_gc_tasks(const General_options& options,
  392. const Task* ,
  393. const Input_objects* input_objects,
  394. Symbol_table* symtab,
  395. Layout* layout,
  396. Workqueue* workqueue,
  397. Mapfile* mapfile)
  398. {
  399. // Read_relocs for all the objects must be done and processed to find
  400. // unused sections before any scanning of the relocs can take place.
  401. Task_token* this_blocker = NULL;
  402. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  403. p != input_objects->relobj_end();
  404. ++p)
  405. {
  406. Task_token* next_blocker = new Task_token(true);
  407. next_blocker->add_blocker();
  408. workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
  409. next_blocker));
  410. this_blocker = next_blocker;
  411. }
  412. // If we are given only archives in input, we have no regular
  413. // objects and THIS_BLOCKER is NULL here. Create a dummy
  414. // blocker here so that we can run the middle tasks immediately.
  415. if (this_blocker == NULL)
  416. {
  417. gold_assert(input_objects->number_of_relobjs() == 0);
  418. this_blocker = new Task_token(true);
  419. }
  420. workqueue->queue(new Task_function(new Middle_runner(options,
  421. input_objects,
  422. symtab,
  423. layout,
  424. mapfile),
  425. this_blocker,
  426. "Task_function Middle_runner"));
  427. }
  428. // Queue up the middle set of tasks. These are the tasks which run
  429. // after all the input objects have been found and all the symbols
  430. // have been read, but before we lay out the output file.
  431. void
  432. queue_middle_tasks(const General_options& options,
  433. const Task* task,
  434. const Input_objects* input_objects,
  435. Symbol_table* symtab,
  436. Layout* layout,
  437. Workqueue* workqueue,
  438. Mapfile* mapfile)
  439. {
  440. Timer* timer = parameters->timer();
  441. if (timer != NULL)
  442. timer->stamp(0);
  443. // Add any symbols named with -u options to the symbol table.
  444. symtab->add_undefined_symbols_from_command_line(layout);
  445. // If garbage collection was chosen, relocs have been read and processed
  446. // at this point by pre_middle_tasks. Layout can then be done for all
  447. // objects.
  448. if (parameters->options().gc_sections())
  449. {
  450. // Find the start symbol if any.
  451. Symbol* sym = symtab->lookup(parameters->entry());
  452. if (sym != NULL)
  453. symtab->gc_mark_symbol(sym);
  454. sym = symtab->lookup(parameters->options().init());
  455. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  456. symtab->gc_mark_symbol(sym);
  457. sym = symtab->lookup(parameters->options().fini());
  458. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  459. symtab->gc_mark_symbol(sym);
  460. // Symbols named with -u should not be considered garbage.
  461. symtab->gc_mark_undef_symbols(layout);
  462. gold_assert(symtab->gc() != NULL);
  463. // Do a transitive closure on all references to determine the worklist.
  464. symtab->gc()->do_transitive_closure();
  465. }
  466. // If identical code folding (--icf) is chosen it makes sense to do it
  467. // only after garbage collection (--gc-sections) as we do not want to
  468. // be folding sections that will be garbage.
  469. if (parameters->options().icf_enabled())
  470. {
  471. symtab->icf()->find_identical_sections(input_objects, symtab);
  472. }
  473. // Call Object::layout for the second time to determine the
  474. // output_sections for all referenced input sections. When
  475. // --gc-sections or --icf is turned on, or when certain input
  476. // sections have to be mapped to unique segments, Object::layout
  477. // is called twice. It is called the first time when symbols
  478. // are added.
  479. if (parameters->options().gc_sections()
  480. || parameters->options().icf_enabled()
  481. || layout->is_unique_segment_for_sections_specified())
  482. {
  483. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  484. p != input_objects->relobj_end();
  485. ++p)
  486. {
  487. Task_lock_obj<Object> tlo(task, *p);
  488. (*p)->layout(symtab, layout, NULL);
  489. }
  490. }
  491. // Layout deferred objects due to plugins.
  492. if (parameters->options().has_plugins())
  493. {
  494. Plugin_manager* plugins = parameters->options().plugins();
  495. gold_assert(plugins != NULL);
  496. plugins->layout_deferred_objects();
  497. }
  498. // Finalize the .eh_frame section.
  499. layout->finalize_eh_frame_section();
  500. /* If plugins have specified a section order, re-arrange input sections
  501. according to a specified section order. If --section-ordering-file is
  502. also specified, do not do anything here. */
  503. if (parameters->options().has_plugins()
  504. && layout->is_section_ordering_specified()
  505. && !parameters->options().section_ordering_file ())
  506. {
  507. for (Layout::Section_list::const_iterator p
  508. = layout->section_list().begin();
  509. p != layout->section_list().end();
  510. ++p)
  511. (*p)->update_section_layout(layout->get_section_order_map());
  512. }
  513. if (parameters->options().gc_sections()
  514. || parameters->options().icf_enabled())
  515. {
  516. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  517. p != input_objects->relobj_end();
  518. ++p)
  519. {
  520. // Update the value of output_section stored in rd.
  521. Read_relocs_data* rd = (*p)->get_relocs_data();
  522. for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
  523. q != rd->relocs.end();
  524. ++q)
  525. {
  526. q->output_section = (*p)->output_section(q->data_shndx);
  527. q->needs_special_offset_handling =
  528. (*p)->is_output_section_offset_invalid(q->data_shndx);
  529. }
  530. }
  531. }
  532. // We have to support the case of not seeing any input objects, and
  533. // generate an empty file. Existing builds depend on being able to
  534. // pass an empty archive to the linker and get an empty object file
  535. // out. In order to do this we need to use a default target.
  536. if (input_objects->number_of_input_objects() == 0
  537. && layout->incremental_base() == NULL)
  538. parameters_force_valid_target();
  539. int thread_count = options.thread_count_middle();
  540. if (thread_count == 0)
  541. thread_count = std::max(2, input_objects->number_of_input_objects());
  542. workqueue->set_thread_count(thread_count);
  543. // Now we have seen all the input files.
  544. const bool doing_static_link =
  545. (!input_objects->any_dynamic()
  546. && !parameters->options().output_is_position_independent());
  547. set_parameters_doing_static_link(doing_static_link);
  548. if (!doing_static_link && options.is_static())
  549. {
  550. // We print out just the first .so we see; there may be others.
  551. gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
  552. gold_error(_("cannot mix -static with dynamic object %s"),
  553. (*input_objects->dynobj_begin())->name().c_str());
  554. }
  555. if (!doing_static_link && parameters->options().relocatable())
  556. gold_fatal(_("cannot mix -r with dynamic object %s"),
  557. (*input_objects->dynobj_begin())->name().c_str());
  558. if (!doing_static_link
  559. && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
  560. gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
  561. (*input_objects->dynobj_begin())->name().c_str());
  562. if (parameters->options().relocatable())
  563. {
  564. Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  565. if (p != input_objects->relobj_end())
  566. {
  567. bool uses_split_stack = (*p)->uses_split_stack();
  568. for (++p; p != input_objects->relobj_end(); ++p)
  569. {
  570. if ((*p)->uses_split_stack() != uses_split_stack)
  571. gold_fatal(_("cannot mix split-stack '%s' and "
  572. "non-split-stack '%s' when using -r"),
  573. (*input_objects->relobj_begin())->name().c_str(),
  574. (*p)->name().c_str());
  575. }
  576. }
  577. }
  578. // For incremental updates, record the existing GOT and PLT entries,
  579. // and the COPY relocations.
  580. if (parameters->incremental_update())
  581. {
  582. Incremental_binary* ibase = layout->incremental_base();
  583. ibase->process_got_plt(symtab, layout);
  584. ibase->emit_copy_relocs(symtab);
  585. }
  586. if (is_debugging_enabled(DEBUG_SCRIPT))
  587. layout->script_options()->print(stderr);
  588. // For each dynamic object, record whether we've seen all the
  589. // dynamic objects that it depends upon.
  590. input_objects->check_dynamic_dependencies();
  591. // Do the --no-undefined-version check.
  592. if (!parameters->options().undefined_version())
  593. {
  594. Script_options* so = layout->script_options();
  595. so->version_script_info()->check_unmatched_names(symtab);
  596. }
  597. // Create any automatic note sections.
  598. layout->create_notes();
  599. // Create any output sections required by any linker script.
  600. layout->create_script_sections();
  601. // Define some sections and symbols needed for a dynamic link. This
  602. // handles some cases we want to see before we read the relocs.
  603. layout->create_initial_dynamic_sections(symtab);
  604. // Define symbols from any linker scripts.
  605. layout->define_script_symbols(symtab);
  606. // TODO(csilvers): figure out a more principled way to get the target
  607. Target* target = const_cast<Target*>(&parameters->target());
  608. // Attach sections to segments.
  609. layout->attach_sections_to_segments(target);
  610. if (!parameters->options().relocatable())
  611. {
  612. // Predefine standard symbols.
  613. define_standard_symbols(symtab, layout);
  614. // Define __start and __stop symbols for output sections where
  615. // appropriate.
  616. layout->define_section_symbols(symtab);
  617. // Define target-specific symbols.
  618. target->define_standard_symbols(symtab, layout);
  619. }
  620. // Make sure we have symbols for any required group signatures.
  621. layout->define_group_signatures(symtab);
  622. Task_token* this_blocker = NULL;
  623. // Allocate common symbols. We use a blocker to run this before the
  624. // Scan_relocs tasks, because it writes to the symbol table just as
  625. // they do.
  626. if (parameters->options().define_common())
  627. {
  628. this_blocker = new Task_token(true);
  629. this_blocker->add_blocker();
  630. workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
  631. this_blocker));
  632. }
  633. // If doing garbage collection, the relocations have already been read.
  634. // Otherwise, read and scan the relocations.
  635. if (parameters->options().gc_sections()
  636. || parameters->options().icf_enabled())
  637. {
  638. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  639. p != input_objects->relobj_end();
  640. ++p)
  641. {
  642. Task_token* next_blocker = new Task_token(true);
  643. next_blocker->add_blocker();
  644. workqueue->queue(new Scan_relocs(symtab, layout, *p,
  645. (*p)->get_relocs_data(),
  646. this_blocker, next_blocker));
  647. this_blocker = next_blocker;
  648. }
  649. }
  650. else
  651. {
  652. // Read the relocations of the input files. We do this to find
  653. // which symbols are used by relocations which require a GOT and/or
  654. // a PLT entry, or a COPY reloc. When we implement garbage
  655. // collection we will do it here by reading the relocations in a
  656. // breadth first search by references.
  657. //
  658. // We could also read the relocations during the first pass, and
  659. // mark symbols at that time. That is how the old GNU linker works.
  660. // Doing that is more complex, since we may later decide to discard
  661. // some of the sections, and thus change our minds about the types
  662. // of references made to the symbols.
  663. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  664. p != input_objects->relobj_end();
  665. ++p)
  666. {
  667. Task_token* next_blocker = new Task_token(true);
  668. next_blocker->add_blocker();
  669. workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
  670. next_blocker));
  671. this_blocker = next_blocker;
  672. }
  673. }
  674. if (this_blocker == NULL)
  675. {
  676. if (input_objects->number_of_relobjs() == 0)
  677. {
  678. // If we are given only archives in input, we have no regular
  679. // objects and THIS_BLOCKER is NULL here. Create a dummy
  680. // blocker here so that we can run the layout task immediately.
  681. this_blocker = new Task_token(true);
  682. }
  683. else
  684. {
  685. // If we failed to open any input files, it's possible for
  686. // THIS_BLOCKER to be NULL here. There's no real point in
  687. // continuing if that happens.
  688. gold_assert(parameters->errors()->error_count() > 0);
  689. gold_exit(GOLD_ERR);
  690. }
  691. }
  692. // When all those tasks are complete, we can start laying out the
  693. // output file.
  694. workqueue->queue(new Task_function(new Layout_task_runner(options,
  695. input_objects,
  696. symtab,
  697. target,
  698. layout,
  699. mapfile),
  700. this_blocker,
  701. "Task_function Layout_task_runner"));
  702. }
  703. // Queue up the final set of tasks. This is called at the end of
  704. // Layout_task.
  705. void
  706. queue_final_tasks(const General_options& options,
  707. const Input_objects* input_objects,
  708. const Symbol_table* symtab,
  709. Layout* layout,
  710. Workqueue* workqueue,
  711. Output_file* of)
  712. {
  713. Timer* timer = parameters->timer();
  714. if (timer != NULL)
  715. timer->stamp(1);
  716. int thread_count = options.thread_count_final();
  717. if (thread_count == 0)
  718. thread_count = std::max(2, input_objects->number_of_input_objects());
  719. workqueue->set_thread_count(thread_count);
  720. bool any_postprocessing_sections = layout->any_postprocessing_sections();
  721. // Use a blocker to wait until all the input sections have been
  722. // written out.
  723. Task_token* input_sections_blocker = NULL;
  724. if (!any_postprocessing_sections)
  725. {
  726. input_sections_blocker = new Task_token(true);
  727. // Write_symbols_task, Relocate_tasks.
  728. input_sections_blocker->add_blocker();
  729. input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
  730. }
  731. // Use a blocker to block any objects which have to wait for the
  732. // output sections to complete before they can apply relocations.
  733. Task_token* output_sections_blocker = new Task_token(true);
  734. output_sections_blocker->add_blocker();
  735. // Use a blocker to block the final cleanup task.
  736. Task_token* final_blocker = new Task_token(true);
  737. // Write_symbols_task, Write_sections_task, Write_data_task,
  738. // Relocate_tasks.
  739. final_blocker->add_blockers(3);
  740. final_blocker->add_blockers(input_objects->number_of_relobjs());
  741. if (!any_postprocessing_sections)
  742. final_blocker->add_blocker();
  743. // Queue a task to write out the symbol table.
  744. workqueue->queue(new Write_symbols_task(layout,
  745. symtab,
  746. input_objects,
  747. layout->sympool(),
  748. layout->dynpool(),
  749. of,
  750. final_blocker));
  751. // Queue a task to write out the output sections.
  752. workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
  753. input_sections_blocker,
  754. final_blocker));
  755. // Queue a task to write out everything else.
  756. workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
  757. // Queue a task for each input object to relocate the sections and
  758. // write out the local symbols.
  759. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  760. p != input_objects->relobj_end();
  761. ++p)
  762. workqueue->queue(new Relocate_task(symtab, layout, *p, of,
  763. input_sections_blocker,
  764. output_sections_blocker,
  765. final_blocker));
  766. // Queue a task to write out the output sections which depend on
  767. // input sections. If there are any sections which require
  768. // postprocessing, then we need to do this last, since it may resize
  769. // the output file.
  770. if (!any_postprocessing_sections)
  771. {
  772. Task* t = new Write_after_input_sections_task(layout, of,
  773. input_sections_blocker,
  774. final_blocker);
  775. workqueue->queue(t);
  776. }
  777. else
  778. {
  779. Task_token* new_final_blocker = new Task_token(true);
  780. new_final_blocker->add_blocker();
  781. Task* t = new Write_after_input_sections_task(layout, of,
  782. final_blocker,
  783. new_final_blocker);
  784. workqueue->queue(t);
  785. final_blocker = new_final_blocker;
  786. }
  787. // Create tasks for tree-style build ID computation, if necessary.
  788. if (strcmp(options.build_id(), "tree") == 0)
  789. {
  790. // Queue a task to compute the build id. This will be blocked by
  791. // FINAL_BLOCKER, and will in turn schedule the task to close
  792. // the output file.
  793. workqueue->queue(new Task_function(new Build_id_task_runner(&options,
  794. layout,
  795. of),
  796. final_blocker,
  797. "Task_function Build_id_task_runner"));
  798. }
  799. else
  800. {
  801. // Queue a task to close the output file. This will be blocked by
  802. // FINAL_BLOCKER.
  803. workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
  804. of, NULL, 0),
  805. final_blocker,
  806. "Task_function Close_task_runner"));
  807. }
  808. }
  809. } // End namespace gold.