xref: /aosp_15_r20/external/google-breakpad/src/common/linux/file_id.h (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // file_id.h: Return a unique identifier for a file
30 //
31 
32 #ifndef COMMON_LINUX_FILE_ID_H__
33 #define COMMON_LINUX_FILE_ID_H__
34 
35 #include <limits.h>
36 #include <string>
37 
38 #include "common/linux/guid_creator.h"
39 #include "common/memory_allocator.h"
40 #include "common/using_std_string.h"
41 
42 namespace google_breakpad {
43 namespace elf {
44 
45 // GNU binutils' ld defaults to 'sha1', which is 160 bits == 20 bytes,
46 // so this is enough to fit that, which most binaries will use.
47 // This is just a sensible default for auto_wasteful_vector so most
48 // callers can get away with stack allocation.
49 static const size_t kDefaultBuildIdSize = 20;
50 
51 class FileID {
52  public:
53   explicit FileID(const char* path);
~FileID()54   ~FileID() {}
55 
56   // Load the identifier for the elf file path specified in the constructor into
57   // |identifier|.
58   //
59   // The current implementation will look for a .note.gnu.build-id
60   // section and use that as the file id, otherwise it falls back to
61   // XORing the first 4096 bytes of the .text section to generate an identifier.
62   bool ElfFileIdentifier(wasteful_vector<uint8_t>& identifier);
63 
64   // Load the identifier for the elf file mapped into memory at |base| into
65   // |identifier|. Return false if the identifier could not be created for this
66   // file.
67   static bool ElfFileIdentifierFromMappedFile(
68       const void* base,
69       wasteful_vector<uint8_t>& identifier);
70 
71   // Convert the |identifier| data to a string.  The string will
72   // be formatted as a UUID in all uppercase without dashes.
73   // (e.g., 22F065BBFC9C49F780FE26A7CEBD7BCE).
74   static string ConvertIdentifierToUUIDString(
75       const wasteful_vector<uint8_t>& identifier);
76 
77   // Convert the entire |identifier| data to a hex string.
78   static string ConvertIdentifierToString(
79       const wasteful_vector<uint8_t>& identifier);
80 
81  private:
82   // Storage for the path specified
83   string path_;
84 };
85 
86 }  // namespace elf
87 }  // namespace google_breakpad
88 
89 #endif  // COMMON_LINUX_FILE_ID_H__
90