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