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.
31 #include "workqueue.h"
34 #include "stringpool.h"
39 class General_options
;
42 class Output_section_data
;
44 class Output_section_headers
;
47 class Output_data_dynamic
;
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
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
59 Layout_task_runner(const General_options
& options
,
60 const Input_objects
* input_objects
,
63 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
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_
;
81 // This class handles the details of laying out input sections.
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
92 template<int size
, bool big_endian
>
94 layout(Relobj
*object
, unsigned int shndx
, const char* name
,
95 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* offset
);
97 // Add an Output_section_data to the layout. This is used for
98 // special sections like the GOT section.
100 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
101 elfcpp::Elf_Xword flags
,
102 Output_section_data
*);
104 // Create dynamic sections if necessary.
106 create_initial_dynamic_sections(const Input_objects
*, Symbol_table
*);
108 // Define __start and __stop symbols for output sections.
110 define_section_symbols(Symbol_table
*, const Target
*);
112 // Return the Stringpool used for symbol names.
115 { return &this->sympool_
; }
117 // Return the Stringpool used for dynamic symbol names and dynamic
121 { return &this->dynpool_
; }
123 // Return whether a section is a .gnu.linkonce section, given the
126 is_linkonce(const char* name
)
127 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
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.
134 add_comdat(const char*, bool group
);
136 // Finalize the layout after all the input sections have been added.
138 finalize(const Input_objects
*, Symbol_table
*);
140 // Return the TLS segment. This will return NULL if there isn't
144 { return this->tls_segment_
; }
146 // Return the normal symbol table.
148 symtab_section() const
150 gold_assert(this->symtab_section_
!= NULL
);
151 return this->symtab_section_
;
154 // Return the dynamic symbol table.
156 dynsym_section() const
158 gold_assert(this->dynsym_section_
!= NULL
);
159 return this->dynsym_section_
;
162 // Return the dynamic tags.
165 { return this->dynamic_data_
; }
167 // Write out data not associated with an input file or the symbol
170 write_data(const Symbol_table
*, Output_file
*) const;
172 // Return an output section named NAME, or NULL if there is none.
174 find_output_section(const char* name
) const;
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.
179 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
180 elfcpp::Elf_Word clear
) const;
182 // The list of segments.
184 typedef std::vector
<Output_segment
*> Segment_list
;
186 // The list of sections not attached to a segment.
188 typedef std::vector
<Output_section
*> Section_list
;
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
;
195 Layout(const Layout
&);
196 Layout
& operator=(const Layout
&);
198 // Mapping from .gnu.linkonce section names to output section names.
199 struct Linkonce_mapping
206 static const Linkonce_mapping linkonce_mapping
[];
207 static const int linkonce_mapping_count
;
209 // Handle an exception frame section.
210 template<int size
, bool big_endian
>
212 layout_eh_frame(Relobj
*, unsigned int, const char*,
213 const elfcpp::Shdr
<size
, big_endian
>&,
214 Output_section
*, off_t
*);
216 // Create a .note section for gold.
218 create_note_section();
220 // Find the first read-only PT_LOAD segment, creating one if
223 find_first_load_seg();
225 // Create the output sections for the symbol table.
227 create_symtab_sections(const Input_objects
*, Symbol_table
*, off_t
*);
229 // Create the .shstrtab section.
233 // Create the section header table.
234 Output_section_headers
*
235 create_shdrs(off_t
*);
237 // Create the dynamic symbol table.
239 create_dynamic_symtab(const Target
*, Symbol_table
*, Output_section
** pdynstr
,
240 unsigned int* plocal_dynamic_count
,
241 std::vector
<Symbol
*>* pdynamic_symbols
,
244 // Finish the .dynamic section and PT_DYNAMIC segment.
246 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
248 // Create the .interp section and PT_INTERP segment.
250 create_interp(const Target
* target
);
252 // Create the version sections.
254 create_version_sections(const Versions
*,
255 unsigned int local_symcount
,
256 const std::vector
<Symbol
*>& dynamic_symbols
,
257 const Output_section
* dynstr
);
259 template<int size
, bool big_endian
>
261 sized_create_version_sections(const Versions
* versions
,
262 unsigned int local_symcount
,
263 const std::vector
<Symbol
*>& dynamic_symbols
,
264 const Output_section
* dynstr
267 // Return whether to include this section in the link.
268 template<int size
, bool big_endian
>
270 include_section(Object
* object
, const char* name
,
271 const elfcpp::Shdr
<size
, big_endian
>&);
273 // Return the output section name to use given an input section
274 // name. Set *PLEN to the length of the name. *PLEN must be
275 // initialized to the length of NAME.
277 output_section_name(const char* name
, size_t* plen
);
279 // Return the output section name to use for a linkonce section
280 // name. PLEN is as for output_section_name.
282 linkonce_output_name(const char* name
, size_t* plen
);
284 // Return the output section for NAME, TYPE and FLAGS.
286 get_output_section(const char* name
, Stringpool::Key name_key
,
287 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
289 // Create a new Output_section.
291 make_output_section(const char* name
, elfcpp::Elf_Word type
,
292 elfcpp::Elf_Xword flags
);
294 // Set the final file offsets of all the segments.
296 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
298 // Set the final file offsets and section indexes of all the
299 // sections not associated with a segment.
301 set_section_offsets(off_t
, unsigned int *pshndx
);
303 // Return whether SEG1 comes before SEG2 in the output file.
305 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
307 // Map from section flags to segment flags.
308 static elfcpp::Elf_Word
309 section_flags_to_segment(elfcpp::Elf_Xword flags
);
311 // A mapping used for group signatures.
312 typedef Unordered_map
<std::string
, bool> Signatures
;
314 // Mapping from input section name/type/flags to output section. We
315 // use canonicalized strings here.
317 typedef std::pair
<Stringpool::Key
,
318 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
323 operator()(const Key
& k
) const;
326 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
328 // A comparison class for segments.
330 struct Compare_segments
333 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
334 { return Layout::segment_precedes(seg1
, seg2
); }
337 // A reference to the options on the command line.
338 const General_options
& options_
;
339 // The output section names.
340 Stringpool namepool_
;
341 // The output symbol names.
343 // The dynamic strings, if needed.
345 // The list of group sections and linkonce sections which we have seen.
346 Signatures signatures_
;
347 // The mapping from input section name/type/flags to output sections.
348 Section_name_map section_name_map_
;
349 // The list of output segments.
350 Segment_list segment_list_
;
351 // The list of output sections.
352 Section_list section_list_
;
353 // The list of output sections which are not attached to any output
355 Section_list unattached_section_list_
;
356 // The list of unattached Output_data objects which require special
357 // handling because they are not Output_sections.
358 Data_list special_output_list_
;
359 // A pointer to the PT_TLS segment if there is one.
360 Output_segment
* tls_segment_
;
361 // The SHT_SYMTAB output section.
362 Output_section
* symtab_section_
;
363 // The SHT_DYNSYM output section if there is one.
364 Output_section
* dynsym_section_
;
365 // The SHT_DYNAMIC output section if there is one.
366 Output_section
* dynamic_section_
;
367 // The dynamic data which goes into dynamic_section_.
368 Output_data_dynamic
* dynamic_data_
;
369 // The exception frame section.
370 Output_section
* eh_frame_section_
;
373 // This task handles writing out data which is not part of a section
376 class Write_data_task
: public Task
379 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
380 Output_file
* of
, Task_token
* final_blocker
)
381 : layout_(layout
), symtab_(symtab
), of_(of
), final_blocker_(final_blocker
)
384 // The standard Task methods.
387 is_runnable(Workqueue
*);
396 const Layout
* layout_
;
397 const Symbol_table
* symtab_
;
399 Task_token
* final_blocker_
;
402 // This task handles writing out the global symbols.
404 class Write_symbols_task
: public Task
407 Write_symbols_task(const Symbol_table
* symtab
, const Target
* target
,
408 const Stringpool
* sympool
, const Stringpool
* dynpool
,
409 Output_file
* of
, Task_token
* final_blocker
)
410 : symtab_(symtab
), target_(target
), sympool_(sympool
), dynpool_(dynpool
),
411 of_(of
), final_blocker_(final_blocker
)
414 // The standard Task methods.
417 is_runnable(Workqueue
*);
426 const Symbol_table
* symtab_
;
427 const Target
* target_
;
428 const Stringpool
* sympool_
;
429 const Stringpool
* dynpool_
;
431 Task_token
* final_blocker_
;
434 // This task function handles closing the file.
436 class Close_task_runner
: public Task_function_runner
439 Close_task_runner(Output_file
* of
)
443 // Run the operation.
451 // A small helper function to align an address.
454 align_address(uint64_t address
, uint64_t addralign
)
457 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
461 } // End namespace gold.
463 #endif // !defined(GOLD_LAYOUT_H)