Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmik committed Aug 19, 2020
0 parents commit a9503c5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dwarf-relocation-golang-bug
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: all test

all: test

testdata/repro:
cd testdata && make

dwarf-relocation-golang-bug: main.go
go build

test: testdata/repro dwarf-relocation-golang-bug
@echo
@echo "Running test"
@./test.sh
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is a standalone repro case for https://github.com/golang/go/issues/40879

* `testdata` contains logic to build an executable `testdata/repro` that:
* has a `.rela.debug_ranges` section
* has a DWARF DIE that uses DW_AT_ranges (to force this, I used a linker script to spread the code across 2 sections at different addresses)
* `main.go` contains logic that prints the PC ranges of `testdata/repro`
* `test.sh` will print the PC ranges using both the Go binary, and `llvm-dwarfdump`, to highlight the difference
* `make` will compile all binaries and run `test.sh`

Note: you need `llvm` installed (for `llvm-dwarfdump`). Alternatively you can change the test.sh script to use the regular `dwarfdump`.
38 changes: 38 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"debug/elf"
"fmt"
"log"
)

func main() {
file, err := elf.Open("testdata/repro")
if err != nil {
log.Fatal(err)
}
defer file.Close()

dw, err := file.DWARF()
if err != nil {
log.Fatal(err)
}

reader := dw.Reader()

// The first entry will be the (only) CU
entry, err := reader.Next()
if err != nil {
log.Fatal(err)
}

// Read PC ranges from DIE
ranges, err := dw.Ranges(entry)
if err != nil {
log.Fatal(err)
}

for i := range ranges {
fmt.Printf("[0x%x, 0x%x)\n", ranges[i][0], ranges[i][1])
}
}
9 changes: 9 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash -eu

echo "Actual ranges:"
llvm-dwarfdump testdata/repro --name=repro.c | grep -A2 DW_AT_ranges | tail -2 | sed 's/))/)/' | tr -d ' '

echo
echo "Ranges derived from Golang's debug/elf + debug/dwarf"
./dwarf-relocation-golang-bug

1 change: 1 addition & 0 deletions testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repro
7 changes: 7 additions & 0 deletions testdata/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: all

repro: repro.c repro.ld
# The following command will likely print a warning (about a missing -T option), which should be ignored.
# Removing the warning would require passing a fully-fledged linker script to bypass gcc's default.
gcc -g $^ -Wl,--emit-relocs -o $@

8 changes: 8 additions & 0 deletions testdata/repro.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__attribute__((section(".coffee_section")))
int coffee(void) {
return 0;
}

int main(int argc, char *argv[]) {
return 0;
}
5 changes: 5 additions & 0 deletions testdata/repro.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECTIONS
{
. = 0xC0FFEE;
.coffee_section : {}
}

0 comments on commit a9503c5

Please sign in to comment.