gold.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // gold.cc -- main linker functions
  2. // Copyright (C) 2006-2022 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.number_of_input_files() == 0)
  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. // We have to support the case of not seeing any input objects, and
  444. // generate an empty file. Existing builds depend on being able to
  445. // pass an empty archive to the linker and get an empty object file
  446. // out. In order to do this we need to use a default target.
  447. if (input_objects->number_of_input_objects() == 0
  448. && layout->incremental_base() == NULL)
  449. parameters_force_valid_target();
  450. // Add any symbols named with -u options to the symbol table.
  451. symtab->add_undefined_symbols_from_command_line(layout);
  452. // If garbage collection was chosen, relocs have been read and processed
  453. // at this point by pre_middle_tasks. Layout can then be done for all
  454. // objects.
  455. if (parameters->options().gc_sections())
  456. {
  457. // Find the start symbol if any.
  458. Symbol* sym = symtab->lookup(parameters->entry());
  459. if (sym != NULL)
  460. symtab->gc_mark_symbol(sym);
  461. sym = symtab->lookup(parameters->options().init());
  462. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  463. symtab->gc_mark_symbol(sym);
  464. sym = symtab->lookup(parameters->options().fini());
  465. if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
  466. symtab->gc_mark_symbol(sym);
  467. // Symbols named with -u should not be considered garbage.
  468. symtab->gc_mark_undef_symbols(layout);
  469. gold_assert(symtab->gc() != NULL);
  470. // Do a transitive closure on all references to determine the worklist.
  471. symtab->gc()->do_transitive_closure();
  472. }
  473. // If identical code folding (--icf) is chosen it makes sense to do it
  474. // only after garbage collection (--gc-sections) as we do not want to
  475. // be folding sections that will be garbage.
  476. if (parameters->options().icf_enabled())
  477. {
  478. symtab->icf()->find_identical_sections(input_objects, symtab);
  479. }
  480. // Call Object::layout for the second time to determine the
  481. // output_sections for all referenced input sections. When
  482. // --gc-sections or --icf is turned on, or when certain input
  483. // sections have to be mapped to unique segments, Object::layout
  484. // is called twice. It is called the first time when symbols
  485. // are added.
  486. if (parameters->options().gc_sections()
  487. || parameters->options().icf_enabled()
  488. || layout->is_unique_segment_for_sections_specified())
  489. {
  490. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  491. p != input_objects->relobj_end();
  492. ++p)
  493. {
  494. Task_lock_obj<Object> tlo(task, *p);
  495. (*p)->layout(symtab, layout, NULL);
  496. }
  497. }
  498. // Layout deferred objects due to plugins.
  499. if (parameters->options().has_plugins())
  500. {
  501. Plugin_manager* plugins = parameters->options().plugins();
  502. gold_assert(plugins != NULL);
  503. plugins->layout_deferred_objects();
  504. }
  505. // Finalize the .eh_frame section.
  506. layout->finalize_eh_frame_section();
  507. /* If plugins have specified a section order, re-arrange input sections
  508. according to a specified section order. If --section-ordering-file is
  509. also specified, do not do anything here. */
  510. if (parameters->options().has_plugins()
  511. && layout->is_section_ordering_specified()
  512. && !parameters->options().section_ordering_file ())
  513. {
  514. for (Layout::Section_list::const_iterator p
  515. = layout->section_list().begin();
  516. p != layout->section_list().end();
  517. ++p)
  518. (*p)->update_section_layout(layout->get_section_order_map());
  519. }
  520. if (parameters->options().gc_sections()
  521. || parameters->options().icf_enabled())
  522. {
  523. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  524. p != input_objects->relobj_end();
  525. ++p)
  526. {
  527. // Update the value of output_section stored in rd.
  528. Read_relocs_data* rd = (*p)->get_relocs_data();
  529. for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
  530. q != rd->relocs.end();
  531. ++q)
  532. {
  533. q->output_section = (*p)->output_section(q->data_shndx);
  534. q->needs_special_offset_handling =
  535. (*p)->is_output_section_offset_invalid(q->data_shndx);
  536. }
  537. }
  538. }
  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. {
  572. const char *name1
  573. = (*input_objects->relobj_begin())->name().c_str();
  574. const char *name2 = (*p)->name().c_str();
  575. const char *name_split = uses_split_stack ? name1 : name2;
  576. const char *name_nosplit = uses_split_stack ? name2 : name1;
  577. gold_fatal(_("cannot mix split-stack '%s' and "
  578. "non-split-stack '%s' when using -r"),
  579. name_split, name_nosplit);
  580. }
  581. }
  582. }
  583. }
  584. // For incremental updates, record the existing GOT and PLT entries,
  585. // and the COPY relocations.
  586. if (parameters->incremental_update())
  587. {
  588. Incremental_binary* ibase = layout->incremental_base();
  589. ibase->process_got_plt(symtab, layout);
  590. ibase->emit_copy_relocs(symtab);
  591. }
  592. if (is_debugging_enabled(DEBUG_SCRIPT))
  593. layout->script_options()->print(stderr);
  594. // For each dynamic object, record whether we've seen all the
  595. // dynamic objects that it depends upon.
  596. input_objects->check_dynamic_dependencies();
  597. // Do the --no-undefined-version check.
  598. if (!parameters->options().undefined_version())
  599. {
  600. Script_options* so = layout->script_options();
  601. so->version_script_info()->check_unmatched_names(symtab);
  602. }
  603. // Create any automatic note sections.
  604. layout->create_notes();
  605. // Create any output sections required by any linker script.
  606. layout->create_script_sections();
  607. // Define some sections and symbols needed for a dynamic link. This
  608. // handles some cases we want to see before we read the relocs.
  609. layout->create_initial_dynamic_sections(symtab);
  610. // Define symbols from any linker scripts.
  611. layout->define_script_symbols(symtab);
  612. // TODO(csilvers): figure out a more principled way to get the target
  613. Target* target = const_cast<Target*>(&parameters->target());
  614. // Attach sections to segments.
  615. layout->attach_sections_to_segments(target);
  616. if (!parameters->options().relocatable())
  617. {
  618. // Predefine standard symbols.
  619. define_standard_symbols(symtab, layout);
  620. // Define __start and __stop symbols for output sections where
  621. // appropriate.
  622. layout->define_section_symbols(symtab);
  623. // Define target-specific symbols.
  624. target->define_standard_symbols(symtab, layout);
  625. }
  626. // Make sure we have symbols for any required group signatures.
  627. layout->define_group_signatures(symtab);
  628. Task_token* this_blocker = NULL;
  629. // Allocate common symbols. We use a blocker to run this before the
  630. // Scan_relocs tasks, because it writes to the symbol table just as
  631. // they do.
  632. if (parameters->options().define_common())
  633. {
  634. this_blocker = new Task_token(true);
  635. this_blocker->add_blocker();
  636. workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
  637. this_blocker));
  638. }
  639. // If doing garbage collection, the relocations have already been read.
  640. // Otherwise, read and scan the relocations.
  641. if (parameters->options().gc_sections()
  642. || parameters->options().icf_enabled())
  643. {
  644. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  645. p != input_objects->relobj_end();
  646. ++p)
  647. {
  648. Task_token* next_blocker = new Task_token(true);
  649. next_blocker->add_blocker();
  650. workqueue->queue(new Scan_relocs(symtab, layout, *p,
  651. (*p)->get_relocs_data(),
  652. this_blocker, next_blocker));
  653. this_blocker = next_blocker;
  654. }
  655. }
  656. else
  657. {
  658. // Read the relocations of the input files. We do this to find
  659. // which symbols are used by relocations which require a GOT and/or
  660. // a PLT entry, or a COPY reloc. When we implement garbage
  661. // collection we will do it here by reading the relocations in a
  662. // breadth first search by references.
  663. //
  664. // We could also read the relocations during the first pass, and
  665. // mark symbols at that time. That is how the old GNU linker works.
  666. // Doing that is more complex, since we may later decide to discard
  667. // some of the sections, and thus change our minds about the types
  668. // of references made to the symbols.
  669. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  670. p != input_objects->relobj_end();
  671. ++p)
  672. {
  673. Task_token* next_blocker = new Task_token(true);
  674. next_blocker->add_blocker();
  675. workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
  676. next_blocker));
  677. this_blocker = next_blocker;
  678. }
  679. }
  680. if (this_blocker == NULL)
  681. {
  682. if (input_objects->number_of_relobjs() == 0)
  683. {
  684. // If we are given only archives in input, we have no regular
  685. // objects and THIS_BLOCKER is NULL here. Create a dummy
  686. // blocker here so that we can run the layout task immediately.
  687. this_blocker = new Task_token(true);
  688. }
  689. else
  690. {
  691. // If we failed to open any input files, it's possible for
  692. // THIS_BLOCKER to be NULL here. There's no real point in
  693. // continuing if that happens.
  694. gold_assert(parameters->errors()->error_count() > 0);
  695. gold_exit(GOLD_ERR);
  696. }
  697. }
  698. // When all those tasks are complete, we can start laying out the
  699. // output file.
  700. workqueue->queue(new Task_function(new Layout_task_runner(options,
  701. input_objects,
  702. symtab,
  703. target,
  704. layout,
  705. mapfile),
  706. this_blocker,
  707. "Task_function Layout_task_runner"));
  708. }
  709. // Queue up the final set of tasks. This is called at the end of
  710. // Layout_task.
  711. void
  712. queue_final_tasks(const General_options& options,
  713. const Input_objects* input_objects,
  714. const Symbol_table* symtab,
  715. Layout* layout,
  716. Workqueue* workqueue,
  717. Output_file* of)
  718. {
  719. Timer* timer = parameters->timer();
  720. if (timer != NULL)
  721. timer->stamp(1);
  722. int thread_count = options.thread_count_final();
  723. if (thread_count == 0)
  724. thread_count = std::max(2, input_objects->number_of_input_objects());
  725. workqueue->set_thread_count(thread_count);
  726. bool any_postprocessing_sections = layout->any_postprocessing_sections();
  727. // Use a blocker to wait until all the input sections have been
  728. // written out.
  729. Task_token* input_sections_blocker = NULL;
  730. if (!any_postprocessing_sections)
  731. {
  732. input_sections_blocker = new Task_token(true);
  733. // Write_symbols_task, Relocate_tasks.
  734. input_sections_blocker->add_blocker();
  735. input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
  736. }
  737. // Use a blocker to block any objects which have to wait for the
  738. // output sections to complete before they can apply relocations.
  739. Task_token* output_sections_blocker = new Task_token(true);
  740. output_sections_blocker->add_blocker();
  741. // Use a blocker to block the final cleanup task.
  742. Task_token* final_blocker = new Task_token(true);
  743. // Write_symbols_task, Write_sections_task, Write_data_task,
  744. // Relocate_tasks.
  745. final_blocker->add_blockers(3);
  746. final_blocker->add_blockers(input_objects->number_of_relobjs());
  747. if (!any_postprocessing_sections)
  748. final_blocker->add_blocker();
  749. // Queue a task to write out the symbol table.
  750. workqueue->queue(new Write_symbols_task(layout,
  751. symtab,
  752. input_objects,
  753. layout->sympool(),
  754. layout->dynpool(),
  755. of,
  756. final_blocker));
  757. // Queue a task to write out the output sections.
  758. workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
  759. input_sections_blocker,
  760. final_blocker));
  761. // Queue a task to write out everything else.
  762. workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
  763. // Queue a task for each input object to relocate the sections and
  764. // write out the local symbols.
  765. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  766. p != input_objects->relobj_end();
  767. ++p)
  768. workqueue->queue(new Relocate_task(symtab, layout, *p, of,
  769. input_sections_blocker,
  770. output_sections_blocker,
  771. final_blocker));
  772. // Queue a task to write out the output sections which depend on
  773. // input sections. If there are any sections which require
  774. // postprocessing, then we need to do this last, since it may resize
  775. // the output file.
  776. if (!any_postprocessing_sections)
  777. {
  778. Task* t = new Write_after_input_sections_task(layout, of,
  779. input_sections_blocker,
  780. final_blocker);
  781. workqueue->queue(t);
  782. }
  783. else
  784. {
  785. Task_token* new_final_blocker = new Task_token(true);
  786. new_final_blocker->add_blocker();
  787. Task* t = new Write_after_input_sections_task(layout, of,
  788. final_blocker,
  789. new_final_blocker);
  790. workqueue->queue(t);
  791. final_blocker = new_final_blocker;
  792. }
  793. // Create tasks for tree-style build ID computation, if necessary.
  794. if (strcmp(options.build_id(), "tree") == 0)
  795. {
  796. // Queue a task to compute the build id. This will be blocked by
  797. // FINAL_BLOCKER, and will in turn schedule the task to close
  798. // the output file.
  799. workqueue->queue(new Task_function(new Build_id_task_runner(&options,
  800. layout,
  801. of),
  802. final_blocker,
  803. "Task_function Build_id_task_runner"));
  804. }
  805. else
  806. {
  807. // Queue a task to close the output file. This will be blocked by
  808. // FINAL_BLOCKER.
  809. workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
  810. of, NULL, 0),
  811. final_blocker,
  812. "Task_function Close_task_runner"));
  813. }
  814. }
  815. } // End namespace gold.