Skip to content

Commit a0b65a7

Browse files
[libc] Switch to use a macro which does not insert a section for every libc function.
Summary: The new macro also inserts the C alias for the C++ implementations without needing an objcopy based post processing step. The CMake rules have been updated to reflect this. More CMake cleanup can be taken up in future rounds and appropriate TODOs have been added for them. Reviewers: mcgrathr, sivachandra Subscribers:
1 parent daaaed6 commit a0b65a7

File tree

147 files changed

+210
-222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+210
-222
lines changed

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -147,79 +147,47 @@ function(add_entrypoint_object target_name)
147147
message(FATAL_ERROR "`add_entrypoint_object` rule requires HDRS to be specified.")
148148
endif()
149149

150-
set(objects_target_name "${fq_target_name}_objects")
150+
set(common_compile_options -fpie ${LLVM_CXX_STD_default} -ffreestanding ${ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS})
151+
set(internal_target_name ${fq_target_name}.__internal__)
152+
set(include_dirs ${LIBC_BUILD_DIR}/include ${LIBC_SOURCE_DIR} ${LIBC_BUILD_DIR})
153+
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
154+
set(full_deps_list ${fq_deps_list} libc.src.__support.common)
155+
156+
add_library(
157+
${internal_target_name}
158+
# TODO: We don't need an object library for internal consumption.
159+
# A future change should switch this to a normal static library.
160+
OBJECT
161+
${ADD_ENTRYPOINT_OBJ_SRCS}
162+
${ADD_ENTRYPOINT_OBJ_HDRS}
163+
)
164+
target_compile_options(${internal_target_name} BEFORE PRIVATE ${common_compile_options})
165+
target_include_directories(${internal_target_name} PRIVATE ${include_dirs})
166+
add_dependencies(${internal_target_name} ${full_deps_list})
151167

152168
add_library(
153-
${objects_target_name}
169+
${fq_target_name}
154170
# We want an object library as the objects will eventually get packaged into
155171
# an archive (like libc.a).
156172
OBJECT
157173
${ADD_ENTRYPOINT_OBJ_SRCS}
158174
${ADD_ENTRYPOINT_OBJ_HDRS}
159175
)
160176
target_compile_options(
161-
${objects_target_name}
162-
BEFORE
163-
PRIVATE
164-
-fpie ${LLVM_CXX_STD_default} -ffreestanding
165-
)
166-
target_include_directories(
167-
${objects_target_name}
168-
PRIVATE
169-
${LIBC_BUILD_DIR}/include
170-
${LIBC_SOURCE_DIR}
171-
${LIBC_BUILD_DIR}
177+
${fq_target_name} BEFORE PRIVATE ${common_compile_options} -DLLVM_LIBC_PUBLIC_PACKAGING
172178
)
173-
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
174-
add_dependencies(
175-
${objects_target_name}
176-
libc.src.__support.common
177-
${fq_deps_list}
178-
)
179-
180-
if(ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS)
181-
target_compile_options(
182-
${objects_target_name}
183-
PRIVATE ${ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS}
184-
)
185-
endif()
179+
target_include_directories(${fq_target_name} PRIVATE ${include_dirs})
180+
add_dependencies(${fq_target_name} ${full_deps_list})
186181

187-
set(object_file_raw "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_raw.o")
188-
set(object_file "${CMAKE_CURRENT_BINARY_DIR}/${target_name}.o")
189-
190-
set(input_objects $<TARGET_OBJECTS:${objects_target_name}>)
191-
add_custom_command(
192-
OUTPUT ${object_file_raw}
193-
DEPENDS ${input_objects}
194-
COMMAND ${CMAKE_LINKER} -r ${input_objects} -o ${object_file_raw}
195-
)
196-
197-
set(alias_attributes "0,function,global")
198-
if(ADD_ENTRYPOINT_OBJ_REDIRECTED)
199-
set(alias_attributes "${alias_attributes},hidden")
200-
endif()
201-
202-
add_custom_command(
203-
OUTPUT ${object_file}
204-
# We llvm-objcopy here as GNU-binutils objcopy does not support the 'hidden' flag.
205-
DEPENDS ${object_file_raw} ${llvm-objcopy}
206-
COMMAND $<TARGET_FILE:llvm-objcopy> --add-symbol
207-
"${entrypoint_name}=.llvm.libc.entrypoint.${entrypoint_name}:${alias_attributes}"
208-
${object_file_raw} ${object_file}
209-
)
210-
211-
add_custom_target(
212-
${fq_target_name}
213-
ALL
214-
DEPENDS ${object_file}
215-
)
216182
set_target_properties(
217183
${fq_target_name}
218184
PROPERTIES
219185
"ENTRYPOINT_NAME" ${entrypoint_name}
220186
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
221-
"OBJECT_FILE" "${object_file}"
222-
"OBJECT_FILE_RAW" "${object_file_raw}"
187+
"OBJECT_FILE" $<TARGET_OBJECTS:${fq_target_name}>
188+
# TODO: We don't need to list internal object files if the internal
189+
# target is a normal static library.
190+
"OBJECT_FILE_RAW" $<TARGET_OBJECTS:${internal_target_name}>
223191
"DEPS" "${fq_deps_list}"
224192
)
225193

@@ -273,7 +241,7 @@ function(add_entrypoint_object target_name)
273241
# crossplatform touch.
274242
COMMAND "${CMAKE_COMMAND}" -E touch ${lint_timestamp}
275243
COMMENT "Linting... ${target_name}"
276-
DEPENDS clang-tidy ${objects_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS}
244+
DEPENDS clang-tidy ${internal_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS}
277245
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
278246
)
279247

libc/src/__support/common.h.def

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@
1515
#define unlikely(x) __builtin_expect (x, 0)
1616
#define UNUSED __attribute__((unused))
1717

18-
<!> Include the platform specific definitions at build time. For example, that
19-
<!> of entrypoint macro.
20-
%%include_file(${platform_defs})
18+
#ifndef LLVM_LIBC_FUNCTION_ATTR
19+
#define LLVM_LIBC_FUNCTION_ATTR
20+
#endif
21+
22+
#ifdef LLVM_LIBC_PUBLIC_PACKAGING
23+
#define LLVM_LIBC_FUNCTION(type, name, arglist) \
24+
type name arglist; \
25+
LLVM_LIBC_FUNCTION_ATTR decltype(__llvm_libc::name) __##name##_impl__ __asm__(#name); \
26+
decltype(__llvm_libc::name) name [[gnu::alias(#name)]]; \
27+
type __##name##_impl__ arglist
28+
#else
29+
#define LLVM_LIBC_FUNCTION(type, name, arglist)\
30+
type name arglist
31+
#endif
2132

2233
#endif // LLVM_LIBC_SUPPORT_COMMON_H

libc/src/assert/__assert_fail.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ static void writeToStderr(const char *s) {
2424
__llvm_libc::syscall(SYS_write, 2, s, length);
2525
}
2626

27-
void LLVM_LIBC_ENTRYPOINT(__assert_fail)(const char *assertion, const char *file,
28-
unsigned line, const char *function) {
27+
LLVM_LIBC_FUNCTION(void, __assert_fail,
28+
(const char *assertion, const char *file, unsigned line,
29+
const char *function)) {
2930
writeToStderr(file);
3031
writeToStderr(": Assertion failed: '");
3132
writeToStderr(assertion);

libc/src/ctype/isalnum.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(isalnum)(int c) { return internal::isalnum(c); }
18+
LLVM_LIBC_FUNCTION(int, isalnum, (int c)) { return internal::isalnum(c); }
1919

2020
} // namespace __llvm_libc

libc/src/ctype/isalpha.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(isalpha)(int c) { return internal::isalpha(c); }
18+
LLVM_LIBC_FUNCTION(int, isalpha, (int c)) { return internal::isalpha(c); }
1919

2020
} // namespace __llvm_libc

libc/src/ctype/isblank.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace __llvm_libc {
1414

1515
// TODO: Currently restricted to default locale.
1616
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(isblank)(int c) {
17+
LLVM_LIBC_FUNCTION(int, isblank, (int c)) {
1818
const unsigned char ch = c;
1919
return ch == ' ' || ch == '\t';
2020
}

libc/src/ctype/iscntrl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace __llvm_libc {
1414

1515
// TODO: Currently restricted to default locale.
1616
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(iscntrl)(int c) {
17+
LLVM_LIBC_FUNCTION(int, iscntrl, (int c)) {
1818
const unsigned char ch = c;
1919
return ch < 0x20 || ch == 0x7f;
2020
}

libc/src/ctype/isdigit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ namespace __llvm_libc {
1414

1515
// TODO: Currently restricted to default locale.
1616
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(isdigit)(int c) { return internal::isdigit(c); }
17+
LLVM_LIBC_FUNCTION(int, isdigit, (int c)) { return internal::isdigit(c); }
1818

1919
} // namespace __llvm_libc

libc/src/ctype/isgraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(isgraph)(int c) { return internal::isgraph(c); }
18+
LLVM_LIBC_FUNCTION(int, isgraph, (int c)) { return internal::isgraph(c); }
1919

2020
} // namespace __llvm_libc

libc/src/ctype/islower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(islower)(int c) { return internal::islower(c); }
18+
LLVM_LIBC_FUNCTION(int, islower, (int c)) { return internal::islower(c); }
1919

2020
} // namespace __llvm_libc

libc/src/ctype/isprint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace __llvm_libc {
1414

1515
// TODO: Currently restricted to default locale.
1616
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(isprint)(int c) {
17+
LLVM_LIBC_FUNCTION(int, isprint, (int c)) {
1818
const unsigned ch = c;
1919
return (ch - ' ') < 95;
2020
}

libc/src/ctype/ispunct.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(ispunct)(int c) {
18+
LLVM_LIBC_FUNCTION(int, ispunct, (int c)) {
1919
return !internal::isalnum(c) && internal::isgraph(c);
2020
}
2121

libc/src/ctype/isspace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace __llvm_libc {
1414

1515
// TODO: Currently restricted to default locale.
1616
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(isspace)(int c) {
17+
LLVM_LIBC_FUNCTION(int, isspace, (int c)) {
1818
const unsigned ch = c;
1919
return ch == ' ' || (ch - '\t') < 5;
2020
}

libc/src/ctype/isupper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(isupper)(int c) { return internal::isupper(c); }
18+
LLVM_LIBC_FUNCTION(int, isupper, (int c)) { return internal::isupper(c); }
1919

2020
} // namespace __llvm_libc

libc/src/ctype/isxdigit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(isxdigit)(int c) {
18+
LLVM_LIBC_FUNCTION(int, isxdigit, (int c)) {
1919
const unsigned ch = c;
2020
return internal::isdigit(ch) || (ch | 32) - 'a' < 6;
2121
}

libc/src/ctype/tolower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(tolower)(int c) {
18+
LLVM_LIBC_FUNCTION(int, tolower, (int c)) {
1919
if (internal::isupper(c))
2020
return c + 'a' - 'A';
2121
return c;

libc/src/ctype/toupper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace __llvm_libc {
1515

1616
// TODO: Currently restricted to default locale.
1717
// These should be extended using locale information.
18-
int LLVM_LIBC_ENTRYPOINT(toupper)(int c) {
18+
LLVM_LIBC_FUNCTION(int, toupper, (int c)) {
1919
if (internal::islower(c))
2020
return c + 'A' - 'a';
2121
return c;

libc/src/errno/__errno_location.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ static thread_local int __errno = 0;
1717
// __errno_location is not really an entry point but we still want it to behave
1818
// like an entry point because the errno macro resolves to the C symbol
1919
// "__errno_location".
20-
int *LLVM_LIBC_ENTRYPOINT(__errno_location)() { return &__errno; }
20+
LLVM_LIBC_FUNCTION(int *, __errno_location, ()) { return &__errno; }
2121

2222
} // namespace __llvm_libc

libc/src/fenv/feclearexcept.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
int LLVM_LIBC_ENTRYPOINT(feclearexcept)(int e) {
14+
LLVM_LIBC_FUNCTION(int, feclearexcept, (int e)) {
1515
return fputil::clearExcept(e);
1616
}
1717

libc/src/fenv/fegetround.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace __llvm_libc {
1313

14-
int LLVM_LIBC_ENTRYPOINT(fegetround)() { return fputil::getRound(); }
14+
LLVM_LIBC_FUNCTION(int, fegetround, ()) { return fputil::getRound(); }
1515

1616
} // namespace __llvm_libc

libc/src/fenv/feraiseexcept.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
int LLVM_LIBC_ENTRYPOINT(feraiseexcept)(int e) {
14+
LLVM_LIBC_FUNCTION(int, feraiseexcept, (int e)) {
1515
return fputil::raiseExcept(e);
1616
}
1717

libc/src/fenv/fesetround.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace __llvm_libc {
1313

14-
int LLVM_LIBC_ENTRYPOINT(fesetround)(int m) { return fputil::setRound(m); }
14+
LLVM_LIBC_FUNCTION(int, fesetround, (int m)) { return fputil::setRound(m); }
1515

1616
} // namespace __llvm_libc

libc/src/fenv/fetestexcept.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace __llvm_libc {
1313

14-
int LLVM_LIBC_ENTRYPOINT(fetestexcept)(int e) { return fputil::testExcept(e); }
14+
LLVM_LIBC_FUNCTION(int, fetestexcept, (int e)) { return fputil::testExcept(e); }
1515

1616
} // namespace __llvm_libc

libc/src/math/ceil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace __llvm_libc {
1313

14-
double LLVM_LIBC_ENTRYPOINT(ceil)(double x) { return fputil::ceil(x); }
14+
LLVM_LIBC_FUNCTION(double, ceil, (double x)) { return fputil::ceil(x); }
1515

1616
} // namespace __llvm_libc

libc/src/math/ceilf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace __llvm_libc {
1313

14-
float LLVM_LIBC_ENTRYPOINT(ceilf)(float x) { return fputil::ceil(x); }
14+
LLVM_LIBC_FUNCTION(float, ceilf, (float x)) { return fputil::ceil(x); }
1515

1616
} // namespace __llvm_libc

libc/src/math/ceill.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
long double LLVM_LIBC_ENTRYPOINT(ceill)(long double x) {
14+
LLVM_LIBC_FUNCTION(long double, ceill, (long double x)) {
1515
return fputil::ceil(x);
1616
}
1717

libc/src/math/copysign.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
double LLVM_LIBC_ENTRYPOINT(copysign)(double x, double y) {
14+
LLVM_LIBC_FUNCTION(double, copysign, (double x, double y)) {
1515
return fputil::copysign(x, y);
1616
}
1717

libc/src/math/copysignf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
float LLVM_LIBC_ENTRYPOINT(copysignf)(float x, float y) {
14+
LLVM_LIBC_FUNCTION(float, copysignf, (float x, float y)) {
1515
return fputil::copysign(x, y);
1616
}
1717

libc/src/math/copysignl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace __llvm_libc {
1313

14-
long double LLVM_LIBC_ENTRYPOINT(copysignl)(long double x, long double y) {
14+
LLVM_LIBC_FUNCTION(long double, copysignl, (long double x, long double y)) {
1515
return fputil::copysign(x, y);
1616
}
1717

libc/src/math/cosf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace __llvm_libc {
2020
// error is 0.5303 * 2^-23. A single-step range reduction is used for
2121
// small values. Large inputs have their range reduced using fast integer
2222
// arithmetic.
23-
float LLVM_LIBC_ENTRYPOINT(cosf)(float y) {
23+
LLVM_LIBC_FUNCTION(float, cosf, (float y)) {
2424
double x = y;
2525
double s;
2626
int n;

libc/src/math/exp2f.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace __llvm_libc {
2222

23-
float LLVM_LIBC_ENTRYPOINT(exp2f)(float x) {
23+
LLVM_LIBC_FUNCTION(float, exp2f, (float x)) {
2424
uint32_t abstop;
2525
uint64_t ki, t;
2626
// double_t for better performance on targets with FLT_EVAL_METHOD==2.

libc/src/math/expf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace __llvm_libc {
2323

24-
float LLVM_LIBC_ENTRYPOINT(expf)(float x) {
24+
LLVM_LIBC_FUNCTION(float, expf, (float x)) {
2525
uint32_t abstop;
2626
uint64_t ki, t;
2727
// double_t for better performance on targets with FLT_EVAL_METHOD == 2.

0 commit comments

Comments
 (0)