LLVM  8.0.1
Macros | Enumerations | Functions
MipsELFObjectWriter.cpp File Reference
#include "MCTargetDesc/MipsFixupKinds.h"
#include "MCTargetDesc/MipsMCTargetDesc.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <list>
#include <utility>
Include dependency graph for MipsELFObjectWriter.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "mips-elf-object-writer"
 

Enumerations

enum  FindBestPredicateResult
 The possible results of the Predicate function used by find_best. More...
 

Functions

template<class InputIt , class OutputIt1 , class OutputIt2 , class UnaryPredicate >
static std::pair< OutputIt1, OutputIt2 > copy_if_else (InputIt First, InputIt Last, OutputIt1 d1, OutputIt2 d2, UnaryPredicate Predicate)
 Copy elements in the range [First, Last) to d1 when the predicate is true or d2 when the predicate is false. More...
 
template<class InputIt , class UnaryPredicate , class Comparator >
static InputIt find_best (InputIt First, InputIt Last, UnaryPredicate Predicate, Comparator BetterThan)
 Find the best match in the range [First, Last). More...
 
static unsigned getMatchingLoType (const ELFRelocationEntry &Reloc)
 Determine the low relocation that matches the given relocation. More...
 
static FindBestPredicateResult isMatchingReloc (const MipsRelocationEntry &X, const ELFRelocationEntry &R, unsigned MatchingType)
 Determine whether a relocation (X) matches the one given in R. More...
 
static bool compareMatchingRelocs (const MipsRelocationEntry &Candidate, const MipsRelocationEntry &PreviousBest)
 Determine whether Candidate or PreviousBest is the better match. More...
 
template<class Container >
static void dumpRelocs (const char *Prefix, const Container &Relocs)
 Print all the relocations. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "mips-elf-object-writer"

Definition at line 32 of file MipsELFObjectWriter.cpp.

Enumeration Type Documentation

◆ FindBestPredicateResult

The possible results of the Predicate function used by find_best.

Definition at line 73 of file MipsELFObjectWriter.cpp.

Function Documentation

◆ compareMatchingRelocs()

static bool compareMatchingRelocs ( const MipsRelocationEntry &  Candidate,
const MipsRelocationEntry &  PreviousBest 
)
static

Determine whether Candidate or PreviousBest is the better match.

The return value is true if Candidate is the better match.

A matching relocation is a better match if:

  • It has a smaller addend.
  • It is not already involved in a match.

Definition at line 198 of file MipsELFObjectWriter.cpp.

Referenced by dumpRelocs().

◆ copy_if_else()

template<class InputIt , class OutputIt1 , class OutputIt2 , class UnaryPredicate >
static std::pair<OutputIt1, OutputIt2> copy_if_else ( InputIt  First,
InputIt  Last,
OutputIt1  d1,
OutputIt2  d2,
UnaryPredicate  Predicate 
)
static

Copy elements in the range [First, Last) to d1 when the predicate is true or d2 when the predicate is false.

This is essentially both std::copy_if and std::remove_copy_if combined into a single pass.

Definition at line 86 of file MipsELFObjectWriter.cpp.

References I.

Referenced by dumpRelocs().

◆ dumpRelocs()

template<class Container >
static void dumpRelocs ( const char Prefix,
const Container &  Relocs 
)
static

Print all the relocations.

Definition at line 208 of file MipsELFObjectWriter.cpp.

References llvm::HexStyle::Asm, assert(), B, compareMatchingRelocs(), copy_if_else(), llvm::dbgs(), llvm::ELF::EM_MIPS, llvm::errs(), find_best(), Fixup, llvm::Mips::fixup_MICROMIPS_26_S1, llvm::Mips::fixup_MICROMIPS_CALL16, llvm::Mips::fixup_MICROMIPS_GOT16, llvm::Mips::fixup_MICROMIPS_GOT_DISP, llvm::Mips::fixup_MICROMIPS_GOT_OFST, llvm::Mips::fixup_MICROMIPS_GOT_PAGE, llvm::Mips::fixup_MICROMIPS_GOTTPREL, llvm::Mips::fixup_MICROMIPS_GPOFF_HI, llvm::Mips::fixup_MICROMIPS_GPOFF_LO, llvm::Mips::fixup_MICROMIPS_HI16, llvm::Mips::fixup_MICROMIPS_HIGHER, llvm::Mips::fixup_MICROMIPS_HIGHEST, llvm::Mips::fixup_MICROMIPS_JALR, llvm::Mips::fixup_MICROMIPS_LO16, llvm::Mips::fixup_MICROMIPS_PC10_S1, llvm::Mips::fixup_MICROMIPS_PC16_S1, llvm::Mips::fixup_MICROMIPS_PC18_S3, llvm::Mips::fixup_MICROMIPS_PC19_S2, llvm::Mips::fixup_MICROMIPS_PC21_S1, llvm::Mips::fixup_MICROMIPS_PC26_S1, llvm::Mips::fixup_MICROMIPS_PC7_S1, llvm::Mips::fixup_MICROMIPS_SUB, llvm::Mips::fixup_MICROMIPS_TLS_DTPREL_HI16, llvm::Mips::fixup_MICROMIPS_TLS_DTPREL_LO16, llvm::Mips::fixup_MICROMIPS_TLS_GD, llvm::Mips::fixup_MICROMIPS_TLS_LDM, llvm::Mips::fixup_MICROMIPS_TLS_TPREL_HI16, llvm::Mips::fixup_MICROMIPS_TLS_TPREL_LO16, llvm::Mips::fixup_Mips_16, llvm::Mips::fixup_Mips_26, llvm::Mips::fixup_Mips_32, llvm::Mips::fixup_Mips_64, llvm::Mips::fixup_Mips_Branch_PCRel, llvm::Mips::fixup_Mips_CALL16, llvm::Mips::fixup_Mips_CALL_HI16, llvm::Mips::fixup_Mips_CALL_LO16, llvm::Mips::fixup_Mips_DTPREL_HI, llvm::Mips::fixup_Mips_DTPREL_LO, llvm::Mips::fixup_Mips_GOT, llvm::Mips::fixup_Mips_GOT_DISP, llvm::Mips::fixup_Mips_GOT_HI16, llvm::Mips::fixup_Mips_GOT_LO16, llvm::Mips::fixup_Mips_GOT_OFST, llvm::Mips::fixup_Mips_GOT_PAGE, llvm::Mips::fixup_Mips_GOTTPREL, llvm::Mips::fixup_Mips_GPOFF_HI, llvm::Mips::fixup_Mips_GPOFF_LO, llvm::Mips::fixup_Mips_GPREL16, llvm::Mips::fixup_Mips_HI16, llvm::Mips::fixup_Mips_HIGHER, llvm::Mips::fixup_Mips_HIGHEST, llvm::Mips::fixup_Mips_JALR, llvm::Mips::fixup_Mips_LO16, llvm::Mips::fixup_Mips_NONE, llvm::Mips::fixup_Mips_PC16, llvm::Mips::fixup_MIPS_PC18_S3, llvm::Mips::fixup_MIPS_PC19_S2, llvm::Mips::fixup_MIPS_PC21_S2, llvm::Mips::fixup_MIPS_PC26_S2, llvm::Mips::fixup_MIPS_PCHI16, llvm::Mips::fixup_MIPS_PCLO16, llvm::Mips::fixup_Mips_SUB, llvm::Mips::fixup_Mips_TLSGD, llvm::Mips::fixup_Mips_TLSLDM, llvm::Mips::fixup_Mips_TPREL_HI, llvm::Mips::fixup_Mips_TPREL_LO, llvm::FK_Data_1, llvm::FK_Data_2, llvm::FK_Data_4, llvm::FK_Data_8, llvm::FK_DTPRel_4, llvm::FK_DTPRel_8, llvm::FK_GPRel_4, llvm::FK_TPRel_4, llvm::FK_TPRel_8, llvm::MCFixup::getKind(), llvm::MCFixup::getLoc(), getMatchingLoType(), getRelocType(), is64Bit(), isMatchingReloc(), llvm::isUInt< 8 >(), Kind, LLVM_DEBUG, LLVM_FALLTHROUGH, llvm_unreachable, llvm::ELFRelocationEntry::Offset, llvm::MCContext::reportError(), llvm::reverse(), llvm::sort(), llvm::ELF::STO_MIPS_MICROMIPS, and X.

◆ find_best()

template<class InputIt , class UnaryPredicate , class Comparator >
static InputIt find_best ( InputIt  First,
InputIt  Last,
UnaryPredicate  Predicate,
Comparator  BetterThan 
)
static

Find the best match in the range [First, Last).

An element matches when Predicate(X) returns FindBest_Match or FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates the search. BetterThan(A, B) is a comparator that returns true when A is a better match than B. The return value is the position of the best match.

This is similar to std::find_if but finds the best of multiple possible matches.

Definition at line 112 of file MipsELFObjectWriter.cpp.

References llvm::dbgs(), I, and LLVM_DEBUG.

Referenced by dumpRelocs().

◆ getMatchingLoType()

static unsigned getMatchingLoType ( const ELFRelocationEntry Reloc)
static

Determine the low relocation that matches the given relocation.

If the relocation does not need a low relocation then the return value is ELF::R_MIPS_NONE.

The relocations that need a matching low part are R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.

Definition at line 142 of file MipsELFObjectWriter.cpp.

References llvm::MCSymbolELF::getBinding(), llvm::ELFRelocationEntry::OriginalSymbol, llvm::ELF::STB_LOCAL, and llvm::ELFRelocationEntry::Type.

Referenced by dumpRelocs().

◆ isMatchingReloc()

static FindBestPredicateResult isMatchingReloc ( const MipsRelocationEntry &  X,
const ELFRelocationEntry R,
unsigned  MatchingType 
)
static

Determine whether a relocation (X) matches the one given in R.

A relocation matches if:

  • It's type matches that of a corresponding low part. This is provided in MatchingType for efficiency.
  • It's based on the same symbol.
  • It's offset of greater or equal to that of the one given in R. It should be noted that this rule assumes the programmer does not use offsets that exceed the alignment of the symbol. The carry-bit will be incorrect if this is not true.

A matching relocation is unbeatable if:

  • It is not already involved in a match.
  • It's offset is exactly that of the one given in R.

Definition at line 179 of file MipsELFObjectWriter.cpp.

References llvm::ELFRelocationEntry::OriginalAddend, and llvm::ELFRelocationEntry::OriginalSymbol.

Referenced by dumpRelocs().