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

    <tr>
        <th>Summary</th>
        <td>
            [libc][doc] file structure and `LLVM_LIBC_ENTRYPOINT`
        </td>
    </tr>

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

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

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

<pre>
    The `implementation_standard.rst` states that:
```rst
``.cpp`` File Structure
-----------------------

The implementation can span multiple ``.cpp`` files. However, the signature of
the entrypoint function should make use of a special macro. For example, the
``round`` function from ``math.h`` should be defined as follows, say in the file
``src/math/math/round.cpp``::

    // --- round.cpp --- //

    namespace LIBC_NAMESPACE {

    double LLVM_LIBC_ENTRYPOINT(round)(double d) {
      // ... implementation goes here.
    }

    } // namespace LIBC_NAMESPACE

Notice the use of the macro ``LLVM_LIBC_ENTRYPOINT``. This macro helps us define
an C alias symbol for the C++ implementation. The C alias need not be added by
the macro by itself. For example, for ELF targets, the macro is defined as
follows::

    #define ENTRYPOINT_SECTION_ATTRIBUTE(name) \
 __attribute__((section(".llvm.libc.entrypoint."#name)))
 #define LLVM_LIBC_ENTRYPOINT(name) ENTRYPOINT_SECTION_ATTRIBUTE(name) name

The macro places the C++ function in a unique section with name
``.llvm.libc.entrypoint.<function name>``. This allows us to add a C alias using
a post build step. For example, for the ``round`` function, one can use
``objcopy`` to add an alias symbol as follows::

 objcopy --add-symbol round=.llvm.libc.entrypoint.round:0,function round.o

NOTE: We use a post build ``objcopy`` step to add an alias instead of using
the ``__attribute__((alias))``. For C++, this ``alias`` attribute requires
mangled names of the referees. Using the post build ``objcopy`` step helps
us avoid putting mangled names with ``alias`` attributes.
```
However, what is really used seems to be `LLVM_LIBC_FUNCTION`.
```c++
//===-- Implementation of round function ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/round.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(double, round, (double x)) { return fputil::round(x); }

} // namespace LIBC_NAMESPACE

```

This also affects docs inside `clang-tidy`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV9tu4zgS_Rr6pSBBpuJLP_gh8QVjIJMOJu6d3X0xKKlkcZYi1STV3f77BS_yLc4iC4xh2JJcdXiq6lSRZsbwg0RckMkTmaxGrLeN0ou3stGq4vKA-t9NPypUdVzsGgQyzXjbCWxRWma5kntjmayYrlJtLJlmYCyzaMA2zJL8kWQrkj2SaRbezubiSVp2XbiCDRcIb1b3pe01BqPk_isi-E_H6ZoQlEyC6ZiEtheWd8KTvl6s5gJNCr-pn_gDNaFLsA2CSwRzq4OqA7p7itLqY6e4tFD3svRLmEb1ooKW_QehN84eGJgOS84EtKzUKoWN0oC_mOMWF7iMXKteVgObAbbWqo1kW2abtIkGcbkCocKaS6yAGaiVEOqncdiGHYFLH4OL7HIdo0tCNw7t_OXXPmfDVelUKP8JAEDohtANJEkCJ3t_F364NZesRdOxEuF5-7Tcvzz-vn57fVyugcyebm0r1RcC4fn5H7_vvfX6ZffHv16_bl92hM5DZugXQufR0N2dcQAu6KVpelv-g0IDDWpML4KZrd7FN1sNIB9xv3R5UZaX6FMcC-4ufaljxe6GE4QHu4abaNyg6Az0JpYyoDMJS2CCMwPm2BZKQK20X2FJ6BOhTzdBOkQ8uUjECqSyTiCsqrCC4njWb1i2OAK3BkX9TphupfXzBizTB7RmaIbgxs2F5ALmoLsPVJMHezgnYf-2Xu62X1_2j7vdH9unb7s1oXOXc1_WyTJ67_fMWs2L3uJ-T-ic0LlB3xf-hqZC_GhTwYsyPbdkSiglNI9o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