@@ -1610,6 +1610,194 @@ unit_addrs_search (const void *vkey, const void *ventry)
1610
1610
return 0 ;
1611
1611
}
1612
1612
1613
+ /* Fill in overlapping ranges as needed. This is a subroutine of
1614
+ resolve_unit_addrs_overlap. */
1615
+
1616
+ static int
1617
+ resolve_unit_addrs_overlap_walk (struct backtrace_state * state ,
1618
+ size_t * pfrom , size_t * pto ,
1619
+ struct unit_addrs * enclosing ,
1620
+ struct unit_addrs_vector * old_vec ,
1621
+ backtrace_error_callback error_callback ,
1622
+ void * data ,
1623
+ struct unit_addrs_vector * new_vec )
1624
+ {
1625
+ struct unit_addrs * old_addrs ;
1626
+ size_t old_count ;
1627
+ struct unit_addrs * new_addrs ;
1628
+ size_t from ;
1629
+ size_t to ;
1630
+
1631
+ old_addrs = (struct unit_addrs * ) old_vec -> vec .base ;
1632
+ old_count = old_vec -> count ;
1633
+ new_addrs = (struct unit_addrs * ) new_vec -> vec .base ;
1634
+
1635
+ for (from = * pfrom , to = * pto ; from < old_count ; from ++ , to ++ )
1636
+ {
1637
+ /* If we are in the scope of a larger range that can no longer
1638
+ cover any further ranges, return back to the caller. */
1639
+
1640
+ if (enclosing != NULL
1641
+ && enclosing -> high <= old_addrs [from ].low )
1642
+ {
1643
+ * pfrom = from ;
1644
+ * pto = to ;
1645
+ return 1 ;
1646
+ }
1647
+
1648
+ new_addrs [to ] = old_addrs [from ];
1649
+
1650
+ /* If we are in scope of a larger range, fill in any gaps
1651
+ between this entry and the next one.
1652
+
1653
+ There is an extra entry at the end of the vector, so it's
1654
+ always OK to refer to from + 1. */
1655
+
1656
+ if (enclosing != NULL
1657
+ && enclosing -> high > old_addrs [from ].high
1658
+ && old_addrs [from ].high < old_addrs [from + 1 ].low )
1659
+ {
1660
+ void * grew ;
1661
+ size_t new_high ;
1662
+
1663
+ grew = backtrace_vector_grow (state , sizeof (struct unit_addrs ),
1664
+ error_callback , data , & new_vec -> vec );
1665
+ if (grew == NULL )
1666
+ return 0 ;
1667
+ new_addrs = (struct unit_addrs * ) new_vec -> vec .base ;
1668
+ to ++ ;
1669
+ new_addrs [to ].low = old_addrs [from ].high ;
1670
+ new_high = old_addrs [from + 1 ].low ;
1671
+ if (enclosing -> high < new_high )
1672
+ new_high = enclosing -> high ;
1673
+ new_addrs [to ].high = new_high ;
1674
+ new_addrs [to ].u = enclosing -> u ;
1675
+ }
1676
+
1677
+ /* If this range has a larger scope than the next one, use it to
1678
+ fill in any gaps. */
1679
+
1680
+ if (old_addrs [from ].high > old_addrs [from + 1 ].high )
1681
+ {
1682
+ * pfrom = from + 1 ;
1683
+ * pto = to + 1 ;
1684
+ if (!resolve_unit_addrs_overlap_walk (state , pfrom , pto ,
1685
+ & old_addrs [from ], old_vec ,
1686
+ error_callback , data , new_vec ))
1687
+ return 0 ;
1688
+ from = * pfrom ;
1689
+ to = * pto ;
1690
+
1691
+ /* Undo the increment the loop is about to do. */
1692
+ from -- ;
1693
+ to -- ;
1694
+ }
1695
+ }
1696
+
1697
+ if (enclosing == NULL )
1698
+ {
1699
+ struct unit_addrs * pa ;
1700
+
1701
+ /* Add trailing entry. */
1702
+
1703
+ pa = ((struct unit_addrs * )
1704
+ backtrace_vector_grow (state , sizeof (struct unit_addrs ),
1705
+ error_callback , data , & new_vec -> vec ));
1706
+ if (pa == NULL )
1707
+ return 0 ;
1708
+ pa -> low = 0 ;
1709
+ -- pa -> low ;
1710
+ pa -> high = pa -> low ;
1711
+ pa -> u = NULL ;
1712
+
1713
+ new_vec -> count = to ;
1714
+ }
1715
+
1716
+ return 1 ;
1717
+ }
1718
+
1719
+ /* It is possible for the unit_addrs list to contain overlaps, as in
1720
+
1721
+ 10: low == 10, high == 20, unit 1
1722
+ 11: low == 12, high == 15, unit 2
1723
+ 12: low == 20, high == 30, unit 1
1724
+
1725
+ In such a case, for pc == 17, a search using units_addr_search will
1726
+ return entry 11. However, pc == 17 doesn't fit in that range. We
1727
+ actually want range 10.
1728
+
1729
+ It seems that in general we might have an arbitrary number of
1730
+ ranges in between 10 and 12.
1731
+
1732
+ To handle this we look for cases where range R1 is followed by
1733
+ range R2 such that R2 is a strict subset of R1. In such cases we
1734
+ insert a new range R3 following R2 that fills in the remainder of
1735
+ the address space covered by R1. That lets a relatively simple
1736
+ search find the correct range.
1737
+
1738
+ These overlaps can occur because of the range merging we do in
1739
+ add_unit_addr. When the linker de-duplicates functions, it can
1740
+ leave behind an address range that refers to the address range of
1741
+ the retained duplicate. If the retained duplicate address range is
1742
+ merged with others, then after sorting we can see overlapping
1743
+ address ranges.
1744
+
1745
+ See https://github.com/ianlancetaylor/libbacktrace/issues/137. */
1746
+
1747
+ static int
1748
+ resolve_unit_addrs_overlap (struct backtrace_state * state ,
1749
+ backtrace_error_callback error_callback ,
1750
+ void * data , struct unit_addrs_vector * addrs_vec )
1751
+ {
1752
+ struct unit_addrs * addrs ;
1753
+ size_t count ;
1754
+ int found ;
1755
+ struct unit_addrs * entry ;
1756
+ size_t i ;
1757
+ struct unit_addrs_vector new_vec ;
1758
+ void * grew ;
1759
+ size_t from ;
1760
+ size_t to ;
1761
+
1762
+ addrs = (struct unit_addrs * ) addrs_vec -> vec .base ;
1763
+ count = addrs_vec -> count ;
1764
+
1765
+ if (count == 0 )
1766
+ return 1 ;
1767
+
1768
+ /* Optimistically assume that overlaps are rare. */
1769
+ found = 0 ;
1770
+ entry = addrs ;
1771
+ for (i = 0 ; i < count - 1 ; i ++ )
1772
+ {
1773
+ if (entry -> low < (entry + 1 )-> low
1774
+ && entry -> high > (entry + 1 )-> high )
1775
+ {
1776
+ found = 1 ;
1777
+ break ;
1778
+ }
1779
+ entry ++ ;
1780
+ }
1781
+ if (!found )
1782
+ return 1 ;
1783
+
1784
+ memset (& new_vec , 0 , sizeof new_vec );
1785
+ grew = backtrace_vector_grow (state ,
1786
+ count * sizeof (struct unit_addrs ),
1787
+ error_callback , data , & new_vec .vec );
1788
+ if (grew == NULL )
1789
+ return 0 ;
1790
+
1791
+ from = 0 ;
1792
+ to = 0 ;
1793
+ resolve_unit_addrs_overlap_walk (state , & from , & to , NULL , addrs_vec ,
1794
+ error_callback , data , & new_vec );
1795
+ backtrace_vector_free (state , & addrs_vec -> vec , error_callback , data );
1796
+ * addrs_vec = new_vec ;
1797
+
1798
+ return 1 ;
1799
+ }
1800
+
1613
1801
/* Sort the line vector by PC. We want a stable sort here to maintain
1614
1802
the order of lines for the same PC values. Since the sequence is
1615
1803
being sorted in place, their addresses cannot be relied on to
@@ -3314,7 +3502,7 @@ read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
3314
3502
3315
3503
if (vec .count == 0 )
3316
3504
{
3317
- /* This is not a failure in the sense of a generating an error,
3505
+ /* This is not a failure in the sense of generating an error,
3318
3506
but it is a failure in that sense that we have no useful
3319
3507
information. */
3320
3508
goto fail ;
@@ -4300,11 +4488,7 @@ build_dwarf_data (struct backtrace_state *state,
4300
4488
void * data )
4301
4489
{
4302
4490
struct unit_addrs_vector addrs_vec ;
4303
- struct unit_addrs * addrs ;
4304
- size_t addrs_count ;
4305
4491
struct unit_vector units_vec ;
4306
- struct unit * * units ;
4307
- size_t units_count ;
4308
4492
struct dwarf_data * fdata ;
4309
4493
4310
4494
if (!build_address_map (state , base_address , dwarf_sections , is_bigendian ,
@@ -4316,12 +4500,12 @@ build_dwarf_data (struct backtrace_state *state,
4316
4500
return NULL ;
4317
4501
if (!backtrace_vector_release (state , & units_vec .vec , error_callback , data ))
4318
4502
return NULL ;
4319
- addrs = ( struct unit_addrs * ) addrs_vec . vec . base ;
4320
- units = ( struct unit * * ) units_vec .vec .base ;
4321
- addrs_count = addrs_vec . count ;
4322
- units_count = units_vec . count ;
4323
- backtrace_qsort ( addrs , addrs_count , sizeof ( struct unit_addrs ),
4324
- unit_addrs_compare );
4503
+
4504
+ backtrace_qsort (( struct unit_addrs * ) addrs_vec .vec .base , addrs_vec . count ,
4505
+ sizeof ( struct unit_addrs ), unit_addrs_compare ) ;
4506
+ if (! resolve_unit_addrs_overlap ( state , error_callback , data , & addrs_vec ))
4507
+ return NULL ;
4508
+
4325
4509
/* No qsort for units required, already sorted. */
4326
4510
4327
4511
fdata = ((struct dwarf_data * )
@@ -4333,10 +4517,10 @@ build_dwarf_data (struct backtrace_state *state,
4333
4517
fdata -> next = NULL ;
4334
4518
fdata -> altlink = altlink ;
4335
4519
fdata -> base_address = base_address ;
4336
- fdata -> addrs = addrs ;
4337
- fdata -> addrs_count = addrs_count ;
4338
- fdata -> units = units ;
4339
- fdata -> units_count = units_count ;
4520
+ fdata -> addrs = ( struct unit_addrs * ) addrs_vec . vec . base ;
4521
+ fdata -> addrs_count = addrs_vec . count ;
4522
+ fdata -> units = ( struct unit * * ) units_vec . vec . base ;
4523
+ fdata -> units_count = units_vec . count ;
4340
4524
fdata -> dwarf_sections = * dwarf_sections ;
4341
4525
fdata -> is_bigendian = is_bigendian ;
4342
4526
memset (& fdata -> fvec , 0 , sizeof fdata -> fvec );
0 commit comments