LLVM 20.0.0git
MCELFExtras.h
Go to the documentation of this file.
1//===- MCELFExtras.h - Extra functions for ELF ------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCELFEXTRAS_H
10#define LLVM_MC_MCELFEXTRAS_H
11
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/bit.h"
15#include "llvm/Support/LEB128.h"
17
18#include <cstdint>
19#include <type_traits>
20
21namespace llvm::ELF {
22// Encode relocations as CREL to OS. ToCrel is responsible for converting a
23// const RelocsTy & to an Elf_Crel.
24template <bool Is64, class RelocsTy, class F>
25void encodeCrel(raw_ostream &OS, RelocsTy Relocs, F ToCrel) {
26 using uint = std::conditional_t<Is64, uint64_t, uint32_t>;
27 uint OffsetMask = 8, Offset = 0, Addend = 0;
28 uint32_t SymIdx = 0, Type = 0;
29 for (const auto &R : Relocs)
30 OffsetMask |= ToCrel(R).r_offset;
31 const int Shift = llvm::countr_zero(OffsetMask);
32 encodeULEB128(Relocs.size() * 8 + ELF::CREL_HDR_ADDEND + Shift, OS);
33 for (const auto &R : Relocs) {
34 auto CR = ToCrel(R);
35 auto DeltaOffset = static_cast<uint>((CR.r_offset - Offset) >> Shift);
36 Offset = CR.r_offset;
37 uint8_t B = (DeltaOffset << 3) + (SymIdx != CR.r_symidx) +
38 (Type != CR.r_type ? 2 : 0) +
39 (Addend != uint(CR.r_addend) ? 4 : 0);
40 if (DeltaOffset < 0x10) {
41 OS << char(B);
42 } else {
43 OS << char(B | 0x80);
44 encodeULEB128(DeltaOffset >> 4, OS);
45 }
46 // Delta symidx/type/addend members (SLEB128).
47 if (B & 1) {
48 encodeSLEB128(static_cast<int32_t>(CR.r_symidx - SymIdx), OS);
49 SymIdx = CR.r_symidx;
50 }
51 if (B & 2) {
52 encodeSLEB128(static_cast<int32_t>(CR.r_type - Type), OS);
53 Type = CR.r_type;
54 }
55 if (B & 4) {
56 encodeSLEB128(std::make_signed_t<uint>(CR.r_addend - Addend), OS);
57 Addend = CR.r_addend;
58 }
59 }
60}
61} // namespace llvm::ELF
62
63#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define F(x, y, z)
Definition: MD5.cpp:55
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file implements the C++20 <bit> header.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Definition: ELF.h:28
void encodeCrel(raw_ostream &OS, RelocsTy Relocs, F ToCrel)
Definition: MCELFExtras.h:25
constexpr unsigned CREL_HDR_ADDEND
Definition: ELF.h:1979
@ Offset
Definition: DWP.cpp:480
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80