1 From 3d9c8f6b3f033a6092425b7344647fb51dbed5c6 Mon Sep 17 00:00:00 2001
2 From: Alan Modra <amodra@gmail.com>
3 Date: Sun, 26 Aug 2018 14:23:38 +0930
4 Subject: [PATCH] Delay evaluation of alignment expressions in output sections
6 git commit 702d16713 broke expressions using CONSTANT(COMMONPAGESIZE)
7 in ALIGN or SUBALIGN of output section statements, because these
8 optional fields were evaluated at script parse time and the patch in
9 question delayed setting of config.commonpagesize. The right thing to
10 do is keep the tree representation of those fields for later
14 * ldlang.h (section_alignment): Make it an expression tree.
15 (subsection_alignment): Likewise.
16 * ldlang.c (topower): Delete.
17 (output_section_statement_newfunc): Adjust initialization.
18 (init_os): Evaluate section_alignment.
19 (lang_size_sections_1): Likewise.
20 (size_input_section): Evaluate subsection_alignment.
21 (lang_enter_output_section_statement): Don't evaluate here.
22 (lang_new_phdr): Use exp_get_vma rather than exp_get_value_int.
23 * ldexp.h (exp_get_value_int): Delete.
24 (exp_get_power): Declare.
25 * ldexp.c (exp_get_value_int): Delete.
26 (exp_get_power): New function.
27 * emultempl/pe.em (place_orphan): Build expression for section
29 * emultempl/pep.em (place_orphan): Likewise.
30 * testsuite/ld-scripts/pr23571.d,
31 * testsuite/ld-scripts/pr23571.t: New test.
32 * testsuite/ld-scripts/align.exp: Run it.
34 ld/ChangeLog | 23 +++++++++++++++++++
35 ld/emultempl/pe.em | 2 +-
36 ld/emultempl/pep.em | 2 +-
37 ld/ldexp.c | 20 ++++++++++++++--
39 ld/ldlang.c | 48 +++++++++++++--------------------------
41 ld/testsuite/ld-scripts/align.exp | 4 ++++
42 ld/testsuite/ld-scripts/pr23571.d | 10 ++++++++
43 ld/testsuite/ld-scripts/pr23571.t | 11 +++++++++
44 10 files changed, 88 insertions(+), 40 deletions(-)
45 create mode 100644 ld/testsuite/ld-scripts/pr23571.d
46 create mode 100644 ld/testsuite/ld-scripts/pr23571.t
48 --- a/ld/emultempl/pe.em
49 +++ b/ld/emultempl/pe.em
50 @@ -2165,7 +2165,7 @@ gld_${EMULATION_NAME}_place_orphan (asec
52 if (bfd_link_relocatable (&link_info))
54 - os->section_alignment = s->alignment_power;
55 + os->section_alignment = exp_intop (1U << s->alignment_power);
56 os->bfd_section->alignment_power = s->alignment_power;
59 --- a/ld/emultempl/pep.em
60 +++ b/ld/emultempl/pep.em
61 @@ -1962,7 +1962,7 @@ gld_${EMULATION_NAME}_place_orphan (asec
63 if (bfd_link_relocatable (&link_info))
65 - os->section_alignment = s->alignment_power;
66 + os->section_alignment = exp_intop (1U << s->alignment_power);
67 os->bfd_section->alignment_power = s->alignment_power;
72 @@ -1522,10 +1522,26 @@ exp_get_vma (etree_type *tree, bfd_vma d
76 +/* Return the smallest non-negative integer such that two raised to
77 + that power is at least as large as the vma evaluated at TREE, if
78 + TREE is a non-NULL expression that can be resolved. If TREE is
79 + NULL or cannot be resolved, return -1. */
82 -exp_get_value_int (etree_type *tree, int def, char *name)
83 +exp_get_power (etree_type *tree, char *name)
85 - return exp_get_vma (tree, def, name);
86 + bfd_vma x = exp_get_vma (tree, -1, name);
90 + if (x == (bfd_vma) -1)
93 + for (n = 0, p2 = 1; p2 < x; ++n, p2 <<= 1)
103 @@ -229,8 +229,8 @@ void exp_print_tree
106 (etree_type *, bfd_vma, char *);
107 -int exp_get_value_int
108 - (etree_type *, int, char *);
110 + (etree_type *, char *);
111 fill_type *exp_get_fill
112 (etree_type *, fill_type *, char *);
113 bfd_vma exp_get_abs_int
116 @@ -1199,8 +1199,8 @@ output_section_statement_newfunc (struct
117 ret = (struct out_section_hash_entry *) entry;
118 memset (&ret->s, 0, sizeof (ret->s));
119 ret->s.header.type = lang_output_section_statement_enum;
120 - ret->s.output_section_statement.subsection_alignment = -1;
121 - ret->s.output_section_statement.section_alignment = -1;
122 + ret->s.output_section_statement.subsection_alignment = NULL;
123 + ret->s.output_section_statement.section_alignment = NULL;
124 ret->s.output_section_statement.block_value = 1;
125 lang_list_init (&ret->s.output_section_statement.children);
126 lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
127 @@ -2193,8 +2193,9 @@ init_os (lang_output_section_statement_t
128 exp_init_os (s->load_base);
130 /* If supplied an alignment, set it. */
131 - if (s->section_alignment != -1)
132 - s->bfd_section->alignment_power = s->section_alignment;
133 + if (s->section_alignment != NULL)
134 + s->bfd_section->alignment_power = exp_get_power (s->section_alignment,
135 + "section alignment");
138 /* Make sure that all output sections mentioned in an expression are
139 @@ -4706,8 +4707,10 @@ size_input_section
140 is greater than any seen before, then record it too. Perform
141 the alignment by inserting a magic 'padding' statement. */
143 - if (output_section_statement->subsection_alignment != -1)
144 - i->alignment_power = output_section_statement->subsection_alignment;
145 + if (output_section_statement->subsection_alignment != NULL)
147 + = exp_get_power (output_section_statement->subsection_alignment,
148 + "subsection alignment");
150 if (o->alignment_power < i->alignment_power)
151 o->alignment_power = i->alignment_power;
152 @@ -5147,7 +5150,8 @@ lang_size_sections_1
153 section_alignment = os->bfd_section->alignment_power;
156 - section_alignment = os->section_alignment;
157 + section_alignment = exp_get_power (os->section_alignment,
158 + "section alignment");
160 /* Align to what the section needs. */
161 if (section_alignment > 0)
162 @@ -5225,7 +5229,8 @@ lang_size_sections_1
163 only align according to the value in the output
165 if (os->lma_region != os->region)
166 - section_alignment = os->section_alignment;
167 + section_alignment = exp_get_power (os->section_alignment,
168 + "section alignment");
169 if (section_alignment > 0)
170 lma = align_power (lma, section_alignment);
172 @@ -6673,25 +6678,6 @@ lang_add_output (const char *name, int f
179 - unsigned int i = 1;
185 - for (l = 0; l < 32; l++)
187 - if (i >= (unsigned int) x)
195 lang_output_section_statement_type *
196 lang_enter_output_section_statement (const char *output_section_statement_name,
197 etree_type *address_exp,
198 @@ -6727,10 +6713,8 @@ lang_enter_output_section_statement (con
199 einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
202 - os->subsection_alignment =
203 - topower (exp_get_value_int (subalign, -1, "subsection alignment"));
204 - os->section_alignment =
205 - topower (exp_get_value_int (align, -1, "section alignment"));
206 + os->subsection_alignment = subalign;
207 + os->section_alignment = align;
209 os->load_base = ebase;
211 @@ -7748,7 +7732,7 @@ lang_new_phdr (const char *name,
212 n = (struct lang_phdr *) stat_alloc (sizeof (struct lang_phdr));
215 - n->type = exp_get_value_int (type, 0, "program header type");
216 + n->type = exp_get_vma (type, 0, "program header type");
217 n->filehdr = filehdr;
222 @@ -143,6 +143,8 @@ typedef struct lang_output_section_state
224 union etree_union *addr_tree;
225 union etree_union *load_base;
226 + union etree_union *section_alignment;
227 + union etree_union *subsection_alignment;
229 /* If non-null, an expression to evaluate after setting the section's
230 size. The expression is evaluated inside REGION (above) with '.'
231 @@ -153,8 +155,6 @@ typedef struct lang_output_section_state
232 lang_output_section_phdr_list *phdrs;
234 unsigned int block_value;
235 - int subsection_alignment; /* Alignment of components. */
236 - int section_alignment; /* Alignment of start of section. */
239 enum section_type sectype;
240 --- a/ld/testsuite/ld-scripts/align.exp
241 +++ b/ld/testsuite/ld-scripts/align.exp
242 @@ -53,3 +53,7 @@ if ![is_aout_format] {
244 run_dump_test align2c
245 set LDFLAGS "$saved_LDFLAGS"
247 +if { [is_elf_format] && ![is_generic_elf] } {
248 + run_dump_test pr23571
251 +++ b/ld/testsuite/ld-scripts/pr23571.d
254 +#ld: -T pr23571.t -z common-page-size=0x1000
260 +Idx Name +Size +VMA +LMA +File off +Algn +Flags
261 + +0 \.text +[0-9a-f]* +0+1000 +0+1000 .*
262 + +1 \.data +[0-9a-f]* +0+2000 +0+2000 +[0-9a-f]* +2\*\*12 .*
264 +++ b/ld/testsuite/ld-scripts/pr23571.t
268 + .text CONSTANT(COMMONPAGESIZE) : {
272 + .data : ALIGN(CONSTANT(COMMONPAGESIZE)) {