Files
sdk/runtime/vm/so_writer.cc
T
Tess Strickland 84a1b114cd [vm] Add base class for shared object writers.
This CL pulls out the refactorings used to support the new MachOWriter
in a followup CL to allow them to be reviewed separately.

Rename Elf -> ElfWriter. Also rename model classes used by ElfWriter
for concepts that exist both in ELF and Mach-O to ElfX. For example,
the old ELF-specific SymbolTable is renamed to ElfSymbolTable.

Adds SharedObjectWriter to serve as a base class for both ElfWriter
and the upcoming MachOWriter.

Adds a new AbstractWriteStream that serves as a common superclass
of both BaseWriteStream and SharedObjectWriter::WriteStream and
allows the creation of fully delegating WriteStreams that do not
maintain a local buffer.

Abstract the old Elf::SymbolData class into
SharedObjectWriter::SymbolData, which stores an enum value as the type
of the symbol instead of storing the ELF encoding of the type.

Rename the DwarfElfStream (which actually wasn't ELF specific, as
all the ELF-specific DWARF information is handled by ElfWriter) to
DwarfSharedObjectStream and put it in a separate header file.

Rename Image::compiled_to_elf() to Image::compiled_to_shared_object()
and add a separate Image::compiled_to_elf() that checks for the ELF
magic value at the DSO base. Also add Image::shared_object_start()
and Image::build_id_start() to return pointers to the DSO base and
the build ID note, respectively.

Refactor Image::build_id() and Image::build_id_length() to check
compiled_for_elf() prior to decoding the data pointed to by
build_id_start() as an ELF note section.

Create an AOTSnapshotType enum to specific the snapshot writer to use
in CreateAppAOTSnapshot instead of using an as_elf boolean.

TEST=refactorings, so existing tests on ci

Change-Id: Ia3ab37a4dff93b6e00390b123753be5a51fbdaaa
Cq-Include-Trybots: luci.dart.try:vm-aot-linux-debug-x64-try,vm-aot-mac-release-arm64-try,vm-aot-dwarf-linux-product-x64-try,vm-linux-debug-x64-try,vm-mac-debug-arm64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421301
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Tess Strickland <sstrickl@google.com>
2025-05-21 01:36:22 -07:00

78 lines
2.7 KiB
C++

// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/so_writer.h"
#if defined(DART_PRECOMPILER)
#include "vm/image_snapshot.h"
namespace dart {
void SharedObjectWriter::WriteStream::WriteBytesWithRelocations(
const uint8_t* bytes,
intptr_t size,
intptr_t start_address,
const RelocationArray& relocations) {
// Resolve relocations as we write.
intptr_t current_pos = 0;
for (const auto& reloc : relocations) {
// We assume here that the relocations are sorted in increasing order,
// with unique section offsets.
const intptr_t preceding = reloc.section_offset - current_pos;
if (preceding > 0) {
WriteBytes(bytes + current_pos, preceding);
current_pos += preceding;
}
ASSERT_EQUAL(current_pos, reloc.section_offset);
intptr_t source_address = reloc.source_offset;
switch (reloc.source_label) {
case Relocation::kSelfRelative:
source_address += start_address + current_pos;
break;
case Relocation::kSnapshotRelative:
// No change to source_address.
break;
default:
ASSERT(reloc.source_label > 0);
source_address += FindValueForLabel(reloc.source_label);
}
ASSERT(reloc.size_in_bytes <= kWordSize);
word to_write = reloc.target_offset - source_address;
switch (reloc.target_label) {
case Relocation::kSelfRelative:
to_write += start_address + current_pos;
break;
case Relocation::kSnapshotRelative:
// No change to to_write.
break;
default: {
ASSERT(reloc.target_label > 0);
intptr_t value;
if (HasValueForLabel(reloc.target_label, &value)) {
to_write += value;
} else {
ASSERT_EQUAL(reloc.target_label, kBuildIdLabel);
ASSERT_EQUAL(reloc.target_offset, 0);
ASSERT_EQUAL(reloc.source_offset, 0);
ASSERT_EQUAL(reloc.size_in_bytes, compiler::target::kWordSize);
// TODO(dartbug.com/43516): Special case for snapshots with deferred
// sections that handles the build ID relocation in an
// InstructionsSection when there is no build ID.
to_write = Image::kNoRelocatedAddress;
}
}
}
ASSERT(Utils::IsInt(reloc.size_in_bytes * kBitsPerByte, to_write));
WriteBytes(reinterpret_cast<const uint8_t*>(&to_write),
reloc.size_in_bytes);
current_pos += reloc.size_in_bytes;
}
WriteBytes(bytes + current_pos, size - current_pos);
}
} // namespace dart
#endif // DART_PRECOMPILER