Skip to content

Commit

Permalink
cmd/ld: correct addresses in windows pe symbol table
Browse files Browse the repository at this point in the history
This should have been part of 36eb4a62fbb6,
but I later discovered that addresses are all wrong.
Appropriate test added now.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/89470043
  • Loading branch information
alexbrainman committed Apr 21, 2014
1 parent 1bb4f37 commit 80e7f97
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/cmd/ld/pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,17 @@ addsym(LSym *s, char *name, int type, vlong addr, vlong size, int ver, LSym *got
ncoffsym++;
}

static vlong
datoffsect(vlong addr)
{
if(addr >= segdata.vaddr)
return addr - segdata.vaddr;
if(addr >= segtext.vaddr)
return addr - segtext.vaddr;
diag("datoff %#llx", addr);
return 0;
}

static void
addsymtable(void)
{
Expand Down Expand Up @@ -540,7 +551,7 @@ addsymtable(void)
lputl(0);
lputl(s->strtbloff);
}
lputl(datoff(s->sym->value));
lputl(datoffsect(s->sym->value));
wputl(s->sect);
wputl(0x0308); // "array of structs"
cput(2); // storage class: external
Expand Down
49 changes: 49 additions & 0 deletions src/cmd/nm/nm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,55 @@
package main

import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)

var testData uint32

func checkSymbols(t *testing.T, nmoutput []byte) {
var checkSymbolsFound, testDataFound bool
scanner := bufio.NewScanner(bytes.NewBuffer(nmoutput))
for scanner.Scan() {
f := strings.Fields(scanner.Text())
if len(f) < 3 {
t.Error("nm must have at least 3 columns")
continue
}
switch f[2] {
case "cmd/nm.checkSymbols":
checkSymbolsFound = true
addr := "0x" + f[0]
if addr != fmt.Sprintf("%p", checkSymbols) {
t.Errorf("nm shows wrong address %v for checkSymbols (%p)", addr, checkSymbols)
}
case "cmd/nm.testData":
testDataFound = true
addr := "0x" + f[0]
if addr != fmt.Sprintf("%p", &testData) {
t.Errorf("nm shows wrong address %v for testData (%p)", addr, &testData)
}
}
}
if err := scanner.Err(); err != nil {
t.Errorf("error while reading symbols: %v", err)
return
}
if !checkSymbolsFound {
t.Error("nm shows no checkSymbols symbol")
}
if !testDataFound {
t.Error("nm shows no testData symbol")
}
}

func TestNM(t *testing.T) {
out, err := exec.Command("go", "build", "-o", "testnm.exe", "cmd/nm").CombinedOutput()
if err != nil {
Expand All @@ -37,4 +79,11 @@ func TestNM(t *testing.T) {
t.Fatalf("go tool nm %v: %v\n%s", exepath, err, string(out))
}
}

cmd := exec.Command("./testnm.exe", os.Args[0])
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
}
checkSymbols(t, out)
}

0 comments on commit 80e7f97

Please sign in to comment.