1 // layout.h -- lay out output file sections for gold -*- C++ -*-
11 #include "workqueue.h"
13 #include "stringpool.h"
18 class General_options
;
21 class Output_section_data
;
23 class Output_section_headers
;
26 class Output_data_dynamic
;
29 // This task function handles mapping the input sections to output
30 // sections and laying them out in memory.
32 class Layout_task_runner
: public Task_function_runner
35 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
36 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
38 Layout_task_runner(const General_options
& options
,
39 const Input_objects
* input_objects
,
42 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
51 Layout_task_runner(const Layout_task_runner
&);
52 Layout_task_runner
& operator=(const Layout_task_runner
&);
54 const General_options
& options_
;
55 const Input_objects
* input_objects_
;
56 Symbol_table
* symtab_
;
60 // This class handles the details of laying out input sections.
65 Layout(const General_options
& options
);
67 // Given an input section SHNDX, named NAME, with data in SHDR, from
68 // the object file OBJECT, return the output section where this
69 // input section should go. Set *OFFSET to the offset within the
71 template<int size
, bool big_endian
>
73 layout(Relobj
*object
, unsigned int shndx
, const char* name
,
74 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* offset
);
76 // Add an Output_section_data to the layout. This is used for
77 // special sections like the GOT section.
79 add_output_section_data(const char* name
, elfcpp::Elf_Word type
,
80 elfcpp::Elf_Xword flags
,
81 Output_section_data
*);
83 // Create dynamic sections if necessary.
85 create_initial_dynamic_sections(const Input_objects
*, Symbol_table
*);
87 // Return the Stringpool used for symbol names.
90 { return &this->sympool_
; }
92 // Return the Stringpool used for dynamic symbol names and dynamic
96 { return &this->dynpool_
; }
98 // Return whether a section is a .gnu.linkonce section, given the
101 is_linkonce(const char* name
)
102 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
104 // Record the signature of a comdat section, and return whether to
105 // include it in the link. The GROUP parameter is true for a
106 // section group signature, false for a signature derived from a
107 // .gnu.linkonce section.
109 add_comdat(const char*, bool group
);
111 // Finalize the layout after all the input sections have been added.
113 finalize(const Input_objects
*, Symbol_table
*);
115 // Return the TLS segment. This will return NULL if there isn't
119 { return this->tls_segment_
; }
121 // Return the normal symbol table.
123 symtab_section() const
125 gold_assert(this->symtab_section_
!= NULL
);
126 return this->symtab_section_
;
129 // Return the dynamic symbol table.
131 dynsym_section() const
133 gold_assert(this->dynsym_section_
!= NULL
);
134 return this->dynsym_section_
;
137 // Return the dynamic tags.
140 { return this->dynamic_data_
; }
142 // Write out data not associated with an input file or the symbol
145 write_data(const Symbol_table
*, const Target
*, Output_file
*) const;
147 // Return an output section named NAME, or NULL if there is none.
149 find_output_section(const char* name
) const;
151 // Return an output segment of type TYPE, with segment flags SET set
152 // and segment flags CLEAR clear. Return NULL if there is none.
154 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
155 elfcpp::Elf_Word clear
) const;
157 // The list of segments.
159 typedef std::vector
<Output_segment
*> Segment_list
;
161 // The list of sections not attached to a segment.
163 typedef std::vector
<Output_section
*> Section_list
;
165 // The list of information to write out which is not attached to
166 // either a section or a segment.
167 typedef std::vector
<Output_data
*> Data_list
;
170 Layout(const Layout
&);
171 Layout
& operator=(const Layout
&);
173 // Mapping from .gnu.linkonce section names to output section names.
174 struct Linkonce_mapping
181 static const Linkonce_mapping linkonce_mapping
[];
182 static const int linkonce_mapping_count
;
184 // Find the first read-only PT_LOAD segment, creating one if
187 find_first_load_seg();
189 // Create the output sections for the symbol table.
191 create_symtab_sections(int size
, const Input_objects
*, Symbol_table
*,
194 // Create the .shstrtab section.
198 // Create the section header table.
199 Output_section_headers
*
200 create_shdrs(int size
, bool big_endian
, off_t
*);
202 // Create the dynamic symbol table.
204 create_dynamic_symtab(const Target
*, Symbol_table
*);
206 // Finish the .dynamic section and PT_DYNAMIC segment.
208 finish_dynamic_section(const Input_objects
*, const Symbol_table
*);
210 // Create the .interp section and PT_INTERP segment.
212 create_interp(const Target
* target
);
214 // Return whether to include this section in the link.
215 template<int size
, bool big_endian
>
217 include_section(Object
* object
, const char* name
,
218 const elfcpp::Shdr
<size
, big_endian
>&);
220 // Return the output section name to use given an input section
221 // name. Set *PLEN to the length of the name. *PLEN must be
222 // initialized to the length of NAME.
224 output_section_name(const char* name
, size_t* plen
);
226 // Return the output section name to use for a linkonce section
227 // name. PLEN is as for output_section_name.
229 linkonce_output_name(const char* name
, size_t* plen
);
231 // Return the output section for NAME, TYPE and FLAGS.
233 get_output_section(const char* name
, Stringpool::Key name_key
,
234 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
236 // Create a new Output_section.
238 make_output_section(const char* name
, elfcpp::Elf_Word type
,
239 elfcpp::Elf_Xword flags
);
241 // Set the final file offsets of all the segments.
243 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
245 // Set the final file offsets and section indices of all the
246 // sections not associated with a segment.
248 set_section_offsets(off_t
, unsigned int *pshndx
);
250 // Return whether SEG1 comes before SEG2 in the output file.
252 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
254 // Map from section flags to segment flags.
255 static elfcpp::Elf_Word
256 section_flags_to_segment(elfcpp::Elf_Xword flags
);
258 // A mapping used for group signatures.
259 typedef Unordered_map
<std::string
, bool> Signatures
;
261 // Mapping from input section name/type/flags to output section. We
262 // use canonicalized strings here.
264 typedef std::pair
<Stringpool::Key
,
265 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
270 operator()(const Key
& k
) const;
273 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
275 // A comparison class for segments.
277 struct Compare_segments
280 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
281 { return Layout::segment_precedes(seg1
, seg2
); }
284 // A reference to the options on the command line.
285 const General_options
& options_
;
286 // The output section names.
287 Stringpool namepool_
;
288 // The output symbol names.
290 // The dynamic strings, if needed.
292 // The list of group sections and linkonce sections which we have seen.
293 Signatures signatures_
;
294 // The mapping from input section name/type/flags to output sections.
295 Section_name_map section_name_map_
;
296 // The list of output segments.
297 Segment_list segment_list_
;
298 // The list of output sections.
299 Section_list section_list_
;
300 // The list of output sections which are not attached to any output
302 Section_list unattached_section_list_
;
303 // The list of unattached Output_data objects which require special
304 // handling because they are not Output_sections.
305 Data_list special_output_list_
;
306 // A pointer to the PT_TLS segment if there is one.
307 Output_segment
* tls_segment_
;
308 // The SHT_SYMTAB output section.
309 Output_section
* symtab_section_
;
310 // The SHT_DYNSYM output section if there is one.
311 Output_section
* dynsym_section_
;
312 // The SHT_DYNAMIC output section if there is one.
313 Output_section
* dynamic_section_
;
314 // The dynamic data which goes into dynamic_section_.
315 Output_data_dynamic
* dynamic_data_
;
318 // This task handles writing out data which is not part of a section
321 class Write_data_task
: public Task
324 Write_data_task(const Layout
* layout
, const Symbol_table
* symtab
,
325 const Target
* target
, Output_file
* of
,
326 Task_token
* final_blocker
)
327 : layout_(layout
), symtab_(symtab
), target_(target
), of_(of
),
328 final_blocker_(final_blocker
)
331 // The standard Task methods.
334 is_runnable(Workqueue
*);
343 const Layout
* layout_
;
344 const Symbol_table
* symtab_
;
345 const Target
* target_
;
347 Task_token
* final_blocker_
;
350 // This task handles writing out the global symbols.
352 class Write_symbols_task
: public Task
355 Write_symbols_task(const Symbol_table
* symtab
, const Target
* target
,
356 const Stringpool
* sympool
, const Stringpool
* dynpool
,
357 Output_file
* of
, Task_token
* final_blocker
)
358 : symtab_(symtab
), target_(target
), sympool_(sympool
), dynpool_(dynpool
),
359 of_(of
), final_blocker_(final_blocker
)
362 // The standard Task methods.
365 is_runnable(Workqueue
*);
374 const Symbol_table
* symtab_
;
375 const Target
* target_
;
376 const Stringpool
* sympool_
;
377 const Stringpool
* dynpool_
;
379 Task_token
* final_blocker_
;
382 // This task function handles closing the file.
384 class Close_task_runner
: public Task_function_runner
387 Close_task_runner(Output_file
* of
)
391 // Run the operation.
399 // A small helper function to align an address.
402 align_address(uint64_t address
, uint64_t addralign
)
405 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
409 } // End namespace gold.
411 #endif // !defined(GOLD_LAYOUT_H)