1// ======================================================== 2// linker_wrapper - Linux Bionic (on the host) 3// ======================================================== 4 5// This is used for bionic on (host) Linux to bootstrap our linker embedded into 6// a binary. 7// 8// Host bionic binaries do not have a PT_INTERP section, instead this gets 9// embedded as the entry point, and the linker is embedded as ELF sections in 10// each binary. There's a linker script that sets all of that up (generated by 11// extract_linker), and defines the extern symbols used in this file. 12package { 13 default_applicable_licenses: ["bionic_linker_license"], 14} 15 16license { 17 name: "bionic_linker_license", 18 visibility: [":__subpackages__"], 19 license_kinds: [ 20 "SPDX-license-identifier-BSD", 21 ], 22 license_text: [ 23 "NOTICE", 24 ], 25} 26 27cc_object { 28 name: "linker_wrapper", 29 host_supported: true, 30 device_supported: false, 31 enabled: false, 32 target: { 33 linux_bionic: { 34 enabled: true, 35 }, 36 }, 37 38 cflags: [ 39 "-fno-stack-protector", 40 "-Wstrict-overflow=5", 41 "-fvisibility=hidden", 42 "-Wall", 43 "-Wextra", 44 "-Wno-unused", 45 "-Werror", 46 ], 47 48 srcs: [ 49 "linker_wrapper.cpp", 50 ], 51 arch: { 52 arm64: { 53 srcs: ["arch/arm64/linker_wrapper_begin.S"], 54 }, 55 x86_64: { 56 srcs: ["arch/x86_64/linker_wrapper_begin.S"], 57 }, 58 }, 59 60 header_libs: ["libc_headers"], 61 62 // We need to access Bionic private headers in the linker. 63 include_dirs: ["bionic/libc"], 64} 65 66// ======================================================== 67// linker default configuration 68// ======================================================== 69 70// Configuration for the linker binary and any of its static libraries. 71cc_defaults { 72 name: "linker_defaults", 73 arch: { 74 arm: { 75 cflags: ["-D__work_around_b_24465209__"], 76 }, 77 x86: { 78 cflags: ["-D__work_around_b_24465209__"], 79 }, 80 }, 81 82 cflags: [ 83 "-fno-stack-protector", 84 "-Wstrict-overflow=5", 85 "-fvisibility=hidden", 86 "-Wall", 87 "-Wextra", 88 "-Wunused", 89 "-Werror", 90 ], 91 92 // TODO: split out the asflags. 93 asflags: [ 94 "-fno-stack-protector", 95 "-Wstrict-overflow=5", 96 "-fvisibility=hidden", 97 "-Wall", 98 "-Wextra", 99 "-Wunused", 100 "-Werror", 101 ], 102 103 product_variables: { 104 debuggable: { 105 cppflags: ["-DUSE_LD_CONFIG_FILE"], 106 }, 107 }, 108 109 cppflags: ["-Wold-style-cast"], 110 111 static_libs: [ 112 "libziparchive", 113 "libbase", 114 "libz", 115 116 "libasync_safe", 117 118 "liblog", 119 ], 120 121 // We need to access Bionic private headers in the linker. 122 include_dirs: ["bionic/libc"], 123} 124 125// ======================================================== 126// linker components 127// ======================================================== 128 129// Enable a module on all targets the linker runs on (ordinary Android targets, Linux Bionic, and 130// native bridge implementations). 131cc_defaults { 132 name: "linker_all_targets", 133 defaults: ["linux_bionic_supported"], 134 recovery_available: true, 135 vendor_ramdisk_available: true, 136 native_bridge_supported: true, 137} 138 139cc_library_static { 140 name: "liblinker_main", 141 defaults: ["linker_defaults", "linker_all_targets"], 142 srcs: ["linker_main.cpp"], 143 144 // Ensure that the compiler won't insert string function calls before ifuncs are resolved. 145 cflags: ["-ffreestanding"], 146} 147 148cc_library_static { 149 name: "liblinker_malloc", 150 defaults: ["linker_defaults", "linker_all_targets"], 151 srcs: ["linker_memory.cpp"], 152} 153 154cc_library_static { 155 name: "liblinker_debuggerd_stub", 156 defaults: ["linker_defaults", "linker_all_targets"], 157 srcs: ["linker_debuggerd_stub.cpp"], 158} 159 160// ======================================================== 161// template for the linker binary 162// ======================================================== 163 164filegroup { 165 name: "linker_sources", 166 srcs: [ 167 "dlfcn.cpp", 168 "linker.cpp", 169 "linker_block_allocator.cpp", 170 "linker_dlwarning.cpp", 171 "linker_cfi.cpp", 172 "linker_config.cpp", 173 "linker_debug.cpp", 174 "linker_gdb_support.cpp", 175 "linker_globals.cpp", 176 "linker_libc_support.c", 177 "linker_libcxx_support.cpp", 178 "linker_namespaces.cpp", 179 "linker_logger.cpp", 180 "linker_mapped_file_fragment.cpp", 181 "linker_note_gnu_property.cpp", 182 "linker_phdr.cpp", 183 "linker_relocate.cpp", 184 "linker_sdk_versions.cpp", 185 "linker_soinfo.cpp", 186 "linker_transparent_hugepage_support.cpp", 187 "linker_tls.cpp", 188 "linker_utils.cpp", 189 "rt.cpp", 190 ], 191} 192 193filegroup { 194 name: "linker_sources_arm", 195 srcs: [ 196 "arch/arm/begin.S", 197 "arch/arm_neon/linker_gnu_hash_neon.cpp", 198 ], 199} 200 201filegroup { 202 name: "linker_sources_arm64", 203 srcs: [ 204 "arch/arm64/begin.S", 205 "arch/arm64/tlsdesc_resolver.S", 206 "arch/arm_neon/linker_gnu_hash_neon.cpp", 207 ], 208} 209 210filegroup { 211 name: "linker_sources_x86", 212 srcs: [ 213 "arch/x86/begin.S", 214 ], 215} 216 217filegroup { 218 name: "linker_sources_x86_64", 219 srcs: [ 220 "arch/x86_64/begin.S", 221 ], 222} 223 224cc_defaults { 225 name: "linker_version_script_overlay", 226 arch: { 227 arm: { version_script: "linker.arm.map" }, 228 arm64: { version_script: "linker.generic.map" }, 229 x86: { version_script: "linker.generic.map" }, 230 x86_64: { version_script: "linker.generic.map" }, 231 }, 232} 233 234// A template for the linker binary. May be inherited by native bridge implementations. 235cc_defaults { 236 name: "linker_bin_template", 237 defaults: ["linker_defaults"], 238 239 srcs: [":linker_sources"], 240 241 arch: { 242 arm: { 243 srcs: [":linker_sources_arm"], 244 }, 245 arm64: { 246 srcs: [":linker_sources_arm64"], 247 }, 248 x86: { 249 srcs: [":linker_sources_x86"], 250 }, 251 x86_64: { 252 srcs: [":linker_sources_x86_64"], 253 }, 254 }, 255 256 // -shared is used to overwrite the -Bstatic and -static flags triggered by enabling 257 // static_executable. This dynamic linker is actually a shared object linked with static 258 // libraries. 259 ldflags: [ 260 "-shared", 261 "-Wl,-Bsymbolic", 262 "-Wl,--exclude-libs,ALL", 263 "-Wl,-soname,ld-android.so", 264 ], 265 266 // we are going to link libc++_static manually because 267 // when stl is not set to "none" build system adds libdl 268 // to the list of static libraries which needs to be 269 // avoided in the case of building loader. 270 stl: "none", 271 272 // we don't want crtbegin.o (because we have begin.o), so unset it 273 // just for this module 274 nocrt: true, 275 276 static_executable: true, 277 278 // Leave the symbols in the shared library so that stack unwinders can produce 279 // meaningful name resolution. 280 strip: { 281 keep_symbols: true, 282 }, 283 284 // Insert an extra objcopy step to add prefix to symbols. This is needed to prevent gdb 285 // looking up symbols in the linker by mistake. 286 prefix_symbols: "__dl_", 287 288 sanitize: { 289 hwaddress: false, 290 }, 291 292 static_libs: [ 293 "liblinker_main", 294 "liblinker_malloc", 295 296 "libc++_static", 297 "libc_nomalloc", 298 "libc_dynamic_dispatch", 299 "libm", 300 "libunwind", 301 ], 302 303 // Ensure that if the linker needs __gnu_Unwind_Find_exidx, then the linker will have a 304 // definition of the symbol. The linker links against libgcc.a, whose arm32 unwinder has a weak 305 // reference to __gnu_Unwind_Find_exidx, which isn't sufficient to pull in the strong definition 306 // of __gnu_Unwind_Find_exidx from libc. An unresolved weak reference would create a 307 // non-relative dynamic relocation in the linker binary, which complicates linker startup. 308 // 309 // This line should be unnecessary because the linker's dependency on libunwind_llvm.a should 310 // override libgcc.a, but this line provides a simpler guarantee. It can be removed once the 311 // linker stops linking against libgcc.a's arm32 unwinder. 312 whole_static_libs: ["libc_unwind_static"], 313 314 system_shared_libs: [], 315 316 // Opt out of native_coverage when opting out of system_shared_libs 317 native_coverage: false, 318} 319 320// ======================================================== 321// linker[_asan][64] binary 322// ======================================================== 323 324cc_binary { 325 name: "linker", 326 defaults: [ 327 "linker_bin_template", 328 "linux_bionic_supported", 329 "linker_version_script_overlay", 330 ], 331 332 srcs: [ 333 "linker_translate_path.cpp", 334 ], 335 336 symlinks: ["linker_asan"], 337 multilib: { 338 lib64: { 339 suffix: "64", 340 }, 341 }, 342 343 compile_multilib: "both", 344 345 recovery_available: true, 346 vendor_ramdisk_available: true, 347 apex_available: [ 348 "//apex_available:platform", 349 "com.android.runtime", 350 ], 351 352 target: { 353 android: { 354 srcs: [ 355 "linker_debuggerd_android.cpp", 356 ], 357 static_libs: [ 358 "libc++demangle", 359 "libdebuggerd_handler_fallback", 360 ], 361 }, 362 linux_bionic: { 363 static_libs: [ 364 "liblinker_debuggerd_stub", 365 ], 366 }, 367 }, 368 369 afdo: true, 370} 371 372// ======================================================== 373// assorted modules 374// ======================================================== 375 376sh_binary { 377 name: "ldd", 378 src: "ldd.sh", 379} 380 381// Used to generate binaries that can be backed by transparent hugepages. 382cc_defaults { 383 name: "linker_hugepage_aligned", 384 arch: { 385 arm64: { 386 ldflags: ["-z max-page-size=0x200000"], 387 }, 388 x86_64: { 389 ldflags: ["-z max-page-size=0x200000"], 390 }, 391 }, 392} 393 394cc_library { 395 // NOTE: --exclude-libs=libgcc.a makes sure that any symbols ld-android.so pulls from 396 // libgcc.a are made static to ld-android.so. This in turn ensures that libraries that 397 // a) pull symbols from libgcc.a and b) depend on ld-android.so will not rely on ld-android.so 398 // to provide those symbols, but will instead pull them from libgcc.a. Specifically, 399 // we use this property to make sure libc.so has its own copy of the code from 400 // libgcc.a it uses. 401 // 402 // DO NOT REMOVE --exclude-libs! 403 404 ldflags: [ 405 "-Wl,--exclude-libs=libgcc.a", 406 "-Wl,--exclude-libs=libgcc_stripped.a", 407 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a", 408 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a", 409 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a", 410 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a", 411 ], 412 413 // for x86, exclude libgcc_eh.a for the same reasons as above 414 arch: { 415 x86: { 416 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], 417 }, 418 x86_64: { 419 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"], 420 }, 421 }, 422 423 srcs: ["ld_android.cpp"], 424 cflags: [ 425 "-Wall", 426 "-Wextra", 427 "-Wunused", 428 "-Werror", 429 ], 430 stl: "none", 431 432 name: "ld-android", 433 defaults: ["linux_bionic_supported", "linker_version_script_overlay"], 434 ramdisk_available: true, 435 vendor_ramdisk_available: true, 436 recovery_available: true, 437 native_bridge_supported: true, 438 439 nocrt: true, 440 system_shared_libs: [], 441 header_libs: ["libc_headers"], 442 443 // Opt out of native_coverage when opting out of system_shared_libs 444 native_coverage: false, 445 446 sanitize: { 447 never: true, 448 }, 449 450 apex_available: [ 451 "//apex_available:platform", 452 "com.android.runtime", 453 ], 454 455 lto: { 456 never: true, 457 }, 458} 459 460cc_test { 461 name: "linker-unit-tests", 462 test_suites: ["device-tests"], 463 464 cflags: [ 465 "-g", 466 "-Wall", 467 "-Wextra", 468 "-Wunused", 469 "-Werror", 470 ], 471 472 // We need to access Bionic private headers in the linker. 473 include_dirs: ["bionic/libc"], 474 475 srcs: [ 476 // Tests. 477 "linker_block_allocator_test.cpp", 478 "linker_config_test.cpp", 479 "linked_list_test.cpp", 480 "linker_note_gnu_property_test.cpp", 481 "linker_sleb128_test.cpp", 482 "linker_utils_test.cpp", 483 "linker_gnu_hash_test.cpp", 484 485 // Parts of the linker that we're testing. 486 "linker_block_allocator.cpp", 487 "linker_config.cpp", 488 "linker_debug.cpp", 489 "linker_note_gnu_property.cpp", 490 "linker_test_globals.cpp", 491 "linker_utils.cpp", 492 ], 493 494 static_libs: [ 495 "libasync_safe", 496 "libbase", 497 "liblog", 498 ], 499 500 arch: { 501 arm: { 502 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"], 503 }, 504 arm64: { 505 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"], 506 }, 507 }, 508} 509 510cc_benchmark { 511 name: "linker-benchmarks", 512 513 srcs: [ 514 "linker_gnu_hash_benchmark.cpp", 515 ], 516 517 arch: { 518 arm: { 519 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"], 520 }, 521 arm64: { 522 srcs: ["arch/arm_neon/linker_gnu_hash_neon.cpp"], 523 }, 524 }, 525} 526