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