1*56bb7041Schristos // script-sections.h -- linker script SECTIONS for gold   -*- C++ -*-
2*56bb7041Schristos 
3*56bb7041Schristos // Copyright (C) 2008-2020 Free Software Foundation, Inc.
4*56bb7041Schristos // Written by Ian Lance Taylor <iant@google.com>.
5*56bb7041Schristos 
6*56bb7041Schristos // This file is part of gold.
7*56bb7041Schristos 
8*56bb7041Schristos // This program is free software; you can redistribute it and/or modify
9*56bb7041Schristos // it under the terms of the GNU General Public License as published by
10*56bb7041Schristos // the Free Software Foundation; either version 3 of the License, or
11*56bb7041Schristos // (at your option) any later version.
12*56bb7041Schristos 
13*56bb7041Schristos // This program is distributed in the hope that it will be useful,
14*56bb7041Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*56bb7041Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*56bb7041Schristos // GNU General Public License for more details.
17*56bb7041Schristos 
18*56bb7041Schristos // You should have received a copy of the GNU General Public License
19*56bb7041Schristos // along with this program; if not, write to the Free Software
20*56bb7041Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*56bb7041Schristos // MA 02110-1301, USA.
22*56bb7041Schristos 
23*56bb7041Schristos // This is for the support of the SECTIONS clause in linker scripts.
24*56bb7041Schristos 
25*56bb7041Schristos #ifndef GOLD_SCRIPT_SECTIONS_H
26*56bb7041Schristos #define GOLD_SCRIPT_SECTIONS_H
27*56bb7041Schristos 
28*56bb7041Schristos #include <cstdio>
29*56bb7041Schristos #include <list>
30*56bb7041Schristos #include <vector>
31*56bb7041Schristos 
32*56bb7041Schristos namespace gold
33*56bb7041Schristos {
34*56bb7041Schristos 
35*56bb7041Schristos struct Parser_output_section_header;
36*56bb7041Schristos struct Parser_output_section_trailer;
37*56bb7041Schristos struct Input_section_spec;
38*56bb7041Schristos class Expression;
39*56bb7041Schristos class Sections_element;
40*56bb7041Schristos class Memory_region;
41*56bb7041Schristos class Phdrs_element;
42*56bb7041Schristos class Output_data;
43*56bb7041Schristos class Output_section_definition;
44*56bb7041Schristos class Output_section;
45*56bb7041Schristos class Output_segment;
46*56bb7041Schristos class Orphan_section_placement;
47*56bb7041Schristos 
48*56bb7041Schristos class Script_sections
49*56bb7041Schristos {
50*56bb7041Schristos  public:
51*56bb7041Schristos   // This is a list, not a vector, because we insert orphan sections
52*56bb7041Schristos   // in the middle.
53*56bb7041Schristos   typedef std::list<Sections_element*> Sections_elements;
54*56bb7041Schristos 
55*56bb7041Schristos   // Logical script section types.  We map section types returned by the
56*56bb7041Schristos   // parser into these since some section types have the same semantics.
57*56bb7041Schristos   enum Section_type
58*56bb7041Schristos   {
59*56bb7041Schristos     // No section type specified.
60*56bb7041Schristos     ST_NONE,
61*56bb7041Schristos     // Section is NOLOAD.  We allocate space in the output but section
62*56bb7041Schristos     // is not loaded in runtime.
63*56bb7041Schristos     ST_NOLOAD,
64*56bb7041Schristos     // No space is allocated to section.
65*56bb7041Schristos     ST_NOALLOC
66*56bb7041Schristos   };
67*56bb7041Schristos 
68*56bb7041Schristos   Script_sections();
69*56bb7041Schristos 
70*56bb7041Schristos   // Start a SECTIONS clause.
71*56bb7041Schristos   void
72*56bb7041Schristos   start_sections();
73*56bb7041Schristos 
74*56bb7041Schristos   // Finish a SECTIONS clause.
75*56bb7041Schristos   void
76*56bb7041Schristos   finish_sections();
77*56bb7041Schristos 
78*56bb7041Schristos   // Return whether we ever saw a SECTIONS clause.  If we did, then
79*56bb7041Schristos   // all section layout needs to go through this class.
80*56bb7041Schristos   bool
saw_sections_clause()81*56bb7041Schristos   saw_sections_clause() const
82*56bb7041Schristos   { return this->saw_sections_clause_; }
83*56bb7041Schristos 
84*56bb7041Schristos   // Return whether we are currently processing a SECTIONS clause.
85*56bb7041Schristos   bool
in_sections_clause()86*56bb7041Schristos   in_sections_clause() const
87*56bb7041Schristos   { return this->in_sections_clause_; }
88*56bb7041Schristos 
89*56bb7041Schristos   // Return whether we ever saw a PHDRS clause.  We ignore the PHDRS
90*56bb7041Schristos   // clause unless we also saw a SECTIONS clause.
91*56bb7041Schristos   bool
saw_phdrs_clause()92*56bb7041Schristos   saw_phdrs_clause() const
93*56bb7041Schristos   { return this->saw_sections_clause_ && this->phdrs_elements_ != NULL; }
94*56bb7041Schristos 
95*56bb7041Schristos   // Start processing entries for an output section.
96*56bb7041Schristos   void
97*56bb7041Schristos   start_output_section(const char* name, size_t namelen,
98*56bb7041Schristos 		       const Parser_output_section_header*);
99*56bb7041Schristos 
100*56bb7041Schristos   // Finish processing entries for an output section.
101*56bb7041Schristos   void
102*56bb7041Schristos   finish_output_section(const Parser_output_section_trailer*);
103*56bb7041Schristos 
104*56bb7041Schristos   // Add a data item to the current output section.
105*56bb7041Schristos   void
106*56bb7041Schristos   add_data(int size, bool is_signed, Expression* val);
107*56bb7041Schristos 
108*56bb7041Schristos   // Add a symbol to be defined.
109*56bb7041Schristos   void
110*56bb7041Schristos   add_symbol_assignment(const char* name, size_t length, Expression* value,
111*56bb7041Schristos 			bool provide, bool hidden);
112*56bb7041Schristos 
113*56bb7041Schristos   // Add an assignment to the special dot symbol.
114*56bb7041Schristos   void
115*56bb7041Schristos   add_dot_assignment(Expression* value);
116*56bb7041Schristos 
117*56bb7041Schristos   // Add an assertion.
118*56bb7041Schristos   void
119*56bb7041Schristos   add_assertion(Expression* check, const char* message, size_t messagelen);
120*56bb7041Schristos 
121*56bb7041Schristos   // Add a setting for the fill value.
122*56bb7041Schristos   void
123*56bb7041Schristos   add_fill(Expression* val);
124*56bb7041Schristos 
125*56bb7041Schristos   // Add an input section specification.
126*56bb7041Schristos   void
127*56bb7041Schristos   add_input_section(const Input_section_spec* spec, bool keep);
128*56bb7041Schristos 
129*56bb7041Schristos   // Saw DATA_SEGMENT_ALIGN.
130*56bb7041Schristos   void
131*56bb7041Schristos   data_segment_align();
132*56bb7041Schristos 
133*56bb7041Schristos   // Saw DATA_SEGMENT_RELRO_END.
134*56bb7041Schristos   void
135*56bb7041Schristos   data_segment_relro_end();
136*56bb7041Schristos 
137*56bb7041Schristos   // Create any required sections.
138*56bb7041Schristos   void
139*56bb7041Schristos   create_sections(Layout*);
140*56bb7041Schristos 
141*56bb7041Schristos   // Add any symbols we are defining to the symbol table.
142*56bb7041Schristos   void
143*56bb7041Schristos   add_symbols_to_table(Symbol_table*);
144*56bb7041Schristos 
145*56bb7041Schristos   // Finalize symbol values and check assertions.
146*56bb7041Schristos   void
147*56bb7041Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout);
148*56bb7041Schristos 
149*56bb7041Schristos   // Find the name of the output section to use for an input file name
150*56bb7041Schristos   // and section name.  This returns a name, and sets
151*56bb7041Schristos   // *OUTPUT_SECTION_SLOT to point to the address where the actual
152*56bb7041Schristos   // output section may be stored.
153*56bb7041Schristos   // 1) If the input section should be discarded, this returns NULL
154*56bb7041Schristos   //    and sets *OUTPUT_SECTION_SLOT to NULL.
155*56bb7041Schristos   // 2) If the input section is mapped by the SECTIONS clause, this
156*56bb7041Schristos   //    returns the name to use for the output section (in permanent
157*56bb7041Schristos   //    storage), and sets *OUTPUT_SECTION_SLOT to point to where the
158*56bb7041Schristos   //    output section should be stored.  **OUTPUT_SECTION_SLOT will be
159*56bb7041Schristos   //    non-NULL if we have seen this output section already.
160*56bb7041Schristos   // 3) If the input section is not mapped by the SECTIONS clause,
161*56bb7041Schristos   //    this returns SECTION_NAME, and sets *OUTPUT_SECTION_SLOT to
162*56bb7041Schristos   //    NULL.
163*56bb7041Schristos   // PSCRIPT_SECTION_TYPE points to a location for returning the section
164*56bb7041Schristos   // type specified in script.  This can be SCRIPT_SECTION_TYPE_NONE if
165*56bb7041Schristos   // no type is specified.
166*56bb7041Schristos   // *KEEP indicates whether the section should survive garbage collection.
167*56bb7041Schristos   // MATCH_INPUT_SPEC indicates whether the section should be matched
168*56bb7041Schristos   // with input section specs or simply against the output section name
169*56bb7041Schristos   // (i.e., for linker-created sections like .dynamic).
170*56bb7041Schristos   const char*
171*56bb7041Schristos   output_section_name(const char* file_name, const char* section_name,
172*56bb7041Schristos 		      Output_section*** output_section_slot,
173*56bb7041Schristos 		      Section_type* pscript_section_type,
174*56bb7041Schristos 		      bool* keep, bool match_input_spec);
175*56bb7041Schristos 
176*56bb7041Schristos   // Place a marker for an orphan output section into the SECTIONS
177*56bb7041Schristos   // clause.
178*56bb7041Schristos   void
179*56bb7041Schristos   place_orphan(Output_section* os);
180*56bb7041Schristos 
181*56bb7041Schristos   // Set the addresses of all the output sections.  Return the segment
182*56bb7041Schristos   // which holds the file header and segment headers, if any.
183*56bb7041Schristos   Output_segment*
184*56bb7041Schristos   set_section_addresses(Symbol_table*, Layout*);
185*56bb7041Schristos 
186*56bb7041Schristos   // Add a program header definition.
187*56bb7041Schristos   void
188*56bb7041Schristos   add_phdr(const char* name, size_t namelen, unsigned int type,
189*56bb7041Schristos 	   bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
190*56bb7041Schristos 	   Expression* load_address);
191*56bb7041Schristos 
192*56bb7041Schristos   // Return the number of segments we expect to create based on the
193*56bb7041Schristos   // SECTIONS clause.
194*56bb7041Schristos   size_t
195*56bb7041Schristos   expected_segment_count(const Layout*) const;
196*56bb7041Schristos 
197*56bb7041Schristos   // Add the file header and segment header to non-load segments as
198*56bb7041Schristos   // specified by the PHDRS clause.
199*56bb7041Schristos   void
200*56bb7041Schristos   put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
201*56bb7041Schristos 
202*56bb7041Schristos   // Look for an output section by name and return the address, the
203*56bb7041Schristos   // load address, the alignment, and the size.  This is used when an
204*56bb7041Schristos   // expression refers to an output section which was not actually
205*56bb7041Schristos   // created.  This returns true if the section was found, false
206*56bb7041Schristos   // otherwise.
207*56bb7041Schristos   bool
208*56bb7041Schristos   get_output_section_info(const char* name, uint64_t* address,
209*56bb7041Schristos                           uint64_t* load_address, uint64_t* addralign,
210*56bb7041Schristos                           uint64_t* size) const;
211*56bb7041Schristos 
212*56bb7041Schristos   // Release all Output_segments.  This is used in relaxation.
213*56bb7041Schristos   void
214*56bb7041Schristos   release_segments();
215*56bb7041Schristos 
216*56bb7041Schristos   // Whether we ever saw a SEGMENT_START expression, the presence of which
217*56bb7041Schristos   // changes the behaviour of -Ttext, -Tdata and -Tbss options.
218*56bb7041Schristos   bool
saw_segment_start_expression()219*56bb7041Schristos   saw_segment_start_expression() const
220*56bb7041Schristos   { return this->saw_segment_start_expression_; }
221*56bb7041Schristos 
222*56bb7041Schristos   // Set the flag which indicates whether we saw a SEGMENT_START expression.
223*56bb7041Schristos   void
set_saw_segment_start_expression(bool value)224*56bb7041Schristos   set_saw_segment_start_expression(bool value)
225*56bb7041Schristos   { this->saw_segment_start_expression_ = value; }
226*56bb7041Schristos 
227*56bb7041Schristos   // Add a memory region.
228*56bb7041Schristos   void
229*56bb7041Schristos   add_memory_region(const char*, size_t, unsigned int,
230*56bb7041Schristos 		    Expression*, Expression*);
231*56bb7041Schristos 
232*56bb7041Schristos   // Find a memory region's origin.
233*56bb7041Schristos   Expression*
234*56bb7041Schristos   find_memory_region_origin(const char*, size_t);
235*56bb7041Schristos 
236*56bb7041Schristos   // Find a memory region's length.
237*56bb7041Schristos   Expression*
238*56bb7041Schristos   find_memory_region_length(const char*, size_t);
239*56bb7041Schristos 
240*56bb7041Schristos   // Find a memory region by name.
241*56bb7041Schristos   Memory_region*
242*56bb7041Schristos   find_memory_region(const char*, size_t);
243*56bb7041Schristos 
244*56bb7041Schristos   // Find a memory region that should be used by a given output section.
245*56bb7041Schristos   Memory_region*
246*56bb7041Schristos   find_memory_region(Output_section_definition*, bool, bool,
247*56bb7041Schristos 		     Output_section_definition**);
248*56bb7041Schristos 
249*56bb7041Schristos   // Returns true if the provide block of memory is contained
250*56bb7041Schristos   // within a memory region.
251*56bb7041Schristos   bool
252*56bb7041Schristos   block_in_region(Symbol_table*, Layout*, uint64_t, uint64_t) const;
253*56bb7041Schristos 
254*56bb7041Schristos   // Set the memory region of the section.
255*56bb7041Schristos   void
256*56bb7041Schristos   set_memory_region(Memory_region*, bool);
257*56bb7041Schristos 
258*56bb7041Schristos   // Print the contents to the FILE.  This is for debugging.
259*56bb7041Schristos   void
260*56bb7041Schristos   print(FILE*) const;
261*56bb7041Schristos 
262*56bb7041Schristos   // Used for orphan sections.
263*56bb7041Schristos   typedef Sections_elements::iterator Elements_iterator;
264*56bb7041Schristos 
265*56bb7041Schristos  private:
266*56bb7041Schristos   typedef std::vector<Memory_region*> Memory_regions;
267*56bb7041Schristos   typedef std::vector<Phdrs_element*> Phdrs_elements;
268*56bb7041Schristos 
269*56bb7041Schristos   // Create segments.
270*56bb7041Schristos   Output_segment*
271*56bb7041Schristos   create_segments(Layout*, uint64_t);
272*56bb7041Schristos 
273*56bb7041Schristos   // Create PT_NOTE and PT_TLS segments.
274*56bb7041Schristos   void
275*56bb7041Schristos   create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
276*56bb7041Schristos 
277*56bb7041Schristos   // Return whether the section is a BSS section.
278*56bb7041Schristos   static bool
279*56bb7041Schristos   is_bss_section(const Output_section*);
280*56bb7041Schristos 
281*56bb7041Schristos   // Return the total size of the headers.
282*56bb7041Schristos   size_t
283*56bb7041Schristos   total_header_size(Layout* layout) const;
284*56bb7041Schristos 
285*56bb7041Schristos   // Return the amount we have to subtract from the LMA to accommodate
286*56bb7041Schristos   // headers of the given size.
287*56bb7041Schristos   uint64_t
288*56bb7041Schristos   header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
289*56bb7041Schristos 
290*56bb7041Schristos   // Create the segments from a PHDRS clause.
291*56bb7041Schristos   Output_segment*
292*56bb7041Schristos   create_segments_from_phdrs_clause(Layout* layout, uint64_t);
293*56bb7041Schristos 
294*56bb7041Schristos   // Attach sections to segments from a PHDRS clause.
295*56bb7041Schristos   void
296*56bb7041Schristos   attach_sections_using_phdrs_clause(Layout*);
297*56bb7041Schristos 
298*56bb7041Schristos   // Set addresses of segments from a PHDRS clause.
299*56bb7041Schristos   Output_segment*
300*56bb7041Schristos   set_phdrs_clause_addresses(Layout*, uint64_t);
301*56bb7041Schristos 
302*56bb7041Schristos   // True if we ever saw a SECTIONS clause.
303*56bb7041Schristos   bool saw_sections_clause_;
304*56bb7041Schristos   // True if we are currently processing a SECTIONS clause.
305*56bb7041Schristos   bool in_sections_clause_;
306*56bb7041Schristos   // The list of elements in the SECTIONS clause.
307*56bb7041Schristos   Sections_elements* sections_elements_;
308*56bb7041Schristos   // The current output section, if there is one.
309*56bb7041Schristos   Output_section_definition* output_section_;
310*56bb7041Schristos   // The list of memory regions in the MEMORY clause.
311*56bb7041Schristos   Memory_regions* memory_regions_;
312*56bb7041Schristos   // The list of program headers in the PHDRS clause.
313*56bb7041Schristos   Phdrs_elements* phdrs_elements_;
314*56bb7041Schristos   // Where to put orphan sections.
315*56bb7041Schristos   Orphan_section_placement* orphan_section_placement_;
316*56bb7041Schristos   // A pointer to the last Sections_element when we see
317*56bb7041Schristos   // DATA_SEGMENT_ALIGN.
318*56bb7041Schristos   Sections_elements::iterator data_segment_align_start_;
319*56bb7041Schristos   // Whether we have seen DATA_SEGMENT_ALIGN.
320*56bb7041Schristos   bool saw_data_segment_align_;
321*56bb7041Schristos   // Whether we have seen DATA_SEGMENT_RELRO_END.
322*56bb7041Schristos   bool saw_relro_end_;
323*56bb7041Schristos   // Whether we have seen SEGMENT_START.
324*56bb7041Schristos   bool saw_segment_start_expression_;
325*56bb7041Schristos   // Whether we have created all necessary segments.
326*56bb7041Schristos   bool segments_created_;
327*56bb7041Schristos };
328*56bb7041Schristos 
329*56bb7041Schristos // Attributes for memory regions.
330*56bb7041Schristos enum
331*56bb7041Schristos {
332*56bb7041Schristos   MEM_EXECUTABLE   = (1 << 0),
333*56bb7041Schristos   MEM_WRITEABLE    = (1 << 1),
334*56bb7041Schristos   MEM_READABLE     = (1 << 2),
335*56bb7041Schristos   MEM_ALLOCATABLE  = (1 << 3),
336*56bb7041Schristos   MEM_INITIALIZED  = (1 << 4),
337*56bb7041Schristos   MEM_ATTR_MASK    = (1 << 5) - 1
338*56bb7041Schristos };
339*56bb7041Schristos 
340*56bb7041Schristos } // End namespace gold.
341*56bb7041Schristos 
342*56bb7041Schristos #endif // !defined(GOLD_SCRIPT_SECTIONS_H
343