]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/layout.h
Add licensing text to every source file.
[thirdparty/binutils-gdb.git] / gold / layout.h
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_LAYOUT_H
24 #define GOLD_LAYOUT_H
25
26 #include <list>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 #include "workqueue.h"
32 #include "object.h"
33 #include "dynobj.h"
34 #include "stringpool.h"
35
36 namespace gold
37 {
38
39 class General_options;
40 class Input_objects;
41 class Symbol_table;
42 class Output_section_data;
43 class Output_section;
44 class Output_section_headers;
45 class Output_segment;
46 class Output_data;
47 class Output_data_dynamic;
48 class Target;
49
50 // This task function handles mapping the input sections to output
51 // sections and laying them out in memory.
52
53 class Layout_task_runner : public Task_function_runner
54 {
55 public:
56 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
57 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
58 // object.
59 Layout_task_runner(const General_options& options,
60 const Input_objects* input_objects,
61 Symbol_table* symtab,
62 Layout* layout)
63 : options_(options), input_objects_(input_objects), symtab_(symtab),
64 layout_(layout)
65 { }
66
67 // Run the operation.
68 void
69 run(Workqueue*);
70
71 private:
72 Layout_task_runner(const Layout_task_runner&);
73 Layout_task_runner& operator=(const Layout_task_runner&);
74
75 const General_options& options_;
76 const Input_objects* input_objects_;
77 Symbol_table* symtab_;
78 Layout* layout_;
79 };
80
81 // This class handles the details of laying out input sections.
82
83 class Layout
84 {
85 public:
86 Layout(const General_options& options);
87
88 // Given an input section SHNDX, named NAME, with data in SHDR, from
89 // the object file OBJECT, return the output section where this
90 // input section should go. Set *OFFSET to the offset within the
91 // output section.
92 template<int size, bool big_endian>
93 Output_section*
94 layout(Relobj *object, unsigned int shndx, const char* name,
95 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
96
97 // Add an Output_section_data to the layout. This is used for
98 // special sections like the GOT section.
99 void
100 add_output_section_data(const char* name, elfcpp::Elf_Word type,
101 elfcpp::Elf_Xword flags,
102 Output_section_data*);
103
104 // Create dynamic sections if necessary.
105 void
106 create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
107
108 // Define __start and __stop symbols for output sections.
109 void
110 define_section_symbols(Symbol_table*, const Target*);
111
112 // Return the Stringpool used for symbol names.
113 const Stringpool*
114 sympool() const
115 { return &this->sympool_; }
116
117 // Return the Stringpool used for dynamic symbol names and dynamic
118 // tags.
119 const Stringpool*
120 dynpool() const
121 { return &this->dynpool_; }
122
123 // Return whether a section is a .gnu.linkonce section, given the
124 // section name.
125 static inline bool
126 is_linkonce(const char* name)
127 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
128
129 // Record the signature of a comdat section, and return whether to
130 // include it in the link. The GROUP parameter is true for a
131 // section group signature, false for a signature derived from a
132 // .gnu.linkonce section.
133 bool
134 add_comdat(const char*, bool group);
135
136 // Finalize the layout after all the input sections have been added.
137 off_t
138 finalize(const Input_objects*, Symbol_table*);
139
140 // Return the TLS segment. This will return NULL if there isn't
141 // one.
142 Output_segment*
143 tls_segment() const
144 { return this->tls_segment_; }
145
146 // Return the normal symbol table.
147 Output_section*
148 symtab_section() const
149 {
150 gold_assert(this->symtab_section_ != NULL);
151 return this->symtab_section_;
152 }
153
154 // Return the dynamic symbol table.
155 Output_section*
156 dynsym_section() const
157 {
158 gold_assert(this->dynsym_section_ != NULL);
159 return this->dynsym_section_;
160 }
161
162 // Return the dynamic tags.
163 Output_data_dynamic*
164 dynamic_data() const
165 { return this->dynamic_data_; }
166
167 // Write out data not associated with an input file or the symbol
168 // table.
169 void
170 write_data(const Symbol_table*, const Target*, Output_file*) const;
171
172 // Return an output section named NAME, or NULL if there is none.
173 Output_section*
174 find_output_section(const char* name) const;
175
176 // Return an output segment of type TYPE, with segment flags SET set
177 // and segment flags CLEAR clear. Return NULL if there is none.
178 Output_segment*
179 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
180 elfcpp::Elf_Word clear) const;
181
182 // The list of segments.
183
184 typedef std::vector<Output_segment*> Segment_list;
185
186 // The list of sections not attached to a segment.
187
188 typedef std::vector<Output_section*> Section_list;
189
190 // The list of information to write out which is not attached to
191 // either a section or a segment.
192 typedef std::vector<Output_data*> Data_list;
193
194 private:
195 Layout(const Layout&);
196 Layout& operator=(const Layout&);
197
198 // Mapping from .gnu.linkonce section names to output section names.
199 struct Linkonce_mapping
200 {
201 const char* from;
202 int fromlen;
203 const char* to;
204 int tolen;
205 };
206 static const Linkonce_mapping linkonce_mapping[];
207 static const int linkonce_mapping_count;
208
209 // Find the first read-only PT_LOAD segment, creating one if
210 // necessary.
211 Output_segment*
212 find_first_load_seg();
213
214 // Create the output sections for the symbol table.
215 void
216 create_symtab_sections(int size, const Input_objects*, Symbol_table*,
217 off_t*);
218
219 // Create the .shstrtab section.
220 Output_section*
221 create_shstrtab();
222
223 // Create the section header table.
224 Output_section_headers*
225 create_shdrs(int size, bool big_endian, off_t*);
226
227 // Create the dynamic symbol table.
228 void
229 create_dynamic_symtab(const Target*, Symbol_table*, Output_section** pdynstr,
230 unsigned int* plocal_dynamic_count,
231 std::vector<Symbol*>* pdynamic_symbols,
232 Versions* versions);
233
234 // Finish the .dynamic section and PT_DYNAMIC segment.
235 void
236 finish_dynamic_section(const Input_objects*, const Symbol_table*);
237
238 // Create the .interp section and PT_INTERP segment.
239 void
240 create_interp(const Target* target);
241
242 // Create the version sections.
243 void
244 create_version_sections(const Target*, const Versions*,
245 unsigned int local_symcount,
246 const std::vector<Symbol*>& dynamic_symbols,
247 const Output_section* dynstr);
248
249 template<int size, bool big_endian>
250 void
251 sized_create_version_sections(const Versions* versions,
252 unsigned int local_symcount,
253 const std::vector<Symbol*>& dynamic_symbols,
254 const Output_section* dynstr
255 ACCEPT_SIZE_ENDIAN);
256
257 // Return whether to include this section in the link.
258 template<int size, bool big_endian>
259 bool
260 include_section(Object* object, const char* name,
261 const elfcpp::Shdr<size, big_endian>&);
262
263 // Return the output section name to use given an input section
264 // name. Set *PLEN to the length of the name. *PLEN must be
265 // initialized to the length of NAME.
266 static const char*
267 output_section_name(const char* name, size_t* plen);
268
269 // Return the output section name to use for a linkonce section
270 // name. PLEN is as for output_section_name.
271 static const char*
272 linkonce_output_name(const char* name, size_t* plen);
273
274 // Return the output section for NAME, TYPE and FLAGS.
275 Output_section*
276 get_output_section(const char* name, Stringpool::Key name_key,
277 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
278
279 // Create a new Output_section.
280 Output_section*
281 make_output_section(const char* name, elfcpp::Elf_Word type,
282 elfcpp::Elf_Xword flags);
283
284 // Set the final file offsets of all the segments.
285 off_t
286 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
287
288 // Set the final file offsets and section indexes of all the
289 // sections not associated with a segment.
290 off_t
291 set_section_offsets(off_t, unsigned int *pshndx);
292
293 // Return whether SEG1 comes before SEG2 in the output file.
294 static bool
295 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
296
297 // Map from section flags to segment flags.
298 static elfcpp::Elf_Word
299 section_flags_to_segment(elfcpp::Elf_Xword flags);
300
301 // A mapping used for group signatures.
302 typedef Unordered_map<std::string, bool> Signatures;
303
304 // Mapping from input section name/type/flags to output section. We
305 // use canonicalized strings here.
306
307 typedef std::pair<Stringpool::Key,
308 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
309
310 struct Hash_key
311 {
312 size_t
313 operator()(const Key& k) const;
314 };
315
316 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
317
318 // A comparison class for segments.
319
320 struct Compare_segments
321 {
322 bool
323 operator()(const Output_segment* seg1, const Output_segment* seg2)
324 { return Layout::segment_precedes(seg1, seg2); }
325 };
326
327 // A reference to the options on the command line.
328 const General_options& options_;
329 // The output section names.
330 Stringpool namepool_;
331 // The output symbol names.
332 Stringpool sympool_;
333 // The dynamic strings, if needed.
334 Stringpool dynpool_;
335 // The list of group sections and linkonce sections which we have seen.
336 Signatures signatures_;
337 // The mapping from input section name/type/flags to output sections.
338 Section_name_map section_name_map_;
339 // The list of output segments.
340 Segment_list segment_list_;
341 // The list of output sections.
342 Section_list section_list_;
343 // The list of output sections which are not attached to any output
344 // segment.
345 Section_list unattached_section_list_;
346 // The list of unattached Output_data objects which require special
347 // handling because they are not Output_sections.
348 Data_list special_output_list_;
349 // A pointer to the PT_TLS segment if there is one.
350 Output_segment* tls_segment_;
351 // The SHT_SYMTAB output section.
352 Output_section* symtab_section_;
353 // The SHT_DYNSYM output section if there is one.
354 Output_section* dynsym_section_;
355 // The SHT_DYNAMIC output section if there is one.
356 Output_section* dynamic_section_;
357 // The dynamic data which goes into dynamic_section_.
358 Output_data_dynamic* dynamic_data_;
359 };
360
361 // This task handles writing out data which is not part of a section
362 // or segment.
363
364 class Write_data_task : public Task
365 {
366 public:
367 Write_data_task(const Layout* layout, const Symbol_table* symtab,
368 const Target* target, Output_file* of,
369 Task_token* final_blocker)
370 : layout_(layout), symtab_(symtab), target_(target), of_(of),
371 final_blocker_(final_blocker)
372 { }
373
374 // The standard Task methods.
375
376 Is_runnable_type
377 is_runnable(Workqueue*);
378
379 Task_locker*
380 locks(Workqueue*);
381
382 void
383 run(Workqueue*);
384
385 private:
386 const Layout* layout_;
387 const Symbol_table* symtab_;
388 const Target* target_;
389 Output_file* of_;
390 Task_token* final_blocker_;
391 };
392
393 // This task handles writing out the global symbols.
394
395 class Write_symbols_task : public Task
396 {
397 public:
398 Write_symbols_task(const Symbol_table* symtab, const Target* target,
399 const Stringpool* sympool, const Stringpool* dynpool,
400 Output_file* of, Task_token* final_blocker)
401 : symtab_(symtab), target_(target), sympool_(sympool), dynpool_(dynpool),
402 of_(of), final_blocker_(final_blocker)
403 { }
404
405 // The standard Task methods.
406
407 Is_runnable_type
408 is_runnable(Workqueue*);
409
410 Task_locker*
411 locks(Workqueue*);
412
413 void
414 run(Workqueue*);
415
416 private:
417 const Symbol_table* symtab_;
418 const Target* target_;
419 const Stringpool* sympool_;
420 const Stringpool* dynpool_;
421 Output_file* of_;
422 Task_token* final_blocker_;
423 };
424
425 // This task function handles closing the file.
426
427 class Close_task_runner : public Task_function_runner
428 {
429 public:
430 Close_task_runner(Output_file* of)
431 : of_(of)
432 { }
433
434 // Run the operation.
435 void
436 run(Workqueue*);
437
438 private:
439 Output_file* of_;
440 };
441
442 // A small helper function to align an address.
443
444 inline uint64_t
445 align_address(uint64_t address, uint64_t addralign)
446 {
447 if (addralign != 0)
448 address = (address + addralign - 1) &~ (addralign - 1);
449 return address;
450 }
451
452 } // End namespace gold.
453
454 #endif // !defined(GOLD_LAYOUT_H)