2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <base/file.h>
23 #include <base/logging.h>
25 #pragma clang diagnostic push
26 #pragma clang diagnostic ignored "-Wunused-parameter"
28 #include <llvm/ADT/StringRef.h>
29 #include <llvm/Object/Binary.h>
30 #include <llvm/Object/ELFObjectFile.h>
31 #include <llvm/Object/ObjectFile.h>
33 #pragma clang diagnostic pop
39 static bool GetBuildIdFromNoteSection(const char* section, size_t section_size, BuildId* build_id) {
40 const char* p = section;
41 const char* end = p + section_size;
43 CHECK_LE(p + 12, end);
44 size_t namesz = *reinterpret_cast<const uint32_t*>(p);
46 size_t descsz = *reinterpret_cast<const uint32_t*>(p);
48 uint32_t type = *reinterpret_cast<const uint32_t*>(p);
50 namesz = ALIGN(namesz, 4);
51 descsz = ALIGN(descsz, 4);
52 CHECK_LE(p + namesz + descsz, end);
53 if ((type == NT_GNU_BUILD_ID) && (strcmp(p, ELF_NOTE_GNU) == 0)) {
54 std::fill(build_id->begin(), build_id->end(), 0);
55 memcpy(build_id->data(), p + namesz, std::min(build_id->size(), descsz));
63 bool GetBuildIdFromNoteFile(const std::string& filename, BuildId* build_id) {
65 if (!android::base::ReadFileToString(filename, &content)) {
66 LOG(DEBUG) << "can't read note file " << filename;
69 if (GetBuildIdFromNoteSection(content.c_str(), content.size(), build_id) == false) {
70 LOG(DEBUG) << "can't read build_id from note file " << filename;
77 bool GetBuildIdFromELFFile(const llvm::object::ELFFile<ELFT>* elf, BuildId* build_id) {
78 for (auto section_iterator = elf->begin_sections(); section_iterator != elf->end_sections();
80 if (section_iterator->sh_type == SHT_NOTE) {
81 auto contents = elf->getSectionContents(&*section_iterator);
82 if (contents.getError()) {
83 LOG(DEBUG) << "read note section error";
86 if (GetBuildIdFromNoteSection(reinterpret_cast<const char*>(contents->data()),
87 contents->size(), build_id)) {
95 bool GetBuildIdFromElfFile(const std::string& filename, BuildId* build_id) {
96 auto owning_binary = llvm::object::createBinary(llvm::StringRef(filename));
97 if (owning_binary.getError()) {
98 PLOG(DEBUG) << "can't open file " << filename;
102 llvm::object::Binary* binary = owning_binary.get().getBinary();
103 if (auto obj = llvm::dyn_cast<llvm::object::ObjectFile>(binary)) {
104 if (auto elf = llvm::dyn_cast<llvm::object::ELF32LEObjectFile>(obj)) {
105 result = GetBuildIdFromELFFile(elf->getELFFile(), build_id);
106 } else if (auto elf = llvm::dyn_cast<llvm::object::ELF64LEObjectFile>(obj)) {
107 result = GetBuildIdFromELFFile(elf->getELFFile(), build_id);
109 PLOG(DEBUG) << "unknown elf format in file " << filename;
113 PLOG(DEBUG) << "can't read build_id from file " << filename;