Skip to content

Commit

Permalink
fix #92, function in text segment align with funcalign
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Nov 16, 2023
1 parent ac97855 commit d842e53
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/pkujhd/goloader/constants"
"github.com/pkujhd/goloader/obj"
"github.com/pkujhd/goloader/objabi/funcalign"
"github.com/pkujhd/goloader/objabi/reloctype"
"github.com/pkujhd/goloader/objabi/symkind"
"github.com/pkujhd/goloader/stackobject"
Expand Down Expand Up @@ -150,7 +151,10 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
symbol.Offset = len(linker.code)
linker.code = append(linker.code, objsym.Data...)
expandFunc(linker, objsym, symbol)
bytearrayAlignNops(linker.Arch, &linker.code, PtrSize)
if len(linker.code)-symbol.Offset < minfunc {
linker.code = append(linker.code, createArchNops(linker.Arch, minfunc-(len(linker.code)-symbol.Offset))...)
}
bytearrayAlignNops(linker.Arch, &linker.code, funcalign.GetFuncAlign(linker.Arch))
symbol.Func = &obj.Func{}
if err := linker.readFuncData(linker.objsymbolMap[name], symbol.Offset); err != nil {
return nil, err
Expand Down
28 changes: 28 additions & 0 deletions objabi/funcalign/funcalign.1.15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build go1.15 && !go1.16
// +build go1.15,!go1.16

package funcalign

import (
"cmd/objfile/sys"
"fmt"
)

func GetFuncAlign(arch *sys.Arch) int {
switch arch.Name {
//see:^cmd/linker/internal/arm/l.go
case sys.ArchARM.Name:
return 4
//see:^cmd/linker/internal/arm64/l.go
case sys.ArchARM64.Name:
return 16
//see:^cmd/linker/internal/x86/l.go
case sys.Arch386.Name:
return 16
// see:^cmd/linker/internal/amd64/l.go
case sys.ArchAMD64.Name:
return 16
default:
panic(fmt.Errorf("not support arch:%s", arch.Name))
}
}
28 changes: 28 additions & 0 deletions objabi/funcalign/funcalign.1.16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build go1.16 && !go1.22
// +build go1.16,!go1.22

package funcalign

import (
"cmd/objfile/sys"
"fmt"
)

func GetFuncAlign(arch *sys.Arch) int {
switch arch.Name {
//see:^cmd/linker/internal/arm/l.go
case sys.ArchARM.Name:
return 4
//see:^cmd/linker/internal/arm64/l.go
case sys.ArchARM64.Name:
return 16
//see:^cmd/linker/internal/x86/l.go
case sys.Arch386.Name:
return 16
// see:^cmd/linker/internal/amd64/l.go
case sys.ArchAMD64.Name:
return 32
default:
panic(fmt.Errorf("not support arch:%s", arch.Name))
}
}
28 changes: 28 additions & 0 deletions objabi/funcalign/funcalign.1.8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build go1.8 && !go1.15
// +build go1.8,!go1.15

package funcalign

import (
"cmd/objfile/sys"
"fmt"
)

func GetFuncAlign(arch *sys.Arch) int {
switch arch.Name {
//see:^cmd/linker/internal/arm/l.go
case sys.ArchARM.Name:
return 4
//see:^cmd/linker/internal/arm64/l.go
case sys.ArchARM64.Name:
return 8
//see:^cmd/linker/internal/x86/l.go
case sys.Arch386.Name:
return 16
// see:^cmd/linker/internal/amd64/l.go
case sys.ArchAMD64.Name:
return 16
default:
panic(fmt.Errorf("not support arch:%s", arch.Name))
}
}

0 comments on commit d842e53

Please sign in to comment.