ldexp.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* ldexp.h -
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #ifndef LDEXP_H
  17. #define LDEXP_H
  18. /* The result of an expression tree */
  19. typedef struct {
  20. bfd_vma value;
  21. char *str;
  22. asection *section;
  23. bool valid_p;
  24. } etree_value_type;
  25. enum node_tree_enum {
  26. etree_binary,
  27. etree_trinary,
  28. etree_unary,
  29. etree_name,
  30. etree_assign,
  31. etree_provide,
  32. etree_provided,
  33. etree_value,
  34. etree_assert,
  35. etree_rel
  36. };
  37. typedef struct {
  38. int node_code;
  39. unsigned int lineno;
  40. const char *filename;
  41. enum node_tree_enum node_class;
  42. } node_type;
  43. typedef union etree_union {
  44. node_type type;
  45. struct {
  46. node_type type;
  47. union etree_union *lhs;
  48. union etree_union *rhs;
  49. } binary;
  50. struct {
  51. node_type type;
  52. union etree_union *cond;
  53. union etree_union *lhs;
  54. union etree_union *rhs;
  55. } trinary;
  56. struct {
  57. node_type type;
  58. const char *dst;
  59. union etree_union *src;
  60. bool hidden;
  61. } assign;
  62. struct {
  63. node_type type;
  64. union etree_union *child;
  65. } unary;
  66. struct {
  67. node_type type;
  68. const char *name;
  69. } name;
  70. struct {
  71. node_type type;
  72. bfd_vma value;
  73. char *str;
  74. } value;
  75. struct {
  76. node_type type;
  77. asection *section;
  78. bfd_vma value;
  79. } rel;
  80. struct {
  81. node_type type;
  82. union etree_union *child;
  83. const char *message;
  84. } assert_s;
  85. } etree_type;
  86. /* Expression evaluation control. */
  87. typedef enum
  88. {
  89. /* Parsing linker script. Will only return "valid" for expressions
  90. that evaluate to a constant. */
  91. lang_first_phase_enum,
  92. /* Prior to section sizing. */
  93. lang_mark_phase_enum,
  94. /* During section sizing. */
  95. lang_allocating_phase_enum,
  96. /* During assignment of symbol values when relaxation in progress. */
  97. lang_assigning_phase_enum,
  98. /* Final assignment of symbol values. */
  99. lang_final_phase_enum,
  100. /* Run after symbol values have been fixed, for lang_map. */
  101. lang_fixed_phase_enum
  102. } lang_phase_type;
  103. union lang_statement_union;
  104. enum phase_enum {
  105. /* We step through the first four states here as we see the
  106. associated linker script tokens. */
  107. exp_seg_none,
  108. exp_seg_align_seen,
  109. exp_seg_relro_seen,
  110. exp_seg_end_seen,
  111. /* The last three states are final, and affect the value returned
  112. by XXX_SEGMENT_ALIGN. */
  113. exp_seg_relro_adjust,
  114. exp_seg_adjust,
  115. exp_seg_done
  116. };
  117. enum relro_enum {
  118. exp_seg_relro_none,
  119. exp_seg_relro_start,
  120. exp_seg_relro_end,
  121. };
  122. typedef struct {
  123. enum phase_enum phase;
  124. bfd_vma base, relro_offset, relro_end, end;
  125. /* MAXPAGESIZE and COMMMONPAGESIZE as passed to DATA_SEGMENT_ALIGN.
  126. relropagesize sets the alignment of the end of the relro segment. */
  127. bfd_vma maxpagesize, commonpagesize, relropagesize;
  128. enum relro_enum relro;
  129. union lang_statement_union *relro_start_stat;
  130. union lang_statement_union *relro_end_stat;
  131. } seg_align_type;
  132. struct ldexp_control {
  133. /* Modify expression evaluation depending on this. */
  134. lang_phase_type phase;
  135. /* Principally used for diagnostics. */
  136. bool assigning_to_dot;
  137. /* Set if the current expression used "dot", SEGMENT_START or
  138. ORIGIN, but not ABSOLUTE or combined symbols in a way that forces
  139. an absolute result. Used in tracking symbols assigned from dot
  140. outside of output section statements, in order to later convert
  141. them from absolute. */
  142. bool rel_from_abs;
  143. /* If evaluating an assignment, the destination. Cleared if an
  144. etree_name NAME matches this, to signal a self-assignment.
  145. Note that an etree_name DEFINED does not clear this field, nor
  146. does the false branch of a trinary expression. */
  147. const char *assign_name;
  148. /* If evaluating an assignment, the source if it is an expression
  149. referencing single etree_name NAME, or a trinary expression where
  150. the true branch references a single etree_name NAME. */
  151. struct bfd_link_hash_entry *assign_src;
  152. /* Working results. */
  153. etree_value_type result;
  154. bfd_vma dot;
  155. /* Current dot and section passed to ldexp folder. */
  156. bfd_vma *dotp;
  157. asection *section;
  158. /* State machine and results for DATASEG. */
  159. seg_align_type dataseg;
  160. };
  161. extern struct ldexp_control expld;
  162. /* A maps from a segment name to a base address. */
  163. typedef struct segment_struct {
  164. /* The next segment in the linked list. */
  165. struct segment_struct *next;
  166. /* The name of the sgement. */
  167. const char *name;
  168. /* The base address for the segment. */
  169. bfd_vma value;
  170. /* True if a SEGMENT_START directive corresponding to this segment
  171. has been seen. */
  172. bool used;
  173. } segment_type;
  174. /* The segments specified by the user on the command-line. */
  175. extern segment_type *segments;
  176. typedef struct _fill_type fill_type;
  177. etree_type *exp_intop
  178. (bfd_vma);
  179. etree_type *exp_bigintop
  180. (bfd_vma, char *);
  181. etree_type *exp_relop
  182. (asection *, bfd_vma);
  183. void exp_fold_tree
  184. (etree_type *, asection *, bfd_vma *);
  185. void exp_fold_tree_no_dot
  186. (etree_type *);
  187. etree_type *exp_binop
  188. (int, etree_type *, etree_type *);
  189. etree_type *exp_trinop
  190. (int,etree_type *, etree_type *, etree_type *);
  191. etree_type *exp_unop
  192. (int, etree_type *);
  193. etree_type *exp_nameop
  194. (int, const char *);
  195. etree_type *exp_assign
  196. (const char *, etree_type *, bool);
  197. etree_type *exp_defsym
  198. (const char *, etree_type *);
  199. etree_type *exp_provide
  200. (const char *, etree_type *, bool);
  201. etree_type *exp_assert
  202. (etree_type *, const char *);
  203. void exp_print_tree
  204. (etree_type *);
  205. bfd_vma exp_get_vma
  206. (etree_type *, bfd_vma, char *);
  207. int exp_get_power
  208. (etree_type *, char *);
  209. fill_type *exp_get_fill
  210. (etree_type *, fill_type *, char *);
  211. bfd_vma exp_get_abs_int
  212. (etree_type *, int, char *);
  213. void ldexp_init (void);
  214. void ldexp_finalize_syms (void);
  215. bool ldexp_is_final_sym_absolute (const struct bfd_link_hash_entry *);
  216. void ldexp_finish (void);
  217. #endif