Files
sdk/runtime/vm/so_writer.h
T
Ryan Macnak d36adbacaf [vm] Remove the VM isolate.
The former contents of the VM isolate are now included into each isolate group. This makes each isolate group's heap independent, and in particular allows each heap to be allocated to a separate pointer cage (not done in this CL).

The duplicated stubs that allowed PC relative calls are removed, since the originals can now be the target of PC relative calls.

The bootstrapping needing to load an AppJIT or AppAOT snapshot is reduced to allocating the oddballs. The code is entirely dropped in the AOT runtime, but the JIT runtime still has it to allow for flags to affect the compilation of the stub code. Further refactoring might be able to remove this for the JIT runtime too, with only gen_snapshot knowing how to bootstrap.

Class serialization no longer distinguishes predefined classes.

The page containing null is marked as never-evacuate. null, false and true must not move because the compiler relies on their low bits having certain patterns for some optimizations. (Previously, the entire VM isolate heap never moved.)

Compaction is disabled for IA32. Due to register pressure, some stub calls must not use a scratch register and embed the address of Code.

The page containing the call-through-safepoint stub is frozen when running with --write-protect-code and the stub is created at runtime (instead of loaded from an AppJIT or AppAOT snapshot). This stub must remain executable even during a safepoint, as a foreign call might during return during a safepoint and only block after the stub directs it to the runtime.

The snapshot symbols are renamed to kDartSnapshotData and kDartSnapshotText. There is no need to distinguish the VM isolate's snapshot, and snaphots are per isolate group not per isolate. Aliases with the old names are added to ease migration.

Some global flags that were automatically set based on the VM isolate's snapshot are now isolate group flags and automatically set by the isolate group's snapshot.

TEST=ci
Change-Id: Iee82016057d609112e9b021d178fc3d4d18b5044
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/500621
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
2026-05-18 11:35:03 -07:00

242 lines
7.6 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.
#ifndef RUNTIME_VM_SO_WRITER_H_
#define RUNTIME_VM_SO_WRITER_H_
#include "platform/globals.h"
#if defined(DART_PRECOMPILER)
#include "vm/allocation.h"
#include "vm/compiler/runtime_api.h"
#include "vm/datastream.h"
#include "vm/growable_array.h"
#include "vm/zone.h"
namespace dart {
class Dwarf;
class ElfWriter;
class MachOWriter;
class SharedObjectWriter : public ZoneObject {
public:
enum class Type {
// A snapshot that should include segment contents.
Snapshot,
// Separately compiled debugging information that should not include
// most segment contents.
DebugInfo,
// A relocatable object file.
Object,
};
enum class Output {
Elf,
MachO,
};
SharedObjectWriter(Zone* zone,
BaseWriteStream* stream,
Type type,
Dwarf* dwarf = nullptr)
: zone_(zone), unwrapped_stream_(stream), type_(type), dwarf_(dwarf) {
// Separate debugging information should always have a Dwarf object.
ASSERT(type == Type::Snapshot || dwarf != nullptr);
// Assumed by various offset logic in the subclasses.
ASSERT_EQUAL(stream->Position(), 0);
}
virtual ~SharedObjectWriter() {}
virtual intptr_t page_size() const = 0;
virtual Output output() const = 0;
static bool IsStripped(Dwarf* dwarf) { return dwarf == nullptr; }
bool IsStripped() const { return IsStripped(dwarf_); }
Zone* zone() const { return zone_; }
Dwarf* dwarf() { return dwarf_; }
SharedObjectWriter::Type type() const { return type_; }
// Stores the information needed to appropriately generate a
// relocation from the target to the source at the given section offset.
struct Relocation {
size_t size_in_bytes;
intptr_t section_offset;
intptr_t source_label;
intptr_t source_offset;
intptr_t target_label;
intptr_t target_offset;
// Used when the corresponding offset is relative from the location of the
// relocation itself.
static constexpr intptr_t kSelfRelative = -1;
// Used when the corresponding offset is relative to the start of the
// snapshot.
static constexpr intptr_t kSnapshotRelative = -2;
Relocation(size_t size_in_bytes,
intptr_t section_offset,
intptr_t source_label,
intptr_t source_offset,
intptr_t target_label,
intptr_t target_offset)
: size_in_bytes(size_in_bytes),
section_offset(section_offset),
source_label(source_label),
source_offset(source_offset),
target_label(target_label),
target_offset(target_offset) {
// Other than special values, all labels should be positive.
ASSERT(source_label > 0 || source_label == kSelfRelative ||
source_label == kSnapshotRelative);
ASSERT(target_label > 0 || target_label == kSelfRelative ||
target_label == kSnapshotRelative);
}
};
using RelocationArray = ZoneGrowableArray<Relocation>;
// Stores the information needed to appropriately generate a symbol
// during finalization.
struct SymbolData {
enum class Type {
Section,
Function,
Object,
};
const char* name;
Type type;
intptr_t offset;
size_t size;
// A positive unique ID only used internally in the Dart VM, not part of
// the shared object output.
intptr_t label;
SymbolData(const char* name,
Type type,
intptr_t offset,
size_t size,
intptr_t label)
: name(name), type(type), offset(offset), size(size), label(label) {
ASSERT(label > 0);
}
};
using SymbolDataArray = ZoneGrowableArray<SymbolData>;
struct WriteStream : public AbstractWriteStream {
explicit WriteStream(SharedObjectWriter::Type type) : type_(type) {}
SharedObjectWriter::Type type() const { return type_; }
void WriteBytesWithRelocations(const uint8_t* bytes,
intptr_t size,
intptr_t start_address,
const RelocationArray& relocations);
virtual bool HasValueForLabel(intptr_t label, intptr_t* value) const = 0;
intptr_t FindValueForLabel(intptr_t label) const {
intptr_t value = -1;
const bool valid = HasValueForLabel(label, &value);
if (!valid) {
FATAL("Expected symbol for label: %" Pd "", label);
}
return value;
}
protected:
virtual void WriteRelocatableValue(intptr_t address,
const Relocation& reloc,
intptr_t reloc_index);
private:
const SharedObjectWriter::Type type_;
DISALLOW_COPY_AND_ASSIGN(WriteStream);
};
class DelegatingWriteStream : public WriteStream {
public:
DelegatingWriteStream(BaseWriteStream* stream,
const SharedObjectWriter& writer)
: WriteStream(writer.type()),
stream_(ASSERT_NOTNULL(stream)),
start_(stream->Position()),
page_size_(writer.page_size()) {
// So that we can use the underlying stream's Align, as all alignments
// will be less than or equal to this alignment.
ASSERT(Utils::IsAligned(start_, page_size_));
}
// We return positions in terms of the local content that has been written,
// ignoring any previous content on the stream.
intptr_t Position() const override { return stream_->Position() - start_; }
void WriteBytes(const void* b, intptr_t size) override {
stream_->WriteBytes(b, size);
}
void WriteByte(uint8_t value) override { stream_->WriteByte(value); }
intptr_t Align(intptr_t alignment, intptr_t offset = 0) override {
ASSERT(Utils::IsPowerOfTwo(alignment));
ASSERT(alignment <= page_size_);
return stream_->Align(alignment, offset);
}
protected:
BaseWriteStream* const stream_;
private:
const intptr_t start_;
const intptr_t page_size_;
DISALLOW_COPY_AND_ASSIGN(DelegatingWriteStream);
};
// Must be the same value as the values returned by ImageWriter::SectionLabel
// for the appropriate section and vm values.
enum ReservedLabels : intptr_t {
kIsolateInstructionsLabel = 1,
kIsolateDataLabel = 2,
kIsolateBssLabel = 3,
kBuildIdLabel = 4,
kMachOEhFrameLabel = 5,
kLastReservedLabel = kMachOEhFrameLabel,
};
virtual void AddText(const char* name,
intptr_t label,
const uint8_t* bytes,
intptr_t size,
const RelocationArray* relocations,
const SymbolDataArray* symbols) = 0;
virtual void AddROData(const char* name,
intptr_t label,
const uint8_t* bytes,
intptr_t size,
const RelocationArray* relocations,
const SymbolDataArray* symbols) = 0;
virtual void Finalize() = 0;
virtual void AssertConsistency(const SharedObjectWriter* debug) const = 0;
virtual const ElfWriter* AsElfWriter() const { return nullptr; }
virtual const MachOWriter* AsMachOWriter() const { return nullptr; }
protected:
Zone* const zone_;
BaseWriteStream* const unwrapped_stream_;
const Type type_;
// If nullptr, then the shared object file should be stripped of static
// information like the static symbol table.
Dwarf* const dwarf_;
};
} // namespace dart
#endif // DART_PRECOMPILER
#endif // RUNTIME_VM_SO_WRITER_H_