github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/cmd/ld/decodesym.c (about)

     1  // Copyright 2012 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  #include	"l.h"
     6  #include	"lib.h"
     7  #include	"../../pkg/runtime/typekind.h"
     8  
     9  // Decoding the type.* symbols.	 This has to be in sync with
    10  // ../../pkg/runtime/type.go, or more specificaly, with what
    11  // ../gc/reflect.c stuffs in these.
    12  
    13  static Reloc*
    14  decode_reloc(Sym *s, int32 off)
    15  {
    16  	int i;
    17  
    18  	for (i = 0; i < s->nr; i++)
    19  		if (s->r[i].off == off)
    20  			return s->r + i;
    21  	return nil;
    22  }
    23  
    24  static Sym*
    25  decode_reloc_sym(Sym *s, int32 off)
    26  {
    27  	Reloc *r;
    28  
    29  	r = decode_reloc(s,off);
    30  	if (r == nil)
    31  		return nil;
    32  	return r->sym;
    33  }
    34  
    35  static uvlong
    36  decode_inuxi(uchar* p, int sz)
    37  {
    38  	uint64 v;
    39  	uint32 l;
    40  	uchar *cast, *inuxi;
    41  	int i;
    42  
    43  	v = l = 0;
    44  	cast = nil;
    45  	inuxi = nil;
    46  	switch (sz) {
    47  	case 2:
    48  		cast = (uchar*)&l;
    49  		inuxi = inuxi2;
    50  		break;
    51  	case 4:
    52  		cast = (uchar*)&l;
    53  		inuxi = inuxi4;
    54  		break;
    55  	case 8:
    56  		cast = (uchar*)&v;
    57  		inuxi = inuxi8;
    58  		break;
    59  	default:
    60  		diag("dwarf: decode inuxi %d", sz);
    61  		errorexit();
    62  	}
    63  	for (i = 0; i < sz; i++)
    64  		cast[inuxi[i]] = p[i];
    65  	if (sz == 8)
    66  		return v;
    67  	return l;
    68  }
    69  
    70  // Type.commonType.kind
    71  uint8
    72  decodetype_kind(Sym *s)
    73  {
    74  	return s->p[1*PtrSize + 7] & ~KindNoPointers;	//  0x13 / 0x1f
    75  }
    76  
    77  // Type.commonType.size
    78  vlong
    79  decodetype_size(Sym *s)
    80  {
    81  	return decode_inuxi(s->p, PtrSize);	 // 0x8 / 0x10
    82  }
    83  
    84  // Type.commonType.gc
    85  Sym*
    86  decodetype_gc(Sym *s)
    87  {
    88  	return decode_reloc_sym(s, 1*PtrSize + 8 + 1*PtrSize);
    89  }
    90  
    91  // Type.ArrayType.elem and Type.SliceType.Elem
    92  Sym*
    93  decodetype_arrayelem(Sym *s)
    94  {
    95  	return decode_reloc_sym(s, CommonSize);	// 0x1c / 0x30
    96  }
    97  
    98  vlong
    99  decodetype_arraylen(Sym *s)
   100  {
   101  	return decode_inuxi(s->p + CommonSize+PtrSize, PtrSize);
   102  }
   103  
   104  // Type.PtrType.elem
   105  Sym*
   106  decodetype_ptrelem(Sym *s)
   107  {
   108  	return decode_reloc_sym(s, CommonSize);	// 0x1c / 0x30
   109  }
   110  
   111  // Type.MapType.key, elem
   112  Sym*
   113  decodetype_mapkey(Sym *s)
   114  {
   115  	return decode_reloc_sym(s, CommonSize);	// 0x1c / 0x30
   116  }
   117  Sym*
   118  decodetype_mapvalue(Sym *s)
   119  {
   120  	return decode_reloc_sym(s, CommonSize+PtrSize);	// 0x20 / 0x38
   121  }
   122  
   123  // Type.ChanType.elem
   124  Sym*
   125  decodetype_chanelem(Sym *s)
   126  {
   127  	return decode_reloc_sym(s, CommonSize);	// 0x1c / 0x30
   128  }
   129  
   130  // Type.FuncType.dotdotdot
   131  int
   132  decodetype_funcdotdotdot(Sym *s)
   133  {
   134  	return s->p[CommonSize];
   135  }
   136  
   137  // Type.FuncType.in.len
   138  int
   139  decodetype_funcincount(Sym *s)
   140  {
   141  	return decode_inuxi(s->p + CommonSize+2*PtrSize, IntSize);
   142  }
   143  
   144  int
   145  decodetype_funcoutcount(Sym *s)
   146  {
   147  	return decode_inuxi(s->p + CommonSize+3*PtrSize + 2*IntSize, IntSize);
   148  }
   149  
   150  Sym*
   151  decodetype_funcintype(Sym *s, int i)
   152  {
   153  	Reloc *r;
   154  
   155  	r = decode_reloc(s, CommonSize + PtrSize);
   156  	if (r == nil)
   157  		return nil;
   158  	return decode_reloc_sym(r->sym, r->add + i * PtrSize);
   159  }
   160  
   161  Sym*
   162  decodetype_funcouttype(Sym *s, int i)
   163  {
   164  	Reloc *r;
   165  
   166  	r = decode_reloc(s, CommonSize + 2*PtrSize + 2*IntSize);
   167  	if (r == nil)
   168  		return nil;
   169  	return decode_reloc_sym(r->sym, r->add + i * PtrSize);
   170  }
   171  
   172  // Type.StructType.fields.Slice::len
   173  int
   174  decodetype_structfieldcount(Sym *s)
   175  {
   176  	return decode_inuxi(s->p + CommonSize + PtrSize, IntSize);
   177  }
   178  
   179  enum {
   180  	StructFieldSize = 5*PtrSize
   181  };
   182  // Type.StructType.fields[]-> name, typ and offset.
   183  char*
   184  decodetype_structfieldname(Sym *s, int i)
   185  {
   186  	Reloc *r;
   187  
   188  	// go.string."foo"  0x28 / 0x40
   189  	s = decode_reloc_sym(s, CommonSize + PtrSize + 2*IntSize + i*StructFieldSize);
   190  	if (s == nil)			// embedded structs have a nil name.
   191  		return nil;
   192  	r = decode_reloc(s, 0);		// s has a pointer to the string data at offset 0
   193  	if (r == nil)			// shouldn't happen.
   194  		return nil;
   195  	return (char*) r->sym->p + r->add;	// the c-string
   196  }
   197  
   198  Sym*
   199  decodetype_structfieldtype(Sym *s, int i)
   200  {
   201  	return decode_reloc_sym(s, CommonSize + PtrSize + 2*IntSize + i*StructFieldSize + 2*PtrSize);
   202  }
   203  
   204  vlong
   205  decodetype_structfieldoffs(Sym *s, int i)
   206  {
   207  	return decode_inuxi(s->p + CommonSize + PtrSize + 2*IntSize + i*StructFieldSize + 4*PtrSize, IntSize);
   208  }
   209  
   210  // InterfaceTYpe.methods.len
   211  vlong
   212  decodetype_ifacemethodcount(Sym *s)
   213  {
   214  	return decode_inuxi(s->p + CommonSize + PtrSize, IntSize);
   215  }