465c24ea9565195b21d220cc14f263db507911b9
[platform/upstream/binutils.git] / gold / script-sections.h
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;
39 class Phdrs_element;
40 class Output_data;
41 class Output_section_definition;
42 class Output_section;
43 class Output_segment;
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
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
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);
92
93   // Add an assignment to the special dot symbol.
94   void
95   add_dot_assignment(Expression* value);
96
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
109   // Saw DATA_SEGMENT_ALIGN.
110   void
111   data_segment_align();
112
113   // Saw DATA_SEGMENT_RELRO_END.
114   void
115   data_segment_relro_end();
116
117   // Create any required sections.
118   void
119   create_sections(Layout*);
120
121   // Add any symbols we are defining to the symbol table.
122   void
123   add_symbols_to_table(Symbol_table*);
124
125   // Finalize symbol values and check assertions.
126   void
127   finalize_symbols(Symbol_table* symtab, const Layout* layout);
128
129   // Find the name of the output section to use for an input file name
130   // and section name.  This returns a name, and sets
131   // *OUTPUT_SECTION_SLOT to point to the address where the actual
132   // output section may be stored.
133   // 1) If the input section should be discarded, this returns NULL
134   //    and sets *OUTPUT_SECTION_SLOT to NULL.
135   // 2) If the input section is mapped by the SECTIONS clause, this
136   //    returns the name to use for the output section (in permanent
137   //    storage), and sets *OUTPUT_SECTION_SLOT to point to where the
138   //    output section should be stored.  **OUTPUT_SECTION_SLOT will be
139   //    non-NULL if we have seen this output section already.
140   // 3) If the input section is not mapped by the SECTIONS clause,
141   //    this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
142   //    NULL.
143   const char*
144   output_section_name(const char* file_name, const char* section_name,
145                       Output_section*** output_section_slot);
146
147   // Place a marker for an orphan output section into the SECTIONS
148   // clause.
149   void
150   place_orphan(Output_section* os);
151
152   // Set the addresses of all the output sections.  Return the segment
153   // which holds the file header and segment headers, if any.
154   Output_segment*
155   set_section_addresses(Symbol_table*, Layout*);
156
157   // Add a program header definition.
158   void
159   add_phdr(const char* name, size_t namelen, unsigned int type,
160            bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
161            Expression* load_address);
162
163   // Return the number of segments we expect to create based on the
164   // SECTIONS clause.
165   size_t
166   expected_segment_count(const Layout*) const;
167
168   // Add the file header and segment header to non-load segments as
169   // specified by the PHDRS clause.
170   void
171   put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
172
173   // Look for an output section by name and return the address, the
174   // load address, the alignment, and the size.  This is used when an
175   // expression refers to an output section which was not actually
176   // created.  This returns true if the section was found, false
177   // otherwise.
178   bool
179   get_output_section_info(const char* name, uint64_t* address,
180                           uint64_t* load_address, uint64_t* addralign,
181                           uint64_t* size) const;
182
183   // Print the contents to the FILE.  This is for debugging.
184   void
185   print(FILE*) const;
186
187  private:
188   typedef std::vector<Sections_element*> Sections_elements;
189
190   typedef std::vector<Phdrs_element*> Phdrs_elements;
191
192   // Create segments.
193   Output_segment*
194   create_segments(Layout*);
195
196   // Create PT_NOTE and PT_TLS segments.
197   void
198   create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
199
200   // Return whether the section is a BSS section.
201   static bool
202   is_bss_section(const Output_section*);
203
204   // Return the total size of the headers.
205   size_t
206   total_header_size(Layout* layout) const;
207
208   // Return the amount we have to subtract from the LMA to accomodate
209   // headers of the given size.
210   uint64_t
211   header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
212
213   // Create the segments from a PHDRS clause.
214   Output_segment*
215   create_segments_from_phdrs_clause(Layout* layout);
216
217   // Attach sections to segments from a PHDRS clause.
218   void
219   attach_sections_using_phdrs_clause(Layout*);
220
221   // Set addresses of segments from a PHDRS clause.
222   Output_segment*
223   set_phdrs_clause_addresses(Layout*);
224
225   // True if we ever saw a SECTIONS clause.
226   bool saw_sections_clause_;
227   // True if we are currently processing a SECTIONS clause.
228   bool in_sections_clause_;
229   // The list of elements in the SECTIONS clause.
230   Sections_elements* sections_elements_;
231   // The current output section, if there is one.
232   Output_section_definition* output_section_;
233   // The list of program headers in the PHDRS clause.
234   Phdrs_elements* phdrs_elements_;
235   // The index of the next Sections_element when we see
236   // DATA_SEGMENT_ALIGN.
237   size_t data_segment_align_index_;
238   // Whether we have seen DATA_SEGMENT_RELRO_END.
239   bool saw_relro_end_;
240 };
241
242 } // End namespace gold.
243
244 #endif // !defined(GOLD_SCRIPT_SECTIONS_H