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 whether a section is a .gnu.linkonce section, given the
95 is_linkonce(const char* name)
96 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
98 // Record the signature of a comdat section, and return whether to
99 // include it in the link. The GROUP parameter is true for a
100 // section group signature, false for a signature derived from a
101 // .gnu.linkonce section.
103 add_comdat(const char*, bool group);
105 // Finalize the layout after all the input sections have been added.
107 finalize(const Input_objects*, Symbol_table*);
109 // Return the TLS segment.
112 { return this->tls_segment_; }
114 // Write out data not associated with an input file or the symbol
117 write_data(const Symbol_table*, const Target*, Output_file*) const;
119 // Return an output section named NAME, or NULL if there is none.
121 find_output_section(const char* name) const;
123 // Return an output segment of type TYPE, with segment flags SET set
124 // and segment flags CLEAR clear. Return NULL if there is none.
126 find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
127 elfcpp::Elf_Word clear) const;
129 // The list of segments.
131 typedef std::vector<Output_segment*> Segment_list;
133 // The list of sections not attached to a segment.
135 typedef std::vector<Output_section*> Section_list;
137 // The list of information to write out which is not attached to
138 // either a section or a segment.
139 typedef std::vector<Output_data*> Data_list;
142 Layout(const Layout&);
143 Layout& operator=(const Layout&);
145 // Mapping from .gnu.linkonce section names to output section names.
146 struct Linkonce_mapping
153 static const Linkonce_mapping linkonce_mapping[];
154 static const int linkonce_mapping_count;
156 // Find the first read-only PT_LOAD segment, creating one if
159 find_first_load_seg();
161 // Create the output sections for the symbol table.
163 create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*,
164 Output_section** ostrtab);
166 // Create the .shstrtab section.
170 // Create the section header table.
171 Output_section_headers*
172 create_shdrs(int size, bool big_endian, off_t*);
174 // Create the dynamic symbol table.
176 create_dynamic_symtab(const Target*, Output_data_dynamic*, Symbol_table*);
178 // Finish the .dynamic section and PT_DYNAMIC segment.
180 finish_dynamic_section(const Input_objects*, const Symbol_table*,
181 Output_data_dynamic*);
183 // Create the .interp section and PT_INTERP segment.
185 create_interp(const Target* target);
187 // Return whether to include this section in the link.
188 template<int size, bool big_endian>
190 include_section(Object* object, const char* name,
191 const elfcpp::Shdr<size, big_endian>&);
193 // Return the output section name to use given an input section
194 // name. Set *PLEN to the length of the name. *PLEN must be
195 // initialized to the length of NAME.
197 output_section_name(const char* name, size_t* plen);
199 // Return the output section name to use for a linkonce section
200 // name. PLEN is as for output_section_name.
202 linkonce_output_name(const char* name, size_t* plen);
204 // Return the output section for NAME, TYPE and FLAGS.
206 get_output_section(const char* name, Stringpool::Key name_key,
207 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
209 // Create a new Output_section.
211 make_output_section(const char* name, elfcpp::Elf_Word type,
212 elfcpp::Elf_Xword flags);
214 // Set the final file offsets of all the segments.
216 set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
218 // Set the final file offsets and section indices of all the
219 // sections not associated with a segment.
221 set_section_offsets(off_t, unsigned int *pshndx);
223 // Return whether SEG1 comes before SEG2 in the output file.
225 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
227 // Map from section flags to segment flags.
228 static elfcpp::Elf_Word
229 section_flags_to_segment(elfcpp::Elf_Xword flags);
231 // A mapping used for group signatures.
232 typedef Unordered_map<std::string, bool> Signatures;
234 // Mapping from input section name/type/flags to output section. We
235 // use canonicalized strings here.
237 typedef std::pair<Stringpool::Key,
238 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
243 operator()(const Key& k) const;
246 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
248 // A comparison class for segments.
250 struct Compare_segments
253 operator()(const Output_segment* seg1, const Output_segment* seg2)
254 { return Layout::segment_precedes(seg1, seg2); }
257 // A reference to the options on the command line.
258 const General_options& options_;
259 // The output section names.
260 Stringpool namepool_;
261 // The output symbol names.
263 // The dynamic strings, if needed.
265 // The list of group sections and linkonce sections which we have seen.
266 Signatures signatures_;
267 // The mapping from input section name/type/flags to output sections.
268 Section_name_map section_name_map_;
269 // The list of output segments.
270 Segment_list segment_list_;
271 // The list of output sections.
272 Section_list section_list_;
273 // The list of output sections which are not attached to any output
275 Section_list unattached_section_list_;
276 // The list of unattached Output_data objects which require special
277 // handling because they are not Output_sections.
278 Data_list special_output_list_;
279 // A pointer to the PT_TLS segment if there is one.
280 Output_segment* tls_segment_;
281 // The SHT_SYMTAB output section.
282 Output_section* symtab_section_;
283 // The SHT_DYNSYM output section if there is one.
284 Output_section* dynsym_section_;
285 // The SHT_DYNAMIC output section if there is one.
286 Output_section* dynamic_section_;
289 // This task handles writing out data which is not part of a section
292 class Write_data_task : public Task
295 Write_data_task(const Layout* layout, const Symbol_table* symtab,
296 const Target* target, Output_file* of,
297 Task_token* final_blocker)
298 : layout_(layout), symtab_(symtab), target_(target), of_(of),
299 final_blocker_(final_blocker)
302 // The standard Task methods.
305 is_runnable(Workqueue*);
314 const Layout* layout_;
315 const Symbol_table* symtab_;
316 const Target* target_;
318 Task_token* final_blocker_;
321 // This task handles writing out the global symbols.
323 class Write_symbols_task : public Task
326 Write_symbols_task(const Symbol_table* symtab, const Target* target,
327 const Stringpool* sympool, Output_file* of,
328 Task_token* final_blocker)
329 : symtab_(symtab), target_(target), sympool_(sympool), of_(of),
330 final_blocker_(final_blocker)
333 // The standard Task methods.
336 is_runnable(Workqueue*);
345 const Symbol_table* symtab_;
346 const Target* target_;
347 const Stringpool* sympool_;
349 Task_token* final_blocker_;
352 // This task function handles closing the file.
354 class Close_task_runner : public Task_function_runner
357 Close_task_runner(Output_file* of)
361 // Run the operation.
369 // A small helper function to align an address.
372 align_address(uint64_t address, uint64_t addralign)
375 address = (address + addralign - 1) &~ (addralign - 1);
379 } // End namespace gold.
381 #endif // !defined(GOLD_LAYOUT_H)