1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include "linker_debug.h"
30*8d67ca89SAndroid Build Coastguard Worker
31*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
32*8d67ca89SAndroid Build Coastguard Worker
33*8d67ca89SAndroid Build Coastguard Worker #include <android-base/strings.h>
34*8d67ca89SAndroid Build Coastguard Worker
35*8d67ca89SAndroid Build Coastguard Worker LinkerDebugConfig g_linker_debug_config;
36*8d67ca89SAndroid Build Coastguard Worker
init_LD_DEBUG(const std::string & value)37*8d67ca89SAndroid Build Coastguard Worker void init_LD_DEBUG(const std::string& value) {
38*8d67ca89SAndroid Build Coastguard Worker if (value.empty()) return;
39*8d67ca89SAndroid Build Coastguard Worker std::vector<std::string> options = android::base::Split(value, ",");
40*8d67ca89SAndroid Build Coastguard Worker for (const auto& o : options) {
41*8d67ca89SAndroid Build Coastguard Worker if (o == "calls") g_linker_debug_config.calls = true;
42*8d67ca89SAndroid Build Coastguard Worker else if (o == "cfi") g_linker_debug_config.cfi = true;
43*8d67ca89SAndroid Build Coastguard Worker else if (o == "dynamic") g_linker_debug_config.dynamic = true;
44*8d67ca89SAndroid Build Coastguard Worker else if (o == "lookup") g_linker_debug_config.lookup = true;
45*8d67ca89SAndroid Build Coastguard Worker else if (o == "props") g_linker_debug_config.props = true;
46*8d67ca89SAndroid Build Coastguard Worker else if (o == "reloc") g_linker_debug_config.reloc = true;
47*8d67ca89SAndroid Build Coastguard Worker else if (o == "statistics") g_linker_debug_config.statistics = true;
48*8d67ca89SAndroid Build Coastguard Worker else if (o == "timing") g_linker_debug_config.timing = true;
49*8d67ca89SAndroid Build Coastguard Worker else if (o == "all") {
50*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.calls = true;
51*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.cfi = true;
52*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.dynamic = true;
53*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.lookup = true;
54*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.props = true;
55*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.reloc = true;
56*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.statistics = true;
57*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.timing = true;
58*8d67ca89SAndroid Build Coastguard Worker } else {
59*8d67ca89SAndroid Build Coastguard Worker __linker_error("$LD_DEBUG is a comma-separated list of:\n"
60*8d67ca89SAndroid Build Coastguard Worker "\n"
61*8d67ca89SAndroid Build Coastguard Worker " calls ctors/dtors/ifuncs\n"
62*8d67ca89SAndroid Build Coastguard Worker " cfi control flow integrity messages\n"
63*8d67ca89SAndroid Build Coastguard Worker " dynamic dynamic section processing\n"
64*8d67ca89SAndroid Build Coastguard Worker " lookup symbol lookup\n"
65*8d67ca89SAndroid Build Coastguard Worker " props ELF property processing\n"
66*8d67ca89SAndroid Build Coastguard Worker " reloc relocation resolution\n"
67*8d67ca89SAndroid Build Coastguard Worker " statistics relocation statistics\n"
68*8d67ca89SAndroid Build Coastguard Worker " timing timing information\n"
69*8d67ca89SAndroid Build Coastguard Worker "\n"
70*8d67ca89SAndroid Build Coastguard Worker "or 'all' for all of the above.\n");
71*8d67ca89SAndroid Build Coastguard Worker }
72*8d67ca89SAndroid Build Coastguard Worker }
73*8d67ca89SAndroid Build Coastguard Worker if (g_linker_debug_config.calls || g_linker_debug_config.cfi ||
74*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.dynamic || g_linker_debug_config.lookup ||
75*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.props || g_linker_debug_config.reloc ||
76*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.statistics || g_linker_debug_config.timing) {
77*8d67ca89SAndroid Build Coastguard Worker g_linker_debug_config.any = true;
78*8d67ca89SAndroid Build Coastguard Worker }
79*8d67ca89SAndroid Build Coastguard Worker }
80*8d67ca89SAndroid Build Coastguard Worker
linker_log_va_list(int prio,const char * fmt,va_list ap)81*8d67ca89SAndroid Build Coastguard Worker static void linker_log_va_list(int prio, const char* fmt, va_list ap) {
82*8d67ca89SAndroid Build Coastguard Worker va_list ap2;
83*8d67ca89SAndroid Build Coastguard Worker va_copy(ap2, ap);
84*8d67ca89SAndroid Build Coastguard Worker async_safe_format_log_va_list(prio, "linker", fmt, ap2);
85*8d67ca89SAndroid Build Coastguard Worker va_end(ap2);
86*8d67ca89SAndroid Build Coastguard Worker
87*8d67ca89SAndroid Build Coastguard Worker async_safe_format_fd_va_list(STDERR_FILENO, fmt, ap);
88*8d67ca89SAndroid Build Coastguard Worker write(STDERR_FILENO, "\n", 1);
89*8d67ca89SAndroid Build Coastguard Worker }
90*8d67ca89SAndroid Build Coastguard Worker
__linker_log(int prio,const char * fmt,...)91*8d67ca89SAndroid Build Coastguard Worker void __linker_log(int prio, const char* fmt, ...) {
92*8d67ca89SAndroid Build Coastguard Worker va_list ap;
93*8d67ca89SAndroid Build Coastguard Worker va_start(ap, fmt);
94*8d67ca89SAndroid Build Coastguard Worker linker_log_va_list(prio, fmt, ap);
95*8d67ca89SAndroid Build Coastguard Worker va_end(ap);
96*8d67ca89SAndroid Build Coastguard Worker }
97*8d67ca89SAndroid Build Coastguard Worker
__linker_error(const char * fmt,...)98*8d67ca89SAndroid Build Coastguard Worker void __linker_error(const char* fmt, ...) {
99*8d67ca89SAndroid Build Coastguard Worker va_list ap;
100*8d67ca89SAndroid Build Coastguard Worker va_start(ap, fmt);
101*8d67ca89SAndroid Build Coastguard Worker linker_log_va_list(ANDROID_LOG_FATAL, fmt, ap);
102*8d67ca89SAndroid Build Coastguard Worker va_end(ap);
103*8d67ca89SAndroid Build Coastguard Worker
104*8d67ca89SAndroid Build Coastguard Worker _exit(EXIT_FAILURE);
105*8d67ca89SAndroid Build Coastguard Worker }
106