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,
40 Task_token* this_blocker)
41 : options_(options), input_objects_(input_objects), symtab_(symtab),
42 this_blocker_(this_blocker)
47 // The standard Task methods.
50 is_runnable(Workqueue*);
59 Layout_task(const Layout_task&);
60 Layout_task& operator=(const Layout_task&);
62 const General_options& options_;
63 const Input_objects* input_objects_;
64 Symbol_table* symtab_;
65 Task_token* this_blocker_;
68 // This class handles the details of laying out input sections.
73 Layout(const General_options& options);
75 // Initialize the object.
79 // Given an input section named NAME with data in SHDR from the
80 // object file OBJECT, return the output section where this input
81 // section should go. Set *OFFSET to the offset within the output
83 template<int size, bool big_endian>
85 layout(Object *object, const char* name,
86 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
88 // Return the Stringpool used for symbol names.
91 { return &this->sympool_; }
93 // Return whether a section is a .gnu.linkonce section, given the
96 is_linkonce(const char* name)
97 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
99 // Record the signature of a comdat section, and return whether to
100 // include it in the link. The GROUP parameter is true for a
101 // section group signature, false for a signature derived from a
102 // .gnu.linkonce section.
104 add_comdat(const char*, bool group);
106 // Finalize the layout after all the input sections have been added.
108 finalize(const Input_objects*, Symbol_table*);
110 // Write out data not associated with an input file or the symbol
113 write_data(Output_file*) const;
115 // The list of segments.
117 typedef std::vector<Output_segment*> Segment_list;
119 // The list of sections not attached to a segment.
121 typedef std::list<Output_section*> Section_list;
123 // The list of information to write out which is not attached to
124 // either a section or a segment.
125 typedef std::list<Output_data*> Data_list;
128 Layout(const Layout&);
129 Layout& operator=(const Layout&);
131 // Mapping from .gnu.linkonce section names to output section names.
132 struct Linkonce_mapping
138 static const Linkonce_mapping linkonce_mapping[];
139 static const int linkonce_mapping_count;
141 // Find the first read-only PT_LOAD segment, creating one if
144 find_first_load_seg();
146 // Set the final file offsets of all the segments.
148 set_segment_offsets(const Target*, Output_segment*);
150 // Set the final file offsets of all the sections not associated
153 set_section_offsets(off_t);
155 // Create the output sections for the symbol table.
157 create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*,
158 Output_section** osymtab,
159 Output_section** ostrtab);
161 // Create the .shstrtab section.
165 // Create the section header table.
166 Output_section_headers*
167 create_shdrs(int size, bool big_endian, off_t*);
169 // Return whether to include this section in the link.
170 template<int size, bool big_endian>
172 include_section(Object* object, const char* name,
173 const elfcpp::Shdr<size, big_endian>&);
175 // Return the output section name to use for a linkonce section
178 linkonce_output_name(const char* name);
180 // Create a new Output_section.
182 make_output_section(const char* name, elfcpp::Elf_Word type,
183 elfcpp::Elf_Xword flags);
185 // Return whether SEG1 comes before SEG2 in the output file.
187 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
189 // Map from section flags to segment flags.
190 static elfcpp::Elf_Word
191 section_flags_to_segment(elfcpp::Elf_Xword flags);
193 // A mapping used for group signatures.
194 typedef Unordered_map<std::string, bool> Signatures;
196 // Mapping from input section name/type/flags to output section. We
197 // use canonicalized strings here.
199 typedef std::pair<const char*,
200 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
205 operator()(const Key& k) const;
208 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
210 // A comparison class for segments.
212 struct Compare_segments
215 operator()(const Output_segment* seg1, const Output_segment* seg2)
216 { return Layout::segment_precedes(seg1, seg2); }
219 // A reference to the options on the command line.
220 const General_options& options_;
221 // The index of the last output section.
222 unsigned int last_shndx_;
223 // The output section names.
224 Stringpool namepool_;
225 // The output symbol names.
227 // The list of group sections and linkonce sections which we have seen.
228 Signatures signatures_;
229 // The mapping from input section name/type/flags to output sections.
230 Section_name_map section_name_map_;
231 // The list of output segments.
232 Segment_list segment_list_;
233 // The list of output sections which are not attached to any output
235 Section_list section_list_;
236 // The list of sections which require special output because they
237 // are not comprised of input sections.
238 Data_list special_output_list_;
241 // This task handles writing out data which is not part of a section
244 class Write_data_task : public Task
247 Write_data_task(const Layout* layout, Output_file* of,
248 Task_token* final_blocker)
249 : layout_(layout), of_(of), final_blocker_(final_blocker)
252 // The standard Task methods.
255 is_runnable(Workqueue*);
264 const Layout* layout_;
266 Task_token* final_blocker_;
269 // This task handles writing out the global symbols.
271 class Write_symbols_task : public Task
274 Write_symbols_task(const Symbol_table* symtab, const Target* target,
275 const Stringpool* sympool, Output_file* of,
276 Task_token* final_blocker)
277 : symtab_(symtab), target_(target), sympool_(sympool), of_(of),
278 final_blocker_(final_blocker)
281 // The standard Task methods.
284 is_runnable(Workqueue*);
293 const Symbol_table* symtab_;
294 const Target* target_;
295 const Stringpool* sympool_;
297 Task_token* final_blocker_;
300 // This task handles closing the file.
302 class Close_task : public Task
305 Close_task(Output_file* of, Task_token* final_blocker)
306 : of_(of), final_blocker_(final_blocker)
309 // The standard task methods.
312 is_runnable(Workqueue*);
322 Task_token* final_blocker_;
325 } // End namespace gold.
327 #endif // !defined(GOLD_LAYOUT_H)