1 // object.h -- support for an object file for linking in gold -*- C++ -*-
21 // Data to pass from read_symbols() to add_symbols().
23 struct Read_symbols_data
27 // Size of symbol data in bytes.
30 File_view
* symbol_names
;
31 // Size of symbol name data in bytes.
32 off_t symbol_names_size
;
35 // Object is an interface which represents either a 32-bit or a 64-bit
36 // input object. This can be a regular object file (ET_REL) or a
37 // shared object (ET_DYN). The actual instantiations are
38 // Sized_object<32> and Sized_object<64>
43 // NAME is the name of the object as we would report it to the user
44 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
45 // used to read the file. OFFSET is the offset within the input
46 // file--0 for a .o or .so file, something else for a .a file.
47 Object(const std::string
& name
, Input_file
* input_file
, bool is_dynamic
,
49 : name_(name
), input_file_(input_file
), offset_(offset
),
50 shnum_(0), is_dynamic_(is_dynamic
), target_(NULL
),
57 // Return the name of the object as we would report it to the tuser.
60 { return this->name_
; }
62 // Return whether this is a dynamic object.
65 { return this->is_dynamic_
; }
67 // Return the target structure associated with this object.
70 { return this->target_
; }
72 // Lock the underlying file.
75 { this->input_file_
->file().lock(); }
77 // Unlock the underlying file.
80 { this->input_file_
->file().unlock(); }
82 // Return whether the underlying file is locked.
85 { return this->input_file_
->file().is_locked(); }
87 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
88 // Return the sized target structure associated with this object.
89 // This is like the target method but it returns a pointer of
90 // appropriate checked type.
91 template<int size
, bool big_endian
>
92 Sized_target
<size
, big_endian
>*
96 // Read the symbol and relocation information.
99 { return this->do_read_symbols(); }
101 // Add symbol information to the global symbol table.
103 add_symbols(Symbol_table
* symtab
, Read_symbols_data rd
)
104 { this->do_add_symbols(symtab
, rd
); }
106 // Pass sections which should be included in the link to the Layout
107 // object, and record where the sections go in the output file.
110 { this->do_layout(lay
); }
112 // Initial local symbol processing: set the offset where local
113 // symbol information will be stored; add local symbol names to
114 // *POOL; return the offset following the local symbols.
116 finalize_local_symbols(off_t off
, Stringpool
* pool
)
117 { return this->do_finalize_local_symbols(off
, pool
); }
119 // What we need to know to map an input section to an output
120 // section. We keep an array of these, one for each input section,
121 // indexed by the input section number.
124 // The output section. This is NULL if the input section is to be
126 Output_section
* output_section
;
127 // The offset within the output section.
131 // Given a section index, return the corresponding Map_to_output
134 section_output_info(unsigned int shnum
) const
135 { return &this->map_to_output_
[shnum
]; }
138 // Read the symbols--implemented by child class.
139 virtual Read_symbols_data
140 do_read_symbols() = 0;
142 // Add symbol information to the global symbol table--implemented by
145 do_add_symbols(Symbol_table
*, Read_symbols_data
) = 0;
147 // Lay out sections--implemented by child class.
149 do_layout(Layout
*) = 0;
151 // Finalize local symbols--implemented by child class.
153 do_finalize_local_symbols(off_t
, Stringpool
*) = 0;
158 { return this->input_file_
; }
160 // Get the offset into the file.
163 { return this->offset_
; }
165 // Get a view into the underlying file.
167 get_view(off_t start
, off_t size
);
169 // Get the number of sections.
172 { return this->shnum_
; }
174 // Set the number of sections.
177 { this->shnum_
= shnum
; }
181 set_target(Target
* target
)
182 { this->target_
= target
; }
184 // Read data from the underlying file.
186 read(off_t start
, off_t size
, void* p
);
188 // Get a lasting view into the underlying file.
190 get_lasting_view(off_t start
, off_t size
);
192 // Return the vector mapping input sections to output sections.
193 std::vector
<Map_to_output
>&
195 { return this->map_to_output_
; }
198 // This class may not be copied.
199 Object(const Object
&);
200 Object
& operator=(const Object
&);
202 // Name of object as printed to use.
204 // For reading the file.
205 Input_file
* input_file_
;
206 // Offset within the file--0 for an object file, non-0 for an
209 // Number of input sections.
211 // Whether this is a dynamic object.
213 // Target functions--may be NULL if the target is not known.
215 // Mapping from input sections to output section.
216 std::vector
<Map_to_output
> map_to_output_
;
219 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
221 // Implement sized_target inline for efficiency. This approach breaks
222 // static type checking, but is made safe using asserts.
224 template<int size
, bool big_endian
>
225 inline Sized_target
<size
, big_endian
>*
226 Object::sized_target()
228 assert(this->target_
->get_size() == size
);
229 assert(this->target_
->is_big_endian() ? big_endian
: !big_endian
);
230 return static_cast<Sized_target
<size
, big_endian
>*>(this->target_
);
235 // A regular object file. This is size and endian specific.
237 template<int size
, bool big_endian
>
238 class Sized_object
: public Object
241 Sized_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
242 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
246 // Set up the object file based on the ELF header.
248 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
254 // Add the symbols to the symbol table.
256 do_add_symbols(Symbol_table
*, Read_symbols_data
);
258 // Lay out the input sections.
262 // Finalize the local symbols.
264 do_finalize_local_symbols(off_t
, Stringpool
*);
266 // Return the appropriate Sized_target structure.
267 Sized_target
<size
, big_endian
>*
270 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
271 return this->Object::sized_target
<size
, big_endian
>();
273 return static_cast<Sized_target
<size
, big_endian
>*>(this->target());
278 // This object may not be copied.
279 Sized_object(const Sized_object
&);
280 Sized_object
& operator=(const Sized_object
&);
283 typedef Sized_object
<size
, big_endian
> This
;
284 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
285 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
286 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
287 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
289 // Read the section header for section SHNUM.
291 section_header(unsigned int shnum
);
293 // Whether to include a section group in the link.
295 include_section_group(Layout
*, unsigned int,
296 const elfcpp::Shdr
<size
, big_endian
>&,
299 // Whether to include a linkonce section in the link.
301 include_linkonce_section(Layout
*, const char*,
302 const elfcpp::Shdr
<size
, big_endian
>&);
304 // ELF file header e_flags field.
306 // File offset of section header table.
308 // Offset of SHT_STRTAB section holding section names.
309 unsigned int shstrndx_
;
310 // Index of SHT_SYMTAB section.
311 unsigned int symtab_shnum_
;
312 // The entries in the symbol table for the external symbols.
314 // File offset for local symbols.
315 off_t local_symbol_offset_
;
318 // A class to manage the list of all objects.
324 : object_list_(), target_(NULL
), any_dynamic_(false)
327 // The type of the list of input objects.
328 typedef std::list
<Object
*> Object_list
;
330 // Add an object to the list.
334 // Get the target we should use for the output file.
337 { return this->target_
; }
339 // Iterate over all objects.
340 Object_list::const_iterator
342 { return this->object_list_
.begin(); }
344 Object_list::const_iterator
346 { return this->object_list_
.end(); }
348 // Return whether we have seen any dynamic objects.
351 { return this->any_dynamic_
; }
354 Input_objects(const Input_objects
&);
355 Input_objects
& operator=(const Input_objects
&);
357 Object_list object_list_
;
362 // Return an Object appropriate for the input file. P is BYTES long,
363 // and holds the ELF header.
365 extern Object
* make_elf_object(const std::string
& name
, Input_file
*,
366 off_t offset
, const unsigned char* p
,
369 } // end namespace gold
371 #endif // !defined(GOLD_OBJECT_H)