diff options
| author | 2025-12-14 12:15:59 +0100 | |
|---|---|---|
| committer | 2025-12-14 12:15:59 +0100 | |
| commit | d8b4e87af669c2b260686a5db67f7f02b4c164d9 (patch) | |
| tree | c46f03cc43462953dfb82df935bb7e2adcb098e4 /obj/obj.c | |
| parent | 59ca5a8db396e3e9c395793d49e1cab05d2d3261 (diff) | |
various relocation related optimization
With 59ca5a8db, querying if a symbol is defined is cheap. If we're
compiling code that calls foo() and we defined foo() in this compilation
unit, we already know its offset within the .text section, so use it
instead of emitting a relocation for the linker to handle. Also, put
small literal data in the .text section instead of .rodata. This seems
to improve performance (cache locality?), and as a bonus, it will be
good for aarch64's instr encoding with smallish PC-relative offsets.
Diffstat (limited to 'obj/obj.c')
| -rw-r--r-- | obj/obj.c | 42 |
1 files changed, 24 insertions, 18 deletions
@@ -6,7 +6,7 @@ void elfinit(void); -enum section elfhassym(const char *); +enum section elfhassym(const char *, uint *value); void elfaddsym(const char *, int info, enum section, uvlong value, uvlong size); void elfreloc(const char *sym, enum relockind, enum section, uint off, vlong addend); void elffini(struct wbuf *); @@ -40,38 +40,44 @@ objdeffunc(const char *nam, bool globl, uint off, uint siz) } enum section -objhassym(const char *name) +objhassym(const char *name, uint *off) { - return elfhassym(name); + return elfhassym(name, off); } uint objnewdat(const char *name, enum section sec, bool globl, uint siz, uint align) { + struct objfile *o = &objout; uint off; - assert(siz && align && ispo2(align)); - switch (sec) { default: assert(0); + case Stext: + assert(align <= targ_primsizes[TYPTR]); + assert(o->textend - siz > o->code); + while ((o->code - o->textbegin) & (align - 1)) ++o->code; + off = o->code - o->textbegin; + o->code += siz; + break; case Srodata: - if (align > objout.rodataalign) objout.rodataalign = align; - while (objout.rodata.n & (align - 1)) vpush(&objout.rodata, 0); - off = objout.rodata.n; - vresize(&objout.rodata, objout.rodata.n + siz); - memset(objout.rodata.p+off, 0, siz); + if (align > o->rodataalign) o->rodataalign = align; + while (o->rodata.n & (align - 1)) vpush(&o->rodata, 0); + off = o->rodata.n; + vresize(&o->rodata, o->rodata.n + siz); + memset(o->rodata.p+off, 0, siz); break; case Sdata: - if (align > objout.dataalign) objout.dataalign = align; - while (objout.data.n & (align - 1)) vpush(&objout.data, 0); - off = objout.data.n; - vresize(&objout.data, objout.data.n + siz); - memset(objout.data.p+off, 0, siz); + if (align > o->dataalign) o->dataalign = align; + while (o->data.n & (align - 1)) vpush(&o->data, 0); + off = o->data.n; + vresize(&o->data, o->data.n + siz); + memset(o->data.p+off, 0, siz); break; case Sbss: - if (align > objout.bssalign) objout.bssalign = align; - off = alignup(objout.nbss, align); - objout.nbss = off + siz; + if (align > o->bssalign) o->bssalign = align; + off = alignup(o->nbss, align); + o->nbss = off + siz; break; } |