#include "gold.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <algorithm>
#include "libiberty.h"
#include "options.h"
#include "debug.h"
#include "workqueue.h"
#include "dirsearch.h"
#include "readsyms.h"
#include "symtab.h"
#include "common.h"
#include "object.h"
#include "layout.h"
#include "reloc.h"
#include "defstd.h"
namespace gold
{
const char* program_name;
void
gold_exit(bool status)
{
if (!status && parameters != NULL && parameters->options_valid())
unlink_if_ordinary(parameters->options().output_file_name());
exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
}
void
gold_nomem()
{
ssize_t len = write(2, program_name, strlen(program_name));
if (len >= 0)
{
const char* const s = ": out of memory\n";
len = write(2, s, strlen(s));
}
gold_exit(false);
}
void
do_gold_unreachable(const char* filename, int lineno, const char* function)
{
fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
program_name, function, filename, lineno);
gold_exit(false);
}
class Middle_runner : public Task_function_runner
{
public:
Middle_runner(const General_options& options,
const Input_objects* input_objects,
Symbol_table* symtab,
Layout* layout, Mapfile* mapfile)
: options_(options), input_objects_(input_objects), symtab_(symtab),
layout_(layout), mapfile_(mapfile)
{ }
void
run(Workqueue*, const Task*);
private:
const General_options& options_;
const Input_objects* input_objects_;
Symbol_table* symtab_;
Layout* layout_;
Mapfile* mapfile_;
};
void
Middle_runner::run(Workqueue* workqueue, const Task* task)
{
queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
this->layout_, workqueue, this->mapfile_);
}
void
queue_initial_tasks(const General_options& options,
Dirsearch& search_path,
const Command_line& cmdline,
Workqueue* workqueue, Input_objects* input_objects,
Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
{
if (cmdline.begin() == cmdline.end())
gold_fatal(_("no input files"));
int thread_count = options.thread_count_initial();
if (thread_count == 0)
thread_count = cmdline.number_of_input_files();
workqueue->set_thread_count(thread_count);
Task_token* this_blocker = NULL;
for (Command_line::const_iterator p = cmdline.begin();
p != cmdline.end();
++p)
{
Task_token* next_blocker = new Task_token(true);
next_blocker->add_blocker();
workqueue->queue(new Read_symbols(options, input_objects, symtab, layout,
&search_path, mapfile, &*p, NULL,
this_blocker, next_blocker));
this_blocker = next_blocker;
}
workqueue->queue(new Task_function(new Middle_runner(options,
input_objects,
symtab,
layout,
mapfile),
this_blocker,
"Task_function Middle_runner"));
}
void
queue_middle_tasks(const General_options& options,
const Task* task,
const Input_objects* input_objects,
Symbol_table* symtab,
Layout* layout,
Workqueue* workqueue,
Mapfile* mapfile)
{
if (input_objects->number_of_input_objects() == 0)
set_parameters_target(¶meters->default_target());
int thread_count = options.thread_count_middle();
if (thread_count == 0)
thread_count = std::max(2, input_objects->number_of_input_objects());
workqueue->set_thread_count(thread_count);
const bool doing_static_link = (!input_objects->any_dynamic()
&& !parameters->options().shared());
set_parameters_doing_static_link(doing_static_link);
if (!doing_static_link && options.is_static())
{
gold_error(_("cannot mix -static with dynamic object %s"),
(*input_objects->dynobj_begin())->name().c_str());
}
if (!doing_static_link && parameters->options().relocatable())
gold_error(_("cannot mix -r with dynamic object %s"),
(*input_objects->dynobj_begin())->name().c_str());
if (!doing_static_link
&& options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
(*input_objects->dynobj_begin())->name().c_str());
if (is_debugging_enabled(DEBUG_SCRIPT))
layout->script_options()->print(stderr);
input_objects->check_dynamic_dependencies();
symtab->detect_odr_violations(task, options.output_file_name());
layout->create_script_sections();
layout->create_initial_dynamic_sections(symtab);
layout->define_script_symbols(symtab);
symtab->add_undefined_symbols_from_command_line();
layout->attach_sections_to_segments();
if (!parameters->options().relocatable())
{
define_standard_symbols(symtab, layout);
layout->define_section_symbols(symtab);
}
layout->define_group_signatures(symtab);
Task_token* blocker = new Task_token(true);
Task_token* symtab_lock = new Task_token(false);
for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
p != input_objects->relobj_end();
++p)
{
blocker->add_blocker();
workqueue->queue(new Read_relocs(options, symtab, layout, *p,
symtab_lock, blocker));
}
if (parameters->options().define_common())
{
blocker->add_blocker();
workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
symtab_lock, blocker));
}
Target* target = const_cast<Target*>(¶meters->target());
workqueue->queue(new Task_function(new Layout_task_runner(options,
input_objects,
symtab,
target,
layout,
mapfile),
blocker,
"Task_function Layout_task_runner"));
}
void
queue_final_tasks(const General_options& options,
const Input_objects* input_objects,
const Symbol_table* symtab,
Layout* layout,
Workqueue* workqueue,
Output_file* of)
{
int thread_count = options.thread_count_final();
if (thread_count == 0)
thread_count = std::max(2, input_objects->number_of_input_objects());
workqueue->set_thread_count(thread_count);
bool any_postprocessing_sections = layout->any_postprocessing_sections();
Task_token* input_sections_blocker = NULL;
if (!any_postprocessing_sections)
input_sections_blocker = new Task_token(true);
Task_token* output_sections_blocker = new Task_token(true);
Task_token* final_blocker = new Task_token(true);
final_blocker->add_blocker();
workqueue->queue(new Write_symbols_task(layout,
symtab,
input_objects,
layout->sympool(),
layout->dynpool(),
of,
final_blocker));
output_sections_blocker->add_blocker();
final_blocker->add_blocker();
workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
final_blocker));
final_blocker->add_blocker();
workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
p != input_objects->relobj_end();
++p)
{
if (input_sections_blocker != NULL)
input_sections_blocker->add_blocker();
final_blocker->add_blocker();
workqueue->queue(new Relocate_task(options, symtab, layout, *p, of,
input_sections_blocker,
output_sections_blocker,
final_blocker));
}
if (!any_postprocessing_sections)
{
final_blocker->add_blocker();
Task* t = new Write_after_input_sections_task(layout, of,
input_sections_blocker,
final_blocker);
workqueue->queue(t);
}
else
{
Task_token *new_final_blocker = new Task_token(true);
new_final_blocker->add_blocker();
Task* t = new Write_after_input_sections_task(layout, of,
final_blocker,
new_final_blocker);
workqueue->queue(t);
final_blocker = new_final_blocker;
}
workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
of),
final_blocker,
"Task_function Close_task_runner"));
}
}