script-sections.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // script-sections.h -- linker script SECTIONS for gold -*- C++ -*-
  2. // Copyright (C) 2008-2015 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. const char*
  136. output_section_name(const char* file_name, const char* section_name,
  137. Output_section*** output_section_slot,
  138. Section_type* pscript_section_type,
  139. bool* keep);
  140. // Place a marker for an orphan output section into the SECTIONS
  141. // clause.
  142. void
  143. place_orphan(Output_section* os);
  144. // Set the addresses of all the output sections. Return the segment
  145. // which holds the file header and segment headers, if any.
  146. Output_segment*
  147. set_section_addresses(Symbol_table*, Layout*);
  148. // Add a program header definition.
  149. void
  150. add_phdr(const char* name, size_t namelen, unsigned int type,
  151. bool filehdr, bool phdrs, bool is_flags_valid, unsigned int flags,
  152. Expression* load_address);
  153. // Return the number of segments we expect to create based on the
  154. // SECTIONS clause.
  155. size_t
  156. expected_segment_count(const Layout*) const;
  157. // Add the file header and segment header to non-load segments as
  158. // specified by the PHDRS clause.
  159. void
  160. put_headers_in_phdrs(Output_data* file_header, Output_data* segment_headers);
  161. // Look for an output section by name and return the address, the
  162. // load address, the alignment, and the size. This is used when an
  163. // expression refers to an output section which was not actually
  164. // created. This returns true if the section was found, false
  165. // otherwise.
  166. bool
  167. get_output_section_info(const char* name, uint64_t* address,
  168. uint64_t* load_address, uint64_t* addralign,
  169. uint64_t* size) const;
  170. // Release all Output_segments. This is used in relaxation.
  171. void
  172. release_segments();
  173. // Whether we ever saw a SEGMENT_START expression, the presence of which
  174. // changes the behaviour of -Ttext, -Tdata and -Tbss options.
  175. bool
  176. saw_segment_start_expression() const
  177. { return this->saw_segment_start_expression_; }
  178. // Set the flag which indicates whether we saw a SEGMENT_START expression.
  179. void
  180. set_saw_segment_start_expression(bool value)
  181. { this->saw_segment_start_expression_ = value; }
  182. // Add a memory region.
  183. void
  184. add_memory_region(const char*, size_t, unsigned int,
  185. Expression*, Expression*);
  186. // Find a memory region's origin.
  187. Expression*
  188. find_memory_region_origin(const char*, size_t);
  189. // Find a memory region's length.
  190. Expression*
  191. find_memory_region_length(const char*, size_t);
  192. // Find a memory region by name.
  193. Memory_region*
  194. find_memory_region(const char*, size_t);
  195. // Find a memory region that should be used by a given output section.
  196. Memory_region*
  197. find_memory_region(Output_section_definition*, bool, bool,
  198. Output_section_definition**);
  199. // Returns true if the provide block of memory is contained
  200. // within a memory region.
  201. bool
  202. block_in_region(Symbol_table*, Layout*, uint64_t, uint64_t) const;
  203. // Set the memory region of the section.
  204. void
  205. set_memory_region(Memory_region*, bool);
  206. // Print the contents to the FILE. This is for debugging.
  207. void
  208. print(FILE*) const;
  209. // Used for orphan sections.
  210. typedef Sections_elements::iterator Elements_iterator;
  211. private:
  212. typedef std::vector<Memory_region*> Memory_regions;
  213. typedef std::vector<Phdrs_element*> Phdrs_elements;
  214. // Create segments.
  215. Output_segment*
  216. create_segments(Layout*, uint64_t);
  217. // Create PT_NOTE and PT_TLS segments.
  218. void
  219. create_note_and_tls_segments(Layout*, const std::vector<Output_section*>*);
  220. // Return whether the section is a BSS section.
  221. static bool
  222. is_bss_section(const Output_section*);
  223. // Return the total size of the headers.
  224. size_t
  225. total_header_size(Layout* layout) const;
  226. // Return the amount we have to subtract from the LMA to accomodate
  227. // headers of the given size.
  228. uint64_t
  229. header_size_adjustment(uint64_t lma, size_t sizeof_headers) const;
  230. // Create the segments from a PHDRS clause.
  231. Output_segment*
  232. create_segments_from_phdrs_clause(Layout* layout, uint64_t);
  233. // Attach sections to segments from a PHDRS clause.
  234. void
  235. attach_sections_using_phdrs_clause(Layout*);
  236. // Set addresses of segments from a PHDRS clause.
  237. Output_segment*
  238. set_phdrs_clause_addresses(Layout*, uint64_t);
  239. // True if we ever saw a SECTIONS clause.
  240. bool saw_sections_clause_;
  241. // True if we are currently processing a SECTIONS clause.
  242. bool in_sections_clause_;
  243. // The list of elements in the SECTIONS clause.
  244. Sections_elements* sections_elements_;
  245. // The current output section, if there is one.
  246. Output_section_definition* output_section_;
  247. // The list of memory regions in the MEMORY clause.
  248. Memory_regions* memory_regions_;
  249. // The list of program headers in the PHDRS clause.
  250. Phdrs_elements* phdrs_elements_;
  251. // Where to put orphan sections.
  252. Orphan_section_placement* orphan_section_placement_;
  253. // A pointer to the last Sections_element when we see
  254. // DATA_SEGMENT_ALIGN.
  255. Sections_elements::iterator data_segment_align_start_;
  256. // Whether we have seen DATA_SEGMENT_ALIGN.
  257. bool saw_data_segment_align_;
  258. // Whether we have seen DATA_SEGMENT_RELRO_END.
  259. bool saw_relro_end_;
  260. // Whether we have seen SEGMENT_START.
  261. bool saw_segment_start_expression_;
  262. // Whether we have created all necessary segments.
  263. bool segments_created_;
  264. };
  265. // Attributes for memory regions.
  266. enum
  267. {
  268. MEM_EXECUTABLE = (1 << 0),
  269. MEM_WRITEABLE = (1 << 1),
  270. MEM_READABLE = (1 << 2),
  271. MEM_ALLOCATABLE = (1 << 3),
  272. MEM_INITIALIZED = (1 << 4),
  273. MEM_ATTR_MASK = (1 << 5) - 1
  274. };
  275. } // End namespace gold.
  276. #endif // !defined(GOLD_SCRIPT_SECTIONS_H