5 #include "src/reloc-info.h" 7 #include "src/assembler-inl.h" 8 #include "src/code-reference.h" 9 #include "src/code-stubs.h" 10 #include "src/deoptimize-reason.h" 11 #include "src/deoptimizer.h" 12 #include "src/heap/heap-write-barrier-inl.h" 13 #include "src/objects/code-inl.h" 14 #include "src/snapshot/snapshot.h" 19 const char*
const RelocInfo::kFillerCommentString =
"DEOPTIMIZATION PADDING";
65 const int kTagBits = 2;
66 const int kTagMask = (1 << kTagBits) - 1;
67 const int kLongTagBits = 6;
69 const int kEmbeddedObjectTag = 0;
70 const int kCodeTargetTag = 1;
71 const int kWasmStubCallTag = 2;
72 const int kDefaultTag = 3;
74 const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
75 const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
76 const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
78 const int kChunkBits = 7;
79 const int kChunkMask = (1 << kChunkBits) - 1;
80 const int kLastChunkTagBits = 1;
81 const int kLastChunkTagMask = 1;
82 const int kLastChunkTag = 1;
88 if (is_uintn(pc_delta, kSmallPCDeltaBits))
return pc_delta;
89 WriteMode(RelocInfo::PC_JUMP);
90 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
91 DCHECK_GT(pc_jump, 0);
93 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
94 byte b = pc_jump & kChunkMask;
95 *--pos_ = b << kLastChunkTagBits;
98 *pos_ = *pos_ | kLastChunkTag;
100 return pc_delta & kSmallPCDeltaMask;
103 void RelocInfoWriter::WriteShortTaggedPC(
uint32_t pc_delta,
int tag) {
105 pc_delta = WriteLongPCJump(pc_delta);
106 *--pos_ = pc_delta << kTagBits | tag;
109 void RelocInfoWriter::WriteShortData(intptr_t data_delta) {
110 *--pos_ =
static_cast<byte
>(data_delta);
113 void RelocInfoWriter::WriteMode(RelocInfo::Mode rmode) {
114 STATIC_ASSERT(RelocInfo::NUMBER_OF_MODES <= (1 << kLongTagBits));
115 *--pos_ =
static_cast<int>((rmode << kTagBits) | kDefaultTag);
118 void RelocInfoWriter::WriteModeAndPC(
uint32_t pc_delta, RelocInfo::Mode rmode) {
120 pc_delta = WriteLongPCJump(pc_delta);
125 void RelocInfoWriter::WriteIntData(
int number) {
126 for (
int i = 0;
i < kIntSize;
i++) {
127 *--pos_ =
static_cast<byte
>(number);
129 number = number >> kBitsPerByte;
133 void RelocInfoWriter::WriteData(intptr_t data_delta) {
134 for (
int i = 0;
i < kIntptrSize;
i++) {
135 *--pos_ =
static_cast<byte
>(data_delta);
137 data_delta = data_delta >> kBitsPerByte;
141 void RelocInfoWriter::Write(
const RelocInfo* rinfo) {
142 RelocInfo::Mode rmode = rinfo->rmode();
144 byte* begin_pos = pos_;
146 DCHECK(rinfo->rmode() < RelocInfo::NUMBER_OF_MODES);
147 DCHECK_GE(rinfo->pc() -
reinterpret_cast<Address
>(last_pc_), 0);
150 static_cast<uint32_t>(rinfo->pc() -
reinterpret_cast<Address
>(last_pc_));
153 if (rmode == RelocInfo::EMBEDDED_OBJECT) {
154 WriteShortTaggedPC(pc_delta, kEmbeddedObjectTag);
155 }
else if (rmode == RelocInfo::CODE_TARGET) {
156 WriteShortTaggedPC(pc_delta, kCodeTargetTag);
157 DCHECK_LE(begin_pos - pos_, RelocInfo::kMaxCallSize);
158 }
else if (rmode == RelocInfo::WASM_STUB_CALL) {
159 WriteShortTaggedPC(pc_delta, kWasmStubCallTag);
161 WriteModeAndPC(pc_delta, rmode);
162 if (RelocInfo::IsComment(rmode)) {
163 WriteData(rinfo->data());
164 }
else if (RelocInfo::IsDeoptReason(rmode)) {
165 DCHECK_LT(rinfo->data(), 1 << kBitsPerByte);
166 WriteShortData(rinfo->data());
167 }
else if (RelocInfo::IsConstPool(rmode) ||
168 RelocInfo::IsVeneerPool(rmode) || RelocInfo::IsDeoptId(rmode) ||
169 RelocInfo::IsDeoptPosition(rmode)) {
170 WriteIntData(static_cast<int>(rinfo->data()));
173 last_pc_ =
reinterpret_cast<byte*
>(rinfo->pc());
175 DCHECK_LE(begin_pos - pos_, kMaxSize);
179 inline int RelocIterator::AdvanceGetTag() {
return *--pos_ & kTagMask; }
181 inline RelocInfo::Mode RelocIterator::GetMode() {
182 return static_cast<RelocInfo::Mode
>((*pos_ >> kTagBits) &
183 ((1 << kLongTagBits) - 1));
186 inline void RelocIterator::ReadShortTaggedPC() {
187 rinfo_.pc_ += *pos_ >> kTagBits;
190 inline void RelocIterator::AdvanceReadPC() { rinfo_.pc_ += *--pos_; }
192 void RelocIterator::AdvanceReadInt() {
194 for (
int i = 0;
i < kIntSize;
i++) {
195 x |=
static_cast<int>(*--pos_) <<
i * kBitsPerByte;
200 void RelocIterator::AdvanceReadData() {
202 for (
int i = 0;
i < kIntptrSize;
i++) {
203 x |=
static_cast<intptr_t
>(*--pos_) <<
i * kBitsPerByte;
208 void RelocIterator::AdvanceReadLongPCJump() {
213 for (
int i = 0;
i < kIntSize;
i++) {
214 byte pc_jump_part = *--pos_;
215 pc_jump |= (pc_jump_part >> kLastChunkTagBits) <<
i * kChunkBits;
216 if ((pc_jump_part & kLastChunkTagMask) == 1)
break;
220 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
223 inline void RelocIterator::ReadShortData() {
224 uint8_t unsigned_b = *pos_;
225 rinfo_.data_ = unsigned_b;
228 void RelocIterator::next() {
235 while (pos_ > end_) {
236 int tag = AdvanceGetTag();
237 if (tag == kEmbeddedObjectTag) {
239 if (SetMode(RelocInfo::EMBEDDED_OBJECT))
return;
240 }
else if (tag == kCodeTargetTag) {
242 if (SetMode(RelocInfo::CODE_TARGET))
return;
243 }
else if (tag == kWasmStubCallTag) {
245 if (SetMode(RelocInfo::WASM_STUB_CALL))
return;
247 DCHECK_EQ(tag, kDefaultTag);
248 RelocInfo::Mode rmode = GetMode();
249 if (rmode == RelocInfo::PC_JUMP) {
250 AdvanceReadLongPCJump();
253 if (RelocInfo::IsComment(rmode)) {
254 if (SetMode(rmode)) {
258 Advance(kIntptrSize);
259 }
else if (RelocInfo::IsDeoptReason(rmode)) {
261 if (SetMode(rmode)) {
265 }
else if (RelocInfo::IsConstPool(rmode) ||
266 RelocInfo::IsVeneerPool(rmode) ||
267 RelocInfo::IsDeoptId(rmode) ||
268 RelocInfo::IsDeoptPosition(rmode)) {
269 if (SetMode(rmode)) {
274 }
else if (SetMode(static_cast<RelocInfo::Mode>(rmode))) {
283 RelocIterator::RelocIterator(Code code,
int mode_mask)
284 : RelocIterator(code, code->unchecked_relocation_info(), mode_mask) {}
286 RelocIterator::RelocIterator(Code code, ByteArray relocation_info,
288 : RelocIterator(code, code->raw_instruction_start(), code->constant_pool(),
289 relocation_info->GetDataEndAddress(),
290 relocation_info->GetDataStartAddress(), mode_mask) {}
292 RelocIterator::RelocIterator(
const CodeReference code_reference,
int mode_mask)
293 : RelocIterator(Code(), code_reference.instruction_start(),
294 code_reference.constant_pool(),
295 code_reference.relocation_end(),
296 code_reference.relocation_start(), mode_mask) {}
298 RelocIterator::RelocIterator(EmbeddedData* embedded_data, Code code,
301 code, embedded_data->InstructionStartOfBuiltin(code->builtin_index()),
302 code->constant_pool(),
303 code->relocation_start() + code->relocation_size(),
304 code->relocation_start(), mode_mask) {}
306 RelocIterator::RelocIterator(
const CodeDesc& desc,
int mode_mask)
307 : RelocIterator(Code(), reinterpret_cast<Address>(desc.buffer), 0,
308 desc.buffer + desc.buffer_size,
309 desc.buffer + desc.buffer_size - desc.reloc_size,
312 RelocIterator::RelocIterator(Vector<byte> instructions,
313 Vector<const byte> reloc_info, Address const_pool,
315 : RelocIterator(Code(), reinterpret_cast<Address>(instructions.start()),
316 const_pool, reloc_info.start() + reloc_info.size(),
317 reloc_info.start(), mode_mask) {}
319 RelocIterator::RelocIterator(Code host, Address pc, Address constant_pool,
320 const byte* pos,
const byte* end,
int mode_mask)
321 : pos_(pos), end_(end), mode_mask_(mode_mask) {
323 DCHECK_GE(pos_, end_);
326 rinfo_.constant_pool_ = constant_pool;
327 if (mode_mask_ == 0) pos_ = end_;
335 bool RelocInfo::OffHeapTargetIsCodedSpecially() {
336 #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_ARM64) || \ 337 defined(V8_TARGET_ARCH_X64) 339 #elif defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_MIPS) || \ 340 defined(V8_TARGET_ARCH_MIPS64) || defined(V8_TARGET_ARCH_PPC) || \ 341 defined(V8_TARGET_ARCH_S390) 346 Address RelocInfo::wasm_call_address()
const {
347 DCHECK_EQ(rmode_, WASM_CALL);
348 return Assembler::target_address_at(pc_, constant_pool_);
351 void RelocInfo::set_wasm_call_address(Address address,
352 ICacheFlushMode icache_flush_mode) {
353 DCHECK_EQ(rmode_, WASM_CALL);
354 Assembler::set_target_address_at(pc_, constant_pool_, address,
358 Address RelocInfo::wasm_stub_call_address()
const {
359 DCHECK_EQ(rmode_, WASM_STUB_CALL);
360 return Assembler::target_address_at(pc_, constant_pool_);
363 void RelocInfo::set_wasm_stub_call_address(Address address,
364 ICacheFlushMode icache_flush_mode) {
365 DCHECK_EQ(rmode_, WASM_STUB_CALL);
366 Assembler::set_target_address_at(pc_, constant_pool_, address,
370 void RelocInfo::set_target_address(Address target,
371 WriteBarrierMode write_barrier_mode,
372 ICacheFlushMode icache_flush_mode) {
373 DCHECK(IsCodeTargetMode(rmode_) || IsRuntimeEntry(rmode_) ||
375 Assembler::set_target_address_at(pc_, constant_pool_, target,
377 if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
378 IsCodeTargetMode(rmode_)) {
379 Code target_code = Code::GetCodeFromTargetAddress(target);
380 MarkingBarrierForCode(host(),
this, target_code);
384 bool RelocInfo::RequiresRelocationAfterCodegen(
const CodeDesc& desc) {
385 RelocIterator it(desc, RelocInfo::PostCodegenRelocationMask());
389 bool RelocInfo::RequiresRelocation(Code code) {
390 RelocIterator it(code, RelocInfo::kApplyMask);
394 #ifdef ENABLE_DISASSEMBLER 395 const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
399 case EMBEDDED_OBJECT:
400 return "embedded object";
402 return "code target";
403 case RELATIVE_CODE_TARGET:
404 return "relative code target";
406 return "runtime entry";
409 case EXTERNAL_REFERENCE:
410 return "external reference";
411 case INTERNAL_REFERENCE:
412 return "internal reference";
413 case INTERNAL_REFERENCE_ENCODED:
414 return "encoded internal reference";
415 case OFF_HEAP_TARGET:
416 return "off heap target";
417 case DEOPT_SCRIPT_OFFSET:
418 return "deopt script offset";
419 case DEOPT_INLINING_ID:
420 return "deopt inlining id";
422 return "deopt reason";
424 return "deopt index";
426 return "constant pool";
428 return "veneer pool";
430 return "internal wasm call";
432 return "wasm stub call";
433 case NUMBER_OF_MODES:
437 return "unknown relocation type";
440 void RelocInfo::Print(Isolate* isolate, std::ostream& os) {
441 os << reinterpret_cast<const void*>(pc_) <<
" " << RelocModeName(rmode_);
442 if (IsComment(rmode_)) {
443 os <<
" (" <<
reinterpret_cast<char*
>(data_) <<
")";
444 }
else if (rmode_ == DEOPT_SCRIPT_OFFSET || rmode_ == DEOPT_INLINING_ID) {
445 os <<
" (" << data() <<
")";
446 }
else if (rmode_ == DEOPT_REASON) {
448 << DeoptimizeReasonToString(static_cast<DeoptimizeReason>(data_)) <<
")";
449 }
else if (rmode_ == EMBEDDED_OBJECT) {
450 os <<
" (" << Brief(target_object()) <<
")";
451 }
else if (rmode_ == EXTERNAL_REFERENCE) {
453 ExternalReferenceEncoder ref_encoder(isolate);
455 << ref_encoder.NameOfAddress(isolate, target_external_reference())
458 os <<
" (" <<
reinterpret_cast<const void*
>(target_external_reference())
460 }
else if (IsCodeTargetMode(rmode_)) {
461 const Address code_target = target_address();
462 Code code = Code::GetCodeFromTargetAddress(code_target);
463 DCHECK(code->IsCode());
464 os <<
" (" << Code::Kind2String(code->kind());
465 if (Builtins::IsBuiltin(code)) {
466 os <<
" " << Builtins::name(code->builtin_index());
467 }
else if (code->kind() == Code::STUB) {
468 os <<
" " << CodeStub::MajorName(CodeStub::GetMajorKey(code));
470 os <<
") (" <<
reinterpret_cast<const void*
>(target_address()) <<
")";
471 }
else if (IsRuntimeEntry(rmode_) && isolate->deoptimizer_data() !=
nullptr) {
474 if (Deoptimizer::IsDeoptimizationEntry(isolate, target_address(), &type)) {
475 int id = GetDeoptimizationId(isolate, type);
476 os <<
" (" << Deoptimizer::MessageFor(type) <<
" deoptimization bailout " 479 }
else if (IsConstPool(rmode_)) {
480 os <<
" (size " <<
static_cast<int>(data_) <<
")";
485 #endif // ENABLE_DISASSEMBLER 488 void RelocInfo::Verify(Isolate* isolate) {
490 case EMBEDDED_OBJECT:
491 Object::VerifyPointer(isolate, target_object());
494 case RELATIVE_CODE_TARGET: {
496 Address addr = target_address();
497 CHECK_NE(addr, kNullAddress);
499 Code code = Code::GetCodeFromTargetAddress(addr);
500 Object* found = isolate->FindCodeObject(addr);
501 CHECK(found->IsCode());
502 CHECK(code->address() == HeapObject::cast(found)->address());
505 case INTERNAL_REFERENCE:
506 case INTERNAL_REFERENCE_ENCODED: {
507 Address target = target_internal_reference();
508 Address pc = target_internal_reference_address();
509 Code code = Code::cast(isolate->FindCodeObject(pc));
510 CHECK(target >= code->InstructionStart());
511 CHECK(target <= code->InstructionEnd());
514 case OFF_HEAP_TARGET: {
515 Address addr = target_off_heap_target();
516 CHECK_NE(addr, kNullAddress);
517 CHECK(!InstructionStream::TryLookupCode(isolate, addr).is_null());
522 case EXTERNAL_REFERENCE:
523 case DEOPT_SCRIPT_OFFSET:
524 case DEOPT_INLINING_ID:
533 case NUMBER_OF_MODES:
539 #endif // VERIFY_HEAP