2009-11-25 08:10:05 +08:00
|
|
|
// inremental.cc -- incremental linking test/deubg tool
|
|
|
|
|
|
|
|
// Copyright 2009 Free Software Foundation, Inc.
|
|
|
|
// Written by Rafael Avila de Espindola <rafael.espindola@gmail.com>
|
|
|
|
|
|
|
|
// This file is part of gold.
|
|
|
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
|
|
// MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
|
|
// This file is a (still incomplete) test/debug tool that should display
|
|
|
|
// all information available in the incremental linking sections in a
|
|
|
|
// format that is easy to read.
|
|
|
|
// Once the format is a bit more stable, this should probably be moved to
|
|
|
|
// readelf. Because of that, the use of gold's data structures and functions
|
|
|
|
// is just a short term convenience and not a design decision.
|
|
|
|
|
|
|
|
#include "gold.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "incremental.h"
|
|
|
|
|
|
|
|
namespace gold
|
|
|
|
{
|
|
|
|
class Output_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace gold;
|
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
template<int size, bool big_endian>
|
|
|
|
static void
|
|
|
|
dump_incremental_inputs(const char* argv0,
|
|
|
|
const char* filename, Incremental_binary* inc)
|
2009-11-25 08:10:05 +08:00
|
|
|
{
|
2009-12-08 05:36:52 +08:00
|
|
|
bool t;
|
2009-11-25 08:10:05 +08:00
|
|
|
unsigned int strtab_shndx;
|
|
|
|
Incremental_binary::Location location;
|
|
|
|
|
|
|
|
t = inc->find_incremental_inputs_section(&location, &strtab_shndx);
|
|
|
|
if (!t)
|
|
|
|
{
|
2009-12-08 05:36:52 +08:00
|
|
|
fprintf(stderr, "%s: %s: no .gnu_incremental_inputs section\n", argv0,
|
2009-11-25 08:10:05 +08:00
|
|
|
filename);
|
2009-12-08 05:36:52 +08:00
|
|
|
exit (1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Incremental_binary::View inputs_view(inc->view(location));
|
2009-12-08 05:36:52 +08:00
|
|
|
const unsigned char* p = inputs_view.data();
|
2009-11-25 08:10:05 +08:00
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
Incremental_inputs_header<size, big_endian> incremental_header(p);
|
2009-11-25 08:10:05 +08:00
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
const unsigned char* incremental_inputs_base =
|
2009-11-25 08:10:05 +08:00
|
|
|
(p + sizeof(Incremental_inputs_header_data));
|
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
if (incremental_header.get_version() != 1)
|
2009-11-25 08:10:05 +08:00
|
|
|
{
|
2009-12-08 05:36:52 +08:00
|
|
|
fprintf(stderr, "%s: %s: unknown incremental version %d\n", argv0,
|
|
|
|
filename, incremental_header.get_version());
|
|
|
|
exit(1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file(inc);
|
2009-11-25 08:10:05 +08:00
|
|
|
|
|
|
|
if (elf_file.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s: invalid string table section %u (type %d != %d)\n",
|
2009-12-08 05:36:52 +08:00
|
|
|
argv0, filename, strtab_shndx,
|
2009-11-25 08:10:05 +08:00
|
|
|
elf_file.section_type(strtab_shndx), elfcpp::SHT_STRTAB);
|
2009-12-08 05:36:52 +08:00
|
|
|
exit(1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Incremental_binary::Location
|
|
|
|
strtab_location(elf_file.section_contents(strtab_shndx));
|
|
|
|
|
|
|
|
Incremental_binary::View strtab_view(inc->view(strtab_location));
|
|
|
|
p = strtab_view.data();
|
|
|
|
|
|
|
|
elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
|
|
|
|
const char* command_line;
|
2009-12-08 05:36:52 +08:00
|
|
|
elfcpp::Elf_Word command_line_offset =
|
|
|
|
incremental_header.get_command_line_offset();
|
|
|
|
t = strtab.get_c_string(command_line_offset, &command_line);
|
2009-11-25 08:10:05 +08:00
|
|
|
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s: failed to get link command line: %zu out of range\n",
|
2009-12-08 05:36:52 +08:00
|
|
|
argv0, filename,
|
|
|
|
static_cast<size_t>(command_line_offset));
|
|
|
|
exit(1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Link command line: %s\n", command_line);
|
|
|
|
|
|
|
|
printf("Input files:\n");
|
2009-12-08 05:36:52 +08:00
|
|
|
for (unsigned i = 0; i < incremental_header.get_input_file_count(); ++i)
|
2009-11-25 08:10:05 +08:00
|
|
|
{
|
2009-12-08 05:36:52 +08:00
|
|
|
const unsigned char* input_p = incremental_inputs_base +
|
|
|
|
i * sizeof(Incremental_inputs_entry_data);
|
|
|
|
Incremental_inputs_entry<size, big_endian> input(input_p);
|
|
|
|
const char* objname;
|
2009-11-25 08:10:05 +08:00
|
|
|
|
2009-12-08 05:36:52 +08:00
|
|
|
t = strtab.get_c_string(input.get_filename_offset(), &objname);
|
2009-11-25 08:10:05 +08:00
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"%s: %s: failed to get file name for object %u:"
|
2009-12-08 05:36:52 +08:00
|
|
|
" %zu out of range\n", argv0, filename, i,
|
|
|
|
static_cast<size_t>(input.get_filename_offset()));
|
|
|
|
exit(1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
printf(" %s\n", objname);
|
2009-11-28 00:15:26 +08:00
|
|
|
printf(" Timestamp sec = %llu\n",
|
2009-12-08 05:36:52 +08:00
|
|
|
static_cast<unsigned long long>(input.get_timestamp_sec()));
|
|
|
|
printf(" Timestamp nsec = %d\n", input.get_timestamp_nsec());
|
2009-11-25 08:10:05 +08:00
|
|
|
printf(" Type = ");
|
|
|
|
// TODO: print the data at input->data_offset once we have it.
|
2009-12-08 05:36:52 +08:00
|
|
|
elfcpp::Elf_Word input_type = input.get_input_type();
|
|
|
|
switch (input_type)
|
2009-11-25 08:10:05 +08:00
|
|
|
{
|
|
|
|
case INCREMENTAL_INPUT_OBJECT:
|
2009-12-02 05:15:06 +08:00
|
|
|
printf("Object\n");
|
2009-11-25 08:10:05 +08:00
|
|
|
break;
|
|
|
|
case INCREMENTAL_INPUT_ARCHIVE:
|
|
|
|
printf("Archive\n");
|
|
|
|
break;
|
|
|
|
case INCREMENTAL_INPUT_SHARED_LIBRARY:
|
|
|
|
printf("Shared library\n");
|
|
|
|
break;
|
|
|
|
case INCREMENTAL_INPUT_SCRIPT:
|
|
|
|
printf("Linker script\n");
|
2009-12-08 05:36:52 +08:00
|
|
|
if (input.get_data_offset() != 0)
|
2009-12-03 00:27:15 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr,"%s: %s: %u is a script but offset is not zero",
|
2009-12-08 05:36:52 +08:00
|
|
|
argv0, filename, i);
|
|
|
|
exit(1);
|
2009-12-03 00:27:15 +08:00
|
|
|
}
|
2009-11-25 08:10:05 +08:00
|
|
|
break;
|
|
|
|
case INCREMENTAL_INPUT_INVALID:
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: invalid file type for object %u: %d\n",
|
2009-12-08 05:36:52 +08:00
|
|
|
argv0, i, input_type);
|
|
|
|
exit(1);
|
2009-11-25 08:10:05 +08:00
|
|
|
}
|
|
|
|
}
|
2009-12-08 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const char* filename = argv[1];
|
|
|
|
|
|
|
|
Output_file* file = new Output_file(filename);
|
|
|
|
|
|
|
|
bool t = file->open_for_modification();
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: open_for_modification(%s): %s\n", argv[0], filename,
|
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Incremental_binary* inc = open_incremental_binary(file);
|
|
|
|
|
|
|
|
if (inc == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: open_incremental_binary(%s): %s\n", argv[0],
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parameters->size_and_endianness())
|
|
|
|
{
|
|
|
|
#ifdef HAVE_TARGET_32_LITTLE
|
|
|
|
case Parameters::TARGET_32_LITTLE:
|
|
|
|
dump_incremental_inputs<32, false>(argv[0], filename, inc);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TARGET_32_BIG
|
|
|
|
case Parameters::TARGET_32_BIG:
|
|
|
|
dump_incremental_inputs<32, true>(argv[0], filename, inc);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TARGET_64_LITTLE
|
|
|
|
case Parameters::TARGET_64_LITTLE:
|
|
|
|
dump_incremental_inputs<64, false>(argv[0], filename, inc);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TARGET_64_BIG
|
|
|
|
case Parameters::TARGET_64_BIG:
|
|
|
|
dump_incremental_inputs<64, true>(argv[0], filename, inc);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
gold_unreachable();
|
|
|
|
}
|
2009-11-25 08:10:05 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|