script-sections.h 11 KB

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