| 6 |
| 7 | #include <arpa/inet.h> |
| 8 | #include <elf.h> |
| 9 |
| 10 | #include "base/bits.h" |
| 11 | #include "base/containers/span.h" |
| 12 | #include "base/hash/sha1.h" |
| 13 | #include "base/strings/safe_sprintf.h" |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | // NOTE: This code may be used in crash handling code, so the implementation |
| 17 | // must avoid dynamic memory allocation or using data structures which rely on |
| 18 | // dynamic allocation. |
| 19 |
| 20 | namespace base { |
| 21 | namespace debug { |
| 22 | namespace { |
| 23 |
| 24 | #if __SIZEOF_POINTER__ == 4 |
| 25 | using Ehdr = Elf32_Ehdr; |
| 26 | using Dyn = Elf32_Dyn; |
| 27 | using Half = Elf32_Half; |
| 28 | using Nhdr = Elf32_Nhdr; |
| 29 | using Word = Elf32_Word; |
| 30 | #else |
| 31 | using Ehdr = Elf64_Ehdr; |
| 32 | using Dyn = Elf64_Dyn; |
| 33 | using Half = Elf64_Half; |
| 34 | using Nhdr = Elf64_Nhdr; |
| 35 | using Word = Elf64_Word; |
| 36 | #endif |
| 37 |
| 38 | constexpr char kGnuNoteName[] = "GNU"; |
| 39 |
| 40 | // Returns a pointer to the header of the ELF binary mapped into memory, |
| 41 | // or a null pointer if the header is invalid. |
| 42 | const Ehdr* GetElfHeader(const void* elf_mapped_base) { |
| 43 | const char* elf_base = reinterpret_cast<const char*>(elf_mapped_base); |
| 44 | if (strncmp(elf_base, ELFMAG, SELFMAG) != 0) |
| 45 | return nullptr; |
| 46 |
| 47 | const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); |
| 48 | return elf_header; |
| 49 | } |
| 50 |
| 51 | // Returns the ELF base address that should be used as a starting point to |
| 52 | // access other segments. |
| 53 | const char* GetElfBaseVirtualAddress(const void* elf_mapped_base) { |
| 54 | const char* elf_base = reinterpret_cast<const char*>(elf_mapped_base); |
| 55 | for (const Phdr& header : GetElfProgramHeaders(elf_mapped_base)) { |
| 56 | if (header.p_type == PT_LOAD) { |
| 57 | size_t load_bias = static_cast<size_t>(header.p_vaddr); |
| 58 | CHECK_GE(reinterpret_cast<uintptr_t>(elf_base), load_bias); |
| 59 | return elf_base - load_bias; |
| 60 | } |
| 61 | } |
| 62 | return elf_base; |
| 63 | } |
| 64 |
| 65 | } // namespace |
| 66 |
| 67 | span<const Phdr> GetElfProgramHeaders(const void* elf_mapped_base) { |
| 68 | // NOTE: Function should use async signal safe calls only. |
| 69 |
| 70 | const char* elf_base = reinterpret_cast<const char*>(elf_mapped_base); |
| 71 | const Ehdr* elf_header = GetElfHeader(elf_mapped_base); |
| 72 | if (!elf_header) |
| 73 | return {}; |
| 74 |
| 75 | return span<const Phdr>( |
| 76 | reinterpret_cast<const Phdr*>(elf_base + elf_header->e_phoff), |
| 77 | elf_header->e_phnum); |
| 78 | } |
| 79 |
| 80 | size_t ReadElfBuildId(const void* elf_mapped_base, |
| 81 | bool uppercase, |
| 82 | ElfBuildIdBuffer build_id) { |
| 83 | // NOTE: Function should use async signal safe calls only. |
| 84 |
| 85 | const char* elf_virtual_base = GetElfBaseVirtualAddress(elf_mapped_base); |
| 86 | const Ehdr* elf_header = GetElfHeader(elf_mapped_base); |
| 87 | if (!elf_header) |
| 88 | return 0; |
| 89 |
| 90 | for (const Phdr& header : GetElfProgramHeaders(elf_mapped_base)) { |
| 91 | if (header.p_type != PT_NOTE) |
| 92 | continue; |
| 93 |
| 94 | // Look for a NT_GNU_BUILD_ID note with name == "GNU". |
| 95 | const char* current_section = elf_virtual_base + header.p_vaddr; |
| 96 | const char* section_end = current_section + header.p_memsz; |
| 97 | const Nhdr* current_note = nullptr; |
| 98 | bool found = false; |
| 99 | while (current_section < section_end) { |
| 100 | current_note = reinterpret_cast<const Nhdr*>(current_section); |
| 101 | if (current_note->n_type == NT_GNU_BUILD_ID) { |
| 102 | StringPiece note_name(current_section + sizeof(Nhdr), |
| 103 | current_note->n_namesz); |
| 104 | // Explicit constructor is used to include the '\0' character. |
| 105 | if (note_name == StringPiece(kGnuNoteName, sizeof(kGnuNoteName))) { |
| 106 | found = true; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 |
| 111 | size_t section_size = bits::Align(current_note->n_namesz, 4) + |
| 112 | bits::Align(current_note->n_descsz, 4) + |
| 113 | sizeof(Nhdr); |
| 114 | if (section_size > static_cast<size_t>(section_end - current_section)) |
| 115 | return 0; |
| 116 | current_section += section_size; |
| 117 | } |
| 118 |
| 119 | if (!found) |
| 120 | continue; |
| 121 |
| 122 | // Validate that the serialized build ID will fit inside |build_id|. |
| 123 | size_t note_size = current_note->n_descsz; |
| 124 | if ((note_size * 2) > kMaxBuildIdStringLength) |
| 125 | continue; |
| 126 |
| 127 | // Write out the build ID as a null-terminated hex string. |
| 128 | const uint8_t* build_id_raw = |
| 129 | reinterpret_cast<const uint8_t*>(current_note) + sizeof(Nhdr) + |
| 130 | bits::Align(current_note->n_namesz, 4); |
| 131 | size_t i = 0; |
| 132 | for (i = 0; i < current_note->n_descsz; ++i) { |
| 133 | strings::SafeSNPrintf(&build_id[i * 2], 3, (uppercase ? "%02X" : "%02x"), |
| 134 | build_id_raw[i]); |
| 135 | } |
| 136 | build_id[i * 2] = '\0'; |
| 137 |
| 138 | // Return the length of the string. |
| 139 | return i * 2; |
| 140 | } |
| 141 |
| 142 | return 0; |
| 143 | } |
| 144 |
| 145 | Optional<StringPiece> ReadElfLibraryName(const void* elf_mapped_base) { |
| 146 | // NOTE: Function should use async signal safe calls only. |
| 147 |
| 148 | const char* elf_base = reinterpret_cast<const char*>(elf_mapped_base); |
| 149 | const Ehdr* elf_header = GetElfHeader(elf_mapped_base); |
| 150 | if (!elf_header) |
| 151 | return {}; |
| 152 |
| 153 | for (const Phdr& header : GetElfProgramHeaders(elf_mapped_base)) { |
| 154 | if (header.p_type != PT_DYNAMIC) |
| 155 | continue; |
| 156 |
| 157 | // Read through the ELF dynamic sections to find the string table and |
| 158 | // SONAME offsets, which are used to compute the offset of the library |
| 159 | // name string. |
| 160 | const Dyn* dynamic_start = |
| 161 | reinterpret_cast<const Dyn*>(elf_base + header.p_vaddr); |
| 162 | const Dyn* dynamic_end = reinterpret_cast<const Dyn*>( |
| 163 | elf_base + header.p_vaddr + header.p_memsz); |
| 164 | Word soname_strtab_offset = 0; |
| 165 | const char* strtab_addr = 0; |
| 166 | for (const Dyn* dynamic_iter = dynamic_start; dynamic_iter < dynamic_end; |
| 167 | ++dynamic_iter) { |
| 168 | if (dynamic_iter->d_tag == DT_STRTAB) { |
| 169 | #if defined(OS_FUCHSIA) || defined(OS_ANDROID) |
| 170 | // Fuchsia and Android executables are position-independent, so treat |
| 171 | // pointers in the ELF header as offsets into the address space instead |
| 172 | // of absolute addresses. |
| 173 | strtab_addr = (size_t)dynamic_iter->d_un.d_ptr + (const char*)elf_base; |
| 174 | #else |
| 175 | strtab_addr = (const char*)dynamic_iter->d_un.d_ptr; |
| 176 | #endif |
| 177 | } else if (dynamic_iter->d_tag == DT_SONAME) { |
| 178 | soname_strtab_offset = dynamic_iter->d_un.d_val; |
| 179 | } |
| 180 | } |
| 181 | if (soname_strtab_offset && strtab_addr) |
| 182 | return StringPiece(strtab_addr + soname_strtab_offset); |
| 183 | } |
| 184 |
| 185 | return nullopt; |
| 186 | } |
| 187 |
| 188 | } // namespace debug |
| 189 | } // namespace base |
| 190 |