github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/objdump/elf.go (about)

     1  // Copyright 2013 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  // Parsing of ELF executables (Linux, FreeBSD, and so on).
     6  
     7  package main
     8  
     9  import (
    10  	"debug/elf"
    11  	"os"
    12  )
    13  
    14  func elfSymbols(f *os.File) (syms []Sym, goarch string) {
    15  	p, err := elf.NewFile(f)
    16  	if err != nil {
    17  		errorf("parsing %s: %v", f.Name(), err)
    18  		return
    19  	}
    20  
    21  	elfSyms, err := p.Symbols()
    22  	if err != nil {
    23  		errorf("parsing %s: %v", f.Name(), err)
    24  		return
    25  	}
    26  
    27  	switch p.Machine {
    28  	case elf.EM_X86_64:
    29  		goarch = "amd64"
    30  	case elf.EM_386:
    31  		goarch = "386"
    32  	case elf.EM_ARM:
    33  		goarch = "arm"
    34  	}
    35  
    36  	for _, s := range elfSyms {
    37  		sym := Sym{Addr: s.Value, Name: s.Name, Size: int64(s.Size), Code: '?'}
    38  		switch s.Section {
    39  		case elf.SHN_UNDEF:
    40  			sym.Code = 'U'
    41  		case elf.SHN_COMMON:
    42  			sym.Code = 'B'
    43  		default:
    44  			i := int(s.Section)
    45  			if i < 0 || i >= len(p.Sections) {
    46  				break
    47  			}
    48  			sect := p.Sections[i]
    49  			switch sect.Flags & (elf.SHF_WRITE | elf.SHF_ALLOC | elf.SHF_EXECINSTR) {
    50  			case elf.SHF_ALLOC | elf.SHF_EXECINSTR:
    51  				sym.Code = 'T'
    52  			case elf.SHF_ALLOC:
    53  				sym.Code = 'R'
    54  			case elf.SHF_ALLOC | elf.SHF_WRITE:
    55  				sym.Code = 'D'
    56  			}
    57  		}
    58  		if elf.ST_BIND(s.Info) == elf.STB_LOCAL {
    59  			sym.Code += 'a' - 'A'
    60  		}
    61  		syms = append(syms, sym)
    62  	}
    63  
    64  	return
    65  }