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; }
98 // Instances of this class should always be created at a specific
103 // Initialize fields from an ELF symbol in OBJECT.
104 template<int size
, bool big_endian
>
106 init_base(const char *name
, const char* version
, Object
* object
,
107 const elfcpp::Sym
<size
, big_endian
>&);
109 // Override existing symbol.
110 template<int size
, bool big_endian
>
112 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
115 Symbol(const Symbol
&);
116 Symbol
& operator=(const Symbol
&);
118 // Symbol name (expected to point into a Stringpool).
120 // Symbol version (expected to point into a Stringpool). This may
122 const char* version_
;
123 // Object in which symbol is defined, or in which it was first seen.
125 // Section number in object_ in which symbol is defined.
128 elfcpp::STT type_
: 4;
130 elfcpp::STB binding_
: 4;
131 // Symbol visibility.
132 elfcpp::STV visibility_
: 2;
133 // Rest of symbol st_other field.
134 unsigned int other_
: 6;
135 // True if this symbol always requires special target-specific
137 bool is_special_
: 1;
138 // True if this is the default version of the symbol.
140 // True if this symbol really forwards to another symbol. This is
141 // used when we discover after the fact that two different entries
142 // in the hash table really refer to the same symbol. This will
143 // never be set for a symbol found in the hash table, but may be set
144 // for a symbol found in the list of symbols attached to an Object.
145 // It forwards to the symbol found in the forwarders_ map of
147 bool is_forwarder_
: 1;
148 // True if we've seen this symbol in a dynamic object.
152 // The parts of a symbol which are size specific. Using a template
153 // derived class like this helps us use less space on a 32-bit system.
156 class Sized_symbol
: public Symbol
159 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
160 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
165 // Initialize fields from an ELF symbol in OBJECT.
166 template<bool big_endian
>
168 init(const char *name
, const char* version
, Object
* object
,
169 const elfcpp::Sym
<size
, big_endian
>&);
171 // Override existing symbol.
172 template<bool big_endian
>
174 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
176 // Return the symbol's value.
179 { return this->value_
; }
181 // Return the symbol's size (we can't call this 'size' because that
182 // is a template parameter).
185 { return this->size_
; }
187 // Set the symbol value. This is called when we store the final
188 // values of the symbols into the symbol table.
190 set_value(Value_type value
)
191 { this->value_
= value
; }
194 Sized_symbol(const Sized_symbol
&);
195 Sized_symbol
& operator=(const Sized_symbol
&);
203 // The main linker symbol table.
212 // Add COUNT external symbols from OBJECT to the symbol table. SYMS
213 // is the symbols, SYM_NAMES is their names, SYM_NAME_SIZE is the
214 // size of SYM_NAMES. This sets SYMPOINTERS to point to the symbols
215 // in the symbol table.
216 template<int size
, bool big_endian
>
218 add_from_object(Sized_object
<size
, big_endian
>* object
,
219 const elfcpp::Sym
<size
, big_endian
>* syms
,
220 size_t count
, const char* sym_names
, size_t sym_name_size
,
221 Symbol
** sympointers
);
225 lookup(const char*, const char* version
= NULL
) const;
227 // Return the real symbol associated with the forwarder symbol FROM.
229 resolve_forwards(Symbol
* from
) const;
231 // Return the size of the symbols in the table.
234 { return this->size_
; }
236 // Return the sized version of a symbol in this table.
239 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
242 const Sized_symbol
<size
>*
243 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
245 // Finalize the symbol table after we have set the final addresses
246 // of all the input sections. This sets the final symbol values and
247 // adds the names to *POOL. It records the file offset OFF, and
248 // returns the new file offset.
250 finalize(off_t
, Stringpool
*);
252 // Write out the global symbols.
254 write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
257 Symbol_table(const Symbol_table
&);
258 Symbol_table
& operator=(const Symbol_table
&);
260 // Set the size of the symbols in the table.
263 { this->size_
= size
; }
265 // Make FROM a forwarder symbol to TO.
267 make_forwarder(Symbol
* from
, Symbol
* to
);
270 template<int size
, bool big_endian
>
272 add_from_object(Sized_object
<size
, big_endian
>*, const char *name
,
273 const char *version
, bool def
,
274 const elfcpp::Sym
<size
, big_endian
>& sym
);
277 template<int size
, bool big_endian
>
279 resolve(Sized_symbol
<size
>* to
,
280 const elfcpp::Sym
<size
, big_endian
>& sym
,
283 template<int size
, bool big_endian
>
285 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
288 // Finalize symbols specialized for size.
291 sized_finalize(off_t
, Stringpool
*);
293 // Write globals specialized for size and endianness.
294 template<int size
, bool big_endian
>
296 sized_write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
298 // The type of the symbol hash table.
300 typedef std::pair
<const char*, const char*> Symbol_table_key
;
302 struct Symbol_table_hash
305 operator()(const Symbol_table_key
&) const;
308 struct Symbol_table_eq
311 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
314 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
315 Symbol_table_eq
> Symbol_table_type
;
317 // The size of the symbols in the symbol table (32 or 64).
320 // The file offset within the output symtab section where we should
324 // The number of global symbols we want to write out.
325 size_t output_count_
;
327 // The symbol hash table.
328 Symbol_table_type table_
;
330 // A pool of symbol names. This is used for all global symbols.
331 // Entries in the hash table point into this pool.
332 Stringpool namepool_
;
334 // Forwarding symbols.
335 Unordered_map
<Symbol
*, Symbol
*> forwarders_
;
338 // We inline get_sized_symbol for efficiency.
342 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
344 assert(size
== this->get_size());
345 return static_cast<Sized_symbol
<size
>*>(sym
);
349 const Sized_symbol
<size
>*
350 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
352 assert(size
== this->get_size());
353 return static_cast<const Sized_symbol
<size
>*>(sym
);
356 } // End namespace gold.
358 #endif // !defined(GOLD_SYMTAB_H)