* options.h (class General_options): Define
[binutils-gdb.git] / gold / target.cc
1 // target.cc
2
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@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 #include "target.h"
25 #include "dynobj.h"
26 #include "elfcpp.h"
27
28 namespace gold
29 {
30
31 // Return whether NAME is a local label name. This is used to implement the
32 // --discard-locals options and can be overriden by children classes to
33 // implement system-specific behaviour. The logic here is the same as that
34 // in _bfd_elf_is_local_label_name().
35
36 bool
37 Target::do_is_local_label_name (const char* name) const
38 {
39 // Normal local symbols start with ``.L''.
40 if (name[0] == '.' && name[1] == 'L')
41 return true;
42
43 // At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
44 // DWARF debugging symbols starting with ``..''.
45 if (name[0] == '.' && name[1] == '.')
46 return true;
47
48 // gcc will sometimes generate symbols beginning with ``_.L_'' when
49 // emitting DWARF debugging output. I suspect this is actually a
50 // small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
51 // ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
52 // underscore to be emitted on some ELF targets). For ease of use,
53 // we treat such symbols as local.
54 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
55 return true;
56
57 return false;
58 }
59
60 // Implementations of methods Target::do_make_elf_object are almost identical
61 // except for the address sizes and endianities. So we extract this
62 // into a template.
63
64 template<int size, bool big_endian>
65 inline Object*
66 Target::do_make_elf_object_implementation(
67 const std::string& name,
68 Input_file* input_file,
69 off_t offset,
70 const elfcpp::Ehdr<size, big_endian>& ehdr)
71 {
72 int et = ehdr.get_e_type();
73 if (et == elfcpp::ET_REL)
74 {
75 Sized_relobj<size, big_endian>* obj =
76 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
77 obj->setup();
78 return obj;
79 }
80 else if (et == elfcpp::ET_DYN)
81 {
82 Sized_dynobj<size, big_endian>* obj =
83 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
84 obj->setup();
85 return obj;
86 }
87 else
88 {
89 gold_error(_("%s: unsupported ELF file type %d"),
90 name.c_str(), et);
91 return NULL;
92 }
93 }
94
95 // Make an ELF object called NAME by reading INPUT_FILE at OFFSET. EHDR
96 // is the ELF header of the object. There are four versions of this
97 // for different address sizes and endianities.
98
99 #ifdef HAVE_TARGET_32_LITTLE
100 Object*
101 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
102 off_t offset, const elfcpp::Ehdr<32, false>& ehdr)
103 {
104 return this->do_make_elf_object_implementation<32, false>(name, input_file,
105 offset, ehdr);
106 }
107 #endif
108
109 #ifdef HAVE_TARGET_32_BIG
110 Object*
111 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
112 off_t offset, const elfcpp::Ehdr<32, true>& ehdr)
113 {
114 return this->do_make_elf_object_implementation<32, true>(name, input_file,
115 offset, ehdr);
116 }
117 #endif
118
119 #ifdef HAVE_TARGET_64_LITTLE
120 Object*
121 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
122 off_t offset, const elfcpp::Ehdr<64, false>& ehdr)
123 {
124 return this->do_make_elf_object_implementation<64, false>(name, input_file,
125 offset, ehdr);
126 }
127 #endif
128
129 #ifdef HAVE_TARGET_64_BIG
130 Object*
131 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
132 off_t offset, const elfcpp::Ehdr<64, true>& ehdr)
133 {
134 return this->do_make_elf_object_implementation<64, true>(name, input_file,
135 offset, ehdr);
136 }
137 #endif
138
139 // Default conversion for -fsplit-stack is to give an error.
140
141 void
142 Target::do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
143 section_size_type, unsigned char*, section_size_type,
144 std::string*, std::string*) const
145 {
146 static bool warned;
147 if (!warned)
148 {
149 gold_error(_("linker does not include stack split support "
150 "required by %s"),
151 object->name().c_str());
152 warned = true;
153 }
154 }
155
156 // Return whether BYTES/LEN matches VIEW/VIEW_SIZE at OFFSET.
157
158 bool
159 Target::match_view(const unsigned char* view, section_size_type view_size,
160 section_offset_type offset, const char* bytes,
161 size_t len) const
162 {
163 if (offset + len > view_size)
164 return false;
165 return memcmp(view + offset, bytes, len) == 0;
166 }
167
168 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
169 // for LEN bytes.
170
171 void
172 Target::set_view_to_nop(unsigned char* view, section_size_type view_size,
173 section_offset_type offset, size_t len) const
174 {
175 gold_assert(offset >= 0 && offset + len <= view_size);
176 if (!this->has_code_fill())
177 memset(view + offset, 0, len);
178 else
179 {
180 std::string fill = this->code_fill(len);
181 memcpy(view + offset, fill.data(), len);
182 }
183 }
184
185 } // End namespace gold.