[Lldb-commits] [lldb] [lldb] In symtab serialization, make Section-relative addrs file-offset (PR #234809)

Jason Molenda via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 26 17:03:39 PDT 2026


https://github.com/jasonmolenda updated https://github.com/llvm/llvm-project/pull/234809

>From ab67efd606d68cffdcfa27eb77142ac53c9d783d Mon Sep 17 00:00:00 2001
From: Jason Molenda <jmolenda at apple.com>
Date: Thu, 22 Oct 2026 17:26:41 -0700
Subject: [PATCH 1/2] [lldb] In symtab serialization, make Section-relative
 addrs file-offset

On Darwin systems, a dynamic library is usually virtual address 0
based when it is linked, destined to be slid to a random virtual
address at runtime via ASLR.  Darwin systems link all system libraries
into a single Shared Cache collection, and in the process of being
added to the shared cache, the virtual address (file address) of
the binary is changed to be non-zero -- to be a part of the shared
cache which has a file address (and also slides at boot-time).  The
binaries -- before Shared Cache creation, and after -- both have the
same filepath and UUID, but the file addresses in them are different.

When lldb creates a DataFileCache symtab serialization of the symbol
table based on a 0-offset linked binary, and then reuses that symtab
for a Shared Cache non-0-offset file address binary (same filepath,
same UUID), when we try to find the correct Section for a Symbol
having a Section+offset, it fails because none of the Sections have
0-based file addresses.

To address this, when serializing Symbols with section-relative
addresses, take the file address we would serialize, get the
ObjectFile's base file address (0 in one case, a much higher address
in the Shared Cache case) and subtract that base address from the
file address.  When deserializing, add the correct file base address
back to the file addresses before doing a Section lookup.

This change required bumping the DataFileCache symtab version.

rdar://190169369
---
 lldb/include/lldb/Symbol/Symbol.h    | 18 ++++++++++++++++--
 lldb/source/Symbol/Symbol.cpp        | 17 +++++++++++------
 lldb/source/Symbol/Symtab.cpp        |  9 ++++++---
 lldb/unittests/Symbol/SymbolTest.cpp |  5 +++--
 lldb/unittests/Symbol/SymtabTest.cpp | 22 +++++++++++-----------
 5 files changed, 47 insertions(+), 24 deletions(-)

diff --git a/lldb/include/lldb/Symbol/Symbol.h b/lldb/include/lldb/Symbol/Symbol.h
index 700b29b23a7472..9266d531fe76c6 100644
--- a/lldb/include/lldb/Symbol/Symbol.h
+++ b/lldb/include/lldb/Symbol/Symbol.h
@@ -317,10 +317,15 @@ class Symbol : public SymbolContextScope {
   ///   and cache file size reduction. Strings are stored as uint32_t string
   ///   table offsets in the cache data.
   ///
+  /// \param file_base_addr
+  ///   The base file address of the ObjectFile, which will be added to
+  ///   any Section-relative file addresses during serialization.
+  ///
   /// \return
   ///   True if the symbol is successfully decoded, false otherwise.
   bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
-              const SectionList *section_list, const StringTableReader &strtab);
+              const SectionList *section_list, const StringTableReader &strtab,
+              lldb::addr_t file_base_addr);
 
   /// Encode this object into a data encoder object.
   ///
@@ -333,7 +338,16 @@ class Symbol : public SymbolContextScope {
   ///   All strings in cache files are put into string tables for efficiency
   ///   and cache file size reduction. Strings are stored as uint32_t string
   ///   table offsets in the cache data.
-  void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
+  /// \param file_base_addr
+  ///   The base file address of the ObjectFile. Symbols with a Section-relative
+  ///   file address will have the base file address subtraced when serializing,
+  ///   and added back again during deserialization, because it is possible for
+  ///   a binary to be rebased to a new file address (including the file
+  ///   addresses of all the symbols) while maintaining the same UUID.  A
+  ///   "rebased" file like this have the identical content, but its file
+  ///   addresses have all been shifted.
+  void Encode(DataEncoder &encoder, ConstStringTable &strtab,
+              lldb::addr_t file_base_addr) const;
 
   bool operator==(const Symbol &rhs) const;
 
diff --git a/lldb/source/Symbol/Symbol.cpp b/lldb/source/Symbol/Symbol.cpp
index e96d6d371798cb..93e0eece61e3d0 100644
--- a/lldb/source/Symbol/Symbol.cpp
+++ b/lldb/source/Symbol/Symbol.cpp
@@ -719,7 +719,7 @@ void Symbol::SynthesizeNameIfNeeded() const {
 
 bool Symbol::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
                     const SectionList *section_list,
-                    const StringTableReader &strtab) {
+                    const StringTableReader &strtab, addr_t file_base_addr) {
   if (!data.ValidOffsetForDataOfSize(*offset_ptr, 8))
     return false;
   m_uid = data.GetU32(offset_ptr);
@@ -746,7 +746,8 @@ bool Symbol::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
     if (is_addr) {
       m_addr_or_reexport.GetAddressRange(*this)
           .GetBaseAddress()
-          .ResolveAddressUsingFileSections(value, section_list);
+          .ResolveAddressUsingFileSections(value + file_base_addr,
+                                           section_list);
     } else {
       m_addr_or_reexport.GetAddressRange(*this).GetBaseAddress().Clear();
       m_addr_or_reexport.GetAddressRange(*this).GetBaseAddress().SetOffset(
@@ -792,7 +793,8 @@ static_assert(sizeof(lldb_private::Symbol) == 72,
 /// do the same thing when decoding the symbol. There are test that ensure this
 /// encoding works for each individual bit. Everything else is very easy to
 /// store.
-void Symbol::Encode(DataEncoder &file, ConstStringTable &strtab) const {
+void Symbol::Encode(DataEncoder &file, ConstStringTable &strtab,
+                    addr_t file_base_addr) const {
   file.AppendU32(m_uid);
   file.AppendU16(m_type_data);
   uint16_t bitfields = m_type;
@@ -828,9 +830,12 @@ void Symbol::Encode(DataEncoder &file, ConstStringTable &strtab) const {
                        .GetSection()
                        .get() != nullptr;
     file.AppendU8(is_addr);
-    file.AppendU64(m_addr_or_reexport.GetAddressRange(*this)
-                       .GetBaseAddress()
-                       .GetFileAddress());
+    uint64_t value = m_addr_or_reexport.GetAddressRange(*this)
+                         .GetBaseAddress()
+                         .GetFileAddress();
+    if (is_addr)
+      value = value - file_base_addr;
+    file.AppendU64(value);
     file.AppendU64(m_addr_or_reexport.GetAddressRange(*this).GetByteSize());
   } else {
     file.AppendU32(strtab.Add(m_addr_or_reexport.GetReExportInfo(*this).name));
diff --git a/lldb/source/Symbol/Symtab.cpp b/lldb/source/Symbol/Symtab.cpp
index efea0339d2ef66..faf45a9c85e267 100644
--- a/lldb/source/Symbol/Symtab.cpp
+++ b/lldb/source/Symbol/Symtab.cpp
@@ -1227,7 +1227,7 @@ bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr,
 }
 
 constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB");
-constexpr uint32_t CURRENT_CACHE_VERSION = 3;
+constexpr uint32_t CURRENT_CACHE_VERSION = 4;
 
 // If the size of the Symbol object changes, the serialized
 // format likely also needs to change so the
@@ -1268,9 +1268,10 @@ bool Symtab::Encode(DataEncoder &encoder) const {
   symtab_encoder.AppendU32(CURRENT_CACHE_VERSION);
   // Encode the number of symbols.
   symtab_encoder.AppendU32(m_symbols.size());
+  addr_t file_base_addr = m_objfile->GetBaseAddress().GetFileAddress();
   // Encode the symbol data for all symbols.
   for (const auto &symbol: m_symbols)
-    symbol.Encode(symtab_encoder, strtab);
+    symbol.Encode(symtab_encoder, strtab, file_base_addr);
 
   // Emit a byte for how many C string maps we emit. We will fix this up after
   // we emit the C string maps since we skip emitting C string maps if they are
@@ -1324,8 +1325,10 @@ bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
       return true;
     m_symbols.resize(num_symbols);
     SectionList *sections = m_objfile->GetModule()->GetSectionList();
+    addr_t file_base_addr = m_objfile->GetBaseAddress().GetFileAddress();
     for (uint32_t i=0; i<num_symbols; ++i) {
-      if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab))
+      if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab,
+                               file_base_addr))
         return false;
     }
   }
diff --git a/lldb/unittests/Symbol/SymbolTest.cpp b/lldb/unittests/Symbol/SymbolTest.cpp
index d019e993f0236c..c8cc02dd37ee0c 100644
--- a/lldb/unittests/Symbol/SymbolTest.cpp
+++ b/lldb/unittests/Symbol/SymbolTest.cpp
@@ -23,7 +23,7 @@ static void EncodeDecode(const Symbol &object, const SectionList *sect_list,
   DataEncoder file(byte_order, addr_size);
   DataEncoder strtab_encoder(byte_order, addr_size);
   ConstStringTable const_strtab;
-  object.Encode(file, const_strtab);
+  object.Encode(file, const_strtab, /*file_base_addr=*/0);
   llvm::ArrayRef<uint8_t> bytes = file.GetData();
   DataExtractor data(bytes.data(), bytes.size(), byte_order, addr_size);
 
@@ -37,7 +37,8 @@ static void EncodeDecode(const Symbol &object, const SectionList *sect_list,
 
   Symbol decoded_object;
   offset_t data_offset = 0;
-  decoded_object.Decode(data, &data_offset, sect_list, strtab_reader);
+  decoded_object.Decode(data, &data_offset, sect_list, strtab_reader,
+                        /*file_base_addr=*/0);
   EXPECT_EQ(object, decoded_object);
 }
 
diff --git a/lldb/unittests/Symbol/SymtabTest.cpp b/lldb/unittests/Symbol/SymtabTest.cpp
index 0236b6caa97068..eb1f78efed588d 100644
--- a/lldb/unittests/Symbol/SymtabTest.cpp
+++ b/lldb/unittests/Symbol/SymtabTest.cpp
@@ -634,7 +634,7 @@ TEST_F(SymtabTest, TestDecodeCStringMaps) {
       0x5f, 0x65, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x5f, 0x68, 0x65, 0x61,
       0x64, 0x65, 0x72, 0x00, 0x53, 0x59, 0x4d, 0x42,
       // clang-format off
-      0x03, 0x00, 0x00, 0x00, // DataFileCache's CURRENT_CACHE_VERSION
+      0x04, 0x00, 0x00, 0x00, // DataFileCache's CURRENT_CACHE_VERSION
       // clang-format on
       0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x2a,
       0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -643,25 +643,25 @@ TEST_F(SymtabTest, TestDecodeCStringMaps) {
       0x00, 0x00, 0x00, 0x00, 0x7b, 0xc3, 0x69, 0x62, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x66, 0x00,
       0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x32, 0x01, 0x6d, 0x00, 0x00,
-      0x00, 0x01, 0x64, 0x3f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
+      0x00, 0x01, 0x64, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x02, 0x32, 0x01, 0x71, 0x00, 0x00, 0x00, 0x01,
-      0x6c, 0x3f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
+      0x6c, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x0c, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x02, 0x32, 0x01, 0x75, 0x00, 0x00, 0x00, 0x01, 0x74, 0x3f,
-      0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x02, 0x32, 0x01, 0x79, 0x00, 0x00, 0x00, 0x01, 0x7c, 0x3f, 0x00, 0x00,
-      0x01, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x0f, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x12,
-      0x02, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
+      0x01, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x64, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
       0x0f, 0x00, 0x01, 0x00, 0x43, 0x4d, 0x41, 0x50, 0x07, 0x00, 0x00, 0x00,
-      0x6d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00,
-      0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-      0x79, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
-      0x01, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
-      0x7e, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00};
+      0x71, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x79, 0x00, 0x00, 0x00,
+      0x05, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+      0x7e, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x00, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+      0x75, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00};
 
   ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
   auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());

>From 4456f1e866883da389cb6e0be1e2eeac75bc23b9 Mon Sep 17 00:00:00 2001
From: Jason Molenda <jmolenda at apple.com>
Date: Mon, 26 Oct 2026 17:02:39 -0700
Subject: [PATCH 2/2] Add test case where I have a YAML for a binary at base
 addr 0, and a YAML for the same binary at base address non-zero. Serialize
 both of them to a symtab data buffer, and compare them byte-by-byte to ensure
 that both binaries serialize to the same content.

---
 lldb/unittests/Symbol/SymtabTest.cpp | 561 +++++++++++++++++++++++++++
 1 file changed, 561 insertions(+)

diff --git a/lldb/unittests/Symbol/SymtabTest.cpp b/lldb/unittests/Symbol/SymtabTest.cpp
index eb1f78efed588d..8973174a50c8b2 100644
--- a/lldb/unittests/Symbol/SymtabTest.cpp
+++ b/lldb/unittests/Symbol/SymtabTest.cpp
@@ -844,3 +844,564 @@ TEST_F(SymtabTest, TestSymbolTableCreatedOnDemand) {
   Symtab *cached_symtab = symbol_file->GetSymtab(/*can_create=*/false);
   ASSERT_EQ(symtab, cached_symtab);
 }
+
+TEST_F(SymtabTest, TestSymbolTableRelativeFileAddresses) {
+  // Section-relative in a serialized symbol table are expressed as
+  // offsets from the base file address of the binary.  It is possible
+  // for a binary to be zero-address offset initially, but then included
+  // in a Darwin shared cache with all of the segment and file addresses
+  // slid to a non-zero value -- with the same filename and UUID.  To make
+  // the serialized symtab work for both, we must make the file addresses
+  // relative ot the file base.
+  //
+  // static int c = 10;
+  // int d = 20;
+  // int foo() { return c; }
+  // int bar() { return foo() + d; }
+  // int main() { int var = bar() + c; return var; }
+  //
+  // clang -dynamiclib -mmacosx-version-min=11 -o liba.dylib a.c
+  // obj2yaml liba.dylib > zero.yaml
+  // clang -image_base 0x3c000 -dynamiclib -mmacosx-version-min=11 -o liba.dylib
+  // a.c obj2yaml liba.dylib > nonzero.yaml
+  //
+  // hand-edit the UUID from one of them to match the other.
+  // Any macOS version higher than 11 results in chained fixups in the binary,
+  // and you can't compile the dylib to a non-zero offset with chained fixups.
+  //
+  // These two YAMLs are below are then serialized into a buffer, and we
+  // compare the buffers -- they must be byte-by-byte identical.
+
+  auto ExpectedFileZeroOffset = TestFile::fromYaml(R"(
+--- !mach-o
+FileHeader:
+  magic:           0xFEEDFACF
+  cputype:         0x100000C
+  cpusubtype:      0x0
+  filetype:        0x6
+  ncmds:           14
+  sizeofcmds:      824
+  flags:           0x100085
+  reserved:        0x0
+LoadCommands:
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         232
+    segname:         __TEXT
+    vmaddr:          0
+    vmsize:          16384
+    fileoff:         0
+    filesize:        16384
+    maxprot:         5
+    initprot:        5
+    nsects:          2
+    flags:           0
+    Sections:
+      - sectname:        __text
+        segname:         __TEXT
+        addr:            0x388
+        size:            96
+        offset:          0x388
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x80000400
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         28000090000540B9C0035FD6FD7BBFA9FD030091FBFFFF9728000090080140B90000080BFD7BC1A8C0035FD6FF8300D1FD7B01A9FD430091BFC31FB8F4FFFF9728000090080540B90800080BE80B00B9E00B40B9FD7B41A9FF830091C0035FD6
+      - sectname:        __unwind_info
+        segname:         __TEXT
+        addr:            0x3E8
+        size:            96
+        offset:          0x3E8
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x0
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         010000001C000000000000001C000000000000001C00000002000000880300004000000040000000E90300000000000040000000000000000000000000000000030000000C00020014000200000000000C000001000000020000000400000000
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         152
+    segname:         __DATA
+    vmaddr:          16384
+    vmsize:          16384
+    fileoff:         16384
+    filesize:        16384
+    maxprot:         3
+    initprot:        3
+    nsects:          1
+    flags:           0
+    Sections:
+      - sectname:        __data
+        segname:         __DATA
+        addr:            0x4000
+        size:            8
+        offset:          0x4000
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x0
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         140000000A000000
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         72
+    segname:         __LINKEDIT
+    vmaddr:          32768
+    vmsize:          16384
+    fileoff:         32768
+    filesize:        584
+    maxprot:         1
+    initprot:        1
+    nsects:          0
+    flags:           0
+  - cmd:             LC_ID_DYLIB
+    cmdsize:         40
+    dylib:
+      name:            24
+      timestamp:       1
+      current_version: 0
+      compatibility_version: 0
+    Content:         liba.dylib
+    ZeroPadBytes:    6
+  - cmd:             LC_DYLD_INFO_ONLY
+    cmdsize:         48
+    rebase_off:      0
+    rebase_size:     0
+    bind_off:        0
+    bind_size:       0
+    weak_bind_off:   0
+    weak_bind_size:  0
+    lazy_bind_off:   0
+    lazy_bind_size:  0
+    export_off:      32768
+    export_size:     56
+  - cmd:             LC_SYMTAB
+    cmdsize:         24
+    symoff:          32832
+    nsyms:           5
+    stroff:          32912
+    strsize:         24
+  - cmd:             LC_DYSYMTAB
+    cmdsize:         80
+    ilocalsym:       0
+    nlocalsym:       1
+    iextdefsym:      1
+    nextdefsym:      4
+    iundefsym:       5
+    nundefsym:       0
+    tocoff:          0
+    ntoc:            0
+    modtaboff:       0
+    nmodtab:         0
+    extrefsymoff:    0
+    nextrefsyms:     0
+    indirectsymoff:  0
+    nindirectsyms:   0
+    extreloff:       0
+    nextrel:         0
+    locreloff:       0
+    nlocrel:         0
+  - cmd:             LC_UUID
+    cmdsize:         24
+    uuid:            9927BB37-87F9-3AAA-B3C2-98C5C531CBD5
+  - cmd:             LC_BUILD_VERSION
+    cmdsize:         32
+    platform:        1
+    minos:           720896
+    sdk:             1769472
+    ntools:          1
+    Tools:
+      - tool:            3
+        version:         1771897088
+  - cmd:             LC_SOURCE_VERSION
+    cmdsize:         16
+    version:         0
+  - cmd:             LC_LOAD_DYLIB
+    cmdsize:         56
+    dylib:
+      name:            24
+      timestamp:       2
+      current_version: 89063424
+      compatibility_version: 65536
+    Content:         '/usr/lib/libSystem.B.dylib'
+    ZeroPadBytes:    6
+  - cmd:             LC_FUNCTION_STARTS
+    cmdsize:         16
+    dataoff:         32824
+    datasize:        8
+  - cmd:             LC_DATA_IN_CODE
+    cmdsize:         16
+    dataoff:         32832
+    datasize:        0
+  - cmd:             LC_CODE_SIGNATURE
+    cmdsize:         16
+    dataoff:         32944
+    datasize:        408
+LinkEditData:
+  ExportTrie:
+    TerminalSize:    0
+    NodeOffset:      0
+    Name:            ''
+    Flags:           0x0
+    Address:         0x0
+    Other:           0x0
+    ImportName:      ''
+    Children:
+      - TerminalSize:    0
+        NodeOffset:      30
+        Name:            _
+        Flags:           0x0
+        Address:         0x0
+        Other:           0x0
+        ImportName:      ''
+        Children:
+          - TerminalSize:    3
+            NodeOffset:      9
+            Name:            bar
+            Flags:           0x0
+            Address:         0x394
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    4
+            NodeOffset:      14
+            Name:            d
+            Flags:           0x0
+            Address:         0x4000
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    3
+            NodeOffset:      20
+            Name:            foo
+            Flags:           0x0
+            Address:         0x388
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    3
+            NodeOffset:      25
+            Name:            main
+            Flags:           0x0
+            Address:         0x3B4
+            Other:           0x0
+            ImportName:      ''
+  NameList:
+    - n_strx:          21
+      n_type:          0xE
+      n_sect:          3
+      n_desc:          0
+      n_value:         16388
+    - n_strx:          2
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         916
+    - n_strx:          7
+      n_type:          0xF
+      n_sect:          3
+      n_desc:          0
+      n_value:         16384
+    - n_strx:          10
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         904
+    - n_strx:          15
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         948
+  StringTable:
+    - ' '
+    - _bar
+    - _d
+    - _foo
+    - _main
+    - _c
+  FunctionStarts:  [ 0x388, 0x394, 0x3B4 ]
+...
+)");
+
+  auto ExpectedFileNonZeroOffset = TestFile::fromYaml(R"(
+--- !mach-o
+FileHeader:
+  magic:           0xFEEDFACF
+  cputype:         0x100000C
+  cpusubtype:      0x0
+  filetype:        0x6
+  ncmds:           14
+  sizeofcmds:      824
+  flags:           0x100085
+  reserved:        0x0
+LoadCommands:
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         232
+    segname:         __TEXT
+    vmaddr:          245760
+    vmsize:          16384
+    fileoff:         0
+    filesize:        16384
+    maxprot:         5
+    initprot:        5
+    nsects:          2
+    flags:           0
+    Sections:
+      - sectname:        __text
+        segname:         __TEXT
+        addr:            0x3C388
+        size:            96
+        offset:          0x388
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x80000400
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         28000090000540B9C0035FD6FD7BBFA9FD030091FBFFFF9728000090080140B90000080BFD7BC1A8C0035FD6FF8300D1FD7B01A9FD430091BFC31FB8F4FFFF9728000090080540B90800080BE80B00B9E00B40B9FD7B41A9FF830091C0035FD6
+      - sectname:        __unwind_info
+        segname:         __TEXT
+        addr:            0x3C3E8
+        size:            96
+        offset:          0x3E8
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x0
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         010000001C000000000000001C000000000000001C00000002000000880300004000000040000000E90300000000000040000000000000000000000000000000030000000C00020014000200000000000C000001000000020000000400000000
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         152
+    segname:         __DATA
+    vmaddr:          262144
+    vmsize:          16384
+    fileoff:         16384
+    filesize:        16384
+    maxprot:         3
+    initprot:        3
+    nsects:          1
+    flags:           0
+    Sections:
+      - sectname:        __data
+        segname:         __DATA
+        addr:            0x40000
+        size:            8
+        offset:          0x4000
+        align:           2
+        reloff:          0x0
+        nreloc:          0
+        flags:           0x0
+        reserved1:       0x0
+        reserved2:       0x0
+        reserved3:       0x0
+        content:         140000000A000000
+  - cmd:             LC_SEGMENT_64
+    cmdsize:         72
+    segname:         __LINKEDIT
+    vmaddr:          278528
+    vmsize:          16384
+    fileoff:         32768
+    filesize:        584
+    maxprot:         1
+    initprot:        1
+    nsects:          0
+    flags:           0
+  - cmd:             LC_ID_DYLIB
+    cmdsize:         40
+    dylib:
+      name:            24
+      timestamp:       1
+      current_version: 0
+      compatibility_version: 0
+    Content:         liba.dylib
+    ZeroPadBytes:    6
+  - cmd:             LC_DYLD_INFO_ONLY
+    cmdsize:         48
+    rebase_off:      0
+    rebase_size:     0
+    bind_off:        0
+    bind_size:       0
+    weak_bind_off:   0
+    weak_bind_size:  0
+    lazy_bind_off:   0
+    lazy_bind_size:  0
+    export_off:      32768
+    export_size:     56
+  - cmd:             LC_SYMTAB
+    cmdsize:         24
+    symoff:          32832
+    nsyms:           5
+    stroff:          32912
+    strsize:         24
+  - cmd:             LC_DYSYMTAB
+    cmdsize:         80
+    ilocalsym:       0
+    nlocalsym:       1
+    iextdefsym:      1
+    nextdefsym:      4
+    iundefsym:       5
+    nundefsym:       0
+    tocoff:          0
+    ntoc:            0
+    modtaboff:       0
+    nmodtab:         0
+    extrefsymoff:    0
+    nextrefsyms:     0
+    indirectsymoff:  0
+    nindirectsyms:   0
+    extreloff:       0
+    nextrel:         0
+    locreloff:       0
+    nlocrel:         0
+  - cmd:             LC_UUID
+    cmdsize:         24
+    uuid:            9927BB37-87F9-3AAA-B3C2-98C5C531CBD5
+  - cmd:             LC_BUILD_VERSION
+    cmdsize:         32
+    platform:        1
+    minos:           720896
+    sdk:             1769472
+    ntools:          1
+    Tools:
+      - tool:            3
+        version:         1771897088
+  - cmd:             LC_SOURCE_VERSION
+    cmdsize:         16
+    version:         0
+  - cmd:             LC_LOAD_DYLIB
+    cmdsize:         56
+    dylib:
+      name:            24
+      timestamp:       2
+      current_version: 89063424
+      compatibility_version: 65536
+    Content:         '/usr/lib/libSystem.B.dylib'
+    ZeroPadBytes:    6
+  - cmd:             LC_FUNCTION_STARTS
+    cmdsize:         16
+    dataoff:         32824
+    datasize:        8
+  - cmd:             LC_DATA_IN_CODE
+    cmdsize:         16
+    dataoff:         32832
+    datasize:        0
+  - cmd:             LC_CODE_SIGNATURE
+    cmdsize:         16
+    dataoff:         32944
+    datasize:        408
+LinkEditData:
+  ExportTrie:
+    TerminalSize:    0
+    NodeOffset:      0
+    Name:            ''
+    Flags:           0x0
+    Address:         0x0
+    Other:           0x0
+    ImportName:      ''
+    Children:
+      - TerminalSize:    0
+        NodeOffset:      30
+        Name:            _
+        Flags:           0x0
+        Address:         0x0
+        Other:           0x0
+        ImportName:      ''
+        Children:
+          - TerminalSize:    3
+            NodeOffset:      9
+            Name:            bar
+            Flags:           0x0
+            Address:         0x394
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    4
+            NodeOffset:      14
+            Name:            d
+            Flags:           0x0
+            Address:         0x4000
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    3
+            NodeOffset:      20
+            Name:            foo
+            Flags:           0x0
+            Address:         0x388
+            Other:           0x0
+            ImportName:      ''
+          - TerminalSize:    3
+            NodeOffset:      25
+            Name:            main
+            Flags:           0x0
+            Address:         0x3B4
+            Other:           0x0
+            ImportName:      ''
+  NameList:
+    - n_strx:          21
+      n_type:          0xE
+      n_sect:          3
+      n_desc:          0
+      n_value:         262148
+    - n_strx:          2
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         246676
+    - n_strx:          7
+      n_type:          0xF
+      n_sect:          3
+      n_desc:          0
+      n_value:         262144
+    - n_strx:          10
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         246664
+    - n_strx:          15
+      n_type:          0xF
+      n_sect:          1
+      n_desc:          0
+      n_value:         246708
+  StringTable:
+    - ' '
+    - _bar
+    - _d
+    - _foo
+    - _main
+    - _c
+  FunctionStarts:  [ 0x388, 0x394, 0x3B4 ]
+...
+)");
+
+  ASSERT_THAT_EXPECTED(ExpectedFileZeroOffset, llvm::Succeeded());
+  auto zero_module_sp =
+      std::make_shared<Module>(ExpectedFileZeroOffset->moduleSpec());
+  ObjectFile *zero_objfile = zero_module_sp->GetObjectFile();
+  ASSERT_NE(zero_objfile, nullptr);
+  // Now encode and decode an actual symbol table from our yaml.
+  Symtab *module_symtab = zero_module_sp->GetSymtab();
+  ASSERT_NE(module_symtab, nullptr);
+  module_symtab->PreloadSymbols();
+  const uint8_t addr_size = 8;
+  DataEncoder zero_file(eByteOrderLittle, addr_size);
+  module_symtab->Encode(zero_file);
+  llvm::ArrayRef<uint8_t> zero_filebytes = zero_file.GetData();
+
+  ASSERT_THAT_EXPECTED(ExpectedFileNonZeroOffset, llvm::Succeeded());
+  auto nonzero_module_sp =
+      std::make_shared<Module>(ExpectedFileNonZeroOffset->moduleSpec());
+  ObjectFile *nonzero_objfile = nonzero_module_sp->GetObjectFile();
+  ASSERT_NE(nonzero_objfile, nullptr);
+  // Now encode and decode an actual symbol table from our yaml.
+  Symtab *nonzero_module_symtab = nonzero_module_sp->GetSymtab();
+  ASSERT_NE(nonzero_module_symtab, nullptr);
+  nonzero_module_symtab->PreloadSymbols();
+  DataEncoder nonzero_file(eByteOrderLittle, addr_size);
+  module_symtab->Encode(nonzero_file);
+  llvm::ArrayRef<uint8_t> nonzero_filebytes = nonzero_file.GetData();
+
+  ASSERT_EQ(zero_filebytes, nonzero_filebytes);
+}



More information about the lldb-commits mailing list