560
650
/* Read the contents of the File Name table. This produces a bunch
561
651
of file names, and for each, an index to the corresponding
562
direcory name entry. */
652
directory name entry. */
653
if (si->ddump_line) {
654
VG_(printf)(" The File Name Table:\n");
655
VG_(printf)(" Entry Dir Time Size Name\n");
563
659
while (* data != 0) {
565
661
Int bytes_read, diridx;
662
Int uu_time, uu_size; /* unused, and a guess */
567
664
data += VG_(strlen) ((Char *) data) + 1;
569
666
diridx = read_leb128 (data, & bytes_read, 0);
570
667
data += bytes_read;
571
read_leb128 (data, & bytes_read, 0);
668
uu_time = read_leb128 (data, & bytes_read, 0);
572
669
data += bytes_read;
573
read_leb128 (data, & bytes_read, 0);
670
uu_size = read_leb128 (data, & bytes_read, 0);
574
671
data += bytes_read;
576
673
addto_WordArray( &filenames, (Word)ML_(addStr)(si,name,-1) );
577
674
addto_WordArray( &fnidx2dir, (Word)diridx );
578
675
if (0) VG_(printf)("file %s diridx %d\n", name, diridx );
677
VG_(printf)(" %d\t%d\t%d\t%d\t%s\n",
678
i, diridx, uu_time, uu_size, name);
580
685
if (*data != 0) {
581
686
ML_(symerr)("can't find NUL at end of DWARF2 file name table");
692
VG_(printf)(" Line Number Statements:\n");
586
694
/* Now display the statements. */
588
696
while (data < end_of_sequence) {
1349
1559
8 is the return address (EIP) */
1351
/* Note that we don't support DWARF3 expressions (DW_CFA_expression,
1352
DW_CFA_def_cfa_expression, DW_CFA_val_expression). The code just
1353
reads over them and ignores them.
1355
Note also, does not support the 64-bit DWARF format (only known
1356
compiler that generates it so far is IBM's xlc/xlC/xlf suite).
1357
Only handles 32-bit DWARF.
1562
/* Comments re DW_CFA_set_loc, 16 Nov 06.
1565
Someone recently sent me a libcrypto.so.0.9.8 as distributed with
1566
Ubuntu of some flavour, compiled with gcc 4.1.2 on amd64. It
1567
causes V's CF reader to complain a lot:
1569
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1570
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1571
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1572
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1573
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:48
1574
>> --19976-- DWARF2 CFI reader: unhandled CFI instruction 0:24
1576
After chasing this around a bit it seems that the CF bytecode
1577
parser lost sync at a DW_CFA_set_loc, which has a single argument
1578
denoting an address.
1580
As it stands that address is extracted by read_Addr(). On amd64
1581
that just fetches 8 bytes regardless of anything else.
1583
read_encoded_Addr() is more sophisticated. This appears to take
1584
into account some kind of encoding flag. When I replace the uses
1585
of read_Addr by read_encoded_Addr for DW_CFA_set_loc, the
1586
complaints go away, there is no loss of sync, and the parsed CF
1587
instructions are the same as shown by readelf --debug-dump=frames.
1589
So it seems a plausible fix. The problem is I looked in the DWARF3
1590
spec and completely failed to figure out whether or not the arg to
1591
DW_CFA_set_loc is supposed to be encoded in a way suitable for
1592
read_encoded_Addr, nor for that matter any description of what it
1593
is that read_encoded_Addr is really decoding.
1596
The problem is that the encoding is not standard - the eh_frame
1597
section uses the same encoding as the dwarf_frame section except
1598
for a few small changes, and this is one of them. So this is not
1599
something the DWARF standard covers.
1601
There is an augmentation string to indicate what is going on though
1602
so that programs can recognise it.
1604
What we are doing seems to match what gdb 6.5 and libdwarf 20060614
1605
do though. I'm not sure about readelf though.
1607
(later): Well dwarfdump barfs on it:
1609
dwarfdump ERROR: dwarf_get_fde_info_for_reg:
1610
DW_DLE_DF_FRAME_DECODING_ERROR(193) (193)
1612
I've looked at binutils as well now, and the code in readelf agrees
1613
with your patch - ie it treats set_loc as having an encoded address
1614
if there is a zR augmentation indicating an encoding.
1616
Quite why gdb and libdwarf don't understand this is an interesting
1619
Final outcome: all uses of read_Addr were replaced by
1620
read_encoded_Addr. A new type AddressDecodingInfo was added to
1621
make it relatively clean to plumb through the extra info needed by
1625
/* More badness re address encoding, 12 Jan 07.
1627
Most gcc provided CIEs have a "zR" augmentation, which means they
1628
supply their own address encoding, and that works fine. However,
1629
some icc9 supplied CIEs have no augmentation, which means they use
1630
the default_Addr_encoding(). That says to use a machine-word sized
1631
value, literally unmodified.
1633
Since .so's are, in general, relocated when loaded, having absolute
1634
addresses in the CFI data makes no sense when read_encoded_Addr is
1635
used to find the initial location for a FDE. The resulting saga:
1638
> I'm chasing a stack backtrace failure for an amd64 .so which was
1639
> created I believe by icc 9.1. After a while I wound up looking at
1640
> this: (readdwarf.c)
1642
> 5083 tom static UChar default_Addr_encoding ( void )
1644
> 3584 tom switch (sizeof(Addr)) {
1645
> 3584 tom case 4: return DW_EH_PE_udata4;
1646
> 3584 tom case 8: return DW_EH_PE_udata8;
1647
> 3584 tom default: vg_assert(0);
1651
> If a CIE does not have an "augmentation string" (typically "zR") then
1652
> addresses are decoded as described by default_Addr_encoding. If there
1653
> is an 'R' in the augmentation string then the encoding to use
1654
> is specified by the CIE itself, which works fine with GCC compiled code
1655
> since that always appears to specify zR.
1659
> Problem is this .so has no augmentation string and so uses the
1660
> default encoding, viz DW_EH_PE_udata8. That appears to mean
1661
> "read a 64 bit number" and use that as-is (for the starting value
1662
> of the program counter when running the CFA program).
1664
Strictly speaking the default is DW_EH_PE_absptr, but that amounts
1665
to either udata4 or udata8 depending on the platform's pointer size
1666
which is a shortcut I used.
1668
> For this .so that gives nonsense (very small) PCs which are later
1669
> rejected by the sanity check which ensures PC ranges fall inside
1670
> the mapped text segment. It seems like the .so expects to have the
1671
> start VMA of the text segment added on. This would correspond to
1673
> static UChar default_Addr_encoding ( void )
1675
> switch (sizeof(Addr)) {
1676
> case 4: return DW_EH_PE_textrel + DW_EH_PE_udata4;
1677
> case 8: return DW_EH_PE_textrel + DW_EH_PE_udata8;
1678
> default: vg_assert(0);
1682
The problem you're seeing is that you have absolute pointers inside
1683
a shared library, which obviously makes little sense on the face of
1684
things as how would the linker know where the library will be
1687
The answer of course is that it doesn't, so if it points absolute
1688
pointers in the frame unwind data is has to include relocations for
1689
them, and I'm betting that if you look at the relocations in the
1690
library you will there are some for that data.
1692
That is fine of course when ld.so maps the library - it will
1693
relocate the eh_frame data as it maps it (or prelinking will
1694
already have done so) and when the g++ exception code kicks in and
1695
unwinds the stack it will see relocated data.
1697
We of course are mapping the section from the ELF file ourselves
1698
and are not applying the relocations, hence the problem you are
1701
Strictly speaking we should apply the relocations but the cheap
1702
solution is essentially to do what you've done - strictly speaking
1703
you should adjust by the difference between the address the library
1704
was linked for and the address it has been loaded at, but a shared
1705
library will normally be linked for address zero I believe. It's
1706
possible that prelinking might change that though?
1709
That all syncs with what I am seeing.
1711
So what I am inclined to do is:
1713
- Leave default_Addr_encoding as it is
1715
- Change read_encoded_Addr's handling of "case DW_EH_PE_absptr" so
1716
it sets base to, as you say, the difference between the address
1717
the library was linked for and the address it has been loaded at
1718
(== the SegInfo's text_bias)
1720
Does that sound sane? I think it should even handle the prelinked
1725
Hmm. Plausible as it sounds, it doesn't work. It now produces
1726
bogus backtraces for locations inside the (statically linked)
1727
memcheck executable.
1729
Besides, there are a couple of other places where read_encoded_Addr
1730
is used -- one of which is used to establish the length of the
1731
address range covered by the current FDE:
1733
fde_arange = read_encoded_Addr(&nbytes, &adi, data);
1735
and it doesn't seem to make any sense for read_encoded_Addr to add
1736
on the text segment bias in that context. The DWARF3 spec says
1737
that both the initial_location and address_range (length) fields
1738
are encoded the same way ("target address"), so it is unclear at
1739
what stage in the process it would be appropriate to relocate the
1740
former but not the latter.
1742
One unprincipled kludge that does work is the following: just
1743
before handing one of the address range fragments off to
1744
ML_(addDiCfSI) for permanent storage, check its start address. If
1745
that is very low (less than 2 M), and is far below the mapped text
1746
segment, and adding the text bias would move the fragment entirely
1747
inside the mapped text segment, then do so. A kind of kludged
1748
last-minute relocation, if you like.
1750
12 Jan 07: committing said kludge (see kludge_then_addDiCfSI). If
1751
the situation clarifies, it can easily enough be backed out and
1752
replaced by a better fix.
1360
1755
/* --------------- Decls --------------- */
1449
1844
The result is then summarised into a sequence of CfiSIs, if
1450
1845
possible. UnwindContext effectively holds the state of the
1451
1846
abstract machine whilst it is running.
1848
The CFA can either be a signed offset from a register,
1851
CFA = cfa_reg + cfa_off when UnwindContext.cfa_is_regoff==True
1854
When .cfa_is_regoff == True, cfa_expr_id must be zero
1855
When .cfa_is_regoff == False, cfa_reg must be zero
1856
and cfa_off must be zero
1858
RegRule describes, for each register, how to get its
1859
value in the previous frame, where 'cfa' denotes the cfa
1860
for the frame as a whole:
1862
RegRule = RR_Undef -- undefined
1863
| RR_Same -- same as in previous frame
1864
| RR_CFAOff arg -- is at * ( cfa + arg )
1865
| RR_CFAValOff arg -- is ( cfa + arg )
1866
| RR_Reg arg -- is in register 'arg'
1867
| RR_Expr arg -- is at * [[ arg ]]
1868
| RR_ValExpr arg -- is [[ arg ]]
1871
Note that RR_Expr is redundant since the same can be represented
1872
using RR_ValExpr with an explicit dereference (CfiExpr_Deref) at
1873
the outermost level.
1875
All expressions are stored in exprs in the containing
1876
UnwindContext. Since the UnwindContext gets reinitialised for each
1877
new FDE, summarise_context needs to copy out any expressions it
1878
wants to keep into the cfsi_exprs field of the containing SegInfo.
1455
enum { RR_Undef, RR_Same, RR_CFAoff, RR_Reg, RR_Arch, RR_Expr,
1456
RR_CFAValoff, RR_ValExpr } tag;
1458
/* Note, .coff and .reg are never both in use. Therefore could
1459
merge them into one. */
1461
/* CFA offset if tag==RR_CFAoff */
1464
/* reg, if tag==RR_Reg */
1882
enum { RR_Undef, RR_Same, RR_CFAOff, RR_CFAValOff,
1883
RR_Reg, /*RR_Expr,*/ RR_ValExpr, RR_Arch } tag;
1884
/* meaning: int offset for CFAoff/CFAValOff
1886
expr index for Expr/ValExpr */
1469
static void ppRegRule ( RegRule* reg )
1891
static void ppRegRule ( XArray* exprs, RegRule* rrule )
1472
case RR_Undef: VG_(printf)("u "); break;
1473
case RR_Same: VG_(printf)("s "); break;
1474
case RR_CFAoff: VG_(printf)("c%d ", reg->coff); break;
1475
case RR_CFAValoff: VG_(printf)("v%d ", reg->coff); break;
1476
case RR_Reg: VG_(printf)("r%d ", reg->reg); break;
1477
case RR_Arch: VG_(printf)("a "); break;
1478
case RR_Expr: VG_(printf)("e "); break;
1479
case RR_ValExpr: VG_(printf)("ve "); break;
1480
default: VG_(core_panic)("ppRegRule");
1894
switch (rrule->tag) {
1895
case RR_Undef: VG_(printf)("u "); break;
1896
case RR_Same: VG_(printf)("s "); break;
1897
case RR_CFAOff: VG_(printf)("c%d ", rrule->arg); break;
1898
case RR_CFAValOff: VG_(printf)("v%d ", rrule->arg); break;
1899
case RR_Reg: VG_(printf)("r%d ", rrule->arg); break;
1900
case RR_ValExpr: VG_(printf)("ve{");
1901
ML_(ppCfiExpr)( exprs, rrule->arg );
1904
case RR_Arch: VG_(printf)("a "); break;
1905
default: VG_(core_panic)("ppRegRule");
1507
1936
VG_(printf)("0x%llx: ", (ULong)ctx->loc);
1508
VG_(printf)("%d(r%d) ", ctx->cfa_offset, ctx->cfa_reg);
1937
if (ctx->cfa_is_regoff) {
1938
VG_(printf)("%d(r%d) ", ctx->cfa_off, ctx->cfa_reg);
1940
vg_assert(ctx->exprs);
1942
ML_(ppCfiExpr)( ctx->exprs, ctx->cfa_expr_ix );
1509
1945
for (i = 0; i < N_CFI_REGS; i++)
1510
ppRegRule(&ctx->reg[i]);
1946
ppRegRule(ctx->exprs, &ctx->reg[i]);
1511
1947
VG_(printf)("\n");
1950
static void* symtab_alloc ( SizeT szB ) {
1951
return VG_(arena_malloc)( VG_AR_SYMTAB, szB );
1953
static void symtab_free ( void* v ) {
1954
VG_(arena_free)( VG_AR_SYMTAB, v );
1514
1957
static void initUnwindContext ( /*OUT*/UnwindContext* ctx )
1520
ctx->ra_reg = RA_REG_DEFAULT;
1523
ctx->cfa_offset = 0;
1963
ctx->ra_reg = RA_REG_DEFAULT;
1965
ctx->cfa_is_regoff = True;
1968
ctx->cfa_expr_ix = 0;
1524
1970
for (i = 0; i < N_CFI_REGS; i++) {
1525
1971
ctx->reg[i].tag = RR_Undef;
1526
ctx->reg[i].coff = 0;
1527
ctx->reg[i].reg = 0;
1972
ctx->reg[i].arg = 0;
1977
/* A structure which holds information needed by read_encoded_Addr().
1982
UChar* ehframe_image;
1986
AddressDecodingInfo;
1532
1989
/* ------------ Deal with summary-info records ------------ */
1534
1991
static void initCfiSI ( DiCfSI* si )
1538
si->cfa_sprel = False;
1549
2006
/* --------------- Summarisation --------------- */
2010
Int copy_convert_CfiExpr_tree ( XArray* dst,
2011
UnwindContext* srcuc,
1551
2014
/* Summarise ctx into si, if possible. Returns True if successful.
1552
2015
This is taken to be just after ctx's loc advances; hence the
1553
2016
summary is up to but not including the current loc. This works
1556
2019
static Bool summarise_context( /*OUT*/DiCfSI* si,
1557
2020
Addr loc_start,
1558
UnwindContext* ctx )
2022
struct _SegInfo* seginfo )
1563
2027
/* How to generate the CFA */
1564
if (ctx->cfa_reg == -1) {
1565
/* it was set by DW_CFA_def_cfa_expression; we don't know what
1570
if (ctx->cfa_reg == SP_REG) {
1571
si->cfa_sprel = True;
1572
si->cfa_off = ctx->cfa_offset;
1574
if (ctx->cfa_reg == FP_REG) {
1575
si->cfa_sprel = False;
1576
si->cfa_off = ctx->cfa_offset;
2028
if (!ctx->cfa_is_regoff) {
2029
/* it was set by DW_CFA_def_cfa_expression; try to convert */
2033
dst = seginfo->cfsi_exprs;
2034
if (src && (VG_(sizeXA)(src) > 0) && (!dst)) {
2035
dst = VG_(newXA)( symtab_alloc, symtab_free,
2038
seginfo->cfsi_exprs = dst;
2040
conv = copy_convert_CfiExpr_tree
2041
( dst, ctx, ctx->cfa_expr_ix );
2042
vg_assert(conv >= -1);
2043
if (conv == -1) { why = 6; goto failed; }
2044
si->cfa_how = CFIC_EXPR;
2046
if (0 && seginfo->ddump_frames)
2047
ML_(ppCfiExpr)(dst, conv);
2049
if (ctx->cfa_is_regoff && ctx->cfa_reg == SP_REG) {
2050
si->cfa_how = CFIC_SPREL;
2051
si->cfa_off = ctx->cfa_off;
2053
if (ctx->cfa_is_regoff && ctx->cfa_reg == FP_REG) {
2054
si->cfa_how = CFIC_FPREL;
2055
si->cfa_off = ctx->cfa_off;
1582
# define SUMMARISE_HOW(_how, _off, _ctxreg) \
1583
switch (_ctxreg.tag) { \
1584
case RR_Undef: _how = CFIR_UNKNOWN; _off = 0; break; \
1585
case RR_Same: _how = CFIR_SAME; _off = 0; break; \
1586
case RR_CFAoff: _how = CFIR_MEMCFAREL; _off = _ctxreg.coff; break; \
1587
case RR_CFAValoff: _how = CFIR_CFAREL; _off = _ctxreg.coff; break; \
1588
default: { why = 2; goto failed; } /* otherwise give up */ \
2061
# define SUMMARISE_HOW(_how, _off, _ctxreg) \
2062
switch (_ctxreg.tag) { \
2064
_how = CFIR_UNKNOWN; _off = 0; break; \
2066
_how = CFIR_SAME; _off = 0; break; \
2068
_how = CFIR_MEMCFAREL; _off = _ctxreg.arg; break; \
2069
case RR_CFAValOff: \
2070
_how = CFIR_CFAREL; _off = _ctxreg.arg; break; \
2071
case RR_ValExpr: { \
2072
XArray *src, *dst; \
2075
dst = seginfo->cfsi_exprs; \
2076
if (src && (VG_(sizeXA)(src) > 0) && (!dst)) { \
2077
dst = VG_(newXA)( symtab_alloc, symtab_free, \
2078
sizeof(CfiExpr) ); \
2080
seginfo->cfsi_exprs = dst; \
2082
conv = copy_convert_CfiExpr_tree \
2083
( dst, ctx, _ctxreg.arg ); \
2084
vg_assert(conv >= -1); \
2085
if (conv == -1) { why = 7; goto failed; } \
2088
if (0 && seginfo->ddump_frames) \
2089
ML_(ppCfiExpr)(dst, conv); \
2093
why = 2; goto failed; /* otherwise give up */ \
1591
2096
SUMMARISE_HOW(si->ra_how, si->ra_off, ctx->reg[ctx->ra_reg] );
2137
/* Copy the tree rooted at srcuc->exprs node srcix to dstxa, on the
2138
way converting any DwReg regs (regs numbered using the Dwarf scheme
2139
defined by each architecture's ABI) into CfiRegs, which are
2140
platform independent. If the conversion isn't possible because
2141
there is no equivalent register, return -1. This has the
2142
undesirable side effect of de-dagifying the input; oh well. */
2143
static Int copy_convert_CfiExpr_tree ( XArray* dstxa,
2144
UnwindContext* srcuc,
2148
Int cpL, cpR, cpA, dwreg;
2149
XArray* srcxa = srcuc->exprs;
2152
vg_assert(srcix >= 0 && srcix < VG_(sizeXA)(srcxa));
2154
src = VG_(indexXA)( srcxa, srcix );
2157
return ML_(CfiExpr_Undef)( dstxa );
2159
cpA = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Deref.ixAddr );
2161
return -1; /* propagate failure */
2162
return ML_(CfiExpr_Deref)( dstxa, cpA );
2164
return ML_(CfiExpr_Const)( dstxa, src->Cex.Const.con );
2166
cpL = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixL );
2167
cpR = copy_convert_CfiExpr_tree( dstxa, srcuc, src->Cex.Binop.ixR );
2168
vg_assert(cpL >= -1 && cpR >= -1);
2169
if (cpL == -1 || cpR == -1)
2170
return -1; /* propagate failure */
2171
return ML_(CfiExpr_Binop)( dstxa, src->Cex.Binop.op, cpL, cpR );
2173
/* should not see these in input (are created only by this
2174
conversion step!) */
2175
VG_(core_panic)("copy_convert_CfiExpr_tree: CfiReg in input");
2177
/* This is the only place where the conversion can fail. */
2178
dwreg = src->Cex.DwReg.reg;
2179
if (dwreg == SP_REG)
2180
return ML_(CfiExpr_CfiReg)( dstxa, Creg_SP );
2181
if (dwreg == FP_REG)
2182
return ML_(CfiExpr_CfiReg)( dstxa, Creg_FP );
2183
if (dwreg == srcuc->ra_reg)
2184
return ML_(CfiExpr_CfiReg)( dstxa, Creg_IP ); /* correct? */
2185
/* else we must fail - can't represent the reg */
2188
VG_(core_panic)("copy_convert_CfiExpr_tree: default");
1632
2193
static void ppUnwindContext_summary ( UnwindContext* ctx )
1634
2195
VG_(printf)("0x%llx-1: ", (ULong)ctx->loc);
1636
2197
if (ctx->cfa_reg == SP_REG) {
1637
VG_(printf)("SP/CFA=%d+SP ", ctx->cfa_offset);
2198
VG_(printf)("SP/CFA=%d+SP ", ctx->cfa_off);
1639
2200
if (ctx->cfa_reg == FP_REG) {
1640
VG_(printf)("SP/CFA=%d+FP ", ctx->cfa_offset);
2201
VG_(printf)("SP/CFA=%d+FP ", ctx->cfa_off);
1642
VG_(printf)("SP/CFA=unknown ", ctx->cfa_offset);
2203
VG_(printf)("SP/CFA=unknown ", ctx->cfa_off);
1645
2206
VG_(printf)("RA=");
1646
ppRegRule( &ctx->reg[ctx->ra_reg] );
2207
ppRegRule( ctx->exprs, &ctx->reg[ctx->ra_reg] );
1648
2209
VG_(printf)("FP=");
1649
ppRegRule( &ctx->reg[FP_REG] );
2210
ppRegRule( ctx->exprs, &ctx->reg[FP_REG] );
1650
2211
VG_(printf)("\n");
1770
static Addr read_encoded_Addr ( UChar* data, UChar encoding, Int *nbytes,
1771
UChar* ehframe, Addr ehframe_addr )
2346
static Addr read_encoded_Addr ( /*OUT*/Int* nbytes,
2347
AddressDecodingInfo* adi,
2350
/* Regarding the handling of DW_EH_PE_absptr. DWARF3 says this
2351
denotes an absolute address, hence you would think 'base' is
2352
zero. However, that is nonsensical (unless relocations are to
2353
be applied to the unwind data before reading it, which sounds
2354
unlikely). My interpretation is that DW_EH_PE_absptr indicates
2355
an address relative to where the object was loaded (technically,
2356
relative to its stated load VMA, hence the use of text_bias
2357
rather than text_avma). Hmm, should we use text_bias or
2358
text_avma here? Not sure.
2360
This view appears to be supported by DWARF3 spec sec 7.3
2361
"Executable Objects and Shared Objects":
2363
This requirement makes the debugging information for shared
2364
objects position independent. Virtual addresses in a shared
2365
object may be calculated by adding the offset to the base
2366
address at which the object was attached. This offset is
2367
available in the run-time linker's data structures.
2371
UChar encoding = adi->encoding;
2372
UChar* ehframe_image = adi->ehframe_image;
2373
Addr ehframe_avma = adi->ehframe_avma;
1776
2375
vg_assert((encoding & DW_EH_PE_indirect) == 0);
2437
/* ------------ Run/show DWARF3 expressions ---------- */
2439
/* Taken from binutils-2.17/include/elf/dwarf2.h */
2440
enum dwarf_location_atom
2444
DW_OP_const1u = 0x08,
2445
DW_OP_const1s = 0x09,
2446
DW_OP_const2u = 0x0a,
2447
DW_OP_const2s = 0x0b,
2448
DW_OP_const4u = 0x0c,
2449
DW_OP_const4s = 0x0d,
2450
DW_OP_const8u = 0x0e,
2451
DW_OP_const8s = 0x0f,
2452
DW_OP_constu = 0x10,
2453
DW_OP_consts = 0x11,
2460
DW_OP_xderef = 0x18,
2471
DW_OP_plus_uconst = 0x23,
2558
DW_OP_breg10 = 0x7a,
2559
DW_OP_breg11 = 0x7b,
2560
DW_OP_breg12 = 0x7c,
2561
DW_OP_breg13 = 0x7d,
2562
DW_OP_breg14 = 0x7e,
2563
DW_OP_breg15 = 0x7f,
2564
DW_OP_breg16 = 0x80,
2565
DW_OP_breg17 = 0x81,
2566
DW_OP_breg18 = 0x82,
2567
DW_OP_breg19 = 0x83,
2568
DW_OP_breg20 = 0x84,
2569
DW_OP_breg21 = 0x85,
2570
DW_OP_breg22 = 0x86,
2571
DW_OP_breg23 = 0x87,
2572
DW_OP_breg24 = 0x88,
2573
DW_OP_breg25 = 0x89,
2574
DW_OP_breg26 = 0x8a,
2575
DW_OP_breg27 = 0x8b,
2576
DW_OP_breg28 = 0x8c,
2577
DW_OP_breg29 = 0x8d,
2578
DW_OP_breg30 = 0x8e,
2579
DW_OP_breg31 = 0x8f,
2584
DW_OP_deref_size = 0x94,
2585
DW_OP_xderef_size = 0x95,
2587
/* DWARF 3 extensions. */
2588
DW_OP_push_object_address = 0x97,
2591
DW_OP_call_ref = 0x9a,
2592
DW_OP_form_tls_address = 0x9b,
2593
DW_OP_call_frame_cfa = 0x9c,
2594
DW_OP_bit_piece = 0x9d,
2595
/* GNU extensions. */
2596
DW_OP_GNU_push_tls_address = 0xe0,
2597
/* HP extensions. */
2598
DW_OP_HP_unknown = 0xe0, /* Ouch, the same as GNU_push_tls_address. */
2599
DW_OP_HP_is_value = 0xe1,
2600
DW_OP_HP_fltconst4 = 0xe2,
2601
DW_OP_HP_fltconst8 = 0xe3,
2602
DW_OP_HP_mod_range = 0xe4,
2603
DW_OP_HP_unmod_range = 0xe5,
2608
/* Convert the DWARF3 expression in expr[0 .. exprlen-1] into a dag
2609
(of CfiExprs) stored in ctx->exprs, and return the index in
2610
ctx->exprs of the root node. Or fail in which case return -1. */
2611
/* IMPORTANT: when adding expression forms here, also remember to
2612
add suitable evaluation code in evalCfiExpr in debuginfo.c. */
2613
static Int dwarfexpr_to_dag ( UnwindContext* ctx,
2614
UChar* expr, Int exprlen,
2615
Bool push_cfa_at_start,
2618
# define N_EXPR_STACK 20
2620
# define PUSH(_arg) \
2622
vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2623
if (sp == N_EXPR_STACK-1) \
2626
stack[sp] = (_arg); \
2629
# define POP(_lval) \
2631
vg_assert(sp >= -1 && sp < N_EXPR_STACK); \
2634
_lval = stack[sp]; \
2645
Int sp; /* # of top element: valid is -1 .. N_EXPR_STACK-1 */
2646
Int stack[N_EXPR_STACK]; /* indices into ctx->exprs */
2648
XArray* dst = ctx->exprs;
2649
UChar* limit = expr + exprlen;
2652
vg_assert(exprlen >= 0);
2654
sp = -1; /* empty */
2656
/* Synthesise the CFA as a CfiExpr */
2657
if (push_cfa_at_start) {
2658
if (ctx->cfa_is_regoff) {
2659
/* cfa is reg +/- offset */
2660
ix = ML_(CfiExpr_Binop)( dst,
2662
ML_(CfiExpr_DwReg)( dst, ctx->cfa_reg ),
2663
ML_(CfiExpr_Const)( dst, (UWord)(Word)ctx->cfa_off )
2667
/* CFA is already an expr; use its root node */
2668
PUSH(ctx->cfa_expr_ix);
2674
vg_assert(sp >= -1 && sp < N_EXPR_STACK);
2677
return -1; /* overrun - something's wrong */
2679
if (expr == limit) {
2680
/* end of expr - return expr on the top of stack. */
2682
return -1; /* stack empty. Bad. */
2687
op = 0; opname = NULL; /* excessively conservative */
2692
case DW_OP_lit0 ... DW_OP_lit31:
2693
/* push: literal 0 .. 31 */
2694
sw = (Word)opcode - (Word)DW_OP_lit0;
2695
vg_assert(sw >= 0 && sw <= 31);
2696
PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2698
VG_(printf)("DW_OP_lit%ld", sw);
2701
case DW_OP_breg0 ... DW_OP_breg31:
2702
/* push: reg + sleb128 */
2703
reg = (Int)opcode - (Int)DW_OP_breg0;
2704
vg_assert(reg >= 0 && reg <= 31);
2705
sw = read_leb128S( &expr );
2706
ix = ML_(CfiExpr_Binop)( dst,
2708
ML_(CfiExpr_DwReg)( dst, reg ),
2709
ML_(CfiExpr_Const)( dst, (UWord)sw )
2713
VG_(printf)("DW_OP_breg%d: %ld", reg, sw);
2716
case DW_OP_plus_uconst:
2717
uw = read_leb128U( &expr );
2718
PUSH( ML_(CfiExpr_Const)( dst, uw ) );
2721
PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
2723
VG_(printf)("DW_OP_plus_uconst: %lu", uw);
2727
/* push: 32-bit signed immediate */
2728
sw = read_le_s_encoded_literal( expr, 4 );
2730
PUSH( ML_(CfiExpr_Const)( dst, (UWord)sw ) );
2732
VG_(printf)("DW_OP_const4s: %ld", sw);
2736
op = Cop_Sub; opname = "minus"; goto binop;
2738
op = Cop_Add; opname = "plus"; goto binop;
2740
op = Cop_And; opname = "and"; goto binop;
2742
op = Cop_Mul; opname = "mul"; goto binop;
2746
PUSH( ML_(CfiExpr_Binop)( dst, op, ix2, ix ) );
2748
VG_(printf)("DW_OP_%s", opname);
2753
PUSH( ML_(CfiExpr_Deref)( dst, ix ) );
2755
VG_(printf)("DW_OP_deref");
2760
VG_(message)(Vg_DebugMsg,
2761
"Warning: DWARF2 CFI reader: unhandled DW_OP_ "
2762
"opcode 0x%x", (Int)opcode);
2766
if (expr < limit && ddump_frames)
2771
vg_assert(sp >= -1 && sp < N_EXPR_STACK);
2775
if (0 && ddump_frames)
2776
ML_(ppCfiExpr)( dst, stack[sp] );
2781
# undef N_EXPR_STACK
1838
2785
/* ------------ Run/show CFI instructions ------------ */
1840
2787
/* Run a CFI instruction, and also return its length.
1843
2790
static Int run_CF_instruction ( /*MOD*/UnwindContext* ctx,
1845
UnwindContext* restore_ctx )
2792
UnwindContext* restore_ctx,
2793
AddressDecodingInfo* adi,
2794
struct _SegInfo* si )
1847
Int off, reg, reg2, nleb, len;
1850
UChar hi2 = (instr[i] >> 6) & 3;
1851
UChar lo6 = instr[i] & 0x3F;
2796
Int off, reg, reg2, nleb, len;
2801
UChar hi2 = (instr[i] >> 6) & 3;
2802
UChar lo6 = instr[i] & 0x3F;
2803
Addr printing_bias = ((Addr)ctx->initloc) - ((Addr)si->text_bias);
1854
2806
if (hi2 == DW_CFA_advance_loc) {
1855
2807
delta = (UInt)lo6;
1856
2808
ctx->loc += delta;
2809
if (si->ddump_frames)
2810
VG_(printf)(" DW_CFA_advance_loc: %d to %08lx\n",
2811
(Int)delta, (Addr)ctx->loc + printing_bias);
1860
2815
if (hi2 == DW_CFA_offset) {
1861
/* Set rule for reg 'lo6' to CFAoffset(off * data_af) */
2816
/* Set rule for reg 'lo6' to CFAOff(off * data_af) */
1862
2817
off = read_leb128( &instr[i], &nleb, 0 );
1864
2819
reg = (Int)lo6;
1865
2820
if (reg < 0 || reg >= N_CFI_REGS)
1866
2821
return 0; /* fail */
1867
ctx->reg[reg].tag = RR_CFAoff;
1868
ctx->reg[reg].coff = off * ctx->data_a_f;
2822
ctx->reg[reg].tag = RR_CFAOff;
2823
ctx->reg[reg].arg = off * ctx->data_a_f;
2824
if (si->ddump_frames)
2825
VG_(printf)(" DW_CFA_offset: r%d at cfa%s%d\n",
2826
(Int)reg, ctx->reg[reg].arg < 0 ? "" : "+",
2827
(Int)ctx->reg[reg].arg );
2025
3057
ignores these. */
2026
3058
off = read_leb128( &instr[i], &nleb, 0 );
3060
if (si->ddump_frames)
3061
VG_(printf)(" rci:DW_CFA_GNU_args_size (ignored)\n");
2030
3064
case DW_CFA_expression:
2031
/* Too difficult to really handle; just skip over it and say
2032
that we don't know what do to with the register. */
2033
if (VG_(clo_trace_cfi))
2034
VG_(printf)("DWARF2 CFI reader: "
2035
"ignoring DW_CFA_expression\n");
3065
/* Identical to DW_CFA_val_expression except that the value
3066
computed is an address and so needs one final
2036
3068
reg = read_leb128( &instr[i], &nleb, 0 );
2038
3070
len = read_leb128( &instr[i], &nleb, 0 );
2041
if (reg < 0 || reg >= N_CFI_REGS)
2042
return 0; /* fail */
2043
ctx->reg[reg].tag = RR_Expr;
3074
if (reg < 0 || reg >= N_CFI_REGS)
3075
return 0; /* fail */
3076
if (si->ddump_frames)
3077
VG_(printf)(" DW_CFA_expression: r%d (",
3079
/* Convert the expression into a dag rooted at ctx->exprs index j,
3081
j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3083
if (si->ddump_frames)
3087
vg_assert(ctx->exprs);
3088
vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3091
return 0; /* fail */
3092
/* Add an extra dereference */
3093
j = ML_(CfiExpr_Deref)( ctx->exprs, j );
3094
ctx->reg[reg].tag = RR_ValExpr;
3095
ctx->reg[reg].arg = j;
2046
3098
case DW_CFA_val_expression:
2047
/* Too difficult to really handle; just skip over it and say
2048
that we don't know what do to with the register. */
2049
if (VG_(clo_trace_cfi))
2050
VG_(printf)("DWARF2 CFI reader: "
2051
"ignoring DW_CFA_val_expression\n");
2052
3099
reg = read_leb128( &instr[i], &nleb, 0 );
2054
3101
len = read_leb128( &instr[i], &nleb, 0 );
2057
3105
if (reg < 0 || reg >= N_CFI_REGS)
2058
3106
return 0; /* fail */
3107
if (si->ddump_frames)
3108
VG_(printf)(" DW_CFA_val_expression: r%d (",
3110
/* Convert the expression into a dag rooted at ctx->exprs index j,
3112
j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3114
if (si->ddump_frames)
3118
vg_assert(ctx->exprs);
3119
vg_assert( j < VG_(sizeXA)(ctx->exprs) );
3122
return 0; /* fail */
2059
3123
ctx->reg[reg].tag = RR_ValExpr;
3124
ctx->reg[reg].arg = j;
2062
3127
case DW_CFA_def_cfa_expression:
2063
if (VG_(clo_trace_cfi))
2064
VG_(printf)("DWARF2 CFI reader: "
2065
"ignoring DW_CFA_def_cfa_expression\n");
2066
3128
len = read_leb128( &instr[i], &nleb, 0 );
2069
ctx->cfa_reg = -1; /* indicating we don't know */
3132
if (si->ddump_frames)
3133
VG_(printf)(" DW_CFA_def_cfa_expression (");
3134
/* Convert the expression into a dag rooted at ctx->exprs index j,
3136
j = dwarfexpr_to_dag ( ctx, expr, len, True/*push CFA at start*/,
3138
if (si->ddump_frames)
3140
ctx->cfa_is_regoff = False;
3143
ctx->cfa_expr_ix = j;
2072
3146
case DW_CFA_GNU_window_save:
2073
3147
/* Ignored. This appears to be sparc-specific; quite why it
2074
3148
turns up in SuSE-supplied x86 .so's beats me. */
3149
if (si->ddump_frames)
3150
VG_(printf)("DW_CFA_GNU_window_save\n");
2078
3154
VG_(message)(Vg_DebugMsg, "DWARF2 CFI reader: unhandled CFI "
2079
3155
"instruction 0:%d", (Int)lo6);
3156
if (si->ddump_frames)
3157
VG_(printf)(" rci:run_CF_instruction:default\n");
2169
3257
reg2 = read_leb128( &instr[i], &nleb, 0);
2171
VG_(printf)("DW_CFA_register(r%d, r%d)\n", reg, reg2);
3259
VG_(printf)(" sci:DW_CFA_register(r%d, r%d)\n", reg, reg2);
2174
3262
case DW_CFA_def_cfa_register:
2175
3263
reg = read_leb128( &instr[i], &nleb, 0);
2177
VG_(printf)("DW_CFA_def_cfa_register(r%d)\n", reg);
3265
VG_(printf)(" sci:DW_CFA_def_cfa_register(r%d)\n", reg);
2180
3268
case DW_CFA_def_cfa_offset:
2181
3269
off = read_leb128( &instr[i], &nleb, 0);
2183
VG_(printf)("DW_CFA_def_cfa_offset(%d)\n", off);
3271
VG_(printf)(" sci:DW_CFA_def_cfa_offset(%d)\n", off);
2186
3274
case DW_CFA_def_cfa_offset_sf:
2187
3275
off = read_leb128( &instr[i], &nleb, 1);
2189
VG_(printf)("DW_CFA_def_cfa_offset_sf(%d)\n", off);
3277
VG_(printf)(" sci:DW_CFA_def_cfa_offset_sf(%d)\n", off);
2192
3280
case DW_CFA_restore_extended:
2193
3281
reg = read_leb128( &instr[i], &nleb, 0);
2195
VG_(printf)("DW_CFA_restore_extended(r%d)\n", reg);
3283
VG_(printf)(" sci:DW_CFA_restore_extended(r%d)\n", reg);
2198
3286
case DW_CFA_undefined:
2199
3287
reg = read_leb128( &instr[i], &nleb, 0);
2201
VG_(printf)("DW_CFA_undefined(r%d)\n", reg);
3289
VG_(printf)(" sci:DW_CFA_undefined(r%d)\n", reg);
2204
3292
case DW_CFA_same_value:
2205
3293
reg = read_leb128( &instr[i], &nleb, 0);
2207
VG_(printf)("DW_CFA_same_value(r%d)\n", reg);
3295
VG_(printf)(" sci:DW_CFA_same_value(r%d)\n", reg);
2210
3298
case DW_CFA_remember_state:
2211
VG_(printf)("DW_CFA_remember_state\n");
3299
VG_(printf)(" sci:DW_CFA_remember_state\n");
2214
3302
case DW_CFA_restore_state:
2215
VG_(printf)("DW_CFA_restore_state\n");
3303
VG_(printf)(" sci:DW_CFA_restore_state\n");
2218
3306
case DW_CFA_GNU_args_size:
2219
3307
off = read_leb128( &instr[i], &nleb, 0 );
2221
VG_(printf)("DW_CFA_GNU_args_size(%d)\n", off );
3309
VG_(printf)(" sci:DW_CFA_GNU_args_size(%d)\n", off );
2224
3312
case DW_CFA_def_cfa_expression:
2225
3313
len = read_leb128( &instr[i], &nleb, 0 );
2228
VG_(printf)("DW_CFA_def_cfa_expression(length %d)\n", len);
3316
VG_(printf)(" sci:DW_CFA_def_cfa_expression(length %d)\n", len);
2231
3319
case DW_CFA_expression:
2456
3558
Figure out which it is. */
2458
3560
ciefde_start = data;
2459
if (VG_(clo_trace_cfi))
2460
VG_(printf)("\ncie/fde.start = %p (ehframe + 0x%x)\n",
2461
ciefde_start, ciefde_start - ehframe);
3562
VG_(printf)("\ncie/fde.start = %p (ehframe_image + 0x%lx)\n",
3563
ciefde_start, ciefde_start - ehframe_image);
2463
ciefde_len = read_UInt(data); data += sizeof(UInt);
2464
if (VG_(clo_trace_cfi))
2465
VG_(printf)("cie/fde.length = %d\n", ciefde_len);
3565
ciefde_len = (ULong) read_UInt(data); data += sizeof(UInt);
3567
VG_(printf)("cie/fde.length = %lld\n", ciefde_len);
2467
3569
/* Apparently, if the .length field is zero, we are at the end
2468
of the sequence. ?? Neither the DWARF2 spec not the AMD64
2469
ABI spec say this, though. */
3570
of the sequence. This is stated in the Generic Elf
3571
Specification (see comments far above here) and is one of the
3572
places where .eh_frame and .debug_frame data differ. */
2470
3573
if (ciefde_len == 0) {
2471
if (data == ehframe + ehframe_sz)
2473
how = "zero-sized CIE/FDE but not at section end";
2477
cie_pointer = read_UInt(data);
2478
data += sizeof(UInt); /* XXX see XXX below */
2479
if (VG_(clo_trace_cfi))
2480
VG_(printf)("cie.pointer = %d\n", cie_pointer);
3574
if (si->ddump_frames)
3575
VG_(printf)("%08lx ZERO terminator\n\n",
3576
((Addr)ciefde_start) - ((Addr)ehframe_image));
3580
/* If the .length field is 0xFFFFFFFF then we're dealing with
3581
64-bit DWARF, and the real length is stored as a 64-bit
3582
number immediately following it. */
3584
if (ciefde_len == 0xFFFFFFFFUL) {
3586
ciefde_len = read_ULong(data); data += sizeof(ULong);
3589
/* Now get the CIE ID, whose size depends on the DWARF 32 vs
3592
cie_pointer = read_ULong(data);
3593
data += sizeof(ULong); /* XXX see XXX below */
3595
cie_pointer = (ULong)read_UInt(data);
3596
data += sizeof(UInt); /* XXX see XXX below */
3600
VG_(printf)("cie.pointer = %lld\n", cie_pointer);
2482
3602
/* If cie_pointer is zero, we've got a CIE; else it's an FDE. */
2483
3603
if (cie_pointer == 0) {
2651
= read_encoded_Addr(data, the_CIEs[cie].address_encoding,
2652
&nbytes, ehframe, ehframe_addr);
3815
adi.encoding = the_CIEs[cie].address_encoding;
3816
adi.ehframe_image = ehframe_image;
3817
adi.ehframe_avma = ehframe_avma;
3818
adi.text_bias = si->text_bias;
3819
fde_initloc = read_encoded_Addr(&nbytes, &adi, data);
2653
3820
data += nbytes;
2654
if (VG_(clo_trace_cfi))
2655
3822
VG_(printf)("fde.initloc = %p\n", (void*)fde_initloc);
2658
= read_encoded_Addr(data, the_CIEs[cie].address_encoding & 0xf,
2659
&nbytes, ehframe, ehframe_addr);
2661
if (VG_(clo_trace_cfi))
3824
adi.encoding = the_CIEs[cie].address_encoding & 0xf;
3825
adi.ehframe_image = ehframe_image;
3826
adi.ehframe_avma = ehframe_avma;
3827
adi.text_bias = si->text_bias;
3829
/* WAS (incorrectly):
3830
fde_arange = read_encoded_Addr(&nbytes, &adi, data);
3832
The following corresponds to what binutils/dwarf.c does:
3834
{ UInt ptr_size = size_of_encoded_Addr( adi.encoding );
3836
case 8: case 4: case 2: case 1:
3838
= (UWord)read_le_u_encoded_literal(data, ptr_size);
3842
how = "unknown arange field encoding in FDE";
2662
3848
VG_(printf)("fde.arangec = %p\n", (void*)fde_arange);
3850
if (si->ddump_frames)
3851
VG_(printf)("%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
3852
((Addr)ciefde_start) - ((Addr)ehframe_image),
3854
(Addr)(UWord)cie_pointer,
3856
((Addr)fde_initloc) - si->text_bias,
3857
((Addr)fde_initloc) - si->text_bias + fde_arange);
2664
3859
if (the_CIEs[cie].saw_z_augmentation) {
2665
data += read_leb128( data, &nbytes, 0);
3860
UInt length = read_leb128( data, &nbytes, 0);
2666
3861
data += nbytes;
3862
if (si->ddump_frames && (length > 0)) {
3864
VG_(printf)(" Augmentation data: ");
3865
for (i = 0; i < length; i++)
3866
VG_(printf)(" %02x", (UInt)data[i]);
3867
VG_(printf)("\n\n");
2669
3872
fde_instrs = data;
2670
3873
fde_ilen = ciefde_start + ciefde_len + sizeof(UInt) - data;
2671
if (VG_(clo_trace_cfi)) {
3874
if (si->trace_cfi) {
2672
3875
VG_(printf)("fde.instrs = %p\n", fde_instrs);
2673
3876
VG_(printf)("fde.ilen = %d\n", (Int)fde_ilen);
2681
3884
data += fde_ilen;
2683
if (VG_(clo_trace_cfi))
2684
show_CF_instructions(fde_instrs, fde_ilen);
3886
adi.encoding = the_CIEs[cie].address_encoding;
3887
adi.ehframe_image = ehframe_image;
3888
adi.ehframe_avma = ehframe_avma;
3889
adi.text_bias = si->text_bias;
3892
show_CF_instructions( fde_instrs, fde_ilen, &adi,
3893
the_CIEs[cie].code_a_f,
3894
the_CIEs[cie].data_a_f );
2686
3896
initUnwindContext(&ctx);
2687
3897
ctx.code_a_f = the_CIEs[cie].code_a_f;
2688
3898
ctx.data_a_f = the_CIEs[cie].data_a_f;
2689
3899
ctx.initloc = fde_initloc;
2690
3900
ctx.ra_reg = the_CIEs[cie].ra_reg;
2692
initUnwindContext(&restore_ctx);
2694
ok = run_CF_instructions(
2695
NULL, &ctx, the_CIEs[cie].instrs,
2696
the_CIEs[cie].ilen, 0, NULL);
3901
ctx.exprs = VG_(newXA)( symtab_alloc, symtab_free,
3903
vg_assert(ctx.exprs);
3905
/* Run the CIE's instructions. Ugly hack: if
3906
--debug-dump=frames is in effect, suppress output for
3907
these instructions since they will already have been shown
3908
at the time the CIE was first encountered. Note, not
3909
thread safe - if this reader is ever made threaded, should
3911
{ Bool hack = si->ddump_frames;
3912
si->ddump_frames = False;
3913
initUnwindContext(&restore_ctx);
3914
ok = run_CF_instructions(
3915
si, False, &ctx, the_CIEs[cie].instrs,
3916
the_CIEs[cie].ilen, 0, NULL, &adi
3918
si->ddump_frames = hack;
3920
/* And now run the instructions for the FDE, starting from
3921
the state created by running the CIE preamble
2698
3924
restore_ctx = ctx;
2699
3925
ok = run_CF_instructions(
2700
si, &ctx, fde_instrs, fde_ilen, fde_arange,
3926
si, True, &ctx, fde_instrs, fde_ilen, fde_arange,
3929
if (si->ddump_frames)
3933
VG_(deleteXA)( ctx.exprs );