1 /*
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 #include "read_elf.h"
18 
19 #include <stdio.h>
20 #include <string.h>
21 #include <algorithm>
22 #include <base/file.h>
23 #include <base/logging.h>
24 
25 #pragma clang diagnostic push
26 #pragma clang diagnostic ignored "-Wunused-parameter"
27 
28 #include <llvm/ADT/StringRef.h>
29 #include <llvm/Object/Binary.h>
30 #include <llvm/Object/ELFObjectFile.h>
31 #include <llvm/Object/ObjectFile.h>
32 
33 #pragma clang diagnostic pop
34 
35 #include <elf.h>
36 
37 #include "utils.h"
38 
GetBuildIdFromNoteSection(const char * section,size_t section_size,BuildId * build_id)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;
42   while (p < end) {
43     CHECK_LE(p + 12, end);
44     size_t namesz = *reinterpret_cast<const uint32_t*>(p);
45     p += 4;
46     size_t descsz = *reinterpret_cast<const uint32_t*>(p);
47     p += 4;
48     uint32_t type = *reinterpret_cast<const uint32_t*>(p);
49     p += 4;
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));
56       return true;
57     }
58     p += namesz + descsz;
59   }
60   return false;
61 }
62 
GetBuildIdFromNoteFile(const std::string & filename,BuildId * build_id)63 bool GetBuildIdFromNoteFile(const std::string& filename, BuildId* build_id) {
64   std::string content;
65   if (!android::base::ReadFileToString(filename, &content)) {
66     LOG(DEBUG) << "can't read note file " << filename;
67     return false;
68   }
69   if (GetBuildIdFromNoteSection(content.c_str(), content.size(), build_id) == false) {
70     LOG(DEBUG) << "can't read build_id from note file " << filename;
71     return false;
72   }
73   return true;
74 }
75 
76 template <class ELFT>
GetBuildIdFromELFFile(const llvm::object::ELFFile<ELFT> * elf,BuildId * build_id)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();
79        ++section_iterator) {
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";
84         continue;
85       }
86       if (GetBuildIdFromNoteSection(reinterpret_cast<const char*>(contents->data()),
87                                     contents->size(), build_id)) {
88         return true;
89       }
90     }
91   }
92   return false;
93 }
94 
GetBuildIdFromElfFile(const std::string & filename,BuildId * 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;
99     return false;
100   }
101   bool result = false;
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);
108     } else {
109       PLOG(DEBUG) << "unknown elf format in file " << filename;
110     }
111   }
112   if (!result) {
113     PLOG(DEBUG) << "can't read build_id from file " << filename;
114   }
115   return result;
116 }
117