Finished layout code.
[binutils-gdb.git] / gold / layout.h
1 // layout.h -- lay out output file sections for gold -*- C++ -*-
2
3 #ifndef GOLD_LAYOUT_H
4 #define GOLD_LAYOUT_H
5
6 #include <list>
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "options.h"
12 #include "workqueue.h"
13 #include "object.h"
14 #include "stringpool.h"
15
16 namespace gold
17 {
18
19 class Input_objects;
20 class Symbol_table;
21 class Output_section;
22 class Output_section_symtab;
23 class Output_section_headers;
24 class Output_segment;
25 class Output_data;
26
27 // This Task handles mapping the input sections to output sections and
28 // laying them out in memory.
29
30 class Layout_task : public Task
31 {
32 public:
33 // OPTIONS is the command line options, INPUT_OBJECTS is the list of
34 // input objects, THIS_BLOCKER is a token which blocks this task
35 // from executing until all the input symbols have been read.
36 Layout_task(const General_options& options,
37 const Input_objects* input_objects,
38 Symbol_table* symtab,
39 Task_token* this_blocker)
40 : options_(options), input_objects_(input_objects), symtab_(symtab),
41 this_blocker_(this_blocker)
42 { }
43
44 ~Layout_task();
45
46 // The standard Task methods.
47
48 Is_runnable_type
49 is_runnable(Workqueue*);
50
51 Task_locker*
52 locks(Workqueue*);
53
54 void
55 run(Workqueue*);
56
57 private:
58 Layout_task(const Layout_task&);
59 Layout_task& operator=(const Layout_task&);
60
61 const General_options& options_;
62 const Input_objects* input_objects_;
63 Symbol_table* symtab_;
64 Task_token* this_blocker_;
65 };
66
67 // This class handles the details of laying out input sections.
68
69 class Layout
70 {
71 public:
72 Layout(const General_options& options);
73
74 // Initialize the object.
75 void
76 init();
77
78 // Given an input section named NAME with data in SHDR from the
79 // object file OBJECT, return the output section where this input
80 // section should go. Set *OFFSET to the offset within the output
81 // section.
82 template<int size, bool big_endian>
83 Output_section*
84 layout(Object *object, const char* name,
85 const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
86
87 // Return whether a section is a .gnu.linkonce section, given the
88 // section name.
89 static inline bool
90 is_linkonce(const char* name)
91 { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
92
93 // Record the signature of a comdat section, and return whether to
94 // include it in the link. The GROUP parameter is true for a
95 // section group signature, false for a signature derived from a
96 // .gnu.linkonce section.
97 bool
98 add_comdat(const char*, bool group);
99
100 // Finalize the layout after all the input sections have been added.
101 off_t
102 finalize(const Input_objects*, Symbol_table*);
103
104 // The list of segments.
105
106 typedef std::vector<Output_segment*> Segment_list;
107
108 // The list of sections not attached to a segment.
109
110 typedef std::list<Output_section*> Section_list;
111
112 // The list of information to write out which is not attached to
113 // either a section or a segment.
114 typedef std::list<Output_data*> Data_list;
115
116 private:
117 Layout(const Layout&);
118 Layout& operator=(const Layout&);
119
120 // Mapping from .gnu.linkonce section names to output section names.
121 struct Linkonce_mapping
122 {
123 const char* from;
124 int fromlen;
125 const char* to;
126 };
127 static const Linkonce_mapping linkonce_mapping[];
128 static const int linkonce_mapping_count;
129
130 // Find the first read-only PT_LOAD segment, creating one if
131 // necessary.
132 Output_segment*
133 find_first_load_seg();
134
135 // Set the final file offsets of all the segments.
136 off_t
137 set_segment_offsets(const Target*, Output_segment*);
138
139 // Set the final file offsets of all the sections not associated
140 // with a segment.
141 off_t
142 set_section_offsets(off_t);
143
144 // Create the output sections for the symbol table.
145 void
146 create_symtab_sections(const Input_objects*, Symbol_table*,
147 Output_section** osymtab,
148 Output_section** ostrtab);
149
150 // Create the .shstrtab section.
151 Output_section*
152 create_shstrtab();
153
154 // Create the section header table.
155 Output_section_headers*
156 create_shdrs(int size, off_t);
157
158 // Return whether to include this section in the link.
159 template<int size, bool big_endian>
160 bool
161 include_section(Object* object, const char* name,
162 const elfcpp::Shdr<size, big_endian>&);
163
164 // Return the output section name to use for a linkonce section
165 // name.
166 static const char*
167 linkonce_output_name(const char* name);
168
169 // Create a new Output_section.
170 Output_section*
171 make_output_section(const char* name, elfcpp::Elf_Word type,
172 elfcpp::Elf_Xword flags);
173
174 // Return whether SEG1 comes before SEG2 in the output file.
175 static bool
176 segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
177
178 // Map from section flags to segment flags.
179 static elfcpp::Elf_Word
180 section_flags_to_segment(elfcpp::Elf_Xword flags);
181
182 // A mapping used for group signatures.
183 typedef Unordered_map<std::string, bool> Signatures;
184
185 // Mapping from input section name/type/flags to output section. We
186 // use canonicalized strings here.
187
188 typedef std::pair<const char*,
189 std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
190
191 struct Hash_key
192 {
193 size_t
194 operator()(const Key& k) const;
195 };
196
197 typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
198
199 // A comparison class for segments.
200
201 struct Compare_segments
202 {
203 bool
204 operator()(const Output_segment* seg1, const Output_segment* seg2)
205 { return Layout::segment_precedes(seg1, seg2); }
206 };
207
208 // A reference to the options on the command line.
209 const General_options& options_;
210 // The output section names.
211 Stringpool namepool_;
212 // The output symbol names.
213 Stringpool sympool_;
214 // The list of group sections and linkonce sections which we have seen.
215 Signatures signatures_;
216 // The mapping from input section name/type/flags to output sections.
217 Section_name_map section_name_map_;
218 // The list of output segments.
219 Segment_list segment_list_;
220 // The list of output sections which are not attached to any output
221 // segment.
222 Section_list section_list_;
223 };
224
225 } // End namespace gold.
226
227 #endif // !defined(GOLD_LAYOUT_H)