Skip to content

Commit 87b72c9

Browse files
committed
Add support for crtbegin and crtend
crtbegin is responsible for: - Calling __register_frame_info which is used by old unwinders. We disable this behavior because we compile libunwind which discovers .eh_frame using dl_iterate_phdr. - Calling .ctors callbacks if CRT doesn't do it In newer compiler .ctors section is placed after .init_array and callbacks are called as if they were from .init_array by the CRT). We disable this behavior because our CRT has support for .init_array and our linker places .ctors next to .init_array. - Calling __cxa_finalize for the current dso_handle. Which calls destructors registerd by __cxa_atexit. We want this behavior.
1 parent 66373c1 commit 87b72c9

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

runtimes/compiler-rt/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@ alias(
33
actual = "@compiler-rt//:clang_rt.builtins.static",
44
visibility = ["//visibility:public"],
55
)
6+
7+
alias(
8+
name = "clang_rt.crtbegin",
9+
actual = "@compiler-rt//:clang_rt.crtbegin.static",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
alias(
14+
name = "clang_rt.crtend",
15+
actual = "@compiler-rt//:clang_rt.crtend.static",
16+
visibility = ["//visibility:public"],
17+
)

third_party/llvm-project/20.1.5/compiler-rt/BUILD.tpl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,49 @@ cc_stage2_static_library(
431431
],
432432
visibility = ["//visibility:public"],
433433
)
434+
435+
CRT_CFLAGS = [
436+
"-std=c11",
437+
"-fPIC",
438+
]
439+
440+
CRT_DEFINES = [
441+
"CRT_HAS_INITFINI_ARRAY",
442+
"CRT_USE_FRAME_REGISTRY",
443+
]
444+
445+
cc_stage2_library(
446+
name = "clang_rt.crtbegin",
447+
srcs = [
448+
"lib/builtins/crtbegin.c",
449+
],
450+
copts = CRT_CFLAGS,
451+
local_defines = CRT_DEFINES,
452+
visibility = ["//visibility:public"],
453+
)
454+
455+
cc_stage2_static_library(
456+
name = "clang_rt.crtbegin.static",
457+
deps = [
458+
":clang_rt.crtbegin",
459+
],
460+
visibility = ["//visibility:public"],
461+
)
462+
463+
cc_stage2_library(
464+
name = "clang_rt.crtend",
465+
srcs = [
466+
"lib/builtins/crtend.c",
467+
],
468+
copts = CRT_CFLAGS,
469+
local_defines = CRT_DEFINES,
470+
visibility = ["//visibility:public"],
471+
)
472+
473+
cc_stage2_static_library(
474+
name = "clang_rt.crtend.static",
475+
deps = [
476+
":clang_rt.crtend",
477+
],
478+
visibility = ["//visibility:public"],
479+
)

toolchain/BUILD.bazel

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ cc_args_list(
4444
],
4545
"@platforms//os:linux": [
4646
"//toolchain/args/linux:crt1",
47+
"//toolchain/args/linux:crtbegin",
4748
"//toolchain/args/linux:linux_default_libs",
4849
],
4950
"//conditions:default": [],
@@ -133,12 +134,26 @@ cc_toolchain(
133134
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
134135
"//toolchain/features:all_non_legacy_builtin_features",
135136
"//toolchain/features/legacy:all_legacy_builtin_features",
136-
],
137+
] + select({
138+
# Should be last. This is a workaround to add those args last.
139+
# See comment of this target.
140+
"@platforms//os:linux": [
141+
"//toolchain/args/linux:crtend_feature",
142+
],
143+
"//conditions:default": [],
144+
}),
137145
enabled_features = [
138146
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
139147
# Do not enable this manually. Those features are enabled internally by --compilation_mode flags family.
140148
"//toolchain/features/legacy:all_legacy_builtin_features",
141-
],
149+
] + select({
150+
# Should be last. This is a workaround to add those args last.
151+
# See comment of this target.
152+
"@platforms//os:linux": [
153+
"//toolchain/args/linux:crtend_feature",
154+
],
155+
"//conditions:default": [],
156+
}),
142157
tool_map = "//toolchain/llvm:all_tools",
143158
compiler = "clang",
144159
)

toolchain/args/linux/BUILD.bazel

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
3+
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
34
load("@rules_cc//cc/toolchains/impl:documented_api.bzl", "cc_args_list")
45

56
package(default_visibility = ["//visibility:public"])
@@ -276,6 +277,57 @@ alias(
276277
})
277278
)
278279

280+
cc_args(
281+
name = "crtbegin",
282+
actions = [
283+
"@rules_cc//cc/toolchains/actions:link_actions",
284+
],
285+
args = [
286+
"-Wl,--whole-archive",
287+
"{crtbegin}",
288+
"-Wl,--no-whole-archive",
289+
],
290+
format = {
291+
"crtbegin": "//runtimes/compiler-rt:clang_rt.crtbegin",
292+
},
293+
data = [
294+
"//runtimes/compiler-rt:clang_rt.crtbegin",
295+
],
296+
)
297+
298+
#FIXME: Remove this
299+
# crtbegin and crtend must wrap all libraries_to_link but for now, we can't
300+
# use libraries_to_link as args because of legacy features not yet ported to args.
301+
#
302+
# As soon as it is the case, we can remove this cc_feature and use libraries_to_link
303+
# as args in the cc_toolchain args attributes, wrapped between crtbegin and crtend.
304+
cc_feature(
305+
name = "crtend_feature",
306+
feature_name = "crtend",
307+
args = [
308+
"//toolchain/args/linux:crtend",
309+
],
310+
visibility = ["//visibility:public"],
311+
)
312+
313+
cc_args(
314+
name = "crtend",
315+
actions = [
316+
"@rules_cc//cc/toolchains/actions:link_actions",
317+
],
318+
args = [
319+
"-Wl,--whole-archive",
320+
"{crtend}",
321+
"-Wl,--no-whole-archive",
322+
],
323+
format = {
324+
"crtend": "//runtimes/compiler-rt:clang_rt.crtend",
325+
},
326+
data = [
327+
"//runtimes/compiler-rt:clang_rt.crtend",
328+
],
329+
)
330+
279331
alias(
280332
name = "linux_default_libs",
281333
actual = select({

0 commit comments

Comments
 (0)