d491d34e93
(Xindex::read_symtab_xindex): New function. (Xindex::sym_xindex_to_shndx): New function. (Sized_relobj::find_symtab): Pick up SHT_SYMTAB_SHNDX section if available. (Sized_relobj::do_initialize_xindex): New function. (Sized_relobj::do_read_symbols): Adjust section links. (Sized_relobj::symbol_section_and_value): Add is_ordinary parameter. Change all callers. (Sized_relobj::include_section_group): Adjust section links and symbol section indexes. (Sized_relobj::do_layout): Adjust section links. (Sized_relobj::do_count_local_symbols): Adjust section links and symbol section indexes. (Sized_relobj::do_finalize_local_symbols): Distinguish between ordinary and special symbols. (Sized_relobj::write_local_symbols): Add symtab_xindex and dynsym_xindex parameters. Change all callers. Adjust section links. Use SHN_XINDEX when needed. (Sized_relobj::get_symbol_location_info): Adjust section links. Don't get fooled by special symbols. * object.h (class Xindex): Define. (class Object): Add xindex_ parameter. Declare virtual functoin do_initialize_xindex. (Object::adjust_sym_shndx): New function. (Object::set_xindex): New protected function. (class Symbol_value): Add is_ordinary_shndx_ field. (Symbol_value::Symbol_value): Initialize is_ordinary_shndx_. (Symbol_value::value): Assert ordinary section. (Symbol_value::initialize_input_to_output_map): Likewise. (Symbol_value::set_input_shndx): Add is_ordinary parameter. Change all callers. (Symbol_value::input_shndx): Add is_ordinary parameter. Change all callers. (class Sized_relobj): Update declarations. (Sized_relobj::local_symbol_input_shndx): Add is_ordinary parameter. Change all callers. (Sized_relobj::adjust_shndx): New function. * dynobj.cc (Sized_dynobj::Sized_dynobj): Initialize dynsym_shndx_ field. (Sized_dynobj::find_dynsym_sections): Remove pdynsym_shndx parameter. Change all callers. Pick up SHT_DYNSYM_SHNDX section for SHT_DYNSYM section if available. Set dynsym_shndx_ field. (Sized_dynobj::read_dynsym_section): Adjust section links. (Sized_dynobj::read_dynamic): Likewise. (Sized_dynobj::do_read_symbols): Use dynsym_shndx_ field. Adjust section links. (Sized_dynobj::do_initialize_xindex): New function. * dynobj.h (class Sized_dynobj): Add dynsym_shndx_ field. Declare do_initialize_xindex. (Sized_dynobj::adjust_shndx): New function. * layout.cc (Layout::Layout): Initialize symtab_xindex_ and dynsym_xindex_ fields. (Layout::finalize): Add a call to set_section_indexes before creating the symtab sections. (Layout::set_section_indexes): Don't do anything if the section already has a section index. (Layout::create_symtab_sections): Add shnum parameter. Change caller. Create .symtab_shndx section if needed. (Layout::create_shdrs): Add shstrtab_section parameter. Change caller. (Layout::allocated_output_section_count): New function. (Layout::create_dynamic_symtab): Create .dynsym_shndx section if needed. * layout.h (class Layout): Add symtab_xindex_ and dynsym_xindex_ fields. Update declarations. (Layout::symtab_xindex): New function. (Layout::dynsym_xindex): New function. (class Write_symbols_task): Add layout_ field. (Write_symbols_task::Write_symbols_task): Add layout parameter. Change caller. * output.cc (Output_section_headers::Output_section_headers): Add shstrtab_section parameter. Change all callers. (Output_section_headers::do_sized_write): Store overflow values for section count and section string table section index in section header zero. (Output_file_header::do_sized_write): Check for overflow of section count and section string table section index. (Output_symtab_xindex::do_write): New function. (Output_symtab_xindex::endian_do_write): New function. * output.h (class Output_section_headers): Add shstrtab_section_. Update declarations. (class Output_symtab_xindex): Define. (Output_section::has_out_shndx): New function. * symtab.cc (Symbol::init_fields): Initialize is_ordinary_shndx_ field. (Symbol::init_base): Add st_shndx and is_ordinary parameters. Change all callers. (Sized_symbol::init): Likewise. (Symbol::output_section): Check for ordinary symbol. (Symbol_table::add_from_object): Remove orig_sym parameter. Add st_shndx, is_ordinary, and orig_st_shndx parameters. Change all callers. (Symbol_table::add_from_relobj): Add symndx_offset parameter. Change all callers. Simplify handling of symbols from sections not included in the link. (Symbol_table::add_from_dynobj): Handle ordinary symbol distinction. (Weak_alias_sorter::operator()): Assert that symbols are ordinary. (Symbol_table::sized_finalize_symbol): Handle ordinary symbol distinction. (Symbol_table::write_globals): Add symtab_xindex and dynsym_xindex parameters. Change all callers. (Symbol_table::sized_write_globals): Likewise. Handle ordinary symbol distinction. Use SHN_XINDEX when needed. (Symbol_table::write_section_symbol): Add symtab_xindex parameter. Change all callers. (Symbol_table::sized_write_section_symbol): Likewise. Use SHN_XINDEX when needed. * symtab.h (class Symbol): Add is_ordinary_shndx_ field. Update declarations. (Symbol::shndx): Add is_ordinary parameter. Change all callers. (Symbol::is_defined): Check is_ordinary. (Symbol::is_undefined, Symbol::is_weak_undefined): Likewise. (Symbol::is_absolute, Symbol::is_common): Likewise. (class Sized_symbol): Update declarations. (class Symbol_table): Update declarations. * resolve.cc (Symbol::override_base): Add st_shndx and is_ordinary parameters. Change all callers. (Sized_symbol::override): Likewise. (Symbol_table::override): Likewise. (symbol_to_bits): Add is_ordinary parameter. Change all callers. (Symbol_table::resolve): Remove orig_sym parameter. Add st_shndx, is_ordinary, and orig_st_shndx parameters. Change all callers. * copy-relocs.cc (Copy_relocs::emit_copy_reloc): Require symbol to be in an ordinary section. * dwarf_reader.cc (Sized_dwarf_line_info::symbol_section): Add object and is_ordinary parameters. Change all callers. (Sized_dwarf_line_info::read_relocs): Add object parameter. Change all callers. Don't add undefined or non-ordinary symbols to reloc_map_. (Sized_dwarf_line_info::read_line_mappings): Add object parameter. Change all callers. * dwarf_reader.h (class Sized_dwarf_line_info): Update declarations. * ehframe.cc (Eh_frame::read_fde): Check for ordinary symbol. * reloc.cc (Sized_relobj::do_read_relocs): Adjust section links. (Sized_relobj::relocate_sections): Likewise. * target-reloc.h (scan_relocs): Adjust section symbol index. (scan_relocatable_relocs): Likewise. * i386.cc (Scan::local): Check for ordinary symbols. * sparc.cc (Scan::local): Likewise. * x86_64.cc (Scan::local): Likewise. * testsuite/binary_unittest.cc (Sized_binary_test): Update calls to symbol_section_and_value. * testsuite/many_sections_test.cc: New file. * testsuite/Makefile.am (BUILT_SOURCES): Define. (check_PROGRAMS): Add many_sections_test. (many_sections_test_SOURCES): Define. (many_sections_test_DEPENDENCIES): Define. (many_sections_test_LDFLAGS): Define. (BUILT_SOURCES): Add many_sections_define.h. (many_sections_define.h): New target. (BUILT_SOURCES): Add many_sections_check.h. (many_sections_check.h): New target. (check_PROGRAMS): Add many_sections_r_test. (many_sections_r_test_SOURCES): Define. (many_sections_r_test_DEPENDENCIES): Define. (many_sections_r_test_LDFLAGS): Define. (many_sections_r_test_LDADD): Define. (many_sections_r_test.o): New target. * testsuite/Makefile.in: Rebuild.
153 lines
4.2 KiB
C++
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());
|
|
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.
|