<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72464>72464</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Failed to dynamic_cast when compile with `-fvisibility=hidden -stdlib=libc++`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          timxx
      </td>
    </tr>
</table>

<pre>
    It's very simple case:

Write two abstract classes in shared library, used the shared library in main program,  use `dynamic_cast` to cast the abstract class, it fails.

sdk.so
```C++

// sdk.h
#pragma once

#define SDK_EXPORT __attribute__((visibility("default")))

class ISDK {
public:
 virtual ~ISDK() {}
  virtual void foo() const = 0;
};

class ISDKV2 : public ISDK {
public:
  virtual void bar() = 0;
};

SDK_EXPORT ISDK *create_sdk();

// sdk.cpp

#include "sdk.h"
#include <iostream>

class SDKV2Impl : public ISDKV2 {
public:
  void foo() const { std::cout << "foo\n"; }
  void bar() { std::cout << "bar\n"; }
};

ISDK *create_sdk() { return new SDKV2Impl(); }
```

demo (links with sdk.so)
``` C++
#include "sdk.h"

#include <memory>

int main(int argc, char **argv) {
  std::unique_ptr<ISDK> sdk(create_sdk());
  ISDKV2 *v2 = dynamic_cast<ISDKV2 *>(sdk.get());
 if (v2) {
    v2->bar();
  } else {
    sdk->foo();
  }

 return 0;
}
```

In above example, it should prints `bar` but `foo` instead.

It's strange when removed the `-stdlib=libc++` flags, it works just fine. (GCC also works as expected).

If I add the `SDK_EXPORT` to the two abstract classes, it also works (or just use `-fvisibility-ms-compat` instead), but I don't want this way, it exports too many unwanted symbols (the vtable and typeinfo)

[bug.zip](https://github.com/llvm/llvm-project/files/13371442/bug.zip)
Extract the `bug.zip`, run with `cmake -DCMAKE_CXX_COMPILER=clang++` , build and run to reproduce the problem.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/e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