1 // layout.h -- lay out output file sections for gold -*- C++ -*-
12 #include "workqueue.h"
14 #include "stringpool.h"
22 class Output_section_symtab
;
23 class Output_section_headers
;
28 // This Task handles mapping the input sections to output sections and
29 // laying them out in memory.
31 class Layout_task
: public Task
34 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
35 // input objects, THIS_BLOCKER is a token which blocks this task
36 // from executing until all the input symbols have been read.
37 Layout_task(const General_options
& options
,
38 const Input_objects
* input_objects
,
41 Task_token
* this_blocker
)
42 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
43 layout_(layout
), this_blocker_(this_blocker
)
48 // The standard Task methods.
51 is_runnable(Workqueue
*);
60 Layout_task(const Layout_task
&);
61 Layout_task
& operator=(const Layout_task
&);
63 const General_options
& options_
;
64 const Input_objects
* input_objects_
;
65 Symbol_table
* symtab_
;
67 Task_token
* this_blocker_
;
70 // This class handles the details of laying out input sections.
75 Layout(const General_options
& options
);
77 // Given an input section named NAME with data in SHDR from the
78 // object file OBJECT, return the output section where this input
79 // section should go. Set *OFFSET to the offset within the output
81 template<int size
, bool big_endian
>
83 layout(Object
*object
, const char* name
,
84 const elfcpp::Shdr
<size
, big_endian
>& shdr
, off_t
* offset
);
86 // Return the Stringpool used for symbol names.
89 { return &this->sympool_
; }
91 // Return whether a section is a .gnu.linkonce section, given the
94 is_linkonce(const char* name
)
95 { return strncmp(name
, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
97 // Record the signature of a comdat section, and return whether to
98 // include it in the link. The GROUP parameter is true for a
99 // section group signature, false for a signature derived from a
100 // .gnu.linkonce section.
102 add_comdat(const char*, bool group
);
104 // Finalize the layout after all the input sections have been added.
106 finalize(const Input_objects
*, Symbol_table
*);
108 // Write out data not associated with an input file or the symbol
111 write_data(Output_file
*) const;
113 // The list of segments.
115 typedef std::vector
<Output_segment
*> Segment_list
;
117 // The list of sections not attached to a segment.
119 typedef std::list
<Output_section
*> Section_list
;
121 // The list of information to write out which is not attached to
122 // either a section or a segment.
123 typedef std::list
<Output_data
*> Data_list
;
126 Layout(const Layout
&);
127 Layout
& operator=(const Layout
&);
129 // Mapping from .gnu.linkonce section names to output section names.
130 struct Linkonce_mapping
136 static const Linkonce_mapping linkonce_mapping
[];
137 static const int linkonce_mapping_count
;
139 // Find the first read-only PT_LOAD segment, creating one if
142 find_first_load_seg();
144 // Set the final file offsets of all the segments.
146 set_segment_offsets(const Target
*, Output_segment
*);
148 // Set the final file offsets of all the sections not associated
151 set_section_offsets(off_t
);
153 // Create the output sections for the symbol table.
155 create_symtab_sections(int size
, const Input_objects
*, Symbol_table
*, off_t
*,
156 Output_section
** osymtab
,
157 Output_section
** ostrtab
);
159 // Create the .shstrtab section.
163 // Create the section header table.
164 Output_section_headers
*
165 create_shdrs(int size
, bool big_endian
, off_t
*);
167 // Return whether to include this section in the link.
168 template<int size
, bool big_endian
>
170 include_section(Object
* object
, const char* name
,
171 const elfcpp::Shdr
<size
, big_endian
>&);
173 // Return the output section name to use for a linkonce section
176 linkonce_output_name(const char* name
);
178 // Create a new Output_section.
180 make_output_section(const char* name
, elfcpp::Elf_Word type
,
181 elfcpp::Elf_Xword flags
);
183 // Return whether SEG1 comes before SEG2 in the output file.
185 segment_precedes(const Output_segment
* seg1
, const Output_segment
* seg2
);
187 // Map from section flags to segment flags.
188 static elfcpp::Elf_Word
189 section_flags_to_segment(elfcpp::Elf_Xword flags
);
191 // A mapping used for group signatures.
192 typedef Unordered_map
<std::string
, bool> Signatures
;
194 // Mapping from input section name/type/flags to output section. We
195 // use canonicalized strings here.
197 typedef std::pair
<const char*,
198 std::pair
<elfcpp::Elf_Word
, elfcpp::Elf_Xword
> > Key
;
203 operator()(const Key
& k
) const;
206 typedef Unordered_map
<Key
, Output_section
*, Hash_key
> Section_name_map
;
208 // A comparison class for segments.
210 struct Compare_segments
213 operator()(const Output_segment
* seg1
, const Output_segment
* seg2
)
214 { return Layout::segment_precedes(seg1
, seg2
); }
217 // A reference to the options on the command line.
218 const General_options
& options_
;
219 // The index of the last output section.
220 unsigned int last_shndx_
;
221 // The output section names.
222 Stringpool namepool_
;
223 // The output symbol names.
225 // The list of group sections and linkonce sections which we have seen.
226 Signatures signatures_
;
227 // The mapping from input section name/type/flags to output sections.
228 Section_name_map section_name_map_
;
229 // The list of output segments.
230 Segment_list segment_list_
;
231 // The list of output sections which are not attached to any output
233 Section_list section_list_
;
234 // The list of sections which require special output because they
235 // are not comprised of input sections.
236 Data_list special_output_list_
;
239 // This task handles writing out data which is not part of a section
242 class Write_data_task
: public Task
245 Write_data_task(const Layout
* layout
, Output_file
* of
,
246 Task_token
* final_blocker
)
247 : layout_(layout
), of_(of
), final_blocker_(final_blocker
)
250 // The standard Task methods.
253 is_runnable(Workqueue
*);
262 const Layout
* layout_
;
264 Task_token
* final_blocker_
;
267 // This task handles writing out the global symbols.
269 class Write_symbols_task
: public Task
272 Write_symbols_task(const Symbol_table
* symtab
, const Target
* target
,
273 const Stringpool
* sympool
, Output_file
* of
,
274 Task_token
* final_blocker
)
275 : symtab_(symtab
), target_(target
), sympool_(sympool
), of_(of
),
276 final_blocker_(final_blocker
)
279 // The standard Task methods.
282 is_runnable(Workqueue
*);
291 const Symbol_table
* symtab_
;
292 const Target
* target_
;
293 const Stringpool
* sympool_
;
295 Task_token
* final_blocker_
;
298 // This task handles closing the file.
300 class Close_task
: public Task
303 Close_task(Output_file
* of
, Task_token
* final_blocker
)
304 : of_(of
), final_blocker_(final_blocker
)
307 // The standard task methods.
310 is_runnable(Workqueue
*);
320 Task_token
* final_blocker_
;
323 } // End namespace gold.
325 #endif // !defined(GOLD_LAYOUT_H)