2015-02-27 22:57:28 -05:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2015-03-05 13:57:36 -05:00
|
|
|
// TODO/NICETOHAVE:
|
|
|
|
|
// - eliminate DW_CLS_ if not used
|
|
|
|
|
// - package info in compilation units
|
|
|
|
|
// - assign global variables and types to their packages
|
|
|
|
|
// - gdb uses c syntax, meaning clumsy quoting is needed for go identifiers. eg
|
|
|
|
|
// ptype struct '[]uint8' and qualifiers need to be quoted away
|
|
|
|
|
// - lexical scoping is lost, so gdb gets confused as to which 'main.i' you mean.
|
|
|
|
|
// - file:line info for variables
|
|
|
|
|
// - make strings a typedef so prettyprinters can see the underlying string type
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
package ld
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cmd/internal/obj"
|
|
|
|
|
"fmt"
|
2016-03-14 09:23:04 -07:00
|
|
|
"log"
|
2015-04-08 12:55:34 -07:00
|
|
|
"os"
|
2015-02-27 22:57:28 -05:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
const infoprefix = "go.dwarf.info."
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
/*
|
|
|
|
|
* Offsets and sizes of the debug_* sections in the cout file.
|
|
|
|
|
*/
|
|
|
|
|
var abbrevsym *LSym
|
|
|
|
|
var arangessec *LSym
|
|
|
|
|
var framesec *LSym
|
2016-03-14 09:23:04 -07:00
|
|
|
var infosec *LSym
|
|
|
|
|
var linesec *LSym
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
var gdbscript string
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Basic I/O
|
|
|
|
|
*/
|
2016-03-14 09:23:04 -07:00
|
|
|
func addrput(s *LSym, addr int64) {
|
2016-04-06 12:01:40 -07:00
|
|
|
switch SysArch.PtrSize {
|
2015-02-27 22:57:28 -05:00
|
|
|
case 4:
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, uint32(addr))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case 8:
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint64(Ctxt, s, uint64(addr))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
func appendUleb128(b []byte, v uint64) []byte {
|
2015-02-27 22:57:28 -05:00
|
|
|
for {
|
2016-02-29 13:07:50 -09:00
|
|
|
c := uint8(v & 0x7f)
|
2015-02-27 22:57:28 -05:00
|
|
|
v >>= 7
|
|
|
|
|
if v != 0 {
|
|
|
|
|
c |= 0x80
|
|
|
|
|
}
|
2016-02-29 13:07:50 -09:00
|
|
|
b = append(b, c)
|
2015-02-27 22:57:28 -05:00
|
|
|
if c&0x80 == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-29 13:07:50 -09:00
|
|
|
return b
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
func appendSleb128(b []byte, v int64) []byte {
|
2015-02-27 22:57:28 -05:00
|
|
|
for {
|
2016-02-29 13:07:50 -09:00
|
|
|
c := uint8(v & 0x7f)
|
|
|
|
|
s := uint8(v & 0x40)
|
2015-02-27 22:57:28 -05:00
|
|
|
v >>= 7
|
|
|
|
|
if (v != -1 || s == 0) && (v != 0 || s != 0) {
|
|
|
|
|
c |= 0x80
|
|
|
|
|
}
|
2016-02-29 13:07:50 -09:00
|
|
|
b = append(b, c)
|
2015-02-27 22:57:28 -05:00
|
|
|
if c&0x80 == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-29 13:07:50 -09:00
|
|
|
return b
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-04-24 20:24:49 -07:00
|
|
|
var encbuf [10]byte
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func uleb128put(s *LSym, v int64) {
|
2016-02-29 13:07:50 -09:00
|
|
|
b := appendUleb128(encbuf[:0], uint64(v))
|
2016-03-14 09:23:04 -07:00
|
|
|
Addbytes(Ctxt, s, b)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func sleb128put(s *LSym, v int64) {
|
2016-02-29 13:07:50 -09:00
|
|
|
b := appendSleb128(encbuf[:0], v)
|
2016-03-14 09:23:04 -07:00
|
|
|
for _, x := range b {
|
|
|
|
|
Adduint8(Ctxt, s, x)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Defining Abbrevs. This is hardcoded, and there will be
|
|
|
|
|
* only a handful of them. The DWARF spec places no restriction on
|
|
|
|
|
* the ordering of attributes in the Abbrevs and DIEs, and we will
|
|
|
|
|
* always write them out in the order of declaration in the abbrev.
|
|
|
|
|
*/
|
|
|
|
|
type DWAttrForm struct {
|
|
|
|
|
attr uint16
|
|
|
|
|
form uint8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Go-specific type attributes.
|
|
|
|
|
const (
|
2015-03-05 13:57:36 -05:00
|
|
|
DW_AT_go_kind = 0x2900
|
|
|
|
|
DW_AT_go_key = 0x2901
|
|
|
|
|
DW_AT_go_elem = 0x2902
|
|
|
|
|
|
|
|
|
|
DW_AT_internal_location = 253 // params and locals; not emitted
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Index into the abbrevs table below.
|
|
|
|
|
// Keep in sync with ispubname() and ispubtype() below.
|
|
|
|
|
// ispubtype considers >= NULLTYPE public
|
|
|
|
|
const (
|
|
|
|
|
DW_ABRV_NULL = iota
|
|
|
|
|
DW_ABRV_COMPUNIT
|
|
|
|
|
DW_ABRV_FUNCTION
|
|
|
|
|
DW_ABRV_VARIABLE
|
|
|
|
|
DW_ABRV_AUTO
|
|
|
|
|
DW_ABRV_PARAM
|
|
|
|
|
DW_ABRV_STRUCTFIELD
|
|
|
|
|
DW_ABRV_FUNCTYPEPARAM
|
|
|
|
|
DW_ABRV_DOTDOTDOT
|
|
|
|
|
DW_ABRV_ARRAYRANGE
|
|
|
|
|
DW_ABRV_NULLTYPE
|
|
|
|
|
DW_ABRV_BASETYPE
|
|
|
|
|
DW_ABRV_ARRAYTYPE
|
|
|
|
|
DW_ABRV_CHANTYPE
|
|
|
|
|
DW_ABRV_FUNCTYPE
|
|
|
|
|
DW_ABRV_IFACETYPE
|
|
|
|
|
DW_ABRV_MAPTYPE
|
|
|
|
|
DW_ABRV_PTRTYPE
|
2015-03-05 13:57:36 -05:00
|
|
|
DW_ABRV_BARE_PTRTYPE // only for void*, no DW_AT_type attr to please gdb 6.
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_ABRV_SLICETYPE
|
|
|
|
|
DW_ABRV_STRINGTYPE
|
|
|
|
|
DW_ABRV_STRUCTTYPE
|
|
|
|
|
DW_ABRV_TYPEDECL
|
|
|
|
|
DW_NABRV
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DWAbbrev struct {
|
|
|
|
|
tag uint8
|
|
|
|
|
children uint8
|
2015-04-10 15:25:10 -07:00
|
|
|
attr []DWAttrForm
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-04-10 15:25:10 -07:00
|
|
|
var abbrevs = [DW_NABRV]DWAbbrev{
|
2015-02-27 22:57:28 -05:00
|
|
|
/* The mandatory DW_ABRV_NULL entry. */
|
2015-04-10 15:25:10 -07:00
|
|
|
{0, 0, []DWAttrForm{}},
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* COMPUNIT */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_compile_unit,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_language, DW_FORM_data1},
|
|
|
|
|
{DW_AT_low_pc, DW_FORM_addr},
|
|
|
|
|
{DW_AT_high_pc, DW_FORM_addr},
|
|
|
|
|
{DW_AT_stmt_list, DW_FORM_data4},
|
2015-04-08 12:55:34 -07:00
|
|
|
{DW_AT_comp_dir, DW_FORM_string},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* FUNCTION */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_subprogram,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_low_pc, DW_FORM_addr},
|
|
|
|
|
{DW_AT_high_pc, DW_FORM_addr},
|
|
|
|
|
{DW_AT_external, DW_FORM_flag},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* VARIABLE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_variable,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_location, DW_FORM_block1},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_external, DW_FORM_flag},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* AUTO */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_variable,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_location, DW_FORM_block1},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* PARAM */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_formal_parameter,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_location, DW_FORM_block1},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* STRUCTFIELD */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_member,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_data_member_location, DW_FORM_block1},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* FUNCTYPEPARAM */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_formal_parameter,
|
|
|
|
|
DW_CHILDREN_no,
|
|
|
|
|
|
|
|
|
|
// No name!
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* DOTDOTDOT */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_unspecified_parameters,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* ARRAYRANGE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_subrange_type,
|
|
|
|
|
DW_CHILDREN_no,
|
|
|
|
|
|
|
|
|
|
// No name!
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_count, DW_FORM_udata},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Below here are the types considered public by ispubtype
|
|
|
|
|
/* NULLTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_unspecified_type,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* BASETYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_base_type,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_encoding, DW_FORM_data1},
|
|
|
|
|
{DW_AT_byte_size, DW_FORM_data1},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* ARRAYTYPE */
|
|
|
|
|
// child is subrange with upper bound
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_array_type,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_byte_size, DW_FORM_udata},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* CHANTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_typedef,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
|
|
|
|
{DW_AT_go_elem, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* FUNCTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_subroutine_type,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
// {DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* IFACETYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_typedef,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* MAPTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_typedef,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
|
|
|
|
{DW_AT_go_key, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_elem, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* PTRTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_pointer_type,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* BARE_PTRTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_pointer_type,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* SLICETYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_structure_type,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_byte_size, DW_FORM_udata},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
|
|
|
|
{DW_AT_go_elem, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* STRINGTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_structure_type,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_byte_size, DW_FORM_udata},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* STRUCTTYPE */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_structure_type,
|
|
|
|
|
DW_CHILDREN_yes,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_byte_size, DW_FORM_udata},
|
|
|
|
|
{DW_AT_go_kind, DW_FORM_data1},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/* TYPEDECL */
|
2015-04-10 15:25:10 -07:00
|
|
|
{
|
2015-02-27 22:57:28 -05:00
|
|
|
DW_TAG_typedef,
|
|
|
|
|
DW_CHILDREN_no,
|
2015-04-10 15:25:10 -07:00
|
|
|
[]DWAttrForm{
|
|
|
|
|
{DW_AT_name, DW_FORM_string},
|
|
|
|
|
{DW_AT_type, DW_FORM_ref_addr},
|
2015-02-27 22:57:28 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
var dwarfp *LSym
|
|
|
|
|
|
|
|
|
|
func writeabbrev() *LSym {
|
|
|
|
|
s := Linklookup(Ctxt, ".debug_abbrev", 0)
|
|
|
|
|
s.Type = obj.SDWARFSECT
|
|
|
|
|
abbrevsym = s
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 1; i < DW_NABRV; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
// See section 7.5.3
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, int64(i))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, int64(abbrevs[i].tag))
|
|
|
|
|
Adduint8(Ctxt, s, abbrevs[i].children)
|
2015-04-10 15:25:10 -07:00
|
|
|
for _, f := range abbrevs[i].attr {
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, int64(f.attr))
|
|
|
|
|
uleb128put(s, int64(f.form))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, 0)
|
|
|
|
|
uleb128put(s, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, 0)
|
|
|
|
|
return s
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Debugging Information Entries and their attributes.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// For DW_CLS_string and _block, value should contain the length, and
|
|
|
|
|
// data the data, for _reference, value is 0 and data is a DWDie* to
|
|
|
|
|
// the referenced instance, for all others, value is the whole thing
|
|
|
|
|
// and data is null.
|
|
|
|
|
|
|
|
|
|
type DWAttr struct {
|
|
|
|
|
link *DWAttr
|
2015-03-05 13:57:36 -05:00
|
|
|
atr uint16 // DW_AT_
|
|
|
|
|
cls uint8 // DW_CLS_
|
2015-02-27 22:57:28 -05:00
|
|
|
value int64
|
|
|
|
|
data interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DWDie struct {
|
|
|
|
|
abbrev int
|
|
|
|
|
link *DWDie
|
|
|
|
|
child *DWDie
|
|
|
|
|
attr *DWAttr
|
2016-03-14 09:23:04 -07:00
|
|
|
sym *LSym
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Root DIEs for compilation units, types and global variables.
|
|
|
|
|
*/
|
|
|
|
|
var dwroot DWDie
|
|
|
|
|
|
|
|
|
|
var dwtypes DWDie
|
|
|
|
|
|
|
|
|
|
var dwglobals DWDie
|
|
|
|
|
|
|
|
|
|
func newattr(die *DWDie, attr uint16, cls int, value int64, data interface{}) *DWAttr {
|
2015-03-02 12:35:15 -05:00
|
|
|
a := new(DWAttr)
|
2015-02-27 22:57:28 -05:00
|
|
|
a.link = die.attr
|
|
|
|
|
die.attr = a
|
|
|
|
|
a.atr = attr
|
|
|
|
|
a.cls = uint8(cls)
|
|
|
|
|
a.value = value
|
|
|
|
|
a.data = data
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Each DIE (except the root ones) has at least 1 attribute: its
|
|
|
|
|
// name. getattr moves the desired one to the front so
|
|
|
|
|
// frequently searched ones are found faster.
|
|
|
|
|
func getattr(die *DWDie, attr uint16) *DWAttr {
|
|
|
|
|
if die.attr.atr == attr {
|
|
|
|
|
return die.attr
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
a := die.attr
|
|
|
|
|
b := a.link
|
2015-02-27 22:57:28 -05:00
|
|
|
for b != nil {
|
|
|
|
|
if b.atr == attr {
|
|
|
|
|
a.link = b.link
|
|
|
|
|
b.link = die.attr
|
|
|
|
|
die.attr = b
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a = b
|
|
|
|
|
b = b.link
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Every DIE has at least a DW_AT_name attribute (but it will only be
|
2016-03-14 09:23:04 -07:00
|
|
|
// written out if it is listed in the abbrev).
|
|
|
|
|
func newdie(parent *DWDie, abbrev int, name string, version int) *DWDie {
|
2015-03-02 12:35:15 -05:00
|
|
|
die := new(DWDie)
|
2015-02-27 22:57:28 -05:00
|
|
|
die.abbrev = abbrev
|
|
|
|
|
die.link = parent.child
|
|
|
|
|
parent.child = die
|
|
|
|
|
|
|
|
|
|
newattr(die, DW_AT_name, DW_CLS_STRING, int64(len(name)), name)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
if name != "" && (abbrev <= DW_ABRV_VARIABLE || abbrev >= DW_ABRV_NULLTYPE) {
|
|
|
|
|
if abbrev != DW_ABRV_VARIABLE || version == 0 {
|
|
|
|
|
die.sym = Linklookup(Ctxt, infoprefix+name, version)
|
|
|
|
|
die.sym.Type = obj.SDWARFINFO
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return die
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func walktypedef(die *DWDie) *DWDie {
|
|
|
|
|
// Resolve typedef if present.
|
|
|
|
|
if die.abbrev == DW_ABRV_TYPEDECL {
|
2015-03-02 12:35:15 -05:00
|
|
|
for attr := die.attr; attr != nil; attr = attr.link {
|
2015-02-27 22:57:28 -05:00
|
|
|
if attr.atr == DW_AT_type && attr.cls == DW_CLS_REFERENCE && attr.data != nil {
|
|
|
|
|
return attr.data.(*DWDie)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return die
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func walksymtypedef(s *LSym) *LSym {
|
|
|
|
|
if t := Linkrlookup(Ctxt, s.Name+".def", int(s.Version)); t != nil {
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// Find child by AT_name using hashtable if available or linear scan
|
|
|
|
|
// if not.
|
2016-03-14 09:23:04 -07:00
|
|
|
func findchild(die *DWDie, name string) *DWDie {
|
2015-10-10 10:57:35 +00:00
|
|
|
var prev *DWDie
|
|
|
|
|
for ; die != prev; prev, die = die, walktypedef(die) {
|
2016-03-14 09:23:04 -07:00
|
|
|
for a := die.child; a != nil; a = a.link {
|
|
|
|
|
if name == getattr(a, DW_AT_name).data {
|
|
|
|
|
return a
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-10-10 10:57:35 +00:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
continue
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func find(name string) *LSym {
|
|
|
|
|
return Linkrlookup(Ctxt, infoprefix+name, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustFind(name string) *LSym {
|
|
|
|
|
r := find(name)
|
2015-02-27 22:57:28 -05:00
|
|
|
if r == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
Exitf("dwarf find: cannot find %s", name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func adddwarfref(ctxt *Link, s *LSym, t *LSym, size int) int64 {
|
|
|
|
|
var result int64
|
|
|
|
|
switch size {
|
2015-02-27 22:57:28 -05:00
|
|
|
default:
|
2016-03-14 09:23:04 -07:00
|
|
|
Diag("invalid size %d in adddwarfref\n", size)
|
|
|
|
|
fallthrough
|
2016-04-06 12:01:40 -07:00
|
|
|
case SysArch.PtrSize:
|
2016-03-14 09:23:04 -07:00
|
|
|
result = Addaddr(ctxt, s, t)
|
|
|
|
|
case 4:
|
|
|
|
|
result = addaddrplus4(ctxt, s, t, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
r := &s.R[len(s.R)-1]
|
|
|
|
|
r.Type = obj.R_DWARFREF
|
|
|
|
|
return result
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func newrefattr(die *DWDie, attr uint16, ref *LSym) *DWAttr {
|
2015-02-27 22:57:28 -05:00
|
|
|
if ref == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return newattr(die, attr, DW_CLS_REFERENCE, 0, ref)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func putattr(s *LSym, abbrev int, form int, cls int, value int64, data interface{}) {
|
2015-02-27 22:57:28 -05:00
|
|
|
switch form {
|
|
|
|
|
case DW_FORM_addr: // address
|
|
|
|
|
if Linkmode == LinkExternal {
|
|
|
|
|
value -= (data.(*LSym)).Value
|
2016-03-14 09:23:04 -07:00
|
|
|
Addaddrplus(Ctxt, s, data.(*LSym), value)
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
addrput(s, value)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_block1: // block
|
|
|
|
|
if cls == DW_CLS_ADDRESS {
|
2016-04-06 12:01:40 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(1+SysArch.PtrSize))
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, DW_OP_addr)
|
|
|
|
|
Addaddr(Ctxt, s, data.(*LSym))
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value &= 0xff
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(value))
|
2015-03-02 12:35:15 -05:00
|
|
|
p := data.([]byte)
|
|
|
|
|
for i := 0; int64(i) < value; i++ {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(p[i]))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DW_FORM_block2: // block
|
|
|
|
|
value &= 0xffff
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint16(Ctxt, s, uint16(value))
|
2015-03-02 12:35:15 -05:00
|
|
|
p := data.([]byte)
|
|
|
|
|
for i := 0; int64(i) < value; i++ {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(p[i]))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DW_FORM_block4: // block
|
|
|
|
|
value &= 0xffffffff
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, uint32(value))
|
2015-03-02 12:35:15 -05:00
|
|
|
p := data.([]byte)
|
|
|
|
|
for i := 0; int64(i) < value; i++ {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(p[i]))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DW_FORM_block: // block
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, value)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
p := data.([]byte)
|
|
|
|
|
for i := 0; int64(i) < value; i++ {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(p[i]))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DW_FORM_data1: // constant
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(value))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_data2: // constant
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint16(Ctxt, s, uint16(value))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_data4: // constant, {line,loclist,mac,rangelist}ptr
|
|
|
|
|
if Linkmode == LinkExternal && cls == DW_CLS_PTR {
|
2016-03-14 09:23:04 -07:00
|
|
|
adddwarfref(Ctxt, s, linesec, 4)
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, uint32(value))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_data8: // constant, {line,loclist,mac,rangelist}ptr
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint64(Ctxt, s, uint64(value))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_sdata: // constant
|
2016-03-14 09:23:04 -07:00
|
|
|
sleb128put(s, value)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_udata: // constant
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(s, value)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_string: // string
|
2016-03-14 09:23:04 -07:00
|
|
|
str := data.(string)
|
|
|
|
|
Addstring(s, str)
|
|
|
|
|
for i := int64(len(str)); i < value; i++ {
|
|
|
|
|
Adduint8(Ctxt, s, 0)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case DW_FORM_flag: // flag
|
|
|
|
|
if value != 0 {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, 1)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In DWARF 2 (which is what we claim to generate),
|
|
|
|
|
// the ref_addr is the same size as a normal address.
|
|
|
|
|
// In DWARF 3 it is always 32 bits, unless emitting a large
|
|
|
|
|
// (> 4 GB of debug info aka "64-bit") unit, which we don't implement.
|
|
|
|
|
case DW_FORM_ref_addr: // reference to a DIE in the .info section
|
|
|
|
|
if data == nil {
|
|
|
|
|
Diag("dwarf: null reference in %d", abbrev)
|
2016-04-06 12:01:40 -07:00
|
|
|
if SysArch.PtrSize == 8 {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint64(Ctxt, s, 0) // invalid dwarf, gdb will complain.
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, 0) // invalid dwarf, gdb will complain.
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-03-14 09:23:04 -07:00
|
|
|
dsym := data.(*LSym)
|
2016-04-06 12:01:40 -07:00
|
|
|
adddwarfref(Ctxt, s, dsym, SysArch.PtrSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DW_FORM_ref1, // reference within the compilation unit
|
|
|
|
|
DW_FORM_ref2, // reference
|
|
|
|
|
DW_FORM_ref4, // reference
|
|
|
|
|
DW_FORM_ref8, // reference
|
|
|
|
|
DW_FORM_ref_udata, // reference
|
|
|
|
|
|
|
|
|
|
DW_FORM_strp, // string
|
|
|
|
|
DW_FORM_indirect: // (see Section 7.5.3)
|
|
|
|
|
fallthrough
|
|
|
|
|
default:
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("dwarf: unsupported attribute form %d / class %d", form, cls)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note that we can (and do) add arbitrary attributes to a DIE, but
|
|
|
|
|
// only the ones actually listed in the Abbrev will be written out.
|
2016-03-14 09:23:04 -07:00
|
|
|
func putattrs(s *LSym, abbrev int, attr *DWAttr) {
|
2015-04-10 15:25:10 -07:00
|
|
|
Outer:
|
|
|
|
|
for _, f := range abbrevs[abbrev].attr {
|
|
|
|
|
for ap := attr; ap != nil; ap = ap.link {
|
|
|
|
|
if ap.atr == f.attr {
|
2016-03-14 09:23:04 -07:00
|
|
|
putattr(s, abbrev, int(f.form), int(ap.cls), ap.value, ap.data)
|
2015-04-10 15:25:10 -07:00
|
|
|
continue Outer
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
putattr(s, abbrev, int(f.form), 0, 0, nil)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func putdies(prev *LSym, die *DWDie) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
for ; die != nil; die = die.link {
|
2016-03-14 09:23:04 -07:00
|
|
|
prev = putdie(prev, die)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, prev, 0)
|
|
|
|
|
return prev
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func putdie(prev *LSym, die *DWDie) *LSym {
|
|
|
|
|
s := die.sym
|
|
|
|
|
if s == nil {
|
|
|
|
|
s = prev
|
|
|
|
|
} else {
|
|
|
|
|
if s.Attr.OnList() {
|
|
|
|
|
log.Fatalf("symbol %s listed multiple times", s.Name)
|
|
|
|
|
}
|
|
|
|
|
s.Attr |= AttrOnList
|
|
|
|
|
prev.Next = s
|
|
|
|
|
}
|
|
|
|
|
uleb128put(s, int64(die.abbrev))
|
|
|
|
|
putattrs(s, die.abbrev, die.attr)
|
2015-02-27 22:57:28 -05:00
|
|
|
if abbrevs[die.abbrev].children != 0 {
|
2016-03-14 09:23:04 -07:00
|
|
|
return putdies(s, die.child)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
return s
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func reverselist(list **DWDie) {
|
2015-03-02 12:35:15 -05:00
|
|
|
curr := *list
|
2015-03-02 14:22:05 -05:00
|
|
|
var prev *DWDie
|
2015-02-27 22:57:28 -05:00
|
|
|
for curr != nil {
|
|
|
|
|
var next *DWDie = curr.link
|
|
|
|
|
curr.link = prev
|
|
|
|
|
prev = curr
|
|
|
|
|
curr = next
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*list = prev
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func reversetree(list **DWDie) {
|
|
|
|
|
reverselist(list)
|
2015-03-02 12:35:15 -05:00
|
|
|
for die := *list; die != nil; die = die.link {
|
2015-02-27 22:57:28 -05:00
|
|
|
if abbrevs[die.abbrev].children != 0 {
|
|
|
|
|
reversetree(&die.child)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newmemberoffsetattr(die *DWDie, offs int32) {
|
|
|
|
|
var block [20]byte
|
2016-02-29 13:07:50 -09:00
|
|
|
b := append(block[:0], DW_OP_plus_uconst)
|
|
|
|
|
b = appendUleb128(b, uint64(offs))
|
|
|
|
|
newattr(die, DW_AT_data_member_location, DW_CLS_BLOCK, int64(len(b)), b)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GDB doesn't like DW_FORM_addr for DW_AT_location, so emit a
|
|
|
|
|
// location expression that evals to a const.
|
|
|
|
|
func newabslocexprattr(die *DWDie, addr int64, sym *LSym) {
|
|
|
|
|
newattr(die, DW_AT_location, DW_CLS_ADDRESS, addr, sym)
|
|
|
|
|
// below
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lookup predefined types
|
|
|
|
|
func lookup_or_diag(n string) *LSym {
|
2015-03-02 12:35:15 -05:00
|
|
|
s := Linkrlookup(Ctxt, n, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
if s == nil || s.Size == 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("dwarf: missing type: %s", n)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dotypedef(parent *DWDie, name string, def *DWDie) {
|
|
|
|
|
// Only emit typedefs for real names.
|
|
|
|
|
if strings.HasPrefix(name, "map[") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(name, "struct {") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if strings.HasPrefix(name, "chan ") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if name[0] == '[' || name[0] == '*' {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if def == nil {
|
|
|
|
|
Diag("dwarf: bad def in dotypedef")
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
def.sym = Linklookup(Ctxt, def.sym.Name+".def", 0)
|
|
|
|
|
def.sym.Type = obj.SDWARFINFO
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// The typedef entry must be created after the def,
|
|
|
|
|
// so that future lookups will find the typedef instead
|
|
|
|
|
// of the real definition. This hooks the typedef into any
|
|
|
|
|
// circular definition loops, so that gdb can understand them.
|
2016-03-14 09:23:04 -07:00
|
|
|
die := newdie(parent, DW_ABRV_TYPEDECL, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
newrefattr(die, DW_AT_type, def.sym)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define gotype, for composite ones recurse into constituents.
|
2016-03-14 09:23:04 -07:00
|
|
|
func defgotype(gotype *LSym) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if gotype == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
return mustFind("<unspecified>")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !strings.HasPrefix(gotype.Name, "type.") {
|
2016-02-17 09:41:12 -05:00
|
|
|
Diag("dwarf: type name doesn't start with \"type.\": %s", gotype.Name)
|
2016-03-14 09:23:04 -07:00
|
|
|
return mustFind("<unspecified>")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
name := gotype.Name[5:] // could also decode from Type.string
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
sdie := find(name)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
if sdie != nil {
|
|
|
|
|
return sdie
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
return newtype(gotype).sym
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func newtype(gotype *LSym) *DWDie {
|
|
|
|
|
name := gotype.Name[5:] // could also decode from Type.string
|
2015-03-02 12:35:15 -05:00
|
|
|
kind := decodetype_kind(gotype)
|
|
|
|
|
bytesize := decodetype_size(gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
var die *DWDie
|
2015-02-27 22:57:28 -05:00
|
|
|
switch kind {
|
|
|
|
|
case obj.KindBool:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_boolean, 0)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindInt,
|
|
|
|
|
obj.KindInt8,
|
|
|
|
|
obj.KindInt16,
|
|
|
|
|
obj.KindInt32,
|
|
|
|
|
obj.KindInt64:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_signed, 0)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindUint,
|
|
|
|
|
obj.KindUint8,
|
|
|
|
|
obj.KindUint16,
|
|
|
|
|
obj.KindUint32,
|
|
|
|
|
obj.KindUint64,
|
|
|
|
|
obj.KindUintptr:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindFloat32,
|
|
|
|
|
obj.KindFloat64:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_float, 0)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindComplex64,
|
|
|
|
|
obj.KindComplex128:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BASETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_complex_float, 0)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindArray:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_ARRAYTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
s := decodetype_arrayelem(gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(die, DW_AT_type, defgotype(s))
|
2016-03-14 09:23:04 -07:00
|
|
|
fld := newdie(die, DW_ABRV_ARRAYRANGE, "range", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// use actual length not upper bound; correct for 0-length arrays.
|
|
|
|
|
newattr(fld, DW_AT_count, DW_CLS_CONSTANT, decodetype_arraylen(gotype), 0)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
newrefattr(fld, DW_AT_type, mustFind("uintptr"))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case obj.KindChan:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_CHANTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
s := decodetype_chanelem(gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(die, DW_AT_go_elem, defgotype(s))
|
2016-03-14 09:23:04 -07:00
|
|
|
// Save elem type for synthesizechantypes. We could synthesize here
|
|
|
|
|
// but that would change the order of DIEs we output.
|
|
|
|
|
newrefattr(die, DW_AT_type, s)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case obj.KindFunc:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_FUNCTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
2016-03-14 09:23:04 -07:00
|
|
|
newrefattr(die, DW_AT_type, mustFind("void"))
|
2015-03-02 12:35:15 -05:00
|
|
|
nfields := decodetype_funcincount(gotype)
|
|
|
|
|
var fld *DWDie
|
|
|
|
|
var s *LSym
|
|
|
|
|
for i := 0; i < nfields; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
s = decodetype_funcintype(gotype, i)
|
2016-03-14 09:23:04 -07:00
|
|
|
fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(fld, DW_AT_type, defgotype(s))
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 11:31:13 -05:00
|
|
|
if decodetype_funcdotdotdot(gotype) {
|
2016-03-14 09:23:04 -07:00
|
|
|
newdie(die, DW_ABRV_DOTDOTDOT, "...", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
nfields = decodetype_funcoutcount(gotype)
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < nfields; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
s = decodetype_funcouttype(gotype, i)
|
2016-03-14 09:23:04 -07:00
|
|
|
fld = newdie(die, DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(fld, DW_AT_type, defptrto(defgotype(s)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case obj.KindInterface:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_IFACETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
nfields := int(decodetype_ifacemethodcount(gotype))
|
|
|
|
|
var s *LSym
|
2015-02-27 22:57:28 -05:00
|
|
|
if nfields == 0 {
|
|
|
|
|
s = lookup_or_diag("type.runtime.eface")
|
|
|
|
|
} else {
|
|
|
|
|
s = lookup_or_diag("type.runtime.iface")
|
|
|
|
|
}
|
|
|
|
|
newrefattr(die, DW_AT_type, defgotype(s))
|
|
|
|
|
|
|
|
|
|
case obj.KindMap:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_MAPTYPE, name, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
s := decodetype_mapkey(gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(die, DW_AT_go_key, defgotype(s))
|
|
|
|
|
s = decodetype_mapvalue(gotype)
|
|
|
|
|
newrefattr(die, DW_AT_go_elem, defgotype(s))
|
2016-03-14 09:23:04 -07:00
|
|
|
// Save gotype for use in synthesizemaptypes. We could synthesize here,
|
|
|
|
|
// but that would change the order of the DIEs.
|
|
|
|
|
newrefattr(die, DW_AT_type, gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case obj.KindPtr:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_PTRTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
2015-03-02 12:35:15 -05:00
|
|
|
s := decodetype_ptrelem(gotype)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(die, DW_AT_type, defgotype(s))
|
|
|
|
|
|
|
|
|
|
case obj.KindSlice:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_SLICETYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
s := decodetype_arrayelem(gotype)
|
2016-03-14 09:23:04 -07:00
|
|
|
elem := defgotype(s)
|
|
|
|
|
newrefattr(die, DW_AT_go_elem, elem)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
case obj.KindString:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_STRINGTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
|
|
|
|
|
|
|
|
|
case obj.KindStruct:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_STRUCTTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
dotypedef(&dwtypes, name, die)
|
|
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, bytesize, 0)
|
2015-03-02 12:35:15 -05:00
|
|
|
nfields := decodetype_structfieldcount(gotype)
|
|
|
|
|
var f string
|
|
|
|
|
var fld *DWDie
|
|
|
|
|
var s *LSym
|
|
|
|
|
for i := 0; i < nfields; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
f = decodetype_structfieldname(gotype, i)
|
|
|
|
|
s = decodetype_structfieldtype(gotype, i)
|
|
|
|
|
if f == "" {
|
|
|
|
|
f = s.Name[5:] // skip "type."
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
fld = newdie(die, DW_ABRV_STRUCTFIELD, f, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newrefattr(fld, DW_AT_type, defgotype(s))
|
|
|
|
|
newmemberoffsetattr(fld, int32(decodetype_structfieldoffs(gotype, i)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case obj.KindUnsafePointer:
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_BARE_PTRTYPE, name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Diag("dwarf: definition of unknown kind %d: %s", kind, gotype.Name)
|
2016-03-14 09:23:04 -07:00
|
|
|
die = newdie(&dwtypes, DW_ABRV_TYPEDECL, name, 0)
|
|
|
|
|
newrefattr(die, DW_AT_type, mustFind("<unspecified>"))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newattr(die, DW_AT_go_kind, DW_CLS_CONSTANT, int64(kind), 0)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
if _, ok := prototypedies[gotype.Name]; ok {
|
|
|
|
|
prototypedies[gotype.Name] = die
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
return die
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func nameFromDIESym(dwtype *LSym) string {
|
|
|
|
|
return strings.TrimSuffix(dwtype.Name[len(infoprefix):], ".def")
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// Find or construct *T given T.
|
2016-03-14 09:23:04 -07:00
|
|
|
func defptrto(dwtype *LSym) *LSym {
|
|
|
|
|
ptrname := "*" + nameFromDIESym(dwtype)
|
|
|
|
|
die := find(ptrname)
|
2015-02-27 22:57:28 -05:00
|
|
|
if die == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
pdie := newdie(&dwtypes, DW_ABRV_PTRTYPE, ptrname, 0)
|
|
|
|
|
newrefattr(pdie, DW_AT_type, dwtype)
|
|
|
|
|
return pdie.sym
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return die
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copies src's children into dst. Copies attributes by value.
|
2016-03-01 23:21:55 +00:00
|
|
|
// DWAttr.data is copied as pointer only. If except is one of
|
2015-02-27 22:57:28 -05:00
|
|
|
// the top-level children, it will not be copied.
|
|
|
|
|
func copychildrenexcept(dst *DWDie, src *DWDie, except *DWDie) {
|
|
|
|
|
for src = src.child; src != nil; src = src.link {
|
|
|
|
|
if src == except {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
c := newdie(dst, src.abbrev, getattr(src, DW_AT_name).data.(string), 0)
|
2015-10-10 10:57:35 +00:00
|
|
|
for a := src.attr; a != nil; a = a.link {
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(c, a.atr, int(a.cls), a.value, a.data)
|
|
|
|
|
}
|
|
|
|
|
copychildrenexcept(c, src, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reverselist(&dst.child)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func copychildren(dst *DWDie, src *DWDie) {
|
|
|
|
|
copychildrenexcept(dst, src, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search children (assumed to have DW_TAG_member) for the one named
|
|
|
|
|
// field and set its DW_AT_type to dwtype
|
2016-03-14 09:23:04 -07:00
|
|
|
func substitutetype(structdie *DWDie, field string, dwtype *LSym) {
|
|
|
|
|
child := findchild(structdie, field)
|
2015-02-27 22:57:28 -05:00
|
|
|
if child == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
Exitf("dwarf substitutetype: %s does not have member %s",
|
|
|
|
|
getattr(structdie, DW_AT_name).data, field)
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
a := getattr(child, DW_AT_type)
|
2015-02-27 22:57:28 -05:00
|
|
|
if a != nil {
|
|
|
|
|
a.data = dwtype
|
|
|
|
|
} else {
|
|
|
|
|
newrefattr(child, DW_AT_type, dwtype)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func findprotodie(name string) *DWDie {
|
|
|
|
|
die, ok := prototypedies[name]
|
|
|
|
|
if ok && die == nil {
|
|
|
|
|
defgotype(lookup_or_diag(name))
|
|
|
|
|
die = prototypedies[name]
|
|
|
|
|
}
|
|
|
|
|
return die
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
func synthesizestringtypes(die *DWDie) {
|
2016-03-14 09:23:04 -07:00
|
|
|
prototype := walktypedef(findprotodie("type.runtime.stringStructDWARF"))
|
2015-02-27 22:57:28 -05:00
|
|
|
if prototype == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ; die != nil; die = die.link {
|
|
|
|
|
if die.abbrev != DW_ABRV_STRINGTYPE {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
copychildren(die, prototype)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func synthesizeslicetypes(die *DWDie) {
|
2016-03-14 09:23:04 -07:00
|
|
|
prototype := walktypedef(findprotodie("type.runtime.slice"))
|
2015-02-27 22:57:28 -05:00
|
|
|
if prototype == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ; die != nil; die = die.link {
|
|
|
|
|
if die.abbrev != DW_ABRV_SLICETYPE {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
copychildren(die, prototype)
|
2016-03-14 09:23:04 -07:00
|
|
|
elem := getattr(die, DW_AT_go_elem).data.(*LSym)
|
2015-02-27 22:57:28 -05:00
|
|
|
substitutetype(die, "array", defptrto(elem))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mkinternaltypename(base string, arg1 string, arg2 string) string {
|
|
|
|
|
var buf string
|
|
|
|
|
|
|
|
|
|
if arg2 == "" {
|
|
|
|
|
buf = fmt.Sprintf("%s<%s>", base, arg1)
|
|
|
|
|
} else {
|
|
|
|
|
buf = fmt.Sprintf("%s<%s,%s>", base, arg1, arg2)
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
n := buf
|
2015-02-27 22:57:28 -05:00
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// synthesizemaptypes is way too closely married to runtime/hashmap.c
|
|
|
|
|
const (
|
|
|
|
|
MaxKeySize = 128
|
|
|
|
|
MaxValSize = 128
|
|
|
|
|
BucketSize = 8
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func mkinternaltype(abbrev int, typename, keyname, valname string, f func(*DWDie)) *LSym {
|
|
|
|
|
name := mkinternaltypename(typename, keyname, valname)
|
|
|
|
|
symname := infoprefix + name
|
|
|
|
|
s := Linkrlookup(Ctxt, symname, 0)
|
|
|
|
|
if s != nil {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
die := newdie(&dwtypes, abbrev, name, 0)
|
|
|
|
|
f(die)
|
|
|
|
|
return die.sym
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
func synthesizemaptypes(die *DWDie) {
|
2016-03-14 09:23:04 -07:00
|
|
|
hash := walktypedef(findprotodie("type.runtime.hmap"))
|
|
|
|
|
bucket := walktypedef(findprotodie("type.runtime.bmap"))
|
2015-03-02 12:35:15 -05:00
|
|
|
|
|
|
|
|
if hash == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
for ; die != nil; die = die.link {
|
|
|
|
|
if die.abbrev != DW_ABRV_MAPTYPE {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
gotype := getattr(die, DW_AT_type).data.(*LSym)
|
|
|
|
|
keytype := decodetype_mapkey(gotype)
|
|
|
|
|
valtype := decodetype_mapvalue(gotype)
|
|
|
|
|
keysize, valsize := decodetype_size(keytype), decodetype_size(valtype)
|
|
|
|
|
keytype, valtype = walksymtypedef(defgotype(keytype)), walksymtypedef(defgotype(valtype))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// compute size info like hashmap.c does.
|
2015-10-10 10:57:35 +00:00
|
|
|
indirect_key, indirect_val := false, false
|
2015-02-27 22:57:28 -05:00
|
|
|
if keysize > MaxKeySize {
|
2016-04-06 12:01:40 -07:00
|
|
|
keysize = int64(SysArch.PtrSize)
|
2015-10-10 10:57:35 +00:00
|
|
|
indirect_key = true
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
if valsize > MaxValSize {
|
2016-04-06 12:01:40 -07:00
|
|
|
valsize = int64(SysArch.PtrSize)
|
2015-10-10 10:57:35 +00:00
|
|
|
indirect_val = true
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct type to represent an array of BucketSize keys
|
2016-03-14 09:23:04 -07:00
|
|
|
keyname := nameFromDIESym(keytype)
|
|
|
|
|
dwhks := mkinternaltype(DW_ABRV_ARRAYTYPE, "[]key", keyname, "", func(dwhk *DWDie) {
|
|
|
|
|
newattr(dwhk, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize*int64(keysize), 0)
|
|
|
|
|
t := keytype
|
|
|
|
|
if indirect_key {
|
|
|
|
|
t = defptrto(keytype)
|
|
|
|
|
}
|
|
|
|
|
newrefattr(dwhk, DW_AT_type, t)
|
|
|
|
|
fld := newdie(dwhk, DW_ABRV_ARRAYRANGE, "size", 0)
|
|
|
|
|
newattr(fld, DW_AT_count, DW_CLS_CONSTANT, BucketSize, 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, mustFind("uintptr"))
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Construct type to represent an array of BucketSize values
|
2016-03-14 09:23:04 -07:00
|
|
|
valname := nameFromDIESym(valtype)
|
|
|
|
|
dwhvs := mkinternaltype(DW_ABRV_ARRAYTYPE, "[]val", valname, "", func(dwhv *DWDie) {
|
|
|
|
|
newattr(dwhv, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize*int64(valsize), 0)
|
|
|
|
|
t := valtype
|
|
|
|
|
if indirect_val {
|
|
|
|
|
t = defptrto(valtype)
|
|
|
|
|
}
|
|
|
|
|
newrefattr(dwhv, DW_AT_type, t)
|
|
|
|
|
fld := newdie(dwhv, DW_ABRV_ARRAYRANGE, "size", 0)
|
|
|
|
|
newattr(fld, DW_AT_count, DW_CLS_CONSTANT, BucketSize, 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, mustFind("uintptr"))
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Construct bucket<K,V>
|
2016-03-14 09:23:04 -07:00
|
|
|
dwhbs := mkinternaltype(DW_ABRV_STRUCTTYPE, "bucket", keyname, valname, func(dwhb *DWDie) {
|
|
|
|
|
// Copy over all fields except the field "data" from the generic
|
|
|
|
|
// bucket. "data" will be replaced with keys/values below.
|
|
|
|
|
copychildrenexcept(dwhb, bucket, findchild(bucket, "data"))
|
|
|
|
|
|
|
|
|
|
fld := newdie(dwhb, DW_ABRV_STRUCTFIELD, "keys", 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, dwhks)
|
|
|
|
|
newmemberoffsetattr(fld, BucketSize)
|
|
|
|
|
fld = newdie(dwhb, DW_ABRV_STRUCTFIELD, "values", 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, dwhvs)
|
|
|
|
|
newmemberoffsetattr(fld, BucketSize+BucketSize*int32(keysize))
|
|
|
|
|
fld = newdie(dwhb, DW_ABRV_STRUCTFIELD, "overflow", 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, defptrto(dwhb.sym))
|
|
|
|
|
newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize)))
|
2016-04-06 12:01:40 -07:00
|
|
|
if SysArch.RegSize > SysArch.PtrSize {
|
2016-03-14 09:23:04 -07:00
|
|
|
fld = newdie(dwhb, DW_ABRV_STRUCTFIELD, "pad", 0)
|
|
|
|
|
newrefattr(fld, DW_AT_type, mustFind("uintptr"))
|
2016-04-06 12:01:40 -07:00
|
|
|
newmemberoffsetattr(fld, BucketSize+BucketSize*(int32(keysize)+int32(valsize))+int32(SysArch.PtrSize))
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
newattr(dwhb, DW_AT_byte_size, DW_CLS_CONSTANT, BucketSize+BucketSize*int64(keysize)+BucketSize*int64(valsize)+int64(SysArch.RegSize), 0)
|
2016-03-14 09:23:04 -07:00
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Construct hash<K,V>
|
2016-03-14 09:23:04 -07:00
|
|
|
dwhs := mkinternaltype(DW_ABRV_STRUCTTYPE, "hash", keyname, valname, func(dwh *DWDie) {
|
|
|
|
|
copychildren(dwh, hash)
|
|
|
|
|
substitutetype(dwh, "buckets", defptrto(dwhbs))
|
|
|
|
|
substitutetype(dwh, "oldbuckets", defptrto(dwhbs))
|
|
|
|
|
newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT, getattr(hash, DW_AT_byte_size).value, nil)
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// make map type a pointer to hash<K,V>
|
2016-03-14 09:23:04 -07:00
|
|
|
newrefattr(die, DW_AT_type, defptrto(dwhs))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func synthesizechantypes(die *DWDie) {
|
2016-03-14 09:23:04 -07:00
|
|
|
sudog := walktypedef(findprotodie("type.runtime.sudog"))
|
|
|
|
|
waitq := walktypedef(findprotodie("type.runtime.waitq"))
|
|
|
|
|
hchan := walktypedef(findprotodie("type.runtime.hchan"))
|
2015-02-27 22:57:28 -05:00
|
|
|
if sudog == nil || waitq == nil || hchan == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
sudogsize := int(getattr(sudog, DW_AT_byte_size).value)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
for ; die != nil; die = die.link {
|
|
|
|
|
if die.abbrev != DW_ABRV_CHANTYPE {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
elemgotype := getattr(die, DW_AT_type).data.(*LSym)
|
|
|
|
|
elemsize := decodetype_size(elemgotype)
|
|
|
|
|
elemname := elemgotype.Name[5:]
|
|
|
|
|
elemtype := walksymtypedef(defgotype(elemgotype))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// sudog<T>
|
2016-03-14 09:23:04 -07:00
|
|
|
dwss := mkinternaltype(DW_ABRV_STRUCTTYPE, "sudog", elemname, "", func(dws *DWDie) {
|
|
|
|
|
copychildren(dws, sudog)
|
|
|
|
|
substitutetype(dws, "elem", elemtype)
|
|
|
|
|
if elemsize > 8 {
|
|
|
|
|
elemsize -= 8
|
|
|
|
|
} else {
|
|
|
|
|
elemsize = 0
|
|
|
|
|
}
|
|
|
|
|
newattr(dws, DW_AT_byte_size, DW_CLS_CONSTANT, int64(sudogsize)+int64(elemsize), nil)
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// waitq<T>
|
2016-03-14 09:23:04 -07:00
|
|
|
dwws := mkinternaltype(DW_ABRV_STRUCTTYPE, "waitq", elemname, "", func(dww *DWDie) {
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
copychildren(dww, waitq)
|
|
|
|
|
substitutetype(dww, "first", defptrto(dwss))
|
|
|
|
|
substitutetype(dww, "last", defptrto(dwss))
|
|
|
|
|
newattr(dww, DW_AT_byte_size, DW_CLS_CONSTANT, getattr(waitq, DW_AT_byte_size).value, nil)
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// hchan<T>
|
2016-03-14 09:23:04 -07:00
|
|
|
dwhs := mkinternaltype(DW_ABRV_STRUCTTYPE, "hchan", elemname, "", func(dwh *DWDie) {
|
|
|
|
|
copychildren(dwh, hchan)
|
|
|
|
|
substitutetype(dwh, "recvq", dwws)
|
|
|
|
|
substitutetype(dwh, "sendq", dwws)
|
|
|
|
|
newattr(dwh, DW_AT_byte_size, DW_CLS_CONSTANT, getattr(hchan, DW_AT_byte_size).value, nil)
|
|
|
|
|
})
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
newrefattr(die, DW_AT_type, defptrto(dwhs))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For use with pass.c::genasmsym
|
|
|
|
|
func defdwsymb(sym *LSym, s string, t int, v int64, size int64, ver int, gotype *LSym) {
|
|
|
|
|
if strings.HasPrefix(s, "go.string.") {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-07-22 14:59:56 -04:00
|
|
|
if strings.HasPrefix(s, "runtime.gcbits.") {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if strings.HasPrefix(s, "type.") && s != "type.*" && !strings.HasPrefix(s, "type..") {
|
|
|
|
|
defgotype(sym)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 14:22:05 -05:00
|
|
|
var dv *DWDie
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
var dt *LSym
|
2015-02-27 22:57:28 -05:00
|
|
|
switch t {
|
|
|
|
|
default:
|
|
|
|
|
return
|
|
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case 'd', 'b', 'D', 'B':
|
2016-03-14 09:23:04 -07:00
|
|
|
dv = newdie(&dwglobals, DW_ABRV_VARIABLE, s, ver)
|
2015-02-27 22:57:28 -05:00
|
|
|
newabslocexprattr(dv, v, sym)
|
|
|
|
|
if ver == 0 {
|
|
|
|
|
newattr(dv, DW_AT_external, DW_CLS_FLAG, 1, 0)
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
|
2015-04-01 09:38:44 -07:00
|
|
|
case 'a', 'p':
|
2015-02-27 22:57:28 -05:00
|
|
|
dt = defgotype(gotype)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dv != nil {
|
|
|
|
|
newrefattr(dv, DW_AT_type, dt)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func movetomodule(parent *DWDie) {
|
2015-03-02 12:35:15 -05:00
|
|
|
die := dwroot.child.child
|
2015-02-27 22:57:28 -05:00
|
|
|
for die.link != nil {
|
|
|
|
|
die = die.link
|
|
|
|
|
}
|
|
|
|
|
die.link = parent.child
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the pcln table contains runtime/runtime.go, use that to set gdbscript path.
|
|
|
|
|
func finddebugruntimepath(s *LSym) {
|
|
|
|
|
if gdbscript != "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 14:02:36 +02:00
|
|
|
for i := range s.Pcln.File {
|
2015-10-10 10:57:35 +00:00
|
|
|
f := s.Pcln.File[i]
|
2015-02-27 22:57:28 -05:00
|
|
|
if i := strings.Index(f.Name, "runtime/runtime.go"); i >= 0 {
|
|
|
|
|
gdbscript = f.Name[:i] + "runtime/runtime-gdb.py"
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Generate short opcodes when possible, long ones when necessary.
|
|
|
|
|
* See section 6.2.5
|
|
|
|
|
*/
|
|
|
|
|
const (
|
|
|
|
|
LINE_BASE = -1
|
|
|
|
|
LINE_RANGE = 4
|
|
|
|
|
OPCODE_BASE = 10
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func putpclcdelta(s *LSym, delta_pc int64, delta_lc int64) {
|
2015-02-27 22:57:28 -05:00
|
|
|
if LINE_BASE <= delta_lc && delta_lc < LINE_BASE+LINE_RANGE {
|
|
|
|
|
var opcode int64 = OPCODE_BASE + (delta_lc - LINE_BASE) + (LINE_RANGE * delta_pc)
|
|
|
|
|
if OPCODE_BASE <= opcode && opcode < 256 {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(opcode))
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if delta_pc != 0 {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, DW_LNS_advance_pc)
|
|
|
|
|
sleb128put(s, delta_pc)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, DW_LNS_advance_line)
|
|
|
|
|
sleb128put(s, delta_lc)
|
|
|
|
|
Adduint8(Ctxt, s, DW_LNS_copy)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newcfaoffsetattr(die *DWDie, offs int32) {
|
|
|
|
|
var block [20]byte
|
2016-02-29 13:07:50 -09:00
|
|
|
b := append(block[:0], DW_OP_call_frame_cfa)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if offs != 0 {
|
2016-02-29 13:07:50 -09:00
|
|
|
b = append(b, DW_OP_consts)
|
|
|
|
|
b = appendSleb128(b, int64(offs))
|
|
|
|
|
b = append(b, DW_OP_plus)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
newattr(die, DW_AT_location, DW_CLS_BLOCK, int64(len(b)), b)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mkvarname(name string, da int) string {
|
2015-03-02 12:35:15 -05:00
|
|
|
buf := fmt.Sprintf("%s#%d", name, da)
|
|
|
|
|
n := buf
|
2015-02-27 22:57:28 -05:00
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Walk prog table, emit line program and build DIE tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-08 12:55:34 -07:00
|
|
|
func getCompilationDir() string {
|
|
|
|
|
if dir, err := os.Getwd(); err == nil {
|
|
|
|
|
return dir
|
|
|
|
|
}
|
|
|
|
|
return "/"
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func writelines(prev *LSym) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if linesec == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
linesec = Linklookup(Ctxt, ".debug_line", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
linesec.Type = obj.SDWARFSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
linesec.R = linesec.R[:0]
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
ls := linesec
|
|
|
|
|
prev.Next = ls
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
unitstart := int64(-1)
|
2016-03-14 09:23:04 -07:00
|
|
|
headerstart := int64(-1)
|
2015-03-02 12:35:15 -05:00
|
|
|
headerend := int64(-1)
|
|
|
|
|
epc := int64(0)
|
2015-03-02 14:22:05 -05:00
|
|
|
var epcs *LSym
|
|
|
|
|
var dwinfo *DWDie
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
lang := DW_LANG_Go
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
s := Ctxt.Textp
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
dwinfo = newdie(&dwroot, DW_ABRV_COMPUNIT, "go", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(dwinfo, DW_AT_language, DW_CLS_CONSTANT, int64(lang), 0)
|
2016-03-14 09:23:04 -07:00
|
|
|
newattr(dwinfo, DW_AT_stmt_list, DW_CLS_PTR, 0, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(dwinfo, DW_AT_low_pc, DW_CLS_ADDRESS, s.Value, s)
|
2015-04-08 12:55:34 -07:00
|
|
|
// OS X linker requires compilation dir or absolute path in comp unit name to output debug info.
|
|
|
|
|
compDir := getCompilationDir()
|
|
|
|
|
newattr(dwinfo, DW_AT_comp_dir, DW_CLS_STRING, int64(len(compDir)), compDir)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Write .debug_line Line Number Program Header (sec 6.2.4)
|
|
|
|
|
// Fields marked with (*) must be changed for 64-bit dwarf
|
2016-03-14 09:23:04 -07:00
|
|
|
unit_length_offset := ls.Size
|
|
|
|
|
Adduint32(Ctxt, ls, 0) // unit_length (*), filled in at end.
|
|
|
|
|
unitstart = ls.Size
|
|
|
|
|
Adduint16(Ctxt, ls, 2) // dwarf version (appendix F)
|
|
|
|
|
header_length_offset := ls.Size
|
|
|
|
|
Adduint32(Ctxt, ls, 0) // header_length (*), filled in at end.
|
|
|
|
|
headerstart = ls.Size
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// cpos == unitstart + 4 + 2 + 4
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, 1) // minimum_instruction_length
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // default_is_stmt
|
|
|
|
|
Adduint8(Ctxt, ls, LINE_BASE&0xFF) // line_base
|
|
|
|
|
Adduint8(Ctxt, ls, LINE_RANGE) // line_range
|
|
|
|
|
Adduint8(Ctxt, ls, OPCODE_BASE) // opcode_base
|
|
|
|
|
Adduint8(Ctxt, ls, 0) // standard_opcode_lengths[1]
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // standard_opcode_lengths[2]
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // standard_opcode_lengths[3]
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // standard_opcode_lengths[4]
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // standard_opcode_lengths[5]
|
|
|
|
|
Adduint8(Ctxt, ls, 0) // standard_opcode_lengths[6]
|
|
|
|
|
Adduint8(Ctxt, ls, 0) // standard_opcode_lengths[7]
|
|
|
|
|
Adduint8(Ctxt, ls, 0) // standard_opcode_lengths[8]
|
|
|
|
|
Adduint8(Ctxt, ls, 1) // standard_opcode_lengths[9]
|
|
|
|
|
Adduint8(Ctxt, ls, 0) // include_directories (empty)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
files := make([]*LSym, Ctxt.Nhistfile)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for f := Ctxt.Filesyms; f != nil; f = f.Next {
|
2015-02-27 22:57:28 -05:00
|
|
|
files[f.Value-1] = f
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; int32(i) < Ctxt.Nhistfile; i++ {
|
2016-03-14 09:23:04 -07:00
|
|
|
Addstring(ls, files[i].Name)
|
|
|
|
|
Adduint8(Ctxt, ls, 0)
|
|
|
|
|
Adduint8(Ctxt, ls, 0)
|
|
|
|
|
Adduint8(Ctxt, ls, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4 zeros: the string termination + 3 fields.
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
// terminate file_names.
|
2016-03-14 09:23:04 -07:00
|
|
|
headerend = ls.Size
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, 0) // start extended opcode
|
2016-04-06 12:01:40 -07:00
|
|
|
uleb128put(ls, 1+int64(SysArch.PtrSize))
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, DW_LNE_set_address)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
pc := s.Value
|
|
|
|
|
line := 1
|
|
|
|
|
file := 1
|
2015-02-27 22:57:28 -05:00
|
|
|
if Linkmode == LinkExternal {
|
2016-03-14 09:23:04 -07:00
|
|
|
Addaddr(Ctxt, ls, s)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2016-03-14 09:23:04 -07:00
|
|
|
addrput(ls, pc)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
var pcfile Pciter
|
|
|
|
|
var pcline Pciter
|
2015-02-27 22:57:28 -05:00
|
|
|
for Ctxt.Cursym = Ctxt.Textp; Ctxt.Cursym != nil; Ctxt.Cursym = Ctxt.Cursym.Next {
|
|
|
|
|
s = Ctxt.Cursym
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
dwfunc := newdie(dwinfo, DW_ABRV_FUNCTION, s.Name, int(s.Version))
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(dwfunc, DW_AT_low_pc, DW_CLS_ADDRESS, s.Value, s)
|
|
|
|
|
epc = s.Value + s.Size
|
|
|
|
|
epcs = s
|
|
|
|
|
newattr(dwfunc, DW_AT_high_pc, DW_CLS_ADDRESS, epc, s)
|
|
|
|
|
if s.Version == 0 {
|
|
|
|
|
newattr(dwfunc, DW_AT_external, DW_CLS_FLAG, 1, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if s.Pcln == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finddebugruntimepath(s)
|
|
|
|
|
|
|
|
|
|
pciterinit(Ctxt, &pcfile, &s.Pcln.Pcfile)
|
|
|
|
|
pciterinit(Ctxt, &pcline, &s.Pcln.Pcline)
|
|
|
|
|
epc = pc
|
|
|
|
|
for pcfile.done == 0 && pcline.done == 0 {
|
|
|
|
|
if epc-s.Value >= int64(pcfile.nextpc) {
|
|
|
|
|
pciternext(&pcfile)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if epc-s.Value >= int64(pcline.nextpc) {
|
|
|
|
|
pciternext(&pcline)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if int32(file) != pcfile.value {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, DW_LNS_set_file)
|
|
|
|
|
uleb128put(ls, int64(pcfile.value))
|
2015-02-27 22:57:28 -05:00
|
|
|
file = int(pcfile.value)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
putpclcdelta(ls, s.Value+int64(pcline.pc)-pc, int64(pcline.value)-int64(line))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
pc = s.Value + int64(pcline.pc)
|
|
|
|
|
line = int(pcline.value)
|
|
|
|
|
if pcfile.nextpc < pcline.nextpc {
|
|
|
|
|
epc = int64(pcfile.nextpc)
|
|
|
|
|
} else {
|
|
|
|
|
epc = int64(pcline.nextpc)
|
|
|
|
|
}
|
|
|
|
|
epc += s.Value
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 10:57:35 +00:00
|
|
|
var (
|
2016-03-02 15:14:02 -05:00
|
|
|
dt, da int
|
|
|
|
|
offs int64
|
2015-10-10 10:57:35 +00:00
|
|
|
)
|
2016-03-02 22:38:42 -05:00
|
|
|
for _, a := range s.Autom {
|
2015-02-27 22:57:28 -05:00
|
|
|
switch a.Name {
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.A_AUTO:
|
2015-02-27 22:57:28 -05:00
|
|
|
dt = DW_ABRV_AUTO
|
2015-05-07 00:48:09 -04:00
|
|
|
offs = int64(a.Aoffset)
|
|
|
|
|
if !haslinkregister() {
|
2016-04-06 12:01:40 -07:00
|
|
|
offs -= int64(SysArch.PtrSize)
|
2015-05-07 00:48:09 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-19 19:33:58 -07:00
|
|
|
case obj.A_PARAM:
|
2015-02-27 22:57:28 -05:00
|
|
|
dt = DW_ABRV_PARAM
|
2015-10-12 12:19:20 +13:00
|
|
|
offs = int64(a.Aoffset) + Ctxt.FixedFrameSize()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(a.Asym.Name, ".autotmp_") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-10-10 10:57:35 +00:00
|
|
|
var n string
|
2016-03-14 09:23:04 -07:00
|
|
|
if findchild(dwfunc, a.Asym.Name) != nil {
|
2015-02-27 22:57:28 -05:00
|
|
|
n = mkvarname(a.Asym.Name, da)
|
|
|
|
|
} else {
|
|
|
|
|
n = a.Asym.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Drop the package prefix from locals and arguments.
|
|
|
|
|
if i := strings.LastIndex(n, "."); i >= 0 {
|
|
|
|
|
n = n[i+1:]
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
dwvar := newdie(dwfunc, dt, n, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newcfaoffsetattr(dwvar, int32(offs))
|
|
|
|
|
newrefattr(dwvar, DW_AT_type, defgotype(a.Gotype))
|
|
|
|
|
|
|
|
|
|
// push dwvar down dwfunc->child to preserve order
|
|
|
|
|
newattr(dwvar, DW_AT_internal_location, DW_CLS_CONSTANT, offs, nil)
|
|
|
|
|
|
|
|
|
|
dwfunc.child = dwvar.link // take dwvar out from the top of the list
|
2015-10-10 10:57:35 +00:00
|
|
|
dws := &dwfunc.child
|
|
|
|
|
for ; *dws != nil; dws = &(*dws).link {
|
2015-02-27 22:57:28 -05:00
|
|
|
if offs > getattr(*dws, DW_AT_internal_location).value {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
dwvar.link = *dws
|
|
|
|
|
*dws = dwvar
|
|
|
|
|
|
|
|
|
|
da++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, ls, 0) // start extended opcode
|
|
|
|
|
uleb128put(ls, 1)
|
|
|
|
|
Adduint8(Ctxt, ls, DW_LNE_end_sequence)
|
|
|
|
|
|
|
|
|
|
newattr(dwinfo, DW_AT_high_pc, DW_CLS_ADDRESS, epc+1, epcs)
|
|
|
|
|
|
|
|
|
|
setuint32(Ctxt, ls, unit_length_offset, uint32(ls.Size-unitstart))
|
|
|
|
|
setuint32(Ctxt, ls, header_length_offset, uint32(headerend-headerstart))
|
|
|
|
|
|
|
|
|
|
return ls
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Emit .debug_frame
|
|
|
|
|
*/
|
|
|
|
|
const (
|
|
|
|
|
CIERESERVE = 16
|
|
|
|
|
DATAALIGNMENTFACTOR = -4
|
|
|
|
|
)
|
|
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
// appendPCDeltaCFA appends per-PC CFA deltas to b and returns the final slice.
|
|
|
|
|
func appendPCDeltaCFA(b []byte, deltapc, cfa int64) []byte {
|
|
|
|
|
b = append(b, DW_CFA_def_cfa_offset_sf)
|
|
|
|
|
b = appendSleb128(b, cfa/DATAALIGNMENTFACTOR)
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case deltapc < 0x40:
|
|
|
|
|
b = append(b, uint8(DW_CFA_advance_loc+deltapc))
|
|
|
|
|
case deltapc < 0x100:
|
|
|
|
|
b = append(b, DW_CFA_advance_loc1)
|
|
|
|
|
b = append(b, uint8(deltapc))
|
|
|
|
|
case deltapc < 0x10000:
|
|
|
|
|
b = append(b, DW_CFA_advance_loc2)
|
|
|
|
|
b = Thearch.Append16(b, uint16(deltapc))
|
|
|
|
|
default:
|
|
|
|
|
b = append(b, DW_CFA_advance_loc4)
|
|
|
|
|
b = Thearch.Append32(b, uint32(deltapc))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-02-29 13:07:50 -09:00
|
|
|
return b
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func writeframes(prev *LSym) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if framesec == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
framesec = Linklookup(Ctxt, ".debug_frame", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
framesec.Type = obj.SDWARFSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
framesec.R = framesec.R[:0]
|
2016-03-14 09:23:04 -07:00
|
|
|
fs := framesec
|
|
|
|
|
prev.Next = fs
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Emit the CIE, Section 6.4.1
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, fs, CIERESERVE) // initial length, must be multiple of thearch.ptrsize
|
|
|
|
|
Adduint32(Ctxt, fs, 0xffffffff) // cid.
|
|
|
|
|
Adduint8(Ctxt, fs, 3) // dwarf version (appendix F)
|
|
|
|
|
Adduint8(Ctxt, fs, 0) // augmentation ""
|
|
|
|
|
uleb128put(fs, 1) // code_alignment_factor
|
|
|
|
|
sleb128put(fs, DATAALIGNMENTFACTOR) // guess
|
|
|
|
|
uleb128put(fs, int64(Thearch.Dwarfreglr)) // return_address_register
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, fs, DW_CFA_def_cfa)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(fs, int64(Thearch.Dwarfregsp)) // register SP (**ABI-dependent, defined in l.h)
|
2015-05-07 00:48:09 -04:00
|
|
|
if haslinkregister() {
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(fs, int64(0)) // offset
|
2015-05-07 00:48:09 -04:00
|
|
|
} else {
|
2016-04-06 12:01:40 -07:00
|
|
|
uleb128put(fs, int64(SysArch.PtrSize)) // offset
|
2015-05-07 00:48:09 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, fs, DW_CFA_offset_extended)
|
|
|
|
|
uleb128put(fs, int64(Thearch.Dwarfreglr)) // return address
|
2015-05-07 00:48:09 -04:00
|
|
|
if haslinkregister() {
|
2016-03-14 09:23:04 -07:00
|
|
|
uleb128put(fs, int64(0)/DATAALIGNMENTFACTOR) // at cfa - 0
|
2015-05-07 00:48:09 -04:00
|
|
|
} else {
|
2016-04-06 12:01:40 -07:00
|
|
|
uleb128put(fs, int64(-SysArch.PtrSize)/DATAALIGNMENTFACTOR) // at cfa - x*4
|
2015-05-07 00:48:09 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// 4 is to exclude the length field.
|
2016-03-14 09:23:04 -07:00
|
|
|
pad := CIERESERVE + 4 - fs.Size
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if pad < 0 {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("dwarf: CIERESERVE too small by %d bytes.", -pad)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Addbytes(Ctxt, fs, zeros[:pad])
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
var deltaBuf []byte
|
2015-03-02 12:35:15 -05:00
|
|
|
var pcsp Pciter
|
2015-02-27 22:57:28 -05:00
|
|
|
for Ctxt.Cursym = Ctxt.Textp; Ctxt.Cursym != nil; Ctxt.Cursym = Ctxt.Cursym.Next {
|
2015-10-10 10:57:35 +00:00
|
|
|
s := Ctxt.Cursym
|
2015-02-27 22:57:28 -05:00
|
|
|
if s.Pcln == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:07:50 -09:00
|
|
|
// Emit a FDE, Section 6.4.1.
|
|
|
|
|
// First build the section contents into a byte buffer.
|
|
|
|
|
deltaBuf = deltaBuf[:0]
|
2015-02-27 22:57:28 -05:00
|
|
|
for pciterinit(Ctxt, &pcsp, &s.Pcln.Pcsp); pcsp.done == 0; pciternext(&pcsp) {
|
2015-10-10 10:57:35 +00:00
|
|
|
nextpc := pcsp.nextpc
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// pciterinit goes up to the end of the function,
|
|
|
|
|
// but DWARF expects us to stop just before the end.
|
|
|
|
|
if int64(nextpc) == s.Size {
|
|
|
|
|
nextpc--
|
|
|
|
|
if nextpc < pcsp.pc {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-07 00:48:09 -04:00
|
|
|
if haslinkregister() {
|
2016-02-29 13:07:50 -09:00
|
|
|
deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(pcsp.value))
|
2015-05-07 00:48:09 -04:00
|
|
|
} else {
|
2016-04-06 12:01:40 -07:00
|
|
|
deltaBuf = appendPCDeltaCFA(deltaBuf, int64(nextpc)-int64(pcsp.pc), int64(SysArch.PtrSize)+int64(pcsp.value))
|
2015-05-07 00:48:09 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-04-06 12:01:40 -07:00
|
|
|
pad := int(Rnd(int64(len(deltaBuf)), int64(SysArch.PtrSize))) - len(deltaBuf)
|
2016-02-29 13:07:50 -09:00
|
|
|
deltaBuf = append(deltaBuf, zeros[:pad]...)
|
|
|
|
|
|
|
|
|
|
// Emit the FDE header, Section 6.4.1.
|
|
|
|
|
// 4 bytes: length, must be multiple of thearch.ptrsize
|
|
|
|
|
// 4 bytes: Pointer to the CIE above, at offset 0
|
|
|
|
|
// ptrsize: initial location
|
|
|
|
|
// ptrsize: address range
|
2016-04-06 12:01:40 -07:00
|
|
|
Adduint32(Ctxt, fs, uint32(4+2*SysArch.PtrSize+len(deltaBuf))) // length (excludes itself)
|
2015-02-27 22:57:28 -05:00
|
|
|
if Linkmode == LinkExternal {
|
2016-03-14 09:23:04 -07:00
|
|
|
adddwarfref(Ctxt, fs, framesec, 4)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, fs, 0) // CIE offset
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
Addaddr(Ctxt, fs, s)
|
|
|
|
|
addrput(fs, s.Size) // address range
|
|
|
|
|
Addbytes(Ctxt, fs, deltaBuf)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
return fs
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Walk DWarfDebugInfoEntries, and emit .debug_info
|
|
|
|
|
*/
|
|
|
|
|
const (
|
|
|
|
|
COMPUNITHEADERSIZE = 4 + 2 + 4 + 1
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func writeinfo(prev *LSym) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if infosec == nil {
|
2016-03-14 09:23:04 -07:00
|
|
|
infosec = Linklookup(Ctxt, ".debug_info", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
infosec.R = infosec.R[:0]
|
2016-03-14 09:23:04 -07:00
|
|
|
infosec.Type = obj.SDWARFINFO
|
|
|
|
|
infosec.Attr |= AttrReachable
|
|
|
|
|
prev.Next, prev = infosec, infosec
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if arangessec == nil {
|
|
|
|
|
arangessec = Linklookup(Ctxt, ".dwarfaranges", 0)
|
|
|
|
|
}
|
|
|
|
|
arangessec.R = arangessec.R[:0]
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for compunit := dwroot.child; compunit != nil; compunit = compunit.link {
|
2016-03-14 09:23:04 -07:00
|
|
|
s := compunit.sym
|
|
|
|
|
prev.Next, prev = s, s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Write .debug_info Compilation Unit Header (sec 7.5.1)
|
|
|
|
|
// Fields marked with (*) must be changed for 64-bit dwarf
|
|
|
|
|
// This must match COMPUNITHEADERSIZE above.
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, 0) // unit_length (*), will be filled in later.
|
|
|
|
|
Adduint16(Ctxt, s, 2) // dwarf version (appendix F)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// debug_abbrev_offset (*)
|
2016-03-14 09:23:04 -07:00
|
|
|
adddwarfref(Ctxt, s, abbrevsym, 4)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(SysArch.PtrSize)) // address_size
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
prev = putdie(prev, compunit)
|
|
|
|
|
cusize := s.Size - 4 // exclude the length field.
|
|
|
|
|
for child := s.Next; child != nil; child = child.Next {
|
|
|
|
|
cusize += child.Size
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
setuint32(Ctxt, s, 0, uint32(cusize))
|
|
|
|
|
newattr(compunit, DW_AT_byte_size, DW_CLS_CONSTANT, int64(cusize), 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
return prev
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Emit .debug_pubnames/_types. _info must have been written before,
|
|
|
|
|
* because we need die->offs and infoo/infosize;
|
|
|
|
|
*/
|
|
|
|
|
func ispubname(die *DWDie) bool {
|
|
|
|
|
switch die.abbrev {
|
2015-04-01 09:38:44 -07:00
|
|
|
case DW_ABRV_FUNCTION, DW_ABRV_VARIABLE:
|
2015-03-02 12:35:15 -05:00
|
|
|
a := getattr(die, DW_AT_external)
|
2015-02-27 22:57:28 -05:00
|
|
|
return a != nil && a.value != 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ispubtype(die *DWDie) bool {
|
|
|
|
|
return die.abbrev >= DW_ABRV_NULLTYPE
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func writepub(sname string, ispub func(*DWDie) bool, prev *LSym) *LSym {
|
|
|
|
|
s := Linklookup(Ctxt, sname, 0)
|
|
|
|
|
s.Type = obj.SDWARFSECT
|
|
|
|
|
prev.Next = s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for compunit := dwroot.child; compunit != nil; compunit = compunit.link {
|
2016-03-14 09:23:04 -07:00
|
|
|
sectionstart := s.Size
|
|
|
|
|
culength := uint32(getattr(compunit, DW_AT_byte_size).value) + 4
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Write .debug_pubnames/types Header (sec 6.1.1)
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, 0) // unit_length (*), will be filled in later.
|
|
|
|
|
Adduint16(Ctxt, s, 2) // dwarf version (appendix F)
|
|
|
|
|
adddwarfref(Ctxt, s, compunit.sym, 4) // debug_info_offset (of the Comp unit Header)
|
|
|
|
|
Adduint32(Ctxt, s, culength) // debug_info_length
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-10-10 10:57:35 +00:00
|
|
|
for die := compunit.child; die != nil; die = die.link {
|
2015-02-27 22:57:28 -05:00
|
|
|
if !ispub(die) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-10-10 10:57:35 +00:00
|
|
|
dwa := getattr(die, DW_AT_name)
|
2016-03-14 09:23:04 -07:00
|
|
|
name := dwa.data.(string)
|
|
|
|
|
if die.sym == nil {
|
|
|
|
|
fmt.Println("Missing sym for ", name)
|
|
|
|
|
}
|
|
|
|
|
adddwarfref(Ctxt, s, die.sym, 4)
|
|
|
|
|
Addstring(s, name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
setuint32(Ctxt, s, sectionstart, uint32(s.Size-sectionstart)-4) // exclude the length field.
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
return s
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* emit .debug_aranges. _info must have been written before,
|
|
|
|
|
* because we need die->offs of dw_globals.
|
|
|
|
|
*/
|
2016-03-14 09:23:04 -07:00
|
|
|
func writearanges(prev *LSym) *LSym {
|
|
|
|
|
s := Linklookup(Ctxt, ".debug_aranges", 0)
|
|
|
|
|
s.Type = obj.SDWARFSECT
|
2016-02-10 16:51:23 -08:00
|
|
|
// The first tuple is aligned to a multiple of the size of a single tuple
|
|
|
|
|
// (twice the size of an address)
|
2016-04-06 12:01:40 -07:00
|
|
|
headersize := int(Rnd(4+2+4+1+1, int64(SysArch.PtrSize*2))) // don't count unit_length field itself
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for compunit := dwroot.child; compunit != nil; compunit = compunit.link {
|
2015-10-10 10:57:35 +00:00
|
|
|
b := getattr(compunit, DW_AT_low_pc)
|
2015-02-27 22:57:28 -05:00
|
|
|
if b == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-10-10 10:57:35 +00:00
|
|
|
e := getattr(compunit, DW_AT_high_pc)
|
2015-02-27 22:57:28 -05:00
|
|
|
if e == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write .debug_aranges Header + entry (sec 6.1.2)
|
2016-04-06 12:01:40 -07:00
|
|
|
unitlength := uint32(headersize) + 4*uint32(SysArch.PtrSize) - 4
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint32(Ctxt, s, unitlength) // unit_length (*)
|
|
|
|
|
Adduint16(Ctxt, s, 2) // dwarf version (appendix F)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
adddwarfref(Ctxt, s, compunit.sym, 4)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
Adduint8(Ctxt, s, uint8(SysArch.PtrSize)) // address_size
|
2016-03-14 09:23:04 -07:00
|
|
|
Adduint8(Ctxt, s, 0) // segment_size
|
|
|
|
|
padding := headersize - (4 + 2 + 4 + 1 + 1)
|
|
|
|
|
for i := 0; i < padding; i++ {
|
|
|
|
|
Adduint8(Ctxt, s, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Addaddrplus(Ctxt, s, b.data.(*LSym), b.value-(b.data.(*LSym)).Value)
|
|
|
|
|
addrput(s, e.value-b.value)
|
|
|
|
|
addrput(s, 0)
|
|
|
|
|
addrput(s, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
if s.Size > 0 {
|
|
|
|
|
prev.Next = s
|
|
|
|
|
prev = s
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
return prev
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
func writegdbscript(prev *LSym) *LSym {
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
if gdbscript != "" {
|
|
|
|
|
s := Linklookup(Ctxt, ".debug_gdb_scripts", 0)
|
|
|
|
|
s.Type = obj.SDWARFSECT
|
|
|
|
|
prev.Next = s
|
|
|
|
|
prev = s
|
|
|
|
|
Adduint8(Ctxt, s, 1) // magic 1 byte?
|
|
|
|
|
Addstring(s, gdbscript)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
return prev
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
var prototypedies map[string]*DWDie
|
2015-04-08 12:55:34 -07:00
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
/*
|
|
|
|
|
* This is the main entry point for generating dwarf. After emitting
|
|
|
|
|
* the mandatory debug_abbrev section, it calls writelines() to set up
|
|
|
|
|
* the per-compilation unit part of the DIE tree, while simultaneously
|
|
|
|
|
* emitting the debug_line section. When the final tree contains
|
|
|
|
|
* forward references, it will write the debug_info section in 2
|
|
|
|
|
* passes.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2016-03-14 09:23:04 -07:00
|
|
|
func dwarfgeneratedebugsyms() {
|
2015-02-27 22:57:28 -05:00
|
|
|
if Debug['w'] != 0 { // disable dwarf
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-04-07 14:00:00 -04:00
|
|
|
if Debug['s'] != 0 && HEADTYPE != obj.Hdarwin {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
if HEADTYPE == obj.Hplan9 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-08 12:55:34 -07:00
|
|
|
if Linkmode == LinkExternal {
|
|
|
|
|
if !Iself && HEADTYPE != obj.Hdarwin {
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
2015-04-08 12:55:34 -07:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
if Debug['v'] != 0 {
|
|
|
|
|
fmt.Fprintf(&Bso, "%5.2f dwarf\n", obj.Cputime())
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For diagnostic messages.
|
|
|
|
|
newattr(&dwtypes, DW_AT_name, DW_CLS_STRING, int64(len("dwtypes")), "dwtypes")
|
|
|
|
|
|
|
|
|
|
// Some types that must exist to define other ones.
|
2016-03-14 09:23:04 -07:00
|
|
|
newdie(&dwtypes, DW_ABRV_NULLTYPE, "<unspecified>", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
newdie(&dwtypes, DW_ABRV_NULLTYPE, "void", 0)
|
|
|
|
|
newdie(&dwtypes, DW_ABRV_BARE_PTRTYPE, "unsafe.Pointer", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
die := newdie(&dwtypes, DW_ABRV_BASETYPE, "uintptr", 0) // needed for array size
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_encoding, DW_CLS_CONSTANT, DW_ATE_unsigned, 0)
|
2016-04-06 12:01:40 -07:00
|
|
|
newattr(die, DW_AT_byte_size, DW_CLS_CONSTANT, int64(SysArch.PtrSize), 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
newattr(die, DW_AT_go_kind, DW_CLS_CONSTANT, obj.KindUintptr, 0)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
// Prototypes needed for type synthesis.
|
|
|
|
|
prototypedies = map[string]*DWDie{
|
|
|
|
|
"type.runtime.stringStructDWARF": nil,
|
|
|
|
|
"type.runtime.slice": nil,
|
|
|
|
|
"type.runtime.hmap": nil,
|
|
|
|
|
"type.runtime.bmap": nil,
|
|
|
|
|
"type.runtime.sudog": nil,
|
|
|
|
|
"type.runtime.waitq": nil,
|
|
|
|
|
"type.runtime.hchan": nil,
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// Needed by the prettyprinter code for interface inspection.
|
|
|
|
|
defgotype(lookup_or_diag("type.runtime._type"))
|
|
|
|
|
|
|
|
|
|
defgotype(lookup_or_diag("type.runtime.interfacetype"))
|
|
|
|
|
defgotype(lookup_or_diag("type.runtime.itab"))
|
|
|
|
|
|
|
|
|
|
genasmsym(defdwsymb)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
dwarfp = writeabbrev()
|
|
|
|
|
last := dwarfp
|
|
|
|
|
last = writelines(last)
|
|
|
|
|
last = writeframes(last)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
synthesizestringtypes(dwtypes.child)
|
|
|
|
|
synthesizeslicetypes(dwtypes.child)
|
|
|
|
|
synthesizemaptypes(dwtypes.child)
|
|
|
|
|
synthesizechantypes(dwtypes.child)
|
|
|
|
|
|
|
|
|
|
reversetree(&dwroot.child)
|
|
|
|
|
reversetree(&dwtypes.child)
|
|
|
|
|
reversetree(&dwglobals.child)
|
|
|
|
|
|
|
|
|
|
movetomodule(&dwtypes)
|
|
|
|
|
movetomodule(&dwglobals)
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
// Need to reorder symbols so SDWARFINFO is after all SDWARFSECT
|
|
|
|
|
// (but we need to generate dies before writepub)
|
|
|
|
|
writeinfo(last)
|
|
|
|
|
infosyms := last.Next
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
last = writepub(".debug_pubnames", ispubname, last)
|
|
|
|
|
last = writepub(".debug_pubtypes", ispubtype, last)
|
|
|
|
|
last = writearanges(last)
|
|
|
|
|
last = writegdbscript(last)
|
|
|
|
|
last.Next = infosyms
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Elf.
|
|
|
|
|
*/
|
|
|
|
|
func dwarfaddshstrings(shstrtab *LSym) {
|
|
|
|
|
if Debug['w'] != 0 { // disable dwarf
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
Addstring(shstrtab, ".debug_abbrev")
|
|
|
|
|
Addstring(shstrtab, ".debug_aranges")
|
|
|
|
|
Addstring(shstrtab, ".debug_frame")
|
|
|
|
|
Addstring(shstrtab, ".debug_info")
|
|
|
|
|
Addstring(shstrtab, ".debug_line")
|
|
|
|
|
Addstring(shstrtab, ".debug_pubnames")
|
|
|
|
|
Addstring(shstrtab, ".debug_pubtypes")
|
|
|
|
|
Addstring(shstrtab, ".debug_gdb_scripts")
|
2015-02-27 22:57:28 -05:00
|
|
|
if Linkmode == LinkExternal {
|
2016-03-14 09:23:04 -07:00
|
|
|
Addstring(shstrtab, elfRelType+".debug_info")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".debug_aranges")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".debug_line")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".debug_frame")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".debug_pubnames")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".debug_pubtypes")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 09:23:04 -07:00
|
|
|
// Add section symbols for DWARF debug info. This is called before
|
2015-02-27 22:57:28 -05:00
|
|
|
// dwarfaddelfheaders.
|
|
|
|
|
func dwarfaddelfsectionsyms() {
|
|
|
|
|
if Debug['w'] != 0 { // disable dwarf
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-04-08 12:55:34 -07:00
|
|
|
if Linkmode != LinkExternal {
|
2016-03-14 09:23:04 -07:00
|
|
|
return
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
sym := Linklookup(Ctxt, ".debug_info", 0)
|
|
|
|
|
putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
|
|
|
|
|
sym = Linklookup(Ctxt, ".debug_abbrev", 0)
|
|
|
|
|
putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
|
|
|
|
|
sym = Linklookup(Ctxt, ".debug_line", 0)
|
|
|
|
|
putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
|
|
|
|
|
sym = Linklookup(Ctxt, ".debug_frame", 0)
|
|
|
|
|
putelfsectionsym(sym, sym.Sect.Elfsect.shnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Windows PE
|
|
|
|
|
*/
|
|
|
|
|
func dwarfaddpeheaders() {
|
|
|
|
|
if Debug['w'] != 0 { // disable dwarf
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-03-14 09:23:04 -07:00
|
|
|
for sect := Segdwarf.Sect; sect != nil; sect = sect.Next {
|
|
|
|
|
h := newPEDWARFSection(sect.Name, int64(sect.Length))
|
|
|
|
|
fileoff := sect.Vaddr - Segdwarf.Vaddr + Segdwarf.Fileoff
|
|
|
|
|
if uint64(h.PointerToRawData) != fileoff {
|
|
|
|
|
Diag("%s.PointerToRawData = %#x, want %#x", sect.Name, h.PointerToRawData, fileoff)
|
|
|
|
|
errorexit()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|