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