Skip to content

Commit a9e79f1

Browse files
Break up large linux impl
1 parent c647a1e commit a9e79f1

File tree

13 files changed

+480
-380
lines changed

13 files changed

+480
-380
lines changed

libcxx/include/__stacktrace/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct _LIBCPP_HIDE_FROM_ABI alloc final {
6969

7070
template <typename _A2>
7171
bool operator==(_A2 const& __rhs) const {
72-
return &__rhs == this;
72+
return std::addressof(__rhs) == this;
7373
}
7474
};
7575

libcxx/include/__stacktrace/basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class _LIBCPP_EXPORTED_FROM_ABI basic_stacktrace {
5555

5656
[[no_unique_address]] _Allocator __alloc_;
5757

58-
using __entry_vec = vector<stacktrace_entry, _Allocator>;
58+
using __entry_vec _LIBCPP_NODEBUG = vector<stacktrace_entry, _Allocator>;
5959
__entry_vec __entries_;
6060

6161
public:

libcxx/include/__stacktrace/hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <__config>
1414
#include <__functional/hash.h>
15+
#include <cstddef>
1516
#include <cstdint>
1617

1718
#include <__stacktrace/base.h>

libcxx/include/module.modulemap.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,9 @@ module std [system] {
20232023
// TODO: Workaround for https://github.com/llvm/llvm-project/issues/120108
20242024
export std.cstddef.byte
20252025
export std.cstddef.size_t
2026+
export std.format.escaped_output_table
20262027
export std.format.formatter
2028+
export std.format.width_estimation_table
20272029
export std.functional.function
20282030
export std.functional.hash
20292031
export std.iterator.iterator

libcxx/lib/abi/x86_64-unknown-linux-gnu.libcxxabi.v1.stable.exceptions.nonew.abilist

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
{'is_defined': False, 'name': '_ZTVSt13runtime_error', 'size': 0, 'type': 'OBJECT'}
3636
{'is_defined': False, 'name': '_ZTVSt14overflow_error', 'size': 0, 'type': 'OBJECT'}
3737
{'is_defined': False, 'name': '_ZTVSt16invalid_argument', 'size': 0, 'type': 'OBJECT'}
38-
{'is_defined': False, 'name': '_ZTVSt9exception', 'size': 0, 'type': 'OBJECT'}
3938
{'is_defined': False, 'name': '_ZdaPv', 'type': 'FUNC'}
4039
{'is_defined': False, 'name': '_ZdaPvSt11align_val_t', 'type': 'FUNC'}
4140
{'is_defined': False, 'name': '_ZdlPv', 'type': 'FUNC'}
@@ -226,10 +225,6 @@
226225
{'is_defined': True, 'name': '_ZNKSt3__115basic_streambufIwNS_11char_traitsIwEEE6getlocEv', 'type': 'FUNC'}
227226
{'is_defined': True, 'name': '_ZNKSt3__115basic_stringbufIcNS_11char_traitsIcEENS_9allocatorIcEEE3strEv', 'type': 'FUNC'}
228227
{'is_defined': True, 'name': '_ZNKSt3__115error_condition7messageEv', 'type': 'FUNC'}
229-
{'is_defined': True, 'name': '_ZNKSt3__116stacktrace_entry11descriptionEv', 'type': 'FUNC'}
230-
{'is_defined': True, 'name': '_ZNKSt3__116stacktrace_entry11source_fileEv', 'type': 'FUNC'}
231-
{'is_defined': True, 'name': '_ZNKSt3__116stacktrace_entry11source_lineEv', 'type': 'FUNC'}
232-
{'is_defined': True, 'name': '_ZNKSt3__116stacktrace_entry13native_handleEv', 'type': 'FUNC'}
233228
{'is_defined': True, 'name': '_ZNKSt3__117bad_function_call4whatEv', 'type': 'FUNC'}
234229
{'is_defined': True, 'name': '_ZNKSt3__117moneypunct_bynameIcLb0EE11do_groupingEv', 'type': 'FUNC'}
235230
{'is_defined': True, 'name': '_ZNKSt3__117moneypunct_bynameIcLb0EE13do_neg_formatEv', 'type': 'FUNC'}
@@ -1927,9 +1922,6 @@
19271922
{'is_defined': True, 'name': '_ZTTNSt3__19strstreamE', 'size': 80, 'type': 'OBJECT'}
19281923
{'is_defined': True, 'name': '_ZTVNSt12experimental15fundamentals_v112bad_any_castE', 'size': 40, 'type': 'OBJECT'}
19291924
{'is_defined': True, 'name': '_ZTVNSt12experimental19bad_optional_accessE', 'size': 40, 'type': 'OBJECT'}
1930-
{'is_defined': True, 'name': '_ZTVNSt3__110__function6__baseIFbRKNS_12__stacktrace3elf6SymbolEEEE', 'size': 88, 'type': 'OBJECT'}
1931-
{'is_defined': True, 'name': '_ZTVNSt3__110__function6__baseIFbRKNS_12__stacktrace3elf7SectionEEEE', 'size': 88, 'type': 'OBJECT'}
1932-
{'is_defined': True, 'name': '_ZTVNSt3__110__function6__baseIFbRKNS_12__stacktrace4toolEEEE', 'size': 88, 'type': 'OBJECT'}
19331925
{'is_defined': True, 'name': '_ZTVNSt3__110istrstreamE', 'size': 80, 'type': 'OBJECT'}
19341926
{'is_defined': True, 'name': '_ZTVNSt3__110moneypunctIcLb0EEE', 'size': 112, 'type': 'OBJECT'}
19351927
{'is_defined': True, 'name': '_ZTVNSt3__110moneypunctIcLb1EEE', 'size': 112, 'type': 'OBJECT'}
@@ -1940,7 +1932,6 @@
19401932
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace10fd_istreamE', 'size': 80, 'type': 'OBJECT'}
19411933
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace15llvm_symbolizerE', 'size': 48, 'type': 'OBJECT'}
19421934
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace4atosE', 'size': 48, 'type': 'OBJECT'}
1943-
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace4toolE', 'size': 48, 'type': 'OBJECT'}
19441935
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace6failedE', 'size': 40, 'type': 'OBJECT'}
19451936
{'is_defined': True, 'name': '_ZTVNSt3__112__stacktrace9addr2lineE', 'size': 48, 'type': 'OBJECT'}
19461937
{'is_defined': True, 'name': '_ZTVNSt3__112bad_weak_ptrE', 'size': 40, 'type': 'OBJECT'}
@@ -2005,8 +1996,6 @@
20051996
{'is_defined': True, 'name': '_ZTVNSt3__120__codecvt_utf8_utf16IDiEE', 'size': 96, 'type': 'OBJECT'}
20061997
{'is_defined': True, 'name': '_ZTVNSt3__120__codecvt_utf8_utf16IDsEE', 'size': 96, 'type': 'OBJECT'}
20071998
{'is_defined': True, 'name': '_ZTVNSt3__120__codecvt_utf8_utf16IwEE', 'size': 96, 'type': 'OBJECT'}
2008-
{'is_defined': True, 'name': '_ZTVNSt3__120__time_get_c_storageIcEE', 'size': 72, 'type': 'OBJECT'}
2009-
{'is_defined': True, 'name': '_ZTVNSt3__120__time_get_c_storageIwEE', 'size': 72, 'type': 'OBJECT'}
20101999
{'is_defined': True, 'name': '_ZTVNSt3__13pmr15memory_resourceE', 'size': 56, 'type': 'OBJECT'}
20112000
{'is_defined': True, 'name': '_ZTVNSt3__13pmr25monotonic_buffer_resourceE', 'size': 56, 'type': 'OBJECT'}
20122001
{'is_defined': True, 'name': '_ZTVNSt3__13pmr26synchronized_pool_resourceE', 'size': 56, 'type': 'OBJECT'}

libcxx/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ set(LIBCXX_SOURCES
4141
ryu/d2s.cpp
4242
ryu/f2s.cpp
4343
stacktrace/builder.cpp
44+
stacktrace/linux/elf.cpp
45+
stacktrace/linux/images.cpp
4446
stacktrace/linux/impl.cpp
4547
stacktrace/macos/impl.cpp
4648
stacktrace/to_string.cpp

libcxx/src/stacktrace/linux/elf.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#if defined(__linux__)
10+
11+
# include "stacktrace/linux/elf.h"
12+
13+
# include <__stacktrace/base.h>
14+
# include <cassert>
15+
# include <cstddef>
16+
# include <cstdlib>
17+
# include <functional>
18+
# include <unistd.h>
19+
20+
_LIBCPP_BEGIN_NAMESPACE_STD
21+
namespace __stacktrace::elf {
22+
23+
ELF::ELF(std::byte const* image) {
24+
auto* p = (uint8_t const*)image;
25+
// Bytes 0..3: magic bytes: 0x7F, 'E', 'L', 'F'
26+
if (*p++ == 0x7f && *p++ == 0x45 && *p++ == 0x4c && *p++ == 0x46) {
27+
auto klass = *p++; // Byte 4 (EI_CLASS): ELF class, 32- or 64-bit (0x01 or 0x02)
28+
auto dataFormat = *p++; // Byte 5 (EI_DATA): (0x01) little- or (0x02) big-endian
29+
auto fileVersion = *p++; // Byte 6 (EI_VERSION): ELF version: expect 1 (latest ELF version)
30+
constexpr static uint16_t kEndianTestWord{0x0201};
31+
auto hostEndianness = *(uint8_t const*)&kEndianTestWord;
32+
if (dataFormat == hostEndianness && fileVersion == 1) {
33+
if (klass == 0x01) {
34+
header_ = Header((Header32 const*)image);
35+
makeSection_ = makeSection32;
36+
makeSymbol_ = makeSymbol32;
37+
secSize_ = sizeof(Section32);
38+
symSize_ = sizeof(Symbol32);
39+
} else if (klass == 0x02) {
40+
header_ = Header((Header64 const*)image);
41+
makeSection_ = makeSection64;
42+
makeSymbol_ = makeSymbol64;
43+
secSize_ = sizeof(Section64);
44+
symSize_ = sizeof(Symbol64);
45+
}
46+
}
47+
}
48+
if (*this) {
49+
nametab_ = section(header_.shstrndx_);
50+
eachSection([&](auto& sec) mutable -> bool {
51+
if (sec.type_ == Section::kSymTab && sec.name() == ".symtab") {
52+
symtab_ = sec;
53+
} else if (sec.type_ == Section::kStrTab && sec.name() == ".strtab") {
54+
strtab_ = sec;
55+
}
56+
return !symtab_ || !strtab_;
57+
});
58+
}
59+
if (symtab_) {
60+
symCount_ = symtab_.size_ / symSize_;
61+
}
62+
}
63+
64+
Section ELF::section(size_t index) {
65+
auto* addr = header_.ptr_ + header_.shoff_ + (index * secSize_);
66+
return makeSection_(this, addr);
67+
}
68+
69+
Symbol ELF::symbol(size_t index) {
70+
auto* addr = symtab_.data() + (index * symSize_);
71+
return makeSymbol_(this, addr);
72+
}
73+
74+
void ELF::eachSection(CB<Section> cb) {
75+
for (size_t i = 0; i < header_.shnum_ && cb(section(i)); i++)
76+
;
77+
}
78+
79+
void ELF::eachSymbol(CB<Symbol> cb) {
80+
for (size_t i = 0; i < symCount_ && cb(symbol(i)); i++)
81+
;
82+
}
83+
84+
Symbol ELF::getSym(uintptr_t addr) {
85+
Symbol ret{};
86+
eachSymbol([&](auto& sym) -> bool {
87+
if (sym.value_ <= addr && sym.value_ > ret.value_) {
88+
ret = sym;
89+
}
90+
return true;
91+
});
92+
return ret;
93+
}
94+
95+
} // namespace __stacktrace::elf
96+
_LIBCPP_END_NAMESPACE_STD
97+
98+
#endif // defined(__linux__)

0 commit comments

Comments
 (0)