1*9356374aSAndroid Build Coastguard Worker // Copyright 2018 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker // 3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker // 7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker // 9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker // limitations under the License. 14*9356374aSAndroid Build Coastguard Worker 15*9356374aSAndroid Build Coastguard Worker // This file contains internal parts of the Abseil symbolizer. 16*9356374aSAndroid Build Coastguard Worker // Do not depend on the anything in this file, it may change at anytime. 17*9356374aSAndroid Build Coastguard Worker 18*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ 19*9356374aSAndroid Build Coastguard Worker #define ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ 20*9356374aSAndroid Build Coastguard Worker 21*9356374aSAndroid Build Coastguard Worker #ifdef __cplusplus 22*9356374aSAndroid Build Coastguard Worker 23*9356374aSAndroid Build Coastguard Worker #include <cstddef> 24*9356374aSAndroid Build Coastguard Worker #include <cstdint> 25*9356374aSAndroid Build Coastguard Worker 26*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h" 27*9356374aSAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 28*9356374aSAndroid Build Coastguard Worker 29*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 30*9356374aSAndroid Build Coastguard Worker #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set 31*9356374aSAndroid Build Coastguard Worker #elif defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) \ 32*9356374aSAndroid Build Coastguard Worker && !defined(__asmjs__) && !defined(__wasm__) 33*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1 34*9356374aSAndroid Build Coastguard Worker 35*9356374aSAndroid Build Coastguard Worker #include <elf.h> 36*9356374aSAndroid Build Coastguard Worker #include <link.h> // For ElfW() macro. 37*9356374aSAndroid Build Coastguard Worker #include <functional> 38*9356374aSAndroid Build Coastguard Worker #include <string> 39*9356374aSAndroid Build Coastguard Worker 40*9356374aSAndroid Build Coastguard Worker namespace absl { 41*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN 42*9356374aSAndroid Build Coastguard Worker namespace debugging_internal { 43*9356374aSAndroid Build Coastguard Worker 44*9356374aSAndroid Build Coastguard Worker // Iterates over all sections, invoking callback on each with the section name 45*9356374aSAndroid Build Coastguard Worker // and the section header. 46*9356374aSAndroid Build Coastguard Worker // 47*9356374aSAndroid Build Coastguard Worker // Returns true on success; otherwise returns false in case of errors. 48*9356374aSAndroid Build Coastguard Worker // 49*9356374aSAndroid Build Coastguard Worker // This is not async-signal-safe. 50*9356374aSAndroid Build Coastguard Worker bool ForEachSection(int fd, 51*9356374aSAndroid Build Coastguard Worker const std::function<bool(absl::string_view name, 52*9356374aSAndroid Build Coastguard Worker const ElfW(Shdr) &)>& callback); 53*9356374aSAndroid Build Coastguard Worker 54*9356374aSAndroid Build Coastguard Worker // Gets the section header for the given name, if it exists. Returns true on 55*9356374aSAndroid Build Coastguard Worker // success. Otherwise, returns false. 56*9356374aSAndroid Build Coastguard Worker bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, 57*9356374aSAndroid Build Coastguard Worker ElfW(Shdr) *out); 58*9356374aSAndroid Build Coastguard Worker 59*9356374aSAndroid Build Coastguard Worker } // namespace debugging_internal 60*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 61*9356374aSAndroid Build Coastguard Worker } // namespace absl 62*9356374aSAndroid Build Coastguard Worker 63*9356374aSAndroid Build Coastguard Worker #endif // ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 64*9356374aSAndroid Build Coastguard Worker 65*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 66*9356374aSAndroid Build Coastguard Worker #error ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE cannot be directly set 67*9356374aSAndroid Build Coastguard Worker #elif defined(__APPLE__) 68*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_HAVE_DARWIN_SYMBOLIZE 1 69*9356374aSAndroid Build Coastguard Worker #endif 70*9356374aSAndroid Build Coastguard Worker 71*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE 72*9356374aSAndroid Build Coastguard Worker #error ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE cannot be directly set 73*9356374aSAndroid Build Coastguard Worker #elif defined(__EMSCRIPTEN__) 74*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_HAVE_EMSCRIPTEN_SYMBOLIZE 1 75*9356374aSAndroid Build Coastguard Worker #endif 76*9356374aSAndroid Build Coastguard Worker 77*9356374aSAndroid Build Coastguard Worker namespace absl { 78*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN 79*9356374aSAndroid Build Coastguard Worker namespace debugging_internal { 80*9356374aSAndroid Build Coastguard Worker 81*9356374aSAndroid Build Coastguard Worker struct SymbolDecoratorArgs { 82*9356374aSAndroid Build Coastguard Worker // The program counter we are getting symbolic name for. 83*9356374aSAndroid Build Coastguard Worker const void *pc; 84*9356374aSAndroid Build Coastguard Worker // 0 for main executable, load address for shared libraries. 85*9356374aSAndroid Build Coastguard Worker ptrdiff_t relocation; 86*9356374aSAndroid Build Coastguard Worker // Read-only file descriptor for ELF image covering "pc", 87*9356374aSAndroid Build Coastguard Worker // or -1 if no such ELF image exists in /proc/self/maps. 88*9356374aSAndroid Build Coastguard Worker int fd; 89*9356374aSAndroid Build Coastguard Worker // Output buffer, size. 90*9356374aSAndroid Build Coastguard Worker // Note: the buffer may not be empty -- default symbolizer may have already 91*9356374aSAndroid Build Coastguard Worker // produced some output, and earlier decorators may have adorned it in 92*9356374aSAndroid Build Coastguard Worker // some way. You are free to replace or augment the contents (within the 93*9356374aSAndroid Build Coastguard Worker // symbol_buf_size limit). 94*9356374aSAndroid Build Coastguard Worker char *const symbol_buf; 95*9356374aSAndroid Build Coastguard Worker size_t symbol_buf_size; 96*9356374aSAndroid Build Coastguard Worker // Temporary scratch space, size. 97*9356374aSAndroid Build Coastguard Worker // Use that space in preference to allocating your own stack buffer to 98*9356374aSAndroid Build Coastguard Worker // conserve stack. 99*9356374aSAndroid Build Coastguard Worker char *const tmp_buf; 100*9356374aSAndroid Build Coastguard Worker size_t tmp_buf_size; 101*9356374aSAndroid Build Coastguard Worker // User-provided argument 102*9356374aSAndroid Build Coastguard Worker void* arg; 103*9356374aSAndroid Build Coastguard Worker }; 104*9356374aSAndroid Build Coastguard Worker using SymbolDecorator = void (*)(const SymbolDecoratorArgs *); 105*9356374aSAndroid Build Coastguard Worker 106*9356374aSAndroid Build Coastguard Worker // Installs a function-pointer as a decorator. Returns a value less than zero 107*9356374aSAndroid Build Coastguard Worker // if the system cannot install the decorator. Otherwise, returns a unique 108*9356374aSAndroid Build Coastguard Worker // identifier corresponding to the decorator. This identifier can be used to 109*9356374aSAndroid Build Coastguard Worker // uninstall the decorator - See RemoveSymbolDecorator() below. 110*9356374aSAndroid Build Coastguard Worker int InstallSymbolDecorator(SymbolDecorator decorator, void* arg); 111*9356374aSAndroid Build Coastguard Worker 112*9356374aSAndroid Build Coastguard Worker // Removes a previously installed function-pointer decorator. Parameter "ticket" 113*9356374aSAndroid Build Coastguard Worker // is the return-value from calling InstallSymbolDecorator(). 114*9356374aSAndroid Build Coastguard Worker bool RemoveSymbolDecorator(int ticket); 115*9356374aSAndroid Build Coastguard Worker 116*9356374aSAndroid Build Coastguard Worker // Remove all installed decorators. Returns true if successful, false if 117*9356374aSAndroid Build Coastguard Worker // symbolization is currently in progress. 118*9356374aSAndroid Build Coastguard Worker bool RemoveAllSymbolDecorators(); 119*9356374aSAndroid Build Coastguard Worker 120*9356374aSAndroid Build Coastguard Worker // Registers an address range to a file mapping. 121*9356374aSAndroid Build Coastguard Worker // 122*9356374aSAndroid Build Coastguard Worker // Preconditions: 123*9356374aSAndroid Build Coastguard Worker // start <= end 124*9356374aSAndroid Build Coastguard Worker // filename != nullptr 125*9356374aSAndroid Build Coastguard Worker // 126*9356374aSAndroid Build Coastguard Worker // Returns true if the file was successfully registered. 127*9356374aSAndroid Build Coastguard Worker bool RegisterFileMappingHint(const void* start, const void* end, 128*9356374aSAndroid Build Coastguard Worker uint64_t offset, const char* filename); 129*9356374aSAndroid Build Coastguard Worker 130*9356374aSAndroid Build Coastguard Worker // Looks up the file mapping registered by RegisterFileMappingHint for an 131*9356374aSAndroid Build Coastguard Worker // address range. If there is one, the file name is stored in *filename and 132*9356374aSAndroid Build Coastguard Worker // *start and *end are modified to reflect the registered mapping. Returns 133*9356374aSAndroid Build Coastguard Worker // whether any hint was found. 134*9356374aSAndroid Build Coastguard Worker bool GetFileMappingHint(const void** start, const void** end, uint64_t* offset, 135*9356374aSAndroid Build Coastguard Worker const char** filename); 136*9356374aSAndroid Build Coastguard Worker 137*9356374aSAndroid Build Coastguard Worker } // namespace debugging_internal 138*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END 139*9356374aSAndroid Build Coastguard Worker } // namespace absl 140*9356374aSAndroid Build Coastguard Worker 141*9356374aSAndroid Build Coastguard Worker #endif // __cplusplus 142*9356374aSAndroid Build Coastguard Worker 143*9356374aSAndroid Build Coastguard Worker #include <stdbool.h> 144*9356374aSAndroid Build Coastguard Worker 145*9356374aSAndroid Build Coastguard Worker #ifdef __cplusplus 146*9356374aSAndroid Build Coastguard Worker extern "C" 147*9356374aSAndroid Build Coastguard Worker #endif // __cplusplus 148*9356374aSAndroid Build Coastguard Worker 149*9356374aSAndroid Build Coastguard Worker bool 150*9356374aSAndroid Build Coastguard Worker AbslInternalGetFileMappingHint(const void** start, const void** end, 151*9356374aSAndroid Build Coastguard Worker uint64_t* offset, const char** filename); 152*9356374aSAndroid Build Coastguard Worker 153*9356374aSAndroid Build Coastguard Worker #endif // ABSL_DEBUGGING_INTERNAL_SYMBOLIZE_H_ 154