2006-09-27 Dave Brolley <brolley@redhat.com>
[deliverable/binutils-gdb.git] / gold / object.h
CommitLineData
bae7f79e
ILT
1// object.h -- support for an object file for linking in gold -*- C++ -*-
2
3#ifndef GOLD_OBJECT_H
4#define GOLD_OBJECT_H
5
14bfc3f5 6#include <cassert>
a2fb1b05 7#include <vector>
14bfc3f5 8
bae7f79e 9#include "elfcpp.h"
bae7f79e 10#include "fileread.h"
14bfc3f5
ILT
11#include "target.h"
12#include "symtab.h"
bae7f79e
ILT
13
14namespace gold
15{
16
a2fb1b05
ILT
17class Output_section;
18class Layout;
19
bae7f79e
ILT
20// Data to pass from read_symbols() to add_symbols().
21
22struct Read_symbols_data
23{
24 // Symbol data.
25 File_view* symbols;
26 // Size of symbol data in bytes.
27 off_t symbols_size;
54dc6425
ILT
28 // Index of first global symbol.
29 unsigned int first_global;
bae7f79e
ILT
30 // Symbol names.
31 File_view* symbol_names;
32 // Size of symbol name data in bytes.
33 off_t symbol_names_size;
34};
35
36// Object is an interface which represents either a 32-bit or a 64-bit
14bfc3f5
ILT
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>
bae7f79e
ILT
40
41class Object
42{
43 public:
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.
14bfc3f5
ILT
48 Object(const std::string& name, Input_file* input_file, bool is_dynamic,
49 off_t offset = 0)
50 : name_(name), input_file_(input_file), offset_(offset),
a2fb1b05
ILT
51 shnum_(0), is_dynamic_(is_dynamic), target_(NULL),
52 map_to_output_()
bae7f79e
ILT
53 { }
54
55 virtual ~Object()
56 { }
57
14bfc3f5 58 // Return the name of the object as we would report it to the tuser.
bae7f79e
ILT
59 const std::string&
60 name() const
61 { return this->name_; }
62
14bfc3f5
ILT
63 // Return whether this is a dynamic object.
64 bool
65 is_dynamic() const
66 { return this->is_dynamic_; }
67
14bfc3f5
ILT
68 // Return the target structure associated with this object.
69 Target*
a2fb1b05 70 target() const
14bfc3f5
ILT
71 { return this->target_; }
72
a2fb1b05
ILT
73 // Lock the underlying file.
74 void
75 lock()
76 { this->input_file_->file().lock(); }
77
78 // Unlock the underlying file.
79 void
80 unlock()
81 { this->input_file_->file().unlock(); }
82
83 // Return whether the underlying file is locked.
84 bool
85 is_locked() const
86 { return this->input_file_->file().is_locked(); }
87
d288e464 88#ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
14bfc3f5
ILT
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>*
94 sized_target();
d288e464 95#endif
bae7f79e 96
a2fb1b05
ILT
97 // Read the symbol and relocation information.
98 Read_symbols_data
99 read_symbols()
100 { return this->do_read_symbols(); }
101
102 // Add symbol information to the global symbol table.
103 void
104 add_symbols(Symbol_table* symtab, Read_symbols_data rd)
105 { this->do_add_symbols(symtab, rd); }
106
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.
109 void
110 layout(Layout* lay)
111 { this->do_layout(lay); }
112
bae7f79e 113 protected:
a2fb1b05
ILT
114 // What we need to know to map an input section to an output
115 // section. We keep an array of these, one for each input section,
116 // indexed by the input section number.
117 struct Map_to_output
118 {
119 // The output section. This is NULL if the input section is to be
120 // discarded.
121 Output_section* output_section;
122 // The offset within the output section.
123 off_t offset;
124 };
125
bae7f79e
ILT
126 // Read the symbols--implemented by child class.
127 virtual Read_symbols_data
128 do_read_symbols() = 0;
129
130 // Add symbol information to the global symbol table--implemented by
131 // child class.
132 virtual void
14bfc3f5 133 do_add_symbols(Symbol_table*, Read_symbols_data) = 0;
bae7f79e 134
a2fb1b05
ILT
135 // Lay out sections--implemented by child class.
136 virtual void
137 do_layout(Layout*) = 0;
138
bae7f79e
ILT
139 // Get the file.
140 Input_file*
141 input_file() const
142 { return this->input_file_; }
143
144 // Get the offset into the file.
145 off_t
146 offset() const
147 { return this->offset_; }
148
149 // Get a view into the underlying file.
150 const unsigned char*
151 get_view(off_t start, off_t size);
152
a2fb1b05
ILT
153 // Get the number of sections.
154 unsigned int
155 shnum(void) const
156 { return this->shnum_; }
157
158 // Set the number of sections.
159 void
160 set_shnum(int shnum)
161 { this->shnum_ = shnum; }
162
14bfc3f5
ILT
163 // Set the target.
164 void
165 set_target(Target* target)
166 { this->target_ = target; }
167
bae7f79e
ILT
168 // Read data from the underlying file.
169 void
170 read(off_t start, off_t size, void* p);
171
172 // Get a lasting view into the underlying file.
173 File_view*
174 get_lasting_view(off_t start, off_t size);
175
a2fb1b05
ILT
176 // Return the vector mapping input sections to output sections.
177 std::vector<Map_to_output>&
178 map_to_output()
179 { return this->map_to_output_; }
180
bae7f79e
ILT
181 private:
182 // This class may not be copied.
183 Object(const Object&);
184 Object& operator=(const Object&);
185
186 // Name of object as printed to use.
187 std::string name_;
188 // For reading the file.
189 Input_file* input_file_;
190 // Offset within the file--0 for an object file, non-0 for an
191 // archive.
192 off_t offset_;
a2fb1b05
ILT
193 // Number of input sections.
194 unsigned int shnum_;
14bfc3f5
ILT
195 // Whether this is a dynamic object.
196 bool is_dynamic_;
197 // Target functions--may be NULL if the target is not known.
198 Target* target_;
a2fb1b05
ILT
199 // Mapping from input sections to output section.
200 std::vector<Map_to_output> map_to_output_;
bae7f79e
ILT
201};
202
d288e464
ILT
203#ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
204
14bfc3f5
ILT
205// Implement sized_target inline for efficiency. This approach breaks
206// static type checking, but is made safe using asserts.
207
208template<int size, bool big_endian>
209inline Sized_target<size, big_endian>*
210Object::sized_target()
211{
212 assert(this->target_->get_size() == size);
213 assert(this->target_->is_big_endian() ? big_endian : !big_endian);
214 return static_cast<Sized_target<size, big_endian>*>(this->target_);
215}
216
d288e464
ILT
217#endif
218
14bfc3f5 219// A regular object file. This is size and endian specific.
bae7f79e
ILT
220
221template<int size, bool big_endian>
222class Sized_object : public Object
223{
224 public:
225 Sized_object(const std::string& name, Input_file* input_file, off_t offset,
226 const typename elfcpp::Ehdr<size, big_endian>&);
227
228 ~Sized_object();
229
a2fb1b05 230 // Set up the object file based on the ELF header.
bae7f79e
ILT
231 void
232 setup(const typename elfcpp::Ehdr<size, big_endian>&);
233
a2fb1b05 234 // Read the symbols.
bae7f79e
ILT
235 Read_symbols_data
236 do_read_symbols();
237
a2fb1b05 238 // Add the symbols to the symbol table.
bae7f79e 239 void
14bfc3f5
ILT
240 do_add_symbols(Symbol_table*, Read_symbols_data);
241
a2fb1b05
ILT
242 // Lay out the input sections.
243 void
244 do_layout(Layout*);
245
246 // Return the appropriate Sized_target structure.
14bfc3f5
ILT
247 Sized_target<size, big_endian>*
248 sized_target()
274e99f9
ILT
249 {
250#ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
251 return this->Object::sized_target<size, big_endian>();
252#else
253 return static_cast<Sized_target<size, big_endian>*>(this->target());
254#endif
255 }
bae7f79e
ILT
256
257 private:
258 // This object may not be copied.
259 Sized_object(const Sized_object&);
260 Sized_object& operator=(const Sized_object&);
261
a2fb1b05
ILT
262 // For convenience.
263 typedef Sized_object<size, big_endian> This;
264 static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
265 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
266 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
267
268 // Whether to include a section group in the link.
269 bool
270 include_section_group(Layout*, unsigned int,
271 const elfcpp::Shdr<size, big_endian>&,
272 std::vector<bool>*);
273
274 // Whether to include a linkonce section in the link.
275 bool
276 include_linkonce_section(Layout*, const char*,
277 const elfcpp::Shdr<size, big_endian>&);
278
bae7f79e
ILT
279 // ELF file header e_flags field.
280 unsigned int flags_;
bae7f79e
ILT
281 // File offset of section header table.
282 off_t shoff_;
bae7f79e
ILT
283 // Offset of SHT_STRTAB section holding section names.
284 unsigned int shstrndx_;
285 // Index of SHT_SYMTAB section.
286 unsigned int symtab_shnum_;
14bfc3f5
ILT
287 // The entries in the symbol table for the external symbols.
288 Symbol** symbols_;
bae7f79e
ILT
289};
290
54dc6425 291// A class to manage the list of all objects.
a2fb1b05 292
54dc6425
ILT
293class Input_objects
294{
295 public:
296 Input_objects()
274e99f9 297 : object_list_(), any_dynamic_(false)
54dc6425
ILT
298 { }
299
300 // The type of the list of input objects.
301 typedef std::list<Object*> Object_list;
302
303 // Add an object to the list.
304 void
305 add_object(Object*);
306
307 // Iterate over all objects.
308 Object_list::const_iterator
309 begin() const
310 { return this->object_list_.begin(); }
311
312 Object_list::const_iterator
313 end() const
314 { return this->object_list_.end(); }
315
316 // Return whether we have seen any dynamic objects.
317 bool
318 any_dynamic() const
319 { return this->any_dynamic_; }
320
321 private:
322 Input_objects(const Input_objects&);
323 Input_objects& operator=(const Input_objects&);
324
325 Object_list object_list_;
326 bool any_dynamic_;
327};
a2fb1b05 328
bae7f79e
ILT
329// Return an Object appropriate for the input file. P is BYTES long,
330// and holds the ELF header.
331
332extern Object* make_elf_object(const std::string& name, Input_file*,
333 off_t offset, const unsigned char* p,
334 off_t bytes);
335
336} // end namespace gold
337
338#endif // !defined(GOLD_OBJECT_H)
This page took 0.038929 seconds and 4 git commands to generate.