pkujhd / goloader

load and run golang code at runtime.
Apache License 2.0
497 stars 58 forks source link

go linker dont use ptrsize to align TEXT symbol #92

Closed fumeboy closed 10 months ago

fumeboy commented 10 months ago

2 things should be notice:

  1. linker dont use ptrsize to align TEXT symbol, but ld.Arch.Funcalign
  2. linker use MINFUNC = 16 as minimal funcsize at assignAddress
// src(go1.19.6)/cmd/link/internal/amd64/l.go

package amd64

const (
    maxAlign  = 32 // max data alignment
    minAlign  = 1  // min data alignment
    funcAlign = 32
)

// src/cmd/link/internal/ld/data.go

// assign addresses to text
func (ctxt *Link) textaddress() {
    addsection(ctxt.loader, ctxt.Arch, &Segtext, ".text", 05)

    // Assign PCs in text segment.
    // Could parallelize, by assigning to text
    // and then letting threads copy down, but probably not worth it.
    sect := Segtext.Sections[0]

    sect.Align = int32(Funcalign)
    // ...
}

// assigns address for a text symbol, returns (possibly new) section, its number, and the address
func assignAddress(ctxt *Link, sect *sym.Section, n int, s loader.Sym, va uint64, isTramp, big bool) (*sym.Section, int, uint64) {
    // ...
    funcsize := uint64(MINFUNC) // spacing required for findfunctab
    if ldr.SymSize(s) > MINFUNC {
        funcsize = uint64(ldr.SymSize(s))
    }
    // ...
}