From Craig Silverstein: rework handling of Script_options.
[deliverable/binutils-gdb.git] / gold / testsuite / binary_unittest.cc
1 // binary_unittest.cc -- test Binary_to_elf
2
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include "elfcpp.h"
31 #include "parameters.h"
32 #include "errors.h"
33 #include "options.h"
34 #include "binary.h"
35 #include "object.h"
36
37 #include "test.h"
38 #include "testfile.h"
39
40 namespace gold_testsuite
41 {
42
43 using namespace gold;
44
45 template<int size, bool big_endian>
46 bool
47 Sized_binary_test(Target* target)
48 {
49 // We need a pretend Task.
50 const Task* task = reinterpret_cast<const Task*>(-1);
51
52 // Use the executable itself as the binary data.
53 struct stat st;
54 CHECK(::stat(gold::program_name, &st) == 0);
55 int o = ::open(gold::program_name, O_RDONLY);
56 CHECK(o >= 0);
57 unsigned char* filedata = new unsigned char[st.st_size];
58 CHECK(::read(o, filedata, st.st_size) == st.st_size);
59 CHECK(::close(o) == 0);
60
61 Binary_to_elf binary(static_cast<elfcpp::EM>(0xffff), size, big_endian,
62 gold::program_name);
63
64 CHECK(binary.convert(task));
65
66 Input_file input_file(task, "test.o", binary.converted_data(),
67 binary.converted_size());
68 Object* object = make_elf_object("test.o", &input_file, 0,
69 binary.converted_data(),
70 binary.converted_size());
71 CHECK(object != NULL);
72 if (object == NULL)
73 return false;
74
75 CHECK(!object->is_dynamic());
76 CHECK(object->target() == target);
77 CHECK(object->shnum() == 5);
78 CHECK(object->section_name(1) == ".data");
79 CHECK(object->section_flags(1) == (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE));
80 section_size_type len;
81 const unsigned char* contents = object->section_contents(1, &len, false);
82 CHECK(len == convert_to_section_size_type(st.st_size));
83 CHECK(memcmp(filedata, contents, len) == 0);
84
85 // Force the symbols to be read internally, so that
86 // symbol_section_and_value will work.
87 Read_symbols_data sd;
88 object->read_symbols(&sd);
89 delete sd.section_headers;
90 delete sd.section_names;
91 delete sd.symbols;
92 delete sd.symbol_names;
93
94 Sized_relobj<size, big_endian>* relobj =
95 static_cast<Sized_relobj<size, big_endian>*>(object);
96 typename Sized_relobj<size, big_endian>::Address value;
97 CHECK(relobj->symbol_section_and_value(0, &value) == 0);
98 CHECK(value == 0);
99 CHECK(relobj->symbol_section_and_value(1, &value) == 1);
100 CHECK(value == 0);
101 CHECK(relobj->symbol_section_and_value(2, &value) == 1);
102 CHECK(static_cast<off_t>(value) == st.st_size);
103 CHECK(relobj->symbol_section_and_value(3, &value) == elfcpp::SHN_ABS);
104 CHECK(static_cast<off_t>(value) == st.st_size);
105
106 object->unlock(task);
107 return true;
108 }
109
110 bool
111 Binary_test(Test_report*)
112 {
113 Errors errors(gold::program_name);
114 initialize_parameters(&errors);
115
116 General_options options;
117 set_parameters_from_options(&options);
118
119 int fail = 0;
120
121 #ifdef HAVE_TARGET_32_LITTLE
122 if (!Sized_binary_test<32, false>(target_test_pointer_32_little))
123 ++fail;
124 #endif
125
126 #ifdef HAVE_TARGET_32_BIG
127 if (!Sized_binary_test<32, true>(target_test_pointer_32_big))
128 ++fail;
129 #endif
130
131 #ifdef HAVE_TARGET_64_LITTLE
132 if (!Sized_binary_test<64, false>(target_test_pointer_64_little))
133 ++fail;
134 #endif
135
136 #ifdef HAVE_TARGET_64_BIG
137 if (!Sized_binary_test<64, true>(target_test_pointer_64_big))
138 ++fail;
139 #endif
140
141 return fail == 0;
142 }
143
144 Register_test binary_register("Binary", Binary_test);
145
146 } // End namespace gold_testsuite.
This page took 0.04482 seconds and 5 git commands to generate.