Skip to content

Commit

Permalink
cmd/link/internal/ld: put abi hash into a note
Browse files Browse the repository at this point in the history
This makes for a more stable API for tools (including cmd/link itself) to
extract the abi hash from a shared library and makes it possible at all for a
library that has had the local symbol table removed.

The existing note-writing code only supports writing notes into the very start
of the object file so they are easy to find in core dumps. This doesn't apply
to the "go" notes and means that all notes have to fit into a fixed size
budget. That's annoying now we have more notes (and the next CL will add
another one) so this does a little bit of work to make adding notes that do not
have to go at the start of the file easier and moves the writing of the package
list note over to that mechanism, which lets me revert a hack that increased
the size budget mentioned above for -buildmode=shared builds.

Change-Id: I6077a68d395c8a2bc43dec8506e73c71ef77d9b9
Reviewed-on: https://go-review.googlesource.com/10375
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
mwhudson authored and ianlancetaylor committed May 27, 2015
1 parent 9262e21 commit 6551803
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 58 deletions.
176 changes: 176 additions & 0 deletions misc/cgo/testshared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"bufio"
"bytes"
"debug/elf"
"encoding/binary"
"errors"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"math/rand"
Expand Down Expand Up @@ -176,6 +178,89 @@ func TestShlibnameFiles(t *testing.T) {
}
}

// Is a given offset into the file contained in a loaded segment?
func isOffsetLoaded(f *elf.File, offset uint64) bool {
for _, prog := range f.Progs {
if prog.Type == elf.PT_LOAD {
if prog.Off <= offset && offset < prog.Off+prog.Filesz {
return true
}
}
}
return false
}

func rnd(v int32, r int32) int32 {
if r <= 0 {
return v
}
v += r - 1
c := v % r
if c < 0 {
c += r
}
v -= c
return v
}

func readwithpad(r io.Reader, sz int32) ([]byte, error) {
data := make([]byte, rnd(sz, 4))
_, err := io.ReadFull(r, data)
if err != nil {
return nil, err
}
data = data[:sz]
return data, nil
}

type note struct {
name string
tag int32
desc string
section *elf.Section
}

// Read all notes from f. As ELF section names are not supposed to be special, one
// looks for a particular note by scanning all SHT_NOTE sections looking for a note
// with a particular "name" and "tag".
func readNotes(f *elf.File) ([]*note, error) {
var notes []*note
for _, sect := range f.Sections {
if sect.Type != elf.SHT_NOTE {
continue
}
r := sect.Open()
for {
var namesize, descsize, tag int32
err := binary.Read(r, f.ByteOrder, &namesize)
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("read namesize failed:", err)
}
err = binary.Read(r, f.ByteOrder, &descsize)
if err != nil {
return nil, fmt.Errorf("read descsize failed:", err)
}
err = binary.Read(r, f.ByteOrder, &tag)
if err != nil {
return nil, fmt.Errorf("read type failed:", err)
}
name, err := readwithpad(r, namesize)
if err != nil {
return nil, fmt.Errorf("read name failed:", err)
}
desc, err := readwithpad(r, descsize)
if err != nil {
return nil, fmt.Errorf("read desc failed:", err)
}
notes = append(notes, &note{name: string(name), tag: tag, desc: string(desc), section: sect})
}
}
return notes, nil
}

func dynStrings(path string, flag elf.DynTag) []string {
f, err := elf.Open(path)
defer f.Close()
Expand Down Expand Up @@ -233,6 +318,97 @@ func TestGOPathShlib(t *testing.T) {
run(t, "executable linked to GOPATH library", "./bin/exe")
}

// The shared library contains a note listing the packages it contains in a section
// that is not mapped into memory.
func testPkgListNote(t *testing.T, f *elf.File, note *note) {
if note.section.Flags != 0 {
t.Errorf("package list section has flags %v", note.section.Flags)
}
if isOffsetLoaded(f, note.section.Offset) {
t.Errorf("package list section contained in PT_LOAD segment")
}
if note.desc != "dep\n" {
t.Errorf("incorrect package list %q", note.desc)
}
}

// The shared library contains a note containing the ABI hash that is mapped into
// memory and there is a local symbol called go.link.abihashbytes that points 16
// bytes into it.
func testABIHashNote(t *testing.T, f *elf.File, note *note) {
if note.section.Flags != elf.SHF_ALLOC {
t.Errorf("abi hash section has flags %v", note.section.Flags)
}
if !isOffsetLoaded(f, note.section.Offset) {
t.Errorf("abihash section not contained in PT_LOAD segment")
}
var hashbytes elf.Symbol
symbols, err := f.Symbols()
if err != nil {
t.Errorf("error reading symbols %v", err)
return
}
for _, sym := range symbols {
if sym.Name == "go.link.abihashbytes" {
hashbytes = sym
}
}
if hashbytes.Name == "" {
t.Errorf("no symbol called go.link.abihashbytes")
return
}
if elf.ST_BIND(hashbytes.Info) != elf.STB_LOCAL {
t.Errorf("%s has incorrect binding %v", hashbytes.Name, elf.ST_BIND(hashbytes.Info))
}
if f.Sections[hashbytes.Section] != note.section {
t.Errorf("%s has incorrect section %v", hashbytes.Name, f.Sections[hashbytes.Section].Name)
}
if hashbytes.Value-note.section.Addr != 16 {
t.Errorf("%s has incorrect offset into section %d", hashbytes.Name, hashbytes.Value-note.section.Addr)
}
}

// The shared library contains notes with defined contents; see above.
func TestNotes(t *testing.T) {
goCmd(t, "install", "-buildmode=shared", "-linkshared", "dep")
f, err := elf.Open(filepath.Join(gopathInstallDir, "libdep.so"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
notes, err := readNotes(f)
if err != nil {
t.Fatal(err)
}
pkgListNoteFound := false
abiHashNoteFound := false
for _, note := range notes {
if note.name != "GO\x00\x00" {
continue
}
switch note.tag {
case 1: // ELF_NOTE_GOPKGLIST_TAG
if pkgListNoteFound {
t.Error("multiple package list notes")
}
testPkgListNote(t, f, note)
pkgListNoteFound = true
case 2: // ELF_NOTE_GOABIHASH_TAG
if abiHashNoteFound {
t.Error("multiple abi hash notes")
}
testABIHashNote(t, f, note)
abiHashNoteFound = true
}
}
if !pkgListNoteFound {
t.Error("package list note not found")
}
if !abiHashNoteFound {
t.Error("abi hash note not found")
}
}

// Testing rebuilding of shared libraries when they are stale is a bit more
// complicated that it seems like it should be. First, we make everything "old": but
// only a few seconds old, or it might be older than 6g (or the runtime source) and
Expand Down
8 changes: 0 additions & 8 deletions src/cmd/link/internal/amd64/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,6 @@ func archinit() {
ld.Elfinit()

ld.HEADR = ld.ELFRESERVE
if ld.Buildmode == ld.BuildmodeShared {
// When building a shared library we write a package list
// note that can get quite large. The external linker will
// re-layout all the sections anyway, so making this larger
// just wastes a little space in the intermediate object
// file, not the final shared library.
ld.HEADR *= 3
}
if ld.INITTEXT == -1 {
ld.INITTEXT = (1 << 22) + int64(ld.HEADR)
}
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/link/internal/ld/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,13 @@ func address() {
}
}

if Buildmode == BuildmodeShared {
s := Linklookup(Ctxt, "go.link.abihashbytes", 0)
sectSym := Linklookup(Ctxt, ".note.go.abihash", 0)
s.Sect = sectSym.Sect
s.Value = int64(sectSym.Sect.Vaddr + 16)
}

xdefine("runtime.text", obj.STEXT, int64(text.Vaddr))
xdefine("runtime.etext", obj.STEXT, int64(text.Vaddr+text.Length))
xdefine("runtime.rodata", obj.SRODATA, int64(rodata.Vaddr))
Expand Down
Loading

0 comments on commit 6551803

Please sign in to comment.