#include "gold.h"
#include <cstdio>
#include <cstring>
#ifdef HAVE_MALLINFO
#include <malloc.h>
#endif
#include "libiberty.h"
#include "script.h"
#include "options.h"
#include "parameters.h"
#include "errors.h"
#include "mapfile.h"
#include "dirsearch.h"
#include "workqueue.h"
#include "object.h"
#include "archive.h"
#include "symtab.h"
#include "layout.h"
using namespace gold;
#ifdef DEBUG
#include <stdio.h>
#include <sys/stat.h>
static std::string
collect_argv(int argc, char** argv)
{
std::string args;
for (int i = 0; i < argc; ++i)
{
args.append(" '");
const char* argpos = argv[i];
while (1)
{
const int len = strcspn(argpos, "'");
args.append(argpos, len);
if (argpos[len] == '\0')
break;
args.append("'\"'\"'");
argpos += len + 1;
}
args.append("'");
}
return args;
}
static void
write_debug_script(std::string filename_str,
const char* argv_0, const char* args)
{
size_t slash = filename_str.rfind('/');
if (slash != std::string::npos)
filename_str = filename_str.c_str() + slash + 1;
filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
const char* filename = filename_str.c_str();
FILE* fp = fopen(filename, "w");
if (fp)
{
fprintf(fp, "[ \"$1\" = debug ]"
" && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
" && shift\n",
argv_0);
fprintf(fp, "$PREFIX%s $*\n", args);
fclose(fp);
chmod(filename, 0755);
}
else
filename = "[none]";
fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
fflush(stderr);
}
#else
static inline std::string
collect_argv(int, char**)
{
return "";
}
static inline void
write_debug_script(std::string, const char*, const char*)
{
}
#endif
int
main(int argc, char** argv)
{
#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
setlocale (LC_MESSAGES, "");
#endif
#if defined (HAVE_SETLOCALE)
setlocale (LC_CTYPE, "");
#endif
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
program_name = argv[0];
expandargv(&argc, &argv);
std::string args = collect_argv(argc, argv);
Errors errors(program_name);
set_parameters_errors(&errors);
Command_line command_line;
command_line.process(argc - 1, const_cast<const char**>(argv + 1));
long start_time = 0;
if (command_line.options().stats())
start_time = get_run_time();
set_parameters_options(&command_line.options());
write_debug_script(command_line.options().output_file_name(),
program_name, args.c_str());
Mapfile* mapfile = NULL;
if (command_line.options().user_set_Map())
{
mapfile = new Mapfile();
if (!mapfile->open(command_line.options().Map()))
{
delete mapfile;
mapfile = NULL;
}
}
if (parameters->options().relocatable())
command_line.script_options().version_script_info()->clear();
Workqueue workqueue(command_line.options());
Input_objects input_objects;
Symbol_table symtab(command_line.number_of_input_files() * 1024,
command_line.version_script());
Layout layout(command_line.options(), &command_line.script_options());
Dirsearch search_path;
search_path.initialize(&workqueue, &command_line.options().library_path());
queue_initial_tasks(command_line.options(), search_path,
command_line, &workqueue, &input_objects,
&symtab, &layout, mapfile);
workqueue.process(0);
if (command_line.options().stats())
{
long run_time = get_run_time() - start_time;
fprintf(stderr, _("%s: total run time: %ld.%06ld seconds\n"),
program_name, run_time / 1000000, run_time % 1000000);
#ifdef HAVE_MALLINFO
struct mallinfo m = mallinfo();
fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
program_name, m.arena);
#endif
File_read::print_stats();
Archive::print_stats();
fprintf(stderr, _("%s: output file size: %lld bytes\n"),
program_name, static_cast<long long>(layout.output_file_size()));
symtab.print_stats();
layout.print_stats();
}
if (mapfile != NULL)
mapfile->close();
if (command_line.options().user_set_print_symbol_counts())
input_objects.print_symbol_counts(&symtab);
if (parameters->options().fatal_warnings()
&& errors.warning_count() > 0
&& errors.error_count() == 0)
gold_error("treating warnings as errors");
gold_exit(errors.error_count() == 0
|| parameters->options().noinhibit_exec());
}