1 //===- GsymCreator.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===//
7 
8 #include "llvm/DebugInfo/GSYM/GsymCreator.h"
9 #include "llvm/DebugInfo/GSYM/FileWriter.h"
10 #include "llvm/DebugInfo/GSYM/Header.h"
11 #include "llvm/DebugInfo/GSYM/LineTable.h"
12 #include "llvm/MC/StringTableBuilder.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <functional>
18 #include <vector>
19 
20 using namespace llvm;
21 using namespace gsym;
22 
23 GsymCreator::GsymCreator(bool Quiet)
24     : StrTab(StringTableBuilder::ELF), Quiet(Quiet) {
25   insertFile(StringRef());
26 }
27 
28 uint32_t GsymCreator::insertFile(StringRef Path, llvm::sys::path::Style Style) {
29   llvm::StringRef directory = llvm::sys::path::parent_path(Path, Style);
30   llvm::StringRef filename = llvm::sys::path::filename(Path, Style);
31   // We must insert the strings first, then call the FileEntry constructor.
32   // If we inline the insertString() function call into the constructor, the
33   // call order is undefined due to parameter lists not having any ordering
34   // requirements.
35   const uint32_t Dir = insertString(directory);
36   const uint32_t Base = insertString(filename);
37   FileEntry FE(Dir, Base);
38 
39   std::lock_guard<std::mutex> Guard(Mutex);
40   const auto NextIndex = Files.size();
41   // Find FE in hash map and insert if not present.
42   auto R = FileEntryToIndex.insert(std::make_pair(FE, NextIndex));
43   if (R.second)
44     Files.emplace_back(FE);
45   return R.first->second;
46 }
47 
48 llvm::Error GsymCreator::save(StringRef Path,
49                               llvm::support::endianness ByteOrder) const {
50   std::error_code EC;
51   raw_fd_ostream OutStrm(Path, EC);
52   if (EC)
53     return llvm::errorCodeToError(EC);
54   FileWriter O(OutStrm, ByteOrder);
55   return encode(O);
56 }
57 
58 llvm::Error GsymCreator::encode(FileWriter &O) const {
59   std::lock_guard<std::mutex> Guard(Mutex);
60   if (Funcs.empty())
61     return createStringError(std::errc::invalid_argument,
62                              "no functions to encode");
63   if (!Finalized)
64     return createStringError(std::errc::invalid_argument,
65                              "GsymCreator wasn't finalized prior to encoding");
66 
67   if (Funcs.size() > UINT32_MAX)
68     return createStringError(std::errc::invalid_argument,
69                              "too many FunctionInfos");
70 
71   const uint64_t MinAddr =
72       BaseAddress ? *BaseAddress : Funcs.front().startAddress();
73   const uint64_t MaxAddr = Funcs.back().startAddress();
74   const uint64_t AddrDelta = MaxAddr - MinAddr;
75   Header Hdr;
76   Hdr.Magic = GSYM_MAGIC;
77   Hdr.Version = GSYM_VERSION;
78   Hdr.AddrOffSize = 0;
79   Hdr.UUIDSize = static_cast<uint8_t>(UUID.size());
80   Hdr.BaseAddress = MinAddr;
81   Hdr.NumAddresses = static_cast<uint32_t>(Funcs.size());
82   Hdr.StrtabOffset = 0; // We will fix this up later.
83   Hdr.StrtabSize = 0;   // We will fix this up later.
84   memset(Hdr.UUID, 0, sizeof(Hdr.UUID));
85   if (UUID.size() > sizeof(Hdr.UUID))
86     return createStringError(std::errc::invalid_argument,
87                              "invalid UUID size %u", (uint32_t)UUID.size());
88   // Set the address offset size correctly in the GSYM header.
89   if (AddrDelta <= UINT8_MAX)
90     Hdr.AddrOffSize = 1;
91   else if (AddrDelta <= UINT16_MAX)
92     Hdr.AddrOffSize = 2;
93   else if (AddrDelta <= UINT32_MAX)
94     Hdr.AddrOffSize = 4;
95   else
96     Hdr.AddrOffSize = 8;
97   // Copy the UUID value if we have one.
98   if (UUID.size() > 0)
99     memcpy(Hdr.UUID, UUID.data(), UUID.size());
100   // Write out the header.
101   llvm::Error Err = Hdr.encode(O);
102   if (Err)
103     return Err;
104 
105   // Write out the address offsets.
106   O.alignTo(Hdr.AddrOffSize);
107   for (const auto &FuncInfo : Funcs) {
108     uint64_t AddrOffset = FuncInfo.startAddress() - Hdr.BaseAddress;
109     switch (Hdr.AddrOffSize) {
110     case 1:
111       O.writeU8(static_cast<uint8_t>(AddrOffset));
112       break;
113     case 2:
114       O.writeU16(static_cast<uint16_t>(AddrOffset));
115       break;
116     case 4:
117       O.writeU32(static_cast<uint32_t>(AddrOffset));
118       break;
119     case 8:
120       O.writeU64(AddrOffset);
121       break;
122     }
123   }
124 
125   // Write out all zeros for the AddrInfoOffsets.
126   O.alignTo(4);
127   const off_t AddrInfoOffsetsOffset = O.tell();
128   for (size_t i = 0, n = Funcs.size(); i < n; ++i)
129     O.writeU32(0);
130 
131   // Write out the file table
132   O.alignTo(4);
133   assert(!Files.empty());
134   assert(Files[0].Dir == 0);
135   assert(Files[0].Base == 0);
136   size_t NumFiles = Files.size();
137   if (NumFiles > UINT32_MAX)
138     return createStringError(std::errc::invalid_argument, "too many files");
139   O.writeU32(static_cast<uint32_t>(NumFiles));
140   for (auto File : Files) {
141     O.writeU32(File.Dir);
142     O.writeU32(File.Base);
143   }
144 
145   // Write out the sting table.
146   const off_t StrtabOffset = O.tell();
147   StrTab.write(O.get_stream());
148   const off_t StrtabSize = O.tell() - StrtabOffset;
149   std::vector<uint32_t> AddrInfoOffsets;
150 
151   // Write out the address infos for each function info.
152   for (const auto &FuncInfo : Funcs) {
153     if (Expected<uint64_t> OffsetOrErr = FuncInfo.encode(O))
154       AddrInfoOffsets.push_back(OffsetOrErr.get());
155     else
156       return OffsetOrErr.takeError();
157   }
158   // Fixup the string table offset and size in the header
159   O.fixup32((uint32_t)StrtabOffset, offsetof(Header, StrtabOffset));
160   O.fixup32((uint32_t)StrtabSize, offsetof(Header, StrtabSize));
161 
162   // Fixup all address info offsets
163   uint64_t Offset = 0;
164   for (auto AddrInfoOffset : AddrInfoOffsets) {
165     O.fixup32(AddrInfoOffset, AddrInfoOffsetsOffset + Offset);
166     Offset += 4;
167   }
168   return ErrorSuccess();
169 }
170 
171 // Similar to std::remove_if, but the predicate is binary and it is passed both
172 // the previous and the current element.
173 template <class ForwardIt, class BinaryPredicate>
174 static ForwardIt removeIfBinary(ForwardIt FirstIt, ForwardIt LastIt,
175                                 BinaryPredicate Pred) {
176   if (FirstIt != LastIt) {
177     auto PrevIt = FirstIt++;
178     FirstIt = std::find_if(FirstIt, LastIt, [&](const auto &Curr) {
179       return Pred(*PrevIt++, Curr);
180     });
181     if (FirstIt != LastIt)
182       for (ForwardIt CurrIt = FirstIt; ++CurrIt != LastIt;)
183         if (!Pred(*PrevIt, *CurrIt)) {
184           PrevIt = FirstIt;
185           *FirstIt++ = std::move(*CurrIt);
186         }
187   }
188   return FirstIt;
189 }
190 
191 llvm::Error GsymCreator::finalize(llvm::raw_ostream &OS) {
192   std::lock_guard<std::mutex> Guard(Mutex);
193   if (Finalized)
194     return createStringError(std::errc::invalid_argument, "already finalized");
195   Finalized = true;
196 
197   // Sort function infos so we can emit sorted functions.
198   llvm::sort(Funcs);
199 
200   // Don't let the string table indexes change by finalizing in order.
201   StrTab.finalizeInOrder();
202 
203   // Remove duplicates function infos that have both entries from debug info
204   // (DWARF or Breakpad) and entries from the SymbolTable.
205   //
206   // Also handle overlapping function. Usually there shouldn't be any, but they
207   // can and do happen in some rare cases.
208   //
209   // (a)          (b)         (c)
210   //     ^  ^       ^            ^
211   //     |X |Y      |X ^         |X
212   //     |  |       |  |Y        |  ^
213   //     |  |       |  v         v  |Y
214   //     v  v       v               v
215   //
216   // In (a) and (b), Y is ignored and X will be reported for the full range.
217   // In (c), both functions will be included in the result and lookups for an
218   // address in the intersection will return Y because of binary search.
219   //
220   // Note that in case of (b), we cannot include Y in the result because then
221   // we wouldn't find any function for range (end of Y, end of X)
222   // with binary search
223   auto NumBefore = Funcs.size();
224   Funcs.erase(
225       removeIfBinary(Funcs.begin(), Funcs.end(),
226                      [&](const auto &Prev, const auto &Curr) {
227                        if (Prev.Range.intersects(Curr.Range)) {
228                          // Overlapping address ranges.
229                          if (Prev.Range == Curr.Range) {
230                            // Same address range. Check if one is from debug
231                            // info and the other is from a symbol table. If
232                            // so, then keep the one with debug info. Our
233                            // sorting guarantees that entries with matching
234                            // address ranges that have debug info are last in
235                            // the sort.
236                            if (Prev == Curr) {
237                              // FunctionInfo entries match exactly (range,
238                              // lines, inlines)
239 
240                              // We used to output a warning here, but this was
241                              // so frequent on some binaries, in particular
242                              // when those were built with GCC, that it slowed
243                              // down processing extremely.
244                              return true;
245                            } else {
246                              if (!Prev.hasRichInfo() && Curr.hasRichInfo()) {
247                                // Same address range, one with no debug info
248                                // (symbol) and the next with debug info. Keep
249                                // the latter.
250                                return true;
251                              } else {
252                                if (!Quiet) {
253                                  OS << "warning: same address range contains "
254                                        "different debug "
255                                     << "info. Removing:\n"
256                                     << Prev << "\nIn favor of this one:\n"
257                                     << Curr << "\n";
258                                }
259                                return true;
260                              }
261                            }
262                          } else {
263                            if (!Quiet) { // print warnings about overlaps
264                              OS << "warning: function ranges overlap:\n"
265                                 << Prev << "\n"
266                                 << Curr << "\n";
267                            }
268                          }
269                        } else if (Prev.Range.size() == 0 &&
270                                   Curr.Range.contains(Prev.Range.Start)) {
271                          if (!Quiet) {
272                            OS << "warning: removing symbol:\n"
273                               << Prev << "\nKeeping:\n"
274                               << Curr << "\n";
275                          }
276                          return true;
277                        }
278 
279                        return false;
280                      }),
281       Funcs.end());
282 
283   // If our last function info entry doesn't have a size and if we have valid
284   // text ranges, we should set the size of the last entry since any search for
285   // a high address might match our last entry. By fixing up this size, we can
286   // help ensure we don't cause lookups to always return the last symbol that
287   // has no size when doing lookups.
288   if (!Funcs.empty() && Funcs.back().Range.size() == 0 && ValidTextRanges) {
289     if (auto Range =
290             ValidTextRanges->getRangeThatContains(Funcs.back().Range.Start)) {
291       Funcs.back().Range.End = Range->End;
292     }
293   }
294   OS << "Pruned " << NumBefore - Funcs.size() << " functions, ended with "
295      << Funcs.size() << " total\n";
296   return Error::success();
297 }
298 
299 uint32_t GsymCreator::insertString(StringRef S, bool Copy) {
300   if (S.empty())
301     return 0;
302 
303   // The hash can be calculated outside the lock.
304   CachedHashStringRef CHStr(S);
305   std::lock_guard<std::mutex> Guard(Mutex);
306   if (Copy) {
307     // We need to provide backing storage for the string if requested
308     // since StringTableBuilder stores references to strings. Any string
309     // that comes from a section in an object file doesn't need to be
310     // copied, but any string created by code will need to be copied.
311     // This allows GsymCreator to be really fast when parsing DWARF and
312     // other object files as most strings don't need to be copied.
313     if (!StrTab.contains(CHStr))
314       CHStr = CachedHashStringRef{StringStorage.insert(S).first->getKey(),
315                                   CHStr.hash()};
316   }
317   return StrTab.add(CHStr);
318 }
319 
320 void GsymCreator::addFunctionInfo(FunctionInfo &&FI) {
321   std::lock_guard<std::mutex> Guard(Mutex);
322   Ranges.insert(FI.Range);
323   Funcs.emplace_back(std::move(FI));
324 }
325 
326 void GsymCreator::forEachFunctionInfo(
327     std::function<bool(FunctionInfo &)> const &Callback) {
328   std::lock_guard<std::mutex> Guard(Mutex);
329   for (auto &FI : Funcs) {
330     if (!Callback(FI))
331       break;
332   }
333 }
334 
335 void GsymCreator::forEachFunctionInfo(
336     std::function<bool(const FunctionInfo &)> const &Callback) const {
337   std::lock_guard<std::mutex> Guard(Mutex);
338   for (const auto &FI : Funcs) {
339     if (!Callback(FI))
340       break;
341   }
342 }
343 
344 size_t GsymCreator::getNumFunctionInfos() const {
345   std::lock_guard<std::mutex> Guard(Mutex);
346   return Funcs.size();
347 }
348 
349 bool GsymCreator::IsValidTextAddress(uint64_t Addr) const {
350   if (ValidTextRanges)
351     return ValidTextRanges->contains(Addr);
352   return true; // No valid text ranges has been set, so accept all ranges.
353 }
354 
355 bool GsymCreator::hasFunctionInfoForAddress(uint64_t Addr) const {
356   std::lock_guard<std::mutex> Guard(Mutex);
357   return Ranges.contains(Addr);
358 }
359