daily update
[binutils.git] / gold / layout.h
blob6c7441e63be93f6cf2bc398f31e5e00ef4d1baaf
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_LAYOUT_H
24 #define GOLD_LAYOUT_H
26 #include <list>
27 #include <string>
28 #include <utility>
29 #include <vector>
31 #include "workqueue.h"
32 #include "object.h"
33 #include "dynobj.h"
34 #include "stringpool.h"
36 namespace gold
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;
50 // This task function handles mapping the input sections to output
51 // sections and laying them out in memory.
53 class Layout_task_runner : public Task_function_runner
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 { }
67 // Run the operation.
68 void
69 run(Workqueue*);
71 private:
72 Layout_task_runner(const Layout_task_runner&);
73 Layout_task_runner& operator=(const Layout_task_runner&);
75 const General_options& options_;
76 const Input_objects* input_objects_;
77 Symbol_table* symtab_;
78 Layout* layout_;
81 // This class handles the details of laying out input sections.
83 class Layout
85 public:
86 Layout(const General_options& options);
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);
97 // Handle a GNU stack note. This is called once per input object
98 // file. SEEN_GNU_STACK is true if the object file has a
99 // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
100 // from that section if there was one.
101 void
102 layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
104 // Add an Output_section_data to the layout. This is used for
105 // special sections like the GOT section.
106 void
107 add_output_section_data(const char* name, elfcpp::Elf_Word type,
108 elfcpp::Elf_Xword flags,
109 Output_section_data*);
111 // Create dynamic sections if necessary.
112 void
113 create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
115 // Define __start and __stop symbols for output sections.
116 void
117 define_section_symbols(Symbol_table*, const Target*);
119 // Return the Stringpool used for symbol names.
120 const Stringpool*
121 sympool() const
122 { return &this->sympool_; }
124 // Return the Stringpool used for dynamic symbol names and dynamic
125 // tags.
126 const Stringpool*
127 dynpool() const
128 { return &this->dynpool_; }
130 // Return whether a section is a .gnu.linkonce section, given the
131 // section name.
132 static inline bool
133 is_linkonce(const char* name)
134 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
136 // Record the signature of a comdat section, and return whether to
137 // include it in the link. The GROUP parameter is true for a
138 // section group signature, false for a signature derived from a
139 // .gnu.linkonce section.
140 bool
141 add_comdat(const char*, bool group);
143 // Finalize the layout after all the input sections have been added.
144 off_t
145 finalize(const Input_objects*, Symbol_table*);
147 // Return the size of the output file.
148 off_t
149 output_file_size() const
150 { return this->output_file_size_; }
152 // Return the TLS segment. This will return NULL if there isn't
153 // one.
154 Output_segment*
155 tls_segment() const
156 { return this->tls_segment_; }
158 // Return the normal symbol table.
159 Output_section*
160 symtab_section() const
162 gold_assert(this->symtab_section_ != NULL);
163 return this->symtab_section_;
166 // Return the dynamic symbol table.
167 Output_section*
168 dynsym_section() const
170 gold_assert(this->dynsym_section_ != NULL);
171 return this->dynsym_section_;
174 // Return the dynamic tags.
175 Output_data_dynamic*
176 dynamic_data() const
177 { return this->dynamic_data_; }
179 // Write out data not associated with an input file or the symbol
180 // table.
181 void
182 write_data(const Symbol_table*, Output_file*) const;
184 // Return an output section named NAME, or NULL if there is none.
185 Output_section*
186 find_output_section(const char* name) const;
188 // Return an output segment of type TYPE, with segment flags SET set
189 // and segment flags CLEAR clear. Return NULL if there is none.
190 Output_segment*
191 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
192 elfcpp::Elf_Word clear) const;
194 // The list of segments.
196 typedef std::vector<Output_segment*> Segment_list;
198 // The list of sections not attached to a segment.
200 typedef std::vector<Output_section*> Section_list;
202 // The list of information to write out which is not attached to
203 // either a section or a segment.
204 typedef std::vector<Output_data*> Data_list;
206 private:
207 Layout(const Layout&);
208 Layout& operator=(const Layout&);
210 // Mapping from .gnu.linkonce section names to output section names.
211 struct Linkonce_mapping
213 const char* from;
214 int fromlen;
215 const char* to;
216 int tolen;
218 static const Linkonce_mapping linkonce_mapping[];
219 static const int linkonce_mapping_count;
221 // Handle an exception frame section.
222 template<int size, bool big_endian>
223 void
224 layout_eh_frame(Relobj*, unsigned int, const char*,
225 const elfcpp::Shdr<size, big_endian>&,
226 Output_section*, off_t*);
228 // Create a .note section for gold.
229 void
230 create_gold_note();
232 // Record whether the stack must be executable.
233 void
234 create_executable_stack_info(const Target*);
236 // Find the first read-only PT_LOAD segment, creating one if
237 // necessary.
238 Output_segment*
239 find_first_load_seg();
241 // Create the output sections for the symbol table.
242 void
243 create_symtab_sections(const Input_objects*, Symbol_table*, off_t*);
245 // Create the .shstrtab section.
246 Output_section*
247 create_shstrtab();
249 // Create the section header table.
250 Output_section_headers*
251 create_shdrs(off_t*);
253 // Create the dynamic symbol table.
254 void
255 create_dynamic_symtab(const Target*, Symbol_table*, Output_section** pdynstr,
256 unsigned int* plocal_dynamic_count,
257 std::vector<Symbol*>* pdynamic_symbols,
258 Versions* versions);
260 // Finish the .dynamic section and PT_DYNAMIC segment.
261 void
262 finish_dynamic_section(const Input_objects*, const Symbol_table*);
264 // Create the .interp section and PT_INTERP segment.
265 void
266 create_interp(const Target* target);
268 // Create the version sections.
269 void
270 create_version_sections(const Versions*,
271 const Symbol_table*,
272 unsigned int local_symcount,
273 const std::vector<Symbol*>& dynamic_symbols,
274 const Output_section* dynstr);
276 template<int size, bool big_endian>
277 void
278 sized_create_version_sections(const Versions* versions,
279 const Symbol_table*,
280 unsigned int local_symcount,
281 const std::vector<Symbol*>& dynamic_symbols,
282 const Output_section* dynstr
283 ACCEPT_SIZE_ENDIAN);
285 // Return whether to include this section in the link.
286 template<int size, bool big_endian>
287 bool
288 include_section(Object* object, const char* name,
289 const elfcpp::Shdr<size, big_endian>&);
291 // Return the output section name to use given an input section
292 // name. Set *PLEN to the length of the name. *PLEN must be
293 // initialized to the length of NAME.
294 static const char*
295 output_section_name(const char* name, size_t* plen);
297 // Return the output section name to use for a linkonce section
298 // name. PLEN is as for output_section_name.
299 static const char*
300 linkonce_output_name(const char* name, size_t* plen);
302 // Return the output section for NAME, TYPE and FLAGS.
303 Output_section*
304 get_output_section(const char* name, Stringpool::Key name_key,
305 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
307 // Create a new Output_section.
308 Output_section*
309 make_output_section(const char* name, elfcpp::Elf_Word type,
310 elfcpp::Elf_Xword flags);
312 // Set the final file offsets of all the segments.
313 off_t
314 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
316 // Set the final file offsets and section indexes of all the
317 // sections not associated with a segment.
318 off_t
319 set_section_offsets(off_t, unsigned int *pshndx, bool do_bits_sections);
321 // Return whether SEG1 comes before SEG2 in the output file.
322 static bool
323 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
325 // Map from section flags to segment flags.
326 static elfcpp::Elf_Word
327 section_flags_to_segment(elfcpp::Elf_Xword flags);
329 // A mapping used for group signatures.
330 typedef Unordered_map<std::string, bool> Signatures;
332 // Mapping from input section name/type/flags to output section. We
333 // use canonicalized strings here.
335 typedef std::pair<Stringpool::Key,
336 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
338 struct Hash_key
340 size_t
341 operator()(const Key& k) const;
344 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
346 // A comparison class for segments.
348 struct Compare_segments
350 bool
351 operator()(const Output_segment* seg1, const Output_segment* seg2)
352 { return Layout::segment_precedes(seg1, seg2); }
355 // A reference to the options on the command line.
356 const General_options& options_;
357 // The output section names.
358 Stringpool namepool_;
359 // The output symbol names.
360 Stringpool sympool_;
361 // The dynamic strings, if needed.
362 Stringpool dynpool_;
363 // The list of group sections and linkonce sections which we have seen.
364 Signatures signatures_;
365 // The mapping from input section name/type/flags to output sections.
366 Section_name_map section_name_map_;
367 // The list of output segments.
368 Segment_list segment_list_;
369 // The list of output sections.
370 Section_list section_list_;
371 // The list of output sections which are not attached to any output
372 // segment.
373 Section_list unattached_section_list_;
374 // The list of unattached Output_data objects which require special
375 // handling because they are not Output_sections.
376 Data_list special_output_list_;
377 // A pointer to the PT_TLS segment if there is one.
378 Output_segment* tls_segment_;
379 // The SHT_SYMTAB output section.
380 Output_section* symtab_section_;
381 // The SHT_DYNSYM output section if there is one.
382 Output_section* dynsym_section_;
383 // The SHT_DYNAMIC output section if there is one.
384 Output_section* dynamic_section_;
385 // The dynamic data which goes into dynamic_section_.
386 Output_data_dynamic* dynamic_data_;
387 // The exception frame section.
388 Output_section* eh_frame_section_;
389 // The size of the output file.
390 off_t output_file_size_;
391 // Whether we have seen an object file marked to require an
392 // executable stack.
393 bool input_requires_executable_stack_;
394 // Whether we have seen at least one object file with an executable
395 // stack marker.
396 bool input_with_gnu_stack_note_;
397 // Whether we have seen at least one object file without an
398 // executable stack marker.
399 bool input_without_gnu_stack_note_;
402 // This task handles writing out data which is not part of a section
403 // or segment.
405 class Write_data_task : public Task
407 public:
408 Write_data_task(const Layout* layout, const Symbol_table* symtab,
409 Output_file* of, Task_token* final_blocker)
410 : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
413 // The standard Task methods.
415 Is_runnable_type
416 is_runnable(Workqueue*);
418 Task_locker*
419 locks(Workqueue*);
421 void
422 run(Workqueue*);
424 private:
425 const Layout* layout_;
426 const Symbol_table* symtab_;
427 Output_file* of_;
428 Task_token* final_blocker_;
431 // This task handles writing out the global symbols.
433 class Write_symbols_task : public Task
435 public:
436 Write_symbols_task(const Symbol_table* symtab, const Target* target,
437 const Stringpool* sympool, const Stringpool* dynpool,
438 Output_file* of, Task_token* final_blocker)
439 : symtab_(symtab), target_(target), sympool_(sympool), dynpool_(dynpool),
440 of_(of), final_blocker_(final_blocker)
443 // The standard Task methods.
445 Is_runnable_type
446 is_runnable(Workqueue*);
448 Task_locker*
449 locks(Workqueue*);
451 void
452 run(Workqueue*);
454 private:
455 const Symbol_table* symtab_;
456 const Target* target_;
457 const Stringpool* sympool_;
458 const Stringpool* dynpool_;
459 Output_file* of_;
460 Task_token* final_blocker_;
463 // This task function handles closing the file.
465 class Close_task_runner : public Task_function_runner
467 public:
468 Close_task_runner(Output_file* of)
469 : of_(of)
472 // Run the operation.
473 void
474 run(Workqueue*);
476 private:
477 Output_file* of_;
480 // A small helper function to align an address.
482 inline uint64_t
483 align_address(uint64_t address, uint64_t addralign)
485 if (addralign != 0)
486 address = (address + addralign - 1) &~ (addralign - 1);
487 return address;
490 } // End namespace gold.
492 #endif // !defined(GOLD_LAYOUT_H)