Skip to content

Commit 7fac517

Browse files
committed
Drop the dependency on <algorithm>, add placement new inline
We haven't eliminated C++ library dependency altogether in D57251, UnwindCursor.hpp had an unused dependency on <algorithm> which was pulling in other C++ headers. Removing that dependency also revealed (correctly) that we need our own global placement new declaration. Now libunwind should be independent of the C++ library. Differential Revision: https://reviews.llvm.org/D57262 llvm-svn: 352553
1 parent fbf40f4 commit 7fac517

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

libunwind/CMakeLists.txt

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ unwind_append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_EHSC_FLAG -EHsc)
287287

288288
unwind_append_if(LIBUNWIND_C_FLAGS LIBUNWIND_HAS_FUNWIND_TABLES -funwind-tables)
289289

290+
# Ensure that we don't depend on C++ standard library.
291+
unwind_append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_NOSTDINCXX_FLAG -nostdinc++)
292+
290293
# Assert
291294
string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
292295
if (LIBUNWIND_ENABLE_ASSERTIONS)
@@ -341,28 +344,6 @@ endif()
341344

342345
include_directories(include)
343346

344-
find_path(
345-
LIBUNWIND_LIBCXX_INCLUDES_INTERNAL
346-
__libcpp_version
347-
PATHS ${LLVM_MAIN_SRC_DIR}/projects/libcxx/include
348-
${LLVM_MAIN_SRC_DIR}/runtimes/libcxx/include
349-
${LLVM_MAIN_SRC_DIR}/../libcxx/include
350-
NO_DEFAULT_PATH
351-
NO_CMAKE_FIND_ROOT_PATH
352-
)
353-
if ((NOT LIBUNWIND_STANDALONE_BUILD OR HAVE_LIBCXX) AND
354-
IS_DIRECTORY "${LIBUNWIND_LIBCXX_INCLUDES_INTERNAL}")
355-
set(LIBUNWIND_CXX_INCLUDE_PATHS_DEFAULT "${LIBUNWIND_LIBCXX_INCLUDES_INTERNAL}")
356-
endif()
357-
358-
set(LIBUNWIND_CXX_INCLUDE_PATHS "${LIBUNWIND_CXX_INCLUDE_PATHS_DEFAULT}" CACHE PATH
359-
"Paths to C++ header directories separated by ';'.")
360-
361-
if (NOT LIBUNWIND_CXX_INCLUDE_PATHS STREQUAL "")
362-
list(APPEND LIBUNWIND_CXX_FLAGS -nostdinc++)
363-
include_directories("${LIBUNWIND_CXX_INCLUDE_PATHS}")
364-
endif()
365-
366347
add_subdirectory(src)
367348

368349
if (LIBUNWIND_INCLUDE_DOCS)

libunwind/src/Unwind-EHABI.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#include <stdlib.h>
2121
#include <string.h>
2222

23-
#include <type_traits>
24-
2523
#include "config.h"
2624
#include "libunwind.h"
2725
#include "libunwind_ext.h"

libunwind/src/Unwind-seh.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ using namespace libunwind;
4949
/// Class of foreign exceptions based on unrecognized SEH exceptions.
5050
static const uint64_t kSEHExceptionClass = 0x434C4E4753454800; // CLNGSEH\0
5151

52+
// libunwind does not and should not depend on C++ library which means that we
53+
// need our own declaration of global placement new.
54+
void *operator new(size_t, void*);
55+
5256
/// Exception cleanup routine used by \c _GCC_specific_handler to
5357
/// free foreign exceptions.
5458
static void seh_exc_cleanup(_Unwind_Reason_Code urc, _Unwind_Exception *exc) {

libunwind/src/UnwindCursor.hpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#ifndef __UNWINDCURSOR_HPP__
1212
#define __UNWINDCURSOR_HPP__
1313

14-
#include <algorithm>
1514
#include <stdint.h>
1615
#include <stdio.h>
1716
#include <stdlib.h>
@@ -105,7 +104,6 @@ class _LIBUNWIND_HIDDEN DwarfFDECache {
105104
static void dyldUnloadHook(const struct mach_header *mh, intptr_t slide);
106105
static bool _registeredForDyldUnloads;
107106
#endif
108-
// Can't use std::vector<> here because this code is below libc++.
109107
static entry *_buffer;
110108
static entry *_bufferUsed;
111109
static entry *_bufferEnd;
@@ -1225,7 +1223,6 @@ template<typename A>
12251223
struct EHABISectionIterator {
12261224
typedef EHABISectionIterator _Self;
12271225

1228-
typedef std::random_access_iterator_tag iterator_category;
12291226
typedef typename A::pint_t value_type;
12301227
typedef typename A::pint_t* pointer;
12311228
typedef typename A::pint_t& reference;
@@ -1279,6 +1276,29 @@ struct EHABISectionIterator {
12791276
const UnwindInfoSections* _sects;
12801277
};
12811278

1279+
namespace {
1280+
1281+
template <typename A>
1282+
EHABISectionIterator<A> EHABISectionUpperBound(
1283+
EHABISectionIterator<A> first,
1284+
EHABISectionIterator<A> last,
1285+
typename A::pint_t value) {
1286+
size_t len = last - first;
1287+
while (len > 0) {
1288+
size_t l2 = len / 2;
1289+
EHABISectionIterator<A> m = first + l2;
1290+
if (value < *m) {
1291+
len = l2;
1292+
} else {
1293+
first = ++m;
1294+
len -= l2 + 1;
1295+
}
1296+
}
1297+
return first;
1298+
}
1299+
1300+
}
1301+
12821302
template <typename A, typename R>
12831303
bool UnwindCursor<A, R>::getInfoFromEHABISection(
12841304
pint_t pc,
@@ -1290,7 +1310,7 @@ bool UnwindCursor<A, R>::getInfoFromEHABISection(
12901310
if (begin == end)
12911311
return false;
12921312

1293-
EHABISectionIterator<A> itNextPC = std::upper_bound(begin, end, pc);
1313+
EHABISectionIterator<A> itNextPC = EHABISectionUpperBound(begin, end, pc);
12941314
if (itNextPC == begin)
12951315
return false;
12961316
EHABISectionIterator<A> itThisPC = itNextPC - 1;
@@ -1300,8 +1320,7 @@ bool UnwindCursor<A, R>::getInfoFromEHABISection(
13001320
// in the table, we don't really know the function extent and have to choose a
13011321
// value for nextPC. Choosing max() will allow the range check during trace to
13021322
// succeed.
1303-
pint_t nextPC = (itNextPC == end) ? std::numeric_limits<pint_t>::max()
1304-
: itNextPC.functionAddress();
1323+
pint_t nextPC = (itNextPC == end) ? UINTPTR_MAX : itNextPC.functionAddress();
13051324
pint_t indexDataAddr = itThisPC.dataAddress();
13061325

13071326
if (indexDataAddr == 0)

libunwind/src/libunwind.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
#include <libunwind.h>
1313

14-
#ifndef NDEBUG
15-
#include <cstdlib> // getenv
16-
#endif
17-
1814
#include "libunwind_ext.h"
1915
#include "config.h"
2016

@@ -27,6 +23,10 @@
2723

2824
using namespace libunwind;
2925

26+
// libunwind does not and should not depend on C++ library which means that we
27+
// need our own declaration of global placement new.
28+
void *operator new(size_t, void*);
29+
3030
/// internal object to represent this processes address space
3131
LocalAddressSpace LocalAddressSpace::sThisAddressSpace;
3232

0 commit comments

Comments
 (0)