| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package elf |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
type nameTest struct { |
| 13 |
val any |
| 14 |
str string |
| 15 |
} |
| 16 |
|
| 17 |
var nameTests = []nameTest{ |
| 18 |
{ELFOSABI_LINUX, "ELFOSABI_LINUX"}, |
| 19 |
{ET_EXEC, "ET_EXEC"}, |
| 20 |
{EM_860, "EM_860"}, |
| 21 |
{SHN_LOPROC, "SHN_LOPROC"}, |
| 22 |
{SHT_PROGBITS, "SHT_PROGBITS"}, |
| 23 |
{SHF_MERGE + SHF_TLS, "SHF_MERGE+SHF_TLS"}, |
| 24 |
{PT_LOAD, "PT_LOAD"}, |
| 25 |
{PF_W + PF_R + 0x50, "PF_W+PF_R+0x50"}, |
| 26 |
{DT_SYMBOLIC, "DT_SYMBOLIC"}, |
| 27 |
{DF_BIND_NOW, "DF_BIND_NOW"}, |
| 28 |
{NT_FPREGSET, "NT_FPREGSET"}, |
| 29 |
{STB_GLOBAL, "STB_GLOBAL"}, |
| 30 |
{STT_COMMON, "STT_COMMON"}, |
| 31 |
{STV_HIDDEN, "STV_HIDDEN"}, |
| 32 |
{R_X86_64_PC32, "R_X86_64_PC32"}, |
| 33 |
{R_ALPHA_OP_PUSH, "R_ALPHA_OP_PUSH"}, |
| 34 |
{R_ARM_THM_ABS5, "R_ARM_THM_ABS5"}, |
| 35 |
{R_386_GOT32, "R_386_GOT32"}, |
| 36 |
{R_PPC_GOT16_HI, "R_PPC_GOT16_HI"}, |
| 37 |
{R_SPARC_GOT22, "R_SPARC_GOT22"}, |
| 38 |
{ET_LOOS + 5, "ET_LOOS+5"}, |
| 39 |
{ProgFlag(0x50), "0x50"}, |
| 40 |
} |
| 41 |
|
| 42 |
func TestNames(t *testing.T) { |
| 43 |
for i, tt := range nameTests { |
| 44 |
s := fmt.Sprint(tt.val) |
| 45 |
if s != tt.str { |
| 46 |
t.Errorf("#%d: Sprint(%d) = %q, want %q", i, tt.val, s, tt.str) |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestNobitsSection(t *testing.T) { |
| 52 |
const testdata = "testdata/gcc-amd64-linux-exec" |
| 53 |
f, err := Open(testdata) |
| 54 |
if err != nil { |
| 55 |
t.Fatalf("could not read %s: %v", testdata, err) |
| 56 |
} |
| 57 |
defer f.Close() |
| 58 |
bss := f.Section(".bss") |
| 59 |
bssData, err := bss.Data() |
| 60 |
if err != nil { |
| 61 |
t.Fatalf("error reading .bss section: %v", err) |
| 62 |
} |
| 63 |
if g, w := uint64(len(bssData)), bss.Size; g != w { |
| 64 |
t.Errorf(".bss section length mismatch: got %d, want %d", g, w) |
| 65 |
} |
| 66 |
for i := range bssData { |
| 67 |
if bssData[i] != 0 { |
| 68 |
t.Fatalf("unexpected non-zero byte at offset %d: %#x", i, bssData[i]) |
| 69 |
} |
| 70 |
} |
| 71 |
} |