Skip to content

purgo build fail at linux #189

Closed
DataDog/go-libddwaf
#63

Description

@Cyberhan123

I try to build an app using purego on ubuntu but it fails on linux

2024-01-07T05:12:29.4981059Z # github.com/cyberhan123/stable-diffusion-desktop
2024-01-07T05:12:29.4983718Z dlopen: unhandled relocation for purego_dlopen (type 46 (SDYNIMPORT) rtype 7 (R_CALL))
2024-01-07T05:12:29.4989379Z dlsym: unhandled relocation for purego_dlsym (type 46 (SDYNIMPORT) rtype 7 (R_CALL))
2024-01-07T05:12:29.4991109Z dlerror: unhandled relocation for purego_dlerror (type 46 (SDYNIMPORT) rtype 7 (R_CALL))
2024-01-07T05:12:29.4992706Z dlclose: unhandled relocation for purego_dlclose (type 46 (SDYNIMPORT) rtype 7 (R_CALL))
2024-01-07T05:12:29.5058987Z   �[90m�[90m•�[0m�[0m �[39m�[39mCompiling application: �[0m�[0m�[30;101m�[30;101m  ERROR  �[0m�[0m �[91m�[91mexit status 1�[0m�[0m

The following is the complete github action log:

App, linuxamd64, ubuntu-latest.txt

Activity

  1. eliottness commented on Jan 15, 2024

    @eliottness
    Contributor

    Hey @Cyberhan123 👋

    Can you also provide the output of go env (feel free to redact any value that you don't feel comfortable sharing)?

  2. Cyberhan123 commented on Jan 15, 2024

    @Cyberhan123
    Author

    In fact, there is in the log.

  3. hajimehoshi commented on Jan 15, 2024

    @hajimehoshi
    Member
  4. added this to the v0.6.0 milestone on Jan 15, 2024
  5. TotallyGamerJet commented on Jan 15, 2024

    @TotallyGamerJet
    Collaborator

    I do not. Seems very strange since no changes to the dynamic linking have been made in quite a while

  6. TotallyGamerJet commented on Jan 15, 2024

    @TotallyGamerJet
    Collaborator

    @Cyberhan123 does it work if you set CGO_ENABLED=0?

  7. eliottness commented on Jan 15, 2024

    @eliottness
    Contributor

    If I am not mistaken, if relocations happens during build process it means the library is being statically linked. Now the question is: why would anyone want to static link libdl? I believe this could be caused by another package somewhere in the dependency tree that added -static or equivalent in the LDFLAGS directive for cgo.

    Adding -x and -work flags should give us enough info to understand if it's the case by grepping in the files and logs. I should be able to try it myself tomorrow.

  8. TotallyGamerJet commented on Jan 15, 2024

    @TotallyGamerJet
    Collaborator

    Relocations happen for any symbol that is in a different object file. Even if that symbol is one that is dynamically linked it must be relocated to the GOT & POT at build time.

    However, I think you might be correct that there is something causing it to try and be statically linked which isn't possible with purego. Please do let me know what you find!

  9. TotallyGamerJet commented on Jan 16, 2024

    @TotallyGamerJet
    Collaborator

    I was able to confirm on Ubunutu arm64 that building with #cgo LDFLAGS: -static produces the same output that is displayed above. This would suggest that this isn't an issue with purego as there is no way to statically link with purego since it requires libdl.so. Someone could rewrite the entire dynamic linker in Go but that sounds like it's begging to break.

  10. Cyberhan123 commented on Jan 16, 2024

    @Cyberhan123
    Author

    @Cyberhan123 does it work if you set CGO_ENABLED=0?

    If it is pure go code, there is no problem,If cgo exists, the above error will be reported.

  11. Cyberhan123 commented on Jan 16, 2024

    @Cyberhan123
    Author

    I was able to confirm on Ubunutu arm64 that building with #cgo LDFLAGS: -static produces the same output that is displayed above. This would suggest that this isn't an issue with purego as there is no way to statically link with purego since it requires libdl.so. Someone could rewrite the entire dynamic linker in Go but that sounds like it's begging to break.

    I'm using wails to build the desktop app ,I need to investigate it, thanks for the heads up.

  12. Cyberhan123 commented on Jan 16, 2024

    @Cyberhan123
    Author

    My code below also causes this problem:

    CGO_ENABLED=1 go build main.go
    
    package main
    
    import (
    	"fmt"
    	"image"
    	"image/color"
    	"image/png"
    	"os"
    
    	sd "github.com/seasonjs/stable-diffusion"
    )
    
    
    // #include <stdio.h>
    // #include <stdlib.h>
    //
    // void __lsan_do_leak_check(void);
    //
    //#cgo CFLAGS: -fsanitize=address
    //#cgo LDFLAGS: -fsanitize=address
    import "C"
    
    func MemoryLeakCheck() error {
    	diffusion, err := sd.NewCStableDiffusion("./deps/linux/lbsd-abi.so")
    	if err != nil {
    		return err
    	}
    	diffusion.SetLogCallBack(func(level sd.LogLevel, text string) {
    		fmt.Printf("%s", text)
    	})
    	ctx := diffusion.NewCtx("./models/miniSD.ckpt", "", "", "", false, false, true, 4, sd.F16, sd.CUDA_RNG, sd.DEFAULT)
    	defer diffusion.FreeCtx(ctx)
    
    	images := diffusion.PredictImage(ctx, "british short hair cat, high quality", "", 0, 7.0, 128, 128, sd.EULER_A, 10, 43, 1)
    
    	err = writeToFile(images[0].Data, int(images[0].Height), int(images[0].Width), "./assets/love_cat1.png")
    	if err != nil {
    		return err
    	}
    	C.__lsan_do_leak_check()
    	return nil
    }
    
    func writeToFile(byteData []byte, height int, width int, outputPath string) error {
    
    	img := image.NewRGBA(image.Rect(0, 0, width, height))
    
    	for y := 0; y < height; y++ {
    		for x := 0; x < width; x++ {
    			idx := (y*width + x) * 3
    			img.Set(x, y, color.RGBA{
    				R: byteData[idx],
    				G: byteData[idx+1],
    				B: byteData[idx+2],
    				A: 255,
    			})
    		}
    	}
    
    	file, err := os.Create(outputPath)
    	if err != nil {
    		return err
    	}
    	defer file.Close()
    
    	err = png.Encode(file, img)
    	if err != nil {
    		return err
    	}
    	return nil
    }
    
    func main() {
    	MemoryLeakCheck()
    }
  13. TotallyGamerJet commented on Jan 16, 2024

    @TotallyGamerJet
    Collaborator

    There is definitely something going on with the connection between having import "C" and import "github.com/seasonjs/stable-diffusion" because doing CGO_ENABLED=1 go build with the below code still produces the error. Is there another import "C" in stable-diffusion? I still don't think this is an issue fixable inside purego.

    package main
    
    import (
    	_ "github.com/seasonjs/stable-diffusion"
    )
    
    
    import "C"
    
    
    func main() {
    }
  14. Cyberhan123 commented on Jan 16, 2024

    @Cyberhan123
    Author

    No cgo code is used in stable-diffusion

  15. 10 remaining items

  16. Cyberhan123 commented on Jan 17, 2024

    @Cyberhan123
    Author

    Let me make a small attempt: @rsc

  17. TotallyGamerJet commented on Jan 17, 2024

    @TotallyGamerJet
    Collaborator

    It likely has to do with the fact that //go:cgo_import_dynamic is for internal linking while //go:cgo_import_static is for external linking. The former is what purego uses and the later can't be used outside the compiler. I just don't know why it worked before.

  18. modified the milestones: v0.6.0, v0.5.2 on Jan 17, 2024
  19. added a commit that references this issue on Jan 17, 2024
    babd452
  20. hajimehoshi commented on Jan 17, 2024

    @hajimehoshi
    Member

    I'll tag v0.5.2 and v0.6.0-alpha.4 a few days later, if we don't find any other issues.

  21. added a commit that references this issue on Jan 17, 2024
    579fc87
  22. hajimehoshi commented on Jan 17, 2024

    @hajimehoshi
    Member

    Let me reopen this. We are not able to cherry-pick this to 0.5 yet.

  23. added 2 commits that reference this issue on Jan 17, 2024
    5e5a90a
    11c42be
  24. hajimehoshi commented on Jan 18, 2024

    @hajimehoshi
    Member

    Done

  25. added a commit that references this issue on Jan 23, 2024
    c6e4d82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions