1 // object.h -- support for an object file for linking in gold -*- C++ -*-
22 // Data to pass from read_symbols() to add_symbols().
24 struct Read_symbols_data
28 // Size of symbol data in bytes.
31 File_view
* symbol_names
;
32 // Size of symbol name data in bytes.
33 off_t symbol_names_size
;
36 // Object is an interface which represents either a 32-bit or a 64-bit
37 // input object. This can be a regular object file (ET_REL) or a
38 // shared object (ET_DYN). The actual instantiations are
39 // Sized_object<32> and Sized_object<64>
44 // NAME is the name of the object as we would report it to the user
45 // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
46 // used to read the file. OFFSET is the offset within the input
47 // file--0 for a .o or .so file, something else for a .a file.
48 Object(const std::string
& name
, Input_file
* input_file
, bool is_dynamic
,
50 : name_(name
), input_file_(input_file
), offset_(offset
),
51 shnum_(0), is_dynamic_(is_dynamic
), target_(NULL
),
58 // Return the name of the object as we would report it to the tuser.
61 { return this->name_
; }
63 // Return whether this is a dynamic object.
66 { return this->is_dynamic_
; }
68 // Return the target structure associated with this object.
71 { return this->target_
; }
73 // Lock the underlying file.
76 { this->input_file_
->file().lock(); }
78 // Unlock the underlying file.
81 { this->input_file_
->file().unlock(); }
83 // Return whether the underlying file is locked.
86 { return this->input_file_
->file().is_locked(); }
88 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
89 // Return the sized target structure associated with this object.
90 // This is like the target method but it returns a pointer of
91 // appropriate checked type.
92 template<int size
, bool big_endian
>
93 Sized_target
<size
, big_endian
>*
97 // Read the symbol and relocation information.
100 { return this->do_read_symbols(); }
102 // Add symbol information to the global symbol table.
104 add_symbols(Symbol_table
* symtab
, Read_symbols_data rd
)
105 { this->do_add_symbols(symtab
, rd
); }
107 // Pass sections which should be included in the link to the Layout
108 // object, and record where the sections go in the output file.
111 { this->do_layout(lay
); }
113 // Initial local symbol processing: set the offset where local
114 // symbol information will be stored; add local symbol names to
115 // *POOL; return the offset following the local symbols.
117 finalize_local_symbols(off_t off
, Stringpool
* pool
)
118 { return this->do_finalize_local_symbols(off
, pool
); }
120 // Relocate the input sections and write out the local symbols.
122 relocate(const General_options
& options
, const Symbol_table
* symtab
,
123 const Stringpool
* sympool
, Output_file
* of
)
124 { return this->do_relocate(options
, symtab
, sympool
, of
); }
126 // What we need to know to map an input section to an output
127 // section. We keep an array of these, one for each input section,
128 // indexed by the input section number.
131 // The output section. This is NULL if the input section is to be
133 Output_section
* output_section
;
134 // The offset within the output section.
138 // Given a section index, return the corresponding Map_to_output
141 section_output_info(unsigned int shnum
) const
143 assert(shnum
< this->map_to_output_
.size());
144 return &this->map_to_output_
[shnum
];
148 // Read the symbols--implemented by child class.
149 virtual Read_symbols_data
150 do_read_symbols() = 0;
152 // Add symbol information to the global symbol table--implemented by
155 do_add_symbols(Symbol_table
*, Read_symbols_data
) = 0;
157 // Lay out sections--implemented by child class.
159 do_layout(Layout
*) = 0;
161 // Finalize local symbols--implemented by child class.
163 do_finalize_local_symbols(off_t
, Stringpool
*) = 0;
165 // Relocate the input sections and write out the local
166 // symbols--implemented by child class.
168 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
169 const Stringpool
*, Output_file
* of
) = 0;
174 { return this->input_file_
; }
176 // Get the offset into the file.
179 { return this->offset_
; }
181 // Get a view into the underlying file.
183 get_view(off_t start
, off_t size
);
185 // Get the number of sections.
188 { return this->shnum_
; }
190 // Set the number of sections.
193 { this->shnum_
= shnum
; }
197 set_target(Target
* target
)
198 { this->target_
= target
; }
200 // Read data from the underlying file.
202 read(off_t start
, off_t size
, void* p
);
204 // Get a lasting view into the underlying file.
206 get_lasting_view(off_t start
, off_t size
);
208 // Return the vector mapping input sections to output sections.
209 std::vector
<Map_to_output
>&
211 { return this->map_to_output_
; }
214 // This class may not be copied.
215 Object(const Object
&);
216 Object
& operator=(const Object
&);
218 // Name of object as printed to user.
220 // For reading the file.
221 Input_file
* input_file_
;
222 // Offset within the file--0 for an object file, non-0 for an
225 // Number of input sections.
227 // Whether this is a dynamic object.
229 // Target functions--may be NULL if the target is not known.
231 // Mapping from input sections to output section.
232 std::vector
<Map_to_output
> map_to_output_
;
235 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
237 // Implement sized_target inline for efficiency. This approach breaks
238 // static type checking, but is made safe using asserts.
240 template<int size
, bool big_endian
>
241 inline Sized_target
<size
, big_endian
>*
242 Object::sized_target()
244 assert(this->target_
->get_size() == size
);
245 assert(this->target_
->is_big_endian() ? big_endian
: !big_endian
);
246 return static_cast<Sized_target
<size
, big_endian
>*>(this->target_
);
251 // A regular object file. This is size and endian specific.
253 template<int size
, bool big_endian
>
254 class Sized_object
: public Object
257 Sized_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
258 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
262 // Set up the object file based on the ELF header.
264 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
270 // Add the symbols to the symbol table.
272 do_add_symbols(Symbol_table
*, Read_symbols_data
);
274 // Lay out the input sections.
278 // Finalize the local symbols.
280 do_finalize_local_symbols(off_t
, Stringpool
*);
282 // Relocate the input sections and write out the local symbols.
284 do_relocate(const General_options
& options
, const Symbol_table
* symtab
,
285 const Stringpool
*, Output_file
* of
);
287 // Return the appropriate Sized_target structure.
288 Sized_target
<size
, big_endian
>*
291 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
292 return this->Object::sized_target
<size
, big_endian
>();
294 return static_cast<Sized_target
<size
, big_endian
>*>(this->target());
299 // This object may not be copied.
300 Sized_object(const Sized_object
&);
301 Sized_object
& operator=(const Sized_object
&);
304 typedef Sized_object
<size
, big_endian
> This
;
305 static const int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
306 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
307 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
308 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
310 // Read the section header for section SHNUM.
312 section_header(unsigned int shnum
);
314 // Whether to include a section group in the link.
316 include_section_group(Layout
*, unsigned int,
317 const elfcpp::Shdr
<size
, big_endian
>&,
320 // Whether to include a linkonce section in the link.
322 include_linkonce_section(Layout
*, const char*,
323 const elfcpp::Shdr
<size
, big_endian
>&);
325 // Views and sizes when relocating.
329 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
;
334 typedef std::vector
<View_size
> Views
;
336 // Write section data to the output file. Record the views and
337 // sizes in VIEWS for use when relocating.
339 write_sections(const unsigned char* pshdrs
, Output_file
*, Views
*);
341 // Relocate the sections in the output file.
343 relocate_sections(const Symbol_table
*, const unsigned char* pshdrs
, Views
*);
345 // Write out the local symbols.
347 write_local_symbols(Output_file
*, const Stringpool
*);
349 // ELF file header e_flags field.
351 // File offset of section header table.
353 // Offset of SHT_STRTAB section holding section names.
354 unsigned int shstrndx_
;
355 // Index of SHT_SYMTAB section.
356 unsigned int symtab_shnum_
;
357 // The number of local symbols.
358 unsigned int local_symbol_count_
;
359 // The number of local symbols which go into the output file.
360 unsigned int output_local_symbol_count_
;
361 // The entries in the symbol table for the external symbols.
363 // File offset for local symbols.
364 off_t local_symbol_offset_
;
365 // Values of local symbols.
366 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*values_
;
369 // A class to manage the list of all objects.
375 : object_list_(), target_(NULL
), any_dynamic_(false)
378 // The type of the list of input objects.
379 typedef std::list
<Object
*> Object_list
;
381 // Add an object to the list.
385 // Get the target we should use for the output file.
388 { return this->target_
; }
390 // Iterate over all objects.
391 Object_list::const_iterator
393 { return this->object_list_
.begin(); }
395 Object_list::const_iterator
397 { return this->object_list_
.end(); }
399 // Return whether we have seen any dynamic objects.
402 { return this->any_dynamic_
; }
405 Input_objects(const Input_objects
&);
406 Input_objects
& operator=(const Input_objects
&);
408 Object_list object_list_
;
413 // Return an Object appropriate for the input file. P is BYTES long,
414 // and holds the ELF header.
417 make_elf_object(const std::string
& name
, Input_file
*,
418 off_t offset
, const unsigned char* p
,
421 } // end namespace gold
423 #endif // !defined(GOLD_OBJECT_H)