OSDN Git Service

2009-08-18 Doug Kwan <dougkwan@google.com>
[pf3gnuchains/pf3gnuchains4x.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
27 namespace gold
28 {
29
30 // Return whether NAME is a local label name.  This is used to implement the
31 // --discard-locals options and can be overriden by children classes to
32 // implement system-specific behaviour.  The logic here is the same as that
33 // in _bfd_elf_is_local_label_name().
34
35 bool
36 Target::do_is_local_label_name (const char* name) const
37 {
38   // Normal local symbols start with ``.L''.
39   if (name[0] == '.' && name[1] == 'L')
40     return true;
41
42   // At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
43   // DWARF debugging symbols starting with ``..''.
44   if (name[0] == '.' && name[1] == '.')
45     return true;
46
47   // gcc will sometimes generate symbols beginning with ``_.L_'' when
48   // emitting DWARF debugging output.  I suspect this is actually a
49   // small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
50   // ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
51   // underscore to be emitted on some ELF targets).  For ease of use,
52   // we treat such symbols as local.
53   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
54     return true;
55
56   return false;
57 }
58
59 // Implementations of methods Target::do_make_elf_object are almost identical
60 // except for the address sizes and endianities.  So we extract this
61 // into a template.
62
63 template<int size, bool big_endian>
64 inline Object*
65 Target::do_make_elf_object_implementation(
66     const std::string& name,
67     Input_file* input_file,
68     off_t offset,
69     const elfcpp::Ehdr<size, big_endian>& ehdr)
70 {
71   int et = ehdr.get_e_type();
72   if (et == elfcpp::ET_REL)
73     {
74       Sized_relobj<size, big_endian>* obj =
75         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
76       obj->setup(this);
77       return obj;
78     }
79   else if (et == elfcpp::ET_DYN)
80     {
81       Sized_dynobj<size, big_endian>* obj =
82         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
83       obj->setup(this);
84       return obj;
85     }
86   else
87     {
88       gold_error(_("%s: unsupported ELF file type %d"),
89                  name.c_str(), et);
90       return NULL;
91     }
92 }
93
94 // Make an ELF object called NAME by reading INPUT_FILE at OFFSET.  EHDR
95 // is the ELF header of the object.  There are four versions of this
96 // for different address sizes and endianities.
97
98 #ifdef HAVE_TARGET_32_LITTLE
99 Object*
100 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
101                            off_t offset, const elfcpp::Ehdr<32, false>& ehdr)
102 {
103   return this->do_make_elf_object_implementation<32, false>(name, input_file,
104                                                             offset, ehdr);
105 }
106 #endif
107
108 #ifdef HAVE_TARGET_32_BIG
109 Object*
110 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
111                            off_t offset, const elfcpp::Ehdr<32, true>& ehdr)
112 {
113   return this->do_make_elf_object_implementation<32, true>(name, input_file,
114                                                            offset, ehdr);
115 }
116 #endif
117
118 #ifdef HAVE_TARGET_64_LITTLE
119 Object*
120 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
121                            off_t offset, const elfcpp::Ehdr<64, false>& ehdr)
122 {
123   return this->do_make_elf_object_implementation<64, false>(name, input_file,
124                                                             offset, ehdr);
125 }
126 #endif
127
128 #ifdef HAVE_TARGET_64_BIG
129 Object*
130 Target::do_make_elf_object(const std::string& name, Input_file* input_file,
131                            off_t offset, const elfcpp::Ehdr<64, true>& ehdr)
132 {
133   return this->do_make_elf_object_implementation<64, true>(name, input_file,
134                                                            offset, ehdr);
135 }
136 #endif
137
138 } // End namespace gold.