2008-02-08 08:06:58 +01:00
|
|
|
// binary_unittest.cc -- test Binary_to_elf
|
|
|
|
|
2015-01-01 15:15:26 +01:00
|
|
|
// Copyright (C) 2008-2015 Free Software Foundation, Inc.
|
2008-02-08 08:06:58 +01:00
|
|
|
// Written by Ian Lance Taylor <iant@google.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.
|
|
|
|
|
|
|
|
#include "gold.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "elfcpp.h"
|
|
|
|
#include "parameters.h"
|
|
|
|
#include "errors.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "binary.h"
|
|
|
|
#include "object.h"
|
2012-12-07 22:48:32 +01:00
|
|
|
#include "descriptors.h"
|
2008-02-08 08:06:58 +01:00
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
#include "testfile.h"
|
|
|
|
|
2012-12-10 18:38:42 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
read_all (int fd, unsigned char* buf, ssize_t size)
|
|
|
|
{
|
|
|
|
ssize_t total_read = 0;
|
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
ssize_t nread = ::read(fd, buf, size);
|
|
|
|
if (nread < 0)
|
|
|
|
return nread;
|
|
|
|
if (nread == 0)
|
|
|
|
break;
|
|
|
|
buf += nread;
|
|
|
|
size -= nread;
|
|
|
|
total_read += nread;
|
|
|
|
}
|
|
|
|
return total_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End anonymous namespace.
|
|
|
|
|
2008-02-08 08:06:58 +01:00
|
|
|
namespace gold_testsuite
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace gold;
|
|
|
|
|
|
|
|
template<int size, bool big_endian>
|
|
|
|
bool
|
2009-10-01 00:21:13 +02:00
|
|
|
Sized_binary_test()
|
2008-02-08 08:06:58 +01:00
|
|
|
{
|
2009-10-01 00:21:13 +02:00
|
|
|
parameters_clear_target();
|
2008-02-08 08:06:58 +01:00
|
|
|
// We need a pretend Task.
|
|
|
|
const Task* task = reinterpret_cast<const Task*>(-1);
|
|
|
|
|
|
|
|
// Use the executable itself as the binary data.
|
|
|
|
struct stat st;
|
|
|
|
CHECK(::stat(gold::program_name, &st) == 0);
|
2012-12-07 22:48:32 +01:00
|
|
|
int o = open_descriptor(-1, gold::program_name, O_RDONLY);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(o >= 0);
|
|
|
|
unsigned char* filedata = new unsigned char[st.st_size];
|
2012-12-10 18:38:42 +01:00
|
|
|
CHECK(read_all(o, filedata, st.st_size) == static_cast<ssize_t>(st.st_size));
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(::close(o) == 0);
|
|
|
|
|
|
|
|
Binary_to_elf binary(static_cast<elfcpp::EM>(0xffff), size, big_endian,
|
|
|
|
gold::program_name);
|
|
|
|
|
|
|
|
CHECK(binary.convert(task));
|
|
|
|
|
|
|
|
Input_file input_file(task, "test.o", binary.converted_data(),
|
|
|
|
binary.converted_size());
|
|
|
|
Object* object = make_elf_object("test.o", &input_file, 0,
|
|
|
|
binary.converted_data(),
|
* readsyms.cc (Read_symbols::incompatible_warning): New function.
(Read_symbols::requeue): New function.
(Read_symbols::do_read_symbols): If make_elf_object fails because
the target type is not configured, and the file was searched for,
issue a warning and retry with the next directory.
(Add_symbols::run): If the file has an incompatible format, and
it was searched for, requeue the Read_symbols task. On error,
release the object.
* readsyms.h (class Read_symbols): Add dirindex_ field. Add
dirindex parameter to constructor. Change all callers. Declare
incompatible_warning and requeue.
(class Add_symbols): Add dirpath_, dirindex_, mapfile_,
input_argument_ and input_group_ fields. Add them to
constructor. Change all callers.
(class Read_script): Add dirindex_ field. Add it to constructor.
Change all callers.
* archive.cc (Archive::setup): Remove input_objects parameter.
Change all callers.
(Archive::get_file_and_offset): Likewise.
(Archive::read_all_symbols): Likewise.
(Archive::read_symbols): Likewise.
(Archive::get_elf_object_for_member): Remove input_objects
parameter. Add punconfigured parameter. Change all callers.
(Archive::add_symbols): Change return type to bool. Check return
value of include_member.
(Archive::include_all_members): Likewise.
(Archive::include_member): Change return type to bool. Return
false if first included object has incompatible target. Set
included_member_ field.
(Add_archive_symbols::run): If add_symbols returns false, requeue
Read_symbols task.
* archive.h (class Archive): Add included_member_ field.
Initialize it in constructor. Add input_file and searched_for
methods. Update declarations.
(class Add_archive_symbols): Add dirpath_, dirindex_, and
input_argument_ fields. Add them to constructor. Change all
callers.
* script.cc: Include "target-select.h".
(class Parser_closure): Add skip_on_incompatible_target_ and
found_incompatible_target_ fields. Add
skip_on_incompatible_target parameter to constructor. Change all
callers. Add methods skip_on_incompatible_target,
clear_skip_on_incompatible_target, found_incompatible_target, and
set_found_incompatible_target.
(read_input_script): Add dirindex parameter. Change all callers.
If parser finds an incompatible target, requeue Read_symbols
task.
(script_set_symbol): Clear skip_on_incompatible_target in
closure.
(script_add_assertion, script_parse_option): Likewise.
(script_start_sections, script_add_phdr): Likewise.
(script_check_output_format): New function.
* script.h (read_input_script): Update declaration.
* script-c.h (script_check_output_format): Declare.
* yyscript.y (file_cmd): Handle OUTPUT_FORMAT.
(ignore_cmd): Remove OUTPUT_FORMAT.
* fileread.cc (Input_file::Input_file): Add explicit this.
(Input_file::will_search_for): New function.
(Input_file::open): Add pindex parameter. Change all callers.
* fileread.h (class Input_file): Add input_file_argument method.
Declare will_search_for. Update declarations.
* object.cc (make_elf_object): Add punconfigured parameter.
Change all callers.
* object.h (class Object): Make input_file public. Add
searched_for method.
(make_elf_object): Update declaration.
* dirsearch.cc (Dirsearch::find): Add pindex parameter. Use it to
restart search.
* dirsearch.h (class Dirsearch): Update declaration.
* options.h (class General_options): Add --warn-search-mismatch.
* parameters.cc (Parameters::is_compatible_target): New function.
* parameters.h (class Parameters): Declare is_compatible_target.
* workqueue.cc (Workqueue::add_blocker): New function.
* workqueue.h (class Workqueue): Declare add_blocker.
2009-03-14 06:56:46 +01:00
|
|
|
binary.converted_size(), NULL);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(object != NULL);
|
|
|
|
if (object == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CHECK(!object->is_dynamic());
|
|
|
|
CHECK(object->shnum() == 5);
|
|
|
|
CHECK(object->section_name(1) == ".data");
|
2008-02-12 01:13:39 +01:00
|
|
|
CHECK(object->section_flags(1) == (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE));
|
2008-02-08 08:06:58 +01:00
|
|
|
section_size_type len;
|
|
|
|
const unsigned char* contents = object->section_contents(1, &len, false);
|
2008-02-12 01:13:39 +01:00
|
|
|
CHECK(len == convert_to_section_size_type(st.st_size));
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(memcmp(filedata, contents, len) == 0);
|
|
|
|
|
|
|
|
// Force the symbols to be read internally, so that
|
|
|
|
// symbol_section_and_value will work.
|
|
|
|
Read_symbols_data sd;
|
|
|
|
object->read_symbols(&sd);
|
|
|
|
delete sd.section_headers;
|
2010-04-08 00:58:23 +02:00
|
|
|
sd.section_headers = NULL;
|
2008-02-08 08:06:58 +01:00
|
|
|
delete sd.section_names;
|
2010-04-08 00:58:23 +02:00
|
|
|
sd.section_names = NULL;
|
2008-02-08 08:06:58 +01:00
|
|
|
delete sd.symbols;
|
2010-04-08 00:58:23 +02:00
|
|
|
sd.symbols = NULL;
|
2008-02-08 08:06:58 +01:00
|
|
|
delete sd.symbol_names;
|
2010-04-08 00:58:23 +02:00
|
|
|
sd.symbol_names = NULL;
|
2008-02-08 08:06:58 +01:00
|
|
|
|
2011-05-24 23:41:10 +02:00
|
|
|
Sized_relobj_file<size, big_endian>* relobj =
|
|
|
|
static_cast<Sized_relobj_file<size, big_endian>*>(object);
|
|
|
|
typename Sized_relobj_file<size, big_endian>::Address value;
|
2008-04-19 20:30:58 +02:00
|
|
|
bool is_ordinary;
|
|
|
|
CHECK(relobj->symbol_section_and_value(0, &value, &is_ordinary) == 0);
|
|
|
|
CHECK(is_ordinary);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(value == 0);
|
2008-04-19 20:30:58 +02:00
|
|
|
CHECK(relobj->symbol_section_and_value(1, &value, &is_ordinary) == 1);
|
|
|
|
CHECK(is_ordinary);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(value == 0);
|
2008-04-19 20:30:58 +02:00
|
|
|
CHECK(relobj->symbol_section_and_value(2, &value, &is_ordinary) == 1);
|
|
|
|
CHECK(is_ordinary);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(static_cast<off_t>(value) == st.st_size);
|
2008-04-19 20:30:58 +02:00
|
|
|
CHECK(relobj->symbol_section_and_value(3, &value, &is_ordinary)
|
|
|
|
== elfcpp::SHN_ABS);
|
|
|
|
CHECK(!is_ordinary);
|
2008-02-08 08:06:58 +01:00
|
|
|
CHECK(static_cast<off_t>(value) == st.st_size);
|
|
|
|
|
|
|
|
object->unlock(task);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Binary_test(Test_report*)
|
|
|
|
{
|
|
|
|
Errors errors(gold::program_name);
|
2008-02-28 01:18:24 +01:00
|
|
|
set_parameters_errors(&errors);
|
2008-02-08 08:06:58 +01:00
|
|
|
|
2008-02-26 23:48:08 +01:00
|
|
|
General_options options;
|
2008-02-28 01:18:24 +01:00
|
|
|
set_parameters_options(&options);
|
2008-02-08 08:06:58 +01:00
|
|
|
|
|
|
|
int fail = 0;
|
|
|
|
|
|
|
|
#ifdef HAVE_TARGET_32_LITTLE
|
2009-10-01 00:21:13 +02:00
|
|
|
if (!Sized_binary_test<32, false>())
|
2008-02-08 08:06:58 +01:00
|
|
|
++fail;
|
2009-10-01 00:21:13 +02:00
|
|
|
CHECK(¶meters->target() == target_test_pointer_32_little);
|
2008-02-08 08:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TARGET_32_BIG
|
2009-10-01 00:21:13 +02:00
|
|
|
if (!Sized_binary_test<32, true>())
|
2008-02-08 08:06:58 +01:00
|
|
|
++fail;
|
2009-10-01 00:21:13 +02:00
|
|
|
CHECK(¶meters->target() == target_test_pointer_32_big);
|
2008-02-08 08:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TARGET_64_LITTLE
|
2009-10-01 00:21:13 +02:00
|
|
|
if (!Sized_binary_test<64, false>())
|
2008-02-08 08:06:58 +01:00
|
|
|
++fail;
|
2009-10-01 00:21:13 +02:00
|
|
|
CHECK(¶meters->target() == target_test_pointer_64_little);
|
2008-02-08 08:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TARGET_64_BIG
|
2009-10-01 00:21:13 +02:00
|
|
|
if (!Sized_binary_test<64, true>())
|
2008-02-08 08:06:58 +01:00
|
|
|
++fail;
|
2009-10-01 00:21:13 +02:00
|
|
|
CHECK(¶meters->target() == target_test_pointer_64_big);
|
2008-02-08 08:06:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return fail == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register_test binary_register("Binary", Binary_test);
|
|
|
|
|
|
|
|
} // End namespace gold_testsuite.
|