More section layout code.
[binutils.git] / gold / layout.h
blob527d2da1f7686d6936bef86cbc06ba3fe31c6d60
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
3 #ifndef GOLD_LAYOUT_H
4 #define GOLD_LAYOUT_H
6 #include <list>
7 #include <string>
8 #include <utility>
9 #include <vector>
11 #include "options.h"
12 #include "workqueue.h"
13 #include "object.h"
14 #include "stringpool.h"
16 namespace gold
19 class Input_objects;
20 class Output_section;
21 class Output_section_symtab;
22 class Output_segment;
23 class Output_data;
25 // This Task handles mapping the input sections to output sections and
26 // laying them out in memory.
28 class Layout_task : public Task
30 public:
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)
39 { }
41 ~Layout_task();
43 // The standard Task methods.
45 Is_runnable_type
46 is_runnable(Workqueue*);
48 Task_locker*
49 locks(Workqueue*);
51 void
52 run(Workqueue*);
54 private:
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.
65 class Layout
67 public:
68 Layout(const General_options& options);
70 // Initialize the object.
71 void
72 init();
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
77 // section.
78 template<int size, bool big_endian>
79 Output_section*
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
84 // section name.
85 static inline bool
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.
93 bool
94 add_comdat(const char*, bool group);
96 // Finalize the layout after all the input sections have been added.
97 void
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;
112 private:
113 Layout(const Layout&);
114 Layout& operator=(const Layout&);
116 // Mapping from .gnu.linkonce section names to output section names.
117 struct Linkonce_mapping
119 const char* from;
120 int fromlen;
121 const char* to;
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>
128 void
129 add_symtab_locals(Object* object, const elfcpp::Shdr<size, big_endian>&);
131 // Create the output sections for the symbol table.
132 void
133 create_symtab_sections();
135 // Finalize the symbol table.
136 void
137 finalize_symtab();
139 // Return whether to include this section in the link.
140 template<int size, bool big_endian>
141 bool
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
146 // name.
147 static const char*
148 linkonce_output_name(const char* name);
150 // Create a new Output_section.
151 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.
156 static bool
157 Layout::segment_precedes(const Output_segment* seg1,
158 const Output_segment* seg2);
160 // Map from section flags to segment flags.
161 static elfcpp::Elf_Word
162 section_flags_to_segment(elfcpp::Elf_Xword flags);
164 // A mapping used for group signatures.
165 typedef Unordered_map<std::string, bool> Signatures;
167 // Mapping from input section name/type/flags to output section. We
168 // use canonicalized strings here.
170 typedef std::pair<const char*,
171 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
173 struct Hash_key
175 size_t
176 operator()(const Key& k) const;
179 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
181 // A comparison class for segments.
183 struct Compare_segments
185 bool
186 operator()(const Output_segment* seg1, const Output_segment* seg2)
187 { return Layout::segment_precedes(seg1, seg2); }
190 // A reference to the options on the command line.
191 const General_options& options_;
192 // The output section names.
193 Stringpool namepool_;
194 // The list of group sections and linkonce sections which we have seen.
195 Signatures signatures_;
196 // The mapping from input section name/type/flags to output sections.
197 Section_name_map section_name_map_;
198 // The list of output segments.
199 Segment_list segment_list_;
200 // The list of output sections which are not attached to any output
201 // segment.
202 Section_list section_list_;
203 // The list of output data objects which are not attached to any
204 // output section or output segment.
205 Data_list data_list_;
208 } // End namespace gold.
210 #endif // !defined(GOLD_LAYOUT_H)