Goals

Add //go:linkname firstmoduledata on runtime.firstmoduledata with go1.23 release.

We want to access it by go:linkname, and keep compatibility with the old go versions.

Proposal Details

runtime.firstmoduledata provides many useful informations for low level develop.

For example: Get the file path of main function or test function.

// get path of the entry file, may be a /xx/xx_test.go or /xx/main.go
func getEntryFilePath() string {
    mainFile := getMainFile()
    if !isTestMode(mainFile) {
        return mainFile
    }
    firstModule := unsafe.Pointer(&firstmoduledata)
    fileTab := *(*[]byte)(reflection.Add(firstModule, filetabField.Offset))
    lastFiles := getLastFiles(fileTab)
    for _, file := range lastFiles {
        if isTestFile(file) {
            return file
        }
    }
    panic("can not found entry file path")
}

Comment From: tianon

I think what Ian meant in https://github.com/golang/go/issues/67401#issuecomment-2248044448 was making proposals for actual exported interfaces for the data you need (such as your getEntryFilePath example), not just a proposal for more linkname (which is very very likely to be simply rejected, per the discussion in that other thread).

Comment From: ianlancetaylor

Yes. This is not the kind of proposal I meant. I meant a maintainable way to access whatever kind of information you are looking for from runtime.firstmoduledata. For example, a proposal for a function to return the file path of the main function. (If you write such a proposal, please be sure to explain why the information is useful.)

I'm going to close this proposal. There is simply no way that we will adopt it. Sorry.