From 67d449c0a608ab6c9891bb9b16cc14016bde8848 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 4 Dec 2025 13:45:57 -0800 Subject: [PATCH] cmd/internal/obj: add Align field to LSym This will permit the compiler and assembler to specify the alignment of symbols that they create. Careful placement of the new field, plus rearranging an existing field, means that LSym does not change size (as tested by TestSizeof). The new alignment field is int16, permitting alignment up to 16384, more than we ever need in practice. If necessary we could change the field to uint8 and store the alignment as a power of two, as is done in the linker. This replaces the Align field in FuncInfo. For #6853 For #36313 Change-Id: I421e8238ab57958fea8e4eab0649ce5288e7f92f Reviewed-on: https://go-review.googlesource.com/c/go/+/727020 LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Auto-Submit: Ian Lance Taylor Reviewed-by: Cherry Mui Reviewed-by: Keith Randall --- src/cmd/internal/obj/link.go | 4 ++-- src/cmd/internal/obj/loong64/asm.go | 4 ++-- src/cmd/internal/obj/objfile.go | 11 ++++------- src/cmd/internal/obj/ppc64/asm9.go | 6 +++--- src/cmd/internal/obj/riscv/obj.go | 4 ++-- src/cmd/internal/obj/util.go | 4 ++-- 6 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/cmd/internal/obj/link.go b/src/cmd/internal/obj/link.go index 05ffba11ea..088a7c57aa 100644 --- a/src/cmd/internal/obj/link.go +++ b/src/cmd/internal/obj/link.go @@ -456,9 +456,10 @@ const ( // It represents Go symbols in a flat pkg+"."+name namespace. type LSym struct { Name string - Type objabi.SymKind Attribute + Type objabi.SymKind + Align int16 Size int64 Gotype *LSym P []byte @@ -475,7 +476,6 @@ type LSym struct { type FuncInfo struct { Args int32 Locals int32 - Align int32 FuncID abi.FuncID FuncFlag abi.FuncFlag StartLine int32 diff --git a/src/cmd/internal/obj/loong64/asm.go b/src/cmd/internal/obj/loong64/asm.go index f992518015..2cf608fb84 100644 --- a/src/cmd/internal/obj/loong64/asm.go +++ b/src/cmd/internal/obj/loong64/asm.go @@ -549,8 +549,8 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) { alignedValue := p.From.Offset m = pcAlignPadLength(ctxt, pc, alignedValue) // Update the current text symbol alignment value. - if int32(alignedValue) > cursym.Func().Align { - cursym.Func().Align = int32(alignedValue) + if int16(alignedValue) > cursym.Align { + cursym.Align = int16(alignedValue) } break case obj.ANOP, obj.AFUNCDATA, obj.APCDATA: diff --git a/src/cmd/internal/obj/objfile.go b/src/cmd/internal/obj/objfile.go index 4401f1bb74..53691ccb6c 100644 --- a/src/cmd/internal/obj/objfile.go +++ b/src/cmd/internal/obj/objfile.go @@ -369,11 +369,8 @@ func (w *writer) Sym(s *LSym) { if strings.HasPrefix(name, "gofile..") { name = filepath.ToSlash(name) } - var align uint32 - if fn := s.Func(); fn != nil { - align = uint32(fn.Align) - } - if s.ContentAddressable() && s.Size != 0 { + align := uint32(s.Align) + if s.ContentAddressable() && s.Size != 0 && align == 0 { // We generally assume data symbols are naturally aligned // (e.g. integer constants), except for strings and a few // compiler-emitted funcdata. If we dedup a string symbol and @@ -895,10 +892,10 @@ func (ctxt *Link) writeSymDebugNamed(s *LSym, name string) { if s.Func() != nil && s.Func().FuncFlag&abi.FuncFlagAsm != 0 { fmt.Fprintf(ctxt.Bso, "asm ") } - fmt.Fprintf(ctxt.Bso, "size=%d", s.Size) + fmt.Fprintf(ctxt.Bso, "size=%d align=%#x", s.Size, s.Align) if s.Type.IsText() { fn := s.Func() - fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x funcid=%#x align=%#x", uint64(fn.Args), uint64(fn.Locals), uint64(fn.FuncID), uint64(fn.Align)) + fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x funcid=%#x", uint64(fn.Args), uint64(fn.Locals), uint64(fn.FuncID)) if s.Leaf() { fmt.Fprintf(ctxt.Bso, " leaf") } diff --git a/src/cmd/internal/obj/ppc64/asm9.go b/src/cmd/internal/obj/ppc64/asm9.go index a39c206c22..06ad72eed8 100644 --- a/src/cmd/internal/obj/ppc64/asm9.go +++ b/src/cmd/internal/obj/ppc64/asm9.go @@ -595,8 +595,8 @@ func addpad(pc, a int64, ctxt *obj.Link, cursym *obj.LSym) int { // requested then the function alignment must also be promoted. // The function alignment is not promoted on AIX at this time. // TODO: Investigate AIX function alignment. - if ctxt.Headtype != objabi.Haix && cursym.Func().Align < int32(a) { - cursym.Func().Align = int32(a) + if ctxt.Headtype != objabi.Haix && cursym.Align < int16(a) { + cursym.Align = int16(a) } if pc&(a-1) != 0 { return int(a - (pc & (a - 1))) @@ -796,7 +796,7 @@ func span9(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) { } c.cursym.Size = pc - c.cursym.Func().Align = falign + c.cursym.Align = int16(falign) c.cursym.Grow(c.cursym.Size) // lay out the code, emitting code and data relocations. diff --git a/src/cmd/internal/obj/riscv/obj.go b/src/cmd/internal/obj/riscv/obj.go index 043be17c07..24e0a454cd 100644 --- a/src/cmd/internal/obj/riscv/obj.go +++ b/src/cmd/internal/obj/riscv/obj.go @@ -793,8 +793,8 @@ func preprocess(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) { ctxt.Diag("alignment value of an instruction must be a power of two and in the range [4, 2048], got %d\n", alignedValue) } // Update the current text symbol alignment value. - if int32(alignedValue) > cursym.Func().Align { - cursym.Func().Align = int32(alignedValue) + if int16(alignedValue) > cursym.Align { + cursym.Align = int16(alignedValue) } } } diff --git a/src/cmd/internal/obj/util.go b/src/cmd/internal/obj/util.go index 7d87bff949..86ba4deb73 100644 --- a/src/cmd/internal/obj/util.go +++ b/src/cmd/internal/obj/util.go @@ -737,7 +737,7 @@ func AlignmentPaddingLength(pc int32, p *Prog, ctxt *Link) int { // the required code alignment func requireAlignment(a int64, ctxt *Link, cursym *LSym) { // TODO remove explicit knowledge about AIX. - if ctxt.Headtype != objabi.Haix && cursym.Func().Align < int32(a) { - cursym.Func().Align = int32(a) + if ctxt.Headtype != objabi.Haix && cursym.Align < int16(a) { + cursym.Align = int16(a) } } -- 2.52.0