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