blob: 764b9db86176edd01b173b5d3969c96708b357c0 [file] [log] [blame]
Christopher Ferrisa21bd932015-02-27 13:39:47 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "DEBUG"
18
19#include <elf.h>
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <string>
25
26#include <backtrace/Backtrace.h>
27#include <base/stringprintf.h>
28#include <log/log.h>
29
30#include "elf_utils.h"
31
32template <typename HdrType, typename PhdrType, typename NhdrType>
33static bool get_build_id(
34 Backtrace* backtrace, uintptr_t base_addr, uint8_t* e_ident, std::string* build_id) {
35 HdrType hdr;
36
37 memcpy(&hdr.e_ident[0], e_ident, EI_NIDENT);
38
39 // First read the rest of the header.
40 if (backtrace->Read(base_addr + EI_NIDENT, reinterpret_cast<uint8_t*>(&hdr) + EI_NIDENT,
41 sizeof(HdrType) - EI_NIDENT) != sizeof(HdrType) - EI_NIDENT) {
42 return false;
43 }
44
45 for (size_t i = 0; i < hdr.e_phnum; i++) {
46 PhdrType phdr;
47 if (backtrace->Read(base_addr + hdr.e_phoff + i * hdr.e_phentsize,
48 reinterpret_cast<uint8_t*>(&phdr), sizeof(phdr)) != sizeof(phdr)) {
49 return false;
50 }
51 // Looking for the .note.gnu.build-id note.
52 if (phdr.p_type == PT_NOTE) {
53 size_t hdr_size = phdr.p_filesz;
54 uintptr_t addr = base_addr + phdr.p_offset;
55 while (hdr_size >= sizeof(NhdrType)) {
56 NhdrType nhdr;
57 if (backtrace->Read(addr, reinterpret_cast<uint8_t*>(&nhdr), sizeof(nhdr)) != sizeof(nhdr)) {
58 return false;
59 }
60 addr += sizeof(nhdr);
61 if (nhdr.n_type == NT_GNU_BUILD_ID) {
62 // Skip the name (which is the owner and should be "GNU").
63 addr += nhdr.n_namesz;
64 uint8_t build_id_data[128];
65 if (nhdr.n_namesz > sizeof(build_id_data)) {
66 ALOGE("Possible corrupted note, name size value is too large: %u",
67 nhdr.n_namesz);
68 return false;
69 }
70 if (backtrace->Read(addr, build_id_data, nhdr.n_descsz) != nhdr.n_descsz) {
71 return false;
72 }
73
74 build_id->clear();
75 for (size_t bytes = 0; bytes < nhdr.n_descsz; bytes++) {
76 *build_id += android::base::StringPrintf("%02x", build_id_data[bytes]);
77 }
78
79 return true;
80 } else {
81 // Move past the extra note data.
82 hdr_size -= sizeof(nhdr);
83 size_t skip_bytes = nhdr.n_namesz + nhdr.n_descsz;
84 addr += skip_bytes;
85 if (hdr_size < skip_bytes) {
86 break;
87 }
88 hdr_size -= skip_bytes;
89 }
90 }
91 }
92 }
93 return false;
94}
95
96bool elf_get_build_id(Backtrace* backtrace, uintptr_t addr, std::string* build_id) {
97 // Read and verify the elf magic number first.
98 uint8_t e_ident[EI_NIDENT];
99 if (backtrace->Read(addr, e_ident, SELFMAG) != SELFMAG) {
100 return false;
101 }
102
103 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
104 return false;
105 }
106
107 // Read the rest of EI_NIDENT.
108 if (backtrace->Read(addr + SELFMAG, e_ident + SELFMAG, EI_NIDENT - SELFMAG) != EI_NIDENT - SELFMAG) {
109 return false;
110 }
111
112 if (e_ident[EI_CLASS] == ELFCLASS32) {
113 return get_build_id<Elf32_Ehdr, Elf32_Phdr, Elf32_Nhdr>(backtrace, addr, e_ident, build_id);
114 } else if (e_ident[EI_CLASS] == ELFCLASS64) {
115 return get_build_id<Elf64_Ehdr, Elf64_Phdr, Elf64_Nhdr>(backtrace, addr, e_ident, build_id);
116 }
117
118 return false;
119}