Path: blob/main/contrib/llvm-project/llvm/lib/BinaryFormat/COFF.cpp
35233 views
//===- llvm/BinaryFormat/COFF.cpp - The COFF format -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/BinaryFormat/COFF.h"9#include "llvm/ADT/SmallVector.h"10#include "llvm/ADT/Twine.h"1112// Maximum offsets for different string table entry encodings.13enum : unsigned { Max7DecimalOffset = 9999999U };14enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 01516// Encode a string table entry offset in base 64, padded to 6 chars, and17// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...18// Buffer must be at least 8 bytes large. No terminating null appended.19static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {20assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&21"Illegal section name encoding for value");2223static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"24"abcdefghijklmnopqrstuvwxyz"25"0123456789+/";2627Buffer[0] = '/';28Buffer[1] = '/';2930char *Ptr = Buffer + 7;31for (unsigned i = 0; i < 6; ++i) {32unsigned Rem = Value % 64;33Value /= 64;34*(Ptr--) = Alphabet[Rem];35}36}3738bool llvm::COFF::encodeSectionName(char *Out, uint64_t Offset) {39if (Offset <= Max7DecimalOffset) {40// Offsets of 7 digits or less are encoded in ASCII.41SmallVector<char, COFF::NameSize> Buffer;42Twine('/').concat(Twine(Offset)).toVector(Buffer);43assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);44std::memcpy(Out, Buffer.data(), Buffer.size());45return true;46}4748if (Offset <= MaxBase64Offset) {49// Starting with 10,000,000, offsets are encoded as base64.50encodeBase64StringEntry(Out, Offset);51return true;52}5354// The offset is too large to be encoded.55return false;56}575859