std::string MemoryValue::toHexString(bool Ox, bool leadingZeros) const { static const char hex[] = "0123456789ABCDEF"; std::stringstream stream{}; if (Ox) { stream << "0x"; } bool zero = true; // reversely iterate over all the bytes in value (high->low) for (std::size_t l = (getSize() + 7) / 8; l-- > 0;) { std::uint8_t byte = getByteAt(l * 8); std::uint8_t upperChar = byte / 16; std::uint8_t lowerChar = byte % 16; // do not print if this is the first character && it's 0 if (leadingZeros || !zero || upperChar != 0) { stream.put(hex[upperChar]); zero = false; } // do not print if this is the first character && it's 0 if (leadingZeros || !zero || lowerChar != 0) { stream.put(hex[lowerChar]); zero = false; } } // if no characters have been printed print 0 if (zero) { stream.put('0'); } return stream.str(); }
Error Assembler::bind(const Label& label) { // Get label data based on label id. uint32_t index = label.getId(); LabelData* data = getLabelData(index); // Label can be bound only once. if (data->offset != -1) return setError(kErrorLabelAlreadyBound); #if !defined(ASMJIT_DISABLE_LOGGER) if (_logger) _logger->logFormat(kLoggerStyleLabel, "L%u:\n", index); #endif // !ASMJIT_DISABLE_LOGGER Error error = kErrorOk; size_t pos = getOffset(); LabelLink* link = data->links; LabelLink* prev = NULL; while (link) { intptr_t offset = link->offset; if (link->relocId != -1) { // Handle RelocData - We have to update RelocData information instead of // patching the displacement in LabelData. _relocList[link->relocId].data += static_cast<Ptr>(pos); } else { // Not using relocId, this means that we are overwriting a real // displacement in the binary stream. int32_t patchedValue = static_cast<int32_t>( static_cast<intptr_t>(pos) - offset + link->displacement); // Size of the value we are going to patch. Only BYTE/DWORD is allowed. uint32_t size = getByteAt(offset); ASMJIT_ASSERT(size == 1 || size == 4); if (size == 4) { setInt32At(offset, patchedValue); } else { ASMJIT_ASSERT(size == 1); if (IntUtil::isInt8(patchedValue)) setByteAt(offset, static_cast<uint8_t>(patchedValue & 0xFF)); else error = kErrorIllegalDisplacement; } } prev = link->prev; link = prev; } // Chain unused links. link = data->links; if (link) { if (prev == NULL) prev = link; prev->prev = _unusedLinks; _unusedLinks = link; } // Set as bound (offset is zero or greater and no links). data->offset = pos; data->links = NULL; if (error != kErrorOk) return setError(error); return error; }