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 | ||
11 | #include "options.h" | |
12 | #include "workqueue.h" | |
13 | #include "object.h" | |
14 | #include "stringpool.h" | |
15 | ||
16 | namespace gold | |
17 | { | |
18 | ||
54dc6425 | 19 | class Input_objects; |
75f65a3e | 20 | class Symbol_table; |
a2fb1b05 | 21 | class Output_section; |
54dc6425 | 22 | class Output_section_symtab; |
75f65a3e | 23 | class Output_section_headers; |
a2fb1b05 | 24 | class Output_segment; |
54dc6425 | 25 | class Output_data; |
a2fb1b05 ILT |
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. | |
54dc6425 ILT |
36 | Layout_task(const General_options& options, |
37 | const Input_objects* input_objects, | |
75f65a3e | 38 | Symbol_table* symtab, |
a2fb1b05 | 39 | Task_token* this_blocker) |
75f65a3e | 40 | : options_(options), input_objects_(input_objects), symtab_(symtab), |
a2fb1b05 ILT |
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_; | |
54dc6425 | 62 | const Input_objects* input_objects_; |
75f65a3e | 63 | Symbol_table* symtab_; |
a2fb1b05 ILT |
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: | |
54dc6425 ILT |
72 | Layout(const General_options& options); |
73 | ||
74 | // Initialize the object. | |
75 | void | |
76 | init(); | |
a2fb1b05 ILT |
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 | ||
54dc6425 | 100 | // Finalize the layout after all the input sections have been added. |
75f65a3e ILT |
101 | off_t |
102 | finalize(const Input_objects*, Symbol_table*); | |
54dc6425 ILT |
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 | ||
a2fb1b05 ILT |
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 | ||
75f65a3e ILT |
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); | |
54dc6425 ILT |
143 | |
144 | // Create the output sections for the symbol table. | |
145 | void | |
75f65a3e ILT |
146 | create_symtab_sections(const Input_objects*, Symbol_table*, |
147 | Output_section** osymtab, | |
148 | Output_section** ostrtab); | |
54dc6425 | 149 | |
75f65a3e ILT |
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); | |
54dc6425 | 157 | |
a2fb1b05 ILT |
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 | |
b3168e9d | 176 | segment_precedes(const Output_segment* seg1, const Output_segment* seg2); |
a2fb1b05 ILT |
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 | ||
a2fb1b05 ILT |
208 | // A reference to the options on the command line. |
209 | const General_options& options_; | |
210 | // The output section names. | |
211 | Stringpool namepool_; | |
75f65a3e ILT |
212 | // The output symbol names. |
213 | Stringpool sympool_; | |
a2fb1b05 ILT |
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) |