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"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/Support/LEB128.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 #include <cstdint>
19 #include <type_traits>
20
21 namespace llvm::ELF {
22 // Encode relocations as CREL to OS. ToCrel is responsible for converting a
23 // const RelocsTy & to an Elf_Crel.
24 template <bool Is64, class RelocsTy, class F>
encodeCrel(raw_ostream & OS,RelocsTy Relocs,F ToCrel)25 void 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
64