Commit | Line | Data |
---|---|---|
494e05f4 ILT |
1 | // script-sections.h -- linker script SECTIONS for gold -*- C++ -*- |
2 | ||
3 | // Copyright 2008 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | // This is for the support of the SECTIONS clause in linker scripts. | |
24 | ||
25 | #ifndef GOLD_SCRIPT_SECTIONS_H | |
26 | #define GOLD_SCRIPT_SECTIONS_H | |
27 | ||
28 | #include <cstdio> | |
29 | #include <vector> | |
30 | ||
31 | namespace gold | |
32 | { | |
33 | ||
34 | struct Parser_output_section_header; | |
35 | struct Parser_output_section_trailer; | |
36 | struct Input_section_spec; | |
37 | class Expression; | |
38 | class Sections_element; | |
1c4f3631 ILT |
39 | class Phdrs_element; |
40 | class Output_data; | |
494e05f4 | 41 | class Output_section_definition; |
a445fddf ILT |
42 | class Output_section; |
43 | class Output_segment; | |
494e05f4 ILT |
44 | |
45 | class Script_sections | |
46 | { | |
47 | public: | |
48 | Script_sections(); | |
49 | ||
50 | // Start a SECTIONS clause. | |
51 | void | |
52 | start_sections(); | |
53 | ||
54 | // Finish a SECTIONS clause. | |
55 | void | |
56 | finish_sections(); | |
57 | ||
58 | // Return whether we ever saw a SECTIONS clause. If we did, then | |
59 | // all section layout needs to go through this class. | |
60 | bool | |
61 | saw_sections_clause() const | |
62 | { return this->saw_sections_clause_; } | |
63 | ||
64 | // Return whether we are currently processing a SECTIONS clause. | |
65 | bool | |
66 | in_sections_clause() const | |
67 | { return this->in_sections_clause_; } | |
68 | ||
1c4f3631 ILT |
69 | // Return whether we ever saw a PHDRS clause. We ignore the PHDRS |
70 | // clause unless we also saw a SECTIONS clause. | |
71 | bool | |
72 | saw_phdrs_clause() const | |
73 | { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; } | |
74 | ||
494e05f4 ILT |
75 | // Start processing entries for an output section. |
76 | void | |
77 | start_output_section(const char* name, size_t namelen, | |
78 | const Parser_output_section_header*); | |
79 | ||
80 | // Finish processing entries for an output section. | |
81 | void | |
82 | finish_output_section(const Parser_output_section_trailer*); | |
83 | ||
84 | // Add a data item to the current output section. | |
85 | void | |
86 | add_data(int size, bool is_signed, Expression* val); | |
87 | ||
88 | // Add a symbol to be defined. | |
89 | void | |
90 | add_symbol_assignment(const char* name, size_t length, Expression* value, | |
91 | bool provide, bool hidden); | |
a445fddf ILT |
92 | |
93 | // Add an assignment to the special dot symbol. | |
94 | void | |
95 | add_dot_assignment(Expression* value); | |
96 | ||
494e05f4 ILT |
97 | // Add an assertion. |
98 | void | |
99 | add_assertion(Expression* check, const char* message, size_t messagelen); | |
100 | ||
101 | // Add a setting for the fill value. | |
102 | void | |
103 | add_fill(Expression* val); | |
104 | ||
105 | // Add an input section specification. | |
106 | void | |
107 | add_input_section(const Input_section_spec* spec, bool keep); | |
108 | ||
a445fddf ILT |
109 | // Add any symbols we are defining to the symbol table. |
110 | void | |
111 | add_symbols_to_table(Symbol_table*); | |
112 | ||
113 | // Finalize symbol values and check assertions. | |
114 | void | |
115 | finalize_symbols(Symbol_table* symtab, const Layout* layout); | |
116 | ||
117 | // Find the name of the output section to use for an input file name | |
118 | // and section name. This returns a name, and sets | |
119 | // *OUTPUT_SECTION_SLOT to point to the address where the actual | |
120 | // output section may be stored. | |
121 | // 1) If the input section should be discarded, this returns NULL | |
122 | // and sets *OUTPUT_SECTION_SLOT to NULL. | |
123 | // 2) If the input section is mapped by the SECTIONS clause, this | |
124 | // returns the name to use for the output section (in permanent | |
125 | // storage), and sets *OUTPUT_SECTION_SLOT to point to where the | |
126 | // output section should be stored. **OUTPUT_SECTION_SLOT will be | |
127 | // non-NULL if we have seen this output section already. | |
128 | // 3) If the input section is not mapped by the SECTIONS clause, | |
129 | // this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to | |
130 | // NULL. | |
131 | const char* | |
132 | output_section_name(const char* file_name, const char* section_name, | |
133 | Output_section*** output_section_slot); | |
134 | ||
135 | // Place a marker for an orphan output section into the SECTIONS | |
136 | // clause. | |
137 | void | |
138 | place_orphan(Output_section* os); | |
139 | ||
140 | // Set the addresses of all the output sections. Return the segment | |
141 | // which holds the file header and segment headers, if any. | |
142 | Output_segment* | |
143 | set_section_addresses(Symbol_table*, Layout*); | |
144 | ||
1c4f3631 ILT |
145 | // Add a program header definition. |
146 | void | |
147 | add_phdr(const char* name, size_t namelen, unsigned int type, | |
148 | bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags, | |
149 | Expression* load_address); | |
150 | ||
3802b2dd ILT |
151 | // Return the number of segments we expect to create based on the |
152 | // SECTIONS clause. | |
153 | size_t | |
154 | expected_segment_count(const Layout*) const; | |
155 | ||
1c4f3631 ILT |
156 | // Add the file header and segment header to non-load segments as |
157 | // specified by the PHDRS clause. | |
158 | void | |
159 | put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers); | |
160 | ||
494e05f4 ILT |
161 | // Print the contents to the FILE. This is for debugging. |
162 | void | |
163 | print(FILE*) const; | |
164 | ||
165 | private: | |
166 | typedef std::vector<Sections_element*> Sections_elements; | |
167 | ||
1c4f3631 ILT |
168 | typedef std::vector<Phdrs_element*> Phdrs_elements; |
169 | ||
a445fddf ILT |
170 | // Create segments. |
171 | Output_segment* | |
172 | create_segments(Layout*); | |
173 | ||
174 | // Create PT_NOTE and PT_TLS segments. | |
175 | void | |
176 | create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*); | |
177 | ||
178 | // Return whether the section is a BSS section. | |
179 | static bool | |
180 | is_bss_section(const Output_section*); | |
181 | ||
1c4f3631 ILT |
182 | // Return the total size of the headers. |
183 | size_t | |
184 | total_header_size(Layout* layout) const; | |
185 | ||
186 | // Return the amount we have to subtract from the LMA to accomodate | |
187 | // headers of the given size. | |
188 | uint64_t | |
189 | header_size_adjustment(uint64_t lma, size_t sizeof_headers) const; | |
190 | ||
191 | // Create the segments from a PHDRS clause. | |
192 | Output_segment* | |
193 | create_segments_from_phdrs_clause(Layout* layout); | |
194 | ||
195 | // Attach sections to segments from a PHDRS clause. | |
196 | void | |
197 | attach_sections_using_phdrs_clause(Layout*); | |
198 | ||
199 | // Set addresses of segments from a PHDRS clause. | |
200 | Output_segment* | |
201 | set_phdrs_clause_addresses(Layout*); | |
202 | ||
494e05f4 ILT |
203 | // True if we ever saw a SECTIONS clause. |
204 | bool saw_sections_clause_; | |
205 | // True if we are currently processing a SECTIONS clause. | |
206 | bool in_sections_clause_; | |
207 | // The list of elements in the SECTIONS clause. | |
208 | Sections_elements* sections_elements_; | |
209 | // The current output section, if there is one. | |
210 | Output_section_definition* output_section_; | |
1c4f3631 ILT |
211 | // The list of program headers in the PHDRS clause. |
212 | Phdrs_elements* phdrs_elements_; | |
494e05f4 ILT |
213 | }; |
214 | ||
215 | } // End namespace gold. | |
216 | ||
217 | #endif // !defined(GOLD_SCRIPT_SECTIONS_H |