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