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 function handles mapping the input sections to output
29 // sections and laying them out in memory.
31 class Layout_task_runner : public Task_function_runner
34 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
35 // input objects, SYMTAB is the symbol table, LAYOUT is the layout
37 Layout_task_runner(const General_options& options,
38 const Input_objects* input_objects,
41 : options_(options), input_objects_(input_objects), symtab_(symtab),
50 Layout_task_runner(const Layout_task_runner&);
51 Layout_task_runner& operator=(const Layout_task_runner&);
53 const General_options& options_;
54 const Input_objects* input_objects_;
55 Symbol_table* symtab_;
59 // This class handles the details of laying out input sections.
64 Layout(const General_options& options);
66 // Given an input section named NAME with data in SHDR from the
67 // object file OBJECT, return the output section where this input
68 // section should go. Set *OFFSET to the offset within the output
70 template<int size, bool big_endian>
72 layout(Object *object, const char* name,
73 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
75 // Return the Stringpool used for symbol names.
78 { return &this->sympool_; }
80 // Return whether a section is a .gnu.linkonce section, given the
83 is_linkonce(const char* name)
84 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
86 // Record the signature of a comdat section, and return whether to
87 // include it in the link. The GROUP parameter is true for a
88 // section group signature, false for a signature derived from a
89 // .gnu.linkonce section.
91 add_comdat(const char*, bool group);
93 // Finalize the layout after all the input sections have been added.
95 finalize(const Input_objects*, Symbol_table*);
97 // Return the TLS segment.
100 { return this->tls_segment_; }
102 // Write out data not associated with an input file or the symbol
105 write_data(Output_file*) const;
107 // The list of segments.
109 typedef std::vector<Output_segment*> Segment_list;
111 // The list of sections not attached to a segment.
113 typedef std::list<Output_section*> Section_list;
115 // The list of information to write out which is not attached to
116 // either a section or a segment.
117 typedef std::list<Output_data*> Data_list;
120 Layout(const Layout&);
121 Layout& operator=(const Layout&);
123 // Mapping from .gnu.linkonce section names to output section names.
124 struct Linkonce_mapping
130 static const Linkonce_mapping linkonce_mapping[];
131 static const int linkonce_mapping_count;
133 // Find the first read-only PT_LOAD segment, creating one if
136 find_first_load_seg();
138 // Set the final file offsets of all the segments.
140 set_segment_offsets(const Target*, Output_segment*);
142 // Set the final file offsets of all the sections not associated
145 set_section_offsets(off_t);
147 // Create the output sections for the symbol table.
149 create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*,
150 Output_section** osymtab,
151 Output_section** ostrtab);
153 // Create the .shstrtab section.
157 // Create the section header table.
158 Output_section_headers*
159 create_shdrs(int size, bool big_endian, off_t*);
161 // Return whether to include this section in the link.
162 template<int size, bool big_endian>
164 include_section(Object* object, const char* name,
165 const elfcpp::Shdr<size, big_endian>&);
167 // Return the output section name to use for a linkonce section
170 linkonce_output_name(const char* name);
172 // Create a new Output_section.
174 make_output_section(const char* name, elfcpp::Elf_Word type,
175 elfcpp::Elf_Xword flags);
177 // Return whether SEG1 comes before SEG2 in the output file.
179 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
181 // Map from section flags to segment flags.
182 static elfcpp::Elf_Word
183 section_flags_to_segment(elfcpp::Elf_Xword flags);
185 // A mapping used for group signatures.
186 typedef Unordered_map<std::string, bool> Signatures;
188 // Mapping from input section name/type/flags to output section. We
189 // use canonicalized strings here.
191 typedef std::pair<const char*,
192 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
197 operator()(const Key& k) const;
200 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
202 // A comparison class for segments.
204 struct Compare_segments
207 operator()(const Output_segment* seg1, const Output_segment* seg2)
208 { return Layout::segment_precedes(seg1, seg2); }
211 // A reference to the options on the command line.
212 const General_options& options_;
213 // The index of the last output section.
214 unsigned int last_shndx_;
215 // The output section names.
216 Stringpool namepool_;
217 // The output symbol names.
219 // The list of group sections and linkonce sections which we have seen.
220 Signatures signatures_;
221 // The mapping from input section name/type/flags to output sections.
222 Section_name_map section_name_map_;
223 // The list of output segments.
224 Segment_list segment_list_;
225 // The list of output sections which are not attached to any output
227 Section_list section_list_;
228 // The list of sections which require special output because they
229 // are not comprised of input sections.
230 Data_list special_output_list_;
231 // A pointer to the PT_TLS segment if there is one.
232 Output_segment* tls_segment_;
235 // This task handles writing out data which is not part of a section
238 class Write_data_task : public Task
241 Write_data_task(const Layout* layout, Output_file* of,
242 Task_token* final_blocker)
243 : layout_(layout), of_(of), final_blocker_(final_blocker)
246 // The standard Task methods.
249 is_runnable(Workqueue*);
258 const Layout* layout_;
260 Task_token* final_blocker_;
263 // This task handles writing out the global symbols.
265 class Write_symbols_task : public Task
268 Write_symbols_task(const Symbol_table* symtab, const Target* target,
269 const Stringpool* sympool, Output_file* of,
270 Task_token* final_blocker)
271 : symtab_(symtab), target_(target), sympool_(sympool), of_(of),
272 final_blocker_(final_blocker)
275 // The standard Task methods.
278 is_runnable(Workqueue*);
287 const Symbol_table* symtab_;
288 const Target* target_;
289 const Stringpool* sympool_;
291 Task_token* final_blocker_;
294 // This task function handles closing the file.
296 class Close_task_runner : public Task_function_runner
299 Close_task_runner(Output_file* of)
303 // Run the operation.
311 } // End namespace gold.
313 #endif // !defined(GOLD_LAYOUT_H)