Issue |
73524
|
Summary |
[libc][doc] file structure and `LLVM_LIBC_ENTRYPOINT`
|
Labels |
libc
|
Assignees |
|
Reporter |
SchrodingerZhu
|
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`.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs