gold/
[binutils-gdb.git] / gold / testsuite / binary_unittest.cc
1 // binary_unittest.cc -- test Binary_to_elf
2
3 // Copyright 2008, 2012 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 #include "descriptors.h"
37
38 #include "test.h"
39 #include "testfile.h"
40
41 namespace gold_testsuite
42 {
43
44 using namespace gold;
45
46 template<int size, bool big_endian>
47 bool
48 Sized_binary_test()
49 {
50 parameters_clear_target();
51 // We need a pretend Task.
52 const Task* task = reinterpret_cast<const Task*>(-1);
53
54 // Use the executable itself as the binary data.
55 struct stat st;
56 CHECK(::stat(gold::program_name, &st) == 0);
57 int o = open_descriptor(-1, gold::program_name, O_RDONLY);
58 CHECK(o >= 0);
59 unsigned char* filedata = new unsigned char[st.st_size];
60 CHECK(::read(o, filedata, st.st_size) == st.st_size);
61 CHECK(::close(o) == 0);
62
63 Binary_to_elf binary(static_cast<elfcpp::EM>(0xffff), size, big_endian,
64 gold::program_name);
65
66 CHECK(binary.convert(task));
67
68 Input_file input_file(task, "test.o", binary.converted_data(),
69 binary.converted_size());
70 Object* object = make_elf_object("test.o", &input_file, 0,
71 binary.converted_data(),
72 binary.converted_size(), NULL);
73 CHECK(object != NULL);
74 if (object == NULL)
75 return false;
76
77 CHECK(!object->is_dynamic());
78 CHECK(object->shnum() == 5);
79 CHECK(object->section_name(1) == ".data");
80 CHECK(object->section_flags(1) == (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE));
81 section_size_type len;
82 const unsigned char* contents = object->section_contents(1, &len, false);
83 CHECK(len == convert_to_section_size_type(st.st_size));
84 CHECK(memcmp(filedata, contents, len) == 0);
85
86 // Force the symbols to be read internally, so that
87 // symbol_section_and_value will work.
88 Read_symbols_data sd;
89 object->read_symbols(&sd);
90 delete sd.section_headers;
91 sd.section_headers = NULL;
92 delete sd.section_names;
93 sd.section_names = NULL;
94 delete sd.symbols;
95 sd.symbols = NULL;
96 delete sd.symbol_names;
97 sd.symbol_names = NULL;
98
99 Sized_relobj_file<size, big_endian>* relobj =
100 static_cast<Sized_relobj_file<size, big_endian>*>(object);
101 typename Sized_relobj_file<size, big_endian>::Address value;
102 bool is_ordinary;
103 CHECK(relobj->symbol_section_and_value(0, &value, &is_ordinary) == 0);
104 CHECK(is_ordinary);
105 CHECK(value == 0);
106 CHECK(relobj->symbol_section_and_value(1, &value, &is_ordinary) == 1);
107 CHECK(is_ordinary);
108 CHECK(value == 0);
109 CHECK(relobj->symbol_section_and_value(2, &value, &is_ordinary) == 1);
110 CHECK(is_ordinary);
111 CHECK(static_cast<off_t>(value) == st.st_size);
112 CHECK(relobj->symbol_section_and_value(3, &value, &is_ordinary)
113 == elfcpp::SHN_ABS);
114 CHECK(!is_ordinary);
115 CHECK(static_cast<off_t>(value) == st.st_size);
116
117 object->unlock(task);
118 return true;
119 }
120
121 bool
122 Binary_test(Test_report*)
123 {
124 Errors errors(gold::program_name);
125 set_parameters_errors(&errors);
126
127 General_options options;
128 set_parameters_options(&options);
129
130 int fail = 0;
131
132 #ifdef HAVE_TARGET_32_LITTLE
133 if (!Sized_binary_test<32, false>())
134 ++fail;
135 CHECK(&parameters->target() == target_test_pointer_32_little);
136 #endif
137
138 #ifdef HAVE_TARGET_32_BIG
139 if (!Sized_binary_test<32, true>())
140 ++fail;
141 CHECK(&parameters->target() == target_test_pointer_32_big);
142 #endif
143
144 #ifdef HAVE_TARGET_64_LITTLE
145 if (!Sized_binary_test<64, false>())
146 ++fail;
147 CHECK(&parameters->target() == target_test_pointer_64_little);
148 #endif
149
150 #ifdef HAVE_TARGET_64_BIG
151 if (!Sized_binary_test<64, true>())
152 ++fail;
153 CHECK(&parameters->target() == target_test_pointer_64_big);
154 #endif
155
156 return fail == 0;
157 }
158
159 Register_test binary_register("Binary", Binary_test);
160
161 } // End namespace gold_testsuite.