Fix library mode linking by optimizing genMainModuleFile
- For library modes (c-archive, c-shared), only generate minimal code with __llgo_argc/__llgo_argv globals - For executable mode, generate full main function with runtime initialization - This fixes undefined symbol errors when linking libraries that reference these globals
This commit is contained in:
@@ -767,6 +767,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, global l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// Generate main module file (needed for global variables even in library modes)
|
||||||
entryObjFile, err := genMainModuleFile(ctx, llssa.PkgRuntime, pkg, needRuntime, needPyInit)
|
entryObjFile, err := genMainModuleFile(ctx, llssa.PkgRuntime, pkg, needRuntime, needPyInit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -978,7 +979,18 @@ define weak void @_start() {
|
|||||||
if !needStart(ctx) {
|
if !needStart(ctx) {
|
||||||
startDefine = ""
|
startDefine = ""
|
||||||
}
|
}
|
||||||
mainCode := fmt.Sprintf(`; ModuleID = 'main'
|
|
||||||
|
var mainCode string
|
||||||
|
// For library modes (c-archive, c-shared), only generate global variables
|
||||||
|
if ctx.buildConf.BuildMode != BuildModeExe {
|
||||||
|
mainCode = `; ModuleID = 'main'
|
||||||
|
source_filename = "main"
|
||||||
|
@__llgo_argc = global i32 0, align 4
|
||||||
|
@__llgo_argv = global ptr null, align 8
|
||||||
|
`
|
||||||
|
} else {
|
||||||
|
// For executable mode, generate full main function
|
||||||
|
mainCode = fmt.Sprintf(`; ModuleID = 'main'
|
||||||
source_filename = "main"
|
source_filename = "main"
|
||||||
%s
|
%s
|
||||||
@__llgo_argc = global i32 0, align 4
|
@__llgo_argc = global i32 0, align 4
|
||||||
@@ -1012,9 +1024,10 @@ _llgo_0:
|
|||||||
ret i32 0
|
ret i32 0
|
||||||
}
|
}
|
||||||
`, declSizeT, stdioDecl,
|
`, declSizeT, stdioDecl,
|
||||||
pyInitDecl, rtInitDecl, mainPkgPath, mainPkgPath,
|
pyInitDecl, rtInitDecl, mainPkgPath, mainPkgPath,
|
||||||
startDefine, mainDefine, stdioNobuf,
|
startDefine, mainDefine, stdioNobuf,
|
||||||
pyInit, rtInit, mainPkgPath, mainPkgPath)
|
pyInit, rtInit, mainPkgPath, mainPkgPath)
|
||||||
|
}
|
||||||
|
|
||||||
return exportObject(ctx, pkg.PkgPath+".main", pkg.ExportFile+"-main", []byte(mainCode))
|
return exportObject(ctx, pkg.PkgPath+".main", pkg.ExportFile+"-main", []byte(mainCode))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user