================
@@ -983,6 +995,44 @@ llvm::Error 
ProcessElfCore::ParseThreadContextsFromNoteSegment(
   }
 }
 
+bool ProcessElfCore::IsElf(const NT_FILE_Entry entry) {
+  const uint8_t elf_header[4] = {0x7f, 0x45, 0x4c,
+                                 0x46}; // ELF file begin with this 4 bytes
+  uint8_t buf[4];
+  Status error;
+  size_t byte_read = ReadMemory(entry.start, buf, 4, error);
+  if (byte_read == 4) {
+    return memcmp(elf_header, buf, 4) == 0;
+  } else {
+    return false;
+  }
+}
+
+UUID ProcessElfCore::FindBuildId(const NT_FILE_Entry entry) {
+  if (!IsElf(entry)) {
+    return UUID();
+  }
+  // Build ID is stored in the ELF file as a section named ".note.gnu.build-id"
+  uint8_t gnu_build_id_bytes[8] = {0x03, 0x00, 0x00, 0x00,
+                                   0x47, 0x4e, 0x55, 0x00};
+  lldb::addr_t gnu_build_id_addr =
+      FastSearch(entry.start, entry.end, gnu_build_id_bytes, 8);
+  if (gnu_build_id_addr == LLDB_INVALID_ADDRESS) {
+    return UUID();
+  }
+  uint8_t buf[36];
+  Status error;
+  size_t byte_read = ReadMemory(gnu_build_id_addr - 8, buf, 36, error);
+  // .note.gnu.build-id starts with 04 00 00 00 {id_byte_size} 00 00 00 03 00 
00
+  // 00 47 4e 55 00
+  if (byte_read == 36) {
----------------
clayborg wrote:

We should not assume there are always 36 bytes here. We should read the first 8 
bytes to get the `namesz` and `descsz` entries using a DataExtractor. Then do 
the right thing when decoding the UUID bytes by using the `namesz` value + 12 
bytes for the `namesz`, `descsz` and `type` uint32_t fields. We should also 
make sure our length isn't out of control and too big:
```
DataExtractor data(buf, sizeof(buf), GetByteOrder(), GetAddressByteSize());
lldb::offset_t offset = 0; 
const uint32_t namesz = data.GetU32(&offset); // Decode the length of the note 
name
const uint32_t descsz = data.GetU32(&offset); // Decode the UUID length
if (0 < uuid_length && uuid_length <= 20)
  offset = 16; // Set the position to point to the UUID bytes
  return UUID(llvm::ArrayRef<uint8_t>(data.GetData(12 + namesz), descsz);
}
```

https://github.com/llvm/llvm-project/pull/92078
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to