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_symtab
;
24 class Output_section_headers
;
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 // Return the Stringpool used for symbol names.
86 { return &this->sympool_
; }
88 // Return whether a section is a .gnu.linkonce section, given the
91 is_linkonce(const char* name
)
92 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
94 // Record the signature of a comdat section, and return whether to
95 // include it in the link. The GROUP parameter is true for a
96 // section group signature, false for a signature derived from a
97 // .gnu.linkonce section.
99 add_comdat(const char*, bool group
);
101 // Finalize the layout after all the input sections have been added.
103 finalize(const Input_objects
*, Symbol_table
*);
105 // Return the TLS segment.
108 { return this->tls_segment_
; }
110 // Write out data not associated with an input file or the symbol
113 write_data(Output_file
*) const;
115 // Return an output section named NAME, or NULL if there is none.
117 find_output_section(const char* name
) const;
119 // Return an output segment of type TYPE, with segment flags SET set
120 // and segment flags CLEAR clear. Return NULL if there is none.
122 find_output_segment(elfcpp::PT type
, elfcpp::Elf_Word set
,
123 elfcpp::Elf_Word clear
) const;
125 // The list of segments.
127 typedef std::vector
<Output_segment
*> Segment_list
;
129 // The list of sections not attached to a segment.
131 typedef std::list
<Output_section
*> Section_list
;
133 // The list of information to write out which is not attached to
134 // either a section or a segment.
135 typedef std::list
<Output_data
*> Data_list
;
138 Layout(const Layout
&);
139 Layout
& operator=(const Layout
&);
141 // Mapping from .gnu.linkonce section names to output section names.
142 struct Linkonce_mapping
149 static const Linkonce_mapping linkonce_mapping
[];
150 static const int linkonce_mapping_count
;
152 // Find the first read-only PT_LOAD segment, creating one if
155 find_first_load_seg();
157 // Create the output sections for the symbol table.
159 create_symtab_sections(int size
, const Input_objects
*, Symbol_table
*, off_t
*,
160 Output_section
** osymtab
,
161 Output_section
** ostrtab
);
163 // Create the .shstrtab section.
167 // Create the section header table.
168 Output_section_headers
*
169 create_shdrs(int size
, bool big_endian
, off_t
*);
171 // Create the dynamic symbol table.
173 create_dynamic_symtab(int size
, Symbol_table
*);
175 // Create the .dynamic section and PT_DYNAMIC segment.
177 create_dynamic_section();
179 // Create the .interp section and PT_INTERP segment.
181 create_interp(const Target
* target
);
183 // Return whether to include this section in the link.
184 template<int size
, bool big_endian
>
186 include_section(Object
* object
, const char* name
,
187 const elfcpp::Shdr
<size
, big_endian
>&);
189 // Return the output section name to use given an input section
190 // name. Set *PLEN to the length of the name. *PLEN must be
191 // initialized to the length of NAME.
193 output_section_name(const char* name
, size_t* plen
);
195 // Return the output section name to use for a linkonce section
196 // name. PLEN is as for output_section_name.
198 linkonce_output_name(const char* name
, size_t* plen
);
200 // Return the output section for NAME, TYPE and FLAGS.
202 get_output_section(const char* name
, Stringpool::Key name_key
,
203 elfcpp::Elf_Word type
, elfcpp::Elf_Xword flags
);
205 // Create a new Output_section.
207 make_output_section(const char* name
, elfcpp::Elf_Word type
,
208 elfcpp::Elf_Xword flags
);
210 // Set the final file offsets of all the segments.
212 set_segment_offsets(const Target
*, Output_segment
*, unsigned int* pshndx
);
214 // Set the final file offsets and section indices of all the
215 // sections not associated with a segment.
217 set_section_offsets(off_t
, unsigned int *pshndx
);
219 // Return whether SEG1 comes before SEG2 in the output file.
221 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
223 // Map from section flags to segment flags.
224 static elfcpp::Elf_Word
225 section_flags_to_segment(elfcpp::Elf_Xword flags
);
227 // A mapping used for group signatures.
228 typedef Unordered_map
<std::string
, bool> Signatures
;
230 // Mapping from input section name/type/flags to output section. We
231 // use canonicalized strings here.
233 typedef std::pair
<Stringpool::Key
,
234 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
239 operator()(const Key
& k
) const;
242 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
244 // A comparison class for segments.
246 struct Compare_segments
249 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
250 { return Layout::segment_precedes(seg1
, seg2
); }
253 // A reference to the options on the command line.
254 const General_options
& options_
;
255 // The output section names.
256 Stringpool namepool_
;
257 // The output symbol names.
259 // The list of group sections and linkonce sections which we have seen.
260 Signatures signatures_
;
261 // The mapping from input section name/type/flags to output sections.
262 Section_name_map section_name_map_
;
263 // The list of output segments.
264 Segment_list segment_list_
;
265 // The list of output sections which are not attached to any output
267 Section_list section_list_
;
268 // The list of sections which require special output because they
269 // are not comprised of input sections.
270 Data_list special_output_list_
;
271 // A pointer to the PT_TLS segment if there is one.
272 Output_segment
* tls_segment_
;
275 // This task handles writing out data which is not part of a section
278 class Write_data_task
: public Task
281 Write_data_task(const Layout
* layout
, Output_file
* of
,
282 Task_token
* final_blocker
)
283 : layout_(layout
), of_(of
), final_blocker_(final_blocker
)
286 // The standard Task methods.
289 is_runnable(Workqueue
*);
298 const Layout
* layout_
;
300 Task_token
* final_blocker_
;
303 // This task handles writing out the global symbols.
305 class Write_symbols_task
: public Task
308 Write_symbols_task(const Symbol_table
* symtab
, const Target
* target
,
309 const Stringpool
* sympool
, Output_file
* of
,
310 Task_token
* final_blocker
)
311 : symtab_(symtab
), target_(target
), sympool_(sympool
), of_(of
),
312 final_blocker_(final_blocker
)
315 // The standard Task methods.
318 is_runnable(Workqueue
*);
327 const Symbol_table
* symtab_
;
328 const Target
* target_
;
329 const Stringpool
* sympool_
;
331 Task_token
* final_blocker_
;
334 // This task function handles closing the file.
336 class Close_task_runner
: public Task_function_runner
339 Close_task_runner(Output_file
* of
)
343 // Run the operation.
351 // A small helper function to align an address.
354 align_address(uint64_t address
, uint64_t addralign
)
357 address
= (address
+ addralign
- 1) &~ (addralign
- 1);
361 } // End namespace gold.
363 #endif // !defined(GOLD_LAYOUT_H)