]>
Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
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 | ||
a2fb1b05 ILT |
11 | #include "workqueue.h" |
12 | #include "object.h" | |
13 | #include "stringpool.h" | |
14 | ||
15 | namespace gold | |
16 | { | |
17 | ||
ead1e424 | 18 | class General_options; |
54dc6425 | 19 | class Input_objects; |
75f65a3e | 20 | class Symbol_table; |
ead1e424 | 21 | class Output_section_data; |
a2fb1b05 | 22 | class Output_section; |
75f65a3e | 23 | class Output_section_headers; |
a2fb1b05 | 24 | class Output_segment; |
54dc6425 | 25 | class Output_data; |
a3ad94ed | 26 | class Output_data_dynamic; |
61ba1cf9 | 27 | class Target; |
a2fb1b05 | 28 | |
92e059d8 ILT |
29 | // This task function handles mapping the input sections to output |
30 | // sections and laying them out in memory. | |
a2fb1b05 | 31 | |
92e059d8 | 32 | class Layout_task_runner : public Task_function_runner |
a2fb1b05 ILT |
33 | { |
34 | public: | |
35 | // OPTIONS is the command line options, INPUT_OBJECTS is the list of | |
92e059d8 ILT |
36 | // input objects, SYMTAB is the symbol table, LAYOUT is the layout |
37 | // object. | |
38 | Layout_task_runner(const General_options& options, | |
39 | const Input_objects* input_objects, | |
40 | Symbol_table* symtab, | |
41 | Layout* layout) | |
75f65a3e | 42 | : options_(options), input_objects_(input_objects), symtab_(symtab), |
92e059d8 | 43 | layout_(layout) |
a2fb1b05 ILT |
44 | { } |
45 | ||
92e059d8 | 46 | // Run the operation. |
a2fb1b05 ILT |
47 | void |
48 | run(Workqueue*); | |
49 | ||
50 | private: | |
92e059d8 ILT |
51 | Layout_task_runner(const Layout_task_runner&); |
52 | Layout_task_runner& operator=(const Layout_task_runner&); | |
a2fb1b05 ILT |
53 | |
54 | const General_options& options_; | |
54dc6425 | 55 | const Input_objects* input_objects_; |
75f65a3e | 56 | Symbol_table* symtab_; |
12e14209 | 57 | Layout* layout_; |
a2fb1b05 ILT |
58 | }; |
59 | ||
60 | // This class handles the details of laying out input sections. | |
61 | ||
62 | class Layout | |
63 | { | |
64 | public: | |
54dc6425 ILT |
65 | Layout(const General_options& options); |
66 | ||
ead1e424 ILT |
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 | |
70 | // output section. | |
a2fb1b05 ILT |
71 | template<int size, bool big_endian> |
72 | Output_section* | |
f6ce93d6 | 73 | layout(Relobj *object, unsigned int shndx, const char* name, |
a2fb1b05 ILT |
74 | const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset); |
75 | ||
ead1e424 ILT |
76 | // Add an Output_section_data to the layout. This is used for |
77 | // special sections like the GOT section. | |
78 | void | |
79 | add_output_section_data(const char* name, elfcpp::Elf_Word type, | |
80 | elfcpp::Elf_Xword flags, | |
81 | Output_section_data*); | |
82 | ||
a3ad94ed ILT |
83 | // Create dynamic sections if necessary. |
84 | void | |
85 | create_initial_dynamic_sections(const Input_objects*, Symbol_table*); | |
86 | ||
61ba1cf9 ILT |
87 | // Return the Stringpool used for symbol names. |
88 | const Stringpool* | |
89 | sympool() const | |
90 | { return &this->sympool_; } | |
91 | ||
a2fb1b05 ILT |
92 | // Return whether a section is a .gnu.linkonce section, given the |
93 | // section name. | |
94 | static inline bool | |
95 | is_linkonce(const char* name) | |
96 | { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; } | |
97 | ||
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. | |
102 | bool | |
103 | add_comdat(const char*, bool group); | |
104 | ||
54dc6425 | 105 | // Finalize the layout after all the input sections have been added. |
75f65a3e ILT |
106 | off_t |
107 | finalize(const Input_objects*, Symbol_table*); | |
54dc6425 | 108 | |
92e059d8 ILT |
109 | // Return the TLS segment. |
110 | Output_segment* | |
111 | tls_segment() const | |
112 | { return this->tls_segment_; } | |
113 | ||
61ba1cf9 ILT |
114 | // Write out data not associated with an input file or the symbol |
115 | // table. | |
116 | void | |
a3ad94ed | 117 | write_data(const Symbol_table*, const Target*, Output_file*) const; |
61ba1cf9 | 118 | |
ead1e424 ILT |
119 | // Return an output section named NAME, or NULL if there is none. |
120 | Output_section* | |
121 | find_output_section(const char* name) const; | |
122 | ||
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. | |
125 | Output_segment* | |
126 | find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
127 | elfcpp::Elf_Word clear) const; | |
128 | ||
54dc6425 ILT |
129 | // The list of segments. |
130 | ||
131 | typedef std::vector<Output_segment*> Segment_list; | |
132 | ||
133 | // The list of sections not attached to a segment. | |
134 | ||
a3ad94ed | 135 | typedef std::vector<Output_section*> Section_list; |
54dc6425 ILT |
136 | |
137 | // The list of information to write out which is not attached to | |
138 | // either a section or a segment. | |
a3ad94ed | 139 | typedef std::vector<Output_data*> Data_list; |
54dc6425 | 140 | |
a2fb1b05 ILT |
141 | private: |
142 | Layout(const Layout&); | |
143 | Layout& operator=(const Layout&); | |
144 | ||
145 | // Mapping from .gnu.linkonce section names to output section names. | |
146 | struct Linkonce_mapping | |
147 | { | |
148 | const char* from; | |
149 | int fromlen; | |
150 | const char* to; | |
ead1e424 | 151 | int tolen; |
a2fb1b05 ILT |
152 | }; |
153 | static const Linkonce_mapping linkonce_mapping[]; | |
154 | static const int linkonce_mapping_count; | |
155 | ||
75f65a3e ILT |
156 | // Find the first read-only PT_LOAD segment, creating one if |
157 | // necessary. | |
158 | Output_segment* | |
159 | find_first_load_seg(); | |
160 | ||
54dc6425 ILT |
161 | // Create the output sections for the symbol table. |
162 | void | |
61ba1cf9 | 163 | create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*, |
75f65a3e | 164 | Output_section** ostrtab); |
54dc6425 | 165 | |
75f65a3e ILT |
166 | // Create the .shstrtab section. |
167 | Output_section* | |
168 | create_shstrtab(); | |
169 | ||
170 | // Create the section header table. | |
171 | Output_section_headers* | |
61ba1cf9 | 172 | create_shdrs(int size, bool big_endian, off_t*); |
54dc6425 | 173 | |
dbe717ef ILT |
174 | // Create the dynamic symbol table. |
175 | void | |
a3ad94ed | 176 | create_dynamic_symtab(const Target*, Output_data_dynamic*, Symbol_table*); |
dbe717ef | 177 | |
a3ad94ed | 178 | // Finish the .dynamic section and PT_DYNAMIC segment. |
dbe717ef | 179 | void |
a3ad94ed ILT |
180 | finish_dynamic_section(const Input_objects*, const Symbol_table*, |
181 | Output_data_dynamic*); | |
dbe717ef ILT |
182 | |
183 | // Create the .interp section and PT_INTERP segment. | |
184 | void | |
185 | create_interp(const Target* target); | |
186 | ||
a2fb1b05 ILT |
187 | // Return whether to include this section in the link. |
188 | template<int size, bool big_endian> | |
189 | bool | |
190 | include_section(Object* object, const char* name, | |
191 | const elfcpp::Shdr<size, big_endian>&); | |
192 | ||
ead1e424 ILT |
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. | |
196 | static const char* | |
197 | output_section_name(const char* name, size_t* plen); | |
198 | ||
a2fb1b05 | 199 | // Return the output section name to use for a linkonce section |
ead1e424 | 200 | // name. PLEN is as for output_section_name. |
a2fb1b05 | 201 | static const char* |
ead1e424 ILT |
202 | linkonce_output_name(const char* name, size_t* plen); |
203 | ||
204 | // Return the output section for NAME, TYPE and FLAGS. | |
205 | Output_section* | |
f0641a0b ILT |
206 | get_output_section(const char* name, Stringpool::Key name_key, |
207 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags); | |
a2fb1b05 ILT |
208 | |
209 | // Create a new Output_section. | |
210 | Output_section* | |
211 | make_output_section(const char* name, elfcpp::Elf_Word type, | |
212 | elfcpp::Elf_Xword flags); | |
213 | ||
dbe717ef ILT |
214 | // Set the final file offsets of all the segments. |
215 | off_t | |
216 | set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx); | |
217 | ||
218 | // Set the final file offsets and section indices of all the | |
219 | // sections not associated with a segment. | |
220 | off_t | |
221 | set_section_offsets(off_t, unsigned int *pshndx); | |
222 | ||
a2fb1b05 ILT |
223 | // Return whether SEG1 comes before SEG2 in the output file. |
224 | static bool | |
b3168e9d | 225 | segment_precedes(const Output_segment* seg1, const Output_segment* seg2); |
a2fb1b05 ILT |
226 | |
227 | // Map from section flags to segment flags. | |
228 | static elfcpp::Elf_Word | |
229 | section_flags_to_segment(elfcpp::Elf_Xword flags); | |
230 | ||
231 | // A mapping used for group signatures. | |
232 | typedef Unordered_map<std::string, bool> Signatures; | |
233 | ||
234 | // Mapping from input section name/type/flags to output section. We | |
235 | // use canonicalized strings here. | |
236 | ||
f0641a0b | 237 | typedef std::pair<Stringpool::Key, |
a2fb1b05 ILT |
238 | std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key; |
239 | ||
240 | struct Hash_key | |
241 | { | |
242 | size_t | |
243 | operator()(const Key& k) const; | |
244 | }; | |
245 | ||
246 | typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map; | |
247 | ||
248 | // A comparison class for segments. | |
249 | ||
250 | struct Compare_segments | |
251 | { | |
252 | bool | |
253 | operator()(const Output_segment* seg1, const Output_segment* seg2) | |
254 | { return Layout::segment_precedes(seg1, seg2); } | |
255 | }; | |
256 | ||
a2fb1b05 ILT |
257 | // A reference to the options on the command line. |
258 | const General_options& options_; | |
259 | // The output section names. | |
260 | Stringpool namepool_; | |
75f65a3e ILT |
261 | // The output symbol names. |
262 | Stringpool sympool_; | |
a3ad94ed ILT |
263 | // The dynamic strings, if needed. |
264 | Stringpool dynpool_; | |
a2fb1b05 ILT |
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_; | |
a3ad94ed ILT |
271 | // The list of output sections. |
272 | Section_list section_list_; | |
a2fb1b05 ILT |
273 | // The list of output sections which are not attached to any output |
274 | // segment. | |
a3ad94ed ILT |
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. | |
61ba1cf9 | 278 | Data_list special_output_list_; |
92e059d8 ILT |
279 | // A pointer to the PT_TLS segment if there is one. |
280 | Output_segment* tls_segment_; | |
a3ad94ed ILT |
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_; | |
61ba1cf9 ILT |
287 | }; |
288 | ||
289 | // This task handles writing out data which is not part of a section | |
290 | // or segment. | |
291 | ||
292 | class Write_data_task : public Task | |
293 | { | |
294 | public: | |
a3ad94ed ILT |
295 | Write_data_task(const Layout* layout, const Symbol_table* symtab, |
296 | const Target* target, Output_file* of, | |
61ba1cf9 | 297 | Task_token* final_blocker) |
a3ad94ed ILT |
298 | : layout_(layout), symtab_(symtab), target_(target), of_(of), |
299 | final_blocker_(final_blocker) | |
61ba1cf9 ILT |
300 | { } |
301 | ||
302 | // The standard Task methods. | |
303 | ||
304 | Is_runnable_type | |
305 | is_runnable(Workqueue*); | |
306 | ||
307 | Task_locker* | |
308 | locks(Workqueue*); | |
309 | ||
310 | void | |
311 | run(Workqueue*); | |
312 | ||
313 | private: | |
314 | const Layout* layout_; | |
a3ad94ed ILT |
315 | const Symbol_table* symtab_; |
316 | const Target* target_; | |
61ba1cf9 ILT |
317 | Output_file* of_; |
318 | Task_token* final_blocker_; | |
319 | }; | |
320 | ||
321 | // This task handles writing out the global symbols. | |
322 | ||
323 | class Write_symbols_task : public Task | |
324 | { | |
325 | public: | |
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) | |
331 | { } | |
332 | ||
333 | // The standard Task methods. | |
334 | ||
335 | Is_runnable_type | |
336 | is_runnable(Workqueue*); | |
337 | ||
338 | Task_locker* | |
339 | locks(Workqueue*); | |
340 | ||
341 | void | |
342 | run(Workqueue*); | |
343 | ||
344 | private: | |
345 | const Symbol_table* symtab_; | |
346 | const Target* target_; | |
347 | const Stringpool* sympool_; | |
348 | Output_file* of_; | |
349 | Task_token* final_blocker_; | |
350 | }; | |
351 | ||
92e059d8 | 352 | // This task function handles closing the file. |
61ba1cf9 | 353 | |
92e059d8 | 354 | class Close_task_runner : public Task_function_runner |
61ba1cf9 ILT |
355 | { |
356 | public: | |
92e059d8 ILT |
357 | Close_task_runner(Output_file* of) |
358 | : of_(of) | |
61ba1cf9 ILT |
359 | { } |
360 | ||
92e059d8 | 361 | // Run the operation. |
61ba1cf9 ILT |
362 | void |
363 | run(Workqueue*); | |
364 | ||
365 | private: | |
366 | Output_file* of_; | |
a2fb1b05 ILT |
367 | }; |
368 | ||
ead1e424 ILT |
369 | // A small helper function to align an address. |
370 | ||
371 | inline uint64_t | |
372 | align_address(uint64_t address, uint64_t addralign) | |
373 | { | |
374 | if (addralign != 0) | |
375 | address = (address + addralign - 1) &~ (addralign - 1); | |
376 | return address; | |
377 | } | |
378 | ||
a2fb1b05 ILT |
379 | } // End namespace gold. |
380 | ||
381 | #endif // !defined(GOLD_LAYOUT_H) |