aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2023-09-05 09:06:28 +0200
committerlemon <lsof@mailbox.org>2023-09-05 09:09:43 +0200
commit70adc3e3e8560eb76d5eb6b93a1f88be7fba549c (patch)
tree190c240d395c799f16b285c837d35f8af05fb1dd
parent1e3093b6208d33c6cdbb355078fc7b6d66f7f9d1 (diff)
make ptr offset in init take obj size into account
-rw-r--r--c.c7
-rw-r--r--test/init.c2
2 files changed, 5 insertions, 4 deletions
diff --git a/c.c b/c.c
index 3f2bdc5..daedde4 100644
--- a/c.c
+++ b/c.c
@@ -1114,11 +1114,12 @@ expr2reloc(union ref *psym, vlong *paddend, const struct expr *ex)
} else if (globsym(psym, ex) && in_range(ex->ty.t, TYARRAY, TYFUNC)) {
*paddend = 0;
} else if (ex->t == ESUB && globsym(psym, &ex->sub[0]) && isint(ex->sub[1].ty) && ex->sub[1].t == ENUMLIT) {
- *paddend = ex->sub[1].i;
+ *paddend = ex->sub[1].i * typesize(ex->sub[0].ty);
} else if (ex->t == EADD) {
for (int swp = 0; swp < 2; ++swp) {
- if (globsym(psym, &ex->sub[swp]) && isint(ex->sub[swp^1].ty) && ex->sub[swp^1].t == ENUMLIT) {
- *paddend = ex->sub[swp^1].i;
+ struct expr *a = &ex->sub[swp], *b = &ex->sub[swp ^ 1];
+ if (globsym(psym, a) && isint(b->ty) && b->t == ENUMLIT) {
+ *paddend = b->i * typesize(a->ty);
return;
}
}
diff --git a/test/init.c b/test/init.c
index 24d1b7f..4087cd9 100644
--- a/test/init.c
+++ b/test/init.c
@@ -41,7 +41,7 @@ void f() {
}
*/
-void *rec[1] = {rec};
+void *rec[1] = {rec+1};
int printf(char *, ...);
int main() {