Finished layout code.
[binutils.git] / gold / object.h
blobe3df36d7a45e558b2e3bb7fc7c10091eef35a617
1 // object.h -- support for an object file for linking in gold -*- C++ -*-
3 #ifndef GOLD_OBJECT_H
4 #define GOLD_OBJECT_H
6 #include <cassert>
7 #include <vector>
9 #include "elfcpp.h"
10 #include "fileread.h"
11 #include "target.h"
12 #include "symtab.h"
14 namespace gold
17 class Stringpool;
18 class Output_section;
19 class Layout;
21 // Data to pass from read_symbols() to add_symbols().
23 struct Read_symbols_data
25 // Symbol data.
26 File_view* symbols;
27 // Size of symbol data in bytes.
28 off_t symbols_size;
29 // Symbol names.
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>
40 class Object
42 public:
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,
48 off_t offset = 0)
49 : name_(name), input_file_(input_file), offset_(offset),
50 shnum_(0), is_dynamic_(is_dynamic), target_(NULL),
51 map_to_output_()
52 { }
54 virtual ~Object()
55 { }
57 // Return the name of the object as we would report it to the tuser.
58 const std::string&
59 name() const
60 { return this->name_; }
62 // Return whether this is a dynamic object.
63 bool
64 is_dynamic() const
65 { return this->is_dynamic_; }
67 // Return the target structure associated with this object.
68 Target*
69 target() const
70 { return this->target_; }
72 // Lock the underlying file.
73 void
74 lock()
75 { this->input_file_->file().lock(); }
77 // Unlock the underlying file.
78 void
79 unlock()
80 { this->input_file_->file().unlock(); }
82 // Return whether the underlying file is locked.
83 bool
84 is_locked() const
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>*
93 sized_target();
94 #endif
96 // Read the symbol and relocation information.
97 Read_symbols_data
98 read_symbols()
99 { return this->do_read_symbols(); }
101 // Add symbol information to the global symbol table.
102 void
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.
108 void
109 layout(Layout* lay)
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.
115 off_t
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.
122 struct Map_to_output
124 // The output section. This is NULL if the input section is to be
125 // discarded.
126 Output_section* output_section;
127 // The offset within the output section.
128 off_t offset;
131 // Given a section index, return the corresponding Map_to_output
132 // information.
133 const Map_to_output*
134 section_output_info(unsigned int shnum) const
135 { return &this->map_to_output_[shnum]; }
137 protected:
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
143 // child class.
144 virtual void
145 do_add_symbols(Symbol_table*, Read_symbols_data) = 0;
147 // Lay out sections--implemented by child class.
148 virtual void
149 do_layout(Layout*) = 0;
151 // Finalize local symbols--implemented by child class.
152 virtual off_t
153 do_finalize_local_symbols(off_t, Stringpool*) = 0;
155 // Get the file.
156 Input_file*
157 input_file() const
158 { return this->input_file_; }
160 // Get the offset into the file.
161 off_t
162 offset() const
163 { return this->offset_; }
165 // Get a view into the underlying file.
166 const unsigned char*
167 get_view(off_t start, off_t size);
169 // Get the number of sections.
170 unsigned int
171 shnum() const
172 { return this->shnum_; }
174 // Set the number of sections.
175 void
176 set_shnum(int shnum)
177 { this->shnum_ = shnum; }
179 // Set the target.
180 void
181 set_target(Target* target)
182 { this->target_ = target; }
184 // Read data from the underlying file.
185 void
186 read(off_t start, off_t size, void* p);
188 // Get a lasting view into the underlying file.
189 File_view*
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>&
194 map_to_output()
195 { return this->map_to_output_; }
197 private:
198 // This class may not be copied.
199 Object(const Object&);
200 Object& operator=(const Object&);
202 // Name of object as printed to use.
203 std::string name_;
204 // For reading the file.
205 Input_file* input_file_;
206 // Offset within the file--0 for an object file, non-0 for an
207 // archive.
208 off_t offset_;
209 // Number of input sections.
210 unsigned int shnum_;
211 // Whether this is a dynamic object.
212 bool is_dynamic_;
213 // Target functions--may be NULL if the target is not known.
214 Target* target_;
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_);
233 #endif
235 // A regular object file. This is size and endian specific.
237 template<int size, bool big_endian>
238 class Sized_object : public Object
240 public:
241 Sized_object(const std::string& name, Input_file* input_file, off_t offset,
242 const typename elfcpp::Ehdr<size, big_endian>&);
244 ~Sized_object();
246 // Set up the object file based on the ELF header.
247 void
248 setup(const typename elfcpp::Ehdr<size, big_endian>&);
250 // Read the symbols.
251 Read_symbols_data
252 do_read_symbols();
254 // Add the symbols to the symbol table.
255 void
256 do_add_symbols(Symbol_table*, Read_symbols_data);
258 // Lay out the input sections.
259 void
260 do_layout(Layout*);
262 // Finalize the local symbols.
263 off_t
264 do_finalize_local_symbols(off_t, Stringpool*);
266 // Return the appropriate Sized_target structure.
267 Sized_target<size, big_endian>*
268 sized_target()
270 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
271 return this->Object::sized_target<size, big_endian>();
272 #else
273 return static_cast<Sized_target<size, big_endian>*>(this->target());
274 #endif
277 private:
278 // This object may not be copied.
279 Sized_object(const Sized_object&);
280 Sized_object& operator=(const Sized_object&);
282 // For convenience.
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.
290 const unsigned char*
291 section_header(unsigned int shnum);
293 // Whether to include a section group in the link.
294 bool
295 include_section_group(Layout*, unsigned int,
296 const elfcpp::Shdr<size, big_endian>&,
297 std::vector<bool>*);
299 // Whether to include a linkonce section in the link.
300 bool
301 include_linkonce_section(Layout*, const char*,
302 const elfcpp::Shdr<size, big_endian>&);
304 // ELF file header e_flags field.
305 unsigned int flags_;
306 // File offset of section header table.
307 off_t shoff_;
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.
313 Symbol** symbols_;
314 // File offset for local symbols.
315 off_t local_symbol_offset_;
318 // A class to manage the list of all objects.
320 class Input_objects
322 public:
323 Input_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.
331 void
332 add_object(Object*);
334 // Get the target we should use for the output file.
335 Target*
336 target() const
337 { return this->target_; }
339 // Iterate over all objects.
340 Object_list::const_iterator
341 begin() const
342 { return this->object_list_.begin(); }
344 Object_list::const_iterator
345 end() const
346 { return this->object_list_.end(); }
348 // Return whether we have seen any dynamic objects.
349 bool
350 any_dynamic() const
351 { return this->any_dynamic_; }
353 private:
354 Input_objects(const Input_objects&);
355 Input_objects& operator=(const Input_objects&);
357 Object_list object_list_;
358 Target* target_;
359 bool any_dynamic_;
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,
367 off_t bytes);
369 } // end namespace gold
371 #endif // !defined(GOLD_OBJECT_H)