1 // symtab.h -- the gold symbol table -*- C++ -*-
11 #include "stringpool.h"
23 template<int size
, bool big_endian
>
26 // The base class of an entry in the symbol table. The symbol table
27 // can have a lot of entries, so we don't want this class to big.
28 // Size dependent fields can be found in the template class
29 // Sized_symbol. Targets may support their own derived classes.
34 // Return the symbol name.
37 { return this->name_
; }
39 // Return the symbol version. This will return NULL for an
40 // unversioned symbol.
43 { return this->version_
; }
45 // Return the object with which this symbol is associated.
48 { return this->object_
; }
50 // Return the symbol binding.
53 { return this->binding_
; }
55 // Return the symbol type.
58 { return this->type_
; }
60 // Return the symbol visibility.
63 { return this->visibility_
; }
65 // Return the non-visibility part of the st_other field.
68 { return this->other_
; }
70 // Return the section index.
73 { return this->shnum_
; }
75 // Return whether this symbol is a forwarder. This will never be
76 // true of a symbol found in the hash table, but may be true of
77 // symbol pointers attached to object files.
80 { return this->is_forwarder_
; }
82 // Mark this symbol as a forwarder.
85 { this->is_forwarder_
= true; }
87 // Return whether this symbol was seen in a dynamic object.
90 { return this->in_dyn_
; }
92 // Mark this symbol as seen in a dynamic object.
95 { this->in_dyn_
= true; }
97 // Return whether this symbol needs an entry in the dynamic symbol
98 // table. FIXME: Needs to be fleshed out.
101 { return this->in_dyn_
; }
104 // Instances of this class should always be created at a specific
109 // Initialize fields from an ELF symbol in OBJECT.
110 template<int size
, bool big_endian
>
112 init_base(const char *name
, const char* version
, Object
* object
,
113 const elfcpp::Sym
<size
, big_endian
>&);
115 // Override existing symbol.
116 template<int size
, bool big_endian
>
118 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
121 Symbol(const Symbol
&);
122 Symbol
& operator=(const Symbol
&);
124 // Symbol name (expected to point into a Stringpool).
126 // Symbol version (expected to point into a Stringpool). This may
128 const char* version_
;
129 // Object in which symbol is defined, or in which it was first seen.
131 // Section number in object_ in which symbol is defined.
134 elfcpp::STT type_
: 4;
136 elfcpp::STB binding_
: 4;
137 // Symbol visibility.
138 elfcpp::STV visibility_
: 2;
139 // Rest of symbol st_other field.
140 unsigned int other_
: 6;
141 // True if this symbol always requires special target-specific
143 bool is_special_
: 1;
144 // True if this is the default version of the symbol.
146 // True if this symbol really forwards to another symbol. This is
147 // used when we discover after the fact that two different entries
148 // in the hash table really refer to the same symbol. This will
149 // never be set for a symbol found in the hash table, but may be set
150 // for a symbol found in the list of symbols attached to an Object.
151 // It forwards to the symbol found in the forwarders_ map of
153 bool is_forwarder_
: 1;
154 // True if we've seen this symbol in a dynamic object.
158 // The parts of a symbol which are size specific. Using a template
159 // derived class like this helps us use less space on a 32-bit system.
162 class Sized_symbol
: public Symbol
165 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
166 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
171 // Initialize fields from an ELF symbol in OBJECT.
172 template<bool big_endian
>
174 init(const char *name
, const char* version
, Object
* object
,
175 const elfcpp::Sym
<size
, big_endian
>&);
177 // Override existing symbol.
178 template<bool big_endian
>
180 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
182 // Return the symbol's value.
185 { return this->value_
; }
187 // Return the symbol's size (we can't call this 'size' because that
188 // is a template parameter).
191 { return this->size_
; }
193 // Set the symbol value. This is called when we store the final
194 // values of the symbols into the symbol table.
196 set_value(Value_type value
)
197 { this->value_
= value
; }
200 Sized_symbol(const Sized_symbol
&);
201 Sized_symbol
& operator=(const Sized_symbol
&);
209 // The main linker symbol table.
218 // Add COUNT external symbols from OBJECT to the symbol table. SYMS
219 // is the symbols, SYM_NAMES is their names, SYM_NAME_SIZE is the
220 // size of SYM_NAMES. This sets SYMPOINTERS to point to the symbols
221 // in the symbol table.
222 template<int size
, bool big_endian
>
224 add_from_object(Sized_object
<size
, big_endian
>* object
,
225 const elfcpp::Sym
<size
, big_endian
>* syms
,
226 size_t count
, const char* sym_names
, size_t sym_name_size
,
227 Symbol
** sympointers
);
231 lookup(const char*, const char* version
= NULL
) const;
233 // Return the real symbol associated with the forwarder symbol FROM.
235 resolve_forwards(Symbol
* from
) const;
237 // Return the size of the symbols in the table.
240 { return this->size_
; }
242 // Return the sized version of a symbol in this table.
245 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
248 const Sized_symbol
<size
>*
249 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
251 // Finalize the symbol table after we have set the final addresses
252 // of all the input sections. This sets the final symbol values and
253 // adds the names to *POOL. It records the file offset OFF, and
254 // returns the new file offset.
256 finalize(off_t
, Stringpool
*);
258 // Write out the global symbols.
260 write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
263 Symbol_table(const Symbol_table
&);
264 Symbol_table
& operator=(const Symbol_table
&);
266 // Set the size of the symbols in the table.
269 { this->size_
= size
; }
271 // Make FROM a forwarder symbol to TO.
273 make_forwarder(Symbol
* from
, Symbol
* to
);
276 template<int size
, bool big_endian
>
278 add_from_object(Sized_object
<size
, big_endian
>*, const char *name
,
279 const char *version
, bool def
,
280 const elfcpp::Sym
<size
, big_endian
>& sym
);
283 template<int size
, bool big_endian
>
285 resolve(Sized_symbol
<size
>* to
,
286 const elfcpp::Sym
<size
, big_endian
>& sym
,
289 template<int size
, bool big_endian
>
291 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
294 // Finalize symbols specialized for size.
297 sized_finalize(off_t
, Stringpool
*);
299 // Write globals specialized for size and endianness.
300 template<int size
, bool big_endian
>
302 sized_write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
304 // The type of the symbol hash table.
306 typedef std::pair
<const char*, const char*> Symbol_table_key
;
308 struct Symbol_table_hash
311 operator()(const Symbol_table_key
&) const;
314 struct Symbol_table_eq
317 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
320 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
321 Symbol_table_eq
> Symbol_table_type
;
323 // The size of the symbols in the symbol table (32 or 64).
326 // The file offset within the output symtab section where we should
330 // The number of global symbols we want to write out.
331 size_t output_count_
;
333 // The symbol hash table.
334 Symbol_table_type table_
;
336 // A pool of symbol names. This is used for all global symbols.
337 // Entries in the hash table point into this pool.
338 Stringpool namepool_
;
340 // Forwarding symbols.
341 Unordered_map
<Symbol
*, Symbol
*> forwarders_
;
344 // We inline get_sized_symbol for efficiency.
348 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
350 assert(size
== this->get_size());
351 return static_cast<Sized_symbol
<size
>*>(sym
);
355 const Sized_symbol
<size
>*
356 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
358 assert(size
== this->get_size());
359 return static_cast<const Sized_symbol
<size
>*>(sym
);
362 } // End namespace gold.
364 #endif // !defined(GOLD_SYMTAB_H)