1 // layout.h -- lay out output file sections for gold -*- C++ -*-
12 #include "workqueue.h"
14 #include "stringpool.h"
21 class Output_section_symtab;
25 // This Task handles mapping the input sections to output sections and
26 // laying them out in memory.
28 class Layout_task : public Task
31 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
32 // input objects, THIS_BLOCKER is a token which blocks this task
33 // from executing until all the input symbols have been read.
34 Layout_task(const General_options& options,
35 const Input_objects* input_objects,
36 Task_token* this_blocker)
37 : options_(options), input_objects_(input_objects),
38 this_blocker_(this_blocker)
43 // The standard Task methods.
46 is_runnable(Workqueue*);
55 Layout_task(const Layout_task&);
56 Layout_task& operator=(const Layout_task&);
58 const General_options& options_;
59 const Input_objects* input_objects_;
60 Task_token* this_blocker_;
63 // This class handles the details of laying out input sections.
68 Layout(const General_options& options);
70 // Initialize the object.
74 // Given an input section named NAME with data in SHDR from the
75 // object file OBJECT, return the output section where this input
76 // section should go. Set *OFFSET to the offset within the output
78 template<int size, bool big_endian>
80 layout(Object *object, const char* name,
81 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
83 // Return whether a section is a .gnu.linkonce section, given the
86 is_linkonce(const char* name)
87 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
89 // Record the signature of a comdat section, and return whether to
90 // include it in the link. The GROUP parameter is true for a
91 // section group signature, false for a signature derived from a
92 // .gnu.linkonce section.
94 add_comdat(const char*, bool group);
96 // Finalize the layout after all the input sections have been added.
98 finalize(const Input_objects*);
100 // The list of segments.
102 typedef std::vector<Output_segment*> Segment_list;
104 // The list of sections not attached to a segment.
106 typedef std::list<Output_section*> Section_list;
108 // The list of information to write out which is not attached to
109 // either a section or a segment.
110 typedef std::list<Output_data*> Data_list;
113 Layout(const Layout&);
114 Layout& operator=(const Layout&);
116 // Mapping from .gnu.linkonce section names to output section names.
117 struct Linkonce_mapping
123 static const Linkonce_mapping linkonce_mapping[];
124 static const int linkonce_mapping_count;
126 // Lay out the local symbols from a SHT_SYMTAB section.
127 template<int size, bool big_endian>
129 add_symtab_locals(Object* object, const elfcpp::Shdr<size, big_endian>&);
131 // Create the output sections for the symbol table.
133 create_symtab_sections();
135 // Finalize the symbol table.
139 // Return whether to include this section in the link.
140 template<int size, bool big_endian>
142 include_section(Object* object, const char* name,
143 const elfcpp::Shdr<size, big_endian>&);
145 // Return the output section name to use for a linkonce section
148 linkonce_output_name(const char* name);
150 // Create a new Output_section.
152 make_output_section(const char* name, elfcpp::Elf_Word type,
153 elfcpp::Elf_Xword flags);
155 // Return whether SEG1 comes before SEG2 in the output file.
157 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
159 // Map from section flags to segment flags.
160 static elfcpp::Elf_Word
161 section_flags_to_segment(elfcpp::Elf_Xword flags);
163 // A mapping used for group signatures.
164 typedef Unordered_map<std::string, bool> Signatures;
166 // Mapping from input section name/type/flags to output section. We
167 // use canonicalized strings here.
169 typedef std::pair<const char*,
170 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
175 operator()(const Key& k) const;
178 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
180 // A comparison class for segments.
182 struct Compare_segments
185 operator()(const Output_segment* seg1, const Output_segment* seg2)
186 { return Layout::segment_precedes(seg1, seg2); }
189 // A reference to the options on the command line.
190 const General_options& options_;
191 // The output section names.
192 Stringpool namepool_;
193 // The list of group sections and linkonce sections which we have seen.
194 Signatures signatures_;
195 // The mapping from input section name/type/flags to output sections.
196 Section_name_map section_name_map_;
197 // The list of output segments.
198 Segment_list segment_list_;
199 // The list of output sections which are not attached to any output
201 Section_list section_list_;
202 // The list of output data objects which are not attached to any
203 // output section or output segment.
204 Data_list data_list_;
207 } // End namespace gold.
209 #endif // !defined(GOLD_LAYOUT_H)