From 5d68766b983213a8750d59cc24dc844c6170626b Mon Sep 17 00:00:00 2001 From: lemon Date: Thu, 19 Mar 2026 19:26:08 +0100 Subject: ir: do not always try to put small literals in .text OpenBSD enforces read xor execute (XOM) even in x86-64. Not aware of any other platforms that do this. --- src/c.c | 6 +++--- src/ir.c | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/c.c b/src/c.c index 75937f3..92f917e 100644 --- a/src/c.c +++ b/src/c.c @@ -1125,9 +1125,9 @@ Unary: internstr fnname = decl->sym; decl->isbuiltin = 0; decl->sym = mkhiddensym(&fnname->c, "__func__", 1); - uint off = objnewdat(decl->sym, objout.code ? Stext : Srodata, 0, typesize(decl->ty), typealign(decl->ty)); - uchar *p = objout.code ? objout.textbegin + off : objout.rodata.p + off; - memcpy(p, fnname, typearrlen(decl->ty)-1); + assert(decl->ty.t == TYARRAY && typechild(decl->ty).t == TYCHAR); + uint siz = typesize(decl->ty); + (void) mkdatref(decl->sym, decl->ty, siz, 1, fnname, siz, 0, 1); } ex = mkexpr(ESYM, tk.span, decl->ty, .qual = decl->qual, .decl = decl - declsbuf.p); } diff --git a/src/ir.c b/src/ir.c index b7dbb2b..ce36e64 100644 --- a/src/ir.c +++ b/src/ir.c @@ -159,12 +159,21 @@ mksymref(internstr s, enum symflags symflags) return newxcon(&con); } +static bool +textdataok(void) +{ + /* openbsd enforces R^X for .text */ + return target.os != OSopenbsd; +} + Ref mkdatref(internstr name, Type ctype, uint siz, uint align, const void *bytes, uint n, bool deref, bool funclocal) { IRDat dat = { .ctype = ctype, .align = align, .siz = siz, .name = name, .section = Srodata }; - if (funclocal && objout.code && align >= 4 && align <= targ_primsizes[TYPTR] && siz <= 16) + + if (funclocal && textdataok() && objout.code + && align >= 4 && align <= targ_primsizes[TYPTR] && siz <= 16) dat.section = Stext; assert(n <= siz && siz && align); -- cgit v1.2.3