binutils-gdb/gold/testsuite/binary_unittest.cc
Ian Lance Taylor 15f8229bbf * 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 05:56:46 +00:00

153 lines
4.2 KiB
C++

// binary_unittest.cc -- test Binary_to_elf
// Copyright 2008 Free Software Foundation, Inc.
// 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"
#include "test.h"
#include "testfile.h"
namespace gold_testsuite
{
using namespace gold;
template<int size, bool big_endian>
bool
Sized_binary_test(Target* target)
{
// 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);
int o = ::open(gold::program_name, O_RDONLY);
CHECK(o >= 0);
unsigned char* filedata = new unsigned char[st.st_size];
CHECK(::read(o, filedata, st.st_size) == st.st_size);
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(),
binary.converted_size(), NULL);
CHECK(object != NULL);
if (object == NULL)
return false;
CHECK(!object->is_dynamic());
CHECK(object->target() == target);
CHECK(object->shnum() == 5);
CHECK(object->section_name(1) == ".data");
CHECK(object->section_flags(1) == (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE));
section_size_type len;
const unsigned char* contents = object->section_contents(1, &len, false);
CHECK(len == convert_to_section_size_type(st.st_size));
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;
delete sd.section_names;
delete sd.symbols;
delete sd.symbol_names;
Sized_relobj<size, big_endian>* relobj =
static_cast<Sized_relobj<size, big_endian>*>(object);
typename Sized_relobj<size, big_endian>::Address value;
bool is_ordinary;
CHECK(relobj->symbol_section_and_value(0, &value, &is_ordinary) == 0);
CHECK(is_ordinary);
CHECK(value == 0);
CHECK(relobj->symbol_section_and_value(1, &value, &is_ordinary) == 1);
CHECK(is_ordinary);
CHECK(value == 0);
CHECK(relobj->symbol_section_and_value(2, &value, &is_ordinary) == 1);
CHECK(is_ordinary);
CHECK(static_cast<off_t>(value) == st.st_size);
CHECK(relobj->symbol_section_and_value(3, &value, &is_ordinary)
== elfcpp::SHN_ABS);
CHECK(!is_ordinary);
CHECK(static_cast<off_t>(value) == st.st_size);
object->unlock(task);
return true;
}
bool
Binary_test(Test_report*)
{
Errors errors(gold::program_name);
set_parameters_errors(&errors);
General_options options;
set_parameters_options(&options);
int fail = 0;
#ifdef HAVE_TARGET_32_LITTLE
if (!Sized_binary_test<32, false>(target_test_pointer_32_little))
++fail;
#endif
#ifdef HAVE_TARGET_32_BIG
if (!Sized_binary_test<32, true>(target_test_pointer_32_big))
++fail;
#endif
#ifdef HAVE_TARGET_64_LITTLE
if (!Sized_binary_test<64, false>(target_test_pointer_64_little))
++fail;
#endif
#ifdef HAVE_TARGET_64_BIG
if (!Sized_binary_test<64, true>(target_test_pointer_64_big))
++fail;
#endif
return fail == 0;
}
Register_test binary_register("Binary", Binary_test);
} // End namespace gold_testsuite.