1 /*
2  * Copyright (C) 2021 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 "android-base/logging.h"
18 
19 #include "base/os.h"
20 #include "base/unix_file/fd_file.h"
21 #include "elf/elf_builder.h"
22 #include "elf/elf_debug_reader.h"
23 #include "elf/xz_utils.h"
24 #include "stream/file_output_stream.h"
25 #include "stream/vector_output_stream.h"
26 
27 #include <algorithm>
28 #include <deque>
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <string_view>
33 #include <vector>
34 
35 namespace art {
36 
37 static constexpr size_t kBlockSize = 32 * KB;
38 
39 constexpr const char kSortedSymbolName[] = "$android.symtab.sorted";
40 
41 template<typename ElfTypes>
WriteMinidebugInfo(const std::vector<uint8_t> & input,std::vector<uint8_t> * output)42 static void WriteMinidebugInfo(const std::vector<uint8_t>& input, std::vector<uint8_t>* output) {
43   using Elf_Addr = typename ElfTypes::Addr;
44   using Elf_Shdr = typename ElfTypes::Shdr;
45   using Elf_Sym = typename ElfTypes::Sym;
46   using Elf_Word = typename ElfTypes::Word;
47   using CIE = typename ElfDebugReader<ElfTypes>::CIE;
48   using FDE = typename ElfDebugReader<ElfTypes>::FDE;
49 
50   ElfDebugReader<ElfTypes> reader(input);
51 
52   std::vector<uint8_t> output_elf_data;
53   VectorOutputStream output_stream("Output ELF", &output_elf_data);
54   InstructionSet isa = ElfBuilder<ElfTypes>::GetIsaFromHeader(*reader.GetHeader());
55   std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &output_stream));
56   builder->Start(/*write_program_headers=*/ false);
57 
58   auto* text = builder->GetText();
59   const Elf_Shdr* original_text = reader.GetSection(".text");
60   CHECK(original_text != nullptr);
61   text->AllocateVirtualMemory(original_text->sh_addr, original_text->sh_size);
62 
63   auto* strtab = builder->GetStrTab();
64   auto* symtab = builder->GetSymTab();
65   strtab->Start();
66   {
67     std::multimap<std::string_view, Elf_Sym> syms;
68     reader.VisitFunctionSymbols([&](Elf_Sym sym, const char* name) {
69       // Exclude non-function or empty symbols.
70       if (ELF32_ST_TYPE(sym.st_info) == STT_FUNC && sym.st_size != 0) {
71         syms.emplace(name, sym);
72       }
73     });
74     reader.VisitDynamicSymbols([&](Elf_Sym sym, const char* name) {
75       // Exclude symbols which will be preserved in the dynamic table anyway.
76       auto it = syms.find(name);
77       if (it != syms.end() && it->second.st_value == sym.st_value) {
78         syms.erase(it);
79       }
80     });
81     if (!syms.empty()) {
82       symtab->Add(strtab->Write(kSortedSymbolName), nullptr, 0, 0, STB_GLOBAL, STT_NOTYPE);
83     }
84     for (auto& entry : syms) {
85       std::string_view name = entry.first;
86       const Elf_Sym& sym = entry.second;
87       Elf_Word name_idx = strtab->Write(name);
88       symtab->Add(name_idx, text, sym.st_value, sym.st_size, STB_GLOBAL, STT_FUNC);
89     }
90   }
91   strtab->End();
92   symtab->WriteCachedSection();
93 
94   auto* debug_frame = builder->GetDebugFrame();
95   debug_frame->Start();
96   {
97     std::map<std::basic_string_view<uint8_t>, Elf_Addr> cie_dedup;
98     std::unordered_map<const CIE*, Elf_Addr> new_cie_offset;
99     std::deque<std::pair<const FDE*, const CIE*>> entries;
100     // Read, de-duplicate and write CIE entries.  Read FDE entries.
101     reader.VisitDebugFrame(
102         [&](const CIE* cie) {
103           std::basic_string_view<uint8_t> key(cie->data(), cie->size());
104           auto it = cie_dedup.emplace(key, debug_frame->GetPosition());
105           if (/* inserted */ it.second) {
106             debug_frame->WriteFully(cie->data(), cie->size());
107           }
108           new_cie_offset[cie] = it.first->second;
109         },
110         [&](const FDE* fde, const CIE* cie) {
111           entries.emplace_back(std::make_pair(fde, cie));
112         });
113     // Sort FDE entries by opcodes to improve locality for compression (saves ~25%).
114     std::stable_sort(entries.begin(), entries.end(), [](const auto& lhs, const auto& rhs) {
115       constexpr size_t opcode_offset = sizeof(FDE);
116       return std::lexicographical_compare(
117           lhs.first->data() + opcode_offset, lhs.first->data() + lhs.first->size(),
118           rhs.first->data() + opcode_offset, rhs.first->data() + rhs.first->size());
119     });
120     // Write all FDE entries while adjusting the CIE offsets to the new locations.
121     for (const auto& entry : entries) {
122       const FDE* fde = entry.first;
123       const CIE* cie = entry.second;
124       FDE new_header = *fde;
125       new_header.cie_pointer = new_cie_offset[cie];
126       debug_frame->WriteFully(&new_header, sizeof(FDE));
127       debug_frame->WriteFully(fde->data() + sizeof(FDE), fde->size() - sizeof(FDE));
128     }
129   }
130   debug_frame->End();
131 
132   builder->End();
133   CHECK(builder->Good());
134 
135   XzCompress(ArrayRef<const uint8_t>(output_elf_data), output, 9 /*size*/, kBlockSize);
136 }
137 
Main(int argc,char ** argv)138 static int Main(int argc, char** argv) {
139   // Check command like arguments.
140   if (argc != 3) {
141     printf("Usage: create_minidebuginfo ELF_FILE OUT_FILE\n");
142     printf("  ELF_FILE: The path to an ELF file with full symbols (before being stripped).\n");
143     printf("  OUT_FILE: The path for the generated mini-debug-info data (not an elf file).\n");
144     return 1;
145   }
146   const char* input_filename = argv[1];
147   const char* output_filename = argv[2];
148 
149   // Read input file.
150   std::unique_ptr<File> input_file(OS::OpenFileForReading(input_filename));
151   CHECK(input_file.get() != nullptr) << "Failed to open input file";
152   std::vector<uint8_t> elf(input_file->GetLength());
153   CHECK(input_file->ReadFully(elf.data(), elf.size())) << "Failed to read input file";
154 
155   // Write output file.
156   std::vector<uint8_t> output;
157   if (ElfDebugReader<ElfTypes32>::IsValidElfHeader(elf)) {
158     WriteMinidebugInfo<ElfTypes32>(elf, &output);
159   } else if (ElfDebugReader<ElfTypes64>::IsValidElfHeader(elf)) {
160     WriteMinidebugInfo<ElfTypes64>(elf, &output);
161   } else {
162     LOG(FATAL) << "Invalid ELF file header " << input_filename;
163   }
164   std::unique_ptr<File> output_file(OS::CreateEmptyFile(output_filename));
165   if (!output_file->WriteFully(output.data(), output.size()) || output_file->FlushClose() != 0) {
166     LOG(FATAL) << "Failed to write " << output_filename;
167   }
168   return 0;
169 }
170 
171 }  // namespace art
172 
main(int argc,char ** argv)173 int main(int argc, char** argv) {
174   return art::Main(argc, argv);
175 }
176